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 : #pragma once 11 : 12 : #include "NEML2ModelInterface.h" 13 : #include "GeneralUserObject.h" 14 : #include "NEML2BatchIndexGenerator.h" 15 : 16 : class MOOSEToNEML2; 17 : 18 : /** 19 : * NEML2ModelExecutor executes a NEML2 model. The NEML2 input variables and model parameters are 20 : * gathered by UserObjects derived from MOOSEToNEML2. This class is derived from GeneralUserObject 21 : * and is not threaded. It relies on a NEML2BatchIndexGenerator to generate batch indices. 22 : */ 23 : class NEML2ModelExecutor : public NEML2ModelInterface<GeneralUserObject> 24 : { 25 : public: 26 : /// Parameters that can be specified under the NEML2Action common area 27 : static InputParameters actionParams(); 28 : 29 : static InputParameters validParams(); 30 : 31 : NEML2ModelExecutor(const InputParameters & params); 32 : 33 : #ifndef NEML2_ENABLED 34 0 : void initialize() override {} 35 0 : void execute() override {} 36 0 : void finalize() override {} 37 : #else 38 : void initialize() override; 39 : void meshChanged() override; 40 : void execute() override; 41 : void finalize() override; 42 : 43 : void initialSetup() override; 44 : 45 : /// Get the batch index for the given element ID 46 : std::size_t getBatchIndex(dof_id_type elem_id) const; 47 : 48 : /// Get a reference(!) to the requested output view 49 : const neml2::Tensor & getOutput(const neml2::VariableName & output_name) const; 50 : 51 : /// Get a reference(!) to the requested output derivative view 52 : const neml2::Tensor & getOutputDerivative(const neml2::VariableName & output_name, 53 : const neml2::VariableName & input_name) const; 54 : 55 : /// Get a reference(!) to the requested output parameter derivative view 56 : const neml2::Tensor & getOutputParameterDerivative(const neml2::VariableName & output_name, 57 : const std::string & parameter_name) const; 58 : 59 : /// check if the output is fully computed and ready to be fetched 60 40220 : bool outputReady() const { return _output_ready; } 61 : 62 : protected: 63 : /// Register a NEML2 input variable gathered by a gatherer 64 : virtual void addGatheredVariable(const UserObjectName &, const neml2::VariableName &); 65 : 66 : /// Register a NEML2 model parameter gathered by a gatherer 67 : virtual void addGatheredParameter(const UserObjectName &, const std::string &); 68 : 69 : /// Prevent output and derivative retrieval after construction 70 : virtual void checkExecutionStage() const final; 71 : 72 : /// Fill input variables and model parameters using the gatherers 73 : virtual void fillInputs(); 74 : 75 : /// Apply the predictor to set current trial state 76 : virtual void applyPredictor(); 77 : 78 : /// Perform the material update 79 : virtual bool solve(); 80 : 81 : /// Extract output derivatives with respect to input variables and model parameters 82 : virtual void extractOutputs(); 83 : 84 : /// Expand tensor shapes if necessary to conformal sizes 85 : virtual void expandInputs(); 86 : 87 : /// Update cached inputs/outputs for on-device state advance 88 : void advanceDeviceCaches(); 89 : 90 : /// The NEML2BatchIndexGenerator used to generate the element-to-batch-index map 91 : const NEML2BatchIndexGenerator & _batch_index_generator; 92 : 93 : /// Advance state on device (rather than via MOSOE material properties) 94 : const bool _keep_tensors_on_device; 95 : 96 : /// Dump input tensor info on failure to aid debugging 97 : const bool _debug_inputs_on_failure; 98 : 99 : /// flag that indicates if output data has been fully computed 100 : bool _output_ready; 101 : 102 : /// The model parameters to update (gathered from MOOSE) 103 : std::map<std::string, neml2::Tensor> _model_params; 104 : 105 : /// The input variables of the material model 106 : neml2::ValueMap _in; 107 : 108 : /// The output variables of the material model 109 : neml2::ValueMap _out; 110 : 111 : /// Cached state outputs from the last successful step (for on-device advance) 112 : neml2::ValueMap _device_state_cache; 113 : 114 : /// Cached force inputs from the last successful step (for on-device advance) 115 : neml2::ValueMap _device_forces_cache; 116 : 117 : /// The derivative of the output variables w.r.t. the input variables 118 : neml2::DerivMap _dout_din; 119 : 120 : // set of variables to skip 121 : std::set<neml2::VariableName> _skip_vars; 122 : 123 : // set of gathered NEML2 input variables 124 : std::set<neml2::VariableName> _gathered_variable_names; 125 : 126 : // set of gathered NEML2 model parameters 127 : std::set<std::string> _gathered_parameter_names; 128 : 129 : /// MOOSE data gathering user objects 130 : std::vector<const MOOSEToNEML2 *> _gatherers; 131 : 132 : /// set of output variables that were retrieved (by other objects) 133 : mutable neml2::ValueMap _retrieved_outputs; 134 : 135 : /// set of derivatives that were retrieved (by other objects) 136 : mutable neml2::DerivMap _retrieved_derivatives; 137 : 138 : /// set of parameter derivatives that were retrieved (by other objects) 139 : mutable std::map<neml2::VariableName, std::map<std::string, neml2::Tensor>> 140 : _retrieved_parameter_derivatives; 141 : 142 : /// Whether the model has any state variable 143 : bool _has_state = false; 144 : 145 : /// Whether the model has any old state variable 146 : bool _has_old_state = false; 147 : 148 : private: 149 : /// Whether an error was encountered 150 : bool _error; 151 : /// Error message 152 : std::string _error_message; 153 : #endif 154 : };