https://mooseframework.inl.gov
NEML2BatchIndexGenerator.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 
11 #include "NEML2Utils.h"
12 
14 
17 {
18  auto params = ElementUserObject::validParams();
19  params.addClassDescription("Generates the element to batch index map for MOOSEToNEML2 gatherers, "
20  "NEML2ToMOOSE retrievers, and the NEML2 executor");
21 
22  // Since we use the NEML2 model to evaluate the residual AND the Jacobian at the same time, we
23  // want to execute this user object only at execute_on = LINEAR (i.e. during residual evaluation).
24  // The NONLINEAR exec flag below is for computing Jacobian during automatic scaling.
26  execute_options = {EXEC_INITIAL, EXEC_LINEAR, EXEC_NONLINEAR};
27  params.set<ExecFlagEnum>("execute_on") = execute_options;
28 
29  return params;
30 }
31 
33  : ElementUserObject(params), _outdated(true)
34 {
35 }
36 
37 void
39 {
40  _outdated = true;
41 }
42 
43 void
45 {
47  return;
48 
49  if (!_outdated)
50  return;
51 
52  _elem_to_batch_index.clear();
54  _batch_index = 0;
55 }
56 
57 void
59 {
61  return;
62 
63  if (!_outdated)
64  return;
65 
67  _batch_index += _qrule->n_points();
68 }
69 
70 void
72 {
74  return;
75 
76  if (!_outdated)
77  return;
78 
79  const auto & m2n = static_cast<const NEML2BatchIndexGenerator &>(uo);
80 
81  // append and renumber maps
82  for (const auto & [elem_id, batch_index] : m2n._elem_to_batch_index)
83  _elem_to_batch_index[elem_id] = _batch_index + batch_index;
84 
85  _batch_index += m2n._batch_index;
86 }
87 
88 void
90 {
91  _outdated = false;
92 }
93 
94 std::size_t
96 {
97  // return cached map lookup if applicable
98  if (_elem_to_batch_index_cache.first == elem_id)
99  return _elem_to_batch_index_cache.second;
100 
101  // else, search the map
102  const auto it = _elem_to_batch_index.find(elem_id);
103  if (it == _elem_to_batch_index.end())
104  mooseError("No batch index found for element id ", elem_id);
106  return it->second;
107 }
void initialize() override
Called before execute() is ever called so that data can be cleared.
A MultiMooseEnum object to hold "execute_on" flags.
Definition: ExecFlagEnum.h:21
std::size_t _batch_index
Highest current batch index.
std::map< dof_id_type, std::size_t > _elem_to_batch_index
Map from element IDs to batch indices.
const unsigned int invalid_uint
static InputParameters validParams()
registerMooseObject("MooseApp", NEML2BatchIndexGenerator)
bool shouldCompute(const SubProblem &)
Determine whether the NEML2 material model should be evaluated.
Definition: NEML2Utils.C:59
void meshChanged() override
Called on this object when the mesh changes.
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
void execute() override
Execute method.
NEML2BatchIndexGenerator(const InputParameters &params)
ExecFlagEnum getDefaultExecFlagEnum()
Return the default ExecFlagEnum for MOOSE.
Definition: MooseUtils.C:1067
void finalize() override
Finalize.
bool _outdated
Whether the batch index map is outdated.
std::pair< dof_id_type, std::size_t > _elem_to_batch_index_cache
cache the index for the current element
const ExecFlagType EXEC_LINEAR
Definition: Moose.C:29
const ExecFlagType EXEC_NONLINEAR
Definition: Moose.C:31
const QBase *const & _qrule
const Elem *const & _current_elem
The current element pointer (available during execute())
FEProblemBase & _fe_problem
Reference to the FEProblemBase for this user object.
Definition: UserObject.h:211
void mooseError(Args &&... args) const
Emits an error prefixed with object name and type.
void threadJoin(const UserObject &) override
Must override.
static InputParameters validParams()
std::size_t getBatchIndex() const
Get the current batch index (in almost all cases this is the total batch size)
Base class for user-specific data.
Definition: UserObject.h:40
uint8_t dof_id_type
NEML2BatchIndexGenerator iterates over the mesh and generates a map from element ID to batch index wh...
const ExecFlagType EXEC_INITIAL
Definition: Moose.C:28