NEML2 2.0.0
|
All NEML2 models can be written in the following form
Defining a custom model therefore involves
The first three are introduced in this tutorial.
The API neml2::Model::declare_input_variable can be used to declare input variables. This method should be called in the constructor of the model. It allocate storage for the underlying tensor and returns a const-reference i.e., const Variable<T> &
, to the variable, where T
is the variable tensor type. Similarly, the method neml2::Model::declare_output_variable is for declaring output variables which returns a non-const-reference i.e., Variable<T> &
, to the variable.
In our example, the only input variable is
Then, use the variable declaration method in the constructor, i.e.
NEML2 will look for an input option named "velocity" and declare a vector-valued input variable with the user-specified variable name. Similarly, another input option named "acceleration" will be used to declare a vector-valued output variable.
Parameter (and buffer) declaration is semantically similarly to variable declaration.
Recall that both parameters and buffers "travel with the model", meaning that when a model is sent to a different device, so are its parameters and buffers. The difference is that parameters are trainable. Therefore, in our example, we'd like to treat the dynamic viscosity as a trainable parameter and the gravitational acceleration as a buffer.
First, create the corresponding members in the class:
Then, call the declaration methods in the constructor, i.e.
The first argument of the parameter/buffer declaration method takes a string specifying the name of the parameter/buffer used internally. These names will then become the "handles" for retrieving and updating parameter/buffer values.
NEML2 will look for input options named "gravitational_acceleration" and "dynamic_viscosity" and declare the corresponding parameter and buffer.
Once all arguments are successfully declared, the structure of the model is determined. NEML2 handles the allocation and arrangement of variables, parameters, buffers, and their underlying tensors. The structure of the model can be inspected by printing out the summary:
Output:
Previous | Next |
---|---|
Connection to input files | The forward operator |