ERF
Energy Research and Forecasting: An Atmospheric Modeling Code
ERF_InitZLevels.cpp File Reference
#include <ERF_TerrainMetrics.H>
#include <ERF_Utils.H>
#include <AMReX_ParmParse.H>
#include <ERF_NumericalConstants.H>
#include <ERF_Interpolation_1D.H>
#include <ERF_GridUtils.H>
#include <cmath>
Include dependency graph for ERF_InitZLevels.cpp:

Functions

void init_zlevels (Vector< Vector< Real >> &zlevels_stag, Vector< Vector< Real >> &stretched_dz_h, Vector< Gpu::DeviceVector< Real >> &stretched_dz_d, Vector< Geometry > const &geom, Vector< IntVect > const &ref_ratio, const Real grid_stretching_ratio, const Real zsurf, const Real dz0)
 
void update_stretched_dz (int lev, Vector< Vector< Real >> const &zlevels_stag, Vector< Vector< Real >> &stretched_dz_h, Vector< Gpu::DeviceVector< Real >> &stretched_dz_d)
 

Function Documentation

◆ init_zlevels()

void init_zlevels ( Vector< Vector< Real >> &  zlevels_stag,
Vector< Vector< Real >> &  stretched_dz_h,
Vector< Gpu::DeviceVector< Real >> &  stretched_dz_d,
Vector< Geometry > const &  geom,
Vector< IntVect > const &  ref_ratio,
const Real  grid_stretching_ratio,
const Real  zsurf,
const Real  dz0 
)

Initialize staggered z-levels and grid spacing for all levels.

Parameters
[out]zlevels_stagStaggered z-level coordinates.
[out]stretched_dz_hHost-side cell heights in z.
[out]stretched_dz_dDevice-side cell heights in z.
[in]geomGeometry describing the domain.
[in]ref_ratioRefinement ratios between levels.
[in]grid_stretching_ratioRatio used for stretching the vertical grid; if zero and dz0 is positive, the ratio is solved for so that the grid closes on the top of the domain.
[in]zsurfHeight of the surface.
[in]dz0Initial cell height at the surface.
34 {
35  int max_level = zlevels_stag.size()-1;
36 
37  for (int lev = 0; lev <= max_level; lev++)
38  {
39  auto dx = geom[lev].CellSizeArray();
40  const Box& domain = geom[lev].Domain();
41  int nz = domain.length(2);
42 
43  zlevels_stag[lev].resize(nz+1);
44 
45  stretched_dz_h[lev].resize(domain.length(2));
46 
47  if (grid_stretching_ratio == 0 && !(dz0 > 0)) {
48  // This is the default for z_levels
49  for (int k = 0; k < nz+1; k++)
50  {
51  zlevels_stag[lev][k] = k * dx[2];
52  }
53  for (int k = 0; k < nz; k++)
54  {
55  stretched_dz_h[lev][k] = dx[2];
56  }
57  } else if (lev == 0 && grid_stretching_ratio == 0) {
58  // Solve for the geometric stretch factor that fills the domain above zsurf with
59  // nz layers whose first is dz0 thick, as the wrfinput path does when it builds
60  // its grid by geometric stretching
61 #ifdef AMREX_USE_FLOAT
62  const Real tol = Real(1.e-4);
63 #else
64  const Real tol = Real(1.e-8);
65 #endif
66  const Real depth = geom[lev].ProbHi(2) - zsurf;
68  erf_grid_utils::build_geometric_zlevels(zlevels_stag[lev], dz0, depth, tol);
70  "Newton iterations to determine the grid stretching factor failed!\n");
71  if (gs.uniform) {
72  Print() << "Note: erf.initial_dz = " << dz0 << " is no thinner than the uniform "
73  << "spacing, so the vertical grid is uniform with dz = " << gs.dz0 << std::endl;
74  }
75  Print() << "Building a stretched grid with dz0: " << gs.dz0
76  << " and stretching factor: " << gs.ratio << std::endl;
77 
78  Print() << "Stretched grid levels at level : " << lev << " is " << zsurf;
79  for (int k = 0; k < nz+1; k++)
80  {
81  zlevels_stag[lev][k] += zsurf;
82  if (k > 0) {
83  stretched_dz_h[lev][k-1] = zlevels_stag[lev][k] - zlevels_stag[lev][k-1];
84  Print() << " " << zlevels_stag[lev][k];
85  }
86  }
87  Print() << std::endl;
88  } else if (lev == 0) {
89  // Create stretched grid based on initial dz and stretching ratio
90  zlevels_stag[lev][0] = zsurf;
91  Real dz = dz0;
92  Print() << "Stretched grid levels at level : " << lev << " is " << zsurf;
93  for (int k = 1; k < nz+1; k++)
94  {
95  stretched_dz_h[lev][k-1] = dz;
96  zlevels_stag[lev][k] = zlevels_stag[lev][k-1] + dz;
97  Print() << " " << zlevels_stag[lev][k];
98  dz *= grid_stretching_ratio;
99  }
100  Print() << std::endl;
101  } else if (lev > 0) {
102  int rr = ref_ratio[lev-1][2];
103  expand_and_interpolate_1d(zlevels_stag[lev], zlevels_stag[lev-1], rr, false);
104  for (int k = 0; k < nz; k++)
105  {
106  stretched_dz_h[lev][k] = (zlevels_stag[lev][k+1] - zlevels_stag[lev][k]);
107  }
108  }
109  }
110 
111  // Try reading in terrain_z_levels, which allows arbitrarily spaced grid
112  // levels to be specified and will take precedence over the
113  // grid_stretching_ratio parameter
114  ParmParse pp("erf");
115  int n_zlevels = pp.countval("terrain_z_levels");
116  if (n_zlevels > 0)
117  {
118  int nz = geom[0].Domain().length(2);
119  if (n_zlevels != nz+1) {
120  Print() << "You supplied " << n_zlevels << " staggered terrain_z_levels " << std::endl;
121  Print() << "but n_cell+1 in the z-direction is " << nz+1 << std::endl;
122  Abort("You must specify a z_level for every value of k");
123  }
124 
125  if (grid_stretching_ratio > 0) {
126  Print() << "Note: Found terrain_z_levels, ignoring grid_stretching_ratio" << std::endl;
127  } else if (dz0 > 0) {
128  Print() << "Note: Found terrain_z_levels, ignoring initial_dz" << std::endl;
129  }
130 
131  pp.getarr("terrain_z_levels", zlevels_stag[0], 0, nz+1);
132 
133  // These levels should range from 0 at the surface to the height of the
134  // top of model domain (see the coordinate surface height, zeta, in
135  // Klemp 2011)
136  AMREX_ALWAYS_ASSERT(zlevels_stag[0][0] == 0);
137 
138  for (int lev = 1; lev <= max_level; lev++) {
139  int rr = ref_ratio[lev-1][2];
140  expand_and_interpolate_1d(zlevels_stag[lev], zlevels_stag[lev-1], rr, false);
141  }
142 
143  for (int lev = 0; lev <= max_level; lev++) {
144  int nz_zlevs = zlevels_stag[lev].size();
145  for (int k = 0; k < nz_zlevs-1; k++)
146  {
147  stretched_dz_h[lev][k] = (zlevels_stag[lev][k+1] - zlevels_stag[lev][k]);
148  }
149  }
150  }
151 
152  for (int lev = 0; lev <= max_level; lev++) {
153  stretched_dz_d[lev].resize(stretched_dz_h[lev].size());
154  Gpu::copy(Gpu::hostToDevice, stretched_dz_h[lev].begin(), stretched_dz_h[lev].end(), stretched_dz_d[lev].begin());
155  }
156 }
const Real dx
Definition: ERF_InitCustomPert_ABL.H:44
ParmParse pp("prob")
AMREX_ALWAYS_ASSERT(bx.length()[2]==khi+1)
AMREX_ALWAYS_ASSERT_WITH_MESSAGE(m_cloud_chamber_config.active, "Cloud Chamber: initializer reached without a parsed configuration")
AMREX_FORCE_INLINE void expand_and_interpolate_1d(amrex::Vector< amrex::Real > &znew, const amrex::Vector< amrex::Real > &zorig, int refine_fac, bool destag=false)
Definition: ERF_Interpolation_1D.H:89
amrex::Real Real
Definition: ERF_ShocInterface.H:19
@ dz
Definition: ERF_AdvanceWDM6.cpp:272
GeometricStretch build_geometric_zlevels(amrex::Vector< amrex::Real > &zlevels, amrex::Real dz0, amrex::Real z_top, amrex::Real tol)
Fill a staggered z-level array with a geometrically stretched grid.
Definition: ERF_GridUtils.H:274
real(c_double), private rr
Definition: ERF_module_mp_morr_two_moment.F90:224
A geometrically stretched vertical grid: dz_k = dz0 * ratio^k.
Definition: ERF_GridUtils.H:172
amrex::Real ratio
layer-to-layer stretch factor
Definition: ERF_GridUtils.H:174
bool converged
true when |residual| <= tol
Definition: ERF_GridUtils.H:177
bool uniform
true when the requested dz0 could not be honored
Definition: ERF_GridUtils.H:176
amrex::Real dz0
thickness of the first layer, as actually used
Definition: ERF_GridUtils.H:173

