1 #ifndef ERF_PROBCOMMON_H_
2 #define ERF_PROBCOMMON_H_
4 #include <AMReX_ParmParse.H>
5 #include <AMReX_Geometry.H>
6 #include <AMReX_FArrayBox.H>
7 #include <AMReX_MultiFab.H>
16 amrex::Real
T_0 = 300.0;
41 std::unique_ptr<amrex::MultiFab>& ,
42 std::unique_ptr<amrex::MultiFab>& ,
43 amrex::Geometry
const& )
45 amrex::Print() <<
"Hydrostatically balanced density was NOT set"
46 <<
" -- an appropriate init_type should probably have been specified"
47 <<
" (e.g., input_sounding, ideal, real, or metgrid)"
49 amrex::Error(
"Should never call erf_init_dens_hse for "+
name()+
" problem");
54 std::unique_ptr<amrex::MultiFab>& ,
55 amrex::Geometry
const& )
88 amrex::Array4<amrex::Real const>
const& ,
89 amrex::Array4<amrex::Real >
const& ,
90 amrex::Array4<amrex::Real >
const& ,
91 amrex::Array4<amrex::Real >
const& ,
92 amrex::Array4<amrex::Real >
const& ,
93 amrex::Array4<amrex::Real >
const& ,
94 amrex::Array4<amrex::Real >
const& ,
95 amrex::Array4<amrex::Real const>
const& ,
96 amrex::Array4<amrex::Real const>
const& ,
97 amrex::GeometryData
const& ,
98 amrex::Array4<amrex::Real const>
const& ,
99 amrex::Array4<amrex::Real const>
const& ,
100 amrex::Array4<amrex::Real const>
const& ,
103 amrex::Print() <<
"No perturbation to background fields supplied for "
104 <<
name() <<
" problem" << std::endl;
118 amrex::Vector<amrex::Real>& src,
119 amrex::Gpu::DeviceVector<amrex::Real>& d_src,
120 const amrex::Geometry& geom,
121 std::unique_ptr<amrex::MultiFab>& z_phys_cc)
123 if (src.empty())
return;
127 amrex::Error(
"Temperature forcing not defined for "+
name()+
" problem with terrain");
129 const int khi = geom.Domain().bigEnd()[2];
132 for (
int k = 0; k <= khi; k++)
141 amrex::Gpu::copy(amrex::Gpu::hostToDevice, src.begin(), src.end(), d_src.begin());
155 amrex::Vector<amrex::Real>& qsrc,
156 amrex::Gpu::DeviceVector<amrex::Real>& d_qsrc,
157 const amrex::Geometry& geom,
158 std::unique_ptr<amrex::MultiFab>& z_phys_cc)
160 if (qsrc.empty())
return;
164 amrex::Error(
"Moisture forcing not defined for "+
name()+
" problem with terrain");
166 const int khi = geom.Domain().bigEnd()[2];
169 for (
int k = 0; k <= khi; k++)
178 amrex::Gpu::copy(amrex::Gpu::hostToDevice, qsrc.begin(), qsrc.end(), d_qsrc.begin());
195 amrex::Vector<amrex::Real>&
wbar,
196 amrex::Gpu::DeviceVector<amrex::Real>& d_wbar,
197 const amrex::Geometry& geom,
198 std::unique_ptr<amrex::MultiFab>& z_phys_cc)
200 if (
wbar.empty())
return;
204 amrex::Error(
"Moisture forcing not defined for "+
name()+
" problem with terrain");
206 const int khi = geom.Domain().bigEnd()[2];
209 for (
int k = 0; k <= khi; k++)
218 amrex::Gpu::copy(amrex::Gpu::hostToDevice,
wbar.begin(),
wbar.end(), d_wbar.begin());
232 amrex::Vector<amrex::Real>& u_geos,
233 amrex::Gpu::DeviceVector<amrex::Real>& d_u_geos,
234 amrex::Vector<amrex::Real>& v_geos,
235 amrex::Gpu::DeviceVector<amrex::Real>& d_v_geos,
236 const amrex::Geometry& geom,
237 std::unique_ptr<amrex::MultiFab>& z_phys_cc)
239 if (u_geos.empty())
return;
243 amrex::Error(
"Geostrophic wind profile not defined for "+
name()+
" problem with terrain");
245 const int khi = geom.Domain().bigEnd()[2];
248 for (
int k = 0; k <= khi; k++)
258 amrex::Gpu::copy(amrex::Gpu::hostToDevice, u_geos.begin(), u_geos.end(), d_u_geos.begin());
259 amrex::Gpu::copy(amrex::Gpu::hostToDevice, v_geos.begin(), v_geos.end(), d_v_geos.begin());
271 amrex::FArrayBox& terrain_fab,
272 const amrex::Real& time)
275 std::string fname, fname_usgs;
276 amrex::ParmParse
pp(
"erf");
277 auto valid_fname =
pp.query(
"terrain_file_name",fname);
278 auto valid_fname_USGS =
pp.query(
"terrain_file_name_USGS",fname_usgs);
286 }
else if (valid_fname_USGS) {
298 const amrex::Geometry& geom,
299 amrex::FArrayBox& terrain_fab,
302 amrex::Vector<amrex::Real> m_xterrain,m_yterrain,m_zterrain;
304 int nx = 0;
int ny = 0;
306 if (amrex::ParallelDescriptor::IOProcessor()) {
308 amrex::Print()<<
"Reading terrain file: "<< fname<< std::endl;
309 std::ifstream file(fname);
311 if (!file.is_open()) {
312 amrex::Abort(
"Error: Could not open the file " + fname+
"\n");
316 if (file.peek() == std::ifstream::traits_type::eof()) {
317 amrex::Abort(
"Error: The file " + fname+
" is empty.\n");
320 amrex::Real value1,value2,value3;
323 amrex::Real lat_min, lon_min;
325 file >> lon_min >> lat_min;
326 if(std::fabs(lon_min) > 180.0) {
327 amrex::Error(
"The value of longitude for entry in the first line in " + fname
328 +
" should not exceed 180. It is " + std::to_string(lon_min));
330 if(std::fabs(lat_min) > 90.0) {
331 amrex::Error(
"The value of latitude for entry in the first line in " + fname
332 +
" should not exceed 90. It is " + std::to_string(lat_min));
338 while (file >> value1 >> value2 >> value3) {
339 m_xterrain.push_back(value1);
341 m_yterrain.push_back(value2);
343 m_zterrain.push_back(value3);
346 AMREX_ASSERT(m_xterrain.size() ==
static_cast<long int>(nx*ny));
347 AMREX_ASSERT(m_yterrain.size() ==
static_cast<long int>(ny));
348 AMREX_ASSERT(m_zterrain.size() ==
static_cast<long int>(nx*ny));
351 file >> nx; file >> ny;
352 AMREX_ALWAYS_ASSERT(nx > 0);
353 AMREX_ALWAYS_ASSERT(ny > 0);
354 m_xterrain.resize(nx);
355 m_yterrain.resize(ny);
356 m_zterrain.resize(nx * ny);
357 for (
int n = 0; n < nx; n++) {
358 file >> m_xterrain[n];
360 for (
int n = 0; n < ny; n++) {
361 file >> m_yterrain[n];
363 for (
int n = 0; n < nx * ny; n++) {
364 file >> m_zterrain[n];
368 amrex::Print() <<
"NX " << nx <<
" " << ny << std::endl;
374 amrex::ParallelDescriptor::Bcast(&nx,1,amrex::ParallelDescriptor::IOProcessorNumber());
375 amrex::ParallelDescriptor::Bcast(&ny,1,amrex::ParallelDescriptor::IOProcessorNumber());
379 m_xterrain.resize(nx);
380 m_yterrain.resize(ny);
381 m_zterrain.resize(nz);
383 amrex::ParallelDescriptor::Bcast(m_xterrain.data(),nx,amrex::ParallelDescriptor::IOProcessorNumber());
384 amrex::ParallelDescriptor::Bcast(m_yterrain.data(),ny,amrex::ParallelDescriptor::IOProcessorNumber());
385 amrex::ParallelDescriptor::Bcast(m_zterrain.data(),nz,amrex::ParallelDescriptor::IOProcessorNumber());
388 amrex::Gpu::DeviceVector<amrex::Real> d_xterrain(nx),d_yterrain(ny),d_zterrain(nz);
389 amrex::Gpu::copy(amrex::Gpu::hostToDevice, m_xterrain.begin(), m_xterrain.end(), d_xterrain.begin());
390 amrex::Gpu::copy(amrex::Gpu::hostToDevice, m_yterrain.begin(), m_yterrain.end(), d_yterrain.begin());
391 amrex::Gpu::copy(amrex::Gpu::hostToDevice, m_zterrain.begin(), m_zterrain.end(), d_zterrain.begin());
393 amrex::Real* d_xt = d_xterrain.data();
394 amrex::Real* d_yt = d_yterrain.data();
395 amrex::Real* d_zt = d_zterrain.data();
397 auto dx = geom.CellSizeArray();
398 auto ProbLoArr = geom.ProbLoArray();
400 int ilo = geom.Domain().smallEnd(0);
401 int jlo = geom.Domain().smallEnd(1);
402 int klo = geom.Domain().smallEnd(2);
403 int ihi = geom.Domain().bigEnd(0) + 1;
404 int jhi = geom.Domain().bigEnd(1) + 1;
406 amrex::Box zbx = terrain_fab.box();
407 amrex::Array4<amrex::Real>
const& z_arr = terrain_fab.array();
409 amrex::ParallelFor(zbx, [=] AMREX_GPU_DEVICE (
int i,
int j,
int )
412 int ii = amrex::min(amrex::max(i,ilo),ihi);
413 int jj = amrex::min(amrex::max(j,jlo),jhi);
416 amrex::Real
x = ProbLoArr[0] + ii * dx[0] + 1e-3;
417 amrex::Real
y = ProbLoArr[1] + jj * dx[1] + 1e-3;
419 int jindex_terrain = -1;
420 for (
int it = 0; it < ny && jindex_terrain == -1; it++) {
422 jindex_terrain = it-1;
425 if (jindex_terrain == -1) {
426 jindex_terrain = ny-1;
429 int ind11, ind12, ind21, ind22;
430 amrex::Real x1, x2, y1, y2;
432 int iindex_terrain=-1;
434 int gstart = (jindex_terrain )*nx;
435 int gend = (jindex_terrain+1)*nx-1;
436 for (
int it = gstart; it <= gend && iindex_terrain == -1; it++) {
438 iindex_terrain = it-gstart;
443 ind11 = jindex_terrain*nx + iindex_terrain;
449 y1 = d_yt[jindex_terrain];
450 y2 = d_yt[jindex_terrain+1];
453 for (
int it = 0; it < nx && iindex_terrain == -1; it++) {
455 iindex_terrain = it-1;
458 if (iindex_terrain == -1) {
459 iindex_terrain = nx-1;
463 x1 = d_xt[iindex_terrain];
464 x2 = d_xt[iindex_terrain+1];
465 y1 = d_yt[jindex_terrain];
466 y2 = d_yt[jindex_terrain+1];
468 ind11 = jindex_terrain * nx + iindex_terrain;
469 ind21 = std::min(jindex_terrain+1,ny-1) * nx + iindex_terrain;
471 ind12 = jindex_terrain * nx + std::min(iindex_terrain+1,nx-1);
472 ind22 = std::min(jindex_terrain+1,ny-1) * nx + std::min(iindex_terrain+1,nx-1);
476 amrex::Real denom = (x2-x1)*(y2-y1);
477 amrex::Real w_11 = (x2-
x)*(y2-
y)/denom;
478 amrex::Real w_12 = (x2-
x)*(
y-y1)/denom;
479 amrex::Real w_21 = (
x-x1)*(y2-
y)/denom;
480 amrex::Real w_22 = (
x-x1)*(
y-y1)/denom;
482 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];
497 amrex::FArrayBox& terrain_fab,
500 amrex::Print() <<
"Initializing flat terrain" << std::endl;
502 terrain_fab.template setVal<amrex::RunOn::Device>(0.0);
507 #ifdef ERF_USE_TERRAIN_VELOCITY
508 virtual amrex::Real compute_terrain_velocity(
const amrex::Real )
510 amrex::Error(
"Should never call compute_terrain_velocity for "+
name()+
" problem");
523 amrex::Geometry
const& ,
524 std::unique_ptr<amrex::MultiFab>& ,
527 amrex::Error(
"Should never call erf_init_rayleigh for "+
name()+
" problem");
534 init_uniform (
const amrex::Box& bx, amrex::Array4<amrex::Real>
const& state)
538 amrex::Print() <<
"Initializing uniform fields"
539 <<
" rho=" << rho_0 <<
" theta=" << T_0
542 ParallelFor(bx, [=] AMREX_GPU_DEVICE (
int i,
int j,
int k) noexcept
563 virtual std::string
name () = 0;
572 const amrex_real* probhi) AMREX_ATTRIBUTE_WEAK;
@ wbar
Definition: ERF_DataStruct.H:70
#define Rho_comp
Definition: ERF_IndexDefines.H:36
#define RhoTheta_comp
Definition: ERF_IndexDefines.H:37
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real pp(amrex::Real y)
Definition: ERF_MicrophysicsUtils.H:219
std::unique_ptr< ProblemBase > amrex_probinit(const amrex_real *problo, const amrex_real *probhi) AMREX_ATTRIBUTE_WEAK
Definition: ERF_ProbCommon.H:23
virtual void init_custom_pert(const amrex::Box &, const amrex::Box &, const amrex::Box &, 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 &, 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 &, amrex::Array4< amrex::Real const > const &, amrex::Array4< amrex::Real const > const &, const SolverChoice &)
Definition: ERF_ProbCommon.H:84
virtual std::string name()=0
void init_uniform(const amrex::Box &bx, amrex::Array4< amrex::Real > const &state)
Definition: ERF_ProbCommon.H:534
void init_terrain_surface(const amrex::Geometry &geom, amrex::FArrayBox &terrain_fab, const amrex::Real &time)
Definition: ERF_ProbCommon.H:270
virtual void update_geostrophic_profile(const amrex::Real &, 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 > &z_phys_cc)
Definition: ERF_ProbCommon.H:231
void read_custom_terrain(const std::string &fname, const bool is_usgs, const amrex::Geometry &geom, amrex::FArrayBox &terrain_fab, const amrex::Real &)
Definition: ERF_ProbCommon.H:296
virtual void update_rhoqt_sources(const amrex::Real &, amrex::Vector< amrex::Real > &qsrc, amrex::Gpu::DeviceVector< amrex::Real > &d_qsrc, const amrex::Geometry &geom, std::unique_ptr< amrex::MultiFab > &z_phys_cc)
Definition: ERF_ProbCommon.H:154
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:522
void init_base_parms(amrex::Real rho_0, amrex::Real T_0)
Definition: ERF_ProbCommon.H:557
virtual void init_custom_terrain(const amrex::Geometry &, amrex::FArrayBox &terrain_fab, const amrex::Real &)
Definition: ERF_ProbCommon.H:496
virtual ~ProblemBase()=default
virtual void erf_init_dens_hse_moist(amrex::MultiFab &, std::unique_ptr< amrex::MultiFab > &, amrex::Geometry const &)
Definition: ERF_ProbCommon.H:53
virtual void update_rhotheta_sources(const amrex::Real &, amrex::Vector< amrex::Real > &src, amrex::Gpu::DeviceVector< amrex::Real > &d_src, const amrex::Geometry &geom, std::unique_ptr< amrex::MultiFab > &z_phys_cc)
Definition: ERF_ProbCommon.H:117
ProbParmDefaults base_parms
Definition: ERF_ProbCommon.H:551
virtual void update_w_subsidence(const amrex::Real &, amrex::Vector< amrex::Real > &wbar, amrex::Gpu::DeviceVector< amrex::Real > &d_wbar, const amrex::Geometry &geom, std::unique_ptr< amrex::MultiFab > &z_phys_cc)
Definition: ERF_ProbCommon.H:194
virtual void erf_init_dens_hse(amrex::MultiFab &, std::unique_ptr< amrex::MultiFab > &, std::unique_ptr< amrex::MultiFab > &, amrex::Geometry const &)
Definition: ERF_ProbCommon.H:40
Definition: ERF_ProbCommon.H:14
amrex::Real T_0
Definition: ERF_ProbCommon.H:16
amrex::Real rho_0
Definition: ERF_ProbCommon.H:15
Definition: ERF_DataStruct.H:82
static void set_flat_terrain_flag()
Definition: ERF_DataStruct.H:586