15 #include <Core/ModuleManager.h>
22 #include "boost/exception/diagnostic_information.hpp"
34 std::tuple<std::map<std::string, std::tuple<std::string, std::string, std::shared_ptr<Core::Calculator>>>,
35 std::vector<std::shared_ptr<Task>>, std::vector<Utils::UniversalSettings::ValueCollection>>
36 loadYamlFile(
const std::string& filename) {
38 std::map<std::string, std::tuple<std::string, std::string, std::shared_ptr<Core::Calculator>>> systems;
41 auto logger = std::make_shared<Core::Log>();
43 auto input = YAML::LoadFile(filename);
45 std::vector<std::string> keywords{
"systems",
"tasks"};
46 Utils::checkYamlKeyRecognition(input, keywords);
49 auto systemsInput = input[
"systems"];
50 for (
size_t i = 0; i < systemsInput.size(); i++) {
52 std::vector<std::string> keywords{
"name",
"method_family",
"path",
"program",
"settings"};
53 Utils::checkYamlKeyRecognition(systemsInput[i], keywords);
55 auto current = systemsInput[i];
56 if (!current[
"name"]) {
57 throw std::logic_error(
"System no. " + std::to_string(i + 1) +
" is missing a name.\n");
59 std::string name = current[
"name"].as<std::string>();
60 if (!current[
"method_family"]) {
61 throw std::logic_error(
"A method_family is missing for the system: '" + name +
"'.\n");
63 std::string methodFamily = current[
"method_family"].as<std::string>();
64 if (!current[
"path"]) {
65 throw std::logic_error(
"An input path is missing for the system: '" + name +
"'.\n");
67 std::string path = current[
"path"].as<std::string>();
69 if (
auto node = current[
"program"]) {
70 program = current[
"program"].as<std::string>();
76 auto calc = Utils::CalculationRoutines::getCalculator(methodFamily, program);
78 if (
auto settingsnode = current[
"settings"]) {
79 nodeToSettings(calc->settings(), settingsnode);
82 calc->setStructure(readResults.first);
83 if (!calc->settings().valid()) {
84 calc->settings().throwIncorrectSettings();
86 if (program.empty()) {
89 Utils::CalculationRoutines::inputPreparation(methodFamily, program);
90 systems[name] = std::make_tuple(methodFamily, program, calc);
94 std::vector<std::shared_ptr<Task>> tasks;
95 std::vector<Utils::UniversalSettings::ValueCollection> tasksettings;
96 auto tasksInput = input[
"tasks"];
97 for (
auto current : tasksInput) {
99 std::vector<std::string> keywords{
"type",
"input",
"output",
"settings"};
100 Utils::checkYamlKeyRecognition(current, keywords);
101 std::string type = current[
"type"].as<std::string>();
103 std::vector<std::string> inputs;
104 auto inputnames = current[
"input"];
105 if (inputnames.size() == 0) {
106 throw std::logic_error(
"Missing system (input name) in task " + type +
"\n");
108 for (
auto inputname : inputnames) {
109 inputs.push_back(inputname.as<std::string>());
112 std::vector<std::string> outputs;
113 if (
auto outputsnames = current[
"output"]) {
114 for (
auto outputname : outputsnames) {
115 outputs.push_back(outputname.as<std::string>());
121 if (current[
"settings"]) {
122 tasksettings.push_back(Utils::deserializeValueCollection(current[
"settings"]));
125 tasksettings.emplace_back();
130 std::vector<std::string> existing;
131 existing.reserve(systems.size());
132 for (
const auto& s : systems) {
133 existing.push_back(s.first);
135 for (
size_t i = 0; i < tasks.size(); i++) {
136 for (
const auto& required : tasks[i]->input()) {
137 if (std::find(existing.begin(), existing.end(), required) == existing.end()) {
138 throw std::logic_error(
"Task No. " + std::to_string(i + 1) +
" requires a system named '" + required +
139 "' which won't be present at that stage.\n");
142 for (
const auto& generated : tasks[i]->output()) {
143 existing.push_back(generated);
149 std::map<std::string, std::shared_ptr<Core::Calculator>> calcs;
150 for (
const auto& s : systems) {
151 calcs[s.first] = std::get<2>(s.second);
153 for (
size_t i = 0; i < tasks.size(); i++) {
154 auto name = tasks[i]->name();
156 tasks[i]->run(calcs, tasksettings[i],
true);
159 throw std::logic_error(
"Encountered the following error in task " + name +
":\n" +
160 boost::current_exception_diagnostic_information());
163 return std::make_tuple(systems, tasks, tasksettings);
169 #endif // READUCT_IO_H_
static std::unique_ptr< Task > produce(std::string name, const std::vector< std::string > &input, const std::vector< std::string > &output, std::shared_ptr< Core::Log > logger=nullptr)
Contstructs a Task with a given set of input and output systems.
Definition: TaskFactory.h:46
static std::pair< AtomCollection, BondOrderCollection > read(const std::string &filename)