/home/runner/work/amr-wind/amr-wind/amr-wind/ocean_waves/relaxation_zones/waves2amr_ops.H Source File

AMR-Wind API: /home/runner/work/amr-wind/amr-wind/amr-wind/ocean_waves/relaxation_zones/waves2amr_ops.H Source File
AMR-Wind API v0.1.0
CFD solver for wind plant simulations
Loading...
Searching...
No Matches
waves2amr_ops.H
Go to the documentation of this file.
1#ifndef W2A_WAVES_OPS_H
2#define W2A_WAVES_OPS_H
3
13#include "AMReX_MultiFabUtil.H"
14
15#ifdef AMR_WIND_USE_W2A
16namespace {
17int evaluate_read_resize(
18 int& ntime,
19 bool& read_flag,
20 bool& resize_flag,
21 amrex::Real& wtime,
22 amrex::Real& t_last,
23 const int new_ntime,
24 const amrex::Real wtinit,
25 const amrex::Real wdt,
26 const amrex::Real newtime)
27{
28 // Flag to indicate that data must be read twice for interpolation
29 int double_data = 0;
30 // Check if time indicates reading must take place
31 if (new_ntime != ntime) {
32 // New data is needed - reading should happen
33 read_flag = true;
34 // If time index has proceeded more than 1 step
35 if (new_ntime > ntime + 1) {
36 // Double reading is necessary
37 double_data = 1;
38 }
39 // Time index for reading
40 ntime = new_ntime;
41 // Sim time to go with recorded data
42 wtime = new_ntime * wdt + wtinit;
43 // If double reading is deemed necessary, check for convenience
44 if (double_data == 1 && std::abs(wtime - newtime) <= 1e-10) {
45 // Reading can be done just once, w2a fields replace ow fields
46 double_data = 2;
47 }
48 }
49 // Check if reading must take place for other reasons
50 if (t_last < -1e-10) {
51 // Signifies initialization from scratch without waves or a restart
52 read_flag = true;
53 // Resizing needs to happen for the first time
54 resize_flag = true;
55
56 // Confirm that new time is not coincident with modes time step
57 if (std::abs(wtime - newtime) > 1e-10) {
58 // Data must be read before and after wtime
59 double_data = 1;
60 } else {
61 // Data need only be read once, w2a fields will replace ow fields
62 double_data = 2;
63 }
64
65 } else if (std::abs(t_last) < 1e-10) {
66 // Signifies initialization with waves
67 read_flag = true;
68 // Resizing needs to happen for the first time
69 resize_flag = true;
70
71 // levelset and velocity fields are up-to-date at t=0
72 // interpolation is ready to go
73 }
74 // Record latest time as 'last' for next timestep
75 t_last = newtime;
76 // Return flag regarding double reading
77 return double_data;
78}
79
80void postprocess_velocity_mfab_liquid(
81 amrex::MultiFab& vel_mfab,
82 amrex::MultiFab& lvs_mfab,
83 const amrex::GpuArray<amrex::Real, AMREX_SPACEDIM> dx)
84{
85 auto vel = vel_mfab.arrays();
86 const auto phi = lvs_mfab.const_arrays();
87 amrex::ParallelFor(
88 vel_mfab, amrex::IntVect(3),
89 [=] AMREX_GPU_DEVICE(int nbx, int i, int j, int k) noexcept {
90 // Set velocity to zero if no liquid present
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;
97 }
98 });
99}
100
101void postprocess_velocity_field_liquid(
102 amr_wind::Field& vel_field,
103 amr_wind::Field& lvs_field,
104 amrex::Vector<amrex::Geometry>& geom_all)
105{
106 int nlevels = vel_field.repo().num_active_levels();
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);
111 }
112}
113
114int update_offset_timestep(const int ntime, const int n0)
115{
116 // Offending timestep (goes too far): ntime + offset
117 // Farthest back timesteps are permitted to go: n0
118 // Subtract offset by offending timestep, add back lower limit
119 // new offset = offset - (ntime + offset) + n0
120 return (-ntime + n0);
121}
122
123void populate_fields_all_levels(
125 amrex::Vector<amrex::Geometry>& geom_all,
126 amr_wind::Field& lvs_field,
127 amr_wind::Field& vel_field,
128 int ntime_off = 0)
129{
130
131 // Get data from modes
132 bool no_EOF = wdata.rmodes.get_data(
133 wdata.ntime + wdata.n_offset + ntime_off, wdata.mX, wdata.mY, wdata.mZ,
134 wdata.mFS);
135 // Navigate when end of file is reached
136 if (!no_EOF) {
137 // End of file detected, reset reading
138 wdata.n_offset = update_offset_timestep(wdata.ntime, wdata.n_winit);
139 // Print warning to screen
140 amrex::Print() << "WARNING (waves2amr_ops): end of mode data file "
141 "detected, resetting to beginning of mode data.\n";
142 // Read data again, now from a valid timestep
143 no_EOF = wdata.rmodes.get_data(
144 wdata.ntime + wdata.n_offset + ntime_off, wdata.mX, wdata.mY,
145 wdata.mZ, wdata.mFS);
146 // If no valid data is detected at this point, abort
147 if (!no_EOF) {
148 amrex::Abort(
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 "
152 "file.");
153 }
154 }
155
156 // Convert to spatial data in vectors
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);
160
161 for (int iht = 0; iht < wdata.indvec.size(); ++iht) {
162 // Get sample height
163 amrex::Real ht = wdata.hvec[wdata.indvec[iht]];
164 // Sample velocity
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,
168 wdata.sp_v_vec, wdata.sp_w_vec, iht * wdata.n0 * wdata.n1);
169 }
170
171 // Interpolate to fields (vector of MultiFabs)
172 interp_to_mfab::interp_eta_to_levelset_field(
173 wdata.n0, wdata.n1, wdata.dx0, wdata.dx1, wdata.zsl, wdata.sp_eta_vec,
174 lvs_field.vec_ptrs(), geom_all);
175 interp_to_mfab::interp_velocity_to_field(
176 wdata.n0, wdata.n1, wdata.dx0, wdata.dx1, wdata.indvec, wdata.hvec,
177 wdata.sp_u_vec, wdata.sp_v_vec, wdata.sp_w_vec, vel_field.vec_ptrs(),
178 geom_all);
179
180 // Zero velocity in pure gas cells
181 postprocess_velocity_field_liquid(vel_field, lvs_field, geom_all);
182}
183
184} // namespace
185#endif
186
188
189template <>
191{
193 // cppcheck-suppress constParameterReference
194 W2AWaves::DataType& data,
195 const ::amr_wind::utils::MultiParser& pp)
196 {
197// Check for W2A initialization
198#ifndef AMR_WIND_USE_W2A
199 // Assert Waves2AMR must be used for initial condition file
200 amrex::Abort(
201 "ocean_waves/W2AWaves: AMR-Wind was not built with Waves2AMR "
202 "support; associated wave data cannot be processed for relaxation "
203 "zones.");
204
205 amrex::ignore_unused(data, pp);
206#else
207 auto& wdata = data.meta();
208 auto& info = data.info();
209 relaxation_zones::read_inputs(wdata, info, pp);
210
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);
215 }
216
217 // Default fftw_plan is deterministic
218 std::string fftw_planner_flag{"estimate"};
219 pp.query("fftw_planner_flag", fftw_planner_flag);
220
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);
224
225 // Read user inputs to height vector
226 int nheights = 0;
227 int nh_above = 1;
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);
232
233 // Initialize mode reader
234 bool file_exists = wdata.rmodes.initialize(wdata.modes_file);
235
236 // Abort if file does not exist
237 if (!file_exists) {
238 amrex::Abort(
239 "Waves2AMR ReadInputsOp: modes file requested does not exist");
240 }
241
242 // Get dt of HOS data
243 wdata.dt_modes = wdata.rmodes.get_dtout();
244
245 // Get initial time and timestep synced
246 if (wdata.t_winit > 0.0) {
247 // If initial time was specified
248 // Get time index near requested time
249 wdata.ntime = wdata.rmodes.time2step(wdata.t_winit, wdata.ntime);
250 // Sync time to time index
251 wdata.t_winit = wdata.dt_modes * wdata.ntime;
252 // Save first timestep
253 wdata.n_winit = wdata.ntime;
254 } else {
255 // If initial timestep is being used
256 wdata.t_winit = wdata.dt_modes * wdata.ntime;
257 // Save first timestep
258 wdata.n_winit = wdata.ntime;
259 }
260
261 // Initialize variables to store modes
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);
268
269 // Get dimensions of data
270 wdata.n0 = wdata.rmodes.get_first_dimension();
271 wdata.n1 = wdata.rmodes.get_second_dimension();
272 // Get resolution
273 wdata.dx0 = wdata.rmodes.get_xlen() / wdata.n0;
274 wdata.dx1 = wdata.rmodes.get_ylen() / wdata.n1;
275 // Get depth
276 const amrex::Real depth = wdata.rmodes.get_depth();
277 // Get dimensional length
278 wdata.dimL = wdata.rmodes.get_L();
279 // Get nominal last timestep of data
280 wdata.n_wstop =
281 (int)((wdata.rmodes.get_Tstop() + 1e-8) / wdata.dt_modes);
282
283 // Check if stop time is exceeded, introduce offset to ntime
284 if (wdata.ntime + wdata.n_offset > wdata.n_wstop) {
285 // If exceeding stop step, calculate new offset
286 wdata.n_offset = update_offset_timestep(wdata.ntime, wdata.n_winit);
287 // Print warning to screen
288 amrex::Print()
289 << "WARNING (waves2amr_ops): available mode data exceeded, "
290 "resetting to beginning of mode data.\n";
291 }
292
293 // Warning if depth does not correspond to simulation
294 if (std::abs(depth - (wdata.zsl - prob_lo_input[2])) > 1e-3 * depth) {
295 amrex::Print()
296 << "WARNING: Mismatch between water depths from AMR-Wind "
297 "domain and HOS data interpreted by Waves2AMR";
298 }
299
300 // Allocate pointers for FFTW
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);
305
306 // Set up planner flag based on input
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")) {
315 amrex::Print()
316 << "WARNING (waves2amr_ops): invalid fftw_planner_flag "
317 "specified; defaulting to estimate (FFTW_ESTIMATE).\n";
318 }
319 // Set up plan for FFTW
320 wdata.plan = modes_hosgrid::plan_ifftw(
321 wdata.n0, wdata.n1, wdata.eta_mptr, plan_f);
322
323 // Create height vector for velocity mode conversion before
324 // interpolation, with prob_lo as bottom
325 int flag = interp_to_mfab::create_height_vector(
326 wdata.hvec, nheights, dz0, wdata.zsl, prob_lo_input[2], nh_above);
327 // Fail if flag indicates it should
328 if (flag > 0) {
329 amrex::Abort(
330 "Waves2AMR ReadInputsOp: create_height_vector error, failure "
331 "code " +
332 std::to_string(flag));
333 }
334
335 // If init_wave_field is activated and initialization will be done, get
336 // modes on every processor
337 if (wdata.init_wave_field && data.sim().time().time_index() == 0) {
338 bool no_EOF = wdata.rmodes.get_data(
339 wdata.ntime + wdata.n_offset, wdata.mX, wdata.mY, wdata.mZ,
340 wdata.mFS);
341 if (!no_EOF) {
342 // End of file detected, reset reading
343 wdata.n_offset =
344 update_offset_timestep(wdata.ntime, wdata.n_winit);
345 // Print warning to screen
346 amrex::Print()
347 << "WARNING (waves2amr_ops): end of mode data file "
348 "detected, resetting to beginning of mode data.\n";
349 // Read data again, now from a valid timestep
350 no_EOF = wdata.rmodes.get_data(
351 wdata.ntime + wdata.n_offset, wdata.mX, wdata.mY, wdata.mZ,
352 wdata.mFS);
353 // If no valid data is detected at this point, abort
354 if (!no_EOF) {
355 amrex::Abort(
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 "
359 "file.");
360 }
361 }
362
363 // Convert modes to spatial data
364 modes_hosgrid::copy_complex(
365 wdata.n0, wdata.n1, wdata.mFS, wdata.eta_mptr);
366 wdata.sp_eta_vec.resize(
367 static_cast<size_t>(wdata.n0) * static_cast<size_t>(wdata.n1),
368 0.0);
369 modes_hosgrid::populate_hos_eta(
370 wdata.rmodes, wdata.plan, wdata.eta_mptr, wdata.sp_eta_vec);
371 // Mesh is not yet created, so get data at every height
372 const auto n_hts = wdata.hvec.size();
373 wdata.sp_u_vec.resize(
374 static_cast<size_t>(wdata.n0 * wdata.n1) * n_hts);
375 wdata.sp_v_vec.resize(
376 static_cast<size_t>(wdata.n0 * wdata.n1) * n_hts);
377 wdata.sp_w_vec.resize(
378 static_cast<size_t>(wdata.n0 * wdata.n1) * n_hts);
379 for (int iht = 0; iht < static_cast<int>(n_hts); ++iht) {
380 // Get sample height
381 amrex::Real ht = wdata.hvec[iht];
382 // Sample velocity
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,
386 wdata.sp_v_vec, wdata.sp_w_vec, iht * wdata.n0 * wdata.n1);
387 }
388 }
389
390 // Declare fields for HOS
391 auto& w2a_levelset =
392 data.sim().repo().declare_field("w2a_levelset", 1, 3, 1);
393 auto& w2a_velocity = data.sim().repo().declare_field(
394 "w2a_velocity", AMREX_SPACEDIM, 3, 1);
395
396 // Extrapolation can work well when finer data is available
397 w2a_levelset.set_default_fillpatch_bc(data.sim().time());
398 w2a_velocity.set_default_fillpatch_bc(data.sim().time());
399#endif
400 }
401}; // namespace ops
402
403template <>
405{
406 void
407 // cppcheck-suppress constParameterReference
409 W2AWaves::DataType & data, int level, const amrex::Geometry & geom)
410 {
411
412#ifdef AMR_WIND_USE_W2A
413 auto& wdata = data.meta();
414
415 auto& sim = data.sim();
416
417 // Fill ow fields, then populate flow fields according to setup
418 auto& ow_levelset = sim.repo().get_field("ow_levelset");
419 auto& ow_velocity = sim.repo().get_field("ow_velocity");
420 auto& levelset = sim.repo().get_field("levelset");
421 auto& velocity = sim.repo().get_field("velocity");
422
423 const auto& problo = geom.ProbLoArray();
424 const auto& probhi = geom.ProbHiArray();
425 const auto& dx = geom.CellSizeArray();
426
427 // Set t_last to 0.0 to signify information read in
428 wdata.t_last = 0.0;
429
430 // indvec is complete upon initialization (all heights every proc)
431 amrex::Vector<int> indvec;
432 indvec.resize(wdata.hvec.size());
433 for (int n = 0; n < indvec.size(); ++n) {
434 indvec[n] = n;
435 }
436 // Interpolate to MultiFabs (one level at a time)
437 interp_to_mfab::interp_eta_to_levelset_multifab(
438 wdata.n0, wdata.n1, wdata.dx0, wdata.dx1, wdata.zsl,
439 wdata.sp_eta_vec, ow_levelset(level), problo, dx);
440 interp_to_mfab::interp_velocity_to_multifab(
441 wdata.n0, wdata.n1, wdata.dx0, wdata.dx1, indvec, wdata.hvec,
442 wdata.sp_u_vec, wdata.sp_v_vec, wdata.sp_w_vec, ow_velocity(level),
443 problo, dx);
444 // Zero velocity in pure air cells
445 postprocess_velocity_mfab_liquid(
446 ow_velocity(level), ow_levelset(level), dx);
447
448 // Populate flow fields according to intended forcing and init setup
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();
453
454 const amrex::Real gen_length = wdata.gen_length;
455 const amrex::Real beach_length = wdata.beach_length;
456 const amrex::Real zero_sea_level = wdata.zsl;
457
458 const bool has_beach = wdata.has_beach;
459 const bool init_wave_field = wdata.init_wave_field;
460
461 amrex::ParallelFor(
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];
466
467 // Wave profile
468 const utils::WaveVec wave_sol{
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};
471 // Quiescent profile
472 const utils::WaveVec quiescent{0.0, 0.0, 0.0, zero_sea_level};
473
474 // Specify initial state for each region of domain
475 const auto bulk = init_wave_field ? wave_sol : quiescent;
476 const auto outlet = has_beach ? quiescent : wave_sol;
477
478 const auto local_profile = utils::harmonize_profiles_1d(
479 x, problo[0], gen_length, probhi[0], beach_length, wave_sol,
480 bulk, outlet);
481
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];
489 }
490 });
491
492 // Start w2a fields at 0, some areas will not be modified
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);
497#else
498 amrex::ignore_unused(data, level, geom);
499#endif
500 }
501}; // namespace ocean_waves
502
503template <>
505{
506 // cppcheck-suppress constParameterReference
508 {
509
510#ifdef AMR_WIND_USE_W2A
511 auto& wdata = data.meta();
512 auto& sim = data.sim();
513
514 // Nudge the solution toward where it should be
515 const amrex::Real newtime = sim.time().new_time();
516
517 // Update ow fields every time
518 auto& m_ow_levelset = sim.repo().get_field("ow_levelset");
519 auto& m_ow_velocity = sim.repo().get_field("ow_velocity");
520 // Update HOS fields when necessary
521 auto& w2a_levelset = sim.repo().get_field("w2a_levelset");
522 auto& w2a_velocity = sim.repo().get_field("w2a_velocity");
523
524 auto nlevels = sim.repo().num_active_levels();
525 auto geom = sim.mesh().Geom();
526
527 // Get value for time interpolation
528 amrex::Real t_last = wdata.t_last;
529
530 // Check if new HOS data needs to be read
531 bool read_flag = false;
532 // Check if time indicates reading must take place
533 int new_ntime =
534 wdata.rmodes.time2step(newtime + wdata.t_winit, wdata.ntime);
535 int double_data = evaluate_read_resize(
536 wdata.ntime, read_flag, wdata.resize_flag, wdata.t, wdata.t_last,
537 new_ntime, wdata.t_winit, wdata.dt_modes, newtime);
538 // Check if stop time is exceeded, introduce offset to ntime
539 if (read_flag) {
540 // Need to only check when reading is occurring
541 if (wdata.ntime + wdata.n_offset > wdata.n_wstop) {
542 // If exceeding stop step, calculate new offset
543 wdata.n_offset =
544 update_offset_timestep(wdata.ntime, wdata.n_winit);
545 // Print warning to screen
546 amrex::Print()
547 << "WARNING (waves2amr_ops): available mode data exceeded, "
548 "resetting to beginning of mode data.\n";
549 }
550 }
551 // Resizing (assuming reading is taking place) must happen after regrid
552 if (wdata.regrid_occurred) {
553 // resize_flag remains true until resizing occurs, but
554 // regrid_occurred resets every timestep
555 wdata.resize_flag = true;
556 }
557
558 // Read HOS data if necessary based on time
559 if (read_flag) {
560
561 if (wdata.resize_flag) {
562 // Reset flag
563 wdata.resize_flag = false;
564 // Flags for indicating overlap, assume none at first
565 bool flag_z = false;
566 bool flag_xlo = false;
567 bool flag_xhi = false;
568 // Get heights for this processor, check overlap in z
569 flag_z =
570 (interp_to_mfab::get_local_height_indices(
571 wdata.indvec, wdata.hvec, m_ow_velocity.vec_ptrs(),
572 geom) == 1);
573 // No overlap from heights definitely means no interp
574
575 // Check lateral bounds (in x)
576 const int dir = 0;
577 flag_xlo =
578 (interp_to_mfab::check_lateral_overlap_lo(
579 wdata.gen_length, dir, m_ow_velocity.vec_ptrs(),
580 geom) == 1);
581 // No overlap with gen region means no interp, unless ...
582 if (wdata.has_outprofile) {
583 // ... if overlap exists here, needing interp
584 flag_xhi =
585 (interp_to_mfab::check_lateral_overlap_hi(
586 wdata.beach_length, dir, m_ow_velocity.vec_ptrs(),
587 geom) == 1);
588 }
589
590 if (flag_z && (flag_xlo || flag_xhi)) {
591 // Interpolation is needed
592 wdata.do_interp = true;
593 // Do resizing
594 wdata.sp_eta_vec.resize(
595 static_cast<size_t>(wdata.n0) *
596 static_cast<size_t>(wdata.n1),
597 0.0);
598 wdata.sp_u_vec.resize(
599 static_cast<size_t>(wdata.n0 * wdata.n1) *
600 wdata.indvec.size());
601 wdata.sp_v_vec.resize(
602 static_cast<size_t>(wdata.n0 * wdata.n1) *
603 wdata.indvec.size());
604 wdata.sp_w_vec.resize(
605 static_cast<size_t>(wdata.n0 * wdata.n1) *
606 wdata.indvec.size());
607 // Sizes will remain constant and need for interpolation
608 // will remain until a regrid occurs
609 } else {
610 // No overlapping with spatial data or no overlapping with
611 // relaxation zones, interpolation can be skipped
612 wdata.do_interp = false;
613 }
614 }
615 // Only perform reading where needed, communicate offset though
616 amrex::ParallelDescriptor::ReduceIntMax(wdata.n_offset);
617
618 // If double read is required, then copy older wave data to ow_
619 // fields and modify interpolation parameters to get things right
620 if (double_data == 1) {
621 if (wdata.do_interp) {
622 populate_fields_all_levels(
623 wdata, geom, m_ow_levelset, m_ow_velocity, -1);
624 }
625
626 // Average down to get fine information on coarse grid where
627 // possible (may be unnecessary)
628 for (int lev = nlevels - 1; lev > 0; --lev) {
629 amrex::average_down(
630 m_ow_velocity(lev), m_ow_velocity(lev - 1), 0,
631 AMREX_SPACEDIM, sim.mesh().refRatio(lev - 1));
632 amrex::average_down(
633 m_ow_levelset(lev), m_ow_levelset(lev - 1), 0, 1,
634 sim.mesh().refRatio(lev - 1));
635 }
636 // Fill patch to get correct ghost cells after average down
637 m_ow_velocity.fillpatch(sim.time().new_time());
638 m_ow_levelset.fillpatch(sim.time().new_time());
639
640 // Prior t_last (corresponding to ow fields)
641 t_last = (wdata.ntime - 1) * wdata.dt_modes;
642 } else if (double_data == 2) {
643 // Restarting simulation or taking a big step, new time at ntime
644 // Initialize ow fields to 0 for time interp, will be replaced
645 m_ow_levelset.setVal(0.0);
646 m_ow_velocity.setVal(0.0);
647 // No modification needed for t_last, leads to interp factor = 1
648 }
649
650 // After possible prior read, now read data for this ntime
651 if (wdata.do_interp) {
652 populate_fields_all_levels(
653 wdata, geom, w2a_levelset, w2a_velocity);
654 }
655
656 // Average down to get fine information on coarse grid where
657 // possible (may be unnecessary)
658 for (int lev = nlevels - 1; lev > 0; --lev) {
659 amrex::average_down(
660 w2a_velocity(lev), w2a_velocity(lev - 1), 0, AMREX_SPACEDIM,
661 sim.mesh().refRatio(lev - 1));
662 amrex::average_down(
663 w2a_levelset(lev), w2a_levelset(lev - 1), 0, 1,
664 sim.mesh().refRatio(lev - 1));
665 }
666 // Fill patch to get correct ghost cells after average down
667 w2a_velocity.fillpatch(sim.time().new_time());
668 w2a_levelset.fillpatch(sim.time().new_time());
669 }
670
671 // Temporally interpolate at every timestep to get target solution
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();
677
678 const amrex::Real W2A_t = wdata.t;
679 amrex::ParallelFor(
680 m_ow_levelset(lev), amrex::IntVect(3),
681 [=] AMREX_GPU_DEVICE(int nbx, int i, int j, int k) noexcept {
682 // Interpolate temporally every time
683 phi[nbx](i, j, k) +=
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);
695 });
696 }
697#else
698 amrex::ignore_unused(data);
699#endif
700 }
701};
702
703} // namespace amr_wind::ocean_waves::ops
704
705#endif /* WAVES2AMR_OPS_H */
FieldRepo & repo()
Return the field repository.
Definition CFDSim.H:62
SimTime & time()
Return simulation time control.
Definition CFDSim.H:58
Definition Field.H:116
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
Definition W2AWaves.H:14
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
Definition W2AWaves.H:95
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, const ::amr_wind::utils::MultiParser &pp)
Definition waves2amr_ops.H:192
Definition OceanWavesOps.H:19
void operator()(W2AWaves::DataType &data)
Definition waves2amr_ops.H:507