Line data Source code
1 : //* This file is part of the MOOSE framework 2 : //* https://www.mooseframework.org 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 "NEML2Action.h" 11 : #include "FEProblem.h" 12 : #include "Factory.h" 13 : #include "NEML2Utils.h" 14 : 15 : #ifdef NEML2_ENABLED 16 : #include "neml2/misc/utils.h" 17 : #include "neml2/misc/parser_utils.h" 18 : #include "neml2/base/HITParser.h" 19 : #endif 20 : 21 : registerMooseAction("TensorMechanicsApp", NEML2Action, "parse_neml2"); 22 : registerMooseAction("TensorMechanicsApp", NEML2Action, "add_material"); 23 : registerMooseAction("TensorMechanicsApp", NEML2Action, "add_user_object"); 24 : 25 : InputParameters 26 4398 : NEML2Action::validParams() 27 : { 28 4398 : InputParameters params = Action::validParams(); 29 4398 : NEML2Utils::addClassDescription(params, "Parse and set up NEML2 objects"); 30 8796 : params.addRequiredParam<FileName>("input", 31 : "Path to the NEML2 input file containing the NEML2 model(s)"); 32 8796 : params.addRequiredParam<std::string>( 33 : "model", 34 : "Name of the NEML2 model, i.e., the string inside the brackets [] in the NEML2 input file " 35 : "that corresponds to the model you want to use."); 36 8796 : params.addParam<bool>("verbose", 37 8796 : true, 38 : "Whether to print additional information about the NEML2 model at the " 39 : "beginning of the simulation"); 40 8796 : params.addParam<std::vector<VariableName>>( 41 13194 : "temperature", std::vector<VariableName>{"0"}, "Coupled temperature"); 42 : 43 8796 : MooseEnum mode("ELEMENT ALL PARSE_ONLY", "ELEMENT"); 44 8796 : mode.addDocumentation("ELEMENT", "Perform constitutive update element-by-element."); 45 8796 : mode.addDocumentation("ALL", "Perform constitutive update for all quadrature points at once."); 46 8796 : mode.addDocumentation("PARSE_ONLY", 47 : "Only parse the NEML2 input file and manufacture the NEML2 model, do not " 48 : "setup any MOOSE objects."); 49 8796 : params.addParam<MooseEnum>("mode", mode, "Mode of operation for the NEML2 material model."); 50 : 51 8796 : params.addParam<std::string>( 52 : "device", 53 : "cpu", 54 : "Device on which to evaluate the NEML2 model. The string supplied must follow the following " 55 : "schema: (cpu|cuda)[:<device-index>] where cpu or cuda specifies the device type, and " 56 : ":<device-index> optionally specifies a device index."); 57 4398 : return params; 58 4398 : } 59 : 60 : #ifndef NEML2_ENABLED 61 : 62 1 : NEML2Action::NEML2Action(const InputParameters & parameters) : Action(parameters) 63 : { 64 1 : NEML2Utils::libraryNotEnabledError(parameters); 65 0 : } 66 : 67 : void 68 0 : NEML2Action::act() 69 : { 70 0 : } 71 : 72 : #else 73 : 74 : NEML2Action::NEML2Action(const InputParameters & parameters) 75 : : Action(parameters), 76 : _fname(getParam<FileName>("input")), 77 : _mname(getParam<std::string>("model")), 78 : _verbose(getParam<bool>("verbose")), 79 : _mode(getParam<MooseEnum>("mode")), 80 : _device(getParam<std::string>("device")) 81 : { 82 : } 83 : 84 : void 85 : NEML2Action::act() 86 : { 87 : const auto mode_name = std::string(_mode); 88 : if (_current_task == "parse_neml2") 89 : { 90 : neml2::HITParser parser; 91 : const auto all_options = parser.parse(_fname); 92 : neml2::Factory::load(all_options); 93 : 94 : if (_verbose) 95 : { 96 : auto & model = neml2::Factory::get_object<neml2::Model>("Models", _mname); 97 : model.to(_device); 98 : 99 : _console << COLOR_YELLOW << "*** BEGIN NEML2 INFO***" << std::endl; 100 : _console << std::endl << "Device: " << _device << std::endl << std::endl; 101 : _console << model << std::endl; 102 : _console << "*** END NEML2 INFO ***" << COLOR_DEFAULT << std::endl; 103 : } 104 : } 105 : 106 : if (_current_task == "add_user_object") 107 : { 108 : if (_mode == "ELEMENT") 109 : { 110 : // no-op 111 : } 112 : else if (_mode == "ALL") 113 : { 114 : auto type = "CauchyStressFromNEML2UO"; 115 : auto params = _factory.getValidParams(type); 116 : params.applyParameters(parameters()); 117 : _problem->addUserObject(type, "_neml2_uo_" + mode_name, params); 118 : } 119 : else if (_mode == "PARSE_ONLY") 120 : { 121 : // no-op 122 : } 123 : else 124 : mooseError("Unsupported mode of constitutive update: ", _mode); 125 : } 126 : 127 : if (_current_task == "add_material") 128 : { 129 : if (_mode == "ELEMENT") 130 : { 131 : auto type = "CauchyStressFromNEML2"; 132 : auto params = _factory.getValidParams(type); 133 : params.applyParameters(parameters()); 134 : _problem->addMaterial(type, "_neml2_stress_" + mode_name, params); 135 : } 136 : else if (_mode == "ALL") 137 : { 138 : auto type = "CauchyStressFromNEML2Receiver"; 139 : auto params = _factory.getValidParams(type); 140 : params.applyParameters(parameters()); 141 : params.set<UserObjectName>("neml2_uo") = "_neml2_uo_" + mode_name; 142 : _problem->addMaterial(type, "_neml2_stress_" + mode_name, params); 143 : } 144 : else if (_mode == "PARSE_ONLY") 145 : { 146 : // no-op 147 : } 148 : else 149 : mooseError("Unsupported mode of constitutive update: ", _mode); 150 : } 151 : } 152 : 153 : #endif // NEML2_ENABLED