19#include "libmesh/id_types.h"
20#include "neml2/csrc/aoti/Exception.h"
49 params.addParam<
bool>(
50 "manage_state_advance",
52 "Keep state and forces on the device and advance it to old state and old forces without a "
53 "roundtrip through MOOSE materials. This is only recommended for explicit time integration "
54 "or when absolutely no restepping occurs (e.g. failed timesteps).");
55 params.addParam<
bool>(
56 "dump_inputs_on_failure",
58 "When a NEML2 constitutive solve fails (recoverable convergence failure), serialize the "
60 "tensors to a per-rank TorchScript file '<model>_count<c>_rank<r>.pt' (c is a persistent "
61 "failure counter). The file can be loaded offline with torch.jit.load to re-evaluate the "
63 "on the failing batch, independently of MOOSE.");
72 params.addClassDescription(
"Execute the specified NEML2 model");
74 params.addRequiredParam<UserObjectName>(
75 "batch_index_generator",
76 "The NEML2BatchIndexGenerator used to generate the element-to-batch-index map.");
77 params.addParam<std::vector<UserObjectName>>(
80 "List of MOOSE*ToNEML2 user objects gathering MOOSE data as NEML2 input variables");
81 params.addParam<std::vector<UserObjectName>>(
84 "List of MOOSE*ToNEML2 user objects gathering MOOSE data as NEML2 model parameters");
90 params.set<
ExecFlagEnum>(
"execute_on") = execute_options;
100 _manage_state_advance(getParam<bool>(
"manage_state_advance")),
101 _dump_inputs_on_failure(getParam<bool>(
"dump_inputs_on_failure")),
102 _output_ready(false),
105 _derivative_valid(true),
114 for (
const auto & gatherer_name : getParam<std::vector<UserObjectName>>(
"gatherers"))
116 for (
const auto & gatherer_name : getParam<std::vector<UserObjectName>>(
"param_gatherers"))
126 for (
const auto & gatherer_name :
getParam<
std::vector<UserObjectName>>(
"gatherers"))
130 const auto & uo = getUserObjectByName<MOOSEToNEML2>(gatherer_name,
false);
133 const auto [base_name, lag] = parseLag(uo.NEML2Name());
136 "The gatherer for old variable `",
138 "` is not needed when `manage_state_advance = true`.");
145 for (
const auto & gatherer_name :
getParam<
std::vector<UserObjectName>>(
"param_gatherers"))
147 const auto & uo = getUserObjectByName<MOOSEToNEML2>(gatherer_name,
false);
164 const auto opts = at::TensorOptions().dtype(at::kDouble).device(
device());
172 for (
const auto & iname :
model().input_names())
181 paramError(
"gatherers",
"The required model input `", iname,
"` is not gathered");
193 const auto opts = at::TensorOptions().dtype(at::kDouble).device(
device());
196 _state_vars[in_names[i]] = at::zeros(in_shapes[i], opts);
208 const std::string & var)
212 "The NEML2 input variable `",
214 "` gathered by UO '",
216 "' is already gathered by another gatherer.");
222 const std::string & param)
226 "The NEML2 model parameter `",
228 "` gathered by UO '",
230 "' is already gathered by another gatherer.");
251 mooseError(
"The mesh changed while `manage_state_advance = true` for NEML2 model executor '",
253 "'. This mode requires a fixed mesh because state history is cached on the device.");
303 auto success =
solve(compute_derivative);
307 if (compute_derivative)
323 TIME_SECTION(
"NEML2::fillInputs", 1,
"Gathering NEML2 inputs and copying them to the device");
343 for (
auto & [var, val] :
_in)
356 catch (std::exception & e)
358 mooseError(
"An error occurred while filling inputs for the NEML2 model. Error message:\n",
372 const auto [base_name, lag] = parseLag(
name);
373 mooseAssert(lag > 0,
"Invalid lag for a stateful variable");
376 const auto curr_name = lagName(base_name, lag - 1);
377 if (
_out.count(curr_name))
379 else if (
_in.count(curr_name))
382 mooseError(
"Failed to find cached value for old variable: ",
name);
401 TIME_SECTION(
"NEML2::solve", 1,
"Evaluating the NEML2 model");
405 if (compute_derivative)
408 _out = std::move(out);
411 if (compute_parameter_derivative)
415 _out = std::move(out);
418 if (!compute_derivative && !compute_parameter_derivative)
428 catch (
const neml2::aoti::Exception & e)
434 if (!e.recoverable())
435 mooseError(
"NEML2 model evaluation failed with a non-recoverable error:\n",
442 catch (
const std::exception & e)
445 mooseError(
"NEML2 model evaluation raised an unexpected error:\n",
462 std::string model_name = getParam<std::string>(
"model");
463 if (model_name.empty())
465 for (
auto & c : model_name)
466 if (!std::isalnum(
static_cast<unsigned char>(c)) && c !=
'_' && c !=
'-')
468 return model_name +
"_count" + std::to_string(
count) +
"_rank" + rank_token +
".pt";
475 "NEML2::extractOutputs", 1,
"Copying NEML2 outputs and derivatives back to the host");
493 if (compute_derivative)
497 for (
auto & [x, target] : dy)
500 if (source.defined())
512 for (
auto & [p, target] : dy)
515 if (source.defined())
521 catch (std::exception & e)
523 mooseError(
"An error occurred while retrieving outputs from the NEML2 model. Error message:\n",
534 const bool local_error =
_error;
537 processor_id_type pid;
554 const unsigned int nl_it =
557 ", execute_on=" + exec +
", nonlinear iteration " + std::to_string(nl_it) +
559 " (one file per failing rank).";
568 std::string msg =
"NEML2 model execution failed on at least one processor with ID " +
569 std::to_string(pid) +
". Error message:\n";
572 msg +=
"\nTo recover, the solution will fail and then be re-attempted with a reduced time "
574 _console << COLOR_YELLOW << msg << COLOR_DEFAULT << std::endl;
586 mooseError(
"NEML2 output variables and derivatives must be retrieved during object "
587 "construction. This is a code problem.");
595 if (!contains(
model().output_names(), output_name))
596 mooseError(
"Trying to retrieve a non-existent NEML2 output variable '", output_name,
"'.");
603 const std::string & input_name)
const
607 if (!contains(
model().output_names(), output_name))
608 mooseError(
"Trying to retrieve the derivative of NEML2 output variable '",
610 "' with respect to NEML2 input variable '",
612 "', but the NEML2 output variable does not exist.");
614 if (!contains(
model().input_names(), input_name))
615 mooseError(
"Trying to retrieve the derivative of NEML2 output variable '",
617 "' with respect to NEML2 input variable '",
619 "', but the NEML2 input variable does not exist.");
626 const std::string & parameter_name)
const
630 if (!contains(
model().output_names(), output_name))
631 mooseError(
"Trying to retrieve the derivative of NEML2 output variable '",
633 "' with respect to NEML2 model parameter '",
635 "', but the NEML2 output variable does not exist.");
637 if (!
model().parameter_base_shapes().
count(parameter_name))
638 mooseError(
"Trying to retrieve the derivative of NEML2 output variable '",
640 "' with respect to NEML2 model parameter '",
642 "', but the NEML2 model parameter does not exist.");
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application.
const ExecFlagType EXEC_TIMESTEP_END
const ExecFlagType EXEC_INITIAL
const ExecFlagType EXEC_LINEAR
const ExecFlagType EXEC_NONLINEAR
registerMooseObject("MooseApp", NEML2ModelExecutor)
if(!dmm->_nl) SETERRQ(PETSC_COMM_WORLD
const ConsoleStream _console
An instance of helper class to write streams to the Console objects.
A MultiMooseEnum object to hold "execute_on" flags.
void setFailNextNonlinearConvergenceCheck()
Skip further residual evaluations and fail the next nonlinear convergence check(s)
virtual bool solverSystemConverged(const unsigned int solver_sys_num) override
const ExecFlagType & getCurrentExecuteOnFlag() const
Return/set the current execution flag.
NonlinearSystemBase & getNonlinearSystemBase(const unsigned int sys_num)
virtual bool isTransient() const override
virtual bool startedInitialSetup()
Returns true if we are in or beyond the initialSetup stage.
const std::string & name() const
Get the name of the class.
void paramError(const std::string ¶m, Args... args) const
Emits an error prefixed with the file and line number of the given param (from the input file) along ...
const T & getParam(const std::string &name) const
Retrieve a parameter for the object.
const std::string & name() const
NEML2BatchIndexGenerator iterates over the mesh and generates a map from element ID to batch index wh...
bool isEmpty() const
Whether the batch is empty.
std::size_t getBatchIndex() const
Get the current batch index (in almost all cases this is the total batch size)
NEML2ModelExecutor executes a NEML2 model.
const bool _dump_inputs_on_failure
Dump the NEML2 input tensors to a per-rank TorchScript file on solve failure, for offline debugging.
std::map< std::string, at::Tensor > _state_vars
Cached stateful variables from the last successful step (for on-device advance)
bool _derivative_valid
Whether the retained input-derivative tensor is consistent with the current batch.
void initialSetup() override
Gets called at the beginning of the simulation before this object is asked to do its job.
std::map< std::string, at::Tensor > _zero_seed_vars
Base-shaped zero tensors for cold-started in-place-updated unknowns (an order-0 NEML2 input that is a...
bool _error
Whether an error was encountered.
void execute() override
Execute method.
std::string _error_message
Error message.
std::map< std::string, std::map< std::string, at::Tensor > > _dout_din
The derivative of the output variables w.r.t. the input variables: J[output][input].
static InputParameters actionParams()
Parameters that can be specified under the NEML2Action common area.
virtual bool solve(bool compute_derivative)
Perform the material update.
std::set< std::string > _gathered_parameter_names
void deviceSynchronize()
Block until all asynchronous device (CUDA) work has completed.
std::map< std::string, at::Tensor > _model_params
Model parameter values gathered from MOOSE (qualified name -> tensor), set on the model before each e...
const at::Tensor & getOutput(const std::string &output_name) const
Get a reference(!) to the requested output view.
std::map< std::string, at::Tensor > _out
The output variables of the material model.
std::vector< const MOOSEToNEML2 * > _param_gatherers
MOOSE data gathering user objects (model parameters)
std::map< std::string, std::map< std::string, at::Tensor > > _retrieved_derivatives
set of derivatives that were retrieved (by other objects)
virtual void checkExecutionStage() const final
Prevent output and derivative retrieval after construction.
static InputParameters validParams()
const bool _manage_state_advance
Advance state on device (rather than via MOOSE material properties)
bool _output_ready
flag that indicates if output data has been fully computed
std::map< std::string, at::Tensor > _in
The input variables of the material model (name -> (batch, *base_shape) tensor)
std::size_t getBatchIndex(dof_id_type elem_id) const
Get the batch index for the given element ID.
const at::Tensor & getOutputDerivative(const std::string &output_name, const std::string &input_name) const
Get a reference(!) to the requested output derivative view.
void advanceState()
Save stateful variables for on-device state advance.
virtual void extractOutputs(bool compute_derivative)
Extract outputs, and their derivatives with respect to the input variables when compute_derivative is...
const at::Tensor & getOutputParameterDerivative(const std::string &output_name, const std::string ¶meter_name) const
Get a reference(!) to the requested output-parameter-derivative view.
std::set< std::string > _gathered_variable_names
void finalize() override
Finalize.
std::string failedInputDumpName(unsigned int count, const std::string &rank_token) const
Build the dump-file name for a failed constitutive update: '<model>_count<count>_rank<rank>....
std::map< std::string, std::map< std::string, at::Tensor > > _dout_dparam
The derivative of the output variables w.r.t. the model parameters: P[output][parameter].
const NEML2BatchIndexGenerator & _batch_index_generator
The NEML2BatchIndexGenerator used to generate the element-to-batch-index map.
unsigned int _num_failed_dumps
Running count of failed constitutive updates dumped on this rank (disambiguates repeated dumps)
void initialize() override
Called before execute() is ever called so that data can be cleared.
virtual void fillInputs()
Fill input variables using the gatherers.
NEML2ModelExecutor(const InputParameters ¶ms)
std::map< std::string, at::Tensor > _retrieved_outputs
set of output variables that were retrieved (by other objects)
std::vector< const MOOSEToNEML2 * > _gatherers
MOOSE data gathering user objects (input variables)
virtual void addGatheredParameter(const UserObjectName &, const std::string &)
Register a NEML2 model parameter gathered by a gatherer.
void meshChanged() override
Called on this object when the mesh changes.
std::map< std::string, std::map< std::string, at::Tensor > > _retrieved_parameter_derivatives
set of output-parameter-derivatives that were retrieved (by other objects)
virtual void addGatheredVariable(const UserObjectName &, const std::string &)
Register a NEML2 input variable gathered by a gatherer.
virtual const std::vector< std::string > & input_names() const =0
virtual std::pair< std::map< std::string, at::Tensor >, neml2::aoti::VariablePairJacobian > jacobian(const std::map< std::string, at::Tensor > &inputs) const =0
Evaluate the model and its full input Jacobian.
virtual void set_parameter(const std::string &name, const at::Tensor &value)=0
Replace a (runtime-flexible) model parameter's value.
virtual std::pair< std::map< std::string, at::Tensor >, neml2::aoti::VariablePairJacobian > param_jacobian(const std::map< std::string, at::Tensor > &inputs) const =0
Evaluate the model and its parameter Jacobian (d output / d parameter).
virtual std::map< std::string, at::Tensor > value(const std::map< std::string, at::Tensor > &inputs) const =0
virtual const std::vector< std::vector< int64_t > > & input_base_shapes() const =0
virtual const std::vector< std::string > & output_names() const =0
Interface class to provide common input parameters, members, and methods for MOOSEObjects that use NE...
static InputParameters validParams()
const NEML2ModelHandle & model() const
Get the NEML2 model handle (cpp-aoti by default, cpp-eager when eager=true)
virtual void validateModel() const
Validate the NEML2 material model.
const at::Device & output_device() const
Get the target output device.
const at::Device & device() const
Get the target compute device.
unsigned int neml2NumThreads() const
libtorch intra-op thread count to use while evaluating the NEML2 model.
RAII guard forcing libtorch's default scalar type to Float64 for its lifetime, restoring the previous...
RAII guard that pins libtorch's intra-op thread count for the current scope and restores the previous...
virtual unsigned int getCurrentNonlinearIterationNumber()=0
const ExecFlagType & _current_execute_flag
Reference to FEProblemBase.
const bool & currentlyComputingJacobian() const
Returns true if the problem is in the process of computing the Jacobian.
void maxloc(T &r, unsigned int &max_id) const
processor_id_type rank() const
void broadcast(T &data, const unsigned int root_id=0, const bool identical_sizes=false) const
int & _t_step
The number of the time step.
std::set< std::string > _depend_uo
Depend UserObjects that to be used both for determining user object sorting and by AuxKernel for find...
FEProblemBase & _fe_problem
Reference to the FEProblemBase for this user object.
const Parallel::Communicator & _communicator
processor_id_type processor_id() const
ExecFlagEnum getDefaultExecFlagEnum()
bool contains(const std::vector< std::string > &v, const std::string &s)
Whether a vector of strings contains a given string.
void dumpInputsToTorchScript(const std::map< std::string, at::Tensor > &inputs, const std::string &filename)
Serialize a set of named NEML2 input tensors to a TorchScript archive at filename (a torch::jit::Modu...
std::pair< std::string, int > parseLag(const std::string &name)
Parse NEML2 v3 lag notation: "var~N" is the N-th old value of a variable (var~1 == old var,...
std::string lagName(const std::string &base, int lag)
Inverse of parseLag: build the variable name for a base name + lag.
static std::string NEML2_help_message
bool contains(std::string_view superstring, std::string_view substring)
auto index_range(const T &sizable)
void synchronize(int64_t device_index)