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 : // MOOSE includes 13 : #include "MooseObjectWarehouse.h" 14 : #include "SetupInterface.h" 15 : 16 : /** 17 : * A class for storing MooseObjects based on execution flag. 18 : * 19 : * Note: The global list of objects, those accessed via the "get" methods of this class, are not 20 : * sorted when the 21 : * sort method is called. This is because cyclic errors occur even when the execution flags 22 : * differ. 23 : */ 24 : template <typename T> 25 : class ExecuteMooseObjectWarehouse : public MooseObjectWarehouse<T> 26 : { 27 : public: 28 : using MooseObjectWarehouse<T>::checkThreadID; 29 : using MooseObjectWarehouse<T>::initialSetup; 30 : using MooseObjectWarehouse<T>::timestepSetup; 31 : using MooseObjectWarehouse<T>::subdomainSetup; 32 : 33 : /** 34 : * Constructor. 35 : * @param threaded True enables threaded object storage (default). 36 : */ 37 : ExecuteMooseObjectWarehouse(const ExecFlagEnum & flags, bool threaded = true); 38 : 39 : virtual ~ExecuteMooseObjectWarehouse(); 40 : 41 : /** 42 : * Adds an object to the storage structure. 43 : * @param object A shared pointer to the object being added 44 : */ 45 : void addObject(std::shared_ptr<T> object, THREAD_ID tid = 0, bool recurse = true) override; 46 : 47 : ///@{ 48 : /** 49 : * Retrieve shared pointers for the given thread and execution type for all/active objects. 50 : * @param exec_flag The execution flag to retrieve objects from 51 : */ 52 : const MooseObjectWarehouse<T> & operator[](ExecFlagType exec_flag) const; 53 : MooseObjectWarehouse<T> & operator[](ExecFlagType exec_flag); 54 : ///@} 55 : 56 : ///@{ 57 : /** 58 : * Provide access to begin/end iterators of the underlying map of execution flags. 59 : */ 60 77 : typename std::map<ExecFlagType, MooseObjectWarehouse<T>>::const_iterator begin() const 61 : { 62 77 : return _execute_objects.begin(); 63 : } 64 77 : typename std::map<ExecFlagType, MooseObjectWarehouse<T>>::const_iterator end() const 65 : { 66 77 : return _execute_objects.end(); 67 : } 68 : ///@} 69 : 70 : /** 71 : * Updates the active objects storage. 72 : */ 73 : void updateActive(THREAD_ID tid = 0) override; 74 : 75 : ///@{ 76 : /** 77 : * Convenience methods for calling object setup methods. 78 : * 79 : * Limits call to these methods only to objects being executed on linear/nonlinear iterations. 80 : */ 81 : void jacobianSetup(THREAD_ID tid = 0) const override; 82 : void residualSetup(THREAD_ID tid = 0) const override; 83 : void setup(const ExecFlagType & exec_flag, THREAD_ID tid = 0) const; 84 : ///@} 85 : 86 : /** 87 : * Performs a sort using the DependencyResolver. 88 : * @param tid The thread id to access. 89 : */ 90 : void sort(THREAD_ID tid = 0); 91 : 92 : bool hasExecType(const ExecFlagType & exec_flag) { return _execute_objects.count(exec_flag) > 0; } 93 : 94 : protected: 95 : // Map of execute objects to storage containers for MooseObjects 96 : std::map<ExecFlagType, MooseObjectWarehouse<T>> _execute_objects; 97 : 98 : /// A helper method for extracting objects from the various storage containers 99 : typename std::map<ExecFlagType, MooseObjectWarehouse<T>>::iterator 100 : getStorageHelper(std::map<ExecFlagType, MooseObjectWarehouse<T>> & objects, 101 : ExecFlagType exec_flag) const; 102 : }; 103 : 104 : template <typename T> 105 1000408 : ExecuteMooseObjectWarehouse<T>::ExecuteMooseObjectWarehouse(const ExecFlagEnum & flags, 106 : bool threaded) 107 1000408 : : MooseObjectWarehouse<T>(threaded) 108 : { 109 : // Initialize the active/all data structures with the correct map entries and empty vectors 110 28008624 : for (const auto & flag : flags.items()) 111 27008216 : _execute_objects.insert(std::make_pair(flag, MooseObjectWarehouse<T>(threaded))); 112 1000408 : } 113 : 114 : template <typename T> 115 931544 : ExecuteMooseObjectWarehouse<T>::~ExecuteMooseObjectWarehouse() 116 : { 117 931544 : } 118 : 119 : template <typename T> 120 : const MooseObjectWarehouse<T> & 121 120645 : ExecuteMooseObjectWarehouse<T>::operator[](ExecFlagType exec_flag) const 122 : { 123 : // Use find to avoid accidental insertion 124 120645 : const auto iter = _execute_objects.find(exec_flag); 125 : 126 120645 : if (iter == _execute_objects.end()) 127 0 : mooseError("Unable to locate the desired execute flag (", 128 : exec_flag, 129 : "), the supplied execution flag was likely " 130 : "not registered."); 131 : 132 120645 : return iter->second; 133 : } 134 : 135 : template <typename T> 136 : MooseObjectWarehouse<T> & 137 71067306 : ExecuteMooseObjectWarehouse<T>::operator[](ExecFlagType exec_flag) 138 : { 139 : // Use find to avoid accidental insertion 140 71067306 : const auto iter = _execute_objects.find(exec_flag); 141 : 142 71067306 : if (iter == _execute_objects.end()) 143 0 : mooseError("Unable to locate the desired execute flag (", 144 : exec_flag, 145 : "), the supplied execution flag was likely " 146 : "not registered."); 147 : 148 71067306 : return iter->second; 149 : } 150 : 151 : template <typename T> 152 : void 153 5208065 : ExecuteMooseObjectWarehouse<T>::updateActive(THREAD_ID tid /* = 0 */) 154 : { 155 : // Update all objects active list 156 5208065 : MooseObjectWarehouse<T>::updateActive(tid); 157 : 158 : // Update the execute flag lists of objects 159 145825535 : for (auto & object_pair : _execute_objects) 160 140617470 : object_pair.second.updateActive(tid); 161 5208065 : } 162 : 163 : template <typename T> 164 : void 165 4563928 : ExecuteMooseObjectWarehouse<T>::jacobianSetup(THREAD_ID tid /* = 0*/) const 166 : { 167 4563928 : checkThreadID(tid); 168 4563928 : const auto iter = _execute_objects.find(EXEC_NONLINEAR); 169 4563928 : if (iter != _execute_objects.end()) 170 4563928 : iter->second.jacobianSetup(tid); 171 4563928 : } 172 : 173 : template <typename T> 174 : void 175 28616816 : ExecuteMooseObjectWarehouse<T>::residualSetup(THREAD_ID tid /* = 0*/) const 176 : { 177 28616816 : checkThreadID(tid); 178 28616816 : const auto iter = _execute_objects.find(EXEC_LINEAR); 179 28616816 : if (iter != _execute_objects.end()) 180 28616816 : iter->second.residualSetup(tid); 181 28616816 : } 182 : 183 : template <typename T> 184 : void 185 7921 : ExecuteMooseObjectWarehouse<T>::setup(const ExecFlagType & exec_flag, THREAD_ID tid /* = 0*/) const 186 : { 187 7921 : checkThreadID(tid); 188 7921 : if (exec_flag == EXEC_INITIAL) 189 823 : initialSetup(tid); 190 7098 : else if (exec_flag == EXEC_TIMESTEP_BEGIN) 191 5005 : timestepSetup(tid); 192 2093 : else if (exec_flag == EXEC_SUBDOMAIN) 193 0 : subdomainSetup(tid); 194 2093 : else if (exec_flag == EXEC_NONLINEAR) 195 0 : jacobianSetup(tid); 196 2093 : else if (exec_flag == EXEC_LINEAR) 197 0 : residualSetup(tid); 198 7921 : } 199 : 200 : template <typename T> 201 : void 202 179580 : ExecuteMooseObjectWarehouse<T>::addObject(std::shared_ptr<T> object, 203 : THREAD_ID tid, 204 : bool /*recurse*/) 205 : { 206 : // Update list of all objects 207 179580 : MooseObjectWarehouse<T>::addObject(object, tid); 208 : 209 : // Update the execute flag lists of objects 210 359136 : if (const auto ptr = std::dynamic_pointer_cast<SetupInterface>(object)) 211 440626 : for (const auto & flag : ptr->getExecuteOnEnum()) 212 261058 : _execute_objects[flag].addObject(object, tid); 213 : else 214 0 : mooseError("The object being added (", 215 0 : object->name(), 216 : ") must inherit from SetupInterface to be added to the ExecuteMooseObjectWarehouse " 217 : "container."); 218 179568 : } 219 : 220 : template <typename T> 221 : void 222 521704 : ExecuteMooseObjectWarehouse<T>::sort(THREAD_ID tid /* = 0*/) 223 : { 224 : // Sort execute object storage 225 14607624 : for (auto & object_pair : _execute_objects) 226 14085920 : object_pair.second.sort(tid); 227 521704 : }