PetIBM 0.5.4
Toolbox and applications of the immersed-boundary method for distributed-memory architectures
misc.cpp
Go to the documentation of this file.
1
8#include <petibm/mesh.h>
9#include <petibm/misc.h>
10#include <petscsys.h>
11
12// private functions
13#include "../private/private.h"
14
15namespace petibm
16{
17namespace misc
18{
19PetscErrorCode checkPeriodicBC(const type::IntVec2D &bcTypes,
20 type::BoolVec2D &periodic)
21{
22 using namespace petibm::type;
23
24 PetscFunctionBeginUser;
25
26 // dimension
27 PetscInt dim = 3;
28
29 // initialize periodic bools
30 periodic = BoolVec2D(3, BoolVec1D(3, PETSC_FALSE));
31
32 // if a boundary of a field is periodic, the counterpart should be, too
33 for (int f = 0; f < 3; f++)
34 {
35 for (int d = 0; d < 3; d++)
36 {
37 bool p1 = (bcTypes[f][d * 2] == int(BCType::PERIODIC));
38 bool p2 = (bcTypes[f][d * 2 + 1] == int(BCType::PERIODIC));
39
40 if (p1 && (!p2))
41 {
42 SETERRQ3(PETSC_COMM_WORLD, PETSC_ERR_ARG_INCOMP,
43 "Boundary %s for velocity field %s is periodic BC, "
44 "but its counterpart, %s, is not!\n",
45 bl2str[BCLoc(d * 2)].c_str(), fd2str[Field(f)].c_str(),
46 bl2str[BCLoc(d * 2 + 1)].c_str());
47 }
48
49 if ((!p1) && p2)
50 {
51 SETERRQ3(PETSC_COMM_WORLD, PETSC_ERR_ARG_INCOMP,
52 "Boundary %s for velocity field %s is periodic BC, "
53 "but its counterpart, %s, is not!\n",
54 bl2str[BCLoc(d * 2 + 1)].c_str(),
55 fd2str[Field(f)].c_str(),
56 bl2str[BCLoc(d * 2)].c_str());
57 }
58
59 if (p1 && p2) periodic[f][d] = PETSC_TRUE;
60 }
61 }
62
63 // if all BCs of w velocity are NOBC, it's a 3D simulation
64 if (std::all_of(bcTypes[2].begin(), bcTypes[2].end(),
65 [](int t) { return t == int(BCType::NOBC); }))
66 dim = 2;
67
68 // if a field at a boundary is periodic BC, other fields should be, too
69 for (int d = 0; d < 3; d++)
70 {
71 PetscInt c = 0;
72 for (int f = 0; f < 3; f++)
73 {
74 if (periodic[f][d]) c += 1;
75 }
76 if ((c != 0) && (c != dim))
77 SETERRQ1(PETSC_COMM_WORLD, PETSC_ERR_ARG_INCOMP,
78 "Not all velocity fields in direction %s are periodic!",
79 type::dir2str[Dir(d)].c_str());
80 }
81
82 PetscFunctionReturn(0);
83} // checkPeriodicBC
84
85PetscErrorCode checkBoundaryProc(const DM &da, const type::IntVec1D &n,
86 const type::BCLoc &loc, PetscBool &onThisProc)
87{
88 PetscFunctionBeginUser;
89
90 PetscErrorCode ierr;
91
92 MPI_Comm bcMPI;
93
94 switch (loc)
95 {
97 ierr = DMDAGetProcessorSubset(da, DM_X, 0, &bcMPI);
98 CHKERRQ(ierr);
99 break;
101 ierr = DMDAGetProcessorSubset(da, DM_X, n[0] - 1, &bcMPI);
102 CHKERRQ(ierr);
103 break;
105 ierr = DMDAGetProcessorSubset(da, DM_Y, 0, &bcMPI);
106 CHKERRQ(ierr);
107 break;
109 ierr = DMDAGetProcessorSubset(da, DM_Y, n[1] - 1, &bcMPI);
110 CHKERRQ(ierr);
111 break;
113 ierr = DMDAGetProcessorSubset(da, DM_Z, 0, &bcMPI);
114 CHKERRQ(ierr);
115 break;
117 ierr = DMDAGetProcessorSubset(da, DM_Z, n[2] - 1, &bcMPI);
118 CHKERRQ(ierr);
119 break;
120 }
121
122 if (bcMPI != MPI_COMM_NULL)
123 onThisProc = PETSC_TRUE;
124 else
125 onThisProc = PETSC_FALSE;
126 PetscFunctionReturn(0);
127} // checkBoundaryProc
128
129PetscErrorCode getGhostPointList(const type::Mesh &mesh,
130 const type::Field &field,
131 const type::BCLoc &loc,
132 type::GhostPointsList &points)
133{
134 PetscFunctionBeginUser;
135
136 PetscErrorCode ierr;
137
138 // the direction of the normal of this boundary; 1: x, 2: y, 3: z
139 PetscInt axis = int(loc) / 2;
140
141 // the two directions that will be used in the following double loop
142 type::IntVec1D pAxes;
143
144 // get pAxes
145 ierr = getPerpendAxes(axis, pAxes); CHKERRQ(ierr);
146
147 // alias
148 const type::IntVec1D &bg = mesh->bg[field];
149 const type::IntVec1D &ed = mesh->ed[field];
150 const type::IntVec1D &n = mesh->n[field];
151 const type::GhostedVec2D &dL = mesh->dL[field];
152 const type::GhostedVec2D &coord = mesh->coord[field];
153
154 for (PetscInt b = bg[pAxes[1]]; b < ed[pAxes[1]]; ++b)
155 {
156 for (PetscInt a = bg[pAxes[0]]; a < ed[pAxes[0]]; ++a)
157 {
158 // he stencil of the ghost point
159 MatStencil ghost;
160
161 // the stencil of the point associated to the ghost point
162 MatStencil target;
163
164 // targetId is the index of target in packed global Vec
165 PetscInt targetId;
166
167 // ghostId is the index of ghost in a local Vec
168 PetscInt ghostId;
169
170 // get the stencils of ghost and target
171 ierr = misc::getGhostTargetStencil(n, loc, {a, b}, ghost, target);
172 CHKERRQ(ierr);
173
174 // get packed index of the target
175 ierr = mesh->getPackedGlobalIndex(field, target, targetId);
176 CHKERRQ(ierr);
177
178 // get the local index of the ghost
179 ierr = mesh->getLocalIndex(field, ghost, ghostId); CHKERRQ(ierr);
180
181 // area is the cell surface area used to calculate flux
182 // d is the distance between the target and the ghost points
183 PetscReal area, d;
184
185 area = dL[pAxes[0]][a] * dL[pAxes[1]][b];
186
187 if ((int(loc) == 1) || (int(loc) == 3) || (int(loc) == 5))
188 d = coord[axis][n[axis]] - coord[axis][n[axis] - 1];
189 else if ((int(loc) == 0) || (int(loc) == 2) || (int(loc) == 4))
190 d = coord[axis][0] - coord[axis][-1];
191
192 points[ghost] = {ghostId, target, targetId, area, d, 0.0, 0.0, 0.0};
193 }
194 }
195
196 PetscFunctionReturn(0);
197} // getGhostPointList
198
199PetscErrorCode getPerpendAxes(const PetscInt &self, type::IntVec1D &pAxes)
200{
201 PetscFunctionBeginUser;
202
203 pAxes = type::IntVec1D(2, 0);
204
205 switch (self)
206 {
207 case 0:
208 pAxes = {1, 2};
209 break;
210 case 1:
211 pAxes = {0, 2};
212 break;
213 case 2:
214 pAxes = {0, 1};
215 break;
216 default:
217 SETERRQ1(PETSC_COMM_WORLD, PETSC_ERR_ARG_WRONG,
218 "Can not recongnize axis %d in the function "
219 "getPerpendAxes!",
220 self);
221 }
222
223 PetscFunctionReturn(0);
224} // getPerpendAxes
225
226PetscErrorCode getGhostTargetStencil(const type::IntVec1D &n,
227 const type::BCLoc &loc,
228 const type::IntVec1D &pIdx,
229 MatStencil &ghost, MatStencil &target)
230{
231 PetscFunctionBeginUser;
232
233 switch (int(loc))
234 {
235 case 0: // XMINUS
236 target = {pIdx[1], pIdx[0], 0, 0};
237 ghost = {pIdx[1], pIdx[0], -1, 0};
238 break;
239 case 1: // XPLUS
240 target = {pIdx[1], pIdx[0], n[0] - 1, 0};
241 ghost = {pIdx[1], pIdx[0], n[0], 0};
242 break;
243 case 2: // YMINUS
244 target = {pIdx[1], 0, pIdx[0], 0};
245 ghost = {pIdx[1], -1, pIdx[0], 0};
246 break;
247 case 3: // YPLUS
248 target = {pIdx[1], n[1] - 1, pIdx[0], 0};
249 ghost = {pIdx[1], n[1], pIdx[0], 0};
250 break;
251 case 4: // ZMINUS
252 target = {0, pIdx[1], pIdx[0], 0};
253 ghost = {-1, pIdx[1], pIdx[0], 0};
254 break;
255 case 5: // ZPLUS
256 target = {n[2] - 1, pIdx[1], pIdx[0], 0};
257 ghost = {n[2], pIdx[1], pIdx[0], 0};
258 break;
259 default:
260 SETERRQ1(PETSC_COMM_WORLD, PETSC_ERR_ARG_WRONG,
261 "Can not recongnize BC location %s in the function "
262 "getGhostTargetStencil!",
263 type::bl2str[loc].c_str());
264 }
265
266 PetscFunctionReturn(0);
267} // getGhostTargetStencil
268
269} // end of namespace misc
270} // end of namespace petibm
std::shared_ptr< mesh::MeshBase > Mesh
Type definition of Mesh.
Definition: mesh.h:348
PetscErrorCode getGhostTargetStencil(const type::IntVec1D &n, const type::BCLoc &loc, const type::IntVec1D &pIdx, MatStencil &ghost, MatStencil &target)
Get the stencils of a desired ghost point and its corresponding boundary point.
Definition: misc.cpp:226
PetscErrorCode checkBoundaryProc(const DM &da, const type::IntVec1D &n, const type::BCLoc &loc, PetscBool &onThisProc)
Check if a boundary is on this process.
Definition: misc.cpp:85
PetscErrorCode getPerpendAxes(const PetscInt &self, type::IntVec1D &pAxes)
An utility to get the perpendicular axes of a desired axis.
Definition: misc.cpp:199
PetscErrorCode getGhostPointList(const type::Mesh &mesh, const type::Field &field, const type::BCLoc &loc, type::GhostPointsList &points)
Get a list of ghost points on a desired boundary.
Definition: misc.cpp:129
PetscErrorCode checkPeriodicBC(const type::IntVec2D &bcTypes, type::BoolVec2D &periodic)
Check if there is any periodic boundary condition and if these periodic BCs make sense.
Definition: misc.cpp:19
std::vector< IntVec1D > IntVec2D
2D std::vector holding PetscInt.
Definition: type.h:135
Field
Legal fields.
Definition: type.h:80
std::map< BCLoc, std::string > bl2str
Mapping between BCLoc and std::string.
Definition: type.cpp:48
std::vector< PetscBool > BoolVec1D
1D std::vector holding PetscBool.
Definition: type.h:147
std::vector< PetscReal * > GhostedVec2D
a vector of pointers to mimic ghosted 1D vectors.
Definition: type.h:154
Dir
Legal physical directions.
Definition: type.h:68
std::vector< BoolVec1D > BoolVec2D
2D std::vector holding PetscBool.
Definition: type.h:149
std::map< MatStencil, GhostPointInfo > GhostPointsList
A map between MatStencil and GhostPointInfo.
Definition: type.h:177
std::map< Field, std::string > fd2str
Mapping between Field and std::string.
Definition: type.cpp:24
std::vector< PetscInt > IntVec1D
1D std::vector holding PetscInt.
Definition: type.h:133
BCLoc
Location of a boundary.
Definition: type.h:108
std::map< Dir, std::string > dir2str
Mapping between Dir and std::string.
Definition: type.cpp:18
@ PERIODIC
Definition: type.h:96
@ NOBC
Definition: type.h:95
@ ZMINUS
Definition: type.h:113
@ XMINUS
Definition: type.h:109
@ YMINUS
Definition: type.h:111
Prototype of mesh::MeshBase, type::Mesh, and factory function.
Prototypes of some miscellaneous functions.
Frequently used types, structures, and enums.
Definition: bodypack.h:255
A toolbox for building flow solvers.
Definition: bodypack.h:52