https://mooseframework.inl.gov
Loading...
Searching...
No Matches
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
13
16{
17 auto params = ElementUserObject::validParams();
18 params.addClassDescription("Generates the element to batch index map for MOOSEToNEML2 gatherers, "
19 "NEML2ToMOOSE retrievers, and the NEML2 executor");
20
21 // Since we use the NEML2 model to evaluate the residual AND the Jacobian at the same time, we
22 // want to execute this user object only at execute_on = LINEAR (i.e. during residual evaluation).
23 // The NONLINEAR exec flag below is for computing Jacobian during automatic scaling.
25 execute_options = {EXEC_INITIAL, EXEC_LINEAR, EXEC_NONLINEAR};
26 params.set<ExecFlagEnum>("execute_on") = execute_options;
27
28 return params;
29}
30
32 : ElementUserObject(params), _outdated(true)
33{
34}
35
36void
41
42void
52
53void
55{
56 if (!_outdated)
57 return;
58
60 _batch_index += _qrule->n_points();
61}
62
63void
65{
66 if (!_outdated)
67 return;
68
69 const auto & m2n = cast_ref<const NEML2BatchIndexGenerator &>(uo);
70
71 // append and renumber maps
72 for (const auto & [elem_id, batch_index] : m2n._elem_to_batch_index)
73 _elem_to_batch_index[elem_id] = _batch_index + batch_index;
74
75 _batch_index += m2n._batch_index;
76}
77
78void
83
84std::size_t
86{
87 // return cached map lookup if applicable
88 if (_elem_to_batch_index_cache.first == elem_id)
89 return _elem_to_batch_index_cache.second;
90
91 // else, search the map
92 const auto it = _elem_to_batch_index.find(elem_id);
93 if (it == _elem_to_batch_index.end())
94 mooseError("No batch index found for element id ", elem_id);
96 return it->second;
97}
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application.
Definition MooseError.h:311
const ExecFlagType EXEC_INITIAL
Definition Moose.C:31
const ExecFlagType EXEC_LINEAR
Definition Moose.C:32
const ExecFlagType EXEC_NONLINEAR
Definition Moose.C:34
registerMooseObject("MooseApp", NEML2BatchIndexGenerator)
static InputParameters validParams()
const QBase *const & _qrule
const Elem *const & _current_elem
The current element pointer (available during execute())
A MultiMooseEnum object to hold "execute_on" flags.
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system.
NEML2BatchIndexGenerator iterates over the mesh and generates a map from element ID to batch index wh...
void finalize() override
Finalize.
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.
void initialize() override
Called before execute() is ever called so that data can be cleared.
void execute() override
Execute method.
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)
void meshChanged() override
Called on this object when the mesh changes.
NEML2BatchIndexGenerator(const InputParameters &params)
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
Base class for user-specific data.
Definition UserObject.h:20
ExecFlagEnum getDefaultExecFlagEnum()
const unsigned int invalid_uint