neml2.factory

Python-native HIT input file factory.

Mirrors the C++ Factory / Registry pattern for Python-native models:

  • register_neml2_object() — decorator that registers a model class under its C++ type name. Model subclasses usually inherit schema-backed from_hit(node, factory); non-model objects and special cases implement their own.

  • load_input() — parse a HIT file and return a lazy _NativeInputFile factory object.

  • load_model() — convenience wrapper: load_input(path).get_model(name).

The factory is “dumb” — it dispatches only by type name. Each registered class is responsible for reading its own parameters and resolving sub-object dependencies via the factory’s get_model / get_solver / get_equation_system methods (mirroring NEML2Object::get_model etc.).

neml2.factory.load_input(path, *, pre=(), post=(), additional_args=())[source]

Parse a HIT input file and return a lazy native factory.

Parameters:
  • path (str | Path) – Path to the HIT .i file.

  • pre (Sequence[str]) – Optional HIT snippets prepended / appended before parsing (same semantics as nmhit.parse_file).

  • post (Sequence[str]) – Optional HIT snippets prepended / appended before parsing (same semantics as nmhit.parse_file).

  • additional_args (Sequence[str]) – Trailing command-line HIT overrides (e.g. ["Models/elasticity/E:=210000"]). Each element is a HIT snippet appended after post so any := override takes effect after the file’s own assignments. Mirrors the C++ side’s neml2::load_input(path, additional_cliargs).

Return type:

_NativeInputFile

neml2.factory.load_model(path, model_name)[source]

Load a named model from a HIT input file as a Python-native model.

Raises KeyError if the model’s type (or any of its sub-object types) isn’t registered in NativeRegistry.

Parameters:
  • path (str | Path) – Path to the HIT .i file.

  • model_name (str) – Name of the model in the [Models] section.

Return type:

Any

neml2.factory.load_nonlinear_system(path, name)[source]

Load a named system from [EquationSystems] as a Python-native object.

Convenience wrapper around load_input(path).get_equation_system(name) — mirrors load_model(). Returns a NonlinearSystem (typically a ModelNonlinearSystem).

Parameters:
Return type:

Any

neml2.factory.load_string(text, *, pre=(), post=(), additional_args=())[source]

Parse an in-memory HIT snippet and return a lazy native factory.

Same semantics as load_input() but reads from a string (via nmhit.parse_text) instead of a file. Used by neml2.drivers.ModelUnitTest.from_string() so a unit test can embed its [Models] / [Tensors] / [Drivers] blocks inline.

Parameters:
Return type:

_NativeInputFile

neml2.factory.register_neml2_object(type_name)[source]

Decorator: register a Model (or solver/system) class under type_name.

The class must provide a from_hit(cls, node, factory) classmethod that constructs an instance from the given nmhit Section node. Model subclasses inherit a default implementation backed by HitSchema; other registered object types implement it directly. The factory argument is a _NativeInputFile instance whose get_model, get_solver, and get_equation_system methods may be called to resolve named sub-objects — exactly as C++ NEML2Object::get_model() does.

Parameters:

type_name (str)

Return type:

Callable[[type[_T]], type[_T]]