ThunderEgg  1.0.0
MatWrapper.h
Go to the documentation of this file.
1 /***************************************************************************
2  * ThunderEgg, a library for solvers on adaptively refined block-structured
3  * Cartesian grids.
4  *
5  * Copyright (c) 2018-2021 Scott Aiton
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <https://www.gnu.org/licenses/>.
19  ***************************************************************************/
20 
21 #ifndef THUNDEREGG_PETSC_MATWRAPPER_H
22 #define THUNDEREGG_PETSC_MATWRAPPER_H
23 
29 #include <ThunderEgg/Operator.h>
30 #include <petscmat.h>
31 
32 namespace ThunderEgg::PETSc {
36 template<int D>
37 class MatWrapper : public Operator<D>
38 {
39 private:
43  Mat A;
50  Vec getPetscVecWithoutGhost(const Vector<D>& vec) const
51  {
52  Vec petsc_vec;
53  VecCreateMPI(vec.getCommunicator().getMPIComm(),
54  vec.getNumLocalCells() * vec.getNumComponents(),
55  PETSC_DETERMINE,
56  &petsc_vec);
57  return petsc_vec;
58  }
65  void copyToPetscVec(const Vector<D>& vec, Vec petsc_vec) const
66  {
67  double* petsc_vec_view;
68  size_t curr_index = 0;
69  VecGetArray(petsc_vec, &petsc_vec_view);
70  for (int i = 0; i < vec.getNumLocalPatches(); i++) {
71  for (int c = 0; c < vec.getNumComponents(); c++) {
73  Loop::Nested<D>(ld.getStart(), ld.getEnd(), [&](const std::array<int, D>& coord) {
74  petsc_vec_view[curr_index] = ld[coord];
75  curr_index++;
76  });
77  }
78  }
79  VecRestoreArray(petsc_vec, &petsc_vec_view);
80  }
87  void copyToVec(Vec petsc_vec, Vector<D>& vec) const
88  {
89  const double* petsc_vec_view;
90  size_t curr_index = 0;
91  VecGetArrayRead(petsc_vec, &petsc_vec_view);
92  for (int i = 0; i < vec.getNumLocalPatches(); i++) {
93  for (int c = 0; c < vec.getNumComponents(); c++) {
95  Loop::Nested<D>(ld.getStart(), ld.getEnd(), [&](const std::array<int, D>& coord) {
96  ld[coord] = petsc_vec_view[curr_index];
97  curr_index++;
98  });
99  }
100  }
101  VecRestoreArrayRead(petsc_vec, &petsc_vec_view);
102  }
109  void destroyPetscVec(const Vector<D>& vec, Vec petsc_vec) const { VecDestroy(&petsc_vec); }
110 
111 public:
119  explicit MatWrapper(Mat A_in)
120  : A(A_in)
121  {}
122 
128  MatWrapper<D>* clone() const override { return new MatWrapper<D>(*this); }
129  void apply(const Vector<D>& x, Vector<D>& b) const override
130  {
131  Vec petsc_x = getPetscVecWithoutGhost(x);
132  copyToPetscVec(x, petsc_x);
133 
134  Vec petsc_b = getPetscVecWithoutGhost(b);
135 
136  MatMult(A, petsc_x, petsc_b);
137 
138  copyToVec(petsc_b, b);
139 
140  destroyPetscVec(x, petsc_x);
141  destroyPetscVec(b, petsc_b);
142  }
143 };
144 } // namespace ThunderEgg::PETSc
145 #endif
ThunderEgg::Vector::getComponentView
ComponentView< double, D > getComponentView(int component_index, int patch_local_index)
Get the ComponentView for the specified patch and component.
Definition: Vector.h:336
ThunderEgg::PETSc::MatWrapper::apply
void apply(const Vector< D > &x, Vector< D > &b) const override
Virtual function that base classes have to implement.
Definition: MatWrapper.h:129
ThunderEgg::PETSc
A collection of wrappers for operating with PETSc.
Definition: KSPSolver.h:35
Operator.h
Operator class.
ThunderEgg::Vector::getCommunicator
const Communicator & getCommunicator() const
get the MPI Comm that this vector uses
Definition: Vector.h:311
ThunderEgg::ComponentView
Array for acessing data of a patch. It supports variable striding.
Definition: ComponentView.h:38
ThunderEgg::Vector::getNumLocalCells
int getNumLocalCells() const
Get the number of local cells int he vector (excluding ghost cells)
Definition: Vector.h:322
ThunderEgg::View::getEnd
const std::array< int, D > & getEnd() const
Get the coordinate of the last element.
Definition: View.h:211
ThunderEgg::PETSc::MatWrapper
Wraps a PETSc Mat object for use as an Operator.
Definition: MatWrapper.h:37
ThunderEgg::PETSc::MatWrapper::clone
MatWrapper< D > * clone() const override
Clone this wrapper.
Definition: MatWrapper.h:128
ThunderEgg::PETSc::MatWrapper::MatWrapper
MatWrapper(Mat A_in)
Construct a new MatWrapper object.
Definition: MatWrapper.h:119
ThunderEgg::Operator
Base class for operators.
Definition: Operator.h:37
ThunderEgg::Vector
Vector class for use in thunderegg.
Definition: Vector.h:42
ThunderEgg::Vector::getNumLocalPatches
int getNumLocalPatches() const
Get the number of local patches.
Definition: Vector.h:316
ThunderEgg::View::getStart
const std::array< int, D > & getStart() const
Get the coordinate of the first element.
Definition: View.h:207