1#ifndef ICNS_DIFFUSION_H
2#define ICNS_DIFFUSION_H
19 PDEFields& fields,
const bool has_overset,
const bool mesh_mapping)
26 this->m_pdefields.field, amrex::Orientation::high));
27 this->m_applier->setDomainBC(
29 this->m_pdefields.field, amrex::Orientation::low),
31 this->m_pdefields.field, amrex::Orientation::high));
34 template <
typename Scheme>
39 auto tau_state = std::is_same<Scheme, fvm::Godunov>::value
45 mlmg.apply(divtau.vec_ptrs(), this->m_pdefields.field.vec_ptrs());
54 const bool has_overset,
55 const bool mesh_mapping,
56 const std::string& prefix =
"diffusion")
58 ,
m_density(fields.repo.get_field(
"density"))
65 iapply.setMaxCoarseningLevel(0);
76 mesh.Geom(0, mesh.finestLevel()),
77 mesh.boxArray(0, mesh.finestLevel()),
78 mesh.DistributionMap(0, mesh.finestLevel()), isolve,
80 amrex::FabFactory<amrex::MLABecLaplacian::FAB>
const*>(),
83 mesh.Geom(0, mesh.finestLevel()),
84 mesh.boxArray(0, mesh.finestLevel()),
85 mesh.DistributionMap(0, mesh.finestLevel()), iapply,
87 amrex::FabFactory<amrex::MLABecLaplacian::FAB>
const*>(),
93 mesh.Geom(0, mesh.finestLevel()),
94 mesh.boxArray(0, mesh.finestLevel()),
95 mesh.DistributionMap(0, mesh.finestLevel()), imask, isolve,
97 amrex::FabFactory<amrex::MLABecLaplacian::FAB>
const*>(),
100 mesh.Geom(0, mesh.finestLevel()),
101 mesh.boxArray(0, mesh.finestLevel()),
102 mesh.DistributionMap(0, mesh.finestLevel()), imask, iapply,
104 amrex::FabFactory<amrex::MLABecLaplacian::FAB>
const*>(),
115 template <
typename Scheme>
118 auto tau_state = std::is_same<Scheme, fvm::Godunov>::value
123 const auto& geom = repo.mesh().Geom();
129 Field const* mesh_detJ =
132 std::unique_ptr<ScratchField> rho_times_detJ =
137 const amrex::Real alpha = 0.0;
138 const amrex::Real beta = -1.0;
141 for (
int lev = 0; lev < nlevels; ++lev) {
146 (*rho_times_detJ)(lev).setVal(0.0);
147 amrex::MultiFab::AddProduct(
148 (*rho_times_detJ)(lev), density(lev), 0, (*mesh_detJ)(lev),
157 geom[lev], viscosity(lev));
168 for (
int lev = 0; lev < nlevels; ++lev) {
170#pragma omp parallel if (amrex::Gpu::notInLaunchRegion())
172 for (amrex::MFIter mfi(divtau(lev), amrex::TilingIfNotGPU());
173 mfi.isValid(); ++mfi) {
174 const auto& bx = mfi.tilebox();
175 const auto& divtau_arr = divtau(lev).array(mfi);
176 const auto& rho_arr = density(lev).const_array(mfi);
179 bx, [=] AMREX_GPU_DEVICE(
int i,
int j,
int k)
noexcept {
180 amrex::Real rhoinv = 1.0 / rho_arr(i, j, k);
181 divtau_arr(i, j, k, 0) *= rhoinv;
182 divtau_arr(i, j, k, 1) *= rhoinv;
183 divtau_arr(i, j, k, 2) *= rhoinv;
194 const auto& geom = repo.
mesh().Geom();
197 const int nlevels = repo.num_active_levels();
199 auto rhs_ptr = repo.create_scratch_field(
"rhs", field.num_comp(), 0);
201 Field const* mesh_detJ =
204 std::unique_ptr<ScratchField> rho_times_detJ =
209 const amrex::Real alpha = 1.0;
210 const amrex::Real beta = dt;
213 for (
int lev = 0; lev < nlevels; ++lev) {
218 (*rho_times_detJ)(lev).setVal(0.0);
219 amrex::MultiFab::AddProduct(
220 (*rho_times_detJ)(lev), density(lev), 0, (*mesh_detJ)(lev),
229 geom[lev], viscosity(lev));
237 for (
int lev = 0; lev < nlevels; ++lev) {
238 auto& rhs = (*rhs_ptr)(lev);
241#pragma omp parallel if (amrex::Gpu::notInLaunchRegion())
243 for (amrex::MFIter mfi(rhs, amrex::TilingIfNotGPU()); mfi.isValid();
245 const auto& bx = mfi.tilebox();
246 const auto& rhs_a = rhs.array(mfi);
247 const auto& fld = field(lev).const_array(mfi);
248 const auto& rho = density(lev).const_array(mfi);
252 [=] AMREX_GPU_DEVICE(
int i,
int j,
int k,
int n)
noexcept {
253 rhs_a(i, j, k, n) = rho(i, j, k) * fld(i, j, k, n);
282 const bool has_overset,
283 const bool mesh_mapping,
284 const std::string& prefix =
"diffusion")
286 ,
m_density(fields.repo.get_field(
"density"))
291 amrex::LPInfo iapply;
293 iapply.setMaxCoarseningLevel(0);
301 for (
int i = 0; i < AMREX_SPACEDIM; ++i) {
304 mesh.Geom(0, mesh.finestLevel()),
305 mesh.boxArray(0, mesh.finestLevel()),
306 mesh.DistributionMap(0, mesh.finestLevel()), isolve);
308 mesh.Geom(0, mesh.finestLevel()),
309 mesh.boxArray(0, mesh.finestLevel()),
310 mesh.DistributionMap(0, mesh.finestLevel()), iapply);
315 mesh.Geom(0, mesh.finestLevel()),
316 mesh.boxArray(0, mesh.finestLevel()),
317 mesh.DistributionMap(0, mesh.finestLevel()), imask, isolve);
319 mesh.Geom(0, mesh.finestLevel()),
320 mesh.boxArray(0, mesh.finestLevel()),
321 mesh.DistributionMap(0, mesh.finestLevel()), imask, iapply);
332 template <
typename Scheme>
335 auto tau_state = std::is_same<Scheme, fvm::Godunov>::value
340 const auto& geom = repo.mesh().Geom();
346 Field const* mesh_detJ =
349 std::unique_ptr<ScratchField> rho_times_detJ =
354 const amrex::Real alpha = 0.0;
355 const amrex::Real beta = -1.0;
357 for (
int i = 0; i < AMREX_SPACEDIM; ++i) {
360 for (
int lev = 0; lev < nlevels; ++lev) {
366 (*rho_times_detJ)(lev).setVal(0.0);
367 amrex::MultiFab::AddProduct(
368 (*rho_times_detJ)(lev), density(lev), 0,
375 lev, (*rho_times_detJ)(lev));
382 geom[lev], viscosity(lev));
389 lev, amrex::GetArrOfConstPtrs(b));
392 auto divtau_comp = divtau.subview(i);
396 mlmg.apply(divtau_comp.vec_ptrs(), vel_comp.vec_ptrs());
400 for (
int lev = 0; lev < nlevels; ++lev) {
402#pragma omp parallel if (amrex::Gpu::notInLaunchRegion())
404 for (amrex::MFIter mfi(divtau(lev), amrex::TilingIfNotGPU());
405 mfi.isValid(); ++mfi) {
406 const auto& bx = mfi.tilebox();
407 const auto& divtau_arr = divtau(lev).array(mfi);
408 const auto& rho_arr = density(lev).const_array(mfi);
411 bx, [=] AMREX_GPU_DEVICE(
int i,
int j,
int k)
noexcept {
412 amrex::Real rhoinv = 1.0 / rho_arr(i, j, k);
413 divtau_arr(i, j, k, 0) *= rhoinv;
414 divtau_arr(i, j, k, 1) *= rhoinv;
415 divtau_arr(i, j, k, 2) *= rhoinv;
428 const int nlevels = repo.num_active_levels();
430 auto rhs_ptr = repo.create_scratch_field(
"rhs", field.num_comp(), 0);
432 const auto& geom = repo.mesh().Geom();
434 Field const* mesh_detJ =
437 std::unique_ptr<ScratchField> rho_times_detJ =
442 const amrex::Real alpha = 1.0;
443 const amrex::Real beta = dt;
445 for (
int i = 0; i < AMREX_SPACEDIM; ++i) {
448 for (
int lev = 0; lev < nlevels; ++lev) {
451 (*rho_times_detJ)(lev).setVal(0.0);
452 amrex::MultiFab::AddProduct(
453 (*rho_times_detJ)(lev), density(lev), 0,
469 geom[lev], viscosity(lev));
475 lev, amrex::GetArrOfConstPtrs(b));
480 for (
int lev = 0; lev < nlevels; ++lev) {
481 auto& rhs = (*rhs_ptr)(lev);
484#pragma omp parallel if (amrex::Gpu::notInLaunchRegion())
486 for (amrex::MFIter mfi(rhs, amrex::TilingIfNotGPU()); mfi.isValid();
488 const auto& bx = mfi.tilebox();
489 const auto& rhs_a = rhs.array(mfi);
490 const auto& fld = field(lev).const_array(mfi);
491 const auto& rho = density(lev).const_array(mfi);
495 [=] AMREX_GPU_DEVICE(
int i,
int j,
int k,
int n)
noexcept {
496 rhs_a(i, j, k, n) = rho(i, j, k) * fld(i, j, k, n);
501 for (
int i = 0; i < AMREX_SPACEDIM; ++i) {
503 auto rhs_ptr_comp = rhs_ptr->subview(i);
509 vel_comp.vec_ptrs(), rhs_ptr_comp.vec_const_ptrs(),
513 field.name() + std::to_string(i) +
"_solve", mlmg);
523 amrex::Array<std::unique_ptr<amrex::MLABecLaplacian>, AMREX_SPACEDIM>
525 amrex::Array<std::unique_ptr<amrex::MLABecLaplacian>, AMREX_SPACEDIM>
532template <
typename Scheme>
541 "DiffusionOp invoked for scalar PDE type");
543 bool use_segregated_op =
false;
546 PDEFields& fields,
const bool has_overset,
const bool mesh_mapping)
548 bool use_tensor_op =
true;
550 amrex::ParmParse pp(fields.
field.
name() +
"_diffusion");
551 pp.query(
"use_tensor_operator", use_tensor_op);
552 pp.query(
"use_segregated_operator", use_segregated_op);
554 if (use_tensor_op && use_segregated_op) {
556 "Tensor and segregated operators should not be enabled "
561 m_tensor_op = std::make_unique<ICNSDiffTensorOp>(
562 fields, has_overset, mesh_mapping);
564 if (use_segregated_op) {
565 m_scalar_segregated_op =
566 std::make_unique<ICNSDiffScalarSegregatedOp>(
567 fields, has_overset, mesh_mapping);
569 m_scalar_op = std::make_unique<ICNSDiffScalarOp>(
570 fields, has_overset, mesh_mapping);
578 m_tensor_op->compute_diff_term<Scheme>(fstate);
580 if (use_segregated_op) {
581 m_scalar_segregated_op->compute_diff_term<Scheme>(fstate);
583 m_scalar_op->compute_diff_term<Scheme>(fstate);
591 m_tensor_op->linsys_solve(dt);
593 if (use_segregated_op) {
594 m_scalar_segregated_op->linsys_solve(dt);
596 m_scalar_op->linsys_solve(dt);
ViewField< Field > subview(const int scomp=0, const int ncomp=1)
Definition Field.H:296
const amrex::IntVect & num_grow() const
Ghost cells.
Definition Field.H:137
const std::string & name() const
Name of this field (including state information)
Definition Field.H:125
int num_comp() const
Number of components for this field.
Definition Field.H:134
amrex::Vector< amrex::MultiFab * > vec_ptrs() noexcept
Return a vector of MultiFab pointers for all levels.
Definition Field.cpp:142
Field & state(const FieldState fstate)
Return field at a different time state.
Definition Field.cpp:114
int num_active_levels() const noexcept
Total number of levels currently active in the AMR mesh.
Definition FieldRepo.H:361
IntField & get_int_field(const std::string &name, const FieldState fstate=FieldState::New) const
Return a reference to an integer field.
Definition FieldRepo.cpp:276
const amrex::AmrCore & mesh() const
Return a reference to the underlying AMR mesh instance.
Definition FieldRepo.H:358
amrex::Vector< const amrex::iMultiFab * > vec_const_ptrs() const noexcept
Definition IntField.cpp:46
Definition DiffusionOps.H:30
PDEFields & m_pdefields
Definition DiffusionOps.H:100
std::unique_ptr< amrex::MLTensorOp > m_applier
Definition DiffusionOps.H:108
virtual void setup_operator(amrex::MLTensorOp &linop, const amrex::Real alpha, const amrex::Real beta, const FieldState fstate)
Definition DiffusionOps.cpp:54
std::unique_ptr< amrex::MLTensorOp > m_solver
Definition DiffusionOps.H:107
Definition icns_diffusion.H:50
void compute_diff_term(const FieldState fstate)
Definition icns_diffusion.H:116
ICNSDiffScalarOp(PDEFields &fields, const bool has_overset, const bool mesh_mapping, const std::string &prefix="diffusion")
Definition icns_diffusion.H:52
void linsys_solve(const amrex::Real dt)
Definition icns_diffusion.H:190
bool m_mesh_mapping
Definition icns_diffusion.H:271
std::unique_ptr< amrex::MLABecLaplacian > m_solver_scalar
Definition icns_diffusion.H:273
Field & m_density
Definition icns_diffusion.H:269
MLMGOptions m_options
Definition icns_diffusion.H:270
PDEFields & m_pdefields
Definition icns_diffusion.H:268
std::unique_ptr< amrex::MLABecLaplacian > m_applier_scalar
Definition icns_diffusion.H:274
Definition icns_diffusion.H:278
MLMGOptions m_options
Definition icns_diffusion.H:520
amrex::Array< std::unique_ptr< amrex::MLABecLaplacian >, AMREX_SPACEDIM > m_solver_scalar
Definition icns_diffusion.H:524
void compute_diff_term(const FieldState fstate)
Definition icns_diffusion.H:333
void linsys_solve(const amrex::Real dt)
Definition icns_diffusion.H:422
PDEFields & m_pdefields
Definition icns_diffusion.H:518
Field & m_density
Definition icns_diffusion.H:519
ICNSDiffScalarSegregatedOp(PDEFields &fields, const bool has_overset, const bool mesh_mapping, const std::string &prefix="diffusion")
Definition icns_diffusion.H:280
bool m_mesh_mapping
Definition icns_diffusion.H:521
amrex::Array< std::unique_ptr< amrex::MLABecLaplacian >, AMREX_SPACEDIM > m_applier_scalar
Definition icns_diffusion.H:526
Definition icns_diffusion.H:16
ICNSDiffTensorOp(PDEFields &fields, const bool has_overset, const bool mesh_mapping)
Definition icns_diffusion.H:18
void compute_diff_term(const FieldState fstate)
Definition icns_diffusion.H:35
FieldState
Definition FieldDescTypes.H:14
@ CELL
Cell-centered (default)
@ New
Same as FieldState::NP1.
void print_mlmg_info(const std::string &solve_name, const amrex::MLMG &mlmg)
Definition console_io.cpp:168
Definition AdvOp_Godunov.H:16
Definition console_io.cpp:25
void viscosity_to_uniform_space(amrex::Array< amrex::MultiFab, AMREX_SPACEDIM > &b, const amr_wind::FieldRepo &repo, int lev)
Definition incflo_diffusion.cpp:205
amrex::Vector< amrex::Array< amrex::LinOpBCType, AMREX_SPACEDIM > > get_diffuse_tensor_bc(amr_wind::Field &velocity, amrex::Orientation::Side side) noexcept
amrex::Array< amrex::MultiFab, AMREX_SPACEDIM > average_velocity_eta_to_faces(const amrex::Geometry &geom, amrex::MultiFab const &cc_eta)
Definition MLMGOptions.H:25
amrex::LPInfo & lpinfo()
Linear operator options during construction.
Definition MLMGOptions.H:48
int max_order
Definition MLMGOptions.H:51
amrex::Real abs_tol
Absolute tolerance for convergence checks.
Definition MLMGOptions.H:57
amrex::Real rel_tol
Relative tolerance for convergence of MLMG solvers.
Definition MLMGOptions.H:54
DiffusionOp(PDEFields &fields, const bool has_overset, const bool mesh_mapping)
Definition icns_diffusion.H:545
void linsys_solve(const amrex::Real dt)
Definition icns_diffusion.H:588
std::unique_ptr< ICNSDiffScalarSegregatedOp > m_scalar_segregated_op
Definition icns_diffusion.H:537
void compute_diff_term(const FieldState fstate)
Definition icns_diffusion.H:575
std::unique_ptr< ICNSDiffScalarOp > m_scalar_op
Definition icns_diffusion.H:536
std::unique_ptr< ICNSDiffTensorOp > m_tensor_op
Definition icns_diffusion.H:535
static constexpr int ndim
Definition icns.H:40
Definition PDEFields.H:27
Field & diff_term
Diffusion term for this PDE.
Definition PDEFields.H:41
Field & mueff
Effective visocity field (e.g., velocity_mueff)
Definition PDEFields.H:36
FieldRepo & repo
Reference to the field repository instance.
Definition PDEFields.H:31
Field & field
Solution variable (e.g., velocity, temperature)
Definition PDEFields.H:34