Scine::Swoose  2.1.0
This is the SCINE module Swoose.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Pages
CalculatorOptions.h
Go to the documentation of this file.
1 
8 #ifndef SWOOSEUTILITIES_CALCULATOROPTIONS_H
9 #define SWOOSEUTILITIES_CALCULATOROPTIONS_H
10 
13 #include <Utils/IO/Yaml.h>
15 #include <yaml-cpp/yaml.h>
16 
17 namespace Scine {
18 namespace SwooseUtilities {
19 
20 namespace {
21 constexpr const char* qmModelKey = "qm_model";
22 constexpr const char* qmModuleKey = "qm_module";
23 constexpr const char* mmModelKey = "mm_model";
24 constexpr const char* defaultMolecularMechanicsModel = "SFAM";
25 } // namespace
26 
33 inline std::string getChosenMMCalculatorOption(YAML::Node& yamlNode) {
34  std::string mmModel = defaultMolecularMechanicsModel;
35  if (yamlNode[mmModelKey]) {
36  mmModel = yamlNode[mmModelKey].as<std::string>();
37  yamlNode.remove(mmModelKey);
38  }
39  // capitalize MM model string and return it
40  std::transform(mmModel.begin(), mmModel.end(), mmModel.begin(), ::toupper);
41  return mmModel;
42 }
43 
49 inline std::pair<std::string, std::string> getChosenQmCalculatorOption(YAML::Node& yamlNode) {
50  std::string qmModel;
51  std::string qmModule;
52  if (yamlNode[qmModelKey]) {
53  qmModel = yamlNode[qmModelKey].as<std::string>();
54  yamlNode.remove(qmModelKey);
55  }
56  if (yamlNode[qmModuleKey]) {
57  qmModule = yamlNode[qmModuleKey].as<std::string>();
58  yamlNode.remove(qmModuleKey);
59  }
60  if (qmModel.empty() || qmModule.empty())
61  throw std::runtime_error("For a QM/MM calculation, one needs to specify the QM model and module.");
62 
63  // Capitalize the QM model:
64  std::transform(qmModel.begin(), qmModel.end(), qmModel.begin(), ::toupper);
65 
66  // Convert module to only first letter as uppercase:
67  std::transform(qmModule.begin(), qmModule.end(), qmModule.begin(), ::tolower);
68  qmModule.at(0) = std::toupper(qmModule.at(0));
69 
70  return {qmModel, qmModule};
71 }
72 
80 inline void fillQmmmCalculatorWithUnderlyingCalculators(std::shared_ptr<Scine::Qmmm::QmmmCalculator>& qmmmCalculator,
81  YAML::Node& yamlNode) {
82  std::string methodFamilies, programs;
83  // legacy support, remove in future and only use method_family and program and load QM/MM calc directly
84  if (yamlNode[qmModelKey]) {
85  methodFamilies = yamlNode[qmModelKey].as<std::string>() + '/' + defaultMolecularMechanicsModel;
86  }
87  else if (yamlNode[Utils::SettingsNames::methodFamily]) {
88  methodFamilies = yamlNode[Utils::SettingsNames::methodFamily].as<std::string>();
89  if (methodFamilies.find('/') == std::string::npos) {
90  methodFamilies += "/" + std::string(defaultMolecularMechanicsModel);
91  }
92  }
93  else {
94  throw std::runtime_error("Please provide two method families for a QM/MM calculation.\n"
95  "For instance, method_family: PM6/SFAM and program: Sparrow/Swoose");
96  }
97  if (yamlNode[qmModuleKey]) {
98  programs = yamlNode[qmModuleKey].as<std::string>() + '/' + "Swoose";
99  }
100  else if (yamlNode[Utils::SettingsNames::program]) {
101  programs = yamlNode[Utils::SettingsNames::program].as<std::string>();
102  if (programs.find('/') == std::string::npos) {
103  programs += "/" + std::string("Swoose");
104  }
105  }
106  else {
107  programs = "Any/Swoose";
108  }
109  std::string error =
110  "The QM/MM calculator could not be loaded via the module system.\nCheck: (i) whether the requested "
111  "calculators are available (see manual), (ii) that you have installed all "
112  "relevant modules, and (iii) that all necessary environment variables are set (e.g., ORCA_BINARY_PATH or "
113  "TURBODIR).";
114  try {
115  auto loadedCalc = Utils::CalculationRoutines::getCalculator(methodFamilies, programs);
116  if (!loadedCalc)
117  throw std::runtime_error(error);
118  auto castedCalc = std::dynamic_pointer_cast<Qmmm::QmmmCalculator>(loadedCalc);
119  if (!castedCalc)
120  throw std::runtime_error("The loaded calculator is not a QM/MM calculator.");
121  qmmmCalculator = castedCalc;
122  }
123  catch (const std::runtime_error& e) {
124  std::cerr << e.what() << std::endl;
125  throw std::runtime_error(error);
126  }
127 }
128 
129 } // namespace SwooseUtilities
130 } // namespace Scine
131 
132 #endif // SWOOSEUTILITIES_CALCULATOROPTIONS_H