flow 1.4.0
Kokkos cut-cell IBM incompressible Navier-Stokes solver + pnm pore extraction
Loading...
Searching...
No Matches
flow_bindings_checks.hpp
Go to the documentation of this file.
1
14#pragma once
15
16#include <array>
17#include <cmath>
18#include <cstddef>
19#include <cstdint>
20#include <cstdio>
21#include <optional>
22#include <stdexcept>
23#include <string>
24#include <vector>
25
27
28inline std::string num(double v) {
29 char b[32];
30 std::snprintf(b, sizeof b, "%g", v);
31 return b;
32}
33inline void require(bool ok, const char* who, const std::string& name, const std::string& rule,
34 double v) {
35 if (!ok) {
36 throw std::invalid_argument(std::string(who) + ": " + name + " must be " + rule + ", got " +
37 num(v));
38 }
39}
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);
42}
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);
45}
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);
48}
49
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);
53}
54
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));
57}
58
59inline std::string component(const char* name, std::size_t i) {
60 return std::string(name) + "[" + std::to_string(i) + "]";
61}
62template <class T, std::size_t N>
63inline void finiteEach(const char* who, const char* name, const std::array<T, N>& a,
64 const char* dim) {
65 for (std::size_t i = 0; i < N; ++i) {
66 finite(who, component(name, i), a[i], dim);
67 }
68}
69inline void finiteEach(const char* who, const char* name, const std::vector<double>& a,
70 const char* dim) {
71 for (std::size_t i = 0; i < a.size(); ++i) {
72 finite(who, component(name, i), a[i], dim);
73 }
74}
75template <class T, std::size_t N>
76inline void countEach(const char* who, const char* name, const std::array<T, N>& a,
77 std::int64_t lo) {
78 for (std::size_t i = 0; i < N; ++i) {
79 count(who, component(name, i), a[i], lo);
80 }
81}
82
83// --- composite checks of one binding lambda ------------------------------------------------
84
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");
90 if (center) {
91 finiteEach("set_instance_motion", "center", *center, "length");
92 }
93}
94
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");
98}
99
100inline void vofSeeds(const std::optional<std::vector<std::array<double, 4>>>& seeds) {
101 if (!seeds) {
102 return;
103 }
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");
109 }
110 positive("enable_vof_blocks", s + "[3]", c[3], "radius, cells");
111 }
112}
113
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");
117}
118
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);
123}
124
125inline void scalarArgs(double diffusivity, const std::optional<int>& iters) {
126 nonneg("add_scalar", "diffusivity", diffusivity, "length^2/time");
127 if (iters) {
128 count("add_scalar", "iters", *iters, 1);
129 }
130}
131
132// --- checked setters (bound in place of a member pointer) -----------------------------------
133
135template <class S>
136void initCells(S* self, int nx, int ny, int nz) {
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);
141}
142
144template <class S>
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) {
148 if (cells[a] <= 0) {
149 throw std::runtime_error("Solver: every cell count must be positive");
150 }
151 }
152 finiteEach("Solver", "origin", origin, "length");
153 if (global_cells) {
154 countEach("Solver", "global_cells", *global_cells, 1);
155 }
156 new (self) S(static_cast<int>(cells[0]), static_cast<int>(cells[1]), static_cast<int>(cells[2]));
157 if (extent) {
158 self->setPhysicalDomain(*extent, origin, global_cells ? *global_cells : cells);
159 }
160}
161template <class S>
162void setRho(S& s, double rho) {
163 positive("set_rho", "rho", rho, "density");
164 s.setRho(rho);
165}
166
167template <class S>
168void setMu(S& s, double mu) {
169 nonneg("set_mu", "mu", mu, "dynamic viscosity");
170 s.setMu(mu);
171}
172template <class S>
173void setDt(S& s, double dt) {
174 positive("set_dt", "dt", dt, "time");
175 s.setDt(dt);
176}
177
178template <class S>
179void setVelocityResidualTolerance(S& s, double rtol) {
180 finite("set_velocity_residual_tolerance", "rtol", rtol, "dimensionless");
181 s.setVelocityResidualTolerance(rtol);
182}
183template <class S>
184void setBackflowStabilization(S& s, double beta) {
185 fraction("set_backflow_stabilization", "beta", beta);
186 s.setBackflowStab(beta);
187}
188template <class S>
189void setPressureSolverParams(S& s, int iters) {
190 count("set_pressure_solver_params", "iters", iters, 1);
191 s.setPressureIterations(iters);
192}
193
194template <class S>
195void setPressureMultigrid(S& s, bool, int levels) {
196 count("set_pressure_multigrid", "levels", levels, 0);
197 s.setPressureLevels(levels);
198}
199
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");
204}
205template <class S>
206void setPressureChebyshev(S& s, bool on, int max_iter, double rtol) {
207 krylovArgs("set_pressure_chebyshev", max_iter, rtol);
208 s.setPressureChebyshev(on, max_iter, rtol);
209}
210template <class S>
211void setPressurePcg(S& s, bool on, int max_iter, double rtol) {
212 krylovArgs("set_pressure_pcg", max_iter, rtol);
213 s.setPressurePcg(on, max_iter, rtol);
214}
215template <class S>
216void setPressureFcg(S& s, bool on, int max_iter, double rtol) {
217 krylovArgs("set_pressure_fcg", max_iter, rtol);
218 s.setPressureFcg(on, max_iter, rtol);
219}
220
221template <class S>
222void setVelocityMultigrid(S& s, bool on, int levels, int vcycles) {
223 count("set_velocity_multigrid", "levels", levels, 1);
224 count("set_velocity_multigrid", "vcycles", vcycles, 1);
225 s.setVelocityMultigrid(on, levels, vcycles);
226}
227template <class S>
228void setBodyForce(S& s, std::array<double, 3> force) {
229 finiteEach("set_body_force", "force", force, "force/volume");
230 s.setBodyForce(force[0], force[1], force[2]);
231}
232template <class S>
233void setVofCflLimit(S& s, double value) {
234 positive("set_vof_cfl_limit", "value", value, "Courant number, dimensionless");
235 s.setVofCflLimit(value);
236}
237template <class S>
238void setCapillaryCfl(S& s, double factor) {
239 positive("set_capillary_cfl", "factor", factor, "dimensionless");
240 s.setCapillaryCfl(factor);
241}
242
243template <class S>
244void setMassFluxUniform(S& s, double mdot) {
245 finite("set_mass_flux_uniform", "mdot", mdot, "mass/(area time)");
246 s.setMassFluxUniform(mdot);
247}
248template <class S>
249void setPropertyTable(S& s, const std::string& target, const std::string& field,
250 const std::vector<double>& x, const std::vector<double>& y) {
251 propertyTable(x, y);
252 s.setPropertyTable(target, field, x, y);
253}
254
255} // namespace peclet::flow::binding_checks
void instanceMotion(const std::array< double, 3 > &lin, const std::array< double, 3 > &ang, const std::optional< std::array< double, 3 > > &center)
set_instance_motion: finite velocities and, when given, a finite rotation center.
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.
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 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 constexpr int N
static double value(int gx, int gy, int gz)