Referenced by ERF::ERF_shared().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ update_stretched_dz()

void update_stretched_dz ( int  lev,
Vector< Vector< Real >> const &  zlevels_stag,
Vector< Vector< Real >> &  stretched_dz_h,
Vector< Gpu::DeviceVector< Real >> &  stretched_dz_d 
)

Re-derive the cell heights at one level from the nominal staggered z-levels.

Parameters
[in]levLevel index.
[in]zlevels_stagStaggered z-level coordinates.
[out]stretched_dz_hHost-side cell heights in z.
[out]stretched_dz_dDevice-side cell heights in z.
171 {
172  AMREX_ALWAYS_ASSERT(lev < static_cast<int>(zlevels_stag.size()));
173  AMREX_ALWAYS_ASSERT(lev < static_cast<int>(stretched_dz_h.size()));
174  AMREX_ALWAYS_ASSERT(lev < static_cast<int>(stretched_dz_d.size()));
175 
176  const int nz = static_cast<int>(zlevels_stag[lev].size()) - 1;
177  AMREX_ALWAYS_ASSERT(nz > 0);
178 
179  stretched_dz_h[lev].resize(nz);
180  for (int k = 0; k < nz; k++) {
181  stretched_dz_h[lev][k] = zlevels_stag[lev][k+1] - zlevels_stag[lev][k];
182  }
183 
184  stretched_dz_d[lev].resize(nz);
185  Gpu::copy(Gpu::hostToDevice, stretched_dz_h[lev].begin(), stretched_dz_h[lev].end(),
186  stretched_dz_d[lev].begin());
187 }

Referenced by ERF::ReadCheckpointFile().

Here is the call graph for this function:
Here is the caller graph for this function: