/home/runner/work/kynema-sgf/kynema-sgf/src/equation_systems/AdvOp_Godunov.H Source File

Kynema-SGF API: /home/runner/work/kynema-sgf/kynema-sgf/src/equation_systems/AdvOp_Godunov.H Source File
Kynema-SGF API v0.1.0
CFD solver for wind plant simulations
Loading...
Searching...
No Matches
AdvOp_Godunov.H
Go to the documentation of this file.
1#ifndef ADVOP_GODUNOV_H
2#define ADVOP_GODUNOV_H
3
4#include <type_traits>
5
11
12#include "AMReX_Gpu.H"
13#include "AMReX_ParmParse.H"
14#include "AMReX_MultiFabUtil.H"
15#include "hydro_utils.H"
16
18#include "AMReX_REAL.H"
19
20using namespace amrex::literals;
21
22namespace kynema_sgf::pde {
23
27template <typename PDE>
29 PDE,
30 fvm::Godunov,
31 typename std::enable_if_t<std::is_base_of_v<ScalarTransport, PDE>>>
32{
34 CFDSim& /* sim */,
35 PDEFields& fields_in,
36 bool /* has_overset */,
37 bool /* variable density */,
38 bool /* mesh mapping */,
39 bool /* is_anelastic */)
40 : fields(fields_in)
41 , density(fields_in.repo.get_field("density"))
42 , u_mac(fields_in.repo.get_field("u_mac"))
43 , v_mac(fields_in.repo.get_field("v_mac"))
44 , w_mac(fields_in.repo.get_field("w_mac"))
45 , m_flux_x(fields_in.repo.create_scratch_field(
46 PDE::ndim, 0, kynema_sgf::FieldLoc::XFACE))
47 , m_flux_y(fields_in.repo.create_scratch_field(
48 PDE::ndim, 0, kynema_sgf::FieldLoc::YFACE))
49 , m_flux_z(fields_in.repo.create_scratch_field(
50 PDE::ndim, 0, kynema_sgf::FieldLoc::ZFACE))
51 , m_face_x(fields_in.repo.create_scratch_field(
52 PDE::ndim, 0, kynema_sgf::FieldLoc::XFACE))
53 , m_face_y(fields_in.repo.create_scratch_field(
54 PDE::ndim, 0, kynema_sgf::FieldLoc::YFACE))
55 , m_face_z(fields_in.repo.create_scratch_field(
56 PDE::ndim, 0, kynema_sgf::FieldLoc::ZFACE))
57 {
58 amrex::ParmParse pp("incflo");
59 pp.query("godunov_type", godunov_type);
60 pp.query("godunov_use_forces_in_trans", godunov_use_forces_in_trans);
61 if (pp.contains("use_ppm") || pp.contains("use_limiter")) {
62 amrex::Abort(
63 "Godunov: use_ppm and use_limiter are deprecated. Please "
64 "update input file");
65 }
66
67 if (amrex::toLower(godunov_type) == "plm") {
69 } else if (amrex::toLower(godunov_type) == "ppm") {
71 } else if (amrex::toLower(godunov_type) == "ppm_nolim") {
73 amrex::Print() << "WARNING: Using advection type ppm_nolim is not "
74 "recommended. Prefer using weno_z."
75 << '\n';
76 } else if (amrex::toLower(godunov_type) == "bds") {
78 advection_type = "BDS";
79 } else if (
80 amrex::toLower(godunov_type) == "weno" ||
81 amrex::toLower(godunov_type) == "weno_js") {
83 } else if (amrex::toLower(godunov_type) == "weno_z") {
85 } else {
86 amrex::Abort(
87 "Invalid godunov_type specified. For godunov_type select "
88 "between plm, ppm, ppm_nolim, bds, weno_js, and weno_z. If no "
89 "godunov_type is specified, the default weno_z is used.");
90 }
91
92 // Flux calculation used in multiphase portions of domain
93 pp.query("mflux_type", mflux_type);
94 if (amrex::toLower(mflux_type) == "minmod") {
96 } else if (amrex::toLower(mflux_type) == "upwind") {
98 } else {
99 amrex::Abort("Invalid argument entered for mflux_type.");
100 }
101
102 amrex::ParmParse pp_eq{fields_in.field.base_name()};
103 pp_eq.query(
104 "allow_inflow_at_pressure_outflow", m_allow_inflow_on_outflow);
105
106 // TODO: Need iconserv flag to be adjusted???
107 iconserv.resize(PDE::ndim, 1);
108 }
109
111 const FieldState /*unused*/,
112 const amrex::Real /*unused*/,
113 const amrex::Real /*unused*/)
114 {}
115
116 void operator()(const FieldState fstate, const amrex::Real dt)
117 {
118 static_assert(
119 PDE::ndim == 1, "Invalid number of components for scalar");
120 auto& repo = fields.repo;
121 const auto& geom = repo.mesh().Geom();
122
123 const auto& src_term = fields.src_term;
124 auto& conv_term = fields.conv_term;
125 const auto& dof_field = fields.field.state(fstate);
126 const auto& dof_nph = fields.field.state(kynema_sgf::FieldState::NPH);
127
128 auto& flux_x = *m_flux_x;
129 auto& flux_y = *m_flux_y;
130 auto& flux_z = *m_flux_z;
131 auto& face_x = *m_face_x;
132 auto& face_y = *m_face_y;
133 auto& face_z = *m_face_z;
134
135 // only needed if multiplying by rho below
136 const auto& den = density.state(fstate);
137 const auto& den_nph = density.state(kynema_sgf::FieldState::NPH);
138
139 const bool mphase_vof = repo.field_exists("vof");
140
141 for (int lev = 0; lev < repo.num_active_levels(); ++lev) {
142 amrex::MFItInfo mfi_info;
143 if (amrex::Gpu::notInLaunchRegion()) {
144 mfi_info.EnableTiling(amrex::IntVect(1024, 1024, 1024))
145 .SetDynamic(true);
146 }
147#ifdef AMREX_USE_OMP
148#pragma omp parallel if (amrex::Gpu::notInLaunchRegion())
149#endif
150 for (amrex::MFIter mfi(dof_field(lev), mfi_info); mfi.isValid();
151 ++mfi) {
152 const auto& bx = mfi.tilebox();
153 auto rho_arr = den(lev).array(mfi);
154 auto rho_nph_arr = den_nph(lev).array(mfi);
155 auto tra_arr = dof_field(lev).array(mfi);
156 auto tra_nph_arr = dof_nph(lev).array(mfi);
157 amrex::FArrayBox rhotracfab;
158 amrex::Array4<amrex::Real> rhotrac;
159 amrex::FArrayBox rhotrac_nph_fab;
160 amrex::Array4<amrex::Real> rhotrac_nph;
161 amrex::FArrayBox srctrac_over_rho_fab;
162 amrex::Array4<amrex::Real> srctrac_over_rho;
163 const auto& src_arr = src_term(lev).const_array(mfi);
164
165 if (PDE::multiply_rho) {
166 auto rhotrac_box =
167 amrex::grow(bx, fvm::Godunov::nghost_state);
168 auto srctrac_box =
169 amrex::grow(bx, fvm::Godunov::nghost_src);
170 rhotracfab.resize(
171 rhotrac_box, PDE::ndim, amrex::The_Async_Arena());
172 rhotrac = rhotracfab.array();
173 rhotrac_nph_fab.resize(
174 rhotrac_box, PDE::ndim, amrex::The_Async_Arena());
175 rhotrac_nph = rhotrac_nph_fab.array();
176 if (mphase_vof) {
177 srctrac_over_rho_fab.resize(
178 rhotrac_box, PDE::ndim, amrex::The_Async_Arena());
179 srctrac_over_rho = srctrac_over_rho_fab.array();
180 }
181
182 amrex::ParallelFor(
183 rhotrac_box, PDE::ndim,
184 [=] AMREX_GPU_DEVICE(int i, int j, int k, int n) {
185 rhotrac(i, j, k, n) =
186 rho_arr(i, j, k) * tra_arr(i, j, k, n);
187 rhotrac_nph(i, j, k, n) =
188 rho_nph_arr(i, j, k) * tra_nph_arr(i, j, k, n);
189 if (mphase_vof && srctrac_box.contains(i, j, k)) {
190 // Need to remove the effect of the multiply_rho
191 // routine -- the setup is different for
192 // mass-consistent multiphase advection
193 srctrac_over_rho(i, j, k, n) =
194 src_arr(i, j, k, n) / rho_nph_arr(i, j, k);
195 }
196 });
197 }
198
205 amrex::FArrayBox tmpfab(
206 amrex::grow(bx, 1), 1, amrex::The_Async_Arena());
207 tmpfab.setVal<amrex::RunOn::Device>(0.0_rt);
208 const auto& divu = tmpfab.array();
209 const bool is_velocity = false;
210 const bool known_edge_state = false;
211 const bool godunov_use_ppm =
214 int limiter_type;
216 limiter_type = PPM::NoLimiter;
218 limiter_type = PPM::WENOZ;
220 limiter_type = PPM::WENO_JS;
221 } else {
222 limiter_type = PPM::default_limiter;
223 }
224
225 // if state is NPH, then n and n+1 are known, and only
226 // spatial extrapolation is performed
227 const amrex::Real dt_extrap =
228 (fstate == FieldState::NPH) ? 0.0_rt : dt;
229
230 HydroUtils::ComputeFluxesOnBoxFromState(
231 bx, PDE::ndim, mfi,
232 ((PDE::multiply_rho && !mphase_vof) ? rhotrac
233 : tra_arr),
234 ((PDE::multiply_rho && !mphase_vof) ? rhotrac_nph
235 : tra_nph_arr),
236 flux_x(lev).array(mfi), flux_y(lev).array(mfi),
237 flux_z(lev).array(mfi), face_x(lev).array(mfi),
238 face_y(lev).array(mfi), face_z(lev).array(mfi),
239 known_edge_state, u_mac(lev).const_array(mfi),
240 v_mac(lev).const_array(mfi),
241 w_mac(lev).const_array(mfi), divu,
242 ((PDE::multiply_rho && mphase_vof)
243 ? srctrac_over_rho
244 : src_term(lev).const_array(mfi)),
245 geom[lev], dt_extrap, dof_field.bcrec(),
246 dof_field.bcrec_device().data(), iconserv.data(),
247 godunov_use_ppm, godunov_use_forces_in_trans,
249 limiter_type, m_allow_inflow_on_outflow);
250 } else {
251 amrex::Abort("Invalid godunov scheme");
252 }
253 }
254 }
255
256 // Multiphase flux operations
257 if (mphase_vof && PDE::multiply_rho) {
258 // Loop levels
260 repo, PDE::ndim, iconserv, flux_x, flux_y, flux_z, dof_field,
261 dof_nph, src_term, den, den_nph, u_mac, v_mac, w_mac,
262 dof_field.bcrec(), dof_field.bcrec_device().data(), den.bcrec(),
263 den.bcrec_device().data(), dt, mflux_scheme,
265 }
266
267 amrex::Vector<amrex::Array<amrex::MultiFab*, AMREX_SPACEDIM>> fluxes(
268 repo.num_active_levels());
269 for (int lev = 0; lev < repo.num_active_levels(); ++lev) {
270 fluxes[lev][0] = &flux_x(lev);
271 fluxes[lev][1] = &flux_y(lev);
272 fluxes[lev][2] = &flux_z(lev);
273 }
274
275 // In order to enforce conservation across coarse-fine boundaries we
276 // must be sure to average down the fluxes before we use them
277 for (int lev = repo.num_active_levels() - 1; lev > 0; --lev) {
278 amrex::IntVect rr =
279 geom[lev].Domain().size() / geom[lev - 1].Domain().size();
280 amrex::average_down_faces(
281 GetArrOfConstPtrs(fluxes[lev]), fluxes[lev - 1], rr,
282 geom[lev - 1]);
283 }
284
285 for (int lev = 0; lev < repo.num_active_levels(); ++lev) {
286#ifdef AMREX_USE_OMP
287#pragma omp parallel if (amrex::Gpu::notInLaunchRegion())
288#endif
289 for (amrex::MFIter mfi(dof_field(lev), amrex::TilingIfNotGPU());
290 mfi.isValid(); ++mfi) {
291 const auto& bx = mfi.tilebox();
292
293 HydroUtils::ComputeDivergence(
294 bx, conv_term(lev).array(mfi), flux_x(lev).array(mfi),
295 flux_y(lev).array(mfi), flux_z(lev).array(mfi), PDE::ndim,
296 geom[lev], amrex::Real(-1.0_rt), fluxes_are_area_weighted);
297 }
298 }
299 }
300
306
307 std::unique_ptr<ScratchField> m_flux_x;
308 std::unique_ptr<ScratchField> m_flux_y;
309 std::unique_ptr<ScratchField> m_flux_z;
310 std::unique_ptr<ScratchField> m_face_x;
311 std::unique_ptr<ScratchField> m_face_y;
312 std::unique_ptr<ScratchField> m_face_z;
313
314 amrex::Gpu::DeviceVector<int> iconserv;
315
318 std::string godunov_type{"weno_z"};
319 std::string mflux_type{"upwind"};
320 const bool fluxes_are_area_weighted{false};
323 std::string advection_type{"Godunov"};
324};
325
326} // namespace kynema_sgf::pde
327
328#endif /* ADVOP_GODUNOV_H */
Definition CFDSim.H:55
Definition Field.H:115
const std::string & base_name() const
Base name (without state information)
Definition Field.H:127
FieldLoc
Definition FieldDescTypes.H:29
FieldState
Definition FieldDescTypes.H:16
@ ZFACE
Face-centered in z-direction.
Definition FieldDescTypes.H:34
@ XFACE
Face-centered in x-direction (e.g., face normal velocity)
Definition FieldDescTypes.H:32
@ YFACE
Face-centered in y-direction.
Definition FieldDescTypes.H:33
@ NPH
State at (n + 1/2) (intermediate) timestep.
Definition FieldDescTypes.H:20
scheme
Definition Godunov.H:8
@ BDS
Definition Godunov.H:12
@ PPM
Definition Godunov.H:10
@ UPWIND
Definition Godunov.H:16
@ PLM
Definition Godunov.H:9
@ WENO_JS
Definition Godunov.H:13
@ MINMOD
Definition Godunov.H:15
@ PPM_NOLIM
Definition Godunov.H:11
@ WENOZ
Definition Godunov.H:14
Definition SchemeTraits.H:6
static AMREX_INLINE void hybrid_fluxes(const FieldRepo &repo, const int ncomp, const amrex::Gpu::DeviceVector< int > &iconserv, ScratchField &flux_x, ScratchField &flux_y, ScratchField &flux_z, const Field &dof_field, const Field &dof_nph, const Field &src_term, const Field &rho_o, const Field &rho_nph, const Field &u_mac, const Field &v_mac, const Field &w_mac, amrex::Vector< amrex::BCRec > const &velbc, amrex::BCRec const *velbc_d, amrex::Vector< amrex::BCRec > const &rhobc, amrex::BCRec const *rhobc_d, const amrex::Real dt, godunov::scheme mflux_scheme, bool allow_inflow_on_outflow, bool use_forces_in_trans, bool pre_multiplied_src_term=false)
Definition vof_momentum_flux.H:11
Definition AdvOp_Godunov.H:22
This test case is intended as an evaluation of the momentum advection scheme.
Definition BCInterface.cpp:12
static constexpr int nghost_src
Number of ghost cells in the source term variable.
Definition SchemeTraits.H:21
static constexpr int nghost_state
Number of ghost in the state variable.
Definition SchemeTraits.H:19
AdvectionOp(CFDSim &, PDEFields &fields_in, bool, bool, bool, bool)
Definition AdvOp_Godunov.H:33
void preadvect(const FieldState, const amrex::Real, const amrex::Real)
Definition AdvOp_Godunov.H:110
void operator()(const FieldState fstate, const amrex::Real dt)
Definition AdvOp_Godunov.H:116
Definition PDEFields.H:27
Field & field
Solution variable (e.g., velocity, temperature)
Definition PDEFields.H:34