Scine::Readuct  5.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>
12 #include <Utils/Settings.h>
13 #include <yaml-cpp/yaml.h>
14 #include <map>
15 #include <memory>
16 #include <string>
17 #include <vector>
18 
19 namespace Scine {
20 namespace Core {
21 class Calculator;
22 }
23 namespace Readuct {
24 
28 class Task {
29  public:
30  using SystemsMap = std::map<std::string, std::shared_ptr<Core::Calculator>>;
31 
32  static std::shared_ptr<Core::Calculator> copyCalculator(SystemsMap& systems, const std::string& name,
33  const std::string& taskName) {
34  const auto findIter = systems.find(name);
35  if (findIter == systems.end()) {
36  throw std::runtime_error("System name '" + name + "' is missing in " + taskName);
37  }
38  return findIter->second->clone();
39  }
40 
47  Task(std::vector<std::string> input, std::vector<std::string> output, std::shared_ptr<Core::Log> logger = nullptr)
48  : _input(std::move(input)), _output(std::move(output)), _logger(std::move(logger)) {
49  if (!_logger) {
50  _logger = std::make_shared<Core::Log>();
51  }
52 
53  if (_input.empty()) {
54  throw std::runtime_error("No input systems specified!");
55  }
56  }
57 
58  virtual ~Task() = default;
59 
64  virtual std::string name() const = 0;
74  virtual bool
75  run(SystemsMap& systems, Utils::UniversalSettings::ValueCollection taskSettings, bool test = false,
76  std::vector<std::function<void(const int&, const Utils::AtomCollection&, const Utils::Results&, const std::string&)>>
77  observers = {}) const = 0;
82  const std::vector<std::string>& input() const {
83  return _input;
84  };
89  const std::vector<std::string>& output() const {
90  return _output;
91  };
96  if (_input.size() > 1) {
97  _logger->warning
98  << " Warning: More than one input system was specified. Only taking first and ignoring all others.\n";
99  }
100  };
105  if (_output.size() > 1) {
106  _logger->warning
107  << " Warning: More than one output system was specified. Only taking first and ignoring all others.\n";
108  }
109  };
110 
111  bool stopOnErrorExtraction(Utils::UniversalSettings::ValueCollection& taskSettings) const {
112  bool stopOnError = true;
113  if (taskSettings.valueExists("allow_unconverged")) {
114  _logger->warning << " The option 'allow_unconverged' is deprecated.\n"
115  << " It has been replaced with 'stop_on_error',\n"
116  << " which is now available for all tasks and is defaulted to 'true'.\n\n";
117  stopOnError = !taskSettings.extract("allow_unconverged", false);
118  }
119  return taskSettings.extract("stop_on_error", stopOnError);
120  };
121 
122  static std::string falseTaskSettingsErrorMessage(const std::string& name) {
123  return " You gave Task settings for the " + name + ",\n" + " but the only possible setting for this task, are the\n" +
124  " 'stop_on_error' option to control whether ReaDuct fails\n" +
125  " with a failed calculation or simply returns false\n" +
126  " and the 'silent_stdout_calculator' option to control whether\n"
127  " the standard output of the calculator should be printed.\n"
128  " You might want to specify the settings you put into the task settings\n" +
129  " in the systems section.";
130  }
131 
132  protected:
133  const std::vector<std::string> _input;
134  const std::vector<std::string> _output;
135  std::shared_ptr<Core::Log> _logger;
136 };
137 
138 } // namespace Readuct
139 } // namespace Scine
140 
141 #endif // READUCT_TASK_H_
void warningIfMultipleOutputsGiven() const
Warn if more than one output system was specified.
Definition: Task.h:104
const std::vector< std::string > & input() const
Getter for the expected names of the input systems.
Definition: Task.h:82
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:47
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:95
const std::vector< std::string > & output() const
Getter for the names of the output systems generated by this task.
Definition: Task.h:89
The base class for all tasks in Readuct.
Definition: Task.h:28