https://mooseframework.inl.gov
Loading...
Searching...
No Matches
MOOSEQuantityToNEML2.C
Go to the documentation of this file.
1//* This file is part of the MOOSE framework
2//* https://mooseframework.inl.gov
3//*
4//* All rights reserved, see COPYRIGHT for full restrictions
5//* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6//*
7//* Licensed under LGPL 2.1, please see LICENSE for details
8//* https://www.gnu.org/licenses/lgpl-2.1.html
9
11#include "Function.h"
12#include "MooseVariableScalar.h"
13
14#define registerMOOSEQuantityToNEML2(T) \
15 registerMooseObject("MooseApp", MOOSE##T##ToNEML2); \
16 registerMooseObject("MooseApp", MOOSEOld##T##ToNEML2)
17
22
23template <typename T, unsigned int state>
26{
27 auto params = MOOSEToNEML2::validParams();
29
30 params.addClassDescription(
31 "Gather a MOOSE quantity of type " + demangle(typeid(T).name()) +
32 " for insertion into the specified input or model parameter of a NEML2 model.");
33 params.template addRequiredParam<std::string>("from_moose",
34 "Name of the MOOSE quantity to read from");
35 MooseEnum moose_type("TIME SCALAR FUNCTION VARIABLE MATERIAL", "MATERIAL");
36 params.template addParam<MooseEnum>(
37 "quantity_type", moose_type, "Type of the MOOSE quantity to read from.");
38
39 // Since we use the NEML2 model to evaluate the residual AND the Jacobian at the same time, we
40 // want to execute this user object only at execute_on = LINEAR (i.e. during residual evaluation).
41 // The NONLINEAR exec flag below is for computing Jacobian during automatic scaling.
43 execute_options = {EXEC_INITIAL, EXEC_LINEAR, EXEC_NONLINEAR};
44 params.set<ExecFlagEnum>("execute_on") = execute_options;
45
46 return params;
47}
48
49template <typename T, unsigned int state>
51 : MOOSEToNEML2(params),
52 ElementUserObject(params)
53#ifdef NEML2_ENABLED
54 ,
55 _type(getParam<MooseEnum>("quantity_type").template getEnum<NEML2Utils::MOOSEIOType>()),
56 _var_scalar(
57 _type == NEML2Utils::MOOSEIOType::SCALAR && state == 0
58 ? &this->_fe_problem.getScalarVariable(_tid, getParam<std::string>("from_moose")).sln()
59 : nullptr),
60 _var_scalar_old(
61 _type == NEML2Utils::MOOSEIOType::SCALAR && state == 1
62 ? &this->_fe_problem.getScalarVariable(_tid, getParam<std::string>("from_moose"))
63 .slnOld()
64 : nullptr),
65 _func(_type == NEML2Utils::MOOSEIOType::FUNCTION
66 ? &this->_fe_problem.getFunction(getParam<std::string>("from_moose"), _tid)
67 : nullptr),
68 _mat_prop(
69 _type == NEML2Utils::MOOSEIOType::MATERIAL && state == 0
70 ? &this->template getMaterialPropertyByName<T>(getParam<std::string>("from_moose"))
71 : nullptr),
72 _mat_prop_old(
73 _type == NEML2Utils::MOOSEIOType::MATERIAL && state == 1
74 ? &this->template getMaterialPropertyOldByName<T>(getParam<std::string>("from_moose"))
75 : nullptr),
76 _var(_type == NEML2Utils::MOOSEIOType::VARIABLE && state == 0
77 ? &this->_fe_problem.getStandardVariable(_tid, getParam<std::string>("from_moose"))
78 .sln()
79 : nullptr),
80 _var_old(_type == NEML2Utils::MOOSEIOType::VARIABLE && state == 1
81 ? &this->_fe_problem.getStandardVariable(_tid, getParam<std::string>("from_moose"))
82 .slnOld()
83 : nullptr),
84 _batched(_type != NEML2Utils::MOOSEIOType::TIME && _type != NEML2Utils::MOOSEIOType::SCALAR)
85#endif
86{
87#ifdef NEML2_ENABLED
88 // A VARIABLE gatherer reads the field through getStandardVariable(...).sln()/slnOld() rather than
89 // the Coupleable interface, so the variable is not otherwise part of this user object's reinit
90 // set. Register it as a dependency so MOOSE refreshes its elemental solution before each
91 // execute(); without this, .sln() can carry a stale (one-timestep-lagged) value depending on what
92 // else in the problem happens to force the variable's reinit. Mirrors
93 // ElementIntegralVariableUserObject.
96 &this->_fe_problem.getStandardVariable(_tid, getParam<std::string>("from_moose")));
97#endif
98}
99
100#ifdef NEML2_ENABLED
101template <typename T, unsigned int state>
102void
104{
105 _buffer.clear();
106}
107
108template <typename T, unsigned int state>
109void
111{
112 if (!_batched)
113 return;
114
115 for (unsigned int qp = 0; qp < _qrule->n_points(); qp++)
116 _buffer.emplace_back(qpData(qp));
117}
118
119template <typename T, unsigned int state>
120void
122{
123 if (!_batched)
124 return;
125 // append vectors
126 const auto & m2n = cast_ref<const MOOSEQuantityToNEML2<T, state> &>(uo);
127 _buffer.insert(_buffer.end(), m2n._buffer.begin(), m2n._buffer.end());
128}
129
130template <typename T, unsigned int state>
131at::Tensor
133{
134 // Unbatched scalars (time / scalar variable) are returned as 0-dim tensors; the executor
135 // broadcasts them against the batched inputs before evaluating the model.
136 const auto opts = at::TensorOptions().dtype(at::kDouble);
137 if (!_batched)
138 {
139 switch (_type)
140 {
142 return state == 0 ? at::scalar_tensor(_t, opts) : at::scalar_tensor(_t_old, opts);
144 return state == 0 ? at::scalar_tensor((*_var_scalar)[0], opts)
145 : at::scalar_tensor((*_var_scalar_old)[0], opts);
150 "Unbatched quantity cannot be of type MATERIAL or VARIABLE. This should never happen.");
151 default:
152 mooseError("Invalid MOOSE quantity type. This should never happen.");
153 }
154 }
155
156 return NEML2Utils::fromBlob(_buffer);
157}
158
159template <typename T, unsigned int state>
160T
162{
163 mooseAssert(
164 _batched,
165 "elemMOOSEData should only be called for batched quantities. This should never happen.");
166
167 // non-scalar type can only be MATERIAL
168 if constexpr (!std::is_same_v<T, Real>)
169 return state == 0 ? (*_mat_prop)[qp] : (*_mat_prop_old)[qp];
170 else
171 switch (_type)
172 {
174 mooseError("Batched quantity cannot be of type TIME. This should never happen.");
176 mooseError("Batched quantity cannot be of type SCALAR. This should never happen.");
178 return _func->value(state == 0 ? _t : _t_old, _q_point[qp]);
180 return state == 0 ? (*_var)[qp] : (*_var_old)[qp];
182 return state == 0 ? (*_mat_prop)[qp] : (*_mat_prop_old)[qp];
183 default:
184 mooseError("Invalid MOOSE quantity type. This should never happen.");
185 }
186}
187#endif
188
189#define instantiateMOOSEQuantityToNEML2(T) \
190 template class MOOSEQuantityToNEML2<T, 0>; \
191 template class MOOSEQuantityToNEML2<T, 1>
192
instantiateMOOSEQuantityToNEML2(Real)
registerMOOSEQuantityToNEML2(Real)
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application.
Definition MooseError.h:311
const ExecFlagType EXEC_INITIAL
Definition Moose.C:31
const ExecFlagType EXEC_LINEAR
Definition Moose.C:32
const ExecFlagType EXEC_NONLINEAR
Definition Moose.C:34
static InputParameters validParams()
A MultiMooseEnum object to hold "execute_on" flags.
virtual MooseVariable & getStandardVariable(const THREAD_ID tid, const std::string &var_name) override
Returns the variable reference for requested MooseVariable which may be in any system.
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system.
void execute() override
Execute method.
at::Tensor gatheredData() const override
Convert data gathered from MOOSE into an at::Tensor (shape (batch, *base_shape))
MOOSEQuantityToNEML2(const InputParameters &params)
void initialize() override
Called before execute() is ever called so that data can be cleared.
static InputParameters validParams()
T qpData(unsigned int) const
const NEML2Utils::MOOSEIOType _type
MOOSE quantity type to read from.
void threadJoin(const UserObject &) override
Must override.
Common interface for inserting gathered MOOSE data into the NEML2 material model.
static InputParameters validParams()
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
Definition MooseEnum.h:55
void addMooseVariableDependency(MooseVariableFieldBase *var)
Call this function to add the passed in MooseVariableFieldBase as a variable that this object depends...
FEProblemBase & _fe_problem
Reference to the FEProblemBase for this user object.
const THREAD_ID _tid
Thread ID of this postprocessor.
Base class for user-specific data.
Definition UserObject.h:20
ExecFlagEnum getDefaultExecFlagEnum()
at::Tensor fromBlob(const std::vector< T > &data)
Map from std::vector<T> to an at::Tensor without copying the data.
Definition NEML2Utils.h:139