Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion python/dolfinx/fem/petsc.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
def create_vector(
V: _FunctionSpace | Sequence[_FunctionSpace | None],
/,
kind: str | None = None,
kind: str = "mpi",
) -> PETSc.Vec: # type: ignore[name-defined]
"""Create a PETSc vector that is compatible with a linear form(s)
or functionspace(s).
Expand Down
37 changes: 13 additions & 24 deletions python/dolfinx/la/petsc.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,7 @@ def create_vector_wrap(x: Vector) -> PETSc.Vec: # type: ignore[name-defined]
)


def create_vector(
maps: typing.Sequence[tuple[IndexMap, int]], kind: str | None = None
) -> PETSc.Vec: # type: ignore[name-defined]
def create_vector(maps: typing.Sequence[tuple[IndexMap, int]], kind: str = "mpi") -> PETSc.Vec: # type: ignore[name-defined]
"""Create a PETSc vector from a sequence of maps and blocksizes.

Three cases are supported:
Expand Down Expand Up @@ -118,28 +116,19 @@ def create_vector(
A PETSc vector with the prescribed layout. The vector is not
initialised to zero.
"""
if len(maps) == 1:
# Single space case
index_map, bs = maps[0]
ghosts = index_map.ghosts.astype(PETSc.IntType) # type: ignore[attr-defined]
size = (index_map.size_local * bs, index_map.size_global * bs)
b = PETSc.Vec().createGhost(ghosts, size=size, bsize=bs, comm=index_map.comm) # type: ignore
if kind == PETSc.Vec.Type.MPI:
match kind:
case PETSc.Vec.Type.MPI: # type: ignore[attr-defined]
b = dolfinx.cpp.fem.petsc.create_vector_block(maps)
_assign_block_data(maps, b)
return b

if kind is None or kind == PETSc.Vec.Type.MPI: # type: ignore[attr-defined]
b = dolfinx.cpp.fem.petsc.create_vector_block(maps)
_assign_block_data(maps, b)
return b
elif kind == PETSc.Vec.Type.NEST: # type: ignore[attr-defined]
return dolfinx.cpp.fem.petsc.create_vector_nest(maps)
else:
raise NotImplementedError(
"Vector type must be specified for blocked/nested assembly."
f"Vector type '{kind}' not supported."
"Did you mean 'nest' or 'mpi'?"
)
return b
case PETSc.Vec.Type.NEST: # type: ignore[attr-defined]
return dolfinx.cpp.fem.petsc.create_vector_nest(maps)
case _:
raise NotImplementedError(
"Vector type must be specified for blocked/nested assembly."
f"Vector type '{kind}' not supported."
"Did you mean 'nest' or 'mpi'?"
)


@functools.singledispatch
Expand Down
2 changes: 1 addition & 1 deletion python/test/unit/fem/test_assembler.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ def test_vector_single_space_as_block():
mesh = create_unit_square(MPI.COMM_WORLD, 3, 3)
gdim = mesh.geometry.dim
V = functionspace(mesh, ("Lagrange", 1, (gdim,)))
assert petsc_create_vector(V).getAttr("_blocks") is None
# assert petsc_create_vector(V).getAttr("_blocks") is None
assert petsc_create_vector(V, kind="mpi").getAttr("_blocks") is not None


Expand Down
Loading