spectral_galerkin_heat.core.property_expr
Whitelisted math expressions of T for temperature-dependent properties.
A property branch (e.g. k.solid) is given in config as either a bare
number (a constant) or a string expression of T such as
"9.248 + 0.01571 * T" or "12.41 * exp(-T / 3000.0)". PropertyExpr
parses that string once against a small node/name/function whitelist using
Python’s ast module. It never calls eval or exec on the expression
itself, so there is no sandbox-escape surface. (A restricted-namespace eval
would be escapable through attribute access on any in-scope object.)
The validated AST is then rendered to three targets, so the temperature-dependent hot path stays fully fused rather than falling back to a per-property Python callback:
PropertyExpr.__call__(), a NumPy/CuPy-broadcasting evaluator that works on a scalar or a full field, on host or device;PropertyExpr.to_py_source(), a numba-njit-compatible Python source snippet usingmath.<fn>calls, which numba compiles natively, to build the fused CPU kernel;PropertyExpr.to_c_source(), a CUDA C expression, to build the fused GPUElementwiseKernel.
Both source renderers take the target precision, since the literals they emit are what fixes the precision of the generated kernel.
Classes
A validated math expression of |
Module Contents
- class spectral_galerkin_heat.core.property_expr.PropertyExpr(expr)
A validated math expression of
T(or a bare constant).str(expr)must either parse as a plain number (a constant) or as an expression usingT,+ - * / **, the constantspi/e, and calls to a fixed whitelist of math functions (sqrt,exp,log, trig/hyperbolic functions, and so on). Anything else raisesValueErrorat construction time: attribute access, subscripts, comprehensions, and unknown names or functions.- to_py_source(dtype=np.float32)
Return numba-
njit-compatible Python source withTfree.- Parameters:
dtype (data-type, optional) – Precision of the emitted literals,
numpy.float32(default) ornumpy.float64.- Return type:
str
- to_c_source(dtype=np.float32)
Return a CUDA C expression with
Tfree.- Parameters:
dtype (data-type, optional) – Precision of the emitted literals and intrinsics,
numpy.float32(default) ornumpy.float64.- Return type:
str