NEML2 2.0.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());
}
}
Variable< Vec > & _a
Acceleration of the projectile (output variable)
Definition ex1.cxx:22
const Variable< Vec > & _v
Velocity of the projectile (input variable)
Definition ex1.cxx:19
const Vec & _g
Gravitational acceleration (parameter)
Definition ex1.cxx:27
const Scalar & _mu
Dynamic viscosity of the medium (parameter)
Definition ex1.cxx:30
void set_value(bool, bool, bool) override
The map between input -> output, and optionally its derivatives.
Definition ex1.cxx:15
Definition DiagnosticsInterface.cxx:30
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;
set_default_dtype(kFloat64);
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;
}
The accessor containing all the information needed to access an item in a LabeledAxis.
Definition LabeledAxisAccessor.h:56

Output: @list-output:ex1