16
16
#include < functional>
17
17
#include < map>
18
18
#include < memory>
19
+ #include < optional>
19
20
#include < petscmat.h>
20
21
#include < petscvec.h>
21
22
#include < span>
@@ -43,7 +44,7 @@ namespace petsc
43
44
// / object.
44
45
template <std::floating_point T>
45
46
Mat create_matrix (const Form<PetscScalar, T>& a,
46
- std::string type = std::string() )
47
+ std::optional<std:: string> type = std::nullopt )
47
48
{
48
49
la::SparsityPattern pattern = fem::create_sparsity_pattern (a);
49
50
pattern.finalize ();
@@ -52,6 +53,7 @@ Mat create_matrix(const Form<PetscScalar, T>& a,
52
53
53
54
// / @brief Initialise a monolithic matrix for an array of bilinear
54
55
// / forms.
56
+ // /
55
57
// / @param[in] a Rectangular array of bilinear forms. The `a(i, j)` form
56
58
// / will correspond to the `(i, j)` block in the returned matrix
57
59
// / @param[in] type The type of PETSc Mat. If empty the PETSc default is
@@ -62,7 +64,7 @@ Mat create_matrix(const Form<PetscScalar, T>& a,
62
64
template <std::floating_point T>
63
65
Mat create_matrix_block (
64
66
const std::vector<std::vector<const Form<PetscScalar, T>*>>& a,
65
- std::string type = std::string() )
67
+ std::optional<std:: string> type = std::nullopt )
66
68
{
67
69
// Extract and check row/column ranges
68
70
std::array<std::vector<std::shared_ptr<const FunctionSpace<T>>>, 2 > V
@@ -191,16 +193,11 @@ Mat create_matrix_block(
191
193
template <std::floating_point T>
192
194
Mat create_matrix_nest (
193
195
const std::vector<std::vector<const Form<PetscScalar, T>*>>& a,
194
- const std::vector<std::vector<std::string>>& types)
196
+ std::optional<std:: vector<std::vector<std::optional<std:: string>>>> types)
195
197
{
196
198
// Extract and check row/column ranges
197
199
auto V = fem::common_function_spaces (extract_function_spaces (a));
198
200
199
- std::vector<std::vector<std::string>> _types (
200
- a.size (), std::vector<std::string>(a.front ().size ()));
201
- if (!types.empty ())
202
- _types = types;
203
-
204
201
// Loop over each form and create matrix
205
202
int rows = a.size ();
206
203
int cols = a.front ().size ();
@@ -212,7 +209,10 @@ Mat create_matrix_nest(
212
209
{
213
210
if (const Form<PetscScalar, T>* form = a[i][j]; form)
214
211
{
215
- mats[i * cols + j] = create_matrix (*form, _types[i][j]);
212
+ if (types)
213
+ mats[i * cols + j] = create_matrix (*form, types->at (i).at (j));
214
+ else
215
+ mats[i * cols + j] = create_matrix (*form, std::nullopt);
216
216
mesh = form->mesh ();
217
217
}
218
218
}
@@ -238,14 +238,14 @@ Mat create_matrix_nest(
238
238
return A;
239
239
}
240
240
241
- // / Initialise monolithic vector. Vector is not zeroed.
241
+ // / @brief Initialise monolithic vector. Vector is not zeroed.
242
242
// /
243
243
// / The caller is responsible for destroying the Mat object
244
244
Vec create_vector_block (
245
245
const std::vector<
246
246
std::pair<std::reference_wrapper<const common::IndexMap>, int >>& maps);
247
247
248
- // / Create nested (VecNest) vector. Vector is not zeroed.
248
+ // / @brief Create nested (VecNest) vector. Vector is not zeroed.
249
249
Vec create_vector_nest (
250
250
const std::vector<
251
251
std::pair<std::reference_wrapper<const common::IndexMap>, int >>& maps);
@@ -283,15 +283,16 @@ void assemble_vector(
283
283
VecGhostRestoreLocalForm (b, &b_local);
284
284
}
285
285
286
- // / Assemble linear form into an already allocated PETSc vector. Ghost
287
- // / contributions are not accumulated (not sent to owner). Caller is
288
- // / responsible for calling VecGhostUpdateBegin/End.
286
+ // / @brief Assemble linear form into an already allocated PETSc vector.
289
287
// /
290
- // / @param[in,out] b The PETsc vector to assemble the form into. The
291
- // / vector must already be initialised with the correct size. The
292
- // / process-local contribution of the form is assembled into this
293
- // / vector. It is not zeroed before assembly.
294
- // / @param[in] L The linear form to assemble
288
+ // / Ghost contributions are not accumulated (not sent to owner). Caller
289
+ // / is responsible for calling `VecGhostUpdateBegin`/`End`.
290
+ // /
291
+ // / @param[in,out] b Vector to assemble the form into. The vector must
292
+ // / already be initialised with the correct size. The process-local
293
+ // / contribution of the form is assembled into this vector. It is not
294
+ // / zeroed before assembly.
295
+ // / @param[in] L Linear form to assemble.
295
296
template <std::floating_point T>
296
297
void assemble_vector (Vec b, const Form<PetscScalar, T>& L)
297
298
{
@@ -462,7 +463,8 @@ void apply_lifting(
462
463
template <std::floating_point T>
463
464
void set_bc (
464
465
Vec b,
465
- const std::vector<std::reference_wrapper<const DirichletBC<PetscScalar, T>>> bcs,
466
+ const std::vector<std::reference_wrapper<const DirichletBC<PetscScalar, T>>>
467
+ bcs,
466
468
const Vec x0, PetscScalar alpha = 1 )
467
469
{
468
470
PetscInt n = 0 ;
0 commit comments