Allium  0.1.0
petsc_mesh_spec.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_SPEC_HPP
16 #define ALLIUM_MESH_PETSC_MESH_SPEC_HPP
17 
18 #include <allium/config.hpp>
19 #ifdef ALLIUM_USE_PETSC
20 
21 #include <allium/util/extern.hpp>
22 #include <allium/util/types.hpp>
23 #include <allium/ipc/comm.hpp>
24 #include <allium/la/petsc_object_ptr.hpp>
25 #include <allium/la/petsc_util.hpp>
26 #include <allium/la/vector_spec.hpp>
27 
28 #include "range.hpp"
29 
30 #include <petscdm.h>
31 #include <petscdmda.h>
32 
33 namespace allium {
34 
38 template <int D>
39 class PetscMeshSpec {};
40 
41 template <>
42 class PetscMeshSpec<2> {
43  public:
44  PetscMeshSpec(Comm comm,
45  std::array<DMBoundaryType, 2> boundary_type,
46  DMDAStencilType stencil_type,
47  std::array<global_size_t, 2> global_size,
48  std::array<PetscInt, 2> ranks_per_dim,
49  int ndof,
50  int stencil_width)
51  : m_spec(comm, 0, 0),
52  m_ndof(ndof)
53  {
54  using namespace petsc;
55  PetscErrorCode ierr;
56 
57  ierr = DMDACreate2d(
58  comm.handle(), // comm
59  boundary_type[0], boundary_type[1],
60  stencil_type,
61  global_size[0], global_size[1],
62  ranks_per_dim[0], ranks_per_dim[1],
63  ndof,
64  stencil_width,
65  nullptr, // PetscInt lx[],
66  nullptr, // PetscInt ly[],
67  m_dm.writable_ptr());
68  chkerr(ierr);
69 
70  ierr = DMSetUp(m_dm); chkerr(ierr);
71 
72  DMDALocalInfo info;
73  ierr = DMDAGetLocalInfo(m_dm, &info); chkerr(ierr);
74 
75  m_spec = VectorSpec(comm,
76  info.xm * info.ym * ndof,
77  info.mx * info.my * ndof);
78  }
79 
80  PetscMeshSpec(const PetscMeshSpec&) = delete;
81  PetscMeshSpec& operator= (const PetscMeshSpec&) = delete;
82 
83  PetscObjectPtr<DM> dm() { return m_dm; }
84  VectorSpec vector_spec() const { return m_spec; }
85  Comm comm() { return m_spec.comm(); }
86 
87  int ndof() { return m_ndof; }
88 
89  Range<2> range() const {
90  using namespace petsc;
91  PetscErrorCode ierr;
92 
93  DMDALocalInfo info;
94  ierr = DMDAGetLocalInfo(m_dm, &info); chkerr(ierr);
95 
96  Point<int, 2> begin{0,0};
97  Point<int, 2> end{info.mx, info.my};
98 
99  return Range<2>(begin, end);
100  }
101 
102  Range<2> local_range() const {
103  using namespace petsc;
104  PetscErrorCode ierr;
105 
106  Point<PetscInt, 2> begin;
107  Point<PetscInt, 2> extent;
108  ierr = DMDAGetCorners(m_dm,
109  &begin[0], &begin[1], nullptr,
110  &extent[0], &extent[1], nullptr);
111 
112  chkerr(ierr);
113 
114  return Range<2>(begin, begin+extent);
115  }
116 
117  Range<2> local_ghost_range() const {
118  using namespace petsc;
119  PetscErrorCode ierr;
120 
121  Point<PetscInt, 2> begin;
122  Point<PetscInt, 2> extent;
123  ierr = DMDAGetGhostCorners(m_dm,
124  &begin[0], &begin[1], nullptr,
125  &extent[0], &extent[1], nullptr);
126 
127  chkerr(ierr);
128 
129  return Range<2>(begin, begin+extent);
130  }
131  private:
132  PetscObjectPtr<DM> m_dm;
133  VectorSpec m_spec;
134  int m_ndof;
135 };
136 
137 #define ALLIUM_PETSC_MESH_SPEC_DECL(extern, D) \
138  extern template class PetscMeshSpec<D>;
139 ALLIUM_EXTERN_D(ALLIUM_PETSC_MESH_SPEC_DECL)
140 
141 }
142 
143 #endif
144 #endif
Wraps an MPI communicator to make MPI easier to use.
Definition: comm.hpp:26
Specification for a PETSc mesh, which is needed to create one.
Definition: petsc_mesh_spec.hpp:39
A point in D dimensions.
Definition: point.hpp:31
An n-d range.
Definition: range.hpp:28
The specification of a vector, needed to create distributed vectors.
Definition: vector_spec.hpp:26