criptic v1
Cosmic Ray Interstellar Propagation Tool using Itô Calculus
Loading...
Searching...
No Matches
CartesianGrid.H
Go to the documentation of this file.
1
11#ifndef _CARTESIANGRID_H_
12#define _CARTESIANGRID_H_
13
14#include <array>
15#include <vector>
16#include "Gas.H"
17#include "../Core/Geometry.H"
18#include "../IO/ParmParser.H"
19#include "../Utils/ErrCodes.H"
20#include "../Utils/RealTensor2.H"
21#include "../Utils/Vec3.H"
22
23namespace criptic {
24 namespace gas {
25
38 class CartesianGrid : public Gas {
39
40 public:
41
66
74 CartesianGrid(const ParmParser& pp,
75 const Geometry& geom_,
76 const IdxType nBB_,
77 const int nGhost_ = 1);
78
86 CartesianGrid(const IdxVec& nGrid_,
87 const Geometry& geom_,
88 const IdxType nBB_,
89 const int nGhost_ = 1);
90
101 virtual void frame(const RealVec &x,
102 const Real t,
103 RealVec& v,
104 TNBBasis& tnb) const override;
105
115 virtual GasData gasData(const RealVec &x,
116 const Real t) const override;
117
124 virtual Real dxGhost() const override { return nGhost * dx.min(); }
125
129 void applyBC();
130
135 const IdxVec& gridSize() const { return nGrid; }
136
141 IdxType nComponents() const { return nComp; }
142
147 const RealBox& xBox() const { return geom.xBox(); }
148
153 const RealVec& xLo() const { return geom.xLo(); }
154
159 const RealVec& xHi() const { return geom.xHi(); }
160
165 const RealVec& xSize() const { return geom.xSize(); }
166
173 template<class T,
174 std::enable_if_t<std::is_integral<T>::value, bool> = true >
175 IdxType idx(const Vec3<T>& iv, IdxType n) const {
176 IdxType i = n + nComp *
177 (iv[0] + nGhost + (nGrid[0] + 2*nGhost) *
178 (iv[1] + nGhost + (nGrid[1] + 2*nGhost) *
179 (iv[2] + nGhost)));
180 return i;
181 }
182
189 void posToIdx(const RealVec& x,
190 SIdxVec& iv,
191 RealVec& f) const {
192 RealVec ix = (x - geom.xLo()) / dx - 0.5;
193 iv = floor(ix);
194 f = ix - iv;
195 }
196
203 template<class T,
204 std::enable_if_t<std::is_integral<T>::value, bool> = true >
206 const RealVec& f = zeroIdxVec) const {
207 return geom.xLo() + (iv + 0.5) * dx;
208 }
209
215 template<class T,
216 std::enable_if_t<std::is_integral<T>::value, bool> = true >
217 Real& operator()(const Vec3<T>& iv, const IdxType n) {
218 return data[idx(iv, n)];
219 }
220
226 template<class T,
227 std::enable_if_t<std::is_integral<T>::value, bool> = true >
228 const Real& operator()(const Vec3<T>& iv, const IdxType n) const {
229 return data[idx(iv, n)];
230 }
231
240 const SignedIdxType j,
241 const SignedIdxType k,
242 const IdxType n) {
243 SIdxVec iv(i,j,k);
244 return data[idx(iv, n)];
245 }
246
255 const SignedIdxType j,
256 const SignedIdxType k,
257 const IdxType n) const {
258 SIdxVec iv(i,j,k);
259 return data[idx(iv, n)];
260 }
261
274 template<class T,
275 std::enable_if_t<std::is_integral<T>::value, bool> = true >
276 std::vector<Real> interp(const Vec3<T>& iv,
277 const RealVec& f,
278 const IdxType sComp = 0,
279 const IdxType eComp = maxIdx) const {
280 IdxType eComp_ = eComp > nComp ? nComp : eComp;
281 assert(sComp < eComp_);
282 std::vector<Real> q(eComp_ - sComp);
283 SIdxVec ivTmp;
284 for (ivTmp[2] = iv[2]; ivTmp[2] <= iv[2]+1; ivTmp[2]++) {
285 Real fz = (ivTmp[2] == iv[2]) ? 1.0 - f[2] : f[2];
286 for (ivTmp[1] = iv[1]; ivTmp[1] <= iv[1]+1; ivTmp[1]++) {
287 Real fy = (ivTmp[1] == iv[1]) ? 1.0 - f[1] : f[1];
288 for (ivTmp[0] = iv[0]; ivTmp[0] <= iv[0]+1; ivTmp[0]++) {
289 Real fx = (ivTmp[0] == iv[0]) ? 1.0 - f[0] : f[0];
290 for (IdxType c = sComp; c < eComp_; c++) {
291 q[c - sComp] += fx * fy * fz * (*this)(ivTmp, c);
292 }
293 }
294 }
295 }
296 return q;
297 }
298
310 template<class T,
311 std::enable_if_t<std::is_integral<T>::value, bool> = true >
313 const RealVec& f,
314 const IdxType c) const {
315 RealVec q;
316 SIdxVec ivTmp;
317 for (ivTmp[2] = iv[2]; ivTmp[2] <= iv[2]+1; ivTmp[2]++) {
318 Real fz = (ivTmp[2] == iv[2]) ? 1.0 - f[2] : f[2];
319 for (ivTmp[1] = iv[1]; ivTmp[1] <= iv[1]+1; ivTmp[1]++) {
320 Real fy = (ivTmp[1] == iv[1]) ? 1.0 - f[1] : f[1];
321 for (ivTmp[0] = iv[0]; ivTmp[0] <= iv[0]+1; ivTmp[0]++) {
322 Real fx = (ivTmp[0] == iv[0]) ? 1.0 - f[0] : f[0];
323 for (IdxType c_ = 0; c_ < 3; c_++) {
324 q[c_] += fx * fy * fz * (*this)(ivTmp, c + c_);
325 }
326 }
327 }
328 }
329 return q;
330 }
331
339 template<class T,
340 std::enable_if_t<std::is_integral<T>::value, bool> = true >
342 const RealVec& f,
343 const IdxType c) const {
344 RealVec q;
345 for (int d = 0; d < 3; d++) {
346 SIdxVec ivTmp;
347 for (ivTmp[2] = iv[2]; ivTmp[2] <= iv[2]+1; ivTmp[2]++) {
348 Real fz;
349 if (d != 2) {
350 fz = (ivTmp[2] == iv[2]) ? 1.0 - f[2] : f[2];
351 } else {
352 fz = (ivTmp[2] == iv[2]) ? -1.0 : 1.0;
353 }
354 for (ivTmp[1] = iv[1]; ivTmp[1] <= iv[1]+1; ivTmp[1]++) {
355 Real fy;
356 if (d != 1) {
357 fy = (ivTmp[1] == iv[1]) ? 1.0 - f[1] : f[1];
358 } else {
359 fy = (ivTmp[1] == iv[1]) ? -1.0 : 1.0;
360 }
361 for (ivTmp[0] = iv[0]; ivTmp[0] <= iv[0]+1; ivTmp[0]++) {
362 Real fx;
363 if (d != 0) {
364 fx = (ivTmp[0] == iv[0]) ? 1.0 - f[0] : f[0];
365 } else {
366 fx = (ivTmp[0] == iv[0]) ? -1.0 : 1.0;
367 }
368 q[d] += fx * fy * fz * (*this)(ivTmp, c);
369 }
370 }
371 }
372 }
373 return q/dx;
374 }
375
383 template<class T,
384 std::enable_if_t<std::is_integral<T>::value, bool> = true >
386 const RealVec& f,
387 const IdxType c) const {
388 RealTensor2 q;
389 for (int d = 0; d < 3; d++) {
390 SIdxVec ivTmp;
391 for (ivTmp[2] = iv[2]; ivTmp[2] <= iv[2]+1; ivTmp[2]++) {
392 Real fz;
393 if (d != 2) {
394 fz = (ivTmp[2] == iv[2]) ? 1.0 - f[2] : f[2];
395 } else {
396 fz = (ivTmp[2] == iv[2]) ? -1.0 : 1.0;
397 }
398 for (ivTmp[1] = iv[1]; ivTmp[1] <= iv[1]+1; ivTmp[1]++) {
399 Real fy;
400 if (d != 1) {
401 fy = (ivTmp[1] == iv[1]) ? 1.0 - f[1] : f[1];
402 } else {
403 fy = (ivTmp[1] == iv[1]) ? -1.0 : 1.0;
404 }
405 for (ivTmp[0] = iv[0]; ivTmp[0] <= iv[0]+1; ivTmp[0]++) {
406 Real fx;
407 if (d != 0) {
408 fx = (ivTmp[0] == iv[0]) ? 1.0 - f[0] : f[0];
409 } else {
410 fx = (ivTmp[0] == iv[0]) ? -1.0 : 1.0;
411 }
412 for (int c_ = 0; c_ < 3; c_++) {
413 q[3*c_ + d] += fx * fy * fz * (*this)(ivTmp, c + c_);
414 }
415 }
416 }
417 }
418 }
419 for (int c_ = 0; c_ < 3; c_++) {
420 for (int d = 0; d < 3; d++) {
421 q[3*c_ + d] /= dx[d];
422 }
423 }
424 return q;
425 }
426
427 private:
428
429 // Private data
430 const Geometry& geom;
436 std::vector<Real> data;
438 };
439 }
440}
441
442#endif
443// _CARTESIANGRID_H_
Interface used to describe background gas.
A class that describes the geometry of a calculation.
Definition Geometry.H:32
const RealBox & xBox() const
Return the domain bounding box.
Definition Geometry.H:59
const RealVec & xSize() const
Return size of computational domain.
Definition Geometry.H:77
const RealVec & xHi() const
Return high boundary of computational domain.
Definition Geometry.H:71
const RealVec & xLo() const
Return low boundary of computational domain.
Definition Geometry.H:65
Class to parse the criptic input deck.
Definition ParmParser.H:37
Class that represents a 3D rectangular prism.
Definition RealBox.H:30
Class that represents a rank 2 tensor.
Definition RealTensor2.H:34
T min() const
Return the value of the smallest element.
Definition Vec3.H:532
Describe a gas with properties stored on a Cartesian grid.
Definition CartesianGrid.H:38
IdxType nComponents() const
Return the number of components.
Definition CartesianGrid.H:141
gasIdxType
Convenience enum to give specify indices in gas array.
Definition CartesianGrid.H:48
@ xHep2Idx
Definition CartesianGrid.H:61
@ vyIdx
Definition CartesianGrid.H:52
@ xeIdx
Definition CartesianGrid.H:62
@ nf
Definition CartesianGrid.H:64
@ vzIdx
Definition CartesianGrid.H:53
@ xHe0Idx
Definition CartesianGrid.H:59
@ BxIdx
Definition CartesianGrid.H:54
@ xHepIdx
Definition CartesianGrid.H:60
@ ZIdx
Definition CartesianGrid.H:63
@ xHpIdx
Definition CartesianGrid.H:58
@ xH0Idx
Definition CartesianGrid.H:57
@ ionDenIdx
Definition CartesianGrid.H:50
@ ByIdx
Definition CartesianGrid.H:55
@ vxIdx
Definition CartesianGrid.H:51
@ BzIdx
Definition CartesianGrid.H:56
@ denIdx
Definition CartesianGrid.H:49
RealTensor2 interpGradVector(const Vec3< T > &iv, const RealVec &f, const IdxType c) const
Compute gradient of a vector quantity by linear interpolation.
Definition CartesianGrid.H:385
IdxType idx(const Vec3< T > &iv, IdxType n) const
Convert an ((i,j,k),n) index to a memory location.
Definition CartesianGrid.H:175
IdxType nComp
Definition CartesianGrid.H:432
RealVec interpGradScalar(const Vec3< T > &iv, const RealVec &f, const IdxType c) const
Compute gradient of a scalar quantity by linear interpolation.
Definition CartesianGrid.H:341
RealVec interpVec(const Vec3< T > &iv, const RealVec &f, const IdxType c) const
Linearly interpolate one vector component to a given point.
Definition CartesianGrid.H:312
virtual void frame(const RealVec &x, const Real t, RealVec &v, TNBBasis &tnb) const override
Compute the comoving TNB frame for the gas.
Definition CartesianGrid.cpp:661
const RealBox & xBox() const
Return the domain bounding box.
Definition CartesianGrid.H:147
const Geometry & geom
Definition CartesianGrid.H:430
virtual Real dxGhost() const override
Size of the ghost region in the gas data.
Definition CartesianGrid.H:124
const IdxVec & gridSize() const
Return the grid size.
Definition CartesianGrid.H:135
IdxType nBB
Definition CartesianGrid.H:433
RealVec dx
Definition CartesianGrid.H:435
const RealVec & xHi() const
Return high boundary of computational domain.
Definition CartesianGrid.H:159
Real & operator()(const Vec3< T > &iv, const IdxType n)
Access data by index.
Definition CartesianGrid.H:217
IdxVec nGrid
Definition CartesianGrid.H:431
const Real & operator()(const Vec3< T > &iv, const IdxType n) const
Access data by index.
Definition CartesianGrid.H:228
const RealVec & xSize() const
Return size of computational domain.
Definition CartesianGrid.H:165
const RealVec & xLo() const
Return low boundary of computational domain.
Definition CartesianGrid.H:153
std::vector< Real > data
Definition CartesianGrid.H:436
std::vector< Real > interp(const Vec3< T > &iv, const RealVec &f, const IdxType sComp=0, const IdxType eComp=maxIdx) const
Linearly interpolate grid data to specified point.
Definition CartesianGrid.H:276
Real & operator()(const SignedIdxType i, const SignedIdxType j, const SignedIdxType k, const IdxType n)
Access data by index.
Definition CartesianGrid.H:239
virtual GasData gasData(const RealVec &x, const Real t) const override
Return background gas state.
Definition CartesianGrid.cpp:683
RealVec idxToPos(const Vec3< T > &iv, const RealVec &f=zeroIdxVec) const
Convert a position in cell coordinates to a physical position.
Definition CartesianGrid.H:205
IdxType nGhost
Definition CartesianGrid.H:434
const Real & operator()(const SignedIdxType i, const SignedIdxType j, const SignedIdxType k, const IdxType n) const
Access data by index.
Definition CartesianGrid.H:254
void posToIdx(const RealVec &x, SIdxVec &iv, RealVec &f) const
Convert a physical position to cell coordinates.
Definition CartesianGrid.H:189
void applyBC()
Fill the ghost cells using the specified boundary condition.
Definition CartesianGrid.cpp:83
Trivial class to hold gas data.
Definition GasData.H:29
Interface class to describe background gas.
Definition Gas.H:44
The primary namespace for criptic objects.
Definition AdvancePacket.H:25
constexpr IdxType maxIdx
Definition Types.H:55
Vec3< T > floor(const criptic::Vec3< T > &v)
Return the elementwise floor of a Vec3.
Definition Vec3.H:755
std::ptrdiff_t SignedIdxType
Definition Types.H:46
std::vector< Real >::size_type IdxType
Definition Types.H:45
double Real
Definition Types.H:38
static const IdxVec zeroIdxVec(0, 0, 0)
Structure to hold TNB basis data.
Definition RealTensor2.H:898