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 : #include "NEML2BatchIndexGenerator.h" 11 : #include "NEML2Utils.h" 12 : 13 : registerMooseObject("MooseApp", NEML2BatchIndexGenerator); 14 : 15 : InputParameters 16 3078 : NEML2BatchIndexGenerator::validParams() 17 : { 18 3078 : auto params = ElementUserObject::validParams(); 19 3078 : 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. 25 3078 : ExecFlagEnum execute_options = MooseUtils::getDefaultExecFlagEnum(); 26 12312 : execute_options = {EXEC_INITIAL, EXEC_LINEAR, EXEC_NONLINEAR}; 27 3078 : params.set<ExecFlagEnum>("execute_on") = execute_options; 28 : 29 6156 : return params; 30 6156 : } 31 : 32 9 : NEML2BatchIndexGenerator::NEML2BatchIndexGenerator(const InputParameters & params) 33 9 : : ElementUserObject(params), _outdated(true) 34 : { 35 9 : } 36 : 37 : void 38 0 : NEML2BatchIndexGenerator::meshChanged() 39 : { 40 0 : _outdated = true; 41 0 : } 42 : 43 : void 44 2817 : NEML2BatchIndexGenerator::initialize() 45 : { 46 2817 : if (!NEML2Utils::shouldCompute(_fe_problem)) 47 1200 : return; 48 : 49 1617 : if (!_outdated) 50 1407 : return; 51 : 52 210 : _elem_to_batch_index.clear(); 53 210 : _elem_to_batch_index_cache = {libMesh::invalid_uint, 0}; 54 210 : _batch_index = 0; 55 : } 56 : 57 : void 58 17610 : NEML2BatchIndexGenerator::execute() 59 : { 60 17610 : if (!NEML2Utils::shouldCompute(_fe_problem)) 61 7500 : return; 62 : 63 10110 : if (!_outdated) 64 9045 : return; 65 : 66 1065 : _elem_to_batch_index[_current_elem->id()] = _batch_index; 67 1065 : _batch_index += _qrule->n_points(); 68 : } 69 : 70 : void 71 352 : NEML2BatchIndexGenerator::threadJoin(const UserObject & uo) 72 : { 73 352 : if (!NEML2Utils::shouldCompute(_fe_problem)) 74 150 : return; 75 : 76 202 : if (!_outdated) 77 201 : return; 78 : 79 1 : const auto & m2n = static_cast<const NEML2BatchIndexGenerator &>(uo); 80 : 81 : // append and renumber maps 82 6 : for (const auto & [elem_id, batch_index] : m2n._elem_to_batch_index) 83 5 : _elem_to_batch_index[elem_id] = _batch_index + batch_index; 84 : 85 1 : _batch_index += m2n._batch_index; 86 : } 87 : 88 : void 89 2465 : NEML2BatchIndexGenerator::finalize() 90 : { 91 2465 : _outdated = false; 92 2465 : } 93 : 94 : std::size_t 95 40120 : NEML2BatchIndexGenerator::getBatchIndex(dof_id_type elem_id) const 96 : { 97 : // return cached map lookup if applicable 98 40120 : if (_elem_to_batch_index_cache.first == elem_id) 99 16787 : return _elem_to_batch_index_cache.second; 100 : 101 : // else, search the map 102 23333 : const auto it = _elem_to_batch_index.find(elem_id); 103 23333 : if (it == _elem_to_batch_index.end()) 104 0 : mooseError("No batch index found for element id ", elem_id); 105 23333 : _elem_to_batch_index_cache = *it; 106 23333 : return it->second; 107 : }