ufl-units
Physical units and dimensional analysis for UFL expressions and forms.
ufl-units lets you attach sympy units to the constants of a variational form, check that the form
is dimensionally consistent, non-dimensionalise it, and report the dimensionless groups that govern
the problem. It depends on UFL, sympy and numpy only — no form compiler and no assembly backend.
Usage
import basix.ufl
import ufl
import sympy.physics.units as syu
from ufl_units import Quantity
mesh = ufl.Mesh(basix.ufl.element("P", "triangle", 1, shape=(2,)))
V = ufl.FunctionSpace(mesh, basix.ufl.element("P", "triangle", 1))
T, v = ufl.Coefficient(V), ufl.TestFunction(V)
kappa = Quantity(mesh, 1.0, syu.W / (syu.K * syu.m), "kappa")
l_ref = Quantity(mesh, 0.1, syu.m, "l_ref")
T_ref = Quantity(mesh, 300.0, syu.K, "T_ref")
form = ufl.inner(kappa * ufl.grad(T), ufl.grad(v)) * ufl.dx
# The mapping carries the reference length of the mesh and the scale of each field
mapping = {mesh: l_ref, T: T_ref * T, v: T_ref * v}
quantities = ufl_units.collect_quantities(form, mapping=mapping)
ufl_units.buckingham_pi_analysis(quantities)
print(ufl_units.get_dimension(form, quantities, mapping=mapping))
# Strip the dimensional scale off the form, leaving the dimensionless remainder. Terms
# may carry different scales: their ratios are the dimensionless groups of the problem,
# kept in the remainder and reported as exponent vectors over `quantities`
factorized = ufl_units.factorize(form, quantities, mapping=mapping)
print(factorized.expr, factorized.factor, factorized.dimless_numbers)
Binding values
A Quantity is a ufl.Constant and therefore carries no value. To use quantities in an assembled
problem, mix QuantityMixin into the constant type of your backend and override _update_value so
that changes of scale reach the value:
import dolfinx
class Quantity(dolfinx.fem.Constant, ufl_units.QuantityMixin):
def __init__(self, mesh, scale, unit, symbol, unit_system=syu.si.SI, **kwargs):
self._init_units(scale, unit, symbol, unit_system)
dolfinx.fem.Constant.__init__(self, mesh, self.factor, **kwargs)
def _update_value(self, factor):
self.value = factor
# The backend base shadows these, so re-bind them
__str__ = ufl_units.QuantityMixin.__str__
__repr__ = ufl_units.QuantityMixin.__repr__
The dolfinx flavour above ships as ufl_units.backends.dolfinx, and the Firedrake one as
ufl_units.backends.firedrake; each is importable wherever its backend is installed:
from ufl_units.backends.dolfinx import Quantity # Quantity(mesh, scale, unit, symbol)
from ufl_units.backends.firedrake import Quantity # Quantity(scale, unit, symbol)
A Firedrake constant is not tied to a mesh, so that flavour takes no domain; the measure it is integrated against carries the domain instead.
Demos
The demo/ folder holds the examples of the accompanying manuscript. Both need dolfinx, so
run them in the ufl-units (dolfinx) devcontainer (see below), or in any environment with
dolfinx and ufl-units installed:
cd demo
python3 navier_stokes.py
python3 neo_hooke.py
navier_stokes.pyprints the Buckingham Pi analysis and the normalized terms of the incompressible Navier-Stokes equations. It then sweeps the Euler number and writes the condition number of the lid-driven cavity Jacobian tonavier_stokes_condition_number.txt, the data of the conditioning figure. The sweep is set with--eu-min,--eu-maxand--num-eu, and the output file with--output. Run it on a single MPI rank, since the condition number is computed from the dense matrix.neo_hooke.pyprints the Buckingham Pi analysis of Neo-Hooke hyperelasticity, the dimensionless numbers of the standard energy, and the normalized terms of its expansion in the Green-Lagrange strain.
Development
Two devcontainers are provided, so that backend integration can be worked on against either finite element library. Pick one when reopening the repository in a container:
ufl-units (dolfinx), ondolfinx/dolfinxufl-units (firedrake), onfiredrakeproject/firedrake-vanilla-default
Both install ufl-units with --no-deps, leaving the image's own fenics-ufl in place.
The tests themselves need neither library, only fenics-basix for a mesh to hang
function spaces off, and run in any environment with pytest -n auto test/.
License
ufl-units is free software distributed under the terms of the MIT License. See LICENSE for the full text.
Metadata
Release files for ufl-units 1.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| ufl_units-1.0.0.tar.gz | 36.1 kB | Without provenance | Details for ufl_units-1.0.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| ufl_units-1.0.0-py3-none-any.whl | Python 3 | none | any | Details for ufl_units-1.0.0-py3-none-any.whl |
Total release size: 57.6 kB
Release files / ufl_units-1.0.0.tar.gz
| Download URL | ufl_units-1.0.0.tar.gz |
|---|---|
| Size | 36.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
c42f31d966249437b0bff753641883d4adc6c6fcbafea0f16354edda832ca78f
|
|
BLAKE2b-256 checksum How to use checksums |
2c70706c3bb01e79a4e5b33ceaac94ae49d673c96d2e6724f664eccb62bfe777
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.14.7
|
Release files / ufl_units-1.0.0-py3-none-any.whl
| Download URL | ufl_units-1.0.0-py3-none-any.whl |
|---|---|
| Size | 21.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
78532d519d8e2d4315a28c5d57ac02c96243c3d7c05c38fcc235b8f897776cdb
|
|
BLAKE2b-256 checksum How to use checksums |
99f046ba303fb711d61873d03ac583a3cbf11450564b95899e57e4afd1d88403
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.14.7
|
Security for ufl-units 1.0.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.