Scine::Readuct  6.0.0
This is the SCINE module ReaDuct.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Pages
Task.h
Go to the documentation of this file.
1 
7 #ifndef READUCT_TASK_H_
8 #define READUCT_TASK_H_
9 
11 #include <Core/Log.h>
13 #include <Utils/Settings.h>
14 #include <yaml-cpp/yaml.h>
15 #include <map>
16 #include <memory>
17 #include <string>
18 #include <vector>
19 
20 namespace Scine {
21 namespace Core {
22 class Calculator;
23 }
24 namespace Readuct {
25 
29 class Task {
30  public:
31  using SystemsMap = std::map<std::string, std::shared_ptr<Core::Calculator>>;
32 
33  static std::shared_ptr<Core::Calculator> copyCalculator(SystemsMap& systems, const std::string& name,
34  const std::string& taskName) {
35  const auto findIter = systems.find(name);
36  if (findIter == systems.end()) {
37  throw std::runtime_error("System name '" + name + "' is missing in " + taskName);
38  }
39  auto calc = findIter->second->clone();
40  calc->results() = findIter->second->results();
41  return calc;
42  }
43 
50  Task(std::vector<std::string> input, std::vector<std::string> output, std::shared_ptr<Core::Log> logger = nullptr)
51  : _input(std::move(input)), _output(std::move(output)), _logger(std::move(logger)) {
52  if (!_logger) {
53  _logger = std::make_shared<Core::Log>();
54  }
55 
56  if (_input.empty()) {
57  throw std::runtime_error("No input systems specified!");
58  }
59  }
60 
61  virtual ~Task() = default;
62 
67  virtual std::string name() const = 0;
77  virtual bool
78  run(SystemsMap& systems, Utils::UniversalSettings::ValueCollection taskSettings, bool test = false,
79  std::vector<std::function<void(const int&, const Utils::AtomCollection&, const Utils::Results&, const std::string&)>>
80  observers = {}) const = 0;
85  const std::vector<std::string>& input() const {
86  return _input;
87  };
92  const std::vector<std::string>& output() const {
93  return _output;
94  };
99  if (_input.size() > 1) {
100  _logger->warning
101  << " Warning: More than one input system was specified. Only taking first and ignoring all others.\n";
102  }
103  };
108  if (_output.size() > 1) {
109  _logger->warning
110  << " Warning: More than one output system was specified. Only taking first and ignoring all others.\n";
111  }
112  };
113 
114  bool stopOnErrorExtraction(Utils::UniversalSettings::ValueCollection& taskSettings) const {
115  bool stopOnError = true;
116  if (taskSettings.valueExists("allow_unconverged")) {
117  _logger->warning << " The option 'allow_unconverged' is deprecated.\n"
118  << " It has been replaced with 'stop_on_error',\n"
119  << " which is now available for all tasks and is defaulted to 'true'.\n\n";
120  stopOnError = !taskSettings.extract("allow_unconverged", false);
121  }
122  return taskSettings.extract("stop_on_error", stopOnError);
123  };
124 
125  static std::string falseTaskSettingsErrorMessage(const std::string& name) {
126  return " You gave Task settings for the " + name + ",\n" + " but the only possible setting for this task, are the\n" +
127  " 'stop_on_error' option to control whether ReaDuct fails\n" +
128  " with a failed calculation or simply returns false\n" +
129  " and the 'silent_stdout_calculator' option to control whether\n"
130  " the standard output of the calculator should be printed.\n"
131  " You might want to specify the settings you put into the task settings\n" +
132  " in the systems section.";
133  }
134 
135  protected:
136  const std::vector<std::string> _input;
137  const std::vector<std::string> _output;
138  std::shared_ptr<Core::Log> _logger;
139 };
140 
141 } // namespace Readuct
142 } // namespace Scine
143 
144 #endif // READUCT_TASK_H_
void warningIfMultipleOutputsGiven() const
Warn if more than one output system was specified.
Definition: Task.h:107
const std::vector< std::string > & input() const
Getter for the expected names of the input systems.
Definition: Task.h:85
Task(std::vector< std::string > input, std::vector< std::string > output, std::shared_ptr< Core::Log > logger=nullptr)
Construct a new Task.
Definition: Task.h:50
virtual std::string name() const =0
Getter for the tasks name.
virtual bool run(SystemsMap &systems, Utils::UniversalSettings::ValueCollection taskSettings, bool test=false, std::vector< std::function< void(const int &, const Utils::AtomCollection &, const Utils::Results &, const std::string &)>> observers={}) const =0
Executes the actual task represented by this class.
void warningIfMultipleInputsGiven() const
Warn if more than one input system was specified.
Definition: Task.h:98
const std::vector< std::string > & output() const
Getter for the names of the output systems generated by this task.
Definition: Task.h:92
The base class for all tasks in Readuct.
Definition: Task.h:29