https://mooseframework.inl.gov
Classes | Enumerations | 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 >
 

Enumerations

enum  MOOSEIOType {
  MOOSEIOType::TIME, MOOSEIOType::SCALAR, MOOSEIOType::FUNCTION, MOOSEIOType::VARIABLE,
  MOOSEIOType::MATERIAL
}
 

Functions

std::string stringify (MOOSEIOType type)
 
std::shared_ptr< neml2::Model > getModel (neml2::Factory &factory, const std::string &name, neml2::Dtype dtype=neml2::kFloat64)
 Get the NEML2 Model. 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
 

Enumeration Type Documentation

◆ MOOSEIOType

Enumerator
TIME 
SCALAR 
FUNCTION 
VARIABLE 
MATERIAL 

Definition at line 35 of file NEML2Utils.h.

Function Documentation

◆ assertNEML2Enabled()

void NEML2Utils::assertNEML2Enabled ( )

Assert that NEML2 is enabled.

A MooseError is raised if NEML2 is not enabled.

Definition at line 81 of file NEML2Utils.C.

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

82 {
83 #ifndef NEML2_ENABLED
85 #endif
86 }
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:311
static const std::string missing_neml2
Definition: NEML2Utils.C:49

◆ 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 137 of file NEML2Utils.h.

138 {
139  if (src.dtype() != neml2::kFloat64)
140  mooseError(
141  "Cannot copy at::Tensor with dtype ", src.dtype(), " into ", demangle(typeid(T).name()));
142  if (src.numel() != Layout<T>::strides[0])
143  mooseError("Cannot copy at::Tensor with shape ",
144  src.sizes(),
145  " into ",
146  demangle(typeid(T).name()),
147  " with different number of elements.");
148  auto dest_tensor = at::from_blob(reinterpret_cast<Real *>(&dest),
149  Layout<T>::shape,
150  at::TensorOptions().dtype(neml2::kFloat64));
151  dest_tensor.copy_(src.reshape(Layout<T>::shape));
152 }
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:311
std::string demangle(const char *name)

◆ docstring()

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

Augment docstring if NEML2 is not enabled.

Definition at line 71 of file NEML2Utils.C.

Referenced by InputParameters::renameCoupledVarInternal(), and InputParameters::renameParamInternal().

72 {
73 #ifndef NEML2_ENABLED
74  return missing_neml2 + " (Original description: " + desc + ")";
75 #else
76  return desc;
77 #endif
78 }
static const std::string missing_neml2
Definition: NEML2Utils.C:49

◆ 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 112 of file NEML2Utils.h.

Referenced by MOOSEQuantityToNEML2< T, state >::gatheredData().

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

◆ 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 38 of file NEML2Utils.C.

Referenced by NEML2Action::NEML2Action().

39 {
40  const auto prev_dtype = neml2::get_default_dtype();
41  neml2::set_default_dtype(dtype);
42  auto model = factory.get_model(name);
43  model->to(dtype);
44  neml2::set_default_dtype(prev_dtype);
45  return model;
46 }

◆ shouldCompute()

bool NEML2Utils::shouldCompute ( const SubProblem problem)

Determine whether the NEML2 material model should be evaluated.

Definition at line 53 of file NEML2Utils.C.

Referenced by NEML2BatchIndexGenerator::execute(), NEML2BatchIndexGenerator::initialize(), NEML2ModelExecutor::meshChanged(), and NEML2BatchIndexGenerator::threadJoin().

54 {
55  // NEML2 computes residual and Jacobian together at EXEC_LINEAR
56  // There is no work to be done at EXEC_NONLINEAR **UNLESS** we are computing the Jacobian for
57  // automatic scaling.
58  if (problem.computingScalingJacobian())
59  return true;
60 
62  return true;
63 
64  if (problem.currentlyComputingJacobian())
65  return false;
66 
67  return true;
68 }
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:1497
const bool & currentlyComputingJacobian() const
Returns true if the problem is in the process of computing the Jacobian.
Definition: SubProblem.h:684

◆ stringify()

std::string NEML2Utils::stringify ( MOOSEIOType  type)

Definition at line 18 of file NEML2Utils.C.

Referenced by Moose::SlepcSupport::clearFreeNonlinearPowerIterations(), NEML2Action::printSummary(), Moose::SlepcSupport::setFreeNonlinearPowerIterations(), Moose::SlepcSupport::setNewtonPetscOptions(), and Moose::SlepcSupport::setSlepcEigenSolverTolerances().

19 {
20  switch (type)
21  {
23  return "TIME";
25  return "SCALAR";
27  return "FUNCTION";
29  return "VARIABLE";
31  return "MATERIAL";
32  default:
33  mooseError("Unknown MOOSE IO type.");
34  }
35 }
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:311

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 49 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 154 of file NEML2Utils.h.

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