jaxmat.utils module#

assert_max_traces(max_traces=1)[source]#

Decorator asserting that a function is traced at most max_traces times, only when the check_max_traces option of jaxmat.config is on (it is off by default).

It is equinox.debug.assert_max_traces() behind a switch, and is placed the same way, inside the JIT wrapper:

@eqx.filter_jit
@assert_max_traces(max_traces=1)
def constitutive_update(self, eps, state, dt): ...

The option is read at trace time, so it can be switched at any point. Only traces happening while it is on are counted, and the count is shared by all instances.

Parameters:

max_traces (int)

initial_value(default, *, dtype=<class 'jax.numpy.float64'>, **kwargs)[source]#

Declare an internal-state initial value.

Cast to dtype like parameter(), but deliberately not parameter-capable: internal-state fields (accumulated plastic strain, viscous strain, back-stresses, …) hold plain numerical data, never a material law. Passing a parameter/module here is a modelling error and is rejected.

enforce_dtype(dtype=<class 'jax.numpy.float64'>, **kwargs)[source]#
default_value(value, dtype=<class 'jax.numpy.float64'>, **kwargs)[source]#

Deprecated alias for initial_value() (internal-state field).

partition_by_node_names(model, freeze_names)[source]#

Partition an equinox model into (trainable, static) where attributes listed in freeze_names are frozen (moved to static).

print_eqx_fields(obj, fields=None, indent=0, format='')[source]#

Recursively print fields of an equinox module or dataclass-like object.

Constrained parameters (see jaxmat.parameters) are printed with their physical values, not with their latent representation.

Parameters:
  • obj (eqx.Module) – The module or object to inspect.

  • fields (list) – Optional list of field names (strings) to print. Supports nested paths like ["layer1", "layer2.weight"]. If None, prints all fields recursively.

  • indent (int) – Internal indentation level (used for recursion).

  • format (str, dict or list) – Format specification (syntax of str.format) used to print numbers and numeric arrays, e.g. ".3e"; other values are printed as they are. A dict gives a specification per field name (for a nested module, a specification or a dict for its own fields), a list one per element of a list or tuple.

save_model(filename, model, hyperparameters=None)[source]#

Serialize and save an Equinox Module in a file.

The file starts with a one-line JSON header (file format, jaxmat version, model class, hyperparameters and paths of the saved leaves), followed by the array and number leaves of the model, written with equinox.tree_serialise_leaves(). Only these leaves are saved: the structure of the model, its static fields (e.g. solvers) and functions (e.g. the transforms of constrained parameters) come from the template given to load_model(). Constrained parameters are saved with their latent values.

Parameters:
  • filename (str or pathlib.Path) – Path of the file to save the module.

  • model (eqx.Module) – Model to save, e.g. a calibrated material or a material state.

  • hyperparameters (dict, optional) – Hyperparameters of the model, that is parameters that describe the shape of the associated PyTree (e.g. a number of branches or layers). They must be JSON serializable, and are passed to the factory given to load_model().

Return type:

None

load_model(filename, skeleton)[source]#

Load a serialized model saved with save_model().

Parameters:
  • filename (str or pathlib.Path) – Path of the file to load the module from.

  • skeleton (eqx.Module or Callable) – Either an equinox module with the same structure as the saved model, whose leaf values are replaced by the saved ones, or a function that takes the saved hyperparameters as keyword arguments and creates such a module.

Raises:

ValueError – If the file was not written by save_model(), if it holds a model of another class than skeleton, or if its leaves do not match those of the template, e.g. a model saved with constrained parameters unwrapped by paramax.unwrap.

Return type:

Module