17#include <dolfinx/la/petsc.h>
38template <dolfinx::scalar T, std::
floating_po
int U>
46#define CHECK_ERROR(NAME) \
50 la::petsc::error(ierr, __FILE__, NAME); \
59template <std::
floating_po
int T>
61 std::optional<std::string> type = std::nullopt)
78template <std::
floating_po
int T>
81 std::optional<std::string> type = std::nullopt)
84 std::array<std::vector<std::shared_ptr<const FunctionSpace<T>>>, 2> V
86 std::array<std::vector<int>, 2> bs_dofs;
87 for (std::size_t i = 0; i < 2; ++i)
90 bs_dofs[i].push_back(_V->dofmap()->bs());
94 std::shared_ptr<const mesh::Mesh<T>>
mesh;
95 std::vector<std::vector<std::unique_ptr<la::SparsityPattern>>> patterns(
97 for (std::size_t row = 0; row < V[0].size(); ++row)
99 for (std::size_t col = 0; col < V[1].size(); ++col)
103 patterns[row].push_back(std::make_unique<la::SparsityPattern>(
109 patterns[row].push_back(
nullptr);
114 throw std::runtime_error(
"Could not find a Mesh.");
117 std::array<std::vector<std::pair<
118 std::reference_wrapper<const common::IndexMap>,
int>>,
121 for (std::size_t d = 0; d < 2; ++d)
123 for (
auto& space : V[d])
125 maps[d].emplace_back(*space->dofmap()->index_map,
126 space->dofmap()->index_map_bs());
131 std::vector<std::vector<const la::SparsityPattern*>> p(V[0].size());
132 for (std::size_t row = 0; row < V[0].size(); ++row)
133 for (std::size_t col = 0; col < V[1].size(); ++col)
134 p[row].push_back(patterns[row][col].get());
152 std::array<std::vector<PetscInt>, 2> _maps;
153 for (
int d = 0; d < 2; ++d)
155 if (d == 1 and V[0] == V[1])
166 std::pair<std::reference_wrapper<const common::IndexMap>,
int>>& map
168 std::vector<PetscInt>& _map = _maps[d];
171 const auto [rank_offset, local_offset, ghosts, _]
173 const std::size_t num_ghosts
174 = std::accumulate(ghosts.begin(), ghosts.end(), std::size_t(0),
175 [](std::size_t n,
auto& g) { return n + g.size(); });
176 _map.reserve(local_offset.back() + num_ghosts);
177 for (std::size_t f = 0; f < map.size(); ++f)
179 auto offset = local_offset[f];
181 int bs = map[f].second;
183 = std::views::iota(std::int32_t(0), bs * imap.
size_local())
184 | std::views::transform([offset, rank_offset](std::int32_t i)
185 {
return i + rank_offset + offset; });
186 _map.insert(_map.end(), owned.begin(), owned.end());
187 _map.insert(_map.end(), ghosts[f].begin(), ghosts[f].end());
192 ISLocalToGlobalMapping petsc_local_to_global0;
193 PetscErrorCode ierr = ISLocalToGlobalMappingCreate(
194 MPI_COMM_SELF, 1, _maps[0].size(), _maps[0].data(), PETSC_COPY_VALUES,
195 &petsc_local_to_global0);
196 CHECK_ERROR(
"ISLocalToGlobalMappingCreate");
199 ierr = MatSetLocalToGlobalMapping(A, petsc_local_to_global0,
200 petsc_local_to_global0);
201 CHECK_ERROR(
"MatSetLocalToGlobalMapping");
202 ierr = ISLocalToGlobalMappingDestroy(&petsc_local_to_global0);
203 CHECK_ERROR(
"ISLocalToGlobalMappingDestroy");
207 ISLocalToGlobalMapping petsc_local_to_global1;
208 ierr = ISLocalToGlobalMappingCreate(MPI_COMM_SELF, 1, _maps[1].size(),
209 _maps[1].data(), PETSC_COPY_VALUES,
210 &petsc_local_to_global1);
211 CHECK_ERROR(
"ISLocalToGlobalMappingCreate");
212 ierr = MatSetLocalToGlobalMapping(A, petsc_local_to_global0,
213 petsc_local_to_global1);
214 CHECK_ERROR(
"MatSetLocalToGlobalMapping");
215 ierr = ISLocalToGlobalMappingDestroy(&petsc_local_to_global0);
216 CHECK_ERROR(
"ISLocalToGlobalMappingDestroy");
217 ierr = ISLocalToGlobalMappingDestroy(&petsc_local_to_global1);
218 CHECK_ERROR(
"ISLocalToGlobalMappingDestroy");
227template <std::
floating_po
int T>
230 std::optional<std::vector<std::vector<std::optional<std::string>>>> types)
233 throw std::runtime_error(
"Rectangular array of forms must be non-empty.");
240 int cols = a.front().size();
241 std::vector<Mat> mats(rows * cols,
nullptr);
242 std::shared_ptr<const mesh::Mesh<T>>
mesh;
243 for (
int i = 0; i < rows; ++i)
245 for (
int j = 0; j < cols; ++j)
250 mats[i * cols + j] =
create_matrix(*form, types->at(i).at(j));
259 throw std::runtime_error(
"Could not find a Mesh.");
268 PetscErrorCode ierr = MatCreate(
mesh->comm(), &A);
269 CHECK_ERROR(
"MatCreate");
270 ierr = MatSetType(A, MATNEST);
271 CHECK_ERROR(
"MatSetType");
272 ierr = MatNestSetSubMats(A, rows,
nullptr, cols,
nullptr, mats.data());
273 CHECK_ERROR(
"MatNestSetSubMats");
275 CHECK_ERROR(
"MatSetUp");
298 std::pair<std::reference_wrapper<const common::IndexMap>,
int>>& maps);
303 std::pair<std::reference_wrapper<const common::IndexMap>,
int>>& maps);
319template <std::
floating_po
int T>
322 std::span<const PetscScalar> constants,
323 const std::map<std::pair<IntegralType, int>,
324 std::pair<std::span<const PetscScalar>,
int>>& coeffs)
327 PetscErrorCode ierr = VecGhostGetLocalForm(b, &b_local);
328 CHECK_ERROR(
"VecGhostGetLocalForm");
330 ierr = VecGetSize(b_local, &n);
331 CHECK_ERROR(
"VecGetSize");
332 PetscScalar* array =
nullptr;
333 ierr = VecGetArray(b_local, &array);
334 CHECK_ERROR(
"VecGetArray");
335 std::span<PetscScalar> _b(array, n);
337 ierr = VecRestoreArray(b_local, &array);
338 CHECK_ERROR(
"VecRestoreArray");
339 ierr = VecGhostRestoreLocalForm(b, &b_local);
340 CHECK_ERROR(
"VecGhostRestoreLocalForm");
353template <std::
floating_po
int T>
357 PetscErrorCode ierr = VecGhostGetLocalForm(b, &b_local);
358 CHECK_ERROR(
"VecGhostGetLocalForm");
360 ierr = VecGetSize(b_local, &n);
361 CHECK_ERROR(
"VecGetSize");
362 PetscScalar* array =
nullptr;
363 ierr = VecGetArray(b_local, &array);
364 CHECK_ERROR(
"VecGetArray");
365 std::span<PetscScalar> _b(array, n);
367 ierr = VecRestoreArray(b_local, &array);
368 CHECK_ERROR(
"VecRestoreArray");
369 ierr = VecGhostRestoreLocalForm(b, &b_local);
370 CHECK_ERROR(
"VecGhostRestoreLocalForm");
404template <std::
floating_po
int T>
410 const std::vector<std::span<const PetscScalar>>& constants,
411 const std::vector<std::map<std::pair<IntegralType, int>,
412 std::pair<std::span<const PetscScalar>,
int>>>&
417 const std::vector<Vec>& x0, PetscScalar alpha)
419 if (!x0.empty() and x0.size() != a.size())
420 throw std::runtime_error(
"Mismatch between x0 and a in apply_lifting.");
423 PetscErrorCode ierr = VecGhostGetLocalForm(b, &b_local);
424 CHECK_ERROR(
"VecGhostGetLocalForm");
426 ierr = VecGetSize(b_local, &n);
427 CHECK_ERROR(
"VecGetSize");
428 PetscScalar* array =
nullptr;
429 ierr = VecGetArray(b_local, &array);
430 CHECK_ERROR(
"VecGetArray");
431 std::span<PetscScalar> _b(array, n);
437 std::vector<std::span<const PetscScalar>> x0_ref;
438 std::vector<Vec> x0_local(a.size());
439 std::vector<const PetscScalar*> x0_array(a.size());
440 for (std::size_t i = 0; i < a.size(); ++i)
443 ierr = VecGhostGetLocalForm(x0[i], &x0_local[i]);
444 CHECK_ERROR(
"VecGhostGetLocalForm");
446 ierr = VecGetSize(x0_local[i], &n0);
447 CHECK_ERROR(
"VecGetSize");
448 ierr = VecGetArrayRead(x0_local[i], &x0_array[i]);
449 CHECK_ERROR(
"VecGetArrayRead");
450 x0_ref.emplace_back(x0_array[i], n0);
455 for (std::size_t i = 0; i < x0_local.size(); ++i)
457 ierr = VecRestoreArrayRead(x0_local[i], &x0_array[i]);
458 CHECK_ERROR(
"VecRestoreArrayRead");
459 ierr = VecGhostRestoreLocalForm(x0[i], &x0_local[i]);
460 CHECK_ERROR(
"VecGhostRestoreLocalForm");
464 ierr = VecRestoreArray(b_local, &array);
465 CHECK_ERROR(
"VecRestoreArray");
466 ierr = VecGhostRestoreLocalForm(b, &b_local);
467 CHECK_ERROR(
"VecGhostRestoreLocalForm");
497template <std::
floating_po
int T>
505 const std::vector<Vec>& x0, PetscScalar alpha)
507 if (!x0.empty() and x0.size() != a.size())
508 throw std::runtime_error(
"Mismatch between x0 and a in apply_lifting.");
511 PetscErrorCode ierr = VecGhostGetLocalForm(b, &b_local);
512 CHECK_ERROR(
"VecGhostGetLocalForm");
514 ierr = VecGetSize(b_local, &n);
515 CHECK_ERROR(
"VecGetSize");
516 PetscScalar* array =
nullptr;
517 ierr = VecGetArray(b_local, &array);
518 CHECK_ERROR(
"VecGetArray");
519 std::span<PetscScalar> _b(array, n);
525 std::vector<std::span<const PetscScalar>> x0_ref;
526 std::vector<Vec> x0_local(a.size());
527 std::vector<const PetscScalar*> x0_array(a.size());
528 for (std::size_t i = 0; i < a.size(); ++i)
531 ierr = VecGhostGetLocalForm(x0[i], &x0_local[i]);
532 CHECK_ERROR(
"VecGhostGetLocalForm");
534 ierr = VecGetSize(x0_local[i], &n0);
535 CHECK_ERROR(
"VecGetSize");
536 ierr = VecGetArrayRead(x0_local[i], &x0_array[i]);
537 CHECK_ERROR(
"VecGetArrayRead");
538 x0_ref.emplace_back(x0_array[i], n0);
543 for (std::size_t i = 0; i < x0_local.size(); ++i)
545 ierr = VecRestoreArrayRead(x0_local[i], &x0_array[i]);
546 CHECK_ERROR(
"VecRestoreArrayRead");
547 ierr = VecGhostRestoreLocalForm(x0[i], &x0_local[i]);
548 CHECK_ERROR(
"VecGhostRestoreLocalForm");
552 ierr = VecRestoreArray(b_local, &array);
553 CHECK_ERROR(
"VecRestoreArray");
554 ierr = VecGhostRestoreLocalForm(b, &b_local);
555 CHECK_ERROR(
"VecGhostRestoreLocalForm");
574template <std::
floating_po
int T>
578 std::optional<const Vec> x0, PetscScalar alpha = 1)
581 PetscErrorCode ierr = VecGetLocalSize(b, &n);
582 CHECK_ERROR(
"VecGetLocalSize");
583 PetscScalar* array =
nullptr;
584 ierr = VecGetArray(b, &array);
585 CHECK_ERROR(
"VecGetArray");
586 std::span<PetscScalar> _b(array, n);
590 ierr = VecGhostGetLocalForm(x0.value(), &x0_local);
591 CHECK_ERROR(
"VecGhostGetLocalForm");
593 ierr = VecGetSize(x0_local, &n0);
594 CHECK_ERROR(
"VecGetSize");
595 const PetscScalar* x0_array =
nullptr;
596 ierr = VecGetArrayRead(x0_local, &x0_array);
597 CHECK_ERROR(
"VecGetArrayRead");
598 std::span<const PetscScalar> _x0(x0_array, n0);
600 bc.get().set(_b, _x0, alpha);
601 ierr = VecRestoreArrayRead(x0_local, &x0_array);
602 CHECK_ERROR(
"VecRestoreArrayRead");
603 ierr = VecGhostRestoreLocalForm(x0.value(), &x0_local);
604 CHECK_ERROR(
"VecGhostRestoreLocalForm");
609 bc.get().set(_b, std::nullopt, alpha);
611 ierr = VecRestoreArray(b, &array);
612 CHECK_ERROR(
"VecRestoreArray");
Functions supporting assembly of finite element fem::Form and fem::Expression.
std::int32_t size_local() const noexcept
Number of indices owned by this process.
Definition IndexMap.cpp:945
Definition DirichletBC.h:258
Definition SparsityPattern.h:26
void finalize()
Finalize sparsity pattern and communicate off-process entries.
Definition SparsityPattern.cpp:264
Functions supporting finite element method operations.
Miscellaneous classes, functions and types.
Definition dolfinx_common.h:8
std::tuple< std::int64_t, std::vector< std::int32_t >, std::vector< std::vector< std::int64_t > >, std::vector< std::vector< int > > > stack_index_maps(const std::vector< std::pair< std::reference_wrapper< const IndexMap >, int > > &maps)
Compute layout data and ghost indices for a stacked (concatenated) index map, i.e....
Definition IndexMap.cpp:659
Helper functions for assembly into PETSc data structures.
Definition petsc.h:43
Mat create_matrix(const Form< PetscScalar, T > &a, std::optional< std::string > type=std::nullopt)
Create a matrix.
Definition petsc.h:60
Mat create_matrix_block(const std::vector< std::vector< const Form< PetscScalar, T > * > > &a, std::optional< std::string > type=std::nullopt)
Initialise a monolithic matrix for an array of bilinear forms.
Definition petsc.h:79
Mat create_matrix_nest(const std::vector< std::vector< const Form< PetscScalar, T > * > > &a, std::optional< std::vector< std::vector< std::optional< std::string > > > > types)
Create nested (MatNest) matrix.
Definition petsc.h:228
void assemble_vector(Vec b, const Form< PetscScalar, T > &L, std::span< const PetscScalar > constants, const std::map< std::pair< IntegralType, int >, std::pair< std::span< const PetscScalar >, int > > &coeffs)
Assemble linear form into an already allocated PETSc vector.
Definition petsc.h:320
void set_bc(Vec b, const std::vector< std::reference_wrapper< const DirichletBC< PetscScalar, T > > > &bcs, std::optional< const Vec > x0, PetscScalar alpha=1)
Definition petsc.h:575
Vec create_vector_block(const std::vector< std::pair< std::reference_wrapper< const common::IndexMap >, int > > &maps)
Initialise monolithic vector. Vector is not zeroed.
Definition petsc.cpp:21
void apply_lifting(Vec b, std::vector< std::optional< std::reference_wrapper< const Form< PetscScalar, T > > > > a, const std::vector< std::span< const PetscScalar > > &constants, const std::vector< std::map< std::pair< IntegralType, int >, std::pair< std::span< const PetscScalar >, int > > > &coeffs, const std::vector< std::vector< std::reference_wrapper< const DirichletBC< PetscScalar, T > > > > &bcs1, const std::vector< Vec > &x0, PetscScalar alpha)
Modify RHS vector to account for Dirichlet boundary conditions.
Definition petsc.h:405
Vec create_vector_nest(const std::vector< std::pair< std::reference_wrapper< const common::IndexMap >, int > > &maps)
Create nested (VecNest) vector. Vector is not zeroed.
Definition petsc.cpp:66
Finite element method functionality.
Definition assemble_expression_impl.h:23
std::vector< std::vector< std::array< std::shared_ptr< const FunctionSpace< U > >, 2 > > > extract_function_spaces(const std::vector< std::vector< const Form< T, U > * > > &a)
Extract test (0) and trial (1) function spaces pairs for each bilinear form for a rectangular array o...
Definition utils.h:173
void apply_lifting(V &&b, const std::vector< std::optional< std::reference_wrapper< const Form< T, U > > > > &a, const std::vector< std::span< const T > > &constants, const std::vector< std::map< std::pair< IntegralType, int >, std::pair< std::span< const T >, int > > > &coeffs, const std::vector< std::vector< std::reference_wrapper< const DirichletBC< T, U > > > > &bcs1, const std::vector< std::span< const T > > &x0, T alpha)
Modify the right-hand side vector to account for constraints (Dirichlet boundary condition constraint...
Definition assembler.h:341
void assemble_vector(V &&b, const Form< T, U > &L, std::span< const T > constants, const std::map< std::pair< IntegralType, int >, std::pair< std::span< const T >, int > > &coefficients)
Assemble linear form into a vector.
Definition assembler.h:233
la::SparsityPattern create_sparsity_pattern(const Form< T, U > &a)
Create a sparsity pattern for a given form.
Definition utils.h:198
std::array< std::vector< std::shared_ptr< const FunctionSpace< T > > >, 2 > common_function_spaces(const std::vector< std::vector< std::array< std::shared_ptr< const FunctionSpace< T > >, 2 > > > &V)
Extract FunctionSpaces for (0) rows blocks and (1) columns blocks from a rectangular array of (test,...
Definition FunctionSpace.h:433
Mat create_matrix(MPI_Comm comm, const SparsityPattern &sp, std::optional< std::string_view > type=std::nullopt)
Definition petsc.cpp:260
Mesh data structures and algorithms on meshes.
Definition DofMap.h:32