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  */
33  TerrainIF (amrex::FArrayBox const& a_z_terrain, amrex::Geometry const& a_geom,
34  amrex::Gpu::DeviceVector<amrex::Real>& a_dz_stretched)
35  : terr_arr(a_z_terrain.const_array()),
36  dz_s(a_dz_stretched.data()),
37  dx(a_geom.CellSize(0)),
38  dy(a_geom.CellSize(1)),
39  dz(a_geom.CellSize(2)),
40  prob_lo_x(a_geom.ProbLo(0)),
41  prob_lo_y(a_geom.ProbLo(1)),
42  prob_lo_z(a_geom.ProbLo(2)),
43  prob_hi_x(a_geom.ProbHi(0)),
44  prob_hi_y(a_geom.ProbHi(1)),
45  i_hi(static_cast<int>(std::round((a_geom.ProbHi(0)-a_geom.ProbLo(0))/a_geom.CellSize(0)))),
46  j_hi(static_cast<int>(std::round((a_geom.ProbHi(1)-a_geom.ProbLo(1))/a_geom.CellSize(1)))),
47  k_hi(static_cast<int>(std::round((a_geom.ProbHi(2)-a_geom.ProbLo(2))/a_geom.CellSize(2))))
48  {}
49 
50  /**
51  * \brief Evaluate the signed implicit-function value at physical coordinates.
52  *
53  * The coordinates are passed through AMREX_D_DECL for spatial-dimension
54  * portability.
55  *
56  * \return Positive inside terrain, zero at the terrain surface, negative in fluid.
57  */
58  AMREX_GPU_HOST_DEVICE inline
60  const noexcept
61  {
62  int i1{};
63  int j1{};
64  int i2{};
65  int j2{};
66 
67  amrex::Real x1{};
68  amrex::Real x2{};
69  amrex::Real y1{};
70  amrex::Real y2{};
71  amrex::Real w1{};
72  amrex::Real w2{};
73  amrex::Real terr_z{amrex::Real(0.0)};
74 
75  // z_stretched
76  const int k1 = amrex::Clamp(static_cast<int>(std::floor((z-prob_lo_z) / dz)), 0, k_hi - 1);
77  const amrex::Real z1 = prob_lo_z + k1*dz;
78  const amrex::Real remainder_z = (z - z1)/dz;
79  amrex::Real z_stretched = prob_lo_z;
80  for (int kk = 0; kk < k1; ++kk) {
81  z_stretched += dz_s[kk];
82  }
83  z_stretched += remainder_z * dz_s[k1];
84 
85  // Interpolation (nine subregions of x-y plane)
86  if (x <= prob_lo_x && y <= prob_lo_y) {
87  terr_z = terr_arr(0 ,0 ,0);
88  } else if (x >= prob_hi_x && y <= prob_lo_y) {
89  terr_z = terr_arr(i_hi,0 ,0);
90  } else if (x >= prob_hi_x && y >= prob_hi_y) {
91  terr_z = terr_arr(i_hi,j_hi,0);
92  } else if (x <= prob_lo_x && y >= prob_hi_y) {
93  terr_z = terr_arr(0 ,j_hi,0);
94  } else if (x > prob_lo_x && x < prob_hi_x && (y <= prob_lo_y || y >= prob_hi_y) ) {
95  i1 = static_cast<int>(std::floor((x-prob_lo_x) / dx));
96  i2 = i1+1;
97  x1 = prob_lo_x + i1*dx;
98  x2 = x1 + dx;
99  w1 = (x2-x)/dx;
100  w2 = (x-x1)/dx;
101  if (y <= prob_lo_y){
102  terr_z = w1*terr_arr(i1,0 ,0) + w2*terr_arr(i2,0 ,0);
103  } else if (y >= prob_hi_y) {
104  terr_z = w1*terr_arr(i1,j_hi,0) + w2*terr_arr(i2,j_hi,0);
105  }
106  } else if (y > prob_lo_y && y < prob_hi_y && (x <= prob_lo_x || x >= prob_hi_x) ) {
107  j1 = static_cast<int>(std::floor((y-prob_lo_y) / dy));
108  j2 = j1+1;
109  y1 = prob_lo_y + j1*dy;
110  y2 = y1 + dy;
111  w1 = (y2-y)/dy;
112  w2 = (y-y1)/dy;
113  if (x <= prob_lo_x){
114  terr_z = w1*terr_arr(0 ,j1,0) + w2*terr_arr(0 ,j2,0);
115  } else if (x >= prob_hi_x) {
116  terr_z = w1*terr_arr(i_hi,j1,0) + w2*terr_arr(i_hi,j2,0);
117  }
118  } else {
119  // Do bilinear interpolation of the terrain surface
120  i1 = static_cast<int>(std::floor((x-prob_lo_x) / dx));
121  i2 = i1+1;
122  j1 = static_cast<int>(std::floor((y-prob_lo_y) / dy));
123  j2 = j1+1;
124  x1 = prob_lo_x + i1*dx;
125  x2 = x1 + dx;
126  y1 = prob_lo_y + j1*dy;
127  y2 = y1 + dy;
128 
129  const amrex::Real denom = dx*dy;
130  const amrex::Real w11 = (x2-x)*(y2-y)/denom;
131  const amrex::Real w12 = (x2-x)*(y-y1)/denom;
132  const amrex::Real w21 = (x-x1)*(y2-y)/denom;
133  const amrex::Real w22 = (x-x1)*(y-y1)/denom;
134  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);
135  }
136  return -(z_stretched - terr_z);
137  }
138 
139  /**
140  * \brief Evaluate the signed implicit-function value at a point array.
141  * \param p Physical coordinate array.
142  * \return Positive inside terrain, zero at the terrain surface, negative in fluid.
143  */
144  AMREX_GPU_HOST_DEVICE
145  inline amrex::Real operator() (const amrex::RealArray& p) const noexcept
146  {
147  return this->operator() (AMREX_D_DECL(p[0], p[1], p[2]));
148  }
149 
150 protected:
151  //! Terrain height array sampled on the horizontal mesh.
152  amrex::Array4<amrex::Real const> terr_arr;
153  //! Device pointer to stretched vertical cell sizes.
155  //! Uniform computational grid spacing in each direction.
157  //! Low problem bounds used for interpolation.
159  //! High problem bounds used for interpolation.
161  //! Highest terrain-array index in the x and y directions.
162  int i_hi, j_hi;
163  //! Number of vertical cells (one past the highest valid dz_s index).
164  int k_hi;
165  /**
166  * \var TerrainIF::dy
167  * \brief Uniform computational grid spacing in y.
168  */
169  /**
170  * \var TerrainIF::dz
171  * \brief Uniform computational grid spacing in z.
172  */
173  /**
174  * \var TerrainIF::prob_lo_y
175  * \brief Low problem bound in y.
176  */
177  /**
178  * \var TerrainIF::prob_lo_z
179  * \brief Low problem bound in z.
180  */
181  /**
182  * \var TerrainIF::prob_hi_y
183  * \brief High problem bound in y.
184  */
185  /**
186  * \var TerrainIF::j_hi
187  * \brief Highest terrain-array index in y.
188  */
189 };
190 
191 #endif
amrex::Real Real
Definition: ERF_ShocInterface.H:19
AMReX EB implicit-function sign convention: >0 is body, 0 is boundary, <0 is fluid.
Definition: ERF_EBIFTerrain.H:24
amrex::Real prob_lo_x
Low problem bounds used for interpolation.
Definition: ERF_EBIFTerrain.H:158
TerrainIF(amrex::FArrayBox const &a_z_terrain, amrex::Geometry const &a_geom, amrex::Gpu::DeviceVector< amrex::Real > &a_dz_stretched)
Construct a terrain implicit function from height and geometry data.
Definition: ERF_EBIFTerrain.H:33
amrex::Array4< amrex::Real const > terr_arr
Terrain height array sampled on the horizontal mesh.
Definition: ERF_EBIFTerrain.H:152
amrex::Real prob_hi_x
High problem bounds used for interpolation.
Definition: ERF_EBIFTerrain.H:160
int k_hi
Number of vertical cells (one past the highest valid dz_s index).
Definition: ERF_EBIFTerrain.H:164
amrex::Real prob_lo_y
Low problem bound in y.
Definition: ERF_EBIFTerrain.H:158
amrex::Real const * dz_s
Device pointer to stretched vertical cell sizes.
Definition: ERF_EBIFTerrain.H:154
amrex::Real dx
Uniform computational grid spacing in each direction.
Definition: ERF_EBIFTerrain.H:156
amrex::Real prob_lo_z
Low problem bound in z.
Definition: ERF_EBIFTerrain.H:158
int j_hi
Highest terrain-array index in y.
Definition: ERF_EBIFTerrain.H:162
int i_hi
Highest terrain-array index in the x and y directions.
Definition: ERF_EBIFTerrain.H:162
amrex::Real dy
Uniform computational grid spacing in y.
Definition: ERF_EBIFTerrain.H:156
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:59
amrex::Real dz
Uniform computational grid spacing in z.
Definition: ERF_EBIFTerrain.H:156
amrex::Real prob_hi_y
High problem bound in y.
Definition: ERF_EBIFTerrain.H:160
@ p
Definition: ERF_WSM6.H:191
real(c_double), private k1
Definition: ERF_module_mp_morr_two_moment.F90:213