include/pops/runtime/builders/block/amr_block_seam.hpp Source FileΒΆ

adc_cpp: include/pops/runtime/builders/block/amr_block_seam.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
amr_block_seam.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <pops/core/state/variables.hpp> // VariableSet/VariableRole/role_from_name (resolve mask)
4#include <pops/runtime/builders/compiled/amr_dsl_block.hpp> // dispatch_amr_block / dispatch_amr_compiled + AmrBuildParams
5#include <pops/runtime/amr/amr_runtime.hpp> // AmrRuntimeBlock + AmrTimeMethod
6#include <pops/runtime/builders/factory/model_factory.hpp> // dispatch_model_for + compiled bricks + ModelSpec
7
8#include <algorithm>
9#include <stdexcept>
10#include <string>
11#include <vector>
12
24
25namespace pops::detail {
26
30inline std::vector<int> resolve_implicit_components_amr(const std::string& block,
31 const VariableSet& cons,
32 const std::vector<std::string>& names,
33 const std::vector<std::string>& roles) {
34 std::vector<int> out;
35 auto push_unique = [&out](int c) {
36 if (std::find(out.begin(), out.end(), c) == out.end())
37 out.push_back(c);
38 };
39 for (const std::string& nm : names) {
40 int idx = -1;
41 for (int i = 0; i < static_cast<int>(cons.names.size()); ++i)
42 if (cons.names[i] == nm) {
43 idx = i;
44 break;
45 }
46 if (idx < 0) {
47 std::string have;
48 for (std::size_t i = 0; i < cons.names.size(); ++i) {
49 if (i)
50 have += ", ";
51 have += cons.names[i];
52 }
53 throw std::runtime_error("AmrSystem::add_block : implicit_vars : variable '" + nm +
54 "' missing from block '" + block +
55 "' (conserved variables : " + have + ")");
56 }
57 push_unique(idx);
58 }
59 for (const std::string& rn : roles) {
60 const VariableRole role = role_from_name(rn);
61 const int idx = cons.index_of(role);
62 if (role == VariableRole::Custom || idx < 0)
63 throw std::runtime_error("AmrSystem::add_block : implicit_roles : role '" + rn +
64 "' missing from block '" + block +
65 "' (the block does not provide this role)");
66 push_unique(idx);
67 }
68 std::sort(out.begin(), out.end());
69 return out;
70}
71
76 std::string name;
77 std::string limiter;
78 std::string riemann;
79 std::vector<double> density;
81 double gamma;
84 bool imex;
85 int stride;
86 std::vector<std::string> implicit_vars;
87 std::vector<std::string> implicit_roles;
89 const std::vector<double>* state; // &BlockSpec::state when has_state, else nullptr (non-owning)
91 int time_method; // 1 == ssprk3 -> AmrTimeMethod::kSsprk3, else kEuler (build_multi mapping)
92 double pos_floor;
93};
94
97template <class TR>
100 dispatch_model_for(a.spec, std::move(tr), [&](auto m) {
101 using M = decltype(m);
102 const std::vector<int> impl_components =
103 a.imex ? resolve_implicit_components_amr(a.name, M::conservative_vars(), a.implicit_vars,
104 a.implicit_roles)
105 : std::vector<int>{};
106 const AmrTimeMethod tmethod =
108 out = dispatch_amr_block(m, a.limiter, a.riemann, S, a.name, a.density, a.has_density, a.gamma,
109 a.substeps, a.recon_prim, a.imex, a.stride, impl_components, a.newton,
110 a.state, a.newton_diagnostics, tmethod, a.pos_floor);
111 });
112 return out;
113}
114
117template <class TR>
118AmrCompiledHooks build_amr_compiled_for(TR tr, const ModelSpec& spec, const std::string& limiter,
119 const std::string& riemann, const AmrBuildParams& bp) {
121 dispatch_model_for(spec, std::move(tr),
122 [&](auto m) { out = dispatch_amr_compiled(m, limiter, riemann, bp); });
123 return out;
124}
125
131template <class TR, class DispatchFn>
133 const SharedAmrLayout& S, DispatchFn dispatch) {
134 AmrRuntimeBlock out;
135 dispatch_model_for(a.spec, std::move(tr), [&](auto m) {
136 using M = decltype(m);
137 const std::vector<int> impl_components =
138 a.imex ? resolve_implicit_components_amr(a.name, M::conservative_vars(), a.implicit_vars,
139 a.implicit_roles)
140 : std::vector<int>{};
141 const AmrTimeMethod tmethod =
142 a.time_method == 1 ? AmrTimeMethod::kSsprk3 : AmrTimeMethod::kEuler;
143 out = dispatch(m, a, S, impl_components, tmethod);
144 });
145 return out;
146}
147
150template <class TR, class DispatchFn>
152 const std::string& limiter, const AmrBuildParams& bp,
153 DispatchFn dispatch) {
155 dispatch_model_for(spec, std::move(tr), [&](auto m) { out = dispatch(m, limiter, bp); });
156 return out;
157}
158
159// Per-transport seam functions (defined in python/amr_block_<transport>.cpp / amr_compiled_<transport>.cpp).
160// TR construction matches dispatch_transport VERBATIM
161// (ExBVelocity{B0}/CompressibleFlux{gamma}/IsothermalFlux{cs2, vacuum_floor}).
165
166AmrCompiledHooks build_amr_compiled_exb(const ModelSpec& spec, const std::string& limiter,
167 const std::string& riemann, const AmrBuildParams& bp);
168AmrCompiledHooks build_amr_compiled_isothermal(const ModelSpec& spec, const std::string& limiter,
169 const std::string& riemann,
170 const AmrBuildParams& bp);
171AmrCompiledHooks build_amr_compiled_compressible(const ModelSpec& spec, const std::string& limiter,
172 const std::string& riemann,
173 const AmrBuildParams& bp);
174
175// ADC-359 per-flux compressible seam leaves: each defined in its own .cpp (build_amr_block_for_flux /
176// build_amr_compiled_for_flux pinned to one flux), so they compile in parallel. The thin dispatchers
177// build_amr_block_compressible / build_amr_compiled_compressible route to them by the riemann string.
179 const SharedAmrLayout& S);
181 const SharedAmrLayout& S);
183 const SharedAmrLayout& S);
185 const SharedAmrLayout& S);
187 const std::string& limiter,
188 const AmrBuildParams& bp);
190 const std::string& limiter,
191 const AmrBuildParams& bp);
193 const std::string& limiter,
194 const AmrBuildParams& bp);
196 const std::string& limiter,
197 const AmrBuildParams& bp);
198
199} // namespace pops::detail
add_compiled_model on the AmrSystem side: wires a COMPILED model (a CompositeModel,...
AMR multi-block engine at RUNTIME (type-erased registry keyed by name).
Assemble a CompositeModel from a ModelSpec (bricks + parameters).
Definition cluster.hpp:37
AmrRuntimeBlock build_amr_block_compressible(const AmrBlockBuildArgs &a, const SharedAmrLayout &S)
AmrRuntimeBlock build_amr_block_for(TR tr, const AmrBlockBuildArgs &a, const SharedAmrLayout &S)
VERBATIM build_multi visitor body with the transport pinned: resolve the partial IMEX mask against th...
Definition amr_block_seam.hpp:98
AmrRuntimeBlock build_amr_block_compressible_roe(const AmrBlockBuildArgs &a, const SharedAmrLayout &S)
AmrCompiledHooks build_amr_compiled_isothermal(const ModelSpec &spec, const std::string &limiter, const std::string &riemann, const AmrBuildParams &bp)
AmrCompiledHooks build_amr_compiled_compressible_hll(const ModelSpec &spec, const std::string &limiter, const AmrBuildParams &bp)
AmrCompiledHooks build_amr_compiled_compressible(const ModelSpec &spec, const std::string &limiter, const std::string &riemann, const AmrBuildParams &bp)
AmrCompiledHooks build_amr_compiled_compressible_rusanov(const ModelSpec &spec, const std::string &limiter, const AmrBuildParams &bp)
AmrCompiledHooks dispatch_amr_compiled(const Model &m, const std::string &lim, const std::string &riem, const AmrBuildParams &bp)
Dispatch of the spatial scheme (limiter x Riemann flux) -> build_amr_compiled.
Definition amr_dsl_block.hpp:953
AmrCompiledHooks build_amr_compiled_for_flux(TR tr, const ModelSpec &spec, const std::string &limiter, const AmrBuildParams &bp, DispatchFn dispatch)
ADC-359 flux subdivision: like build_amr_compiled_for, with the riemann dispatch supplied by dispatch...
Definition amr_block_seam.hpp:151
AmrRuntimeBlock build_amr_block_isothermal(const AmrBlockBuildArgs &a, const SharedAmrLayout &S)
AmrCompiledHooks build_amr_compiled_exb(const ModelSpec &spec, const std::string &limiter, const std::string &riemann, const AmrBuildParams &bp)
POPS_COLD_FN void dispatch_model_for(const ModelSpec &m, TR tr, Visitor &&visitor)
Same as dispatch_model but with the transport brick ALREADY chosen (tr).
Definition model_factory.hpp:196
AmrCompiledHooks build_amr_compiled_compressible_roe(const ModelSpec &spec, const std::string &limiter, const AmrBuildParams &bp)
AmrRuntimeBlock dispatch_amr_block(const Model &m, const std::string &lim, const std::string &riem, const SharedAmrLayout &S, const std::string &name, const std::vector< double > &density, bool has_density, double gamma, int substeps, bool recon_prim, bool imex, int stride=1, const std::vector< int > &implicit_components={}, const NewtonOptions &nopts={}, const std::vector< double > *state=nullptr, bool newton_diagnostics=false, AmrTimeMethod time_method=AmrTimeMethod::kEuler, double pos_floor=0.0)
Dispatch of the spatial scheme (limiter x Riemann flux) -> build_amr_block.
Definition amr_dsl_block.hpp:825
AmrCompiledHooks build_amr_compiled_compressible_hllc(const ModelSpec &spec, const std::string &limiter, const AmrBuildParams &bp)
std::vector< int > resolve_implicit_components_amr(const std::string &block, const VariableSet &cons, const std::vector< std::string > &names, const std::vector< std::string > &roles)
AMR partial-IMEX-mask resolution, moved out of amr_system.cpp's anonymous namespace (ADC-335) so the ...
Definition amr_block_seam.hpp:30
AmrRuntimeBlock build_amr_block_exb(const AmrBlockBuildArgs &a, const SharedAmrLayout &S)
AmrRuntimeBlock build_amr_block_for_flux(TR tr, const AmrBlockBuildArgs &a, const SharedAmrLayout &S, DispatchFn dispatch)
ADC-359 flux subdivision (compressible only): like build_amr_block_for, but the riemann dispatch is s...
Definition amr_block_seam.hpp:132
AmrRuntimeBlock build_amr_block_compressible_hllc(const AmrBlockBuildArgs &a, const SharedAmrLayout &S)
AmrCompiledHooks build_amr_compiled_for(TR tr, const ModelSpec &spec, const std::string &limiter, const std::string &riemann, const AmrBuildParams &bp)
VERBATIM single-block visitor body with the transport pinned: produce the type-erased AmrCompiledHook...
Definition amr_block_seam.hpp:118
AmrRuntimeBlock build_amr_block_compressible_hll(const AmrBlockBuildArgs &a, const SharedAmrLayout &S)
AmrRuntimeBlock build_amr_block_compressible_rusanov(const AmrBlockBuildArgs &a, const SharedAmrLayout &S)
AmrTimeMethod
Definition amr_flux_helpers.hpp:46
VariableRole
PHYSICAL role of a component.
Definition variables.hpp:27
VariableRole role_from_name(const std::string &s)
Forward declaration: VariableSet::index_of(const std::string&) resolves a canonical role NAME via rol...
Definition variables.hpp:130
Frozen parameters passed to the deferred build of the compiled path (add_compiled_model).
Definition amr_system.hpp:84
Type-erased closures of a compiled AMR block, produced by amr_dsl_block::build_amr_compiled and insta...
Definition amr_system.hpp:153
Type-erased closures of ONE AMR block, placed on the shared hierarchy.
Definition amr_runtime.hpp:98
Brick composition of a block plus parameters.
Definition model_spec.hpp:27
Options of the local Newton of the implicit source (backward-Euler).
Definition implicit_stepper.hpp:114
A model's variable set: kind (cons/prim), names, size, canonical roles (optional, parallel to names; ...
Definition variables.hpp:58
std::vector< std::string > names
Definition variables.hpp:60
int index_of(VariableRole role) const
Index of the component carrying role (first occurrence), -1 if absent.
Definition variables.hpp:67
Non-model inputs of a MULTI-block AMR build (the fields the build_multi visitor read off the BlockSpe...
Definition amr_block_seam.hpp:74
bool recon_prim
Definition amr_block_seam.hpp:83
std::string name
Definition amr_block_seam.hpp:76
ModelSpec spec
Definition amr_block_seam.hpp:75
double pos_floor
Definition amr_block_seam.hpp:92
std::string limiter
Definition amr_block_seam.hpp:77
int stride
Definition amr_block_seam.hpp:85
bool has_density
Definition amr_block_seam.hpp:80
std::string riemann
Definition amr_block_seam.hpp:78
const std::vector< double > * state
Definition amr_block_seam.hpp:89
NewtonOptions newton
Definition amr_block_seam.hpp:88
int time_method
Definition amr_block_seam.hpp:91
int substeps
Definition amr_block_seam.hpp:82
bool newton_diagnostics
Definition amr_block_seam.hpp:90
std::vector< std::string > implicit_roles
Definition amr_block_seam.hpp:87
double gamma
Definition amr_block_seam.hpp:81
bool imex
Definition amr_block_seam.hpp:84
std::vector< double > density
Definition amr_block_seam.hpp:79
std::vector< std::string > implicit_vars
Definition amr_block_seam.hpp:86
SHARED layout of a multi-block AMR hierarchy (PR1 capstone), frozen at construction.
Definition amr_dsl_block.hpp:420
Descriptor of a model's variables (Vars).