Allium  0.1.0
petsc_mesh.hpp
1 // Copyright 2020 Hannah Rittich
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef ALLIUM_MESH_PETSC_MESH_HPP
16 #define ALLIUM_MESH_PETSC_MESH_HPP
17 
18 #include <allium/config.hpp>
19 #ifdef ALLIUM_USE_PETSC
20 
21 #include <allium/ipc/comm.hpp>
22 #include <allium/la/petsc_abstract_vector.hpp>
23 #include "petsc_mesh_spec.hpp"
24 #include "range.hpp"
25 #include "local_mesh.hpp"
26 
27 #include <petscdm.h>
28 #include <petscdmda.h>
29 
30 namespace allium {
31 
35 template <int D>
36 class IPetscMesh {
37  public:
38  virtual ~IPetscMesh() {}
39 
40  virtual std::shared_ptr<PetscMeshSpec<D>> mesh_spec() const = 0;
41  virtual PetscObjectPtr<Vec> petsc_vec() = 0;
42  virtual Range<D> local_range() const = 0;
43 };
44 
48 template <typename N, int D>
49 class PetscMesh
50  : public PetscAbstractVectorStorage<N>,
51  public IPetscMesh<D>
52 {
53  public:
55 
56  explicit PetscMesh(std::shared_ptr<PetscMeshSpec<D>> spec);
57  PetscMesh(const PetscMesh&) = delete;
58  PetscMesh& operator= (const PetscMesh&) = delete;
59 
60  PetscMesh(std::shared_ptr<PetscMeshSpec<D>> spec,
62 
63  std::shared_ptr<PetscMeshSpec<D>> mesh_spec() const override { return m_spec; }
64  PetscObjectPtr<Vec> petsc_vec() override { return this->native(); }
65  Range<D> local_range() const override { return m_spec->local_range(); }
66  private:
67  std::shared_ptr<PetscMeshSpec<D>> m_spec;
68 
69  PetscMesh* allocate_like() const& override;
70  PetscMesh* clone() const& override;
71 };
72 
76 template <typename N, int D>
77 class PetscLocalMesh : public IPetscMesh<D>
78 {
79  public:
80  explicit PetscLocalMesh(std::shared_ptr<PetscMeshSpec<2>> spec)
81  : m_spec(spec)
82  {
83  using namespace petsc;
84  PetscErrorCode ierr;
85 
86  ierr = DMCreateLocalVector(spec->dm(), m_ptr.writable_ptr());
87  chkerr(ierr);
88  }
89 
90  PetscLocalMesh(const PetscLocalMesh&) = delete;
91  PetscLocalMesh& operator= (const PetscLocalMesh&) = delete;
92 
93  void assign(const PetscMesh<N,D>& global_mesh)
94  {
95  using namespace petsc;
96  PetscErrorCode ierr;
97 
98  ierr = DMGlobalToLocal(m_spec->dm(), global_mesh.native(), INSERT_VALUES, m_ptr);
99  chkerr(ierr);
100  }
101 
102  std::shared_ptr<PetscMeshSpec<D>> mesh_spec() const override { return m_spec; }
103  PetscObjectPtr<Vec> petsc_vec() override { return m_ptr; }
104  Range<D> local_range() const override { return m_spec->local_ghost_range(); }
105  private:
106  std::shared_ptr<PetscMeshSpec<D>> m_spec;
107  PetscObjectPtr<Vec> m_ptr;
108 };
109 
113 template <typename N, int D, bool is_mutable=true>
114 class PetscMeshValues;
115 
117 template <int D> struct PetscArrayType {
118  using type = typename PetscArrayType<D-1>::type*;
119 };
120 template <> struct PetscArrayType<0> { using type = PetscScalar; };
121 
123 
124 
125 template <bool is_mutable>
126 class PetscMeshValues<PetscScalar, 2, is_mutable>
127 {
128  public:
129  using Mesh
130  = typename std::conditional<is_mutable, IPetscMesh<2>, const IPetscMesh<2>>::type;
131  using Reference
132  = typename std::conditional<is_mutable, PetscScalar, const PetscScalar>::type;
133 
134  explicit PetscMeshValues(Mesh& mesh)
135  : m_mesh(&mesh)
136  {
137  using namespace petsc;
138 
139  PetscErrorCode ierr;
140  ierr = DMDAVecGetArray(m_mesh->mesh_spec()->dm(),
141  const_cast<IPetscMesh<2>*>(m_mesh)->petsc_vec(),
142  &m_values);
143  chkerr(ierr);
144 
145  m_ndof = mesh.mesh_spec()->ndof();
146 
147  #ifdef ALLIUM_BOUND_CHECKS
148  m_range = mesh.local_range();
149  #endif
150  }
151  ~PetscMeshValues() {
152  using namespace petsc;
153 
154  if (m_mesh != nullptr) {
155  PetscErrorCode ierr;
156  ierr = DMDAVecRestoreArray(m_mesh->mesh_spec()->dm(),
157  const_cast<IPetscMesh<2>*>(m_mesh)->petsc_vec(),
158  &m_values);
159  chkerr(ierr);
160  }
161  }
162 
163  PetscMeshValues(const PetscMeshValues&) = delete;
164  PetscMeshValues& operator= (const PetscMeshValues&) = delete;
165 
167  m_values = other.m_values;
168  m_mesh = other.m_mesh;
169  m_ndof = other.m_ndof;
170  #ifdef ALLIUM_BOUND_CHECKS
171  m_range = other.m_range;
172  #endif
173 
174  other.m_mesh = nullptr;
175  }
176 
177  PetscScalar& operator() (int i, int j, int dof = 0) {
178  #ifdef ALLIUM_BOUND_CHECKS
179  if (!m_range.in({i,j}))
180  throw std::logic_error("Out of range access");
181  #endif
182  return m_values[j][i * m_ndof + dof];
183  }
184 
185  int dof_count() const { return m_ndof; }
186  Range<2> local_range() const { return m_mesh->local_range(); }
187 
188  private:
189  typename PetscArrayType<2>::type m_values;
190  Mesh* m_mesh;
191  int m_ndof;
192  #ifdef ALLIUM_BOUND_CHECKS
193  Range<2> m_range;
194  #endif
195 };
196 
197 template <typename N, int D, bool is_mutable>
199 {
200  public:
201  using Mesh = typename PetscMeshValues<PetscScalar, 2, is_mutable>::Mesh;
202 
203  PetscMeshValues(Mesh& mesh)
204  : m_native_values(mesh),
205  m_converted_values(
206  Range<D+1>(mesh.local_range().begin_pos().joined(0),
207  mesh.local_range().end_pos().joined(mesh.mesh_spec()->ndof())))
208  {
209  for (auto p : mesh.local_range()) {
210  for (int i_dof=0; i_dof < m_native_values.dof_count(); ++i_dof) {
211  m_converted_values[p.joined(i_dof)]
212  = narrow_number<N, PetscScalar>()(m_native_values(p[0], p[1], i_dof));
213  }
214  }
215  }
216 
217  PetscMeshValues(PetscMeshValues&& other) = default;
218 
219  ~PetscMeshValues()
220  {
221  for (auto p : m_native_values.local_range()) {
222  for (int i_dof=0; i_dof < m_native_values.dof_count(); ++i_dof) {
223  m_native_values(p[0], p[1], i_dof) = m_converted_values[p.joined(i_dof)];
224  }
225  }
226  }
227 
228  N& operator() (int i, int j, int dof = 0) {
229  return m_converted_values[Point<int, D+1>({i, j, dof})];
230  }
231 
232  private:
234  LocalMesh<N, D+1> m_converted_values;
235 };
236 
237 template <typename N, int D>
239  return PetscMeshValues<N, D, true>(mesh);
240 }
241 
242 template <typename N, int D>
243 PetscMeshValues<N, D, false> local_mesh(const PetscMesh<N, D>& mesh) {
244  return PetscMeshValues<N, D, false>(mesh);
245 }
246 
247 template <typename N, int D>
248 PetscMeshValues<N, D, true> local_mesh(PetscLocalMesh<N, D>& mesh) {
249  return PetscMeshValues<N, D, true>(mesh);
250 }
251 
252 template <typename N, int D>
253 PetscMeshValues<N, D, false> local_mesh(const PetscLocalMesh<N, D>& mesh) {
254  return PetscMeshValues<N, D, false>(mesh);
255 }
256 
257 #define ALLIUM_PETSC_MESH_DECL(extern, N) \
258  extern template class PetscMesh<N, 2>; \
259  extern template class PetscLocalMesh<N, 2>;
260 ALLIUM_EXTERN_N(ALLIUM_PETSC_MESH_DECL)
261 
262 }
263 
264 #endif
265 #endif
Interface to PETSc based meshes.
Definition: petsc_mesh.hpp:36
A vector based on a PETSc vector.
Definition: petsc_abstract_vector.hpp:193
A (portion of a) PETSc mesh that is stored locally.
Definition: petsc_mesh.hpp:78
Definition: petsc_mesh_spec.hpp:42
Specification for a PETSc mesh, which is needed to create one.
Definition: petsc_mesh_spec.hpp:39
Accessor to the values stored in a PETSc mesh.
Definition: petsc_mesh.hpp:199
A mesh implementation based on PETSc.
Definition: petsc_mesh.hpp:52
A point in D dimensions.
Definition: point.hpp:31
An n-d range.
Definition: range.hpp:28
Narrows a number type, which can result in a loss of information or accuracy.
Definition: numeric.hpp:46