13#include "AMReX_MultiFabUtil.H"
15#ifdef AMR_WIND_USE_W2A
17int evaluate_read_resize(
24 const amrex::Real wtinit,
25 const amrex::Real wdt,
26 const amrex::Real newtime)
31 if (new_ntime != ntime) {
35 if (new_ntime > ntime + 1) {
42 wtime = new_ntime * wdt + wtinit;
44 if (double_data == 1 && std::abs(wtime - newtime) <= 1e-10) {
50 if (t_last < -1e-10) {
57 if (std::abs(wtime - newtime) > 1e-10) {
65 }
else if (std::abs(t_last) < 1e-10) {
80void postprocess_velocity_mfab_liquid(
81 amrex::MultiFab& vel_mfab,
82 amrex::MultiFab& lvs_mfab,
83 const amrex::GpuArray<amrex::Real, AMREX_SPACEDIM> dx)
85 auto vel = vel_mfab.arrays();
86 const auto phi = lvs_mfab.const_arrays();
88 vel_mfab, amrex::IntVect(3),
89 [=] AMREX_GPU_DEVICE(
int nbx,
int i,
int j,
int k)
noexcept {
91 const amrex::Real cell_length_2D =
92 std::sqrt(dx[0] * dx[0] + dx[2] * dx[2]);
93 if (phi[nbx](i, j, k) + cell_length_2D >= 0) {
94 vel[nbx](i, j, k, 0) = 0.0;
95 vel[nbx](i, j, k, 1) = 0.0;
96 vel[nbx](i, j, k, 2) = 0.0;
101void postprocess_velocity_field_liquid(
104 amrex::Vector<amrex::Geometry>& geom_all)
107 for (
int lev = 0; lev < nlevels; ++lev) {
108 const auto& dx_lev = geom_all[lev].CellSizeArray();
109 postprocess_velocity_mfab_liquid(
110 vel_field(lev), lvs_field(lev), dx_lev);
114int update_offset_timestep(
const int ntime,
const int n0)
120 return (-ntime + n0);
123void populate_fields_all_levels(
125 amrex::Vector<amrex::Geometry>& geom_all,
132 bool no_EOF = wdata.rmodes.get_data(
140 amrex::Print() <<
"WARNING (waves2amr_ops): end of mode data file "
141 "detected, resetting to beginning of mode data.\n";
143 no_EOF = wdata.rmodes.get_data(
145 wdata.
mZ, wdata.
mFS);
149 "waves2amr_ops: end of mode data file detected after "
150 "resetting to beginning; please evaluate HOS_init_time "
151 "or HOS_init_timestep and check the length of the mode "
157 modes_hosgrid::copy_complex(wdata.
n0, wdata.
n1, wdata.
mFS, wdata.eta_mptr);
158 modes_hosgrid::populate_hos_eta(
159 wdata.rmodes, wdata.plan, wdata.eta_mptr, wdata.
sp_eta_vec);
161 for (
int iht = 0; iht < wdata.
indvec.size(); ++iht) {
163 amrex::Real ht = wdata.
hvec[wdata.
indvec[iht]];
165 modes_hosgrid::populate_hos_vel(
166 wdata.rmodes, ht, wdata.
mX, wdata.
mY, wdata.
mZ, wdata.plan,
167 wdata.u_mptr, wdata.v_mptr, wdata.w_mptr, wdata.
sp_u_vec,
172 interp_to_mfab::interp_eta_to_levelset_field(
175 interp_to_mfab::interp_velocity_to_field(
181 postprocess_velocity_field_liquid(vel_field, lvs_field, geom_all);
195 const ::amr_wind::utils::MultiParser& pp)
198#ifndef AMR_WIND_USE_W2A
201 "ocean_waves/W2AWaves: AMR-Wind was not built with Waves2AMR "
202 "support; associated wave data cannot be processed for relaxation "
205 amrex::ignore_unused(data, pp);
207 auto& wdata = data.
meta();
208 auto& info = data.
info();
211 pp.get(
"HOS_modes_filename", wdata.
modes_file);
212 pp.query(
"HOS_init_timestep", wdata.
ntime);
213 if (!pp.contains(
"HOS_init_timestep")) {
214 pp.query(
"HOS_init_time", wdata.
t_winit);
218 std::string fftw_planner_flag{
"estimate"};
219 pp.query(
"fftw_planner_flag", fftw_planner_flag);
221 amrex::Vector<amrex::Real> prob_lo_input(AMREX_SPACEDIM);
222 amrex::ParmParse pp_geom(
"geometry");
223 pp_geom.getarr(
"prob_lo", prob_lo_input);
228 amrex::Real dz0 = 0.;
229 pp.get(
"number_interp_points_in_z", nheights);
230 pp.get(
"interp_spacing_at_surface", dz0);
231 pp.query(
"number_interp_above_surface", nh_above);
234 bool file_exists = wdata.rmodes.initialize(wdata.
modes_file);
239 "Waves2AMR ReadInputsOp: modes file requested does not exist");
243 wdata.
dt_modes = wdata.rmodes.get_dtout();
262 int vsize = wdata.rmodes.get_vector_size();
263 double initval = 0.0;
264 wdata.
mX.resize(vsize, initval);
265 wdata.
mY.resize(vsize, initval);
266 wdata.
mZ.resize(vsize, initval);
267 wdata.
mFS.resize(vsize, initval);
270 wdata.
n0 = wdata.rmodes.get_first_dimension();
271 wdata.
n1 = wdata.rmodes.get_second_dimension();
273 wdata.
dx0 = wdata.rmodes.get_xlen() / wdata.
n0;
274 wdata.
dx1 = wdata.rmodes.get_ylen() / wdata.
n1;
276 const amrex::Real depth = wdata.rmodes.get_depth();
278 wdata.
dimL = wdata.rmodes.get_L();
281 (int)((wdata.rmodes.get_Tstop() + 1e-8) / wdata.
dt_modes);
289 <<
"WARNING (waves2amr_ops): available mode data exceeded, "
290 "resetting to beginning of mode data.\n";
294 if (std::abs(depth - (wdata.
zsl - prob_lo_input[2])) > 1e-3 * depth) {
296 <<
"WARNING: Mismatch between water depths from AMR-Wind "
297 "domain and HOS data interpreted by Waves2AMR";
301 wdata.eta_mptr = modes_hosgrid::allocate_complex(wdata.
n0, wdata.
n1);
302 wdata.u_mptr = modes_hosgrid::allocate_complex(wdata.
n0, wdata.
n1);
303 wdata.v_mptr = modes_hosgrid::allocate_complex(wdata.
n0, wdata.
n1);
304 wdata.w_mptr = modes_hosgrid::allocate_complex(wdata.
n0, wdata.
n1);
307 auto plan_f = modes_hosgrid::planner_flags::estimate;
308 if (fftw_planner_flag ==
"patient") {
309 plan_f = modes_hosgrid::planner_flags::patient;
310 }
else if (fftw_planner_flag ==
"exhaustive") {
311 plan_f = modes_hosgrid::planner_flags::exhaustive;
312 }
else if (fftw_planner_flag ==
"measure") {
313 plan_f = modes_hosgrid::planner_flags::measure;
314 }
else if (!(fftw_planner_flag ==
"estimate")) {
316 <<
"WARNING (waves2amr_ops): invalid fftw_planner_flag "
317 "specified; defaulting to estimate (FFTW_ESTIMATE).\n";
320 wdata.plan = modes_hosgrid::plan_ifftw(
321 wdata.
n0, wdata.
n1, wdata.eta_mptr, plan_f);
325 int flag = interp_to_mfab::create_height_vector(
326 wdata.
hvec, nheights, dz0, wdata.
zsl, prob_lo_input[2], nh_above);
330 "Waves2AMR ReadInputsOp: create_height_vector error, failure "
332 std::to_string(flag));
338 bool no_EOF = wdata.rmodes.get_data(
347 <<
"WARNING (waves2amr_ops): end of mode data file "
348 "detected, resetting to beginning of mode data.\n";
350 no_EOF = wdata.rmodes.get_data(
356 "waves2amr_ops: end of mode data file detected after "
357 "resetting to beginning; please evaluate HOS_init_time "
358 "or HOS_init_timestep and check the length of the mode "
364 modes_hosgrid::copy_complex(
365 wdata.
n0, wdata.
n1, wdata.
mFS, wdata.eta_mptr);
367 static_cast<size_t>(wdata.
n0) *
static_cast<size_t>(wdata.
n1),
369 modes_hosgrid::populate_hos_eta(
370 wdata.rmodes, wdata.plan, wdata.eta_mptr, wdata.
sp_eta_vec);
372 const auto n_hts = wdata.
hvec.size();
374 static_cast<size_t>(wdata.
n0 * wdata.
n1) * n_hts);
376 static_cast<size_t>(wdata.
n0 * wdata.
n1) * n_hts);
378 static_cast<size_t>(wdata.
n0 * wdata.
n1) * n_hts);
379 for (
int iht = 0; iht < static_cast<int>(n_hts); ++iht) {
381 amrex::Real ht = wdata.
hvec[iht];
383 modes_hosgrid::populate_hos_vel(
384 wdata.rmodes, ht, wdata.
mX, wdata.
mY, wdata.
mZ, wdata.plan,
385 wdata.u_mptr, wdata.v_mptr, wdata.w_mptr, wdata.
sp_u_vec,
394 "w2a_velocity", AMREX_SPACEDIM, 3, 1);
398 w2a_velocity.set_default_fillpatch_bc(data.
sim().
time());
412#ifdef AMR_WIND_USE_W2A
413 auto& wdata = data.
meta();
415 auto& sim = data.
sim();
423 const auto& problo = geom.ProbLoArray();
424 const auto& probhi = geom.ProbHiArray();
425 const auto& dx = geom.CellSizeArray();
431 amrex::Vector<int> indvec;
432 indvec.resize(wdata.
hvec.size());
433 for (
int n = 0; n < indvec.size(); ++n) {
437 interp_to_mfab::interp_eta_to_levelset_multifab(
439 wdata.
sp_eta_vec, ow_levelset(level), problo, dx);
440 interp_to_mfab::interp_velocity_to_multifab(
445 postprocess_velocity_mfab_liquid(
446 ow_velocity(level), ow_levelset(level), dx);
449 const auto& ow_phi = ow_levelset(level).const_arrays();
450 const auto& ow_vel = ow_velocity(level).const_arrays();
451 const auto& phi = levelset(level).arrays();
452 const auto& vel = velocity(level).arrays();
454 const amrex::Real gen_length = wdata.
gen_length;
456 const amrex::Real zero_sea_level = wdata.
zsl;
462 velocity(level), amrex::IntVect(3),
463 [=] AMREX_GPU_DEVICE(
int nbx,
int i,
int j,
int k)
noexcept {
464 const amrex::Real x = problo[0] + (i + 0.5) * dx[0];
465 const amrex::Real z = problo[2] + (k + 0.5) * dx[2];
469 ow_vel[nbx](i, j, k, 0), ow_vel[nbx](i, j, k, 1),
470 ow_vel[nbx](i, j, k, 2), ow_phi[nbx](i, j, k) + z};
475 const auto bulk = init_wave_field ? wave_sol : quiescent;
476 const auto outlet = has_beach ? quiescent : wave_sol;
479 x, problo[0], gen_length, probhi[0], beach_length, wave_sol,
482 phi[nbx](i, j, k) = local_profile[3] - z;
483 const amrex::Real cell_length_2D =
484 std::sqrt(dx[0] * dx[0] + dx[2] * dx[2]);
485 if (phi[nbx](i, j, k) + cell_length_2D >= 0) {
486 vel[nbx](i, j, k, 0) = local_profile[0];
487 vel[nbx](i, j, k, 1) = local_profile[1];
488 vel[nbx](i, j, k, 2) = local_profile[2];
493 auto& w2a_levelset = sim.repo().get_field(
"w2a_levelset");
494 auto& w2a_velocity = sim.repo().get_field(
"w2a_velocity");
495 w2a_levelset.setVal(0.0);
496 w2a_velocity.setVal(0.0);
498 amrex::ignore_unused(data, level, geom);
510#ifdef AMR_WIND_USE_W2A
511 auto& wdata = data.
meta();
512 auto& sim = data.
sim();
518 auto& m_ow_levelset = sim.repo().get_field(
"ow_levelset");
519 auto& m_ow_velocity = sim.repo().get_field(
"ow_velocity");
521 auto& w2a_levelset = sim.repo().get_field(
"w2a_levelset");
522 auto& w2a_velocity = sim.repo().get_field(
"w2a_velocity");
524 auto nlevels = sim.repo().num_active_levels();
525 auto geom = sim.mesh().Geom();
528 amrex::Real t_last = wdata.
t_last;
531 bool read_flag =
false;
534 wdata.rmodes.time2step(newtime + wdata.
t_winit, wdata.
ntime);
535 int double_data = evaluate_read_resize(
547 <<
"WARNING (waves2amr_ops): available mode data exceeded, "
548 "resetting to beginning of mode data.\n";
566 bool flag_xlo =
false;
567 bool flag_xhi =
false;
570 (interp_to_mfab::get_local_height_indices(
571 wdata.
indvec, wdata.
hvec, m_ow_velocity.vec_ptrs(),
578 (interp_to_mfab::check_lateral_overlap_lo(
579 wdata.
gen_length, dir, m_ow_velocity.vec_ptrs(),
585 (interp_to_mfab::check_lateral_overlap_hi(
590 if (flag_z && (flag_xlo || flag_xhi)) {
595 static_cast<size_t>(wdata.
n0) *
596 static_cast<size_t>(wdata.
n1),
599 static_cast<size_t>(wdata.
n0 * wdata.
n1) *
602 static_cast<size_t>(wdata.
n0 * wdata.
n1) *
605 static_cast<size_t>(wdata.
n0 * wdata.
n1) *
616 amrex::ParallelDescriptor::ReduceIntMax(wdata.
n_offset);
620 if (double_data == 1) {
622 populate_fields_all_levels(
623 wdata, geom, m_ow_levelset, m_ow_velocity, -1);
628 for (
int lev = nlevels - 1; lev > 0; --lev) {
630 m_ow_velocity(lev), m_ow_velocity(lev - 1), 0,
631 AMREX_SPACEDIM, sim.mesh().refRatio(lev - 1));
633 m_ow_levelset(lev), m_ow_levelset(lev - 1), 0, 1,
634 sim.mesh().refRatio(lev - 1));
637 m_ow_velocity.fillpatch(sim.time().new_time());
638 m_ow_levelset.fillpatch(sim.time().new_time());
642 }
else if (double_data == 2) {
645 m_ow_levelset.setVal(0.0);
646 m_ow_velocity.setVal(0.0);
652 populate_fields_all_levels(
653 wdata, geom, w2a_levelset, w2a_velocity);
658 for (
int lev = nlevels - 1; lev > 0; --lev) {
660 w2a_velocity(lev), w2a_velocity(lev - 1), 0, AMREX_SPACEDIM,
661 sim.mesh().refRatio(lev - 1));
663 w2a_levelset(lev), w2a_levelset(lev - 1), 0, 1,
664 sim.mesh().refRatio(lev - 1));
667 w2a_velocity.fillpatch(sim.time().new_time());
668 w2a_levelset.fillpatch(sim.time().new_time());
672 for (
int lev = 0; lev < nlevels; ++lev) {
673 auto phi = m_ow_levelset(lev).arrays();
674 auto vel = m_ow_velocity(lev).arrays();
675 auto W2A_phi = w2a_levelset(lev).arrays();
676 auto W2A_vel = w2a_velocity(lev).arrays();
678 const amrex::Real W2A_t = wdata.
t;
680 m_ow_levelset(lev), amrex::IntVect(3),
681 [=] AMREX_GPU_DEVICE(
int nbx,
int i,
int j,
int k)
noexcept {
684 (W2A_phi[nbx](i, j, k) - phi[nbx](i, j, k)) *
685 (newtime - t_last) / (W2A_t - t_last + 1e-16);
686 vel[nbx](i, j, k, 0) +=
687 (W2A_vel[nbx](i, j, k, 0) - vel[nbx](i, j, k, 0)) *
688 (newtime - t_last) / (W2A_t - t_last + 1e-16);
689 vel[nbx](i, j, k, 1) +=
690 (W2A_vel[nbx](i, j, k, 1) - vel[nbx](i, j, k, 1)) *
691 (newtime - t_last) / (W2A_t - t_last + 1e-16);
692 vel[nbx](i, j, k, 2) +=
693 (W2A_vel[nbx](i, j, k, 2) - vel[nbx](i, j, k, 2)) *
694 (newtime - t_last) / (W2A_t - t_last + 1e-16);
698 amrex::ignore_unused(data);
FieldRepo & repo()
Return the field repository.
Definition CFDSim.H:62
SimTime & time()
Return simulation time control.
Definition CFDSim.H:58
FieldRepo & repo() const
FieldRepo instance that manages this field.
Definition Field.H:159
void set_default_fillpatch_bc(const SimTime &time, const amrex::BCType::mathematicalBndryTypes bctype=amrex::BCType::hoextrap) noexcept
Definition Field.cpp:377
amrex::Vector< amrex::MultiFab * > vec_ptrs() noexcept
Return a vector of MultiFab pointers for all levels.
Definition Field.cpp:142
Field & declare_field(const std::string &name, const int ncomp=1, const int ngrow=0, const int nstates=1, const FieldLoc floc=FieldLoc::CELL)
Definition FieldRepo.cpp:83
Field & get_field(const std::string &name, const FieldState fstate=FieldState::New) const
Definition FieldRepo.cpp:149
int num_active_levels() const noexcept
Total number of levels currently active in the AMR mesh.
Definition FieldRepo.H:361
AMREX_FORCE_INLINE int time_index() const
Definition SimTime.H:114
AMREX_FORCE_INLINE amrex::Real new_time() const
Definition SimTime.H:108
Definition OceanWavesTypes.H:59
OceanWavesTrait::MetaType & meta()
Definition OceanWavesTypes.H:89
CFDSim & sim()
Definition OceanWavesTypes.H:83
OceanWavesTrait::InfoType & info()
Definition OceanWavesTypes.H:86
Definition OceanWavesOps.H:8
void read_inputs(RelaxZonesBaseData &wdata, OceanWavesInfo &, const ::amr_wind::utils::MultiParser &pp)
Definition relaxation_zones_ops.cpp:15
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE WaveVec harmonize_profiles_1d(const amrex::Real x, const amrex::Real left_bdy, const amrex::Real left_length, const amrex::Real right_bdy, const amrex::Real right_length, const WaveVec left, const WaveVec bulk, const WaveVec right)
Definition wave_utils_K.H:67
amrex::GpuArray< amrex::Real, 4 > WaveVec
Definition wave_utils_K.H:9
bool has_beach
Definition RelaxationZones.H:34
bool has_outprofile
Definition RelaxationZones.H:35
amrex::Real beach_length
Definition RelaxationZones.H:27
bool init_wave_field
Definition RelaxationZones.H:30
amrex::Real gen_length
Definition RelaxationZones.H:25
bool regrid_occurred
Definition RelaxationZones.H:39
amrex::Real zsl
Definition RelaxationZones.H:17
amrex::Real dimL
Definition W2AWaves.H:24
amrex::Real dt_modes
Definition W2AWaves.H:26
amrex::Gpu::DeviceVector< amrex::Real > sp_v_vec
Definition W2AWaves.H:69
std::vector< std::complex< double > > mFS
Definition W2AWaves.H:47
amrex::Vector< int > indvec
Definition W2AWaves.H:63
int n1
Definition W2AWaves.H:19
amrex::Gpu::DeviceVector< amrex::Real > sp_u_vec
Definition W2AWaves.H:69
int n_winit
Definition W2AWaves.H:34
amrex::Vector< amrex::Real > hvec
Definition W2AWaves.H:61
int n_offset
Definition W2AWaves.H:38
amrex::Real dx0
Definition W2AWaves.H:21
int ntime
Definition W2AWaves.H:32
std::vector< std::complex< double > > mX
Definition W2AWaves.H:47
amrex::Real t_last
Definition W2AWaves.H:42
amrex::Gpu::DeviceVector< amrex::Real > sp_w_vec
Definition W2AWaves.H:70
int n0
Definition W2AWaves.H:18
std::vector< std::complex< double > > mY
Definition W2AWaves.H:47
amrex::Real dx1
Definition W2AWaves.H:22
amrex::Real t_winit
Definition W2AWaves.H:30
int n_wstop
Definition W2AWaves.H:36
std::string modes_file
Definition W2AWaves.H:16
std::vector< std::complex< double > > mZ
Definition W2AWaves.H:47
amrex::Gpu::DeviceVector< amrex::Real > sp_eta_vec
Definition W2AWaves.H:69
bool do_interp
Definition W2AWaves.H:65
bool resize_flag
Definition W2AWaves.H:67
amrex::Real t
Definition W2AWaves.H:44
void operator()(W2AWaves::DataType &data, int level, const amrex::Geometry &geom)
Definition waves2amr_ops.H:408
Definition OceanWavesOps.H:32
void operator()(W2AWaves::DataType &data)
Definition waves2amr_ops.H:507
Definition OceanWavesOps.H:35