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;
197 amrex::MultiFab* src,
198 const amrex::Geometry& ,
199 std::unique_ptr<amrex::MultiFab>& ,
200 const amrex::Vector<amrex::Real>& )
202 if (src->empty())
return;
204 amrex::Warning(
"Temperature forcing not defined for "+
name()+
" problem");
205 for ( amrex::MFIter mfi(*src, amrex::TilingIfNotGPU()); mfi.isValid(); ++mfi )
207 const auto &box = mfi.tilebox();
208 const amrex::Array4<amrex::Real>& src_arr = src->array(mfi);
211 ParallelFor(box, [=] AMREX_GPU_DEVICE (
int i,
int j,
int k) noexcept {
214 src_arr(i, j, k) =
zero;
231 amrex::MultiFab* qsrc,
232 const amrex::Geometry& ,
233 std::unique_ptr<amrex::MultiFab>& ,
234 const amrex::Vector<amrex::Real>& )
236 if (qsrc->empty())
return;
238 amrex::Warning(
"Moisture forcing not defined for "+
name()+
" problem");
239 for ( amrex::MFIter mfi(*qsrc, amrex::TilingIfNotGPU()); mfi.isValid(); ++mfi )
241 const auto &box = mfi.tilebox();
242 const amrex::Array4<amrex::Real>& qsrc_arr = qsrc->array(mfi);
245 ParallelFor(box, [=] AMREX_GPU_DEVICE (
int i,
int j,
int k) noexcept {
248 qsrc_arr(i, j, k) =
zero;
268 amrex::Vector<amrex::Real>&
wbar,
269 amrex::Gpu::DeviceVector<amrex::Real>& d_wbar,
270 const amrex::MultiFab& ,
271 const amrex::Geometry& geom,
272 std::unique_ptr<amrex::MultiFab>& ,
273 const amrex::Vector<amrex::Real>& )
275 if (
wbar.empty())
return;
277 amrex::Warning(
"Moisture forcing not defined for "+
name()+
" problem");
279 const int khi = geom.Domain().bigEnd()[2];
282 for (
int k = 0; k <=
khi; k++)
290 amrex::Gpu::copy(amrex::Gpu::hostToDevice,
wbar.begin(),
wbar.end(), d_wbar.begin());
305 amrex::Vector<amrex::Real>& u_geos,
306 amrex::Gpu::DeviceVector<amrex::Real>& d_u_geos,
307 amrex::Vector<amrex::Real>& v_geos,
308 amrex::Gpu::DeviceVector<amrex::Real>& d_v_geos,
309 const amrex::Geometry& geom,
310 std::unique_ptr<amrex::MultiFab>& ,
311 const amrex::Vector<amrex::Real>& )
313 if (u_geos.empty())
return;
315 amrex::Warning(
"Geostrophic wind profile not defined for "+
name()+
" problem");
317 const int khi = geom.Domain().bigEnd()[2];
320 for (
int k = 0; k <=
khi; k++)
329 amrex::Gpu::copy(amrex::Gpu::hostToDevice, u_geos.begin(), u_geos.end(), d_u_geos.begin());
330 amrex::Gpu::copy(amrex::Gpu::hostToDevice, v_geos.begin(), v_geos.end(), d_v_geos.begin());
342 amrex::FArrayBox& buildings_fab,
347 amrex::ParmParse
pp(
"erf");
348 auto valid_fname =
pp.queryAdd(
"buildings_file_name",fname);
367 amrex::FArrayBox& terrain_fab,
370 amrex::ParmParse
pp(
"erf");
373 std::string fname_nc;
374 auto valid_fname_nc =
pp.query(
"terrain_file_name_nc", fname_nc);
376 std::string fname, fname_usgs;
377 auto valid_fname =
pp.query(
"terrain_file_name", fname);
378 auto valid_fname_USGS =
pp.query(
"terrain_file_name_USGS", fname_usgs);
380 if (valid_fname_nc) {
381 #ifdef ERF_USE_NETCDF
382 read_terrain_netcdf(fname_nc, geom, terrain_fab, time);
384 amrex::Abort(
"ERF must be compiled with NetCDF support to use terrain_file_name_nc");
386 }
else if (valid_fname) {
389 }
else if (valid_fname_USGS) {
409 amrex::ParmParse
pp(
"erf");
411 if (
pp.query(
"terrain_file_name_nc", fname) ||
412 pp.query(
"terrain_file_name", fname) ||
413 pp.query(
"terrain_file_name_USGS", fname)) {
417 std::string custom_terrain_type =
"None";
418 amrex::ParmParse
pp_prob(
"prob");
419 pp_prob.query(
"custom_terrain_type", custom_terrain_type);
420 return (custom_terrain_type !=
"None");
423 #ifdef ERF_USE_NETCDF
425 read_terrain_netcdf (
const std::string& fname,
426 const amrex::Geometry& geom,
427 amrex::FArrayBox& terrain_fab,
430 amrex::Print() <<
"Reading terrain from NetCDF file: " << fname << std::endl;
432 amrex::Vector<amrex::Real> m_xterrain, m_yterrain, m_zterrain;
436 int mass_point_terrain = 0;
438 if (amrex::ParallelDescriptor::IOProcessor()) {
439 auto ncf = ncutils::NCFile::open(fname, NC_NOWRITE);
442 std::string height_varname =
"height";
443 if (!ncf.has_var(
"height")) {
444 if (ncf.has_var(
"z")) height_varname =
"z";
445 else if (ncf.has_var(
"terrain")) height_varname =
"terrain";
446 else if (ncf.has_var(
"HGT_M")) height_varname =
"HGT_M";
447 else if (ncf.has_var(
"AGL")) height_varname =
"AGL";
449 amrex::Abort(
"No terrain height variable found in NetCDF file. "
450 "Expected: 'height', 'z', 'terrain', 'HGT_M', or 'AGL'");
452 amrex::Print() <<
" Using terrain variable: " << height_varname << std::endl;
454 auto var = ncf.var(height_varname);
455 auto dims = var.shape();
456 const auto dim_names = var.dimnames();
457 const auto is_y_dimension = [] (
const std::string&
name) {
458 return name ==
"y" ||
name ==
"south_north";
460 const auto is_x_dimension = [] (
const std::string&
name) {
461 return name ==
"x" ||
name ==
"west_east";
463 const auto is_time_dimension = [] (
const std::string&
name) {
464 return name ==
"time" ||
name ==
"Time";
467 bool has_time_dimension =
false;
468 if (dims.size() == 2) {
469 if (dim_names.size() != 2 || !is_y_dimension(dim_names[0]) ||
470 !is_x_dimension(dim_names[1])) {
471 amrex::Abort(
"NetCDF terrain field '" + height_varname +
"' in '" +
472 fname +
"' must use dimensions (y,x) or "
473 "(south_north,west_east)");
477 }
else if (dims.size() == 3) {
478 if (dim_names.size() != 3 || !is_time_dimension(dim_names[0]) ||
479 !is_y_dimension(dim_names[1]) || !is_x_dimension(dim_names[2])) {
480 amrex::Abort(
"NetCDF terrain field '" + height_varname +
"' in '" +
481 fname +
"' must use dimensions (time,y,x) with time leading; "
482 "accepted layouts are (y,x) and (time,y,x)");
485 amrex::Abort(
"NetCDF terrain field '" + height_varname +
"' in '" +
486 fname +
"' has no time records");
488 has_time_dimension =
true;
492 amrex::Abort(
"NetCDF terrain field '" + height_varname +
"' in '" + fname +
493 "' has unsupported rank " + std::to_string(dims.size()) +
494 "; accepted layouts are (y,x) and (time,y,x)");
497 if (
nx < 2 ||
ny < 2) {
498 amrex::Abort(
"NetCDF terrain field '" + height_varname +
"' in '" +
499 fname +
"' requires at least two x and y coordinates "
500 "for bilinear interpolation");
503 const bool has_x = ncf.has_var(
"x");
504 const bool has_y = ncf.has_var(
"y");
505 if (has_x != has_y) {
506 amrex::Abort(
"NetCDF terrain file '" + fname +
507 "' must provide both one-dimensional 'x' and 'y' coordinates");
510 const std::string terrain_description =
511 "NetCDF terrain field '" + height_varname +
"' in '" + fname +
"'";
512 m_zterrain.resize(
nx *
ny);
514 if (height_varname !=
"HGT_M" || !has_time_dimension ||
515 dim_names != std::vector<std::string>{
"Time",
"south_north",
"west_east"}) {
516 amrex::Abort(
"NetCDF terrain field '" + height_varname +
"' in '" + fname +
517 "' requires one-dimensional 'x' and 'y' coordinates; a genuine "
518 "WPS file must provide HGT_M(Time,south_north,west_east)");
521 const auto require_real_attribute = [&] (
const std::string&
name) {
522 if (!ncf.has_attr(
name)) {
523 amrex::Abort(
"WPS terrain file '" + fname +
524 "' is missing required global attribute '" +
name +
"'");
526 std::vector<double> values;
527 ncf.get_attr(
name, values);
528 if (values.size() != 1 || !std::isfinite(values[0])) {
529 amrex::Abort(
"WPS terrain file '" + fname +
530 "' must have one finite global attribute '" +
name +
"'");
534 const auto require_int_attribute = [&] (
const std::string&
name) {
535 if (!ncf.has_attr(
name)) {
536 amrex::Abort(
"WPS terrain file '" + fname +
537 "' is missing required global attribute '" +
name +
"'");
539 std::vector<int> values;
540 ncf.get_attr(
name, values);
541 if (values.size() != 1) {
542 amrex::Abort(
"WPS terrain file '" + fname +
543 "' must have one global attribute '" +
name +
"'");
548 const int map_proj = require_int_attribute(
"MAP_PROJ");
559 if (map_proj != 1 && map_proj != 2 && map_proj != 3) {
562 ?
"; latitude/longitude WPS grids are not supported because their "
563 "DX/DY are in degrees rather than metres"
564 :
"; supported projections are 1 (Lambert), 2 (polar), "
566 amrex::Abort(
"WPS terrain file '" + fname +
"' uses unsupported MAP_PROJ=" +
567 std::to_string(map_proj) + reason);
569 const double cen_lat = require_real_attribute(
"CEN_LAT");
570 const double cen_lon = require_real_attribute(
"CEN_LON");
571 const double stand_lon = require_real_attribute(
"STAND_LON");
572 const double wps_dx = require_real_attribute(
"DX");
573 const double wps_dy = require_real_attribute(
"DY");
574 if (wps_dx <= 0.0 || wps_dy <= 0.0 || std::abs(cen_lat) > 90.0 ||
575 std::abs(cen_lon) > 360.0 || std::abs(stand_lon) > 360.0) {
576 amrex::Abort(
"WPS terrain file '" + fname +
577 "' has invalid projection or grid metadata");
580 const double truelat1 = require_real_attribute(
"TRUELAT1");
581 if (std::abs(truelat1) > 90.0) {
582 amrex::Abort(
"WPS terrain file '" + fname +
583 "' has invalid TRUELAT1");
586 const double truelat2 = require_real_attribute(
"TRUELAT2");
587 if (std::abs(truelat2) > 90.0) {
588 amrex::Abort(
"WPS terrain file '" + fname +
589 "' has invalid TRUELAT2");
593 const int wps_nx = require_int_attribute(
"WEST-EAST_GRID_DIMENSION");
594 const int wps_ny = require_int_attribute(
"SOUTH-NORTH_GRID_DIMENSION");
595 if (wps_nx !=
nx + 1 || wps_ny !=
ny + 1) {
596 amrex::Abort(
"WPS terrain file '" + fname +
597 "' has grid dimensions inconsistent with HGT_M: expected " +
598 std::to_string(
nx + 1) +
" x " + std::to_string(
ny + 1));
601 const auto validate_wps_coordinate = [&] (
const std::string&
name) {
602 if (!ncf.has_var(
name)) {
603 amrex::Abort(
"WPS terrain file '" + fname +
604 "' is missing required coordinate variable '" +
name +
"'");
606 const auto coordinate = ncf.var(
name);
607 if (coordinate.shape() != dims ||
608 coordinate.dimnames() != dim_names) {
609 amrex::Abort(
"WPS terrain coordinate '" +
name +
"' in '" + fname +
610 "' must have dimensions (Time,south_north,west_east) "
613 amrex::Vector<amrex::Real> values(
nx *
ny);
614 const std::vector<size_t> start{0, 0, 0};
615 const std::vector<size_t> count{
616 1,
static_cast<size_t>(
ny),
static_cast<size_t>(
nx)};
617 coordinate.get(values.data(), start, count);
618 if (!std::all_of(values.begin(), values.end(),
619 [] (
amrex::Real value) { return std::isfinite(value); })) {
620 amrex::Abort(
"WPS terrain coordinate '" +
name +
"' in '" + fname +
621 "' contains nonfinite values");
624 validate_wps_coordinate(
"XLAT_M");
625 validate_wps_coordinate(
"XLONG_M");
627 const auto dx = geom.CellSizeArray();
628 const auto close_enough = [] (
amrex::Real lhs,
double rhs) {
631 return std::abs(lhs -
static_cast<amrex::Real>(rhs)) <=
634 if (geom.Domain().length(0) !=
nx || geom.Domain().length(1) !=
ny ||
635 !close_enough(
dx[0], wps_dx) || !close_enough(
dx[1], wps_dy) ||
636 !close_enough(geom.ProbHi(0) - geom.ProbLo(0), wps_dx *
nx) ||
637 !close_enough(geom.ProbHi(1) - geom.ProbLo(1), wps_dy *
ny)) {
638 amrex::Abort(
"WPS terrain grid does not match the ERF Geometry; "
639 "HGT_M dimensions, DX/DY, and the ERF domain extent must agree");
647 m_xterrain.resize(
nx);
648 m_yterrain.resize(
ny);
649 for (
int i = 0; i <
nx; ++i) {
650 m_xterrain[i] = geom.ProbLo(0) +
654 for (
int j = 0; j <
ny; ++j) {
655 m_yterrain[j] = geom.ProbLo(1) +
659 const std::vector<size_t> start{0, 0, 0};
660 const std::vector<size_t> count{
661 1,
static_cast<size_t>(
ny),
static_cast<size_t>(
nx)};
662 var.get(m_zterrain.data(), start, count);
663 mass_point_terrain = 1;
665 const auto x_shape = ncf.var(
"x").shape();
666 const auto y_shape = ncf.var(
"y").shape();
667 if (x_shape.size() != 1 || x_shape[0] !=
static_cast<std::size_t
>(
nx)) {
668 amrex::Abort(
"NetCDF terrain x coordinate in '" + fname +
669 "' must be one-dimensional with length " + std::to_string(
nx));
671 if (y_shape.size() != 1 || y_shape[0] !=
static_cast<std::size_t
>(
ny)) {
672 amrex::Abort(
"NetCDF terrain y coordinate in '" + fname +
673 "' must be one-dimensional with length " + std::to_string(
ny));
676 m_xterrain.resize(
nx);
677 m_yterrain.resize(
ny);
678 ncf.var(
"x").get(m_xterrain.data());
679 ncf.var(
"y").get(m_yterrain.data());
681 amrex::Real x_origin, x_spacing, y_origin, y_spacing;
683 m_xterrain,
nx,
"x", terrain_description, x_origin, x_spacing);
684 if (coordinate_error.empty()) {
686 m_yterrain,
ny,
"y", terrain_description, y_origin, y_spacing);
688 if (!coordinate_error.empty()) {
689 amrex::Abort(coordinate_error);
692 if (has_time_dimension) {
693 const std::vector<size_t> start{0, 0, 0};
694 const std::vector<size_t> count{
695 1,
static_cast<size_t>(
ny),
static_cast<size_t>(
nx)};
696 var.get(m_zterrain.data(), start, count);
698 var.get(m_zterrain.data());
701 if (!std::all_of(m_zterrain.begin(), m_zterrain.end(),
702 [] (
amrex::Real value) { return std::isfinite(value); })) {
703 amrex::Abort(terrain_description +
" contains nonfinite height values");
707 amrex::Print() <<
" Grid size: " <<
nx <<
" x " <<
ny <<
"\n"
708 <<
" X range: [" << m_xterrain[0] <<
", " << m_xterrain[
nx-1] <<
"]\n"
709 <<
" Y range: [" << m_yterrain[0] <<
", " << m_yterrain[
ny-1] <<
"]\n"
710 <<
" Height range: ["
711 << *std::min_element(m_zterrain.begin(), m_zterrain.end()) <<
", "
712 << *std::max_element(m_zterrain.begin(), m_zterrain.end()) <<
"]\n";
716 amrex::ParallelDescriptor::Bcast(&
nx, 1, amrex::ParallelDescriptor::IOProcessorNumber());
717 amrex::ParallelDescriptor::Bcast(&
ny, 1, amrex::ParallelDescriptor::IOProcessorNumber());
718 amrex::ParallelDescriptor::Bcast(&mass_point_terrain, 1,
719 amrex::ParallelDescriptor::IOProcessorNumber());
721 if (!amrex::ParallelDescriptor::IOProcessor()) {
722 m_xterrain.resize(
nx);
723 m_yterrain.resize(
ny);
724 m_zterrain.resize(
nx *
ny);
727 amrex::ParallelDescriptor::Bcast(m_xterrain.data(),
nx, amrex::ParallelDescriptor::IOProcessorNumber());
728 amrex::ParallelDescriptor::Bcast(m_yterrain.data(),
ny, amrex::ParallelDescriptor::IOProcessorNumber());
729 amrex::ParallelDescriptor::Bcast(m_zterrain.data(),
nx *
ny, amrex::ParallelDescriptor::IOProcessorNumber());
732 amrex::Gpu::DeviceVector<amrex::Real> d_xterrain(
nx), d_yterrain(
ny), d_zterrain(
nx *
ny);
733 amrex::Gpu::copy(amrex::Gpu::hostToDevice, m_xterrain.begin(), m_xterrain.end(), d_xterrain.begin());
734 amrex::Gpu::copy(amrex::Gpu::hostToDevice, m_yterrain.begin(), m_yterrain.end(), d_yterrain.begin());
735 amrex::Gpu::copy(amrex::Gpu::hostToDevice, m_zterrain.begin(), m_zterrain.end(), d_zterrain.begin());
741 auto dx_arr = geom.CellSizeArray();
742 auto prob_lo = geom.ProbLoArray();
744 int ilo = geom.Domain().smallEnd(0);
745 int jlo = geom.Domain().smallEnd(1);
746 int klo = geom.Domain().smallEnd(2);
747 int ihi = geom.Domain().bigEnd(0) + 1;
748 int jhi = geom.Domain().bigEnd(1) + 1;
750 amrex::Box zbx = terrain_fab.box();
751 amrex::Array4<amrex::Real>
const& z_arr = terrain_fab.array();
752 const amrex::Real terrain_dx = m_xterrain[1] - m_xterrain[0];
753 const amrex::Real terrain_dy = m_yterrain[1] - m_yterrain[0];
755 if (!mass_point_terrain && !zbx.isEmpty()) {
756 amrex::Box target_box = zbx & amrex::convert(geom.Domain(), zbx.ixType());
757 if (!target_box.isEmpty()) {
758 target_box.setSmall(2, zbx.smallEnd(2));
759 target_box.setBig(2, zbx.smallEnd(2));
760 amrex::ReduceOps<amrex::ReduceOpSum, amrex::ReduceOpSum> reduce_op;
761 amrex::ReduceData<amrex::Long, amrex::Long> reduce_data(reduce_op);
762 reduce_op.eval(target_box, reduce_data,
763 [=] AMREX_GPU_DEVICE (
int i,
int j,
int ) -> amrex::GpuTuple<amrex::Long, amrex::Long> {
767 x, d_xt[0], terrain_dx,
nx);
769 y, d_yt[0], terrain_dy,
ny);
770 const amrex::Long outside =
771 (x_stencil.inside && y_stencil.inside) ? amrex::Long(0) : amrex::Long(1);
772 return {amrex::Long(1), outside};
779 const auto local = reduce_data.value();
780 const amrex::Long total_targets = amrex::get<0>(local);
781 const amrex::Long outside_targets = amrex::get<1>(local);
782 if (amrex::ParallelDescriptor::IOProcessor() && outside_targets > 0) {
792 amrex::Print() <<
"WARNING: Terrain source grid does not cover ERF terrain nodes: outside "
793 << outside_targets <<
" of " << total_targets <<
" ("
794 << percentage <<
"%). Source extent x=[" << m_xterrain[0]
795 <<
", " << source_xhi <<
"] y=[" << m_yterrain[0] <<
", "
796 << source_yhi <<
"]; target extent x=[" << target_xlo <<
", "
797 << target_xhi <<
"] y=[" << target_ylo <<
", " << target_yhi
798 <<
"]. Outside nodes are set to zero.\n";
805 int ii = amrex::min(amrex::max(i, ilo), ihi);
806 int jj = amrex::min(amrex::max(j, jlo), jhi);
813 if (mass_point_terrain) {
818 x = amrex::min(amrex::max(
x, d_xt[0]), d_xt[
nx-1]);
819 y = amrex::min(amrex::max(
y, d_yt[0]), d_yt[
ny-1]);
822 x, d_xt[0], dx_t,
nx);
824 y, d_yt[0], dy_t,
ny);
826 if (x_stencil.inside && y_stencil.inside) {
827 const int i_t = x_stencil.lower;
828 const int j_t = y_stencil.lower;
849 amrex::Gpu::synchronize();
856 const amrex::Geometry& geom,
857 amrex::FArrayBox& terrain_fab,
860 amrex::Vector<amrex::Real> m_xterrain,m_yterrain,m_zterrain;
862 int nx = 0;
int ny = 0;
864 if (amrex::ParallelDescriptor::IOProcessor()) {
866 amrex::Print()<<
"Reading terrain file: "<< fname<< std::endl;
867 std::ifstream file(fname);
869 if (!file.is_open()) {
870 amrex::Abort(
"Error: Could not open the file " + fname+
"\n");
874 if (file.peek() == std::ifstream::traits_type::eof()) {
875 amrex::Abort(
"Error: The file " + fname+
" is empty.\n");
883 file >> lon_min >> lat_min;
885 amrex::Error(
"The value of longitude for entry in the first line in " + fname
886 +
" should not exceed amrex::Real(180.) It is " + std::to_string(lon_min));
889 amrex::Error(
"The value of latitude for entry in the first line in " + fname
890 +
" should not exceed amrex::Real(90.) It is " + std::to_string(lat_min));
896 while (file >> value1 >> value2 >> value3) {
897 m_xterrain.push_back(value1);
899 m_yterrain.push_back(value2);
901 m_zterrain.push_back(value3);
904 AMREX_ASSERT(m_xterrain.size() ==
static_cast<long int>(
nx*
ny));
905 AMREX_ASSERT(m_yterrain.size() ==
static_cast<long int>(
ny));
906 AMREX_ASSERT(m_zterrain.size() ==
static_cast<long int>(
nx*
ny));
910 nx = erf_get_single_value<int>(file,cnt); cnt++;
911 ny = erf_get_single_value<int>(file,cnt); cnt++;
912 amrex::Print()<<
"Expecting " <<
nx <<
" values of x, " <<
913 ny <<
" values of y, and " <<
914 nx*
ny <<
" values of z" << std::endl;
917 m_xterrain.resize(
nx);
918 m_yterrain.resize(
ny);
919 m_zterrain.resize(
nx *
ny);
920 for (
int n = 0; n <
nx; n++) {
921 m_xterrain[n] = erf_get_single_value<amrex::Real>(file,cnt);
924 for (
int n = 0; n <
ny; n++) {
925 m_yterrain[n] = erf_get_single_value<amrex::Real>(file,cnt);
928 for (
int n = 0; n <
nx *
ny; n++) {
929 m_zterrain[n] = erf_get_single_value<amrex::Real>(file,cnt);
938 amrex::ParallelDescriptor::Bcast(&
nx,1,amrex::ParallelDescriptor::IOProcessorNumber());
939 amrex::ParallelDescriptor::Bcast(&
ny,1,amrex::ParallelDescriptor::IOProcessorNumber());
945 "USGS terrain file must have at least two points in each direction");
949 int nx_vals = is_usgs ? (
nx *
ny) :
nx;
951 m_xterrain.resize(nx_vals);
952 m_yterrain.resize(
ny);
953 m_zterrain.resize(nz);
955 amrex::ParallelDescriptor::Bcast(m_xterrain.data(),nx_vals,amrex::ParallelDescriptor::IOProcessorNumber());
956 amrex::ParallelDescriptor::Bcast(m_yterrain.data(),
ny,amrex::ParallelDescriptor::IOProcessorNumber());
957 amrex::ParallelDescriptor::Bcast(m_zterrain.data(),nz,amrex::ParallelDescriptor::IOProcessorNumber());
960 amrex::Gpu::DeviceVector<amrex::Real> d_xterrain(nx_vals),d_yterrain(
ny),d_zterrain(nz);
961 amrex::Gpu::copy(amrex::Gpu::hostToDevice, m_xterrain.begin(), m_xterrain.end(), d_xterrain.begin());
962 amrex::Gpu::copy(amrex::Gpu::hostToDevice, m_yterrain.begin(), m_yterrain.end(), d_yterrain.begin());
963 amrex::Gpu::copy(amrex::Gpu::hostToDevice, m_zterrain.begin(), m_zterrain.end(), d_zterrain.begin());
969 auto dx = geom.CellSizeArray();
970 auto ProbLoArr = geom.ProbLoArray();
972 int ilo = geom.Domain().smallEnd(0);
973 int jlo = geom.Domain().smallEnd(1);
974 int klo = geom.Domain().smallEnd(2);
975 int ihi = geom.Domain().bigEnd(0) + 1;
976 int jhi = geom.Domain().bigEnd(1) + 1;
978 amrex::Box zbx = terrain_fab.box();
979 amrex::Array4<amrex::Real>
const& z_arr = terrain_fab.array();
984 int ii = amrex::min(amrex::max(i,ilo),ihi);
985 int jj = amrex::min(amrex::max(j,jlo),jhi);
991 int ind11, ind12, ind21, ind22;
994 int iindex_terrain=-1;
995 int jindex_terrain=-1;
1005 bool jfound =
false;
1006 for (
int it = 0; it <
ny && !jfound; it++) {
1008 jindex_terrain = it-1; jfound =
true;
1012 jindex_terrain =
ny-1;
1015 jindex_terrain = amrex::min(amrex::max(jindex_terrain,0),
ny-2);
1017 int gstart = (jindex_terrain )*
nx;
1018 int gend = (jindex_terrain+1)*
nx-1;
1019 bool ifound =
false;
1020 for (
int it = gstart; it <= gend && !ifound; it++) {
1022 iindex_terrain = it-gstart-1; ifound =
true;
1026 iindex_terrain =
nx-1;
1029 iindex_terrain = amrex::min(amrex::max(iindex_terrain,0),
nx-2);
1032 ind11 = jindex_terrain*
nx + iindex_terrain;
1039 y1 = d_yt[jindex_terrain];
1040 y2 = d_yt[jindex_terrain+1];
1046 z_arr(i,j,
klo) = d_zt[ind11];
1054 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];
1059 bool jfound =
false;
1060 for (
int it = 0; it <
ny && !jfound; it++) {
1062 jindex_terrain = it-1; jfound =
true;
1067 jindex_terrain =
ny-1;
1069 jindex_terrain = amrex::max(jindex_terrain,0);
1071 bool ifound =
false;
1072 for (
int it = 0; it <
nx && !ifound; it++) {
1074 iindex_terrain = it-1; ifound =
true;
1079 iindex_terrain =
nx-1;
1081 iindex_terrain = amrex::max(iindex_terrain,0);
1085 int ip1 = amrex::min(iindex_terrain+1,
nx-1);
1086 int jp1 = amrex::min(jindex_terrain+1,
ny-1);
1089 x1 = d_xt[iindex_terrain];
1091 y1 = d_yt[jindex_terrain];
1098 ind11 = iindex_terrain *
ny + jindex_terrain;
1099 ind21 = ip1 *
ny + jindex_terrain;
1101 ind12 = iindex_terrain *
ny + jp1;
1102 ind22 = ip1 *
ny + jp1;
1108 ind11 = jindex_terrain *
nx + iindex_terrain;
1109 ind12 = jp1 *
nx + iindex_terrain;
1111 ind21 = jindex_terrain *
nx + ip1;
1112 ind22 = jp1 *
nx + ip1;
1116 bool interp_x = (ip1 != iindex_terrain) && (x2 != x1);
1117 bool interp_y = (jp1 != jindex_terrain) && (y2 != y1);
1119 if (!interp_x && !interp_y)
1121 z_arr(i,j,
klo) = d_zt[ind11];
1123 else if (interp_x && !interp_y)
1128 z_arr(i,j,
klo) = (w_11*d_zt[ind11] + w_21*d_zt[ind21])/denom;
1130 else if (!interp_x && interp_y)
1135 z_arr(i,j,
klo) = (w_11*d_zt[ind11] + w_12*d_zt[ind12])/denom;
1144 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;
1161 amrex::FArrayBox& terrain_fab,
1164 std::string custom_terrain_type =
"None";
1165 amrex::ParmParse
pp_prob(
"prob");
pp_prob.queryAdd(
"custom_terrain_type",custom_terrain_type);
1167 if (custom_terrain_type !=
"None")
1169 amrex::Print() <<
"Calling custom terrain initialization" << std::endl;
1173 amrex::Print() <<
"Initializing flat terrain" << std::endl;
1174 terrain_fab.template setVal<amrex::RunOn::Device>(0);
1178 amrex::Print() <<
"Resetting mesh type to StretchedDz" << std::endl;
1183 #ifdef ERF_USE_TERRAIN_VELOCITY
1184 virtual amrex::Real compute_terrain_velocity (
const double )
1186 amrex::Error(
"Should never call compute_terrain_velocity for "+
name()+
" problem");
1199 amrex::Geometry
const& ,
1200 std::unique_ptr<amrex::MultiFab>& ,
1220 std::string prob_name;
1221 amrex::ParmParse
pp(
"erf");
1222 pp.get(
"prob_name",prob_name);
return prob_name;
1232 const amrex_real*
probhi) AMREX_ATTRIBUTE_WEAK;
@ wbar
Definition: ERF_DataStruct.H:194
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:37
constexpr amrex::Real zero
Definition: ERF_NumericalConstants.H:36
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
void init_terrain_surface(const amrex::Geometry &geom, amrex::FArrayBox &terrain_fab, const double &time)
Definition: ERF_ProbCommon.H:366
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 > &, const amrex::Vector< amrex::Real > &)
Definition: ERF_ProbCommon.H:304
virtual void update_rhotheta_sources(const double &, amrex::MultiFab *src, const amrex::Geometry &, std::unique_ptr< amrex::MultiFab > &, const amrex::Vector< amrex::Real > &)
Definition: ERF_ProbCommon.H:196
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:407
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:1160
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_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 > &, const amrex::Vector< amrex::Real > &)
Definition: ERF_ProbCommon.H:267
void init_buildings_surface(const amrex::Geometry &geom, amrex::FArrayBox &buildings_fab, const double &time)
Definition: ERF_ProbCommon.H:341
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:854
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:1198
void init_base_parms(amrex::Real rho_0, amrex::Real T_0)
Definition: ERF_ProbCommon.H:1214
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:1219
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:1208
virtual void update_rhoqt_sources(const double &, amrex::MultiFab *qsrc, const amrex::Geometry &, std::unique_ptr< amrex::MultiFab > &, const amrex::Vector< amrex::Real > &)
Definition: ERF_ProbCommon.H:230
@ T
Definition: ERF_IndexDefines.H:131
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
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
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:685
static MeshType mesh_type
Vertical mesh representation.
Definition: ERF_DataStruct.H:2236
static void set_mesh_type(MeshType new_mesh_type)
Override the globally selected mesh type.
Definition: ERF_DataStruct.H:2243