PetIBM 0.5.4
Toolbox and applications of the immersed-boundary method for distributed-memory architectures
lininterp.cpp
Go to the documentation of this file.
1
8#include <petibm/lininterp.h>
9
10namespace petibm
11{
12
13namespace misc
14{
15// Factory function to create a linear interpolation object.
16PetscErrorCode createLinInterp(const MPI_Comm &comm,
17 const type::RealVec1D &point,
18 const type::Mesh &mesh,
19 const type::Field &field,
20 type::LinInterp &interp)
21{
22 PetscFunctionBeginUser;
23
24 switch (mesh->dim)
25 {
26 case 2:
27 interp = std::make_shared<BiLinInterp>(comm, point, mesh, field);
28 break;
29 case 3:
30 interp = std::make_shared<TriLinInterp>(comm, point, mesh, field);
31 break;
32 default:
33 SETERRQ(comm, PETSC_ERR_ARG_UNKNOWN_TYPE,
34 "Unknown number of dimensions. Accepted values are: 2 and 3");
35 }
36
37 PetscFunctionReturn(0);
38} // createLinInterp
39
40//***************************************************************************//
41//************************* LinInterpBase *************************//
42//***************************************************************************//
43
44// Destructor
46{
47 PetscErrorCode ierr;
48 PetscBool finalized;
49
50 PetscFunctionBeginUser;
51
52 ierr = PetscFinalized(&finalized); CHKERRV(ierr);
53 if (finalized) return;
54
55 ierr = destroy(); CHKERRV(ierr);
56} // LinInterpBase::~LinInterpBase
57
58// Manually destroy the data of the object.
59PetscErrorCode LinInterpBase::destroy()
60{
61 PetscFunctionBeginUser;
62
63 comm = MPI_COMM_NULL;
64 commSize = commRank = 0;
65
66 PetscFunctionReturn(0);
67} // LinInterpBase::destroy
68
69// Initialize the interpolation object.
70PetscErrorCode LinInterpBase::init(const MPI_Comm &inComm,
71 const type::RealVec1D &point,
72 const type::Mesh &mesh,
73 const type::Field &field)
74{
75 PetscErrorCode ierr;
76
77 PetscFunctionBeginUser;
78
79 comm = inComm;
80 ierr = MPI_Comm_size(comm, &commSize); CHKERRQ(ierr);
81 ierr = MPI_Comm_rank(comm, &commRank); CHKERRQ(ierr);
82
83 target = point;
84 idxDirs = type::IntVec1D(mesh->dim, 0);
85 bl = type::RealVec1D(mesh->dim, 0.0);
86 tr = type::RealVec1D(mesh->dim, 0.0);
87
88 ierr = getBoxCoords(mesh, field); CHKERRQ(ierr);
89
90 PetscFunctionReturn(0);
91} // LinInterpBase::init
92
93// Get the gridline indices of the front-bottom-left neighbor.
95 const type::Field &field)
96{
97 PetscFunctionBeginUser;
98
99 std::vector<PetscReal>::iterator low;
100 for (PetscInt d = 0; d < mesh->dim; ++d)
101 {
102 type::RealVec1D line(mesh->coord[field][d],
103 mesh->coord[field][d] + mesh->n[field][d]);
104 low = std::lower_bound(line.begin(), line.end(), target[d]);
105 idxDirs[d] = low - line.begin() - 1;
106 }
107
108 PetscFunctionReturn(0);
109} // LinInterpBase::getBLGridlineIndices
110
111// Get the coordinates of the neighbors.
112PetscErrorCode LinInterpBase::getBoxCoords(const type::Mesh &mesh,
113 const type::Field &field)
114{
115 PetscErrorCode ierr;
116
117 PetscFunctionBeginUser;
118
119 ierr = getBLGridlineIndices(mesh, field); CHKERRQ(ierr);
120
121 for (PetscInt d = 0; d < mesh->dim; ++d)
122 {
123 bl[d] = mesh->coord[field][d][idxDirs[d]];
124 tr[d] = mesh->coord[field][d][idxDirs[d] + 1];
125 }
126
127 PetscFunctionReturn(0);
128} // LinInterpBase::getBoxCoords
129
130//***************************************************************************//
131//************************* TriLinInterp *************************//
132//***************************************************************************//
133
134// Constructor. Initialize the tri-linear interpolation object.
135TriLinInterp::TriLinInterp(const MPI_Comm &comm,
136 const type::RealVec1D &point,
137 const type::Mesh &mesh,
138 const type::Field &field)
139{
140 init(comm, point, mesh, field);
141} // TriLinInterp::TriLinInterp
142
143// Perform tri-linear interpolation.
144PetscErrorCode TriLinInterp::interpolate(const DM &da, const Vec &vec, PetscReal &v)
145{
146 PetscErrorCode ierr;
147 PetscReal ***a;
148 PetscReal c00, c01, c10, c11, c0, c1;
149 PetscInt &io = idxDirs[0], &jo = idxDirs[1], &ko = idxDirs[2];
150 PetscReal &x = target[0], &y = target[1], &z = target[2];
151 PetscReal &x0 = bl[0], &y0 = bl[1], &z0 = bl[2],
152 &x1 = tr[0], &y1 = tr[1], &z1 = tr[2];
153 PetscReal xd, yd, zd;
154
155 PetscFunctionBeginUser;
156
157 ierr = DMDAVecGetArrayRead(da, vec, &a); CHKERRQ(ierr);
158 xd = (x - x0) / (x1 - x0);
159 yd = (y - y0) / (y1 - y0);
160 zd = (z - z0) / (z1 - z0);
161 c00 = (1 - xd) * a[ko][jo][io] + xd * a[ko][jo][io+1];
162 c01 = (1 - xd) * a[ko][jo+1][io] + xd * a[ko][jo+1][io+1];
163 c10 = (1 - xd) * a[ko+1][jo][io] + xd * a[ko+1][jo][io+1];
164 c11 = (1 - xd) * a[ko+1][jo+1][io] + xd * a[ko+1][jo+1][io+1];
165 c0 = (1 - yd) * c00 + yd * c01;
166 c1 = (1 - yd) * c10 + yd * c11;
167 v = (1 - zd) * c0 + zd * c1;
168 ierr = DMDAVecRestoreArrayRead(da, vec, &a); CHKERRQ(ierr);
169
170 PetscFunctionReturn(0);
171} // TriLinInterp::interpolate
172
173//***************************************************************************//
174//************************* BiLinInterp *************************//
175//***************************************************************************//
176
177// Constructor. Initialize the bi-linear interpolation object.
178BiLinInterp::BiLinInterp(const MPI_Comm &comm,
179 const type::RealVec1D &point,
180 const type::Mesh &mesh,
181 const type::Field &field)
182{
183 init(comm, point, mesh, field);
184} // BiLinInterp::BiLinInterp
185
186// Perform bi-linear interpolation.
187PetscErrorCode BiLinInterp::interpolate(const DM &da, const Vec &vec, PetscReal &v)
188{
189 PetscErrorCode ierr;
190 PetscReal **a;
191 PetscReal c0, c1;
192 PetscInt &io = idxDirs[0], &jo = idxDirs[1];
193 PetscReal &x = target[0], &y = target[1];
194 PetscReal &x0 = bl[0], &y0 = bl[1],
195 &x1 = tr[0], &y1 = tr[1];
196 PetscReal xd, yd;
197
198 PetscFunctionBeginUser;
199
200 ierr = DMDAVecGetArrayRead(da, vec, &a); CHKERRQ(ierr);
201 xd = (x - x0) / (x1 - x0);
202 yd = (y - y0) / (y1 - y0);
203 c0 = (1 - xd) * a[jo][io] + xd * a[jo][io + 1];
204 c1 = (1 - xd) * a[jo + 1][io] + xd * a[jo + 1][io + 1];
205 v = (1 - yd) * c0 + yd * c1;
206 ierr = DMDAVecRestoreArrayRead(da, vec, &a); CHKERRQ(ierr);
207
208 PetscFunctionReturn(0);
209} // BiLinInterp::interpolate
210
211} // end of namespace misc
212
213} // end of namespace petibm
PetscErrorCode interpolate(const DM &da, const Vec &vec, PetscReal &v)
Definition: lininterp.cpp:187
BiLinInterp(const MPI_Comm &comm, const type::RealVec1D &point, const type::Mesh &mesh, const type::Field &field)
Constructor. Initialize the linear interpolation object.
Definition: lininterp.cpp:178
type::RealVec1D bl
Coordinates of the front-bottom-left neighbor.
Definition: lininterp.h:53
PetscMPIInt commSize
Size of the MPI communicator.
Definition: lininterp.h:65
type::RealVec1D target
Coordinates of the point to interpolate.
Definition: lininterp.h:50
type::RealVec1D tr
Coordinates of the back-top-right neighbor.
Definition: lininterp.h:56
MPI_Comm comm
MPI communicator.
Definition: lininterp.h:62
PetscErrorCode getBoxCoords(const type::Mesh &mesh, const type::Field &field)
Get the coordinates of the neighbors.
Definition: lininterp.cpp:112
PetscMPIInt commRank
Rank of the local process in the communicator.
Definition: lininterp.h:68
PetscErrorCode init(const MPI_Comm &comm, const type::RealVec1D &point, const type::Mesh &mesh, const type::Field &field)
Initialize the linear interpolation object.
Definition: lininterp.cpp:70
PetscErrorCode getBLGridlineIndices(const type::Mesh &mesh, const type::Field &field)
Get the gridline indices of the front-bottom-left neighbor.
Definition: lininterp.cpp:94
type::IntVec1D idxDirs
Gridline indices of the front-bottom-left neighbor.
Definition: lininterp.h:59
PetscErrorCode destroy()
Manually destroy data in the object.
Definition: lininterp.cpp:59
PetscErrorCode interpolate(const DM &da, const Vec &vec, PetscReal &v)
Definition: lininterp.cpp:144
TriLinInterp(const MPI_Comm &comm, const type::RealVec1D &point, const type::Mesh &mesh, const type::Field &field)
Constructor. Initialize the linear interpolation object.
Definition: lininterp.cpp:135
std::shared_ptr< mesh::MeshBase > Mesh
Type definition of Mesh.
Definition: mesh.h:348
std::shared_ptr< misc::LinInterpBase > LinInterp
Type definition of LinInterp.
Definition: lininterp.h:172
PetscErrorCode createLinInterp(const MPI_Comm &comm, const type::RealVec1D &point, const type::Mesh &mesh, const type::Field &field, type::LinInterp &interp)
Factory function to create a linear interpolation object.
Definition: lininterp.cpp:16
Field
Legal fields.
Definition: type.h:80
std::vector< PetscInt > IntVec1D
1D std::vector holding PetscInt.
Definition: type.h:133
std::vector< PetscReal > RealVec1D
1D std::vector holding PetscReal.
Definition: type.h:140
Prototype of the linear interpolation classes, definition of type::LinInterp, and factory function.
A toolbox for building flow solvers.
Definition: bodypack.h:52