https://mooseframework.inl.gov
NEML2Utils.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 
10 #include "NEML2Utils.h"
11 #include "SubProblem.h"
12 
13 namespace NEML2Utils
14 {
15 #ifdef NEML2_ENABLED
16 
17 std::shared_ptr<neml2::Model>
18 getModel(neml2::Factory & factory, const std::string & name, neml2::Dtype dtype)
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 }
27 
28 void
29 assertVariable(const neml2::VariableName & v)
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 }
37 
38 void
39 assertOldVariable(const neml2::VariableName & v)
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 }
47 
48 neml2::VariableName
49 parseVariableName(const std::string & s)
50 {
51  return neml2::utils::parse<neml2::VariableName>(s);
52 }
53 #endif // NEML2_ENABLED
54 
55 static const std::string missing_neml2 = "The `NEML2` library is required but not enabled. Refer "
56  "to the documentation for guidance on how to enable it.";
57 
58 bool
59 shouldCompute(const SubProblem & problem)
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 }
75 
76 std::string
77 docstring(const std::string & desc)
78 {
79 #ifndef NEML2_ENABLED
80  return missing_neml2 + " (Original description: " + desc + ")";
81 #else
82  return desc;
83 #endif
84 }
85 
86 void
88 {
89 #ifndef NEML2_ENABLED
91 #endif
92 }
93 
94 } // namespace NEML2Utils
std::string name(const ElemQuality q)
virtual bool computingScalingJacobian() const =0
Getter for whether we&#39;re computing the scaling jacobian.
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:302
bool shouldCompute(const SubProblem &)
Determine whether the NEML2 material model should be evaluated.
Definition: NEML2Utils.C:59
void assertOldVariable(const neml2::VariableName &)
Assert that the NEML2 variable name sits on either the old_forces or the old_state subaxis...
Definition: NEML2Utils.C:39
const bool & currentlyComputingResidualAndJacobian() const
Returns true if the problem is in the process of computing the residual and the Jacobian.
Definition: SubProblem.h:1487
std::string docstring(const std::string &desc)
Augment docstring if NEML2 is not enabled.
Definition: NEML2Utils.C:77
void assertNEML2Enabled()
Assert that NEML2 is enabled.
Definition: NEML2Utils.C:87
std::shared_ptr< neml2::Model > getModel(neml2::Factory &factory, const std::string &name, neml2::Dtype dtype=neml2::kFloat64)
Get the NEML2 Model.
Definition: NEML2Utils.C:18
Generic class for solving transient nonlinear problems.
Definition: SubProblem.h:78
void assertVariable(const neml2::VariableName &)
Assert that the NEML2 variable name sits on either the forces or the state subaxis.
Definition: NEML2Utils.C:29
const bool & currentlyComputingJacobian() const
Returns true if the problem is in the process of computing the Jacobian.
Definition: SubProblem.h:684
static const std::string missing_neml2
Definition: NEML2Utils.C:55
neml2::VariableName parseVariableName(const std::string &)
Parse a raw string into NEML2 variable name.
Definition: NEML2Utils.C:49