1"""Contract: the public setters refuse a NaN/Inf or physically inadmissible value with ValueError.
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).
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).
14Run: PYTHONPATH=<build> python -m pytest -q tests/python/test_input_validation.py
22from peclet
import flow
24NAN, INF = math.nan, math.inf
30 return cls((N, N, N), extent=(1.0, 1.0, 1.0))
34 """A physical all-fluid box with properties and a geometry (what add_scalar / VoF need)."""
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"))
47 s.add_scalar(
"c", 0.1)
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))})
65 """Phase change is staggered-only: SolverColocated gets VoF alone (its setter then refuses)."""
68 s.enable_phase_change(1.0, 10.0)
74 s.enable_vof_blocks([(4.0, 4.0, 4.0, 2.0)])
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)
83_NO_SCENE = (RuntimeError,
"set_scene first")
84_NO_WALL = (RuntimeError,
"wetting wall")
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,
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],
110 (
"set_pressure_multigrid",
lambda c, v:
_box(c).set_pressure_multigrid(
True, v), [-1], 0,
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,
118 (
"set_pressure_pcg.rtol",
lambda c, v:
_box(c).set_pressure_pcg(
True, 200, v), [NAN, INF],
120 (
"set_pressure_fcg.max_iter",
lambda c, v:
_box(c).set_pressure_fcg(
True, v), [-1, 0], 200,
122 (
"set_pressure_fcg.rtol",
lambda c, v:
_box(c).set_pressure_fcg(
True, 200, v), [NAN, -1.0],
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]",
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]",
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,
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,
152 (
"set_capillary_cfl",
lambda c, v:
_vof(c).set_capillary_cfl(v), [NAN, 0.0, -1.0], 0.5,
154 (
"set_mass_flux_uniform",
lambda c, v:
_phase_change(c).set_mass_flux_uniform(v), [NAN, INF],
156 (
"set_vof_inflow",
lambda c, v:
_vof_open(c).set_vof_inflow(
"-x", v), [NAN, -0.5, 1.5], 0.5,
158 (
"set_vof_backflow",
lambda c, v:
_vof_open(c).set_vof_backflow(
"+x", v), [NAN, 1.5], 1.0,
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,
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,
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]",
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]",
179 CASES.append((
"init_mpi",
lambda c, v:
_box(c).init_mpi(N, v, N), [-1, 0],
None,
"gny",
None))
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]
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:
191 assert re.search(rf
"{re.escape(param)} must be ", msg), msg
192 assert "got" in msg, msg
195@pytest.mark.parametrize(
"cls,case,call,value,outcome", _GOOD,
196 ids=[f
"{c[0].__name__}.{c[1]}" for c
in _GOOD])
198 if callable(outcome):
199 outcome = outcome(cls)
204 with pytest.raises(exc, match=match):
210 with pytest.raises(ValueError, match=
r"^set_rho: rho must be finite and > 0 \(density\), "
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, "
221 s.set_pressure_pcg(
True, 0)
224@pytest.mark.parametrize("cls", CLASSES, ids=lambda c: c.__name__)
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
238 """The cells check predates this file: RuntimeError, message unchanged."""
239 with pytest.raises(RuntimeError, match=
"every cell count must be positive"):