ERF
Energy Research and Forecasting: An Atmospheric Modeling Code
ERF_EBIFTerrain.H
Go to the documentation of this file.
1 /**
2  * \file ERF_EBIFTerrain.H
3  * \brief Defines the implicit function used to build EB terrain geometry.
4  */
5 #ifndef ERF_TERRAIN_IF_H_
6 #define ERF_TERRAIN_IF_H_
7 
8 #include <AMReX_Array.H>
9 #include <AMReX_EB2_IF_Base.H>
10 
11 #include <cmath>
12 #include <algorithm>
13 
14 //! AMReX EB implicit-function sign convention: >0 is body, 0 is boundary, <0 is fluid.
15 
16 /**
17  * \brief Terrain implicit function backed by gridded terrain heights.
18  *
19  * The operator interpolates terrain height in x-y, maps the query z coordinate
20  * through stretched vertical spacing, and returns the AMReX EB signed value.
21  */
22 class TerrainIF
23  : amrex::GPUable
24 {
25 public:
26 
27  /**
28  * \brief Construct a terrain implicit function from height and geometry data.
29  * \param a_z_terrain Terrain elevation values on the horizontal mesh.
30  * \param a_geom Geometry used for grid spacing and problem bounds.
31  * \param a_dz_stretched Device vector of stretched vertical cell sizes.
32  * \param a_periodic_wrap In a periodic direction of a_geom, evaluate at the periodic image
33  * inside the domain, so that the surface is the same across the periodic faces.
34  */
35  TerrainIF (amrex::FArrayBox const& a_z_terrain, amrex::Geometry const& a_geom,
36  amrex::Gpu::DeviceVector<amrex::Real>& a_dz_stretched,
37  bool a_periodic_wrap = false)
38  : terr_arr(a_z_terrain.const_array()),
39  dz_s(a_dz_stretched.data()),
40  dx(a_geom.CellSize(0)),
41  dy(a_geom.CellSize(1)),
42  dz(a_geom.CellSize(2)),
43  prob_lo_x(a_geom.ProbLo(0)),
44  prob_lo_y(a_geom.ProbLo(1)),
45  prob_lo_z(a_geom.ProbLo(2)),
46  prob_hi_x(a_geom.ProbHi(0)),
47  prob_hi_y(a_geom.ProbHi(1)),
48  i_hi(static_cast<int>(std::round((a_geom.ProbHi(0)-a_geom.ProbLo(0))/a_geom.CellSize(0)))),
49  j_hi(static_cast<int>(std::round((a_geom.ProbHi(1)-a_geom.ProbLo(1))/a_geom.CellSize(1)))),
50  k_hi(static_cast<int>(std::round((a_geom.ProbHi(2)-a_geom.ProbLo(2))/a_geom.CellSize(2)))),
51  per_x(a_periodic_wrap && a_geom.isPeriodic(0)),
52  per_y(a_periodic_wrap && a_geom.isPeriodic(1))
53  {}
54 
55  /**
56  * \brief Evaluate the signed implicit-function value at physical coordinates.
57  *
58  * The coordinates are passed through AMREX_D_DECL for spatial-dimension
59  * portability.
60  *
61  * \return Positive inside terrain, zero at the terrain surface, negative in fluid.
62  */
63  AMREX_GPU_HOST_DEVICE inline
64  amrex::Real operator() (AMREX_D_DECL(amrex::Real x, amrex::Real y, amrex::Real z))
65  const noexcept
66  {
67  int i1{};
68  int j1{};
69  int i2{};
70  int j2{};
71 
72  amrex::Real x1{};
73  amrex::Real x2{};
74  amrex::Real y1{};
75  amrex::Real y2{};
76  amrex::Real w1{};
77  amrex::Real w2{};
78  amrex::Real terr_z{amrex::Real(0.0)};
79 
80  // A point beyond a periodic face sees the surface at its periodic image; a point
81  // inside the domain is left exactly as it is
82  auto wrap = [] (amrex::Real a, amrex::Real lo, amrex::Real hi) {
83  if (a >= lo && a < hi) { return a; }
84  const amrex::Real len = hi - lo;
85  amrex::Real r = std::fmod(a - lo, len);
86  if (r < amrex::Real(0.0)) { r += len; }
87  if (r >= len) { r = amrex::Real(0.0); }
88  return lo + r;
89  };
90  if (per_x) { x = wrap(x, prob_lo_x, prob_hi_x); }
91  if (per_y) { y = wrap(y, prob_lo_y, prob_hi_y); }
92 
93  // z_stretched
94  const int k1 = amrex::Clamp(static_cast<int>(std::floor((z-prob_lo_z) / dz)), 0, k_hi - 1);
95  const amrex::Real z1 = prob_lo_z + k1*dz;
96  const amrex::Real remainder_z = (z - z1)/dz;
97  amrex::Real z_stretched = prob_lo_z;
98  for (int kk = 0; kk < k1; ++kk) {
99  z_stretched += dz_s[kk];
100  }
101  z_stretched += remainder_z * dz_s[k1];
102 
103  // Interpolation (nine subregions of x-y plane)
104  if (x <= prob_lo_x && y <= prob_lo_y) {
105  terr_z = terr_arr(0 ,0 ,0);
106  } else if (x >= prob_hi_x && y <= prob_lo_y) {
107  terr_z = terr_arr(i_hi,0 ,0);
108  } else if (x >= prob_hi_x && y >= prob_hi_y) {
109  terr_z = terr_arr(i_hi,j_hi,0);
110  } else if (x <= prob_lo_x && y >= prob_hi_y) {
111  terr_z = terr_arr(0 ,j_hi,0);
112  } else if (x > prob_lo_x && x < prob_hi_x && (y <= prob_lo_y || y >= prob_hi_y) ) {
113  i1 = static_cast<int>(std::floor((x-prob_lo_x) / dx));
114  i2 = i1+1;
115  x1 = prob_lo_x + i1*dx;
116  x2 = x1 + dx;
117  w1 = (x2-x)/dx;
118  w2 = (x-x1)/dx;
119  if (y <= prob_lo_y){
120  terr_z = w1*terr_arr(i1,0 ,0) + w2*terr_arr(i2,0 ,0);
121  } else if (y >= prob_hi_y) {
122  terr_z = w1*terr_arr(i1,j_hi,0) + w2*terr_arr(i2,j_hi,0);
123  }
124  } else if (y > prob_lo_y && y < prob_hi_y && (x <= prob_lo_x || x >= prob_hi_x) ) {
125  j1 = static_cast<int>(std::floor((y-prob_lo_y) / dy));
126  j2 = j1+1;
127  y1 = prob_lo_y + j1*dy;
128  y2 = y1 + dy;
129  w1 = (y2-y)/dy;
130  w2 = (y-y1)/dy;
131  if (x <= prob_lo_x){
132  terr_z = w1*terr_arr(0 ,j1,0) + w2*terr_arr(0 ,j2,0);
133  } else if (x >= prob_hi_x) {
134  terr_z = w1*terr_arr(i_hi,j1,0) + w2*terr_arr(i_hi,j2,0);
135  }
136  } else {
137  // Do bilinear interpolation of the terrain surface
138  i1 = static_cast<int>(std::floor((x-prob_lo_x) / dx));
139  i2 = i1+1;
140  j1 = static_cast<int>(std::floor((y-prob_lo_y) / dy));
141  j2 = j1+1;
142  x1 = prob_lo_x + i1*dx;
143  x2 = x1 + dx;
144  y1 = prob_lo_y + j1*dy;
145  y2 = y1 + dy;
146 
147  const amrex::Real denom = dx*dy;
148  const amrex::Real w11 = (x2-x)*(y2-y)/denom;
149  const amrex::Real w12 = (x2-x)*(y-y1)/denom;
150  const amrex::Real w21 = (x-x1)*(y2-y)/denom;
151  const amrex::Real w22 = (x-x1)*(y-y1)/denom;
152  terr_z = w11*terr_arr(i1,j1,0) + w12*terr_arr(i1,j2,0) + w21*terr_arr(i2,j1,0) + w22*terr_arr(i2,j2,0);
153  }
154  return -(z_stretched - terr_z);
155  }
156 
157  /**
158  * \brief Evaluate the signed implicit-function value at a point array.
159  * \param p Physical coordinate array.
160  * \return Positive inside terrain, zero at the terrain surface, negative in fluid.
161  */
162  AMREX_GPU_HOST_DEVICE
163  inline amrex::Real operator() (const amrex::RealArray& p) const noexcept
164  {
165  return this->operator() (AMREX_D_DECL(p[0], p[1], p[2]));
166  }
167 
168 protected:
169  //! Terrain height array sampled on the horizontal mesh.
170  amrex::Array4<amrex::Real const> terr_arr;
171  //! Device pointer to stretched vertical cell sizes.
172  amrex::Real const* dz_s;
173  //! Uniform computational grid spacing in each direction.
174  amrex::Real dx, dy, dz;
175  //! Low problem bounds used for interpolation.
177  //! High problem bounds used for interpolation.
178  amrex::Real prob_hi_x, prob_hi_y;
179  //! Highest terrain-array index in the x and y directions.
180  int i_hi, j_hi;
181  //! Number of vertical cells (one past the highest valid dz_s index).
182  int k_hi;
183  //! Whether x and y are evaluated at their periodic image inside the domain.
184  bool per_x, per_y;
185  /**
186  * \var TerrainIF::dy
187  * \brief Uniform computational grid spacing in y.
188  */
189  /**
190  * \var TerrainIF::dz
191  * \brief Uniform computational grid spacing in z.
192  */
193  /**
194  * \var TerrainIF::prob_lo_y
195  * \brief Low problem bound in y.
196  */
197  /**
198  * \var TerrainIF::prob_lo_z
199  * \brief Low problem bound in z.
200  */
201  /**
202  * \var TerrainIF::prob_hi_y
203  * \brief High problem bound in y.
204  */
205  /**
206  * \var TerrainIF::j_hi
207  * \brief Highest terrain-array index in y.
208  */
209 };
210 
211 #endif
AMReX EB implicit-function sign convention: >0 is body, 0 is boundary, <0 is fluid.
Definition: ERF_EBIFTerrain.H:24
bool per_x
Whether x and y are evaluated at their periodic image inside the domain.
Definition: ERF_EBIFTerrain.H:184
TerrainIF(amrex::FArrayBox const &a_z_terrain, amrex::Geometry const &a_geom, amrex::Gpu::DeviceVector< amrex::Real > &a_dz_stretched, bool a_periodic_wrap=false)
Construct a terrain implicit function from height and geometry data.
Definition: ERF_EBIFTerrain.H:35
amrex::Real prob_lo_x
Low problem bounds used for interpolation.
Definition: ERF_EBIFTerrain.H:176
amrex::Array4< amrex::Real const > terr_arr
Terrain height array sampled on the horizontal mesh.
Definition: ERF_EBIFTerrain.H:170
amrex::Real prob_hi_x
High problem bounds used for interpolation.
Definition: ERF_EBIFTerrain.H:178
int k_hi
Number of vertical cells (one past the highest valid dz_s index).
Definition: ERF_EBIFTerrain.H:182
amrex::Real prob_lo_y
Low problem bound in y.
Definition: ERF_EBIFTerrain.H:176
amrex::Real const * dz_s
Device pointer to stretched vertical cell sizes.
Definition: ERF_EBIFTerrain.H:172
amrex::Real dx
Uniform computational grid spacing in each direction.
Definition: ERF_EBIFTerrain.H:174
amrex::Real prob_lo_z
Low problem bound in z.
Definition: ERF_EBIFTerrain.H:176
int j_hi
Highest terrain-array index in y.
Definition: ERF_EBIFTerrain.H:180
int i_hi
Highest terrain-array index in the x and y directions.
Definition: ERF_EBIFTerrain.H:180
amrex::Real dy
Uniform computational grid spacing in y.
Definition: ERF_EBIFTerrain.H:174
bool per_y
Definition: ERF_EBIFTerrain.H:184
AMREX_GPU_HOST_DEVICE amrex::Real operator()(AMREX_D_DECL(amrex::Real x, amrex::Real y, amrex::Real z)) const noexcept
Evaluate the signed implicit-function value at physical coordinates.
Definition: ERF_EBIFTerrain.H:64
amrex::Real dz
Uniform computational grid spacing in z.
Definition: ERF_EBIFTerrain.H:174
amrex::Real prob_hi_y
High problem bound in y.
Definition: ERF_EBIFTerrain.H:178
@ p
Definition: ERF_WSM6.H:303
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE int wrap(int i, int lo, int n)
Definition: ERF_TopographicShadingKernels.H:36
real(c_double), private k1
Definition: ERF_module_mp_morr_two_moment.F90:213