https://mooseframework.inl.gov
Classes | Functions | Variables
NEML2Utils Namespace Reference

Classes

struct  Layout
 
struct  Layout< RankFourTensor >
 
struct  Layout< RankTwoTensor >
 
struct  Layout< Real >
 
struct  Layout< RealVectorValue >
 
struct  Layout< SymmetricRankFourTensor >
 
struct  Layout< SymmetricRankTwoTensor >
 

Functions

std::shared_ptr< neml2::Model > getModel (neml2::Factory &factory, const std::string &name, neml2::Dtype dtype=neml2::kFloat64)
 Get the NEML2 Model. More...
 
void assertVariable (const neml2::VariableName &)
 Assert that the NEML2 variable name sits on either the forces or the state subaxis. More...
 
void assertOldVariable (const neml2::VariableName &)
 Assert that the NEML2 variable name sits on either the old_forces or the old_state subaxis. More...
 
neml2::VariableName parseVariableName (const std::string &)
 Parse a raw string into NEML2 variable name. More...
 
template<typename T >
neml2::Tensor fromBlob (const std::vector< T > &data)
 Map from std::vector<T> to neml2::Tensor without copying the data. More...
 
template<typename T >
void copyTensorToMOOSEData (const at::Tensor &src, T &dest)
 Directly copy a contiguous chunk of memory of a at::Tensor to a MOOSE data of type T. More...
 
bool shouldCompute (const SubProblem &)
 Determine whether the NEML2 material model should be evaluated. More...
 
std::string docstring (const std::string &desc)
 Augment docstring if NEML2 is not enabled. More...
 
void assertNEML2Enabled ()
 Assert that NEML2 is enabled. More...
 

Variables

static std::string NEML2_help_message
 
static const std::string missing_neml2
 

Function Documentation

◆ assertNEML2Enabled()

void NEML2Utils::assertNEML2Enabled ( )

Assert that NEML2 is enabled.

A MooseError is raised if NEML2 is not enabled.

Definition at line 87 of file NEML2Utils.C.

Referenced by NEML2Action::NEML2Action(), NEML2ActionCommon::NEML2ActionCommon(), and NEML2ToMOOSEMaterialProperty< T >::NEML2ToMOOSEMaterialProperty().

88 {
89 #ifndef NEML2_ENABLED
91 #endif
92 }
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:302
static const std::string missing_neml2
Definition: NEML2Utils.C:55

◆ assertOldVariable()

void NEML2Utils::assertOldVariable ( const neml2::VariableName &  v)

Assert that the NEML2 variable name sits on either the old_forces or the old_state subaxis.

Definition at line 39 of file NEML2Utils.C.

Referenced by MOOSEToNEML2::checkMode().

40 {
41  if (v.empty())
42  mooseError("Empty NEML2 variable");
43 
44  if (!v.is_old_force() && !v.is_old_state())
45  mooseError("The NEML2 variable '", v, "' is on the wrong subaxis.");
46 }
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:302

◆ assertVariable()

void NEML2Utils::assertVariable ( const neml2::VariableName &  v)

Assert that the NEML2 variable name sits on either the forces or the state subaxis.

Definition at line 29 of file NEML2Utils.C.

Referenced by MOOSEToNEML2::checkMode().

30 {
31  if (v.empty())
32  mooseError("Empty NEML2 variable");
33 
34  if (!v.is_force() && !v.is_state())
35  mooseError("The NEML2 variable '", v, "' is on the wrong subaxis.");
36 }
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:302

◆ copyTensorToMOOSEData()

template<typename T >
void NEML2Utils::copyTensorToMOOSEData ( const at::Tensor src,
T &  dest 
)

Directly copy a contiguous chunk of memory of a at::Tensor to a MOOSE data of type T.

This assumes the at::Tensor and T have the same layout, for example both row-major with T = RankTwoTensor. If the layouts are different, we may need to reshape/reorder/transpose the at::Tensor before copying.

For this method to work,

  1. the address of dest must align with the first element of the data,
  2. the number of elements in dest must match the number of elements in src,
  3. the src tensor must be of type neml2::kFloat64, and
  4. data in dest must be reinterpretable as Real.

Definition at line 135 of file NEML2Utils.h.

136 {
137  if (src.dtype() != neml2::kFloat64)
138  mooseError(
139  "Cannot copy at::Tensor with dtype ", src.dtype(), " into ", demangle(typeid(T).name()));
140  if (src.numel() != Layout<T>::strides[0])
141  mooseError("Cannot copy at::Tensor with shape ",
142  src.sizes(),
143  " into ",
144  demangle(typeid(T).name()),
145  " with different number of elements.");
146  auto dest_tensor = at::from_blob(reinterpret_cast<Real *>(&dest),
147  Layout<T>::shape,
148  at::TensorOptions().dtype(neml2::kFloat64));
149  dest_tensor.copy_(src.reshape(Layout<T>::shape));
150 }
std::string name(const ElemQuality q)
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:302
std::string demangle(const char *name)

◆ docstring()

std::string NEML2Utils::docstring ( const std::string &  desc)

◆ fromBlob()

