Coverage for python/src/dolfinx_mpc/assemble_vector.py: 100%

57 statements  

« prev     ^ index     » next       coverage.py v7.15.2, created at 2026-07-21 19:42 +0000

1# Copyright (C) 2020 Jørgen S. Dokken 

2# 

3# This file is part of DOLFINX_MPC 

4# 

5# SPDX-License-Identifier: MIT 

6from __future__ import annotations 

7 

8import contextlib 

9from typing import Iterable, Optional, Sequence, Union 

10 

11from petsc4py import PETSc as _PETSc 

12 

13import dolfinx.cpp as _cpp 

14import dolfinx.fem as _fem 

15import numpy 

16from dolfinx import default_scalar_type 

17from dolfinx.common import Timer 

18from dolfinx.la.petsc import create_vector 

19 

20import dolfinx_mpc.cpp 

21 

22from .multipointconstraint import MultiPointConstraint, _float_classes 

23 

24 

25def apply_lifting( 

26 b: _PETSc.Vec, 

27 form: Union[Iterable[Sequence[_fem.Form]], Iterable[_fem.Form]], # type: ignore 

28 bcs: Union[Sequence[_fem.DirichletBC], Sequence[Sequence[_fem.DirichletBC]]], 

29 constraint: Union[MultiPointConstraint, Sequence[MultiPointConstraint]], 

30 x0: Optional[Sequence[_PETSc.Vec]] = None, 

31 scale: _float_classes = default_scalar_type(1.0), # type: ignore 

32 num_threads: Optional[int] = 1, 

33): # type: ignore 

34 """ 

35 Apply lifting to vector b, i.e. 

36 :math:`b = b - scale \\cdot K^T (A_j (g_j - x0_j))` 

37 

38 Args: 

39 b: PETSc vector to assemble into 

40 form: The linear form 

41 bcs: List of Dirichlet boundary conditions 

42 constraint: The multi point constraint 

43 x0: List of vectors 

44 scale: Scaling for lifting 

45 num_threads: The number of threads to use for certain operations 

46 """ 

47 t = Timer("~MPC: Apply lifting (C++)") 

48 if isinstance(scale, numpy.generic): # nanobind conversion of numpy dtypes to general Python types 

49 scale = scale.item() # type: ignore 

50 

51 if b.getType() == "nest": 

52 try: 

53 bcs = _fem.bcs_by_block(_fem.extract_function_spaces(form, 1), bcs) # type: ignore 

54 except AttributeError: 

55 pass 

56 x0 = [] if x0 is None else x0.getNestSubVecs() # type: ignore 

57 assert isinstance(form, Sequence) and isinstance(constraint, Sequence) 

58 for b_sub, a_sub, mpc_i in zip(b.getNestSubVecs(), form, constraint): 

59 _a = [None if form is None else form._cpp_object for form in a_sub] # type:ignore 

60 _bcs = [[bc._cpp_object for bc in bcs0] for bcs0 in bcs] # type: ignore 

61 dolfinx_mpc.cpp.mpc.apply_lifting(b_sub.array_w, _a, _bcs, x0, scale, mpc_i._cpp_object, num_threads) 

62 else: 

63 with contextlib.ExitStack() as stack: 

64 if x0 is None: 

65 x0 = [] 

66 else: 

67 x0 = [stack.enter_context(x.localForm()) for x in x0] 

68 x0_r = [x.array_r for x in x0] 

69 b_local = stack.enter_context(b.localForm()) 

70 _forms = [f._cpp_object for f in form] # type: ignore 

71 _bcs = [[bc._cpp_object for bc in bcs0] for bcs0 in bcs] # type: ignore 

72 assert isinstance(constraint, MultiPointConstraint) 

73 dolfinx_mpc.cpp.mpc.apply_lifting( 

74 b_local.array_w, _forms, _bcs, x0_r, scale, constraint._cpp_object, num_threads 

75 ) 

76 t.stop() 

77 

78 

79def assemble_vector( 

80 form: _fem.Form, 

81 constraint: MultiPointConstraint, 

82 b: Optional[_PETSc.Vec] = None, # type: ignore 

83 num_threads: Optional[int] = 1, 

84) -> _PETSc.Vec: # type: ignore 

85 """ 

86 Assemble a linear form into vector `b` with corresponding multi point constraint 

87 

88 Args: 

89 form: The linear form 

90 constraint: The multi point constraint 

91 b: PETSc vector to assemble 

92 

93 Returns: 

94 The vector with the assembled linear form (`b` if supplied) 

95 """ 

96 

97 if b is None: 

98 b = create_vector([(constraint.function_space.dofmap.index_map, constraint.function_space.dofmap.index_map_bs)]) 

99 t = Timer("~MPC: Assemble vector (C++)") 

100 with b.localForm() as b_local: 

101 b_local.set(0.0) 

102 dolfinx_mpc.cpp.mpc.assemble_vector(b_local.array_w, form._cpp_object, constraint._cpp_object, num_threads) 

103 t.stop() 

104 return b 

105 

106 

107def create_vector_nest(L: Sequence[_fem.Form], constraints: Sequence[MultiPointConstraint]) -> _PETSc.Vec: # type: ignore 

108 """ 

109 Create a PETSc vector of type "nest" appropriate for the provided multi 

110 point constraints 

111 

112 Args: 

113 L: A sequence of linear forms 

114 constraints: An ordered list of multi point constraints 

115 

116 Returns: 

117 PETSc.Vec: A PETSc vector of type "nest" #type: ignore 

118 """ 

119 assert len(constraints) == len(L) 

120 

121 maps = [ 

122 (constraint.function_space.dofmap.index_map, constraint.function_space.dofmap.index_map_bs) 

123 for constraint in constraints 

124 ] 

125 return _cpp.fem.petsc.create_vector_nest(maps) 

126 

127 

128def assemble_vector_nest( 

129 b: _PETSc.Vec, # type: ignore 

130 L: Sequence[_fem.Form], 

131 constraints: Sequence[MultiPointConstraint], 

132 num_threads: Optional[int] = 1, 

133): 

134 """ 

135 Assemble a linear form into a PETSc vector of type "nest" 

136 

137 Args: 

138 b: A PETSc vector of type "nest" 

139 L: A sequence of linear forms 

140 constraints: An ordered list of multi point constraints 

141 """ 

142 assert len(constraints) == len(L) 

143 assert b.getType() == "nest" 

144 

145 b_sub_vecs = b.getNestSubVecs() 

146 for i, L_row in enumerate(L): 

147 assemble_vector(L_row, constraints[i], b=b_sub_vecs[i], num_threads=num_threads)