NEML2 2.1.0
Loading...
Searching...
No Matches
The forward operator

Method signature

The base class neml2::Model declares a pure virtual method neml2::Model::set_value for derived classes to define the forward operator. The forward operator is responsible for calculating output variables given input variables, parameters, and buffers. Optionally, the neml2::Model::set_value method can define derivatives and second derivatives, if requested.

The signature of the method is

virtual void set_value(bool, bool, bool) = 0;

The three boolean arguments denote whether the caller is requesting output variable values, their derivatives, or their second derivatives. Derived classes should therefore override this method, i.e.

void set_value(bool, bool, bool) override;

Implementation

By default, the model forward operator is responsible for calculating the output variable values and their first derivatives. Definition of second order derivatives is not required. Such default behavior can be changed by modifying the corresponding input file option in neml2::Model::expected_options, i.e.

options.set<bool>("define_values") = true;
options.set<bool>("define_derivatives") = true;
options.set<bool>("define_second_derivatives") = false;

In other words, the default configuration guarantees the third boolean argument of neml2::Model::set_value to always be false, while at least one of the first two boolean arguments is true.

Recall that the equation for this model is

\[ \boldsymbol{a} = \boldsymbol{g} - \mu \boldsymbol{v}. \]

The forward operator can be implemented as

#include "neml2/tensors/functions/imap.h"
namespace neml2
{
void
ProjectileAcceleration::set_value(bool out, bool dout, bool /*d2out*/)
{
if (out)
_a = _g - _mu * _v;
if (dout)
_a.d(_v) = -_mu * imap_v<Vec>(_v.options());
}
}
Definition DiagnosticsInterface.h:31
imap_t< T >::type imap_v(const TensorOptions &options=default_tensor_options())
Get the identity map interpreted as the concrete primitive tensor type.
Definition imap.h:66

Evaluation

The model definition is said to be complete once all components are properly defined. Custom models can be evaluated in the same way as existing models that come with NEML2.

int
main()
{
using namespace neml2;
auto model = load_model("input.i", "accel");
// Input velocity
auto vel_name = VariableName("state", "v");
auto vel = Vec::fill(10, 2, 0);
// Evaluate the model
auto output = model->value({{vel_name, vel}});
// Output acceleration
auto accel_name = VariableName("state", "a");
auto & accel = output[accel_name];
std::cout << "Acceleration: \n" << accel << std::endl;
}
static Vec fill(Args &&... args)
Definition PrimitiveTensor.h:331
constexpr auto kFloat64
Definition types.h:54
void set_default_dtype(Dtype dtype)
LabeledAxisAccessor VariableName
Definition LabeledAxisAccessor.h:185
std::shared_ptr< Model > load_model(const std::filesystem::path &path, const std::string &mname)
A convenient function to load an input file and get a model.

Output:

Acceleration:
-0.0100
-9.8120
0.0000
[ CPUDoubleType{3} ]