https://mooseframework.inl.gov
ActionWarehouse.h
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 
10 #pragma once
11 
12 #include <string>
13 #include <set>
14 #include <map>
15 #include <vector>
16 #include <list>
17 #include <ostream>
18 
19 // MOOSE includes
20 #include "Action.h"
21 #include "ConsoleStreamInterface.h"
22 
24 using ActionIterator = std::list<Action *>::iterator;
25 
26 class MooseMesh;
27 class Syntax;
28 class ActionFactory;
29 class FEProblem;
30 class PhysicsBase;
31 
36 {
37 public:
38  ActionWarehouse(MooseApp & app, Syntax & syntax, ActionFactory & factory);
40 
45  void build();
46 
50  void clear();
51 
55  bool empty() const { return _action_blocks.empty(); }
56 
60  void addActionBlock(std::shared_ptr<Action> blk);
61 
67  void checkUnsatisfiedActions() const;
68 
74  void printActionDependencySets() const;
75 
82  void printInputFile(std::ostream & out);
83 
85 
91  ActionIterator actionBlocksWithActionBegin(const std::string & task);
92  ActionIterator actionBlocksWithActionEnd(const std::string & task);
94 
98  const std::vector<std::shared_ptr<Action>> & allActionBlocks() const;
99 
104  const std::list<Action *> & getActionListByName(const std::string & task) const;
105 
110  template <class T>
111  const T & getAction(const std::string & name) const
112  {
113  typename std::shared_ptr<T> p;
114  for (auto act_ptr : _all_ptrs)
115  {
116  if (act_ptr->name() == name)
117  {
118  p = std::dynamic_pointer_cast<T>(act_ptr);
119  if (p)
120  break;
121  }
122  }
123  if (!p)
124  {
125  std::vector<std::string> all_names;
126  for (auto act_ptr : _all_ptrs)
127  if (!act_ptr->name().empty())
128  all_names.push_back(act_ptr->name());
129  else
130  all_names.push_back("unnamed");
131  mooseError("Action with name '",
132  name,
133  "' does not exist.\n These are the Actions that do exist:",
134  Moose::stringify(all_names));
135  }
136  return *p;
137  }
138  template <class T>
139  T * getPhysics(const std::string & name) const
140  {
141  auto physics = const_cast<T *>(&getAction<T>(name));
142  if (!dynamic_cast<const PhysicsBase *>(physics))
143  mooseError("The Physics requested of type '",
144  MooseUtils::prettyCppType<T>(),
145  "' and name '",
146  name,
147  "' is not derived from the PhysicsBase class");
148  return physics;
149  }
150 
154  template <class T>
155  std::vector<const T *> getActions()
156  {
157  // we need to create the map first to ensure that all actions in the map are unique
158  // and the actions are sorted by their names
159  typename std::map<std::string, const std::shared_ptr<T>> actions;
160  for (auto act_ptr : _all_ptrs)
161  {
162  auto p = std::dynamic_pointer_cast<T>(act_ptr);
163  if (p)
164  actions.insert(std::make_pair(act_ptr->name(), p));
165  }
166  // construct the vector from the map entries
167  std::vector<const T *> action_vector;
168  for (auto & pair : actions)
169  action_vector.push_back(pair.second.get());
170  return action_vector;
171  }
172 
176  template <class T>
177  std::vector<T *> getPhysics()
178  {
179  const auto physics_vector = getActions<T>();
180  for (const auto phys_ptr : physics_vector)
181  if (!dynamic_cast<const PhysicsBase *>(phys_ptr))
182  mooseError("The Physics requested of type '",
183  MooseUtils::prettyCppType<T>(),
184  "' and name '",
185  phys_ptr->name(),
186  "' is not derived from the PhysicsBase class");
187  return physics_vector;
188  }
189 
196  template <class T>
197  const T * getActionByTask(const std::string & task)
198  {
199  const auto it = _action_blocks.find(task);
200  if (it == _action_blocks.end())
201  return nullptr;
202 
203  T * p = nullptr;
204  for (const auto & action : it->second)
205  {
206  T * tp = dynamic_cast<T *>(action);
207  if (tp)
208  {
209  if (p)
210  mooseError("More than one actions have been detected in getActionByTask for the task '",
211  task,
212  "' in the app '",
213  getMooseAppName(),
214  "'");
215  else
216  p = tp;
217  }
218  }
219  return p;
220  }
221 
222  void setFinalTask(const std::string & task);
223 
227  bool hasActions(const std::string & task) const;
228 
233  void executeAllActions();
234 
239  void executeActionsWithAction(const std::string & name);
240 
246  void showActionDependencies(bool state = true) { _show_action_dependencies = state; }
247 
253  void showActions(bool state = true) { _show_actions = state; }
254 
260  void showParser(bool state = true) { _show_parser = state; }
261 
263  Syntax & syntax() { return _syntax; }
264 
265  // We are not really using the reference counting capabilities of
266  // shared pointers here, just their memory management capability.
267  // Therefore, _mesh is actually being used more like a unique_ptr in
268  // this context. Since full support for unique_ptr is not quite
269  // available yet, we've implemented it as a std::shared_ptr.
270  std::shared_ptr<MooseMesh> & mesh() { return _mesh; }
271  const std::shared_ptr<MooseMesh> & getMesh() const { return _mesh; }
272 
273  std::shared_ptr<MooseMesh> & displacedMesh() { return _displaced_mesh; }
274  const std::shared_ptr<MooseMesh> & getDisplacedMesh() const { return _displaced_mesh; }
275 
276  std::shared_ptr<FEProblemBase> & problemBase() { return _problem; }
277  std::shared_ptr<FEProblem> problem();
278  MooseApp & mooseApp() { return _app; }
279  const std::string & getMooseAppName();
280  const std::string & getCurrentTaskName() const { return _current_task; }
281 
285  const Action * getCurrentAction() const { return _current_action; }
289  std::string getCurrentActionName() const;
290 
294  bool hasTask(const std::string & task) const;
298  bool isTaskComplete(const std::string & task) const;
299 
300 protected:
308  void buildBuildableActions(const std::string & task);
309 
310  std::vector<std::shared_ptr<Action>> _all_ptrs;
311 
319  std::map<std::string, std::list<Action *>> _action_blocks;
321  std::vector<std::string> _ordered_names;
323  std::set<std::string> _completed_tasks;
325  std::set<std::string> _unsatisfied_dependencies;
326 
333 
340 
341  // When executing the actions in the warehouse, this string will always contain
342  // the current task name
343  std::string _current_task;
344  // The current action that is running
346 
347  //
348  // data created by actions
349  //
350 
352  std::shared_ptr<MooseMesh> _mesh;
353 
355  std::shared_ptr<MooseMesh> _displaced_mesh;
356 
358  std::shared_ptr<FEProblemBase> _problem;
359 
360 private:
362  std::string _final_task;
363 
364  const std::list<Action *> _empty_action_list;
365 
367  mutable std::mutex _completed_tasks_mutex;
368 };
std::string name(const ElemQuality q)
const T & getAction(const std::string &name) const
Retrieve an action with its name and the desired type.
bool _show_parser
Whether or not to print messages when actions are inserted in the warehouse by the parser...
void checkUnsatisfiedActions() const
This method checks the actions stored in the warehouse against the list of required registered action...
MooseApp & mooseApp()
std::list< Action * >::iterator ActionIterator
alias to hide implementation details
const Action * getCurrentAction() const
Specialization of SubProblem for solving nonlinear equations plus auxiliary equations.
Definition: FEProblem.h:20
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:302
bool isTaskComplete(const std::string &task) const
Syntax & _syntax
Reference to a "syntax" of actions.
std::shared_ptr< MooseMesh > & displacedMesh()
Base class to help creating an entire physics.
Definition: PhysicsBase.h:30
const std::shared_ptr< MooseMesh > & getMesh() const
bool hasTask(const std::string &task) const
const std::string & getMooseAppName()
void setFinalTask(const std::string &task)
std::string getCurrentActionName() const
void showActions(bool state=true)
This method sets a Boolean which is used to show information about action execution of various wareho...
Base class for MOOSE-based applications.
Definition: MooseApp.h:96
Storage for action instances.
void addActionBlock(std::shared_ptr< Action > blk)
This method add an Action instance to the warehouse.
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 showActionDependencies(bool state=true)
This method sets a Boolean which is used to print information about action dependencies before variou...
void printInputFile(std::ostream &out)
This method uses the Actions in the warehouse to reproduce the input file.
void executeActionsWithAction(const std::string &name)
This method executes only the actions in the warehouse that satisfy the task passed in...
const std::list< Action * > & getActionListByName(const std::string &task) const
Retrieve a constant list of Action pointers associated with the passed in task.
void printActionDependencySets() const
This method is used only during debugging when show_actions is set to true.
Base class for actions.
Definition: Action.h:33
std::shared_ptr< FEProblemBase > & problemBase()
std::string _final_task
Last task to run before (optional) early termination - blank means no early termination.
std::vector< std::string > _ordered_names
The container that holds the sorted action names from the DependencyResolver.
std::shared_ptr< MooseMesh > & mesh()
void showParser(bool state=true)
This method sets a Boolean which is used to show debugging information when actions are inserted in t...
bool empty() const
returns a Boolean indicating whether the warehouse is empty or not.
std::set< std::string > _completed_tasks
The completed tasks.
std::mutex _completed_tasks_mutex
Mutex for preventing read/write races for _completed_tasks.
Specialized factory for generic Action System objects.
Definition: ActionFactory.h:50
An inteface for the _console for outputting to the Console object.
MooseMesh wraps a libMesh::Mesh object and enhances its capabilities by caching additional data and s...
Definition: MooseMesh.h:88
std::map< std::string, std::list< Action * > > _action_blocks
Pointers to the actual parsed input file blocks.
std::string stringify(const T &t)
conversion to string
Definition: Conversion.h:64
std::shared_ptr< MooseMesh > _displaced_mesh
Possible mesh for displaced problem.
bool _generator_valid
Flag to indicate whether or not there is an active iterator on this class.
const std::string & getCurrentTaskName() const
std::vector< T * > getPhysics()
Retrieve all Physics with a specific type ordered by their names.
const std::list< Action * > _empty_action_list
std::vector< std::shared_ptr< Action > > _all_ptrs
std::shared_ptr< FEProblemBase > _problem
Problem class.
bool hasActions(const std::string &task) const
Check if Actions associated with passed in task exist.
ActionWarehouse(MooseApp &app, Syntax &syntax, ActionFactory &factory)
std::shared_ptr< MooseMesh > _mesh
Mesh class.
const std::shared_ptr< MooseMesh > & getDisplacedMesh() const
bool _show_actions
Whether or not the action warehouse prints the action execution information.
MooseApp & _app
The MooseApp this Warehouse is associated with.
const std::vector< std::shared_ptr< Action > > & allActionBlocks() const
Returns a reference to all of the actions.
void build()
Builds all auto-buildable tasks.
T * getPhysics(const std::string &name) const
Holding syntax for parsing input files.
Definition: Syntax.h:21
const T * getActionByTask(const std::string &task)
Retrieve the action on a specific task with its type.
void executeAllActions()
This method loops over all actions in the warehouse and executes them.
ActionIterator actionBlocksWithActionEnd(const std::string &task)
std::set< std::string > _unsatisfied_dependencies
Use to store the current list of unsatisfied dependencies.
Action * _current_action
bool _show_action_dependencies
Whether or not the action warehouse prints the action dependency information.
std::shared_ptr< FEProblem > problem()
std::vector< const T * > getActions()
Retrieve all actions in a specific type ordered by their names.
ActionIterator actionBlocksWithActionBegin(const std::string &task)
Iterators to the Actions in the warehouse.
void clear()
This method deletes all of the Actions in the warehouse.
void buildBuildableActions(const std::string &task)
This method auto-builds all Actions that needs to be built and adds them to ActionWarehouse.
ActionFactory & _action_factory
The Factory that builds Actions.
std::string _current_task