1 #ifndef ERF_PROBCOMMON_H_
2 #define ERF_PROBCOMMON_H_
7 #include <AMReX_ParmParse.H>
8 #include <AMReX_Geometry.H>
9 #include <AMReX_FArrayBox.H>
10 #include <AMReX_MultiFab.H>
11 #include <AMReX_Reduce.H>
35 if (std::getline(is, line)) {
36 std::stringstream ss(line);
39 amrex::Abort(
"Failed to read");
42 amrex::Print() <<
"Trying to read line " << n <<
" in this file " << std::endl;
43 amrex::Abort(
"Wrong format: more than one number in this line");
46 amrex::Abort(
"Unable to read this line");
53 amrex::FArrayBox& terrain_fab,
78 std::unique_ptr<amrex::MultiFab>& ,
79 std::unique_ptr<amrex::MultiFab>& ,
80 amrex::Geometry
const& ,
81 const amrex::Vector<amrex::Real>& ,
84 amrex::Print() <<
"Hydrostatically balanced density was NOT set"
85 <<
" -- an appropriate init_type should probably have been specified"
86 <<
" (e.g., input_sounding, WRFInput, or Metgrid)"
88 amrex::Error(
"Should never call this version of erf_init_dens_hse_dry for "+
name()+
" problem");
94 amrex::Error(
"Should never call this version of erf_init_const_dens_hse for "+
name()+
" problem");
99 amrex::MultiFab& , amrex::MultiFab& ,
102 amrex::Error(
"Should never call this version of erf_init_const_dens_and_th_hse for "+
name()+
" problem");
107 amrex::MultiFab& , amrex::MultiFab& ,
109 std::unique_ptr<amrex::MultiFab>& )
111 amrex::Error(
"Should never call this version of erf_init_const_dens_and_linear_th_hse for "+
name()+
" problem");
116 std::unique_ptr<amrex::MultiFab>& ,
117 amrex::Geometry
const& )
139 amrex::Array4<amrex::Real const>
const& ,
140 amrex::Array4<amrex::Real >
const& ,
141 amrex::Array4<amrex::Real >
const& ,
142 amrex::Array4<amrex::Real >
const& ,
143 amrex::Array4<amrex::Real const>
const& ,
144 amrex::Array4<amrex::Real const>
const& ,
145 amrex::GeometryData
const& ,
146 amrex::Array4<amrex::Real const>
const& ,
149 amrex::Print() <<
"No perturbation to background fields supplied for "
150 <<
name() <<
" problem" << std::endl;
173 amrex::Array4<amrex::Real >
const& ,
174 amrex::Array4<amrex::Real >
const& ,
175 amrex::Array4<amrex::Real >
const& ,
176 amrex::Array4<amrex::Real const>
const& ,
177 amrex::GeometryData
const& ,
178 amrex::Array4<amrex::Real const>
const& ,
179 amrex::Array4<amrex::Real const>
const& ,
182 amrex::Print() <<
"No perturbation velocities supplied for " <<
name() <<
" problem" << std::endl;
196 amrex::MultiFab* src,
197 const amrex::Geometry& ,
198 std::unique_ptr<amrex::MultiFab>& )
200 if (src->empty())
return;
202 amrex::Warning(
"Temperature forcing not defined for "+
name()+
" problem");
203 for ( amrex::MFIter mfi(*src, amrex::TilingIfNotGPU()); mfi.isValid(); ++mfi )
205 const auto &box = mfi.tilebox();
206 const amrex::Array4<amrex::Real>& src_arr = src->array(mfi);
209 ParallelFor(box, [=] AMREX_GPU_DEVICE (
int i,
int j,
int k) noexcept {
212 src_arr(i, j, k) =
zero;
228 amrex::MultiFab* qsrc,
229 const amrex::Geometry& ,
230 std::unique_ptr<amrex::MultiFab>& )
232 if (qsrc->empty())
return;
234 amrex::Warning(
"Moisture forcing not defined for "+
name()+
" problem");
235 for ( amrex::MFIter mfi(*qsrc, amrex::TilingIfNotGPU()); mfi.isValid(); ++mfi )
237 const auto &box = mfi.tilebox();
238 const amrex::Array4<amrex::Real>& qsrc_arr = qsrc->array(mfi);
241 ParallelFor(box, [=] AMREX_GPU_DEVICE (
int i,
int j,
int k) noexcept {
244 qsrc_arr(i, j, k) =
zero;
263 amrex::Vector<amrex::Real>&
wbar,
264 amrex::Gpu::DeviceVector<amrex::Real>& d_wbar,
265 const amrex::MultiFab& ,
266 const amrex::Geometry& geom,
267 std::unique_ptr<amrex::MultiFab>& )
269 if (
wbar.empty())
return;
271 amrex::Warning(
"Moisture forcing not defined for "+
name()+
" problem");
273 const int khi = geom.Domain().bigEnd()[2];
276 for (
int k = 0; k <=
khi; k++)
284 amrex::Gpu::copy(amrex::Gpu::hostToDevice,
wbar.begin(),
wbar.end(), d_wbar.begin());
298 amrex::Vector<amrex::Real>& u_geos,
299 amrex::Gpu::DeviceVector<amrex::Real>& d_u_geos,
300 amrex::Vector<amrex::Real>& v_geos,
301 amrex::Gpu::DeviceVector<amrex::Real>& d_v_geos,
302 const amrex::Geometry& geom,
303 std::unique_ptr<amrex::MultiFab>& )
305 if (u_geos.empty())
return;
307 amrex::Warning(
"Geostrophic wind profile not defined for "+
name()+
" problem");
309 const int khi = geom.Domain().bigEnd()[2];
312 for (
int k = 0; k <=
khi; k++)
321 amrex::Gpu::copy(amrex::Gpu::hostToDevice, u_geos.begin(), u_geos.end(), d_u_geos.begin());
322 amrex::Gpu::copy(amrex::Gpu::hostToDevice, v_geos.begin(), v_geos.end(), d_v_geos.begin());
334 amrex::FArrayBox& buildings_fab,
339 amrex::ParmParse
pp(
"erf");
340 auto valid_fname =
pp.queryAdd(
"buildings_file_name",fname);
359 amrex::FArrayBox& terrain_fab,
362 amrex::ParmParse
pp(
"erf");
365 std::string fname_nc;
366 auto valid_fname_nc =
pp.query(
"terrain_file_name_nc", fname_nc);
368 std::string fname, fname_usgs;
369 auto valid_fname =
pp.query(
"terrain_file_name", fname);
370 auto valid_fname_USGS =
pp.query(
"terrain_file_name_USGS", fname_usgs);
372 if (valid_fname_nc) {
373 #ifdef ERF_USE_NETCDF
374 read_terrain_netcdf(fname_nc, geom, terrain_fab, time);
376 amrex::Abort(
"ERF must be compiled with NetCDF support to use terrain_file_name_nc");
378 }
else if (valid_fname) {
381 }
else if (valid_fname_USGS) {
401 amrex::ParmParse
pp(
"erf");
403 if (
pp.query(
"terrain_file_name_nc", fname) ||
404 pp.query(
"terrain_file_name", fname) ||
405 pp.query(
"terrain_file_name_USGS", fname)) {
409 std::string custom_terrain_type =
"None";
410 amrex::ParmParse
pp_prob(
"prob");
411 pp_prob.query(
"custom_terrain_type", custom_terrain_type);
412 return (custom_terrain_type !=
"None");
415 #ifdef ERF_USE_NETCDF
417 read_terrain_netcdf (
const std::string& fname,
418 const amrex::Geometry& geom,
419 amrex::FArrayBox& terrain_fab,
422 amrex::Print() <<
"Reading terrain from NetCDF file: " << fname << std::endl;
424 amrex::Vector<amrex::Real> m_xterrain, m_yterrain, m_zterrain;
428 int mass_point_terrain = 0;
430 if (amrex::ParallelDescriptor::IOProcessor()) {
431 auto ncf = ncutils::NCFile::open(fname, NC_NOWRITE);
434 std::string height_varname =
"height";
435 if (!ncf.has_var(
"height")) {
436 if (ncf.has_var(
"z")) height_varname =
"z";
437 else if (ncf.has_var(
"terrain")) height_varname =
"terrain";
438 else if (ncf.has_var(
"HGT_M")) height_varname =
"HGT_M";
439 else if (ncf.has_var(
"AGL")) height_varname =
"AGL";
441 amrex::Abort(
"No terrain height variable found in NetCDF file. "
442 "Expected: 'height', 'z', 'terrain', 'HGT_M', or 'AGL'");
444 amrex::Print() <<
" Using terrain variable: " << height_varname << std::endl;
446 auto var = ncf.var(height_varname);
447 auto dims = var.shape();
448 const auto dim_names = var.dimnames();
449 const auto is_y_dimension = [] (
const std::string&
name) {
450 return name ==
"y" ||
name ==
"south_north";
452 const auto is_x_dimension = [] (
const std::string&
name) {
453 return name ==
"x" ||
name ==
"west_east";
455 const auto is_time_dimension = [] (
const std::string&
name) {
456 return name ==
"time" ||
name ==
"Time";
459 bool has_time_dimension =
false;
460 if (dims.size() == 2) {
461 if (dim_names.size() != 2 || !is_y_dimension(dim_names[0]) ||
462 !is_x_dimension(dim_names[1])) {
463 amrex::Abort(
"NetCDF terrain field '" + height_varname +
"' in '" +
464 fname +
"' must use dimensions (y,x) or "
465 "(south_north,west_east)");
469 }
else if (dims.size() == 3) {
470 if (dim_names.size() != 3 || !is_time_dimension(dim_names[0]) ||
471 !is_y_dimension(dim_names[1]) || !is_x_dimension(dim_names[2])) {
472 amrex::Abort(
"NetCDF terrain field '" + height_varname +
"' in '" +
473 fname +
"' must use dimensions (time,y,x) with time leading; "
474 "accepted layouts are (y,x) and (time,y,x)");
477 amrex::Abort(
"NetCDF terrain field '" + height_varname +
"' in '" +
478 fname +
"' has no time records");
480 has_time_dimension =
true;
484 amrex::Abort(
"NetCDF terrain field '" + height_varname +
"' in '" + fname +
485 "' has unsupported rank " + std::to_string(dims.size()) +
486 "; accepted layouts are (y,x) and (time,y,x)");
489 if (
nx < 2 ||
ny < 2) {
490 amrex::Abort(
"NetCDF terrain field '" + height_varname +
"' in '" +
491 fname +
"' requires at least two x and y coordinates "
492 "for bilinear interpolation");
495 const bool has_x = ncf.has_var(
"x");
496 const bool has_y = ncf.has_var(
"y");
497 if (has_x != has_y) {
498 amrex::Abort(
"NetCDF terrain file '" + fname +
499 "' must provide both one-dimensional 'x' and 'y' coordinates");
502 const std::string terrain_description =
503 "NetCDF terrain field '" + height_varname +
"' in '" + fname +
"'";
504 m_zterrain.resize(
nx *
ny);
506 if (height_varname !=
"HGT_M" || !has_time_dimension ||
507 dim_names != std::vector<std::string>{
"Time",
"south_north",
"west_east"}) {
508 amrex::Abort(
"NetCDF terrain field '" + height_varname +
"' in '" + fname +
509 "' requires one-dimensional 'x' and 'y' coordinates; a genuine "
510 "WPS file must provide HGT_M(Time,south_north,west_east)");
513 const auto require_real_attribute = [&] (
const std::string&
name) {
514 if (!ncf.has_attr(
name)) {
515 amrex::Abort(
"WPS terrain file '" + fname +
516 "' is missing required global attribute '" +
name +
"'");
518 std::vector<double> values;
519 ncf.get_attr(
name, values);
520 if (values.size() != 1 || !std::isfinite(values[0])) {
521 amrex::Abort(
"WPS terrain file '" + fname +
522 "' must have one finite global attribute '" +
name +
"'");
526 const auto require_int_attribute = [&] (
const std::string&
name) {
527 if (!ncf.has_attr(
name)) {
528 amrex::Abort(
"WPS terrain file '" + fname +
529 "' is missing required global attribute '" +
name +
"'");
531 std::vector<int> values;
532 ncf.get_attr(
name, values);
533 if (values.size() != 1) {
534 amrex::Abort(
"WPS terrain file '" + fname +
535 "' must have one global attribute '" +
name +
"'");
540 const int map_proj = require_int_attribute(
"MAP_PROJ");
551 if (map_proj != 1 && map_proj != 2 && map_proj != 3) {
554 ?
"; latitude/longitude WPS grids are not supported because their "
555 "DX/DY are in degrees rather than metres"
556 :
"; supported projections are 1 (Lambert), 2 (polar), "
558 amrex::Abort(
"WPS terrain file '" + fname +
"' uses unsupported MAP_PROJ=" +
559 std::to_string(map_proj) + reason);
561 const double cen_lat = require_real_attribute(
"CEN_LAT");
562 const double cen_lon = require_real_attribute(
"CEN_LON");
563 const double stand_lon = require_real_attribute(
"STAND_LON");
564 const double wps_dx = require_real_attribute(
"DX");
565 const double wps_dy = require_real_attribute(
"DY");
566 if (wps_dx <= 0.0 || wps_dy <= 0.0 || std::abs(cen_lat) > 90.0 ||
567 std::abs(cen_lon) > 360.0 || std::abs(stand_lon) > 360.0) {
568 amrex::Abort(
"WPS terrain file '" + fname +
569 "' has invalid projection or grid metadata");
572 const double truelat1 = require_real_attribute(
"TRUELAT1");
573 if (std::abs(truelat1) > 90.0) {
574 amrex::Abort(
"WPS terrain file '" + fname +
575 "' has invalid TRUELAT1");
578 const double truelat2 = require_real_attribute(
"TRUELAT2");
579 if (std::abs(truelat2) > 90.0) {
580 amrex::Abort(
"WPS terrain file '" + fname +
581 "' has invalid TRUELAT2");
585 const int wps_nx = require_int_attribute(
"WEST-EAST_GRID_DIMENSION");
586 const int wps_ny = require_int_attribute(
"SOUTH-NORTH_GRID_DIMENSION");
587 if (wps_nx !=
nx + 1 || wps_ny !=
ny + 1) {
588 amrex::Abort(
"WPS terrain file '" + fname +
589 "' has grid dimensions inconsistent with HGT_M: expected " +
590 std::to_string(
nx + 1) +
" x " + std::to_string(
ny + 1));
593 const auto validate_wps_coordinate = [&] (
const std::string&
name) {
594 if (!ncf.has_var(
name)) {
595 amrex::Abort(
"WPS terrain file '" + fname +
596 "' is missing required coordinate variable '" +
name +
"'");
598 const auto coordinate = ncf.var(
name);
599 if (coordinate.shape() != dims ||
600 coordinate.dimnames() != dim_names) {
601 amrex::Abort(
"WPS terrain coordinate '" +
name +
"' in '" + fname +
602 "' must have dimensions (Time,south_north,west_east) "
605 amrex::Vector<amrex::Real> values(
nx *
ny);
606 const std::vector<size_t> start{0, 0, 0};
607 const std::vector<size_t> count{
608 1,
static_cast<size_t>(
ny),
static_cast<size_t>(
nx)};
609 coordinate.get(values.data(), start, count);
610 if (!std::all_of(values.begin(), values.end(),
611 [] (
amrex::Real value) { return std::isfinite(value); })) {
612 amrex::Abort(
"WPS terrain coordinate '" +
name +
"' in '" + fname +
613 "' contains nonfinite values");
616 validate_wps_coordinate(
"XLAT_M");
617 validate_wps_coordinate(
"XLONG_M");
619 const auto dx = geom.CellSizeArray();
620 const auto close_enough = [] (
amrex::Real lhs,
double rhs) {
623 return std::abs(lhs -
static_cast<amrex::Real>(rhs)) <=
626 if (geom.Domain().length(0) !=
nx || geom.Domain().length(1) !=
ny ||
627 !close_enough(
dx[0], wps_dx) || !close_enough(
dx[1], wps_dy) ||
628 !close_enough(geom.ProbHi(0) - geom.ProbLo(0), wps_dx *
nx) ||
629 !close_enough(geom.ProbHi(1) - geom.ProbLo(1), wps_dy *
ny)) {
630 amrex::Abort(
"WPS terrain grid does not match the ERF Geometry; "
631 "HGT_M dimensions, DX/DY, and the ERF domain extent must agree");
639 m_xterrain.resize(
nx);
640 m_yterrain.resize(
ny);
641 for (
int i = 0; i <
nx; ++i) {
642 m_xterrain[i] = geom.ProbLo(0) +
646 for (
int j = 0; j <
ny; ++j) {
647 m_yterrain[j] = geom.ProbLo(1) +
651 const std::vector<size_t> start{0, 0, 0};
652 const std::vector<size_t> count{
653 1,
static_cast<size_t>(
ny),
static_cast<size_t>(
nx)};
654 var.get(m_zterrain.data(), start, count);
655 mass_point_terrain = 1;
657 const auto x_shape = ncf.var(
"x").shape();
658 const auto y_shape = ncf.var(
"y").shape();
659 if (x_shape.size() != 1 || x_shape[0] !=
static_cast<std::size_t
>(
nx)) {
660 amrex::Abort(
"NetCDF terrain x coordinate in '" + fname +
661 "' must be one-dimensional with length " + std::to_string(
nx));
663 if (y_shape.size() != 1 || y_shape[0] !=
static_cast<std::size_t
>(
ny)) {
664 amrex::Abort(
"NetCDF terrain y coordinate in '" + fname +
665 "' must be one-dimensional with length " + std::to_string(
ny));
668 m_xterrain.resize(
nx);
669 m_yterrain.resize(
ny);
670 ncf.var(
"x").get(m_xterrain.data());
671 ncf.var(
"y").get(m_yterrain.data());
673 amrex::Real x_origin, x_spacing, y_origin, y_spacing;
675 m_xterrain,
nx,
"x", terrain_description, x_origin, x_spacing);
676 if (coordinate_error.empty()) {
678 m_yterrain,
ny,
"y", terrain_description, y_origin, y_spacing);
680 if (!coordinate_error.empty()) {
681 amrex::Abort(coordinate_error);
684 if (has_time_dimension) {
685 const std::vector<size_t> start{0, 0, 0};
686 const std::vector<size_t> count{
687 1,
static_cast<size_t>(
ny),
static_cast<size_t>(
nx)};
688 var.get(m_zterrain.data(), start, count);
690 var.get(m_zterrain.data());
693 if (!std::all_of(m_zterrain.begin(), m_zterrain.end(),
694 [] (
amrex::Real value) { return std::isfinite(value); })) {
695 amrex::Abort(terrain_description +
" contains nonfinite height values");
699 amrex::Print() <<
" Grid size: " <<
nx <<
" x " <<
ny <<
"\n"
700 <<
" X range: [" << m_xterrain[0] <<
", " << m_xterrain[
nx-1] <<
"]\n"
701 <<
" Y range: [" << m_yterrain[0] <<
", " << m_yterrain[
ny-1] <<
"]\n"
702 <<
" Height range: ["
703 << *std::min_element(m_zterrain.begin(), m_zterrain.end()) <<
", "
704 << *std::max_element(m_zterrain.begin(), m_zterrain.end()) <<
"]\n";
708 amrex::ParallelDescriptor::Bcast(&
nx, 1, amrex::ParallelDescriptor::IOProcessorNumber());
709 amrex::ParallelDescriptor::Bcast(&
ny, 1, amrex::ParallelDescriptor::IOProcessorNumber());
710 amrex::ParallelDescriptor::Bcast(&mass_point_terrain, 1,
711 amrex::ParallelDescriptor::IOProcessorNumber());
713 if (!amrex::ParallelDescriptor::IOProcessor()) {
714 m_xterrain.resize(
nx);
715 m_yterrain.resize(
ny);
716 m_zterrain.resize(
nx *
ny);
719 amrex::ParallelDescriptor::Bcast(m_xterrain.data(),
nx, amrex::ParallelDescriptor::IOProcessorNumber());
720 amrex::ParallelDescriptor::Bcast(m_yterrain.data(),
ny, amrex::ParallelDescriptor::IOProcessorNumber());
721 amrex::ParallelDescriptor::Bcast(m_zterrain.data(),
nx *
ny, amrex::ParallelDescriptor::IOProcessorNumber());
724 amrex::Gpu::DeviceVector<amrex::Real> d_xterrain(
nx), d_yterrain(
ny), d_zterrain(
nx *
ny);
725 amrex::Gpu::copy(amrex::Gpu::hostToDevice, m_xterrain.begin(), m_xterrain.end(), d_xterrain.begin());
726 amrex::Gpu::copy(amrex::Gpu::hostToDevice, m_yterrain.begin(), m_yterrain.end(), d_yterrain.begin());
727 amrex::Gpu::copy(amrex::Gpu::hostToDevice, m_zterrain.begin(), m_zterrain.end(), d_zterrain.begin());
733 auto dx_arr = geom.CellSizeArray();
734 auto prob_lo = geom.ProbLoArray();
736 int ilo = geom.Domain().smallEnd(0);
737 int jlo = geom.Domain().smallEnd(1);
738 int klo = geom.Domain().smallEnd(2);
739 int ihi = geom.Domain().bigEnd(0) + 1;
740 int jhi = geom.Domain().bigEnd(1) + 1;
742 amrex::Box zbx = terrain_fab.box();
743 amrex::Array4<amrex::Real>
const& z_arr = terrain_fab.array();
744 const amrex::Real terrain_dx = m_xterrain[1] - m_xterrain[0];
745 const amrex::Real terrain_dy = m_yterrain[1] - m_yterrain[0];
747 if (!mass_point_terrain && !zbx.isEmpty()) {
748 amrex::Box target_box = zbx & amrex::convert(geom.Domain(), zbx.ixType());
749 if (!target_box.isEmpty()) {
750 target_box.setSmall(2, zbx.smallEnd(2));
751 target_box.setBig(2, zbx.smallEnd(2));
752 amrex::ReduceOps<amrex::ReduceOpSum, amrex::ReduceOpSum> reduce_op;
753 amrex::ReduceData<amrex::Long, amrex::Long> reduce_data(reduce_op);
754 reduce_op.eval(target_box, reduce_data,
755 [=] AMREX_GPU_DEVICE (
int i,
int j,
int ) -> amrex::GpuTuple<amrex::Long, amrex::Long> {
759 x, d_xt[0], terrain_dx,
nx);
761 y, d_yt[0], terrain_dy,
ny);
762 const amrex::Long outside =
763 (x_stencil.inside && y_stencil.inside) ? amrex::Long(0) : amrex::Long(1);
764 return {amrex::Long(1), outside};
771 const auto local = reduce_data.value();
772 const amrex::Long total_targets = amrex::get<0>(local);
773 const amrex::Long outside_targets = amrex::get<1>(local);
774 if (amrex::ParallelDescriptor::IOProcessor() && outside_targets > 0) {
784 amrex::Print() <<
"WARNING: Terrain source grid does not cover ERF terrain nodes: outside "
785 << outside_targets <<
" of " << total_targets <<
" ("
786 << percentage <<
"%). Source extent x=[" << m_xterrain[0]
787 <<
", " << source_xhi <<
"] y=[" << m_yterrain[0] <<
", "
788 << source_yhi <<
"]; target extent x=[" << target_xlo <<
", "
789 << target_xhi <<
"] y=[" << target_ylo <<
", " << target_yhi
790 <<
"]. Outside nodes are set to zero.\n";
797 int ii = amrex::min(amrex::max(i, ilo), ihi);
798 int jj = amrex::min(amrex::max(j, jlo), jhi);
805 if (mass_point_terrain) {
810 x = amrex::min(amrex::max(
x, d_xt[0]), d_xt[
nx-1]);
811 y = amrex::min(amrex::max(
y, d_yt[0]), d_yt[
ny-1]);
814 x, d_xt[0], dx_t,
nx);
816 y, d_yt[0], dy_t,
ny);
818 if (x_stencil.inside && y_stencil.inside) {
819 const int i_t = x_stencil.lower;
820 const int j_t = y_stencil.lower;
841 amrex::Gpu::synchronize();
848 const amrex::Geometry& geom,
849 amrex::FArrayBox& terrain_fab,
852 amrex::Vector<amrex::Real> m_xterrain,m_yterrain,m_zterrain;
854 int nx = 0;
int ny = 0;
856 if (amrex::ParallelDescriptor::IOProcessor()) {
858 amrex::Print()<<
"Reading terrain file: "<< fname<< std::endl;
859 std::ifstream file(fname);
861 if (!file.is_open()) {
862 amrex::Abort(
"Error: Could not open the file " + fname+
"\n");
866 if (file.peek() == std::ifstream::traits_type::eof()) {
867 amrex::Abort(
"Error: The file " + fname+
" is empty.\n");
875 file >> lon_min >> lat_min;
877 amrex::Error(
"The value of longitude for entry in the first line in " + fname
878 +
" should not exceed amrex::Real(180.) It is " + std::to_string(lon_min));
881 amrex::Error(
"The value of latitude for entry in the first line in " + fname
882 +
" should not exceed amrex::Real(90.) It is " + std::to_string(lat_min));
888 while (file >> value1 >> value2 >> value3) {
889 m_xterrain.push_back(value1);
891 m_yterrain.push_back(value2);
893 m_zterrain.push_back(value3);
896 AMREX_ASSERT(m_xterrain.size() ==
static_cast<long int>(
nx*
ny));
897 AMREX_ASSERT(m_yterrain.size() ==
static_cast<long int>(
ny));
898 AMREX_ASSERT(m_zterrain.size() ==
static_cast<long int>(
nx*
ny));
902 nx = erf_get_single_value<int>(file,cnt); cnt++;
903 ny = erf_get_single_value<int>(file,cnt); cnt++;
904 amrex::Print()<<
"Expecting " <<
nx <<
" values of x, " <<
905 ny <<
" values of y, and " <<
906 nx*
ny <<
" values of z" << std::endl;
909 m_xterrain.resize(
nx);
910 m_yterrain.resize(
ny);
911 m_zterrain.resize(
nx *
ny);
912 for (
int n = 0; n <
nx; n++) {
913 m_xterrain[n] = erf_get_single_value<amrex::Real>(file,cnt);
916 for (
int n = 0; n <
ny; n++) {
917 m_yterrain[n] = erf_get_single_value<amrex::Real>(file,cnt);
920 for (
int n = 0; n <
nx *
ny; n++) {
921 m_zterrain[n] = erf_get_single_value<amrex::Real>(file,cnt);
930 amrex::ParallelDescriptor::Bcast(&
nx,1,amrex::ParallelDescriptor::IOProcessorNumber());
931 amrex::ParallelDescriptor::Bcast(&
ny,1,amrex::ParallelDescriptor::IOProcessorNumber());
937 "USGS terrain file must have at least two points in each direction");
941 int nx_vals = is_usgs ? (
nx *
ny) :
nx;
943 m_xterrain.resize(nx_vals);
944 m_yterrain.resize(
ny);
945 m_zterrain.resize(nz);
947 amrex::ParallelDescriptor::Bcast(m_xterrain.data(),nx_vals,amrex::ParallelDescriptor::IOProcessorNumber());
948 amrex::ParallelDescriptor::Bcast(m_yterrain.data(),
ny,amrex::ParallelDescriptor::IOProcessorNumber());
949 amrex::ParallelDescriptor::Bcast(m_zterrain.data(),nz,amrex::ParallelDescriptor::IOProcessorNumber());
952 amrex::Gpu::DeviceVector<amrex::Real> d_xterrain(nx_vals),d_yterrain(
ny),d_zterrain(nz);
953 amrex::Gpu::copy(amrex::Gpu::hostToDevice, m_xterrain.begin(), m_xterrain.end(), d_xterrain.begin());
954 amrex::Gpu::copy(amrex::Gpu::hostToDevice, m_yterrain.begin(), m_yterrain.end(), d_yterrain.begin());
955 amrex::Gpu::copy(amrex::Gpu::hostToDevice, m_zterrain.begin(), m_zterrain.end(), d_zterrain.begin());
961 auto dx = geom.CellSizeArray();
962 auto ProbLoArr = geom.ProbLoArray();
964 int ilo = geom.Domain().smallEnd(0);
965 int jlo = geom.Domain().smallEnd(1);
966 int klo = geom.Domain().smallEnd(2);
967 int ihi = geom.Domain().bigEnd(0) + 1;
968 int jhi = geom.Domain().bigEnd(1) + 1;
970 amrex::Box zbx = terrain_fab.box();
971 amrex::Array4<amrex::Real>
const& z_arr = terrain_fab.array();
976 int ii = amrex::min(amrex::max(i,ilo),ihi);
977 int jj = amrex::min(amrex::max(j,jlo),jhi);
983 int ind11, ind12, ind21, ind22;
986 int iindex_terrain=-1;
987 int jindex_terrain=-1;
998 for (
int it = 0; it <
ny && !jfound; it++) {
1000 jindex_terrain = it-1; jfound =
true;
1004 jindex_terrain =
ny-1;
1007 jindex_terrain = amrex::min(amrex::max(jindex_terrain,0),
ny-2);
1009 int gstart = (jindex_terrain )*
nx;
1010 int gend = (jindex_terrain+1)*
nx-1;
1011 bool ifound =
false;
1012 for (
int it = gstart; it <= gend && !ifound; it++) {
1014 iindex_terrain = it-gstart-1; ifound =
true;
1018 iindex_terrain =
nx-1;
1021 iindex_terrain = amrex::min(amrex::max(iindex_terrain,0),
nx-2);
1024 ind11 = jindex_terrain*
nx + iindex_terrain;
1031 y1 = d_yt[jindex_terrain];
1032 y2 = d_yt[jindex_terrain+1];
1038 z_arr(i,j,
klo) = d_zt[ind11];
1046 z_arr(i,j,
klo) = w_11*d_zt[ind11] + w_12*d_zt[ind12] + w_21*d_zt[ind21] + w_22*d_zt[ind22];
1051 bool jfound =
false;
1052 for (
int it = 0; it <
ny && !jfound; it++) {
1054 jindex_terrain = it-1; jfound =
true;
1059 jindex_terrain =
ny-1;
1061 jindex_terrain = amrex::max(jindex_terrain,0);
1063 bool ifound =
false;
1064 for (
int it = 0; it <
nx && !ifound; it++) {
1066 iindex_terrain = it-1; ifound =
true;
1071 iindex_terrain =
nx-1;
1073 iindex_terrain = amrex::max(iindex_terrain,0);
1077 int ip1 = amrex::min(iindex_terrain+1,
nx-1);
1078 int jp1 = amrex::min(jindex_terrain+1,
ny-1);
1081 x1 = d_xt[iindex_terrain];
1083 y1 = d_yt[jindex_terrain];
1090 ind11 = iindex_terrain *
ny + jindex_terrain;
1091 ind21 = ip1 *
ny + jindex_terrain;
1093 ind12 = iindex_terrain *
ny + jp1;
1094 ind22 = ip1 *
ny + jp1;
1100 ind11 = jindex_terrain *
nx + iindex_terrain;
1101 ind12 = jp1 *
nx + iindex_terrain;
1103 ind21 = jindex_terrain *
nx + ip1;
1104 ind22 = jp1 *
nx + ip1;
1108 bool interp_x = (ip1 != iindex_terrain) && (x2 != x1);
1109 bool interp_y = (jp1 != jindex_terrain) && (y2 != y1);
1111 if (!interp_x && !interp_y)
1113 z_arr(i,j,
klo) = d_zt[ind11];
1115 else if (interp_x && !interp_y)
1120 z_arr(i,j,
klo) = (w_11*d_zt[ind11] + w_21*d_zt[ind21])/denom;
1122 else if (!interp_x && interp_y)
1127 z_arr(i,j,
klo) = (w_11*d_zt[ind11] + w_12*d_zt[ind12])/denom;
1136 z_arr(i,j,
klo) = (w_11*d_zt[ind11] + w_12*d_zt[ind12] + w_21*d_zt[ind21] + w_22*d_zt[ind22]) / denom;
1153 amrex::FArrayBox& terrain_fab,
1156 std::string custom_terrain_type =
"None";
1157 amrex::ParmParse
pp_prob(
"prob");
pp_prob.queryAdd(
"custom_terrain_type",custom_terrain_type);
1159 if (custom_terrain_type !=
"None")
1161 amrex::Print() <<
"Calling custom terrain initialization" << std::endl;
1165 amrex::Print() <<
"Initializing flat terrain" << std::endl;
1166 terrain_fab.template setVal<amrex::RunOn::Device>(0);
1170 amrex::Print() <<
"Resetting mesh type to StretchedDz" << std::endl;
1175 #ifdef ERF_USE_TERRAIN_VELOCITY
1176 virtual amrex::Real compute_terrain_velocity(
const double )
1178 amrex::Error(
"Should never call compute_terrain_velocity for "+
name()+
" problem");
1191 amrex::Geometry
const& ,
1192 std::unique_ptr<amrex::MultiFab>& ,
1212 std::string prob_name;
1213 amrex::ParmParse
pp(
"erf");
1214 pp.get(
"prob_name",prob_name);
return prob_name;
1224 const amrex_real*
probhi) AMREX_ATTRIBUTE_WEAK;
@ wbar
Definition: ERF_DataStruct.H:179
auto probhi
Definition: ERF_InitCustomPertVels_ABL.H:37
auto problo
Definition: ERF_InitCustomPertVels_ABL.H:36
const int nx
Definition: ERF_InitCustomPertVels_CloudChamber.H:14
const int ny
Definition: ERF_InitCustomPertVels_CloudChamber.H:15
const int klo
Definition: ERF_InitCustomPert_ABL.H:75
Real rho_0
Definition: ERF_InitCustomPert_ABL.H:18
ParmParse pp_prob("prob")
Real T_0
Definition: ERF_InitCustomPert_ABL.H:19
const Real dx
Definition: ERF_InitCustomPert_ABL.H:44
const int khi
Definition: ERF_InitCustomPert_Bubble.H:21
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")
const GpuArray< Real, AMREX_SPACEDIM > prob_lo
Definition: ERF_InitCustomPert_CloudChamber.H:33
ParallelFor(fab_box, [=] AMREX_GPU_DEVICE(int i, int j, int k) { qrcuten_arr(i, j, k)=Real(0);qscuten_arr(i, j, k)=Real(0);qicuten_arr(i, j, k)=Real(0);})
Dimensionless numeric literals and pure mathematical constants.
constexpr amrex::Real one
Definition: ERF_NumericalConstants.H:30
constexpr amrex::Real zero
Definition: ERF_NumericalConstants.H:29
void init_my_custom_terrain(const amrex::Geometry &geom, amrex::FArrayBox &terrain_fab, const double &time)
T erf_get_single_value(std::istream &is, int n)
Definition: ERF_ProbCommon.H:31
std::unique_ptr< ProblemBase > amrex_probinit(const amrex_real *problo, const amrex_real *probhi) AMREX_ATTRIBUTE_WEAK
amrex::Real Real
Definition: ERF_ShocInterface.H:19
Definition: ERF_ProbCommon.H:60
virtual void update_w_subsidence(const double &, amrex::Vector< amrex::Real > &wbar, amrex::Gpu::DeviceVector< amrex::Real > &d_wbar, const amrex::MultiFab &, const amrex::Geometry &geom, std::unique_ptr< amrex::MultiFab > &)
Definition: ERF_ProbCommon.H:262
void init_terrain_surface(const amrex::Geometry &geom, amrex::FArrayBox &terrain_fab, const double &time)
Definition: ERF_ProbCommon.H:358
virtual void update_geostrophic_profile(const double &, amrex::Vector< amrex::Real > &u_geos, amrex::Gpu::DeviceVector< amrex::Real > &d_u_geos, amrex::Vector< amrex::Real > &v_geos, amrex::Gpu::DeviceVector< amrex::Real > &d_v_geos, const amrex::Geometry &geom, std::unique_ptr< amrex::MultiFab > &)
Definition: ERF_ProbCommon.H:297
virtual void update_rhotheta_sources(const double &, amrex::MultiFab *src, const amrex::Geometry &, std::unique_ptr< amrex::MultiFab > &)
Definition: ERF_ProbCommon.H:195
virtual void erf_init_const_dens_and_linear_th_hse(amrex::MultiFab &, amrex::MultiFab &, amrex::MultiFab &, amrex::MultiFab &, amrex::MultiFab &, amrex::Real, std::unique_ptr< amrex::MultiFab > &)
Definition: ERF_ProbCommon.H:106
virtual void init_custom_pert_vels(const amrex::Box &, const amrex::Box &, const amrex::Box &, amrex::Array4< amrex::Real > const &, amrex::Array4< amrex::Real > const &, amrex::Array4< amrex::Real > const &, amrex::Array4< amrex::Real const > const &, amrex::GeometryData const &, amrex::Array4< amrex::Real const > const &, amrex::Array4< amrex::Real const > const &, const SolverChoice &, const int)
Definition: ERF_ProbCommon.H:170
virtual bool terrain_is_specified()
Definition: ERF_ProbCommon.H:399
virtual void erf_init_const_dens_hse(amrex::MultiFab &)
Definition: ERF_ProbCommon.H:92
virtual void init_custom_terrain(const amrex::Geometry &geom, amrex::FArrayBox &terrain_fab, const double &time)
Definition: ERF_ProbCommon.H:1152
virtual void erf_init_dens_hse_dry(amrex::MultiFab &, std::unique_ptr< amrex::MultiFab > &, std::unique_ptr< amrex::MultiFab > &, amrex::Geometry const &, const amrex::Vector< amrex::Real > &, bool, bool)
Definition: ERF_ProbCommon.H:77
virtual void update_rhoqt_sources(const double &, amrex::MultiFab *qsrc, const amrex::Geometry &, std::unique_ptr< amrex::MultiFab > &)
Definition: ERF_ProbCommon.H:227
void init_buildings_surface(const amrex::Geometry &geom, amrex::FArrayBox &buildings_fab, const double &time)
Definition: ERF_ProbCommon.H:333
void read_custom_terrain(const std::string &fname, const bool is_usgs, const amrex::Geometry &geom, amrex::FArrayBox &terrain_fab, const double &)
Definition: ERF_ProbCommon.H:846
virtual void erf_init_rayleigh(amrex::Vector< amrex::Vector< amrex::Real > > &, amrex::Geometry const &, std::unique_ptr< amrex::MultiFab > &, amrex::Real)
Definition: ERF_ProbCommon.H:1190
void init_base_parms(amrex::Real rho_0, amrex::Real T_0)
Definition: ERF_ProbCommon.H:1206
virtual ~ProblemBase()=default
virtual void erf_init_dens_hse_moist(amrex::MultiFab &, std::unique_ptr< amrex::MultiFab > &, amrex::Geometry const &)
Definition: ERF_ProbCommon.H:115
virtual void init_custom_pert(const amrex::Box &, amrex::Array4< amrex::Real const > const &, amrex::Array4< amrex::Real > const &, amrex::Array4< amrex::Real > const &, amrex::Array4< amrex::Real > const &, amrex::Array4< amrex::Real const > const &, amrex::Array4< amrex::Real const > const &, amrex::GeometryData const &, amrex::Array4< amrex::Real const > const &, const SolverChoice &, const int)
Definition: ERF_ProbCommon.H:138
std::string name()
Definition: ERF_ProbCommon.H:1211
virtual void erf_init_const_dens_and_th_hse(amrex::MultiFab &, amrex::MultiFab &, amrex::MultiFab &, amrex::MultiFab &, amrex::MultiFab &, amrex::Real)
Definition: ERF_ProbCommon.H:98
ProbParmDefaults base_parms
Definition: ERF_ProbCommon.H:1200
@ T
Definition: ERF_IndexDefines.H:128
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:56
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:173
Definition: ERF_ProbCommon.H:25
amrex::Real T_0
Definition: ERF_ProbCommon.H:27
amrex::Real rho_0
Definition: ERF_ProbCommon.H:26
Definition: ERF_DataStruct.H:662
static MeshType mesh_type
Vertical mesh representation.
Definition: ERF_DataStruct.H:1958
static void set_mesh_type(MeshType new_mesh_type)
Override the globally selected mesh type.
Definition: ERF_DataStruct.H:1965