https://mooseframework.inl.gov
Loading...
Searching...
No Matches
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"
22
24using ActionIterator = std::list<Action *>::iterator;
25
26class MooseMesh;
27class Syntax;
28class ActionFactory;
29class FEProblem;
30class PhysicsBase;
31
36{
37public:
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 & getAction(const std::string & name)
140 {
141 return const_cast<T &>(static_cast<const ActionWarehouse *>(this)->getAction<T>(name));
142 }
143
148 template <class T>
149 T * getPhysics(const std::string & name) const
150 {
151 auto physics = const_cast<T *>(&getAction<T>(name));
152 if (!dynamic_cast<const PhysicsBase *>(physics))
153 mooseError("The Physics requested of type '",
154 MooseUtils::prettyCppType<T>(),
155 "' and name '",
156 name,
157 "' is not derived from the PhysicsBase class");
158 return physics;
159 }
160
164 template <class T>
165 std::vector<const T *> getActions()
166 {
167 // we need to create the map first to ensure that all actions in the map are unique
168 // and the actions are sorted by their names
169 typename std::map<std::string, const std::shared_ptr<T>> actions;
170 for (auto act_ptr : _all_ptrs)
171 {
172 auto p = std::dynamic_pointer_cast<T>(act_ptr);
173 if (p)
174 actions.insert(std::make_pair(act_ptr->name(), p));
175 }
176 // construct the vector from the map entries
177 std::vector<const T *> action_vector;
178 for (auto & pair : actions)
179 action_vector.push_back(pair.second.get());
180 return action_vector;
181 }
182
186 template <class T>
187 std::vector<T *> getPhysics()
188 {
189 const auto physics_vector = getActions<T>();
190 for (const auto phys_ptr : physics_vector)
191 if (!dynamic_cast<const PhysicsBase *>(phys_ptr))
192 mooseError("The Physics requested of type '",
193 MooseUtils::prettyCppType<T>(),
194 "' and name '",
195 phys_ptr->name(),
196 "' is not derived from the PhysicsBase class");
197 return physics_vector;
198 }
199
206 template <class T>
207 const T * getActionByTask(const std::string & task)
208 {
209 const auto it = _action_blocks.find(task);
210 if (it == _action_blocks.end())
211 return nullptr;
212
213 T * p = nullptr;
214 for (const auto & action : it->second)
215 {
216 T * tp = dynamic_cast<T *>(action);
217 if (tp)
218 {
219 if (p)
220 mooseError("More than one actions have been detected in getActionByTask for the task '",
221 task,
222 "' in the app '",
224 "'");
225 else
226 p = tp;
227 }
228 }
229 return p;
230 }
231
232 void setFinalTask(const std::string & task);
233
237 bool hasActions(const std::string & task) const;
238
243 void executeAllActions();
244
249 void executeActionsWithAction(const std::string & task_name);
250
256 void showActionDependencies(bool state = true) { _show_action_dependencies = state; }
257
263 void showActions(bool state = true) { _show_actions = state; }
264
270 void showParser(bool state = true) { _show_parser = state; }
271
273 Syntax & syntax() { return _syntax; }
274
275 // We are not really using the reference counting capabilities of
276 // shared pointers here, just their memory management capability.
277 // Therefore, _mesh is actually being used more like a unique_ptr in
278 // this context. Since full support for unique_ptr is not quite
279 // available yet, we've implemented it as a std::shared_ptr.
280 std::shared_ptr<MooseMesh> & mesh() { return _mesh; }
281 const std::shared_ptr<MooseMesh> & getMesh() const { return _mesh; }
282
283 std::shared_ptr<MooseMesh> & displacedMesh() { return _displaced_mesh; }
284 const std::shared_ptr<MooseMesh> & getDisplacedMesh() const { return _displaced_mesh; }
285
286 std::shared_ptr<FEProblemBase> & problemBase() { return _problem; }
287 std::shared_ptr<FEProblem> problem();
288 MooseApp & mooseApp() { return _app; }
289 const std::string & getMooseAppName();
290 const std::string & getCurrentTaskName() const { return _current_task; }
291
295 const Action * getCurrentAction() const { return _current_action; }
299 std::string getCurrentActionName() const;
300
304 bool hasTask(const std::string & task) const;
308 bool isTaskComplete(const std::string & task) const;
309
310protected:
318 void buildBuildableActions(const std::string & task);
319
325 std::vector<UserObjectName> getUserObjectParamDependencies(const InputParameters & params) const;
326
334 void sortUserObjectActions(std::list<Action *> & actions) const;
335
336 std::vector<std::shared_ptr<Action>> _all_ptrs;
337
345 std::map<std::string, std::list<Action *>> _action_blocks;
347 std::vector<std::string> _ordered_names;
349 std::set<std::string> _completed_tasks;
351 std::set<std::string> _unsatisfied_dependencies;
352
359
366
367 // When executing the actions in the warehouse, this string will always contain
368 // the current task name
369 std::string _current_task;
370 // The current action that is running
372
373 //
374 // data created by actions
375 //
376
378 std::shared_ptr<MooseMesh> _mesh;
379
381 std::shared_ptr<MooseMesh> _displaced_mesh;
382
384 std::shared_ptr<FEProblemBase> _problem;
385
386private:
388 std::string _final_task;
389
390 const std::list<Action *> _empty_action_list;
391
393 mutable std::mutex _completed_tasks_mutex;
394};
std::list< Action * >::iterator ActionIterator
alias to hide implementation details
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application.
Definition MooseError.h:311
Specialized factory for generic Action System objects.
Storage for action instances.
std::mutex _completed_tasks_mutex
Mutex for preventing read/write races for _completed_tasks.
const std::string & getMooseAppName()
const std::shared_ptr< MooseMesh > & getDisplacedMesh() const
void checkUnsatisfiedActions() const
This method checks the actions stored in the warehouse against the list of required registered action...
void setFinalTask(const std::string &task)
void printInputFile(std::ostream &out)
This method uses the Actions in the warehouse to reproduce the input file.
std::shared_ptr< MooseMesh > & mesh()
std::string _final_task
Last task to run before (optional) early termination - blank means no early termination.
std::shared_ptr< FEProblemBase > & problemBase()
std::map< std::string, std::list< Action * > > _action_blocks
Pointers to the actual parsed input file blocks.
void showActionDependencies(bool state=true)
This method sets a Boolean which is used to print information about action dependencies before variou...
std::string _current_task
const Action * getCurrentAction() const
bool _show_actions
Whether or not the action warehouse prints the action execution information.
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.
const T * getActionByTask(const std::string &task)
Retrieve the action on a specific task with its type.
std::set< std::string > _unsatisfied_dependencies
Use to store the current list of unsatisfied dependencies.
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.
const std::string & getCurrentTaskName() const
std::string getCurrentActionName() const
bool _show_action_dependencies
Whether or not the action warehouse prints the action dependency information.
T * getPhysics(const std::string &name) const
Retrieve a Physics with its name and the desired type.
MooseApp & mooseApp()
void showParser(bool state=true)
This method sets a Boolean which is used to show debugging information when actions are inserted in t...
const std::shared_ptr< MooseMesh > & getMesh() const
const std::list< Action * > _empty_action_list
std::vector< UserObjectName > getUserObjectParamDependencies(const InputParameters &params) const
std::vector< const T * > getActions()
Retrieve all actions in a specific type ordered by their names.
bool empty() const
returns a Boolean indicating whether the warehouse is empty or not.
bool isTaskComplete(const std::string &task) const
std::shared_ptr< FEProblem > problem()
bool _generator_valid
Flag to indicate whether or not there is an active iterator on this class.
std::vector< T * > getPhysics()
Retrieve all Physics with a specific type ordered by their names.
Syntax & _syntax
Reference to a "syntax" of actions.
std::vector< std::string > _ordered_names
The container that holds the sorted action names from the DependencyResolver.
const T & getAction(const std::string &name) const
Retrieve an action with its name and the desired type.
std::shared_ptr< FEProblemBase > _problem
Problem class.
void sortUserObjectActions(std::list< Action * > &actions) const
Reorder the UserObject-constructing actions actions so that a UserObject that references another (thr...
bool hasActions(const std::string &task) const
Check if Actions associated with passed in task exist.
std::shared_ptr< MooseMesh > & displacedMesh()
ActionIterator actionBlocksWithActionEnd(const std::string &task)
T & getAction(const std::string &name)
ActionFactory & _action_factory
The Factory that builds Actions.
std::vector< std::shared_ptr< Action > > _all_ptrs
ActionIterator actionBlocksWithActionBegin(const std::string &task)
Iterators to the Actions in the warehouse.
bool _show_parser
Whether or not to print messages when actions are inserted in the warehouse by the parser.
std::set< std::string > _completed_tasks
The completed tasks.
void build()
Builds all auto-buildable tasks.
void showActions(bool state=true)
This method sets a Boolean which is used to show information about action execution of various wareho...
void executeActionsWithAction(const std::string &task_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.
std::shared_ptr< MooseMesh > _displaced_mesh
Possible mesh for displaced problem.
void printActionDependencySets() const
This method is used only during debugging when show_actions is set to true.
void executeAllActions()
This method loops over all actions in the warehouse and executes them.
bool hasTask(const std::string &task) const
void addActionBlock(std::shared_ptr< Action > blk)
This method add an Action instance to the warehouse.
std::shared_ptr< MooseMesh > _mesh
Mesh class.
Base class for actions.
Definition Action.h:38
An inteface for the _console for outputting to the Console object.
Specialization of SubProblem for solving nonlinear equations plus auxiliary equations.
Definition FEProblem.h:21
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system.
Base class for MOOSE-based applications.
Definition MooseApp.h:110
MooseMesh wraps a libMesh::Mesh object and enhances its capabilities by caching additional data and s...
Definition MooseMesh.h:95
Base class to help creating an entire physics.
Definition PhysicsBase.h:31
Holding syntax for parsing input files.
Definition Syntax.h:22
std::string stringify(const T &t)
conversion to string
Definition Conversion.h:64