template<typename T >
neml2::Tensor NEML2Utils::fromBlob ( const std::vector< T > &  data)

Map from std::vector<T> to neml2::Tensor without copying the data.

This method is used in gatherers which gather data from MOOSE as input variables to the NEML2 material model. So in theory, we only need to provide Layout specializations for MOOSE types that can potentially be used as NEML2 input variables.

For this method to work, the underlying data in data must be reinterpretable as Real (neml2::kFloat64). The data class T must also be aligned and follow the striding implied by Layout<T>::shape. The data class T must also have no padding or overhead.

Definition at line 110 of file NEML2Utils.h.

Referenced by MOOSEToNEML2Batched< Real >::gatheredData().

111 {
112  // The const_cast is fine because torch works with non-const ptr so that it can optionally handle
113  // deallocation. But we are not going to let torch do that.
114  const auto torch_tensor = at::from_blob(const_cast<T *>(data.data()),
115  neml2::utils::add_shapes(data.size(), Layout<T>::shape),
116  at::TensorOptions().dtype(neml2::kFloat64));
117  return neml2::Tensor(torch_tensor, 1);
118 }

◆ getModel()

std::shared_ptr< neml2::Model > NEML2Utils::getModel ( neml2::Factory &  factory,
const std::string &  name,
neml2::Dtype  dtype = neml2::kFloat64 
)

Get the NEML2 Model.

This is mostly the same as the plain neml2::get_model() method, but it also guards the default dtype and sends the model to the target device.

Returns
neml2::Model&

Definition at line 18 of file NEML2Utils.C.

Referenced by NEML2Action::NEML2Action().

19 {
20  const auto prev_dtype = neml2::get_default_dtype();
21  neml2::set_default_dtype(dtype);
22  auto model = factory.get_model(name);
23  model->to(dtype);
24  neml2::set_default_dtype(prev_dtype);
25  return model;
26 }

◆ parseVariableName()

neml2::VariableName NEML2Utils::parseVariableName ( const std::string &  s)

Parse a raw string into NEML2 variable name.

Definition at line 49 of file NEML2Utils.C.

Referenced by NEML2ModelExecutor::NEML2ModelExecutor(), MOOSEToNEML2::setMode(), NEML2Action::setupDerivativeMappings(), NEML2Action::setupInputMappings(), NEML2Action::setupOutputMappings(), and NEML2Action::setupParameterDerivativeMappings().

50 {
51  return neml2::utils::parse<neml2::VariableName>(s);
52 }

◆ shouldCompute()

bool NEML2Utils::shouldCompute ( const SubProblem problem)

Determine whether the NEML2 material model should be evaluated.

Definition at line 59 of file NEML2Utils.C.

Referenced by NEML2BatchIndexGenerator::execute(), BatchMaterial< Tuple, Output, Input >::execute(), BatchMaterial< Tuple, Output, Input >::finalize(), NEML2BatchIndexGenerator::initialize(), BatchMaterial< Tuple, Output, Input >::initialize(), NEML2ModelExecutor::meshChanged(), NEML2BatchIndexGenerator::threadJoin(), and BatchMaterial< Tuple, Output, Input >::threadJoin().

60 {
61  // NEML2 computes residual and Jacobian together at EXEC_LINEAR
62  // There is no work to be done at EXEC_NONLINEAR **UNLESS** we are computing the Jacobian for
63  // automatic scaling.
64  if (problem.computingScalingJacobian())
65  return true;
66 
68  return true;
69 
70  if (problem.currentlyComputingJacobian())
71  return false;
72 
73  return true;
74 }
virtual bool computingScalingJacobian() const =0
Getter for whether we&#39;re computing the scaling jacobian.
const bool & currentlyComputingResidualAndJacobian() const
Returns true if the problem is in the process of computing the residual and the Jacobian.
Definition: SubProblem.h:1487
const bool & currentlyComputingJacobian() const
Returns true if the problem is in the process of computing the Jacobian.
Definition: SubProblem.h:684

Variable Documentation

◆ missing_neml2

const std::string NEML2Utils::missing_neml2
static
Initial value:
= "The `NEML2` library is required but not enabled. Refer "
"to the documentation for guidance on how to enable it."

Definition at line 55 of file NEML2Utils.C.

Referenced by assertNEML2Enabled(), and docstring().

◆ NEML2_help_message

std::string NEML2Utils::NEML2_help_message
static
Initial value:
= R""""(
==============================================================================
To debug NEML2 related issues:
1. Build and run MOOSE in dbg mode.
2. Re-run the simulation using the dbg executable, and often times
NEML2 will provide a more helpful error message.
3. If the error message is not helpful, or if there is still no error message,
run the simulation through a debugger: See
https://mooseframework.inl.gov/application_development/debugging.html
4. If the issue is due to a NEML2 bug, feel free to report it at
https://github.com/applied-material-modeling/neml2/issues
==============================================================================
)""""

Definition at line 152 of file NEML2Utils.h.

Referenced by NEML2ModelExecutor::applyPredictor(), NEML2ModelExecutor::extractOutputs(), and NEML2ModelExecutor::fillInputs().