Scine::Swoose  2.1.0
This is the SCINE module Swoose.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Pages
TaskManagement.h
Go to the documentation of this file.
1 
8 #ifndef SWOOSE_TASKMANAGEMENT_H
9 #define SWOOSE_TASKMANAGEMENT_H
10 
11 #include "Tasks.h"
12 #include <Core/Log.h>
13 #include <Core/ModuleManager.h>
19 #include <Utils/IO/Yaml.h>
21 #include <yaml-cpp/yaml.h>
22 
23 namespace Scine {
24 namespace Swoose {
25 namespace TaskManagement {
26 
33 std::shared_ptr<Core::Calculator> getMmCalculatorFromModel(std::string mmModel, Core::ModuleManager& manager) {
34  std::shared_ptr<Core::Calculator> calculator;
35  try {
36  calculator = manager.get<Core::Calculator>(mmModel);
37  }
38  catch (const std::runtime_error& e) {
39  throw std::runtime_error("MM model could not be loaded via the module system.");
40  }
41  return calculator;
42 }
43 
54 void manageTasks(Core::ModuleManager& manager, std::string mode, bool quantum, bool hessianRequired,
55  std::string structureFile, YAML::Node& yamlNode, std::string yamlSettingsPath, Core::Log& log) {
56  // For the objects that we want to be mostly silent
57  Core::Log warningLog = Core::Log::silent();
58  warningLog.warning.add("cerr", Core::Log::cerrSink());
59  warningLog.error.add("cerr", Core::Log::cerrSink());
60 
61  if (mode == "calculate" && !quantum) {
62  auto mmModel = SwooseUtilities::getChosenMMCalculatorOption(yamlNode);
63  auto calculator = getMmCalculatorFromModel(mmModel, manager);
64  calculator->setLog(log);
65  Utils::nodeToSettings(calculator->settings(), yamlNode, false);
66  Utils::PropertyList properties = Utils::Property::Energy | Utils::Property::Gradients;
67  if (hessianRequired)
68  properties = Utils::Property::Energy | Utils::Property::Gradients | Utils::Property::Hessian;
69  Tasks::runMMCalculationTask(*calculator, structureFile, properties, log);
70  }
71  else if (mode == "calculate") {
72  std::shared_ptr<Qmmm::QmmmCalculator> calculator;
73  SwooseUtilities::fillQmmmCalculatorWithUnderlyingCalculators(calculator, yamlNode);
74  calculator->setLog(log);
75  Utils::PropertyList properties = Utils::Property::Energy | Utils::Property::Gradients;
76  Tasks::runQmmmCalculationTask(*calculator, structureFile, properties, log, yamlNode);
77  }
78  else if (mode == "parametrize") {
79  std::shared_ptr<Core::MMParametrizer> parametrizer;
80  try {
81  parametrizer = manager.get<Core::MMParametrizer>("SFAM_parametrizer");
82  }
83  catch (const std::runtime_error& e) {
84  throw std::runtime_error("The SFAM parametrizer could not be loaded via the module system.");
85  }
86  parametrizer->setLog(log);
87  Utils::nodeToSettings(parametrizer->settings(), yamlNode, true);
88  parametrizer->settings().modifyString(SwooseUtilities::SettingsNames::yamlSettingsFilePath, yamlSettingsPath);
89  Tasks::runSFAMParametrizationTask(*parametrizer, structureFile, log);
90  }
91  else if (mode == "md" && !quantum) {
92  auto mmModel = SwooseUtilities::getChosenMMCalculatorOption(yamlNode);
93  auto calculator = getMmCalculatorFromModel(mmModel, manager);
94  calculator->setLog(warningLog);
95  Utils::nodeToSettings(calculator->settings(), yamlNode, true);
96  Utils::MolecularDynamics molecularDynamics(*calculator);
97  Utils::nodeToSettings(molecularDynamics.settings(), yamlNode, true);
98  molecularDynamics.settings().normalizeStringCases(); // convert all option names to lower case letters
99  Tasks::runMDSimulationTask(molecularDynamics, structureFile, log);
100  }
101  else if (mode == "md") {
102  std::shared_ptr<Qmmm::QmmmCalculator> calculator;
103  calculator->setLog(warningLog);
104  SwooseUtilities::fillQmmmCalculatorWithUnderlyingCalculators(calculator, yamlNode);
105  Utils::nodeToSettings(calculator->settings(), yamlNode, true);
106  Utils::MolecularDynamics molecularDynamics(*calculator);
107  Utils::nodeToSettings(molecularDynamics.settings(), yamlNode, true);
108  Tasks::runMDSimulationTask(molecularDynamics, structureFile, log);
109  }
110  else if (mode == "optimize" && !quantum) {
111  auto mmModel = SwooseUtilities::getChosenMMCalculatorOption(yamlNode);
112  auto calculator = getMmCalculatorFromModel(mmModel, manager);
113  calculator->setLog(warningLog);
114  Utils::nodeToSettings(calculator->settings(), yamlNode, true);
115  auto optimizer = std::make_unique<Utils::GeometryOptimizer<Utils::Bfgs>>(*calculator);
116  auto optSettings = optimizer->getSettings();
117  Utils::nodeToSettings(optSettings, yamlNode, true);
118  optimizer->setSettings(optSettings);
119  Tasks::runMMOptimizationTask(*calculator, *optimizer, structureFile, log, yamlNode);
120  }
121  else if (mode == "optimize") {
122  std::shared_ptr<Qmmm::QmmmCalculator> calculator;
123  calculator->setLog(warningLog);
124  SwooseUtilities::fillQmmmCalculatorWithUnderlyingCalculators(calculator, yamlNode);
125  auto optimizer = std::make_unique<Utils::QmmmGeometryOptimizer<Utils::Bfgs>>(*calculator);
126  auto optSettings = optimizer->getSettings();
127  Utils::nodeToSettings(optSettings, yamlNode, true);
128  optimizer->setSettings(optSettings);
129  Tasks::runQmmmOptimizationTask<Utils::Bfgs>(*calculator, *optimizer, structureFile, log, yamlNode);
130  }
131  else if (mode == "select_qm") {
132  Tasks::runQmRegionSelectionTask(structureFile, log, yamlNode, yamlSettingsPath);
133  }
134  else if (mode == "prepare-analyze" || mode == "prepare-protonate" || mode == "prepare-finalize" || mode == "prepare-automate") {
135  auto processor = std::make_unique<Scine::StructurePreparation::StructureProcessor>();
136  Utils::nodeToSettings(processor->settings(), yamlNode, false); // TODO: allow superfluous?
137  Tasks::runPDBPreparationTask(*processor, structureFile, mode, log);
138  }
139  else {
140  throw std::runtime_error(
141  "Your specified mode is not valid. Options are: parametrize, calculate, md, optimize, and select_qm.");
142  }
143 }
144 
145 } // namespace TaskManagement
146 } // namespace Swoose
147 } // namespace Scine
148 
149 #endif // SWOOSE_TASKMANAGEMENT_H
static SinkPtr cerrSink()
static Log silent()