www.mooseframework.org
ExecuteMooseObjectWarehouse.h
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://www.mooseframework.org
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 
24 template <typename T>
26 {
27 public:
32 
37  ExecuteMooseObjectWarehouse(const ExecFlagEnum & flags, bool threaded = true);
38 
40 
45  virtual void addObject(std::shared_ptr<T> object, THREAD_ID tid = 0, bool recurse = true);
46  void addObjectMask(std::shared_ptr<T> object,
47  THREAD_ID tid = 0,
48  std::uint16_t flag_mask = std::numeric_limits<std::uint16_t>::max());
49 
51 
55  const MooseObjectWarehouse<T> & operator[](ExecFlagType exec_flag) const;
58 
60 
63  typename std::map<ExecFlagType, MooseObjectWarehouse<T>>::const_iterator begin() const
64  {
65  return _execute_objects.begin();
66  }
67  typename std::map<ExecFlagType, MooseObjectWarehouse<T>>::const_iterator end() const
68  {
69  return _execute_objects.end();
70  }
72 
76  virtual void updateActive(THREAD_ID tid = 0);
77 
79 
84  void jacobianSetup(THREAD_ID tid = 0) const;
85  void residualSetup(THREAD_ID tid = 0) const;
86  void setup(const ExecFlagType & exec_flag, THREAD_ID tid = 0) const;
88 
93  void sort(THREAD_ID tid = 0);
94 
95 protected:
96  // Map of execute objects to storage containers for MooseObjects
97  std::map<ExecFlagType, MooseObjectWarehouse<T>> _execute_objects;
98 
100  typename std::map<ExecFlagType, MooseObjectWarehouse<T>>::iterator
102  ExecFlagType exec_flag) const;
103 };
104 
105 template <typename T>
107  bool threaded)
108  : MooseObjectWarehouse<T>(threaded)
109 {
110  // Initialize the active/all data structures with the correct map entries and empty vectors
111  for (const auto & flag : flags.items())
112  _execute_objects.insert(std::make_pair(flag, MooseObjectWarehouse<T>(threaded)));
113 }
114 
115 template <typename T>
117 {
118 }
119 
120 template <typename T>
122 operator[](ExecFlagType exec_flag) const
123 {
124  // Use find to avoid accidental insertion
125  const auto iter = _execute_objects.find(exec_flag);
126 
127  if (iter == _execute_objects.end())
128  mooseError("Unable to locate the desired execute flag (",
129  exec_flag,
130  "), the supplied execution flag was likely "
131  "not registered.");
132 
133  return iter->second;
134 }
135 
136 template <typename T>
138 {
139  // Use find to avoid accidental insertion
140  const auto iter = _execute_objects.find(exec_flag);
141 
142  if (iter == _execute_objects.end())
143  mooseError("Unable to locate the desired execute flag (",
144  exec_flag,
145  "), the supplied execution flag was likely "
146  "not registered.");
147 
148  return iter->second;
149 }
150 
151 template <typename T>
152 void
154 {
155  // Update all objects active list
157 
158  // Update the execute flag lists of objects
159  for (auto & object_pair : _execute_objects)
160  object_pair.second.updateActive(tid);
161 }
162 
163 template <typename T>
164 void
166 {
167  checkThreadID(tid);
168  const auto iter = _execute_objects.find(EXEC_NONLINEAR);
169  if (iter != _execute_objects.end())
170  iter->second.jacobianSetup(tid);
171 }
172 
173 template <typename T>
174 void
176 {
177  checkThreadID(tid);
178  const auto iter = _execute_objects.find(EXEC_LINEAR);
179  if (iter != _execute_objects.end())
180  iter->second.residualSetup(tid);
181 }
182 
183 template <typename T>
184 void
185 ExecuteMooseObjectWarehouse<T>::setup(const ExecFlagType & exec_flag, THREAD_ID tid /* = 0*/) const
186 {
187  checkThreadID(tid);
188  if (exec_flag == EXEC_INITIAL)
189  initialSetup(tid);
190  else if (exec_flag == EXEC_TIMESTEP_BEGIN)
191  timestepSetup(tid);
192  else if (exec_flag == EXEC_SUBDOMAIN)
193  subdomainSetup(tid);
194  else if (exec_flag == EXEC_NONLINEAR)
195  jacobianSetup(tid);
196  else if (exec_flag == EXEC_LINEAR)
197  residualSetup(tid);
198 }
199 
200 template <typename T>
201 void
203  THREAD_ID tid,
204  bool /*recurse*/)
205 {
206  addObjectMask(object, tid, 0xFFFF);
207 }
208 
209 template <typename T>
210 void
212  THREAD_ID tid,
213  std::uint16_t flag_mask)
214 {
215  // Update list of all objects
217 
218  // Update the execute flag lists of objects
219  std::shared_ptr<SetupInterface> ptr = std::dynamic_pointer_cast<SetupInterface>(object);
220  if (ptr)
221  {
222  MooseEnumIterator iter = ptr->getExecuteOnEnum().begin();
223  for (; iter != ptr->getExecuteOnEnum().end(); ++iter)
224  {
225 
226  auto masked_flag = static_cast<std::uint16_t>(iter->id()) & flag_mask;
227  if (masked_flag != 0)
228  _execute_objects[*iter].addObject(object, tid);
229  }
230  }
231  else
232  mooseError("The object being added (",
233  object->name(),
234  ") must inherit from SetupInterface to be added to the ExecuteMooseObjectWarehouse "
235  "container.");
236 }
237 
238 template <typename T>
239 void
241 {
242  // Sort execute object storage
243  for (auto & object_pair : _execute_objects)
244  object_pair.second.sort(tid);
245 }
246 
void addObjectMask(std::shared_ptr< T > object, THREAD_ID tid=0, std::uint16_t flag_mask=std::numeric_limits< std::uint16_t >::max())
A MultiMooseEnum object to hold "execute_on" flags.
Definition: ExecFlagEnum.h:24
void sort(THREAD_ID tid=0)
Performs a sort using the DependencyResolver.
MooseEnumIterator begin() const
Returns a begin/end iterator to all of the items in the enum.
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:207
A storage container for MooseObjects that inherit from SetupInterface.
const MooseObjectWarehouse< T > & operator[](ExecFlagType exec_flag) const
Retrieve shared pointers for the given thread and execution type for all/active objects.
void residualSetup(THREAD_ID tid=0) const
std::unique_ptr< T_DEST, T_DELETER > dynamic_pointer_cast(std::unique_ptr< T_SRC, T_DELETER > &src)
These are reworked from https://stackoverflow.com/a/11003103.
void setup(const ExecFlagType &exec_flag, THREAD_ID tid=0) const
A class for storing MooseObjects based on execution flag.
void jacobianSetup(THREAD_ID tid=0) const
Convenience methods for calling object setup methods.
std::map< ExecFlagType, MooseObjectWarehouse< T > > _execute_objects
const std::set< ExecFlagType > & items() const
Reference the all the available items.
Definition: ExecFlagEnum.h:73
std::map< ExecFlagType, MooseObjectWarehouse< T > >::const_iterator begin() const
Provide access to begin/end iterators of the underlying map of execution flags.
const ExecFlagType EXEC_TIMESTEP_BEGIN
const ExecFlagEnum & getExecuteOnEnum() const
Return the execute on MultiMooseEnum for this object.
std::vector< MooseEnumItem >::const_iterator MooseEnumIterator
const ExecFlagType EXEC_LINEAR
std::map< ExecFlagType, MooseObjectWarehouse< T > >::iterator getStorageHelper(std::map< ExecFlagType, MooseObjectWarehouse< T >> &objects, ExecFlagType exec_flag) const
A helper method for extracting objects from the various storage containers.
std::map< ExecFlagType, MooseObjectWarehouse< T > >::const_iterator end() const
const ExecFlagType EXEC_NONLINEAR
ExecuteMooseObjectWarehouse(const ExecFlagEnum &flags, bool threaded=true)
Constructor.
Class for containing MooseEnum item information.
Definition: MooseEnumItem.h:21
virtual void addObject(std::shared_ptr< T > object, THREAD_ID tid=0, bool recurse=true)
Adds an object to the storage structure.
virtual void updateActive(THREAD_ID tid=0) override
Update the active status of Kernels.
const ExecFlagType EXEC_SUBDOMAIN
virtual void updateActive(THREAD_ID tid=0)
Updates the active objects storage.
virtual void addObject(std::shared_ptr< T > object, THREAD_ID tid=0, bool recurse=true) override
Adds an object to the storage structure.
unsigned int THREAD_ID
Definition: MooseTypes.h:161
const ExecFlagType EXEC_INITIAL