Line data Source code
1 :
2 : #ifndef AMREX_COORDSYS_H_
3 : #define AMREX_COORDSYS_H_
4 : #include <AMReX_Config.H>
5 :
6 : #include <AMReX.H>
7 : #include <AMReX_REAL.H>
8 : #include <AMReX_Array.H>
9 : #include <AMReX_Vector.H>
10 : #include <AMReX_Box.H>
11 :
12 : #include <limits>
13 :
14 : namespace amrex {
15 :
16 : class FArrayBox;
17 :
18 : /**
19 : * \brief Coordinate System
20 : *
21 : * Routines for mapping between physical coordinate system and index space.
22 : */
23 : class CoordSys
24 : {
25 : public:
26 :
27 : enum CoordType { undef = -1, cartesian = 0, RZ = 1, SPHERICAL = 2 };
28 : //! Nice ASCII output.
29 : friend std::ostream& operator<< (std::ostream&, const CoordSys& );
30 :
31 : //! Nice ASCII input.
32 : friend std::istream& operator>> (std::istream&, CoordSys& );
33 :
34 : //! Is ok?
35 : [[nodiscard]] bool Ok () const noexcept { return ok; }
36 :
37 : //! Set the CoordType.
38 : void SetCoord (CoordType coord) noexcept { c_sys = coord; }
39 :
40 : //! Returns the CoordType.
41 : [[nodiscard]] CoordType Coord () const noexcept { return c_sys; }
42 :
43 : //! Returns the CoordType as an int.
44 : [[nodiscard]] int CoordInt () const noexcept { return static_cast<int>(c_sys); }
45 :
46 : //! Is CoordType == SPHERICAL?
47 : [[nodiscard]] bool IsSPHERICAL () const noexcept {
48 : BL_ASSERT(c_sys != undef); return (c_sys == SPHERICAL);
49 : }
50 :
51 : //! Is CoordType == RZ?
52 : [[nodiscard]] bool IsRZ () const noexcept {
53 : BL_ASSERT(c_sys != undef); return (c_sys == RZ);
54 : }
55 :
56 : //! Is CoordType == cartesian?
57 : [[nodiscard]] bool IsCartesian () const noexcept {
58 : BL_ASSERT(c_sys != undef); return (c_sys == cartesian);
59 : }
60 :
61 : //! Sets the offset for each coordinate direction.
62 : void SetOffset (const Real* x_lo) noexcept;
63 :
64 : //! Returns the offset.
65 : [[nodiscard]] const Real* Offset () const noexcept { return offset; }
66 :
67 : //! Returns the offset for the specified coordinate direction.
68 : [[nodiscard]] Real Offset (int dir) const noexcept { return offset[dir]; }
69 :
70 : //! Returns the cellsize for each coordinate direction.
71 3354990 : [[nodiscard]] const Real* CellSize () const noexcept { BL_ASSERT(ok); return dx; }
72 :
73 : //! Returns the cellsize for the specified coordinate direction.
74 : [[nodiscard]] Real CellSize (int dir) const noexcept { BL_ASSERT(ok); return dx[dir]; }
75 :
76 : [[nodiscard]] GpuArray<Real,AMREX_SPACEDIM> CellSizeArray () const noexcept {
77 : BL_ASSERT(ok);
78 : return {{ AMREX_D_DECL(dx[0],dx[1],dx[2]) }};
79 : }
80 :
81 : //! Returns the inverse cellsize for each coordinate direction.
82 : [[nodiscard]] const Real* InvCellSize () const noexcept { BL_ASSERT(ok); return inv_dx; }
83 :
84 : //! Returns the inverse cellsize for the specified coordinate direction.
85 : [[nodiscard]] Real InvCellSize (int dir) const noexcept { BL_ASSERT(ok); return inv_dx[dir]; }
86 :
87 : [[nodiscard]] GpuArray<Real,AMREX_SPACEDIM> InvCellSizeArray () const noexcept {
88 : BL_ASSERT(ok);
89 : return {{ AMREX_D_DECL(inv_dx[0],inv_dx[1],inv_dx[2]) }};
90 : }
91 :
92 : //! Returns location of cell center in specified direction.
93 : [[nodiscard]] Real CellCenter (int point, int dir) const noexcept
94 : {
95 : BL_ASSERT(ok); return offset[dir] + dx[dir]*((Real)0.5+ (Real)point);
96 : }
97 :
98 : //! Return location of cell center.
99 : void CellCenter (const IntVect& point, Vector<Real>& loc) const noexcept;
100 :
101 : //! Return location of cell center.
102 : void CellCenter (const IntVect& point, Real* loc) const noexcept;
103 :
104 : //! Returns location of lo edge in specified direction.
105 : [[nodiscard]] Real LoEdge (int point, int dir) const noexcept
106 : {
107 : BL_ASSERT(ok); return offset[dir] + dx[dir]*static_cast<Real>(point);
108 : }
109 :
110 : //! Equivalent to LoEdge(point[dir], dir).
111 : [[nodiscard]] Real LoEdge (const IntVect& point, int dir) const noexcept
112 : {
113 : BL_ASSERT(ok); return offset[dir] + dx[dir]*static_cast<Real>(point[dir]);
114 : }
115 :
116 : //! Returns location of hi edge in specified direction.
117 : [[nodiscard]] Real HiEdge (int point, int dir) const noexcept
118 : {
119 : BL_ASSERT(ok); return offset[dir] + dx[dir]*static_cast<Real>(point + 1);
120 : }
121 :
122 : //! Equivalent to HiEdge(point[dir], dir).
123 : [[nodiscard]] Real HiEdge (const IntVect& point, int dir) const noexcept
124 : {
125 : BL_ASSERT(ok); return offset[dir] + dx[dir]*static_cast<Real>(point[dir] + 1);
126 : }
127 :
128 : //! Sets location of lo face into loc.
129 : void LoFace (const IntVect& point, int dir, Vector<Real>& loc) const noexcept;
130 :
131 : //! Sets location of lo face into loc.
132 : void LoFace (const IntVect& point, int dir, Real* loc) const noexcept;
133 :
134 : //! Sets location of hi face into loc.
135 : void HiFace (const IntVect& point, int dir, Vector<Real>& loc) const noexcept;
136 :
137 : //! Sets location of hi face into loc.
138 : void HiFace (const IntVect& point, int dir, Real* loc) const noexcept;
139 :
140 : //! Return location of lower left hand corner.
141 : void LoNode (const IntVect& point, Vector<Real>& loc) const noexcept;
142 :
143 : //! Return location of lower left hand corner.
144 : void LoNode (const IntVect& point, Real* loc) const noexcept;
145 :
146 : //! Return location of upper right hand corner.
147 : void HiNode (const IntVect& point, Vector<Real>& loc) const noexcept;
148 :
149 : //! Return location of upper right hand corner.
150 : void HiNode (const IntVect& point, Real* loc) const noexcept;
151 : /**
152 : * \brief Returns cell centered index of cell containing point.
153 : * This may return undesired results if point
154 : * is on a cell boundary.
155 : */
156 : IntVect CellIndex (const Real* point) const noexcept;
157 : /**
158 : * \brief Returns node centered index of lower left hand corner of
159 : * cell containing this point.
160 : */
161 : IntVect LowerIndex (const Real* point) const noexcept;
162 : /**
163 : * \brief Returns node centered index of upper right hand corner of
164 : * cell containing this point.
165 : */
166 : IntVect UpperIndex (const Real* point) const noexcept;
167 : /**
168 : * \brief Compute cell volumes in given region and place them into
169 : * input FAB.
170 : */
171 : void SetVolume (FArrayBox& a_volfab, const Box& region) const;
172 : /**
173 : * \brief Compute cell volumes in given region and place them into
174 : * resize()d input FAB.
175 : */
176 : void GetVolume (FArrayBox& vol, const Box& region) const;
177 : /**
178 : * \brief Compute d(log(A))/dr at cell centers in given region and
179 : * place them into input FAB.
180 : */
181 : void SetDLogA (FArrayBox& a_dlogafab, const Box& region, int dir) const;
182 : /**
183 : * \brief Compute d(log(A))/dr at cell centers in given region and
184 : * return the results in the resize()d input FAB.
185 : */
186 : void GetDLogA (FArrayBox& dloga, const Box& region, int dir) const;
187 :
188 : //! Return the volume of the specified cell.
189 : [[nodiscard]] Real Volume (const IntVect& point) const;
190 :
191 : //! Return the volume of the specified cell.
192 : [[nodiscard]] Real Volume (const Real xlo[AMREX_SPACEDIM],
193 : const Real xhi[AMREX_SPACEDIM]) const;
194 :
195 : /**
196 : * \brief Compute area of cell faces in given region and given
197 : * index direction and return the result in input FAB.
198 : */
199 : void SetFaceArea (FArrayBox& a_areafab, const Box& region, int dir) const;
200 :
201 : /**
202 : * \brief Compute area of cell faces in given region and given
203 : * index direction and return the result in resize()d input FAB.
204 : */
205 : void GetFaceArea (FArrayBox& area, const Box& region, int dir) const;
206 :
207 : //! Returns lo face area of given cell in direction dir.
208 : [[nodiscard]] Real AreaLo (const IntVect& point, int dir) const noexcept;
209 :
210 : //! Returns hi face area of given cell in direction dir.
211 : [[nodiscard]] Real AreaHi (const IntVect& point, int dir) const noexcept;
212 :
213 : /**
214 : * \brief Return array of physical locations of cell edges
215 : * in the resize()d input array.
216 : */
217 : void GetEdgeLoc (Vector<Real>& loc, const Box& region, int dir) const;
218 :
219 : /**
220 : * \brief Return array of physical locations of cell centers
221 : * in the resize()d input array.
222 : */
223 : void GetCellLoc (Vector<Real>& loc, const Box& region, int dir) const;
224 :
225 : /**
226 : * \brief Return array of volume coordinates at cell edges
227 : * for region in given direction.
228 : */
229 : void GetEdgeVolCoord (Vector<Real>& vc, const Box& region, int dir) const;
230 :
231 : /**
232 : * \brief Return array of volume coordinates at cell centers
233 : * for region in given direction.
234 : */
235 : void GetCellVolCoord (Vector<Real>& vc, const Box& region, int dir) const;
236 :
237 : protected:
238 : // c_sys and offset used to be static
239 : CoordType c_sys = undef;
240 : Real offset[AMREX_SPACEDIM];
241 :
242 : Real dx[AMREX_SPACEDIM] = {AMREX_D_DECL(0.,0.,0.)};
243 : Real inv_dx[AMREX_SPACEDIM]
244 : = {AMREX_D_DECL(std::numeric_limits<Real>::infinity(),
245 : std::numeric_limits<Real>::infinity(),
246 : std::numeric_limits<Real>::infinity())};
247 : bool ok = false;
248 : };
249 :
250 : }
251 :
252 : #endif /*_COORDSYS_H_*/
|