https://mooseframework.inl.gov
Executor.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 "Executioner.h"
13 #include "ExecutorInterface.h"
14 
15 #include <string>
16 
17 class Problem;
18 class Executor;
19 
26 class Executor : public Executioner, public ExecutorInterface
27 {
28 public:
35  struct Result
36  {
37  Result() : converged(true), _name("NO_NAME") {}
38  Result(const std::string & name) : converged(true), _name(name) {}
39  Result(const MooseObject * obj) : converged(true), _name(obj->name()) {}
40 
46  bool converged = true;
47 
49  std::string reason;
50 
56  std::map<std::string, Result> subs;
57 
62  std::string
63  str(bool success_msg = false, const std::string & indent = "", const std::string & subname = "")
64  {
65  std::string s = indent + label(success_msg, subname) + "\n";
66  for (auto & entry : subs)
67  s += entry.second.str(success_msg, indent + " ", entry.first);
68  return s;
69  }
70 
75  void pass(const std::string & msg, bool overwrite = false)
76  {
77  mooseAssert(converged || overwrite,
78  "cannot override nonconverged executioner result with a passing one");
79  ((void)(overwrite)); // avoid unused error due to assert
80  reason = msg;
81  converged = true;
82  }
83 
86  void fail(const std::string & msg)
87  {
88  reason = msg;
89  converged = false;
90  }
91 
97  bool record(const std::string & name, const Result & r)
98  {
99  subs[name] = r;
100  return r.convergedAll();
101  }
102 
108  bool convergedAll() const
109  {
110  if (!converged)
111  return false;
112  for (auto & entry : subs)
113  if (!entry.second.convergedAll())
114  return false;
115  return true;
116  }
117 
118  private:
119  std::string label(bool success_msg, const std::string & subname = "")
120  {
121  std::string state_str =
122  success_msg || !converged ? (std::string("(") + (converged ? "pass" : "FAIL") + ")") : "";
123  return subname + (subname.empty() ? "" : ":") + _name + state_str +
124  ((success_msg || !converged) && !reason.empty() ? ": " + reason : "");
125  }
126  std::string _name;
127  };
128 
130 
131  virtual ~Executor() {}
132 
133  static InputParameters validParams();
134 
137  Result exec();
138 
139  virtual void execute() override final {}
140 
147  {
148  _result = Result(this);
149  return _result;
150  }
151 
153  virtual bool lastSolveConverged() const override { return _result.convergedAll(); }
154 
155 protected:
160  virtual Result run() = 0;
161 
170 
171 private:
175 };
virtual void execute() override final
Pure virtual execute function MUST be overridden by children classes.
Definition: Executor.h:139
std::string indent(unsigned int spaces)
Create empty string for indenting.
Definition: ConsoleUtils.C:41
std::string str(bool success_msg=false, const std::string &indent="", const std::string &subname="")
Prints a full recursive output of this result object - including all descendant&#39;s results...
Definition: Executor.h:63
Result exec()
This is the main function for executors - this is how executors should invoke child/sub executors - b...
Definition: Executor.C:42
std::string label(bool success_msg, const std::string &subname="")
Definition: Executor.h:119
Class that hold the whole problem being solved.
Definition: Problem.h:19
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
virtual bool lastSolveConverged() const override
Whether the executor and all its sub-executors passed / converged.
Definition: Executor.h:153
Result(const MooseObject *obj)
Definition: Executor.h:39
virtual const std::string & name() const
Get the name of the class.
Definition: MooseBase.h:57
void fail(const std::string &msg)
Marks the result as failing/unconverged with the given msg text describing detail about how things ra...
Definition: Executor.h:86
Result(const std::string &name)
Definition: Executor.h:38
void pass(const std::string &msg, bool overwrite=false)
Marks the result as passing/converged with the given msg text describing detail about how things ran...
Definition: Executor.h:75
bool record(const std::string &name, const Result &r)
Records results from sub/internal executors in a executor&#39;s result.
Definition: Executor.h:97
Result _result
Stores the result representing the outcome from the run function.
Definition: Executor.h:174
std::tuple< CheckState, std::string, std::string > Result
Result from a capability check: the state, the reason, and the documentation.
Result & newResult()
Executors need to return a Result object describing how execution went - rather than constructing Res...
Definition: Executor.h:146
Every object that can be built by the factory should be derived from this class.
Definition: MooseObject.h:28
bool converged
whether or not a executor ran its code successfully - only reports results from the executor itself...
Definition: Executor.h:46
ExecFlagType _begin_flag
The execute-on flag to associate with the beginning of this executor&#39;s execution. ...
Definition: Executor.h:165
Executioners are objects that do the actual work of solving your problem.
Definition: Executioner.h:30
virtual Result run()=0
This function contains the primary execution implementation for a executor.
std::string _name
Definition: Executor.h:126
virtual ~Executor()
Definition: Executor.h:131
Executor(const InputParameters &parameters)
Definition: Executor.C:29
Interface class for classes which interact with Postprocessors.
Class for containing MooseEnum item information.
Definition: MooseEnumItem.h:18
static InputParameters validParams()
Definition: Executor.C:17
class infix_ostream_iterator if void
Definition: InfixIterator.h:26
const InputParameters & parameters() const
Get the parameters of the object.
This object tracks the success/failure state of the executor system as execution proceeds in a simula...
Definition: Executor.h:35
std::map< std::string, Result > subs
Maps a name/label of a executor&#39;s internal/sub executors to the result object returned by running eac...
Definition: Executor.h:56
ExecFlagType _end_flag
The execute-on flag to associate with the end of this executor&#39;s execution.
Definition: Executor.h:169
bool convergedAll() const
Returns false if any single executor in the current hierarchy of results (i.e.
Definition: Executor.h:108
std::string reason
Optional message detailing why an executor passed or failed (i.e. failed to converge).
Definition: Executor.h:49
The Executor class directs the execution flow of simulations.
Definition: Executor.h:26