Skip to content

Commit f4d0bc0

Browse files
committed
fix: petsc...
1 parent 55974d6 commit f4d0bc0

1 file changed

Lines changed: 52 additions & 46 deletions

File tree

python/dolfinx/fem/petsc.py

Lines changed: 52 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ def create_matrix(
194194
else: # Array of 'kind' types
195195
return _cpp.fem.petsc.create_matrix_nest(_a, kind)
196196
else: # Single form
197-
return _cpp.fem.petsc.create_matrix(a._cpp_object, kind)
197+
return _cpp.fem.petsc.create_matrix(a._cpp_object, kind) # type: ignore
198198

199199

200200
# -- Vector assembly ------------------------------------------------------
@@ -264,7 +264,7 @@ def assemble_vector(
264264

265265

266266
@assemble_vector.register
267-
def _(
267+
def _( # type: ignore
268268
b: PETSc.Vec, # type: ignore[name-defined]
269269
L: Form | Sequence[Form],
270270
constants: npt.NDArray | Sequence[npt.NDArray] | None = None,
@@ -315,10 +315,16 @@ def _(
315315
elif isinstance(L, Sequence):
316316
constants = pack_constants(L) if constants is None else constants
317317
coeffs = pack_coefficients(L) if coeffs is None else coeffs
318-
offset0, offset1 = b.getAttr("_blocks")
318+
offset0, offset1 = b.getAttr("_blocks") # type: ignore
319319
with b.localForm() as b_l:
320320
for L_, const, coeff, off0, off1, offg0, offg1 in zip(
321-
L, constants, coeffs, offset0, offset0[1:], offset1, offset1[1:]
321+
L,
322+
constants,
323+
coeffs,
324+
offset0, # type: ignore
325+
offset0[1:], # type: ignore
326+
offset1, # type: ignore
327+
offset1[1:], # type: ignore
322328
):
323329
bx_ = np.zeros((off1 - off0) + (offg1 - offg0), dtype=PETSc.ScalarType) # type: ignore[attr-defined]
324330
_assemble_vector_array(bx_, L_, const, coeff) # type: ignore[arg-type]
@@ -402,8 +408,8 @@ def assemble_matrix(
402408

403409

404410
@assemble_matrix.register
405-
def _(
406-
A: PETSc.Mat, # type: ignore[name-defined]
411+
def _( # type: ignore
412+
A: PETSc.Mat,
407413
a: Form | Sequence[Sequence[Form]],
408414
bcs: Sequence[DirichletBC] | None = None,
409415
diag: float = 1,
@@ -467,7 +473,7 @@ def _(
467473
for i, a_row in enumerate(a):
468474
for j, a_sub in enumerate(a_row):
469475
if a_sub is not None:
470-
Asub = A.getLocalSubMatrix(is0[i], is1[j])
476+
Asub = A.getLocalSubMatrix(is0[i], is1[j]) # type: ignore
471477
_cpp.fem.petsc.assemble_matrix(
472478
Asub,
473479
a_sub._cpp_object,
@@ -476,7 +482,7 @@ def _(
476482
_bcs,
477483
True,
478484
)
479-
A.restoreLocalSubMatrix(is0[i], is1[j], Asub)
485+
A.restoreLocalSubMatrix(is0[i], is1[j], Asub) # type: ignore
480486
elif i == j:
481487
for bc in _bcs:
482488
row_forms = [row_form for row_form in a_row if row_form is not None]
@@ -495,15 +501,15 @@ def _(
495501
for i, a_row in enumerate(a):
496502
for j, a_sub in enumerate(a_row):
497503
if a_sub is not None:
498-
Asub = A.getLocalSubMatrix(is0[i], is1[j])
504+
Asub = A.getLocalSubMatrix(is0[i], is1[j]) # type: ignore
499505
if a_sub.function_spaces[0] is a_sub.function_spaces[1]:
500506
_cpp.fem.petsc.insert_diagonal(Asub, a_sub.function_spaces[0], _bcs, diag)
501-
A.restoreLocalSubMatrix(is0[i], is1[j], Asub)
507+
A.restoreLocalSubMatrix(is0[i], is1[j], Asub) # type: ignore
502508
else: # Non-blocked
503509
constants = pack_constants(a) if constants is None else constants # type: ignore[assignment]
504510
coeffs = pack_coefficients(a) if coeffs is None else coeffs # type: ignore[assignment]
505511
_bcs = [bc._cpp_object for bc in bcs] if bcs is not None else []
506-
_cpp.fem.petsc.assemble_matrix(A, a._cpp_object, constants, coeffs, _bcs)
512+
_cpp.fem.petsc.assemble_matrix(A, a._cpp_object, constants, coeffs, _bcs) # type: ignore
507513
if a.function_spaces[0] is a.function_spaces[1]:
508514
A.assemblyBegin(PETSc.Mat.AssemblyType.FLUSH) # type: ignore[attr-defined]
509515
A.assemblyEnd(PETSc.Mat.AssemblyType.FLUSH) # type: ignore[attr-defined]
@@ -605,7 +611,7 @@ def apply_lifting(
605611
else:
606612
xlocal = None
607613

608-
offset0, offset1 = b.getAttr("_blocks")
614+
offset0, offset1 = b.getAttr("_blocks") # type: ignore
609615
with b.localForm() as b_l:
610616
for i, (a_, off0, off1, offg0, offg1) in enumerate(
611617
zip(a, offset0, offset0[1:], offset1, offset1[1:])
@@ -628,8 +634,6 @@ def apply_lifting(
628634
b_local = stack.enter_context(b.localForm())
629635
_apply_lifting(b_local.array_w, a, bcs, x0_r, alpha, constants, coeffs) # type: ignore[arg-type]
630636

631-
return b
632-
633637

634638
def set_bc(
635639
b: PETSc.Vec, # type: ignore[name-defined]
@@ -663,19 +667,19 @@ def set_bc(
663667
return
664668

665669
if not isinstance(bcs[0], Sequence):
666-
x0 = x0.array_r if x0 is not None else None
670+
x0 = x0.array_r if x0 is not None else None # type: ignore
667671
for bc in bcs:
668-
bc.set(b.array_w, x0, alpha) # type: ignore[union-attr]
672+
bc.set(b.array_w, x0, alpha) # type: ignore
669673
elif b.getType() == PETSc.Vec.Type.NEST: # type: ignore[attr-defined]
670674
_b = b.getNestSubVecs()
671-
x0 = len(_b) * [None] if x0 is None else x0.getNestSubVecs()
672-
for b_sub, bc, x_sub in zip(_b, bcs, x0): # type: ignore[assignment, arg-type]
675+
x0 = len(_b) * [None] if x0 is None else x0.getNestSubVecs() # type: ignore
676+
for b_sub, bc, x_sub in zip(_b, bcs, x0): # type: ignore
673677
set_bc(b_sub, bc, x_sub, alpha) # type: ignore[arg-type]
674678
else: # block vector
675-
offset0, _ = b.getAttr("_blocks")
679+
offset0, _ = b.getAttr("_blocks") # type: ignore
676680
b_array = b.getArray(readonly=False)
677681
x_array = x0.getArray(readonly=True) if x0 is not None else None
678-
for bcs, off0, off1 in zip(bcs, offset0, offset0[1:]): # type: ignore[assignment]
682+
for bcs, off0, off1 in zip(bcs, offset0, offset0[1:]): # type: ignore
679683
x0_sub = x_array[off0:off1] if x0 is not None else None # type: ignore[index]
680684
for bc in bcs:
681685
bc.set(b_array[off0:off1], x0_sub, alpha) # type: ignore[arg-type, union-attr]
@@ -855,13 +859,13 @@ def __init__(
855859
opts.prefixPush(self.solver.getOptionsPrefix())
856860

857861
for k, v in petsc_options.items():
858-
opts[k] = v
862+
opts[k] = v # type: ignore
859863

860864
self.solver.setFromOptions()
861865

862866
# Tidy up global options
863867
for k in petsc_options.keys():
864-
del opts[k]
868+
del opts[k] # type: ignore
865869

866870
opts.prefixPop()
867871

@@ -921,17 +925,17 @@ def solve(self) -> _Function | Sequence[_Function]:
921925
apply_lifting(self.b, self.a, bcs=bcs1) # type: ignore[arg-type]
922926
dolfinx.la.petsc._ghost_update(
923927
self.b,
924-
PETSc.InsertMode.ADD, # type: ignore[attr-defined]
925-
PETSc.ScatterMode.REVERSE, # type: ignore[attr-defined]
928+
PETSc.InsertMode.ADD, # type: ignore
929+
PETSc.ScatterMode.REVERSE, # type: ignore
926930
)
927931
bcs0 = _bcs_by_block(_extract_function_spaces(self.L), self.bcs) # type: ignore[arg-type]
928932
dolfinx.fem.petsc.set_bc(self.b, bcs0)
929933
else: # single form
930934
apply_lifting(self.b, [self.a], bcs=[self.bcs]) # type: ignore[arg-type]
931935
dolfinx.la.petsc._ghost_update(
932936
self.b,
933-
PETSc.InsertMode.ADD, # type: ignore[attr-defined]
934-
PETSc.ScatterMode.REVERSE, # type: ignore[attr-defined]
937+
PETSc.InsertMode.ADD, # type: ignore
938+
PETSc.ScatterMode.REVERSE, # type: ignore
935939
)
936940
for bc in self.bcs:
937941
bc.set(self.b.array_w)
@@ -940,8 +944,8 @@ def solve(self) -> _Function | Sequence[_Function]:
940944

941945
# Solve linear system and update ghost values in the solution
942946
self.solver.solve(self.b, self.x)
943-
dolfinx.la.petsc._ghost_update(self.x, PETSc.InsertMode.INSERT, PETSc.ScatterMode.FORWARD) # type: ignore[attr-defined]
944-
dolfinx.fem.petsc.assign(self.x, self.u)
947+
dolfinx.la.petsc._ghost_update(self.x, PETSc.InsertMode.INSERT, PETSc.ScatterMode.FORWARD) # type: ignore
948+
dolfinx.fem.petsc.assign(self.x, self.u) # type: ignore
945949
return self.u
946950

947951
@property
@@ -965,7 +969,7 @@ def A(self) -> PETSc.Mat: # type: ignore[name-defined]
965969
return self._A
966970

967971
@property
968-
def P_mat(self) -> PETSc.Mat: # type: ignore[name-defined]
972+
def P_mat(self) -> PETSc.Mat | None: # type: ignore[name-defined]
969973
"""Preconditioner matrix."""
970974
return self._P_mat
971975

@@ -1043,10 +1047,10 @@ def assemble_residual(
10431047
format of this argument.
10441048
"""
10451049
# Update input vector before assigning
1046-
dolfinx.la.petsc._ghost_update(x, PETSc.InsertMode.INSERT, PETSc.ScatterMode.FORWARD) # type: ignore[attr-defined]
1050+
dolfinx.la.petsc._ghost_update(x, PETSc.InsertMode.INSERT, PETSc.ScatterMode.FORWARD) # type: ignore
10471051

10481052
# Assign the input vector to the unknowns
1049-
assign(x, u)
1053+
assign(x, u) # type: ignore
10501054

10511055
# Assign block data if block assembly is requested
10521056
if isinstance(residual, Sequence) and b.getType() != PETSc.Vec.Type.NEST: # type: ignore[attr-defined]
@@ -1062,16 +1066,16 @@ def assemble_residual(
10621066
if isinstance(jacobian, Sequence):
10631067
# Nest and blocked lifting
10641068
bcs1 = _bcs_by_block(_extract_function_spaces(jacobian, 1), bcs) # type: ignore[arg-type]
1065-
apply_lifting(b, jacobian, bcs=bcs1, x0=x, alpha=-1.0)
1066-
dolfinx.la.petsc._ghost_update(b, PETSc.InsertMode.ADD, PETSc.ScatterMode.REVERSE) # type: ignore[attr-defined]
1069+
apply_lifting(b, jacobian, bcs=bcs1, x0=x, alpha=-1.0) # type: ignore
1070+
dolfinx.la.petsc._ghost_update(b, PETSc.InsertMode.ADD, PETSc.ScatterMode.REVERSE) # type: ignore
10671071
bcs0 = _bcs_by_block(_extract_function_spaces(residual), bcs) # type: ignore[arg-type]
10681072
set_bc(b, bcs0, x0=x, alpha=-1.0)
10691073
else:
10701074
# Single form lifting
10711075
apply_lifting(b, [jacobian], bcs=[bcs], x0=[x], alpha=-1.0)
1072-
dolfinx.la.petsc._ghost_update(b, PETSc.InsertMode.ADD, PETSc.ScatterMode.REVERSE) # type: ignore[attr-defined]
1076+
dolfinx.la.petsc._ghost_update(b, PETSc.InsertMode.ADD, PETSc.ScatterMode.REVERSE) # type: ignore
10731077
set_bc(b, bcs, x0=x, alpha=-1.0)
1074-
dolfinx.la.petsc._ghost_update(b, PETSc.InsertMode.INSERT, PETSc.ScatterMode.FORWARD) # type: ignore[attr-defined]
1078+
dolfinx.la.petsc._ghost_update(b, PETSc.InsertMode.INSERT, PETSc.ScatterMode.FORWARD) # type: ignore
10751079

10761080

10771081
def assemble_jacobian(
@@ -1114,8 +1118,8 @@ def assemble_jacobian(
11141118
"""
11151119
# Copy existing soultion into the function used in the residual and
11161120
# Jacobian
1117-
dolfinx.la.petsc._ghost_update(x, PETSc.InsertMode.INSERT, PETSc.ScatterMode.FORWARD) # type: ignore[attr-defined]
1118-
assign(x, u)
1121+
dolfinx.la.petsc._ghost_update(x, PETSc.InsertMode.INSERT, PETSc.ScatterMode.FORWARD) # type: ignore
1122+
assign(x, u) # type: ignore
11191123

11201124
# Assemble Jacobian
11211125
J.zeroEntries()
@@ -1146,6 +1150,8 @@ class NonlinearProblem:
11461150
``.destroy()`` on returned PETSc objects.
11471151
""" # noqa: D301
11481152

1153+
_P_mat: PETSc.Mat | None
1154+
11491155
def __init__(
11501156
self,
11511157
F: ufl.form.Form | Sequence[ufl.form.Form],
@@ -1303,13 +1309,13 @@ def __init__(
13031309
opts.prefixPush(self.solver.getOptionsPrefix())
13041310

13051311
for k, v in petsc_options.items():
1306-
opts[k] = v
1312+
opts[k] = v # type: ignore
13071313

13081314
self.solver.setFromOptions()
13091315

13101316
# Tidy up global options
13111317
for k in petsc_options.keys():
1312-
del opts[k]
1318+
del opts[k] # type: ignore
13131319

13141320
opts.prefixPop()
13151321

@@ -1346,10 +1352,10 @@ def solve(self) -> _Function | Sequence[_Function]:
13461352

13471353
# Solve problem
13481354
self.solver.solve(None, self.x)
1349-
dolfinx.la.petsc._ghost_update(self.x, PETSc.InsertMode.INSERT, PETSc.ScatterMode.FORWARD) # type: ignore[attr-defined]
1355+
dolfinx.la.petsc._ghost_update(self.x, PETSc.InsertMode.INSERT, PETSc.ScatterMode.FORWARD) # type: ignore
13501356

13511357
# Copy solution back to function
1352-
assign(self.x, self.u)
1358+
assign(self.x, self.u) # type: ignore
13531359

13541360
return self.u
13551361

@@ -1518,7 +1524,7 @@ def F(self, x: PETSc.Vec, b: PETSc.Vec) -> None: # type: ignore[name-defined]
15181524
"""
15191525
# Reset the residual vector
15201526
dolfinx.la.petsc._zero_vector(b)
1521-
assemble_vector(b, self._L)
1527+
assemble_vector(b, self._L) # type: ignore
15221528

15231529
# Apply boundary condition
15241530
if self.bcs is not None:
@@ -1629,7 +1635,7 @@ def assign(u: _Function | Sequence[_Function], x: PETSc.Vec): # type: ignore[na
16291635

16301636

16311637
@assign.register
1632-
def _(x: PETSc.Vec, u: _Function | Sequence[_Function]): # type: ignore[name-defined]
1638+
def _(x: PETSc.Vec, u: _Function | Sequence[_Function]): # type: ignore[name-defined, misc]
16331639
"""Assign vector entries to :class:`Function` degrees-of-freedom.
16341640
16351641
Assigns values in ``x`` to the degrees-of-freedom of ``u``, which is
@@ -1643,7 +1649,7 @@ def _(x: PETSc.Vec, u: _Function | Sequence[_Function]): # type: ignore[name-de
16431649
u: ``Function`` (s) to assign degree-of-freedom values to.
16441650
"""
16451651
if x.getType() == PETSc.Vec.Type().NEST: # type: ignore[attr-defined]
1646-
dolfinx.la.petsc.assign(x, [v.x.array for v in u])
1652+
dolfinx.la.petsc.assign(x, [v.x.array for v in u]) # type: ignore
16471653
else:
16481654
if isinstance(u, Sequence):
16491655
data0, data1 = [], []
@@ -1652,9 +1658,9 @@ def _(x: PETSc.Vec, u: _Function | Sequence[_Function]): # type: ignore[name-de
16521658
n = v.function_space.dofmap.index_map.size_local
16531659
data0.append(v.x.array[: bs * n])
16541660
data1.append(v.x.array[bs * n :])
1655-
dolfinx.la.petsc.assign(x, data0 + data1)
1661+
dolfinx.la.petsc.assign(x, data0 + data1) # type: ignore
16561662
else:
1657-
dolfinx.la.petsc.assign(x, u.x.array)
1663+
dolfinx.la.petsc.assign(x, u.x.array) # type: ignore
16581664

16591665

16601666
def get_petsc_lib() -> pathlib.Path:

0 commit comments

Comments
 (0)