Skip to content

petsc_to_psydac keeps stale values in out where the PETSc vector is zero #640

Description

@yguclu

Problem

psydac.linalg.utilities.petsc_to_psydac(x, Xh, out=None) converts a PETSc vector x to a PSYDAC vector. If out is given, the result is written into it, but only the non-zero entries of x are copied (if value != 0:), and out is not zeroed beforehand. Hence, wherever x is zero, out keeps its previous values, and the result is wrong. This happens for both StencilVector and BlockVector outputs.

Reproducer

import numpy as np
from psydac.ddm.cart import DomainDecomposition, CartDecomposition
from psydac.linalg.stencil import StencilVectorSpace
from psydac.linalg.block import BlockVectorSpace
from psydac.linalg.topetsc import vec_topetsc
from psydac.linalg.utilities import array_to_psydac, petsc_to_psydac

def stencil_space(n, p=1):
    dd = DomainDecomposition([n - p], [False])
    cart = CartDecomposition(dd, [n], [np.array([0])], [np.array([n - 1])], [p], [1])
    return StencilVectorSpace(cart)

V = stencil_space(6)
W = BlockVectorSpace(stencil_space(6), stencil_space(4))

for space, values in [(V, [1., 0., 2., 0., 3., 0.]),
                      (W, [1., 0., 2., 0., 3., 0., 0., 4., 0., 5.])]:
    x = array_to_psydac(np.array(values), space)
    out = array_to_psydac(np.full(space.dimension, 9.), space)  # stale values
    petsc_to_psydac(vec_topetsc(x), space, out=out)
    print(type(x).__name__)
    print('  x  :', x.toarray())
    print('  out:', out.toarray())

Output:

StencilVector
  x  : [1. 0. 2. 0. 3. 0.]
  out: [1. 9. 2. 9. 3. 9.]
BlockVector
  x  : [1. 0. 2. 0. 3. 0. 0. 4. 0. 5.]
  out: [1. 9. 2. 9. 3. 9. 9. 4. 9. 5.]

In both cases out should be equal to x.

Proposed solution

Copy all the local entries of x into out, including the zeros. PR #592, which vectorizes petsc_to_psydac, already does this; a regression test with a pre-filled out should be added there.

Activity

  1. added
    linalg
    on Nov 5, 2026
  2. added theissue type on Nov 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    PETSclinalg

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions