www.mooseframework.org
ActionWarehouse.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 #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 
35 {
36 public:
37  ActionWarehouse(MooseApp & app, Syntax & syntax, ActionFactory & factory);
39 
44  void build();
45 
49  void clear();
50 
54  bool empty() const { return _action_blocks.empty(); }
55 
59  void addActionBlock(std::shared_ptr<Action> blk);
60 
66  void checkUnsatisfiedActions() const;
67 
73  void printActionDependencySets() const;
74 
81  void printInputFile(std::ostream & out);
82 
84 
90  ActionIterator actionBlocksWithActionBegin(const std::string & task);
91  ActionIterator actionBlocksWithActionEnd(const std::string & task);
93 
97  const std::vector<std::shared_ptr<Action>> & allActionBlocks() const;
98 
103  const std::list<Action *> & getActionListByName(const std::string & task) const;
104 
109  template <class T>
110  const T & getAction(const std::string & name)
111  {
112  typename std::shared_ptr<T> p;
113  for (auto act_ptr : _all_ptrs)
114  {
115  if (act_ptr->name() == name)
116  {
117  p = std::dynamic_pointer_cast<T>(act_ptr);
118  if (p)
119  break;
120  }
121  }
122  if (!p)
123  mooseError("Action with name being ", name, " does not exist");
124  return *p;
125  }
126 
130  template <class T>
131  std::vector<const T *> getActions()
132  {
133  // we need to create the map first to ensure that all actions in the map are unique
134  // and the actions are sorted by their names
135  typename std::map<std::string, const std::shared_ptr<T>> actions;
136  for (auto act_ptr : _all_ptrs)
137  {
138  auto p = std::dynamic_pointer_cast<T>(act_ptr);
139  if (p)
140  actions.insert(std::make_pair(act_ptr->name(), p));
141  }
142  // construct the vector from the map entries
143  std::vector<const T *> action_vector;
144  for (auto & pair : actions)
145  action_vector.push_back(pair.second.get());
146  return action_vector;
147  }
148 
155  template <class T>
156  const T * getActionByTask(const std::string & task)
157  {
158  const auto it = _action_blocks.find(task);
159  if (it == _action_blocks.end())
160  return nullptr;
161 
162  T * p = nullptr;
163  for (const auto & action : it->second)
164  {
165  T * tp = dynamic_cast<T *>(action);
166  if (tp)
167  {
168  if (p)
169  mooseError("More than one actions have been detected in getActionByTask");
170  else
171  p = tp;
172  }
173  }
174  return p;
175  }
176 
177  void setFinalTask(const std::string & task);
178 
182  bool hasActions(const std::string & task) const;
183 
188  void executeAllActions();
189 
194  void executeActionsWithAction(const std::string & name);
195 
201  void showActions(bool state = true) { _show_actions = state; }
202 
203  void showParser(bool state = true) { _show_parser = state; }
204 
206  Syntax & syntax() { return _syntax; }
207 
208  // We are not really using the reference counting capabilities of
209  // shared pointers here, just their memory management capability.
210  // Therefore, _mesh is actually being used more like a unique_ptr in
211  // this context. Since full support for unique_ptr is not quite
212  // available yet, we've implemented it as a std::shared_ptr.
213  std::shared_ptr<MooseMesh> & mesh() { return _mesh; }
214  const std::shared_ptr<MooseMesh> & getMesh() const { return _mesh; }
215 
216  std::shared_ptr<MooseMesh> & displacedMesh() { return _displaced_mesh; }
217  const std::shared_ptr<MooseMesh> & getDisplacedMesh() const { return _displaced_mesh; }
218 
219  std::shared_ptr<FEProblemBase> & problemBase() { return _problem; }
220  std::shared_ptr<FEProblem> problem();
221  MooseApp & mooseApp() { return _app; }
222  const std::string & getCurrentTaskName() const { return _current_task; }
223 
224  std::string getCurrentActionName() const;
225 
226 protected:
234  void buildBuildableActions(const std::string & task);
235 
236  std::vector<std::shared_ptr<Action>> _all_ptrs;
237 
245  std::map<std::string, std::list<Action *>> _action_blocks;
247  std::map<std::string, std::vector<Action *>> _requested_action_blocks;
249  std::vector<std::string> _ordered_names;
251  std::set<std::string> _unsatisfied_dependencies;
252 
259 
260  // DEBUGGING
263 
264  // When executing the actions in the warehouse, this string will always contain
265  // the current task name
266  std::string _current_task;
267 
268  //
269  // data created by actions
270  //
271 
273  std::shared_ptr<MooseMesh> _mesh;
274 
276  std::shared_ptr<MooseMesh> _displaced_mesh;
277 
279  std::shared_ptr<FEProblemBase> _problem;
280 
281 private:
283  std::string _final_task;
284 
286 
287  const std::list<Action *> _empty_action_list;
288 };
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
Specialization of SubProblem for solving nonlinear equations plus auxiliary equations.
Definition: FEProblem.h:24
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:207
Syntax & _syntax
Reference to a "syntax" of actions.
std::shared_ptr< MooseMesh > & displacedMesh()
const std::shared_ptr< MooseMesh > & getMesh() const
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 debugging information during various warehouse opera...
Base class for MOOSE-based applications.
Definition: MooseApp.h:59
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 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.
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)
bool empty() const
returns a Boolean indicating whether the warehouse is empty or not.
Specialized factory for generic Action System objects.
Definition: ActionFactory.h:64
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:74
std::map< std::string, std::list< Action * > > _action_blocks
Pointers to the actual parsed input file blocks.
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
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
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.
Holding syntax for parsing input files.
Definition: Syntax.h:20
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.
std::map< std::string, std::vector< Action * > > _requested_action_blocks
Action blocks that have been requested.
const T & getAction(const std::string &name)
Retrieve an action with its name and the desired type.
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.
ActionIterator _act_iter
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