DOLFIN-X
DOLFIN-X C++ interface
utils.h
1 // Copyright (C) 2018-2019 Garth N. Wells
2 //
3 // This file is part of DOLFINX (https://www.fenicsproject.org)
4 //
5 // SPDX-License-Identifier: LGPL-3.0-or-later
6 
7 #pragma once
8 
9 #include <Eigen/Dense>
10 #include <array>
11 #include <dolfinx/common/MPI.h>
12 #include <petscis.h>
13 #include <petscmat.h>
14 #include <petscvec.h>
15 #include <string>
16 #include <vector>
17 
18 namespace dolfinx
19 {
20 namespace common
21 {
22 class IndexMap;
23 }
24 namespace fem
25 {
26 class Form;
27 }
28 namespace la
29 {
30 class VectorSpaceBasis;
31 class SparsityPattern;
32 
34 enum class Norm
35 {
36  l1,
37  l2,
38  linf,
39  frobenius
40 };
41 
45 
49  MPI_Comm comm, std::array<std::int64_t, 2> range,
50  const Eigen::Ref<const Eigen::Array<std::int64_t, Eigen::Dynamic, 1>>&
51  ghost_indices,
52  int block_size);
53 
56 Mat create_petsc_matrix(MPI_Comm comm, const SparsityPattern& sparsity_pattern);
57 
60 MatNullSpace create_petsc_nullspace(MPI_Comm comm,
61  const VectorSpaceBasis& nullspace);
62 
64 
71 std::vector<IS>
72 create_petsc_index_sets(const std::vector<const common::IndexMap*>& maps);
73 
75 void petsc_error(int error_code, std::string filename,
76  std::string petsc_function);
77 
80 {
81 public:
83  VecWrapper(Vec y, bool ghosted = true);
84  VecWrapper(const VecWrapper& w) = delete;
87  VecWrapper& operator=(const VecWrapper& w) = delete;
89  VecWrapper& operator=(VecWrapper&& w);
90  ~VecWrapper();
92  void restore();
94  Eigen::Map<Eigen::Matrix<PetscScalar, Eigen::Dynamic, 1>> x;
95 
96 private:
97  PetscScalar* array = nullptr;
98  Vec _y;
99  Vec _y_local = nullptr;
100  bool _ghosted;
101 };
102 
106 {
107 public:
109  VecReadWrapper(const Vec y, bool ghosted = true);
110  VecReadWrapper(const VecReadWrapper& w) = delete;
113  VecReadWrapper& operator=(const VecReadWrapper& w) = delete;
115  VecReadWrapper& operator=(VecReadWrapper&& w);
116  ~VecReadWrapper();
118  void restore();
120  Eigen::Map<const Eigen::Matrix<PetscScalar, Eigen::Dynamic, 1>> x;
121 
122 private:
123  PetscScalar const* array = nullptr;
124  Vec _y;
125  Vec _y_local = nullptr;
126  bool _ghosted;
127 };
128 
129 // /// Get sub-matrix. Sub-matrix is local to the process
130 // Mat get_local_submatrix(const Mat A, const IS row, const IS col);
131 
132 // /// Restore local submatrix
133 // void restore_local_submatrix(const Mat A, const IS row, const IS col, Mat*
134 // Asub);
135 
137 std::vector<Eigen::Matrix<PetscScalar, Eigen::Dynamic, 1>>
138 get_local_vectors(const Vec x,
139  const std::vector<const common::IndexMap*>& maps);
140 
142 // std::vector<Vec>
143 // get_local_petsc_vectors(const Vec x,
144 // const std::vector<const common::IndexMap*>& maps);
145 
148  Vec x,
149  const std::vector<Eigen::Matrix<PetscScalar, Eigen::Dynamic, 1>>& x_b,
150  const std::vector<const common::IndexMap*>& maps);
151 
152 } // namespace la
153 } // namespace dolfinx
dolfinx::la::Norm
Norm
Norm types.
Definition: utils.h:34
dolfinx::la::VecWrapper
Wrapper around a PETSc Vec object, to simplify direct access to data.
Definition: utils.h:79
dolfinx::la::get_local_vectors
std::vector< Eigen::Matrix< PetscScalar, Eigen::Dynamic, 1 > > get_local_vectors(const Vec x, const std::vector< const common::IndexMap * > &maps)
Copy blocks from Vec into Eigen vectors.
Definition: utils.cpp:399
dolfinx::la::create_petsc_index_sets
std::vector< IS > create_petsc_index_sets(const std::vector< const common::IndexMap * > &maps)
Compute IndexSets (IS) for stacked index maps. E.g., if map[0] = {0, 1, 2, 3, 4, 5,...
Definition: utils.cpp:222
dolfinx::la::petsc_error
void petsc_error(int error_code, std::string filename, std::string petsc_function)
Print error message for PETSc calls that return an error.
Definition: utils.cpp:246
dolfinx::common::IndexMap
This class represents the distribution index arrays across processes. An index array is a contiguous ...
Definition: IndexMap.h:27
dolfinx::la::create_petsc_matrix
Mat create_petsc_matrix(MPI_Comm comm, const SparsityPattern &sparsity_pattern)
Create a PETSc Mat. Caller is responsible for destroying the returned object.
Definition: utils.cpp:84
dolfinx::la::VecReadWrapper::restore
void restore()
Restore PETSc Vec.
Definition: utils.cpp:377
dolfinx::la::create_petsc_vector
Vec create_petsc_vector(const common::IndexMap &map)
Create a ghosted PETSc Vec. Caller is responsible for destroying the returned object.
Definition: utils.cpp:31
dolfinx::la::scatter_local_vectors
void scatter_local_vectors(Vec x, const std::vector< Eigen::Matrix< PetscScalar, Eigen::Dynamic, 1 >> &x_b, const std::vector< const common::IndexMap * > &maps)
Copy blocks from Vec into local Vec objects.
Definition: utils.cpp:478
dolfinx::la::VecWrapper::restore
void restore()
Restore PETSc Vec object.
Definition: utils.cpp:319
dolfinx::la::VecWrapper::VecWrapper
VecWrapper(Vec y, bool ghosted=true)
Wrap PETSc Vec y.
Definition: utils.cpp:276
dolfinx::la::VecReadWrapper
Read-only wrapper around a PETSc Vec object, to simplify direct access to data.
Definition: utils.h:105
dolfinx::la::VecReadWrapper::VecReadWrapper
VecReadWrapper(const Vec y, bool ghosted=true)
Wrap PETSc Vec y.
Definition: utils.cpp:333
dolfinx::la::VecWrapper::x
Eigen::Map< Eigen::Matrix< PetscScalar, Eigen::Dynamic, 1 > > x
Eigen Map into PETSc Vec.
Definition: utils.h:94
dolfinx::la::create_petsc_nullspace
MatNullSpace create_petsc_nullspace(MPI_Comm comm, const VectorSpaceBasis &nullspace)
Create PETSc MatNullSpace. Caller is responsible for destruction returned object.
Definition: utils.cpp:195
dolfinx::la::VecReadWrapper::x
Eigen::Map< const Eigen::Matrix< PetscScalar, Eigen::Dynamic, 1 > > x
Eigen Map into PETSc Vec.
Definition: utils.h:120