NEML2 1.4.0
Loading...
Searching...
No Matches
Python Package

Note
The NEML2 Python package is experimental. APIs are expected to change.

Load and evaluate a model from input file

With the NEML2 Python package, the same input file in the other tutorial can be directly used in a Python script. The Python APIs closely ressembles the C++ APIs. For example, the previous C++ example translates to the following Python script.

import neml2
model = neml2.load_model("input.i", "model")
model.reinit(3)
x = neml2.LabeledVector.empty(3, [model.input_axis()])
x.batch[0] = neml2.SR2.fill([0.1, 0.2, 0.3, -0.1, -0.1, 0.2])
x.batch[1] = neml2.SR2.fill([0.2, 0.2, 0.1, -0.1, -0.2, -0.5])
x.batch[2] = neml2.SR2.fill([0.3, -0.2, 0.05, -0.1, -0.3, 0.1])
y = model.value(x)
static LabeledVector empty(TensorShapeRef batch_shape, const std::array< const LabeledAxis *, D > &axes, const torch::TensorOptions &options=default_tensor_options())
Setup new empty storage.
Definition LabeledTensor.cxx:97
static SR2 fill(const Real &a, const torch::TensorOptions &options=default_tensor_options())
Fill the diagonals with a11 = a22 = a33 = a.
Definition SR2.cxx:46
Model & load_model(const std::filesystem::path &path, const std::string &mname, bool enable_ad)
A convenient function to load an input file and get a model.
Definition Factory.cxx:69

All is the same in the equivalent C++ example, the above Python script parses the input file named "input.i", loads the linear elasticity model named "model", constructs 3 strain tensors, and finally performs the 3 material updates simultaneously.