1 #ifndef ERF_GRID_UTILS_H_
2 #define ERF_GRID_UTILS_H_
11 #include <AMReX_GpuQualifiers.H>
12 #include <AMReX_Math.H>
13 #include <AMReX_REAL.H>
14 #include <AMReX_Vector.H>
28 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
35 if (lhs_abs > scale) scale = lhs_abs;
36 if (rhs_abs > scale) scale = rhs_abs;
47 return (real_ulp > float_ulp) ? real_ulp : float_ulp;
59 const std::string& axis_name,
60 const std::string& field_description,
64 if (expected_size < 2) {
65 return field_description +
" requires at least two " + axis_name +
66 " coordinates for bilinear interpolation";
68 if (coordinates.size() !=
static_cast<amrex::Long
>(expected_size)) {
69 return field_description +
" has " + axis_name +
" coordinate length " +
70 std::to_string(coordinates.size()) +
", expected " +
71 std::to_string(expected_size);
74 origin = coordinates.front();
85 spacing = (coordinates[expected_size-1] - coordinates.front()) /
87 if (!std::isfinite(origin) || !std::isfinite(spacing) || spacing <=
amrex::Real(0.0)) {
88 return field_description +
" must have finite, strictly increasing " +
89 axis_name +
" coordinates with nonzero spacing";
92 for (
int index = 1; index < expected_size; ++index) {
94 if (!std::isfinite(coordinate) || coordinate <= coordinates[index-1]) {
95 return field_description +
" must have finite, strictly increasing " +
96 axis_name +
" coordinates";
115 const amrex::Real local_spacing = coordinate - coordinates[index-1];
119 if (std::abs(local_spacing - spacing) > spacing_tolerance) {
120 return field_description +
" has nonuniform " + axis_name +
121 " coordinates, which are not supported";
131 const std::string& reference_description,
132 const std::string& candidate_description)
134 if (candidate.
nx != reference.
nx || candidate.
ny != reference.
ny) {
135 return candidate_description +
" dimensions (" + std::to_string(candidate.
nx) +
136 " x " + std::to_string(candidate.
ny) +
") do not match " +
137 reference_description +
" dimensions (" + std::to_string(reference.
nx) +
138 " x " + std::to_string(reference.
ny) +
")";
141 struct MetadataValue {
146 const MetadataValue values[] = {
147 {
"x spacing", reference.
dx, candidate.
dx},
148 {
"y spacing", reference.
dy, candidate.
dy},
149 {
"x origin", reference.
xmin, candidate.
xmin},
150 {
"y origin", reference.
ymin, candidate.
ymin}
152 for (
const auto& value : values) {
153 if (!
nearly_equal(value.reference_value, value.candidate_value)) {
154 std::ostringstream message;
155 message << candidate_description <<
" " << value.name <<
" ("
156 << value.candidate_value <<
") does not match "
157 << reference_description <<
" (" << value.reference_value <<
")";
158 return message.str();
210 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
227 if (nz == 1 || dz0 >= z_top/Nz) {
239 const int max_iter = 50;
241 while (amrex::Math::abs(F) > tol && iter < max_iter) {
247 SFact = (SFact > SFact_min) ? SFact : SFact_min;
256 gs.
converged = (amrex::Math::abs(F) <= tol);
273 inline GeometricStretch
279 const int nz =
static_cast<int>(zlevels.size()) - 1;
281 "build_geometric_zlevels needs a zlevels array of at least two entries");
287 for (
int k(1); k<=nz; ++k) {
288 zlevels[k] = zlevels[k-1] +
dz;
303 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
308 int point_count) noexcept
311 if (point_count < 2 || spacing <=
amrex::Real(0.0)) {
319 if (coordinate < origin - lower_tolerance || coordinate > upper + upper_tolerance) {
324 (coordinate <= origin + lower_tolerance) ? origin :
325 ((coordinate >= upper - upper_tolerance) ? upper : coordinate);
326 const amrex::Real fractional_index = (bounded_coordinate - origin) / spacing;
327 int lower =
static_cast<int>(amrex::Math::floor(fractional_index));
328 lower = (lower < point_count - 1) ? lower : point_count - 2;
334 stencil.
lower = lower;
AMREX_ALWAYS_ASSERT_WITH_MESSAGE(m_cloud_chamber_config.active, "Cloud Chamber: initializer reached without a parsed configuration")
std::string name
Definition: ERF_Plotfile2DCatalog.cpp:105
amrex::Real Real
Definition: ERF_ShocInterface.H:19
@ dz
Definition: ERF_AdvanceWDM6.cpp:272
Definition: ERF_GridUtils.H:16
std::string validate_uniform_axis(const amrex::Vector< amrex::Real > &coordinates, int expected_size, const std::string &axis_name, const std::string &field_description, amrex::Real &origin, amrex::Real &spacing)
Definition: ERF_GridUtils.H:57
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
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE GeometricStretch geometric_stretch(amrex::Real dz0, amrex::Real z_top, int nz, amrex::Real tol) noexcept
Solve for the geometric stretch factor that fills a domain of depth z_top with nz layers whose first ...
Definition: ERF_GridUtils.H:212
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE InterpolationStencil uniform_interpolation_stencil(amrex::Real coordinate, amrex::Real origin, amrex::Real spacing, int point_count) noexcept
Definition: ERF_GridUtils.H:305
bool nearly_equal(amrex::Real lhs, amrex::Real rhs)
Definition: ERF_GridUtils.H:51
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real comparison_tolerance(amrex::Real lhs, amrex::Real rhs) noexcept
Definition: ERF_GridUtils.H:30
std::string validate_matching_grid(const UniformGridMetadata &reference, const UniformGridMetadata &candidate, const std::string &reference_description, const std::string &candidate_description)
Definition: ERF_GridUtils.H:129
real(c_double), parameter epsilon
Definition: ERF_module_model_constants.F90:12
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
amrex::Real residual
sum of the layers minus z_top, at exit
Definition: ERF_GridUtils.H:175
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
Definition: ERF_GridUtils.H:297
int lower
Definition: ERF_GridUtils.H:298
bool inside
Definition: ERF_GridUtils.H:300
amrex::Real weight
Definition: ERF_GridUtils.H:299