15 #ifndef ALLIUM_MESH_PETSC_MESH_HPP
16 #define ALLIUM_MESH_PETSC_MESH_HPP
18 #include <allium/config.hpp>
19 #ifdef ALLIUM_USE_PETSC
21 #include <allium/ipc/comm.hpp>
22 #include <allium/la/petsc_abstract_vector.hpp>
23 #include "petsc_mesh_spec.hpp"
25 #include "local_mesh.hpp"
28 #include <petscdmda.h>
40 virtual std::shared_ptr<PetscMeshSpec<D>> mesh_spec()
const = 0;
42 virtual Range<D> local_range()
const = 0;
48 template <
typename N,
int D>
63 std::shared_ptr<PetscMeshSpec<D>> mesh_spec()
const override {
return m_spec; }
65 Range<D> local_range()
const override {
return m_spec->local_range(); }
67 std::shared_ptr<PetscMeshSpec<D>> m_spec;
69 PetscMesh* allocate_like()
const&
override;
76 template <
typename N,
int D>
83 using namespace petsc;
86 ierr = DMCreateLocalVector(spec->dm(), m_ptr.writable_ptr());
95 using namespace petsc;
98 ierr = DMGlobalToLocal(m_spec->dm(), global_mesh.native(), INSERT_VALUES, m_ptr);
102 std::shared_ptr<PetscMeshSpec<D>> mesh_spec()
const override {
return m_spec; }
104 Range<D> local_range()
const override {
return m_spec->local_ghost_range(); }
106 std::shared_ptr<PetscMeshSpec<D>> m_spec;
113 template <
typename N,
int D,
bool is_mutable=true>
117 template <
int D>
struct PetscArrayType {
118 using type =
typename PetscArrayType<D-1>::type*;
120 template <>
struct PetscArrayType<0> {
using type = PetscScalar; };
125 template <
bool is_mutable>
130 =
typename std::conditional<is_mutable, IPetscMesh<2>,
const IPetscMesh<2>>::type;
132 =
typename std::conditional<is_mutable, PetscScalar, const PetscScalar>::type;
137 using namespace petsc;
140 ierr = DMDAVecGetArray(m_mesh->mesh_spec()->dm(),
145 m_ndof = mesh.mesh_spec()->ndof();
147 #ifdef ALLIUM_BOUND_CHECKS
148 m_range = mesh.local_range();
152 using namespace petsc;
154 if (m_mesh !=
nullptr) {
156 ierr = DMDAVecRestoreArray(m_mesh->mesh_spec()->dm(),
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;
174 other.m_mesh =
nullptr;
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");
182 return m_values[j][i * m_ndof + dof];
185 int dof_count()
const {
return m_ndof; }
186 Range<2> local_range()
const {
return m_mesh->local_range(); }
189 typename PetscArrayType<2>::type m_values;
192 #ifdef ALLIUM_BOUND_CHECKS
197 template <
typename N,
int D,
bool is_mutable>
201 using Mesh =
typename PetscMeshValues<PetscScalar, 2, is_mutable>::Mesh;
204 : m_native_values(mesh),
206 Range<D+1>(mesh.local_range().begin_pos().joined(0),
207 mesh.local_range().end_pos().joined(mesh.mesh_spec()->ndof())))
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)]
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)];
228 N& operator() (
int i,
int j,
int dof = 0) {
237 template <
typename N,
int D>
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);
247 template <
typename N,
int D>
248 PetscMeshValues<N, D, true> local_mesh(PetscLocalMesh<N, D>& mesh) {
249 return PetscMeshValues<N, D, true>(mesh);
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);
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)
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
Definition: petsc_mesh.hpp:127
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