NEML2 1.4.0
|
The following tutorials serve as a developer-facing step-by-step guide for creating and testing a custom material model. A simple linear isotropic hardening is used as the example in this tutorial. The model can be mathematically written as
\begin{align*} k &= H \bar{\varepsilon}^p, \end{align*}
where \(\bar{\varepsilon}^p\) is the equivalent plastic strain and \(k\) is the isotropic hardening. The input variable for this model is \(\boldsymbol{\varepsilon}\), the output variable for this model is \(k\), and the parameters of the model is \(H\).
Recall that NEML2 models operates on labeled tensors, and that the collection of labels (with their corresponding layout) is called an labeled axis (LabeledAxis). NEML2 predefines 5 sub-axes to categorize all the input, output and intermediate variables:
In NEML2, the following naming conventions are recommended:
ep
. However, if the member variable is protected or private, it is recommended to prefix it with an underscore, i.e. _ep
.PascalCase
.snake_case
.The development of every model begins with the declaration and registration of its input and output variables. Here, we first define an abstract base class that will be later used to define the linear isotropic hardening relation. The abstract base class defines the isotropic hardening relation
\[ k = f\left( \bar{\varepsilon}^p \right), \]
mapping the equivalent plastic strain to the isotropic hardening. The base class is named IsotropicHardening
following the naming conventions. The header file IsotropicHardening.h is displayed below
Since isotropic hardening is a model, the class inherits from Model
. The user-facing expected options are defined by the static method expected_options
. NEML2 handles the parsing of user-specified options and pass them to the constructor (see Input file syntax on how the input file works). The input variable of the model is the equivalent plastic strain, and the output variable of the model is the isotropic hardening. Their corresponding variable value references are stored as _ep
and _h
, respectively, again following the naming conventions.
The expected options and the constructor are defined as
Recall that variable names on LabeledAxis
are always fully qualified, the equivalent plastic strain and the isotropic hardening are denoted as "state/internal/ep" and "state/internal/k", respectively. An instance of the class is constructed by extracting user-specified options (of type OptionSet
). Note how declare_input_variable<Scalar>
and declare_output_variable<Scalar>
are used to declare and register the input and output variables.
Now that the abstract base class IsotropicHardening
has been implemented, we are ready to define our first concrete NEML2 model that describes a linear isotropic hardening relation
\[ k = H \bar{\varepsilon}^p. \]
Note that \(H\) is a model parameter. Following the naming convention, the concrete class is named LinearIsotropicHardening
. The header file is displayed below.
It derives from the abstract base class IsotropicHardening
and implements the method set_value
as the forward operator. The model parameter \(H\) is stored as a protected member variable _K
. The model implementation is shown below.
Note that an additional option named "hardening_modulus" is requested from the user. The model parameter is registered using the API declare_parameter<Scalar>
. In the set_value
method, the current value of the input variable, equivalent plastic strain, is stored in the member _ep
, and so the isotropic hardening can be computed as
The computed result is copied into the model output variable _h
by
In addition, the first derivative of the forward operator is defined as
Last but not the least, the model is registed in the NEML2 model factory using the macro
so that an instance of the class can be created at runtime.
It is of paramount importance to ensure the correctness of the implementation. NEML2 offers 5 types of tests with different purposes.
A Catch test refers to a test directly written in C++ source code within the Catch2 framework. It offers the highest level of flexibility, but requires more effort to set up. To understand how a Catch2 test works, please refer to the official Catch2 documentation.
A model unit test examines the outputs of a Model
given a predefined set of inputs. Model unit tests can be directly designed using the input file syntax with the ModelUnitTest
type. A variety of checks can be turned on and off based on input file options. To list a few: check_first_derivatives
compares the implemented first order derivatives of the model against finite-differencing results, and the test is marked as passing only if the two derivatives are within tolerances specified with derivative_abs_tol
and derivative_rel_tol
; if check_cuda
is set to true
, all checks are repeated twice, once on CPU and once on GPU (if available), and pass only if the two evaluations yield same results within tolerances.
All input files for model unit tests should be stored inside tests/unit/models
. Every input file with the .i
extension will be automatically discovered and executed. To run all the model unit tests, use the following commands
To run a specific model unit test, use the -c
command line option followed by the relative location of the input file, i.e.
A model regression test runs a Model
using a user specified driver. The results are compared against a predefined reference (stored on the disk checked into the repository). The test passes only if the current results are the same as the predefined reference (again within specified tolerances). The regression tests ensure the consistency of implementations across commits. Currently, TransientRegression
is the only supported type of regression test.
Each input file for model regression tests should be stored inside a separate folder inside tests/regression
. Every input file with the .i
extension will be automatically discovered and executed. To run all the model regression tests, use the regression_tests
executable followed by the physics module, i.e.
To run a specific model regression test, use the -c
command line option followed by the relative location of the input file, i.e.
Note that the regression test expects an option reference
which specifies the relative location to the reference solution.
The model verification test is similar to the model regression test in terms of workflow. The difference is the a verification test defines the reference solution using NEML, the predecessor of NEML2. Since NEML was developed with strict software assurance, the verification tests ensure that the migration from NEML to NEML2 does not cause any regression in software quality.
Each input file for model verification tests should be stored inside a separate folder inside tests/verification
. Every input file with the .i
extension will be automatically discovered and executed. To run all the model verification tests, use the verification_tests
executable followed by the physics module, i.e.
To run a specific model verification test, use the -c
command line option followed by the relative location of the input file, i.e.
The regression test compares variables (specified using the variables
option) against reference values (specified using the references
option). The reference variables can be read using input objects with type VTestTimeSeries
.