Line data Source code
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 45 : getModel(neml2::Factory & factory, const std::string & name, neml2::Dtype dtype) 19 : { 20 45 : const auto prev_dtype = neml2::get_default_dtype(); 21 45 : neml2::set_default_dtype(dtype); 22 45 : auto model = factory.get_model(name); 23 45 : model->to(dtype); 24 45 : neml2::set_default_dtype(prev_dtype); 25 45 : return model; 26 0 : } 27 : 28 : void 29 35 : assertVariable(const neml2::VariableName & v) 30 : { 31 35 : if (v.empty()) 32 0 : mooseError("Empty NEML2 variable"); 33 : 34 35 : if (!v.is_force() && !v.is_state()) 35 0 : mooseError("The NEML2 variable '", v, "' is on the wrong subaxis."); 36 35 : } 37 : 38 : void 39 0 : assertOldVariable(const neml2::VariableName & v) 40 : { 41 0 : if (v.empty()) 42 0 : mooseError("Empty NEML2 variable"); 43 : 44 0 : if (!v.is_old_force() && !v.is_old_state()) 45 0 : mooseError("The NEML2 variable '", v, "' is on the wrong subaxis."); 46 0 : } 47 : 48 : neml2::VariableName 49 465 : parseVariableName(const std::string & s) 50 : { 51 465 : 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 37266 : 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 37266 : if (problem.computingScalingJacobian()) 65 108 : return true; 66 : 67 37158 : if (problem.currentlyComputingResidualAndJacobian()) 68 0 : return true; 69 : 70 37158 : if (problem.currentlyComputingJacobian()) 71 14640 : return false; 72 : 73 22518 : return true; 74 : } 75 : 76 : std::string 77 1269548 : docstring(const std::string & desc) 78 : { 79 : #ifndef NEML2_ENABLED 80 2255184 : return missing_neml2 + " (Original description: " + desc + ")"; 81 : #else 82 141956 : return desc; 83 : #endif 84 : } 85 : 86 : void 87 258 : assertNEML2Enabled() 88 : { 89 : #ifndef NEML2_ENABLED 90 3 : mooseError(missing_neml2); 91 : #endif 92 255 : } 93 : 94 : } // namespace NEML2Utils