Overview

moju is a physics supervision and audit library for SciML. The core is JAX-native: moju.piratio provides Groups, Models, Laws, and Operators, while moju.monitor provides ResidualEngine, build_loss, audit, and visualize. The moju.torch namespace exposes a PyTorch-facing engine and interop helpers.

Installation

pip install moju                 # residuals, audit, visualize, PDF
pip install "moju[io]"           # + state_ref file loaders
pip install "moju[studio]"       # Streamlit Moju Studio
pip install "moju[torch,io]"     # PyTorch + file loaders

API at a glance

High-level architecture

User config and state dictionaries flow through ResidualEngine. Groups materialize dimensionless quantities into merged state. Laws produce governing residuals. Constitutive audits compare catalog models against references or implied values. Optional data/ residuals run in eval mode when state_ref is supplied. build_loss uses law residuals for optimization; audit and visualize summarize the full log.

flowchart TB subgraph config [User config] Laws[Laws specs] Groups[Groups specs] Constitutive[Constitutive audits] Constants[Constants] end subgraph inputs [Inputs] StatePred[state_pred] StateRef[state_ref optional] end subgraph engine [ResidualEngine] Merge[Merge state constants groups] LawResiduals[Laws residuals] ConstitutiveResiduals[Constitutive residuals] DataResiduals[data residuals in eval] Log[Audit log] end StatePred --> Merge Constants --> Merge Groups --> Merge Merge --> LawResiduals Merge --> ConstitutiveResiduals StateRef --> DataResiduals Laws --> LawResiduals Constitutive --> ConstitutiveResiduals LawResiduals --> Log ConstitutiveResiduals --> Log DataResiduals --> Log LawResiduals --> BuildLoss[build_loss] Log --> Audit[audit] Log --> Visualize[visualize]

Training and monitoring (ResidualEngine)

Path A builds state_pred from (model, params, collocation) using a state_builder. Path B passes state_pred directly, which is convenient for CFD snapshots, saved neural-operator fields, or already-computed derivatives.

In the minimal 1D slab cooling example, Path B supplies T, T_t, T_laplacian, x, t, L, and material properties directly. No finite-difference inference is needed. The Fourier conduction law runs from those fields, and the default law-linked implied audit checks Models.thermal_diffusivity(k, rho, cp) against the diffusivity implied by the law fields.

Use monitor_training_vs_eval.md for run-mode details and law_implied_audits.md for the law-linked constitutive audit registry.

PyTorch support

Install moju[torch] to use the moju.torch namespace. It provides TorchResidualEngine, build_loss_torch, r_eff_scalar_torch, Torch-native nondimensionalization helpers, and wrap_law_torch for calling JAX laws from Torch tensors through jax2torch.

See scripts/torch_laws_jax2torch_example.py, tests/test_torch_engine.py, and tests/test_torch_interop.py for the current public surface.

Examples and guides