Scine::Swoose  1.0.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>
18 #include <Utils/IO/Yaml.h>
20 #include <yaml-cpp/yaml.h>
21 
22 namespace Scine {
23 namespace Swoose {
24 namespace TaskManagement {
25 
33 void fillQmmmCalculatorWithUnderlyingCalculators(Qmmm::QmmmCalculator& qmmmCalculator, Core::ModuleManager& manager,
34  YAML::Node& yamlNode) {
35  std::shared_ptr<Core::Calculator> qmCalculator, mmCalculator;
36  auto qmCalculatorOption = SwooseUtilities::getChosenQmCalculatorOption(yamlNode);
37  auto mmModel = SwooseUtilities::getChosenMMCalculatorOption(yamlNode);
38  try {
39  qmCalculator = manager.get<Core::Calculator>(qmCalculatorOption.first, qmCalculatorOption.second);
40  mmCalculator = manager.get<Core::Calculator>(mmModel);
41  }
42  catch (const std::runtime_error& e) {
43  throw std::runtime_error(
44  "The QM or MM calculator could not be loaded via the module system.\nCheck: (i) whether the requested "
45  "calculators are available (see manual), (ii) that you have installed all "
46  "relevant modules, and (iii) that all necessary environment variables are set (e.g., ORCA_BINARY_PATH or "
47  "TURBODIR).");
48  }
49  qmmmCalculator.setUnderlyingCalculators(qmCalculator, mmCalculator);
50 }
51 
58 std::shared_ptr<Core::Calculator> getMmCalculatorFromModel(std::string mmModel, Core::ModuleManager& manager) {
59  std::shared_ptr<Core::Calculator> calculator;
60  try {
61  calculator = manager.get<Core::Calculator>(mmModel);
62  }
63  catch (const std::runtime_error& e) {
64  throw std::runtime_error("MM model could not be loaded via the module system.");
65  }
66  return calculator;
67 }
68 
79 void manageTasks(Core::ModuleManager& manager, std::string mode, bool quantum, bool hessianRequired,
80  std::string structureFile, YAML::Node& yamlNode, std::string yamlSettingsPath, Core::Log& log) {
81  // For the objects that we want to be mostly silent
82  Core::Log warningLog = Core::Log::silent();
83  warningLog.warning.add("cerr", Core::Log::cerrSink());
84  warningLog.error.add("cerr", Core::Log::cerrSink());
85 
86  if (mode == "calculate" && !quantum) {
87  auto mmModel = SwooseUtilities::getChosenMMCalculatorOption(yamlNode);
88  auto calculator = getMmCalculatorFromModel(mmModel, manager);
89  calculator->setLog(log);
90  Utils::nodeToSettings(calculator->settings(), yamlNode, false);
91  Utils::PropertyList properties = Utils::Property::Energy | Utils::Property::Gradients;
92  if (hessianRequired)
93  properties = Utils::Property::Energy | Utils::Property::Gradients | Utils::Property::Hessian;
94  Tasks::runMMCalculationTask(*calculator, structureFile, properties, log);
95  }
96  else if (mode == "calculate") {
97  Qmmm::QmmmCalculator calculator;
98  calculator.setLog(log);
99  fillQmmmCalculatorWithUnderlyingCalculators(calculator, manager, yamlNode);
100  Utils::PropertyList properties = Utils::Property::Energy | Utils::Property::Gradients;
101  Tasks::runQmmmCalculationTask(calculator, structureFile, properties, log, yamlNode);
102  }
103  else if (mode == "parametrize") {
104  std::shared_ptr<Core::MMParametrizer> parametrizer;
105  try {
106  parametrizer = manager.get<Core::MMParametrizer>("SFAM_parametrizer");
107  }
108  catch (const std::runtime_error& e) {
109  throw std::runtime_error("The SFAM parametrizer could not be loaded via the module system.");
110  }
111  parametrizer->setLog(log);
112  Utils::nodeToSettings(parametrizer->settings(), yamlNode, true);
113  parametrizer->settings().modifyString(SwooseUtilities::SettingsNames::yamlSettingsFilePath, yamlSettingsPath);
114  Tasks::runSFAMParametrizationTask(*parametrizer, structureFile, log);
115  }
116  else if (mode == "md" && !quantum) {
117  auto mmModel = SwooseUtilities::getChosenMMCalculatorOption(yamlNode);
118  auto calculator = getMmCalculatorFromModel(mmModel, manager);
119  calculator->setLog(warningLog);
120  Utils::nodeToSettings(calculator->settings(), yamlNode, true);
121  Utils::MolecularDynamics molecularDynamics(*calculator);
122  Utils::nodeToSettings(molecularDynamics.settings(), yamlNode, true);
123  molecularDynamics.settings().normalizeStringCases(); // convert all option names to lower case letters
124  Tasks::runMDSimulationTask(molecularDynamics, structureFile, log);
125  }
126  else if (mode == "md") {
127  Qmmm::QmmmCalculator calculator;
128  calculator.setLog(warningLog);
129  fillQmmmCalculatorWithUnderlyingCalculators(calculator, manager, yamlNode);
130  Utils::nodeToSettings(calculator.settings(), yamlNode, true);
131  Utils::MolecularDynamics molecularDynamics(calculator);
132  Utils::nodeToSettings(molecularDynamics.settings(), yamlNode, true);
133  Tasks::runMDSimulationTask(molecularDynamics, structureFile, log);
134  }
135  else if (mode == "optimize" && !quantum) {
136  auto mmModel = SwooseUtilities::getChosenMMCalculatorOption(yamlNode);
137  auto calculator = getMmCalculatorFromModel(mmModel, manager);
138  calculator->setLog(warningLog);
139  Utils::nodeToSettings(calculator->settings(), yamlNode, true);
140  auto optimizer = std::make_unique<Utils::GeometryOptimizer<Utils::Bfgs>>(*calculator);
141  auto optSettings = optimizer->getSettings();
142  Utils::nodeToSettings(optSettings, yamlNode, true);
143  optimizer->setSettings(optSettings);
144  Tasks::runMMOptimizationTask(*calculator, *optimizer, structureFile, log, yamlNode);
145  }
146  else if (mode == "optimize") {
147  Qmmm::QmmmCalculator calculator;
148  calculator.setLog(warningLog);
149  fillQmmmCalculatorWithUnderlyingCalculators(calculator, manager, yamlNode);
150  auto optimizer = std::make_unique<Utils::QmmmGeometryOptimizer<Utils::Bfgs>>(calculator);
151  auto optSettings = optimizer->getSettings();
152  Utils::nodeToSettings(optSettings, yamlNode, true);
153  optimizer->setSettings(optSettings);
154  Tasks::runQmmmOptimizationTask<Utils::Bfgs>(calculator, *optimizer, structureFile, log, yamlNode);
155  }
156  else if (mode == "select_qm") {
157  Tasks::runQmRegionSelectionTask(structureFile, log, yamlNode, yamlSettingsPath);
158  }
159  else {
160  throw std::runtime_error(
161  "Your specified mode is not valid. Options are: parametrize, calculate, md, optimize, select_qm.");
162  }
163 }
164 
165 } // namespace TaskManagement
166 } // namespace Swoose
167 } // namespace Scine
168 
169 #endif // SWOOSE_TASKMANAGEMENT_H
static SinkPtr cerrSink()
static Log silent()