include/pops/runtime/builders/compiled/dsl_block.hpp Source FileΒΆ

adc_cpp: include/pops/runtime/builders/compiled/dsl_block.hpp Source File
adc_cpp 0.3.0
Model-free C++23 core for coupled hyperbolic-elliptic systems on adaptive (AMR) meshes, with MPI and GPU (Kokkos) backends
dsl_block.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <pops/core/model/physical_model.hpp> // aux_comps<Model>: aux width requested by the model
6
7#include <functional>
8#include <string>
9#include <utility>
10
38
39namespace pops {
40
52template <class Model>
53void add_compiled_model(System& sys, const std::string& name, Model model,
54 const std::string& limiter = "minmod",
55 const std::string& riemann = "rusanov",
56 const std::string& recon = "conservative",
57 const std::string& time = "explicit", double gamma = 1.4, int substeps = 1,
58 bool evolve = true, int stride = 1, double positivity_floor = 0) {
59 const bool imex = (time == "imex");
60 const bool recon_prim = (recon == "primitive");
61 // EXPLICIT RK scheme marshaled by the production path (add_native_block -> pops_install_native
62 // -> this template): "ssprk3" (3 stages, order 3, less dissipative, to pair with weno5), "euler"
63 // (ForwardEuler, order 1: fidelity to first-order references, validation) vs "ssprk2"
64 // (historical default, bit-identical). Has effect ONLY on the explicit advance -- IMEX keeps its
65 // ForwardEuler half-step + implicit source, so method is ignored when imex. We thus align the
66 // production .so with the native add_block path (system.cpp) which already exposed ssprk3; any
67 // other string ("explicit"/unknown) falls back to ssprk2 (add_native_block validates the upstream string).
68 const std::string method = (time == "ssprk3") ? "ssprk3" : (time == "euler") ? "euler" : "ssprk2";
69 // The block may read extra auxiliary fields (aux_comps<Model> > 3, e.g. B_z of a magnetized
70 // source): we widen the System's SHARED aux channel BEFORE capturing its address, so that the
71 // closure reads a wide enough aux. Base model (3) -> no-op, unchanged.
72 sys.ensure_aux_width(aux_comps<Model>());
73 const GridContext ctx = sys.grid_context();
74 BlockClosures clo = make_block(model, limiter, riemann, ctx, imex, recon_prim, method, {}, {},
75 nullptr, static_cast<Real>(positivity_floor));
76 std::function<Real(const MultiFab&)> ms = make_max_speed(model, ctx);
77 std::function<void(const MultiFab&, MultiFab&)> pr = make_poisson_rhs(model);
78 sys.install_block(name, Model::n_vars, Model::conservative_vars(), Model::primitive_vars(), gamma,
79 std::move(clo), std::move(ms), std::move(pr), substeps, evolve, stride);
80 // cons <-> prim conversions OF THE MODEL (set/get_primitive_state): same formulas as the flux of
81 // the production path. Set AFTER install_block (like set_block_ghosts); a native .so loader
82 // recompiled against this header (ABI key verified) carries them too.
83 auto conv = make_cell_convert(model);
84 sys.set_block_conversion(name, std::move(conv.first), std::move(conv.second));
85 // OPTIONAL step bounds of the model (HasSourceFrequency / HasStabilityDt traits, see
86 // core/physical_model.hpp): compiled here like flux/source (a DSL model declaring
87 // m.source_frequency(...) / m.stability_dt(...) carries them down to the System's step_cfl).
88 // EMPTY functions if the model does not declare the traits -> historical step policy. The
89 // STABILITY speed (HasStabilitySpeed) is already carried by make_max_speed above.
90 sys.set_block_dt_bounds(name, make_source_frequency(model, ctx), make_stability_dt(model, ctx));
91 // Scheme GHOSTS: WENO5 reads a 5-point stencil (3 ghosts) > the 2 allocated by install_block.
92 // We reallocate the block state with block_n_ghost(limiter) -- SAME mechanism as add_block (PR #88) --
93 // so that fill_boundary + assemble_rhs do not read out of bounds on the System's real MultiFab.
94 // none/minmod/vanleer (<= 2 ghosts): no-op, allocation and result bit-identical to before.
95 sys.set_block_ghosts(name, block_n_ghost(limiter));
96}
97
98} // namespace pops
Builds the closures of a block (time advance + residual + Poisson contribution) from a COMPILED model...
Field distributed over a level: decomposition (BoxArray) + distribution (DistributionMapping) + ncomp...
Definition multifab.hpp:33
Coupled multi-species system, composed at runtime from generic bricks.
Definition system.hpp:89
POPS_EXPORT GridContext grid_context()
REAL mesh + BC + aux of the System (aux not owned)
POPS_EXPORT void set_block_dt_bounds(const std::string &name, std::function< Real(const MultiFab &)> source_frequency, std::function< Real(const MultiFab &)> stability_dt)
Installs the optional STEP BOUNDS of a block (after install_block): reduction of the max source frequ...
POPS_EXPORT void ensure_aux_width(int ncomp)
Guarantees that the SHARED aux channel has at least ncomp components.
POPS_EXPORT void install_block(const std::string &name, int ncomp, const VariableSet &cons_vars, const VariableSet &prim_vars, double gamma, BlockClosures closures, std::function< Real(const MultiFab &)> max_speed, std::function< void(const MultiFab &, MultiFab &)> poisson_rhs, int substeps, bool evolve, int stride=1)
Installs a block from already-built closures (cf.
POPS_EXPORT void set_block_conversion(const std::string &name, CellConvert prim_to_cons, CellConvert cons_to_prim)
Installs the pointwise cons <-> prim conversions of a block (after install_block).
POPS_EXPORT void set_block_ghosts(const std::string &name, int n_ghost)
Guarantees that the state U of block name carries at least n_ghost ghosts (width of the spatial stenc...
Definition amr_hierarchy.hpp:29
int block_n_ghost(const std::string &lim)
Number of ghosts required by the spatial scheme lim (single source: Limiter::n_ghost).
Definition block_builder.hpp:759
std::function< Real(const MultiFab &)> make_stability_dt(const Model &m, const GridContext &ctx)
Closure of the block min admissible step (bound dt <= stability_dt * substeps / stride,...
Definition block_builder.hpp:852
double Real
Definition types.hpp:30
void add_compiled_model(AmrSystem &sys, const std::string &name, Model model, const std::string &limiter="minmod", const std::string &riemann="rusanov", const std::string &recon="conservative", const std::string &time="explicit", double gamma=1.4, int substeps=1, int stride=1, const std::vector< std::string > &implicit_vars={}, const std::vector< std::string > &implicit_roles={}, double pos_floor=0.0)
Wires model (concrete CompositeModel) as an AMR block of sys, with the requested scheme.
Definition amr_dsl_block.hpp:1028
POPS_COLD_FN BlockClosures make_block(const Model &m, const std::string &lim, const std::string &riem, const GridContext &ctx, bool imex, bool recon_prim, const std::string &method="ssprk2", const std::vector< int > &implicit_components={}, const NewtonOptions &newton_opts={}, NewtonReport *newton_report=nullptr, Real pos_floor=Real(0), bool wave_speed_cache=false)
Definition block_builder.hpp:726
std::function< void(const MultiFab &, MultiFab &)> make_poisson_rhs(const Model &m)
Block contribution to the Poisson right-hand side: rhs += elliptic_rhs(U) (host loop).
Definition block_builder.hpp:861
std::function< Real(const MultiFab &)> make_max_speed(const Model &m, const GridContext &ctx)
Closure of the speed used by the block CFL step.
Definition block_builder.hpp:831
std::function< Real(const MultiFab &)> make_source_frequency(const Model &m, const GridContext &ctx)
Closure of the block max source frequency (bound dt <= cfl * substeps / (stride * mu)).
Definition block_builder.hpp:842
std::pair< std::function< void(const double *, double *)>, std::function< void(const double *, double *)> > make_cell_convert(const Model &m)
PER-CELL (one cell) cons <-> prim conversions of the MODEL, type-erased over arrays of Model::n_vars ...
Definition block_builder.hpp:875
C++20 concepts defining the contract of the physics layer.
Compiled block closures, frozen at add time.
Definition grid_context.hpp:61
Mesh + transport BC + aux shared by a block closures.
Definition grid_context.hpp:41
Runtime multi-species composition: one coupled system, block by block.