28inline std::string
num(
double v) {
30 std::snprintf(b,
sizeof b,
"%g", v);
33inline void require(
bool ok,
const char* who,
const std::string& name,
const std::string& rule,
36 throw std::invalid_argument(std::string(who) +
": " + name +
" must be " + rule +
", got " +
40inline void finite(
const char* who,
const std::string& name,
double v,
const char* dim) {
41 require(std::isfinite(v), who, name, std::string(
"finite (") + dim +
")", v);
43inline void positive(
const char* who,
const std::string& name,
double v,
const char* dim) {
44 require(std::isfinite(v) && v > 0.0, who, name, std::string(
"finite and > 0 (") + dim +
")", v);
46inline void nonneg(
const char* who,
const std::string& name,
double v,
const char* dim) {
47 require(std::isfinite(v) && v >= 0.0, who, name, std::string(
"finite and >= 0 (") + dim +
")", v);
50inline void fraction(
const char* who,
const std::string& name,
double v) {
51 require(std::isfinite(v) && v >= 0.0 && v <= 1.0, who, name,
52 "finite and in [0, 1] (dimensionless)", v);
55inline void count(
const char* who,
const std::string& name, std::int64_t v, std::int64_t lo) {
56 require(v >= lo, who, name,
"an integer >= " + std::to_string(lo),
static_cast<double>(v));
59inline std::string
component(
const char* name, std::size_t i) {
60 return std::string(name) +
"[" + std::to_string(i) +
"]";
62template <
class T, std::
size_t N>
63inline void finiteEach(
const char* who,
const char* name,
const std::array<T, N>& a,
65 for (std::size_t i = 0; i <
N; ++i) {
69inline void finiteEach(
const char* who,
const char* name,
const std::vector<double>& a,
71 for (std::size_t i = 0; i < a.size(); ++i) {
75template <
class T, std::
size_t N>
76inline void countEach(
const char* who,
const char* name,
const std::array<T, N>& a,
78 for (std::size_t i = 0; i <
N; ++i) {
86inline void instanceMotion(
const std::array<double, 3>& lin,
const std::array<double, 3>& ang,
87 const std::optional<std::array<double, 3>>& center) {
88 finiteEach(
"set_instance_motion",
"lin_vel", lin,
"length/time");
89 finiteEach(
"set_instance_motion",
"ang_vel", ang,
"1/time");
91 finiteEach(
"set_instance_motion",
"center", *center,
"length");
95inline void instanceTransform(
const std::array<double, 3>& t,
const std::array<double, 4>& q) {
96 finiteEach(
"set_instance_transform",
"translation", t,
"length");
97 finiteEach(
"set_instance_transform",
"quat", q,
"dimensionless");
100inline void vofSeeds(
const std::optional<std::vector<std::array<double, 4>>>& seeds) {
104 for (std::size_t k = 0; k < seeds->size(); ++k) {
105 const auto& c = (*seeds)[k];
106 const std::string s =
"seeds[" + std::to_string(k) +
"]";
107 for (std::size_t i = 0; i < 3; ++i) {
108 finite(
"enable_vof_blocks", s +
"[" + std::to_string(i) +
"]", c[i],
"cells");
110 positive(
"enable_vof_blocks", s +
"[3]", c[3],
"radius, cells");
114inline void propertyTable(
const std::vector<double>& x,
const std::vector<double>& y) {
115 finiteEach(
"set_property_table",
"x", x,
"the field's units");
116 finiteEach(
"set_property_table",
"y", y,
"the target property's units");
119inline void globalDims(
const char* who,
int gnx,
int gny,
int gnz) {
120 count(who,
"gnx", gnx, 1);
121 count(who,
"gny", gny, 1);
122 count(who,
"gnz", gnz, 1);
125inline void scalarArgs(
double diffusivity,
const std::optional<int>& iters) {
126 nonneg(
"add_scalar",
"diffusivity", diffusivity,
"length^2/time");
128 count(
"add_scalar",
"iters", *iters, 1);
137 count(
"Solver",
"nx", nx, 1);
138 count(
"Solver",
"ny", ny, 1);
139 count(
"Solver",
"nz", nz, 1);
140 new (self) S(nx, ny, nz);
145void initPhysical(S* self, std::array<long, 3> cells, std::optional<std::array<double, 3>> extent,
146 std::array<double, 3> origin, std::optional<std::array<long, 3>> global_cells) {
147 for (
int a = 0; a < 3; ++a) {
149 throw std::runtime_error(
"Solver: every cell count must be positive");
152 finiteEach(
"Solver",
"origin", origin,
"length");
154 countEach(
"Solver",
"global_cells", *global_cells, 1);
156 new (self) S(
static_cast<int>(cells[0]),
static_cast<int>(cells[1]),
static_cast<int>(cells[2]));
158 self->setPhysicalDomain(*extent, origin, global_cells ? *global_cells : cells);
163 positive(
"set_rho",
"rho", rho,
"density");
169 nonneg(
"set_mu",
"mu", mu,
"dynamic viscosity");
174 positive(
"set_dt",
"dt", dt,
"time");
180 finite(
"set_velocity_residual_tolerance",
"rtol", rtol,
"dimensionless");
181 s.setVelocityResidualTolerance(rtol);
185 fraction(
"set_backflow_stabilization",
"beta", beta);
186 s.setBackflowStab(beta);
190 count(
"set_pressure_solver_params",
"iters", iters, 1);
191 s.setPressureIterations(iters);
196 count(
"set_pressure_multigrid",
"levels", levels, 0);
197 s.setPressureLevels(levels);
201inline void krylovArgs(
const char* who,
int max_iter,
double rtol) {
202 count(who,
"max_iter", max_iter, 1);
203 nonneg(who,
"rtol", rtol,
"dimensionless");
207 krylovArgs(
"set_pressure_chebyshev", max_iter, rtol);
208 s.setPressureChebyshev(on, max_iter, rtol);
212 krylovArgs(
"set_pressure_pcg", max_iter, rtol);
213 s.setPressurePcg(on, max_iter, rtol);
217 krylovArgs(
"set_pressure_fcg", max_iter, rtol);
218 s.setPressureFcg(on, max_iter, rtol);
223 count(
"set_velocity_multigrid",
"levels", levels, 1);
224 count(
"set_velocity_multigrid",
"vcycles", vcycles, 1);
225 s.setVelocityMultigrid(on, levels, vcycles);
229 finiteEach(
"set_body_force",
"force", force,
"force/volume");
230 s.setBodyForce(force[0], force[1], force[2]);
234 positive(
"set_vof_cfl_limit",
"value",
value,
"Courant number, dimensionless");
235 s.setVofCflLimit(
value);
239 positive(
"set_capillary_cfl",
"factor", factor,
"dimensionless");
240 s.setCapillaryCfl(factor);
245 finite(
"set_mass_flux_uniform",
"mdot", mdot,
"mass/(area time)");
246 s.setMassFluxUniform(mdot);
250 const std::vector<double>& x,
const std::vector<double>& y) {
252 s.setPropertyTable(target, field, x, y);
void instanceMotion(const std::array< double, 3 > &lin, const std::array< double, 3 > &ang, const std::optional< std::array< double, 3 > > ¢er)
set_instance_motion: finite velocities and, when given, a finite rotation center.
void setDt(S &s, double dt)
void scalarArgs(double diffusivity, const std::optional< int > &iters)
add_scalar: a non-negative diffusivity and, when given, at least one sweep.
void setMu(S &s, double mu)
mu = 0 is admitted: the inviscid limit (scripts/pressure_h_independence.py and others use it).
void globalDims(const char *who, int gnx, int gny, int gnz)
init_mpi / the global grid: at least one cell per axis.
std::string num(double v)
void setPressureMultigrid(S &s, bool, int levels)
levels = 0 is the automatic depth.
void setVelocityResidualTolerance(S &s, double rtol)
rtol < 0 follows the pressure solver's rtol and 0 is the legacy criterion: finite only.
void setMassFluxUniform(S &s, double mdot)
The sign is the direction (positive = evaporation): finite only.
void setPropertyTable(S &s, const std::string &target, const std::string &field, const std::vector< double > &x, const std::vector< double > &y)
void setVelocityMultigrid(S &s, bool on, int levels, int vcycles)
The solver used to clamp levels and vcycles below 1 up to 1 silently.
void setPressurePcg(S &s, bool on, int max_iter, double rtol)
void setBodyForce(S &s, std::array< double, 3 > force)
void instanceTransform(const std::array< double, 3 > &t, const std::array< double, 4 > &q)
set_instance_transform: a finite translation and a finite quaternion.
void setRho(S &s, double rho)
void setPressureSolverParams(S &s, int iters)
void finite(const char *who, const std::string &name, double v, const char *dim)
void fraction(const char *who, const std::string &name, double v)
A volume fraction or a dimensionless coefficient bounded by one: [0, 1].
void setPressureChebyshev(S &s, bool on, int max_iter, double rtol)
void countEach(const char *who, const char *name, const std::array< T, N > &a, std::int64_t lo)
void setCapillaryCfl(S &s, double factor)
void initCells(S *self, int nx, int ny, int nz)
Solver(nx, ny, nz): at least one cell per axis (a negative count asked Kokkos for 16 EiB).
void nonneg(const char *who, const std::string &name, double v, const char *dim)
void require(bool ok, const char *who, const std::string &name, const std::string &rule, double v)
std::string component(const char *name, std::size_t i)
Per-component forms for a triple or a quaternion: the parameter is named name[i].
void finiteEach(const char *who, const char *name, const std::array< T, N > &a, const char *dim)
void setPressureFcg(S &s, bool on, int max_iter, double rtol)
void positive(const char *who, const std::string &name, double v, const char *dim)
void propertyTable(const std::vector< double > &x, const std::vector< double > &y)
set_property_table: finite abscissae and values.
void vofSeeds(const std::optional< std::vector< std::array< double, 4 > > > &seeds)
enable_vof_blocks: each seed sphere (cx, cy, cz, radius) in cell units, global indices.
void initPhysical(S *self, std::array< long, 3 > cells, std::optional< std::array< double, 3 > > extent, std::array< double, 3 > origin, std::optional< std::array< long, 3 > > global_cells)
Solver(cells, extent=None, origin=(0, 0, 0), global_cells=None).
void count(const char *who, const std::string &name, std::int64_t v, std::int64_t lo)
A count (cells, levels, iterations): an integer >= lo.
void setBackflowStabilization(S &s, double beta)
void setVofCflLimit(S &s, double value)
void krylovArgs(const char *who, int max_iter, double rtol)
The three pressure drivers share the cap/tolerance pair: >= 1 iteration, rtol >= 0 (0 runs to the cap...
static double value(int gx, int gy, int gz)