NEML2 2.0.0
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
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

a=gμv.

The forward operator can be implemented as

namespace neml2
{
void
ProjectileAcceleration::set_value(bool out, bool dout, bool /*d2out*/)
{
if (out)
_a = _g - _mu * _v;
if (dout)
_a.d(_v) = -_mu * Vec::identity_map(_v.options());
}
}
Variable< Vec > & _a
Acceleration of the projectile (output variable)
Definition src1.cxx:19
const Variable< Vec > & _v
Velocity of the projectile (input variable)
Definition src1.cxx:16
const Vec & _g
Gravitational acceleration (parameter)
Definition src1.cxx:22
const Scalar & _mu
Dynamic viscosity of the medium (parameter)
Definition src1.cxx:25
void set_value(bool, bool, bool) override
The map between input -> output, and optionally its derivatives.
Definition src1.cxx:13
static R2 identity_map(const TensorOptions &options=default_tensor_options())
The derivative of a Vec with respect to itself.
Definition Vec.cxx:42
Definition DiagnosticsInterface.cxx:30

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(const Real &v1, const Real &v2, const Real &v3, const TensorOptions &options=default_tensor_options())
Definition VecBase.cxx:34
constexpr auto kFloat64
Definition types.h:53
Model & load_model(const std::filesystem::path &path, const std::string &mname)
A convenient function to load an input file and get a model.
Definition Model.cxx:48
void set_default_dtype(Dtype dtype)
Definition defaults.cxx:32
LabeledAxisAccessor VariableName
Definition LabeledAxisAccessor.h:185

Output:

Acceleration:
0.01 *
-1.0000
-981.2000
0.0000
[ CPUDoubleType{3} ]