flow 1.4.0
Kokkos cut-cell IBM incompressible Navier-Stokes solver + pnm pore extraction
Loading...
Searching...
No Matches
test_input_validation.py
Go to the documentation of this file.
1"""Contract: the public setters refuse a NaN/Inf or physically inadmissible value with ValueError.
2
3Validation lives in the binding (src/flow_bindings_checks.hpp, used from src/flow_bindings.cpp);
4the message names the setter, the parameter, the admissible range with its dimension and the
5offending value. Each validated argument is covered on both solvers twice: every bad value raises
6ValueError naming the parameter, and one valid value is accepted (where a call needs a fixture
7this file does not build -- a scene, a wetting wall -- the valid value is shown to pass validation
8by the call reaching the C++ check behind it, which raises RuntimeError instead).
9
10Where a sign or a 0 is a switch the check is "finite" only (set_velocity_residual_tolerance: < 0
11follows the pressure rtol; set_contact_angle_dynamic sigma: <= 0 takes set_surface_tension's;
12set_vof_block_assign every: 0 never re-assigns; set_mass_flux_uniform: the sign is the direction).
13
14Run: PYTHONPATH=<build> python -m pytest -q tests/python/test_input_validation.py
15"""
16import math
17import re
18
19import numpy as np
20import pytest
21
22from peclet import flow
23
24NAN, INF = math.nan, math.inf
25N = 8
26CLASSES = [flow.Solver, flow.SolverColocated]
27
28
29def _box(cls):
30 return cls((N, N, N), extent=(1.0, 1.0, 1.0))
31
32
33def _fluid(cls, **bc):
34 """A physical all-fluid box with properties and a geometry (what add_scalar / VoF need)."""
35 s = _box(cls)
36 s.set_rho(1.0)
37 s.set_mu(0.1)
38 s.set_dt(0.01)
39 for face, (kind, vel) in bc.items():
40 s.set_domain_bc(face, kind, vel)
41 s.set_pressure_geometry(np.full((N, N, N), 10.0, order="F"))
42 return s
43
44
45def _scalar(cls):
46 s = _fluid(cls)
47 s.add_scalar("c", 0.1)
48 return s
49
50
51def _vof(cls):
52 s = _fluid(cls)
53 s.enable_vof()
54 return s
55
56
57def _vof_open(cls):
58 """VoF with an inflow -x face and an outflow +x face."""
59 s = _fluid(cls, **{"-x": ("inflow", (0.1, 0.0, 0.0)), "+x": ("outflow", (0.0, 0.0, 0.0))})
60 s.enable_vof()
61 return s
62
63
65 """Phase change is staggered-only: SolverColocated gets VoF alone (its setter then refuses)."""
66 s = _vof(cls)
67 if cls is flow.Solver:
68 s.enable_phase_change(1.0, 10.0)
69 return s
70
71
72def _blocks(cls):
73 s = _vof(cls)
74 s.enable_vof_blocks([(4.0, 4.0, 4.0, 2.0)])
75 return s
76
77
78def _staggered_only(match):
79 """Accepted on Solver; on SolverColocated past validation into the solver's refusal."""
80 return lambda cls: None if cls is flow.Solver else (RuntimeError, match)
81
82
83_NO_SCENE = (RuntimeError, "set_scene first") # past validation, refused by the solver
84_NO_WALL = (RuntimeError, "wetting wall")
85
86# (id, call(cls, v), bad values, good value, parameter as the message names it, good outcome)
87# `good outcome` None = the call returns; (exc, regex) = it gets past validation into a C++ check;
88# a callable maps the class to one of those.
89CASES = [
90 ("Solver.nx", lambda c, v: c(v, N, N), [-1, 0], N, "nx", None),
91 ("Solver.nz", lambda c, v: c(N, N, v), [0], N, "nz", None),
92 ("Solver.origin", lambda c, v: c((N, N, N), extent=(1.0, 1.0, 1.0), origin=(0.0, v, 0.0)),
93 [NAN, INF], -0.5, "origin[1]", None),
94 ("Solver.global_cells",
95 lambda c, v: c((N, N, N), extent=(1.0, 1.0, 1.0), global_cells=(N, N, v)), [-1, 0], N,
96 "global_cells[2]", None),
97 ("set_rho", lambda c, v: _box(c).set_rho(v), [NAN, INF, 0.0, -1.0], 1000.0, "rho", None),
98 ("set_mu", lambda c, v: _box(c).set_mu(v), [NAN, INF, -1e-3], 0.0, "mu", None),
99 ("set_dt", lambda c, v: _box(c).set_dt(v), [NAN, INF, 0.0, -1e-3], 1e-3, "dt", None),
100 ("set_body_force", lambda c, v: _box(c).set_body_force((0.0, v, 0.0)), [NAN, INF], -9.81,
101 "force[1]", None),
102 ("set_superficial_velocity", lambda c, v: _box(c).set_superficial_velocity(True, "x", v),
103 [NAN, INF], 0.5, "velocity", _staggered_only("staggered Solver only")),
104 ("set_velocity_residual_tolerance", lambda c, v: _box(c).set_velocity_residual_tolerance(v),
105 [NAN, INF], -1.0, "rtol", None),
106 ("set_backflow_stabilization", lambda c, v: _box(c).set_backflow_stabilization(v),
107 [NAN, -0.1, 1.5], 0.2, "beta", None),
108 ("set_pressure_solver_params", lambda c, v: _box(c).set_pressure_solver_params(v), [-1, 0],
109 20, "iters", None),
110 ("set_pressure_multigrid", lambda c, v: _box(c).set_pressure_multigrid(True, v), [-1], 0,
111 "levels", None),
112 ("set_pressure_chebyshev.max_iter", lambda c, v: _box(c).set_pressure_chebyshev(True, v),
113 [-1, 0], 120, "max_iter", None),
114 ("set_pressure_chebyshev.rtol", lambda c, v: _box(c).set_pressure_chebyshev(True, 120, v),
115 [NAN, -1e-9], 0.0, "rtol", None),
116 ("set_pressure_pcg.max_iter", lambda c, v: _box(c).set_pressure_pcg(True, v), [-1, 0], 200,
117 "max_iter", None),
118 ("set_pressure_pcg.rtol", lambda c, v: _box(c).set_pressure_pcg(True, 200, v), [NAN, INF],
119 1e-8, "rtol", None),
120 ("set_pressure_fcg.max_iter", lambda c, v: _box(c).set_pressure_fcg(True, v), [-1, 0], 200,
121 "max_iter", None),
122 ("set_pressure_fcg.rtol", lambda c, v: _box(c).set_pressure_fcg(True, 200, v), [NAN, -1.0],
123 1e-8, "rtol", None),
124 ("set_velocity_multigrid.levels", lambda c, v: _box(c).set_velocity_multigrid(True, v),
125 [-1, 0], 4, "levels", None),
126 ("set_velocity_multigrid.vcycles", lambda c, v: _box(c).set_velocity_multigrid(True, 4, v),
127 [-1, 0], 8, "vcycles", None),
128 ("set_domain_bc", lambda c, v: _box(c).set_domain_bc("-x", "inflow", (v, 0.0, 0.0)),
129 [NAN, INF], 0.1, "velocity[0]", None),
130 ("set_instance_motion.lin_vel", lambda c, v: _box(c).set_instance_motion(0, (v, 0.0, 0.0)),
131 [NAN, INF], 0.1, "lin_vel[0]", _NO_SCENE),
132 ("set_instance_motion.ang_vel",
133 lambda c, v: _box(c).set_instance_motion(0, ang_vel=(0.0, 0.0, v)), [NAN], 1.0,
134 "ang_vel[2]", _NO_SCENE),
135 ("set_instance_motion.center",
136 lambda c, v: _box(c).set_instance_motion(0, center=(v, 0.5, 0.5)), [INF], 0.5, "center[0]",
137 _NO_SCENE),
138 ("set_instance_transform.translation",
139 lambda c, v: _box(c).set_instance_transform(0, (0.0, v, 0.0)), [NAN], 0.1, "translation[1]",
140 _NO_SCENE),
141 ("set_instance_transform.quat",
142 lambda c, v: _box(c).set_instance_transform(0, (0.0, 0.0, 0.0), (0.0, 0.0, 0.0, v)), [NAN],
143 1.0, "quat[3]", _NO_SCENE),
144 ("add_scalar.diffusivity", lambda c, v: _fluid(c).add_scalar("c", v), [NAN, -1.0], 0.0,
145 "diffusivity", None),
146 ("add_scalar.iters", lambda c, v: _fluid(c).add_scalar("c", 0.1, iters=v), [-1, 0], 1,
147 "iters", None),
148 ("set_scalar_bc", lambda c, v: _scalar(c).set_scalar_bc("c", "-x", "dirichlet", v),
149 [NAN, INF], 1.0, "value", None),
150 ("set_vof_cfl_limit", lambda c, v: _box(c).set_vof_cfl_limit(v), [NAN, 0.0, -0.25], 0.25,
151 "value", None),
152 ("set_capillary_cfl", lambda c, v: _vof(c).set_capillary_cfl(v), [NAN, 0.0, -1.0], 0.5,
153 "factor", None),
154 ("set_mass_flux_uniform", lambda c, v: _phase_change(c).set_mass_flux_uniform(v), [NAN, INF],
155 -0.1, "mdot", _staggered_only("phase change is not enabled")),
156 ("set_vof_inflow", lambda c, v: _vof_open(c).set_vof_inflow("-x", v), [NAN, -0.5, 1.5], 0.5,
157 "value", None),
158 ("set_vof_backflow", lambda c, v: _vof_open(c).set_vof_backflow("+x", v), [NAN, 1.5], 1.0,
159 "value", None),
160 ("enable_vof_blocks.center", lambda c, v: _vof(c).enable_vof_blocks([(v, 4.0, 4.0, 2.0)]),
161 [NAN], 4.0, "seeds[0][0]", None),
162 ("enable_vof_blocks.radius", lambda c, v: _vof(c).enable_vof_blocks([(4.0, 4.0, 4.0, v)]),
163 [NAN, 0.0, -1.0], 2.0, "seeds[0][3]", None),
164 ("set_vof_block_assign", lambda c, v: _blocks(c).set_vof_block_assign("lpt", v), [-1], 0,
165 "every", None),
166 ("set_contact_angle_dynamic.sigma",
167 lambda c, v: _vof(c).set_contact_angle_dynamic(True, 90.0, 0.05, 1.0, v), [NAN, INF], 0.0,
168 "sigma", _NO_WALL),
169 ("set_property_model", lambda c, v: _vof(c).set_property_model("mu", "linear", "C", (v, 1.0)),
170 [NAN, INF], 0.1, "params[0]", None),
171 ("set_property_table.x",
172 lambda c, v: _vof(c).set_property_table("mu", "C", (0.0, v), (0.1, 1.0)), [NAN], 1.0, "x[1]",
173 None),
174 ("set_property_table.y",
175 lambda c, v: _vof(c).set_property_table("mu", "C", (0.0, 1.0), (v, 1.0)), [INF], 0.1, "y[0]",
176 None),
177]
178if hasattr(flow.Solver, "init_mpi"): # bound under PECLET_FLOW_MPI; a valid call would MPI_Init
179 CASES.append(("init_mpi", lambda c, v: _box(c).init_mpi(N, v, N), [-1, 0], None, "gny", None))
180
181_BAD = [(cls, c[0], c[1], b, c[4]) for cls in CLASSES for c in CASES for b in c[2]]
182_GOOD = [(cls, c[0], c[1], c[3], c[5]) for cls in CLASSES for c in CASES if c[3] is not None]
183
184
185@pytest.mark.parametrize("cls,case,call,value,param", _BAD,
186 ids=[f"{c[0].__name__}.{c[1]}={c[3]}" for c in _BAD])
188 with pytest.raises(ValueError) as err:
189 call(cls, value)
190 msg = str(err.value)
191 assert re.search(rf"{re.escape(param)} must be ", msg), msg
192 assert "got" in msg, msg
193
194
195@pytest.mark.parametrize("cls,case,call,value,outcome", _GOOD,
196 ids=[f"{c[0].__name__}.{c[1]}" for c in _GOOD])
197def test_valid_value_is_accepted(cls, case, call, value, outcome):
198 if callable(outcome):
199 outcome = outcome(cls)
200 if outcome is None:
201 call(cls, value)
202 else:
203 exc, match = outcome
204 with pytest.raises(exc, match=match):
205 call(cls, value)
206
207
209 s = _box(flow.Solver)
210 with pytest.raises(ValueError, match=r"^set_rho: rho must be finite and > 0 \‍(density\‍), "
211 r"got -1$"):
212 s.set_rho(-1.0)
213 with pytest.raises(ValueError, match=r"^set_body_force: force\‍[2\‍] must be finite "
214 r"\‍(force/volume\‍), got nan$"):
215 s.set_body_force((0.0, 0.0, NAN))
216 with pytest.raises(ValueError, match=r"^set_backflow_stabilization: beta must be finite and "
217 r"in \‍[0, 1\‍] \‍(dimensionless\‍), got 1.5$"):
218 s.set_backflow_stabilization(1.5)
219 with pytest.raises(ValueError, match=r"^set_pressure_pcg: max_iter must be an integer >= 1, "
220 r"got 0$"):
221 s.set_pressure_pcg(True, 0)
222
223
224@pytest.mark.parametrize("cls", CLASSES, ids=lambda c: c.__name__)
226 s = _box(cls)
227 s.set_vof_cfl_limit(0.3)
228 s.set_velocity_residual_tolerance(1e-6)
229 with pytest.raises(ValueError):
230 s.set_vof_cfl_limit(NAN)
231 with pytest.raises(ValueError):
232 s.set_velocity_residual_tolerance(INF)
233 assert s.vof_cfl_limit() == 0.3
234 assert s.velocity_residual_tolerance() == 1e-6
235
236
238 """The cells check predates this file: RuntimeError, message unchanged."""
239 with pytest.raises(RuntimeError, match="every cell count must be positive"):
240 flow.Solver((0, N, N), extent=(1.0, 1.0, 1.0))
test_valid_value_is_accepted(cls, case, call, value, outcome)
test_bad_value_raises_value_error_naming_the_parameter(cls, case, call, value, param)