Debian Codegraph @

avogadro/calc/energymanager.cpp

raw
1 /******************************************************************************
2 This source file is part of the Avogadro project.
3 This source code is released under the 3-Clause BSD License, (see "LICENSE").
4 ******************************************************************************/
5
6 #include "energymanager.h"
7 #include "energycalculator.h"
8 #include "lennardjones.h"
9 #include "uff.h"
10
11 namespace Avogadro::Calc {
12
13 EnergyManager& EnergyManager::instance()
14 {
15 static EnergyManager instance;
16 return instance;
17 }
18
19 void EnergyManager::appendError(const std::string& errorMessage)
20 {
21 m_error += errorMessage + "\n";
22 }
23
24 bool EnergyManager::registerModel(EnergyCalculator* model)
25 {
26 return instance().addModel(model);
27 }
28
29 bool EnergyManager::unregisterModel(const std::string& identifier)
30 {
31 return instance().removeModel(identifier);
32 }
33
34 bool EnergyManager::addModel(EnergyCalculator* model)
35 {
36 if (model == nullptr) {
37 appendError("Supplied model was null.");
38 return false;
39 }
40
41 if (m_identifiers.find(model->identifier()) != m_identifiers.end()) {
42 appendError("Model " + model->identifier() + " already loaded.");
43 return false;
44 }
45
46 // If we got here then the model is unique enough to be added.
47 size_t index = m_models.size();
48 m_models.push_back(model);
49 m_identifiers[model->identifier()] = index;
50 m_identifierToName[model->identifier()] = model->name();
51
52 return true;
53 }
54
55 EnergyCalculator* EnergyManager::model(const std::string& identifier) const
56 {
57 auto it = m_identifiers.find(identifier);
58 if (it == m_identifiers.end()) {
59 return nullptr;
60 }
61 return m_models[it->second]->newInstance();
62 }
63
64 bool EnergyManager::removeModel(const std::string& identifier)
65 {
66 auto ids = m_identifiers[identifier];
67 m_identifiers.erase(identifier);
68 m_identifierToName.erase(identifier);
69
70 auto* model = m_models[ids];
71
72 if (model != nullptr) {
73 m_models[ids] = nullptr;
74 delete model;
75 }
76
77 return true;
78 }
79
80 std::string EnergyManager::nameForModel(const std::string& identifier) const
81 {
82 auto it = m_identifierToName.find(identifier);
83 if (it == m_identifierToName.end()) {
84 return identifier;
85 }
86 return it->second;
87 }
88
89 EnergyManager::EnergyManager()
90 {
91 // add any default models here
92
93 // LJ is the fallback, since it can handle anything
94 // (maybe not well, but it can handle it)
95 addModel(new LennardJones);
96 // UFF is good for a wide range of molecules
97 addModel(new UFF);
98 }
99
100 EnergyManager::~EnergyManager()
101 {
102 // Delete the models that were loaded.
103 for (auto& m_model : m_models) {
104 delete m_model;
105 }
106 m_models.clear();
107 }
108
109 std::set<std::string> EnergyManager::identifiers() const
110 {
111 std::set<std::string> identifiers;
112 for (auto& it : m_identifiers) {
113 identifiers.insert(it.first);
114 }
115 return identifiers;
116 }
117
118 std::set<std::string> EnergyManager::identifiersForMolecule(
119 const Core::Molecule& molecule) const
120 {
121 std::set<std::string> identifiers;
122
123 // check our models for compatibility
124 for (auto* m_model : m_models) {
125 if (m_model == nullptr)
126 continue;
127
128 // we can check easy things first
129 // - is the molecule an ion based on total charge
130 // - is the molecule a radical based on spin multiplicity
131 // - does the molecule have a unit cell
132 if (molecule.totalCharge() != 0 && !m_model->acceptsIons())
133 continue;
134 if (molecule.totalSpinMultiplicity() != 1 && !m_model->acceptsRadicals())
135 continue;
136 if (molecule.unitCell() != nullptr && !m_model->acceptsUnitCell())
137 continue;
138
139 // Finally, we check that every element in the molecule
140 // is handled by the model
141 auto mask = m_model->elements() & molecule.elements();
142 if (mask.count() == molecule.elements().count())
143 identifiers.insert(m_model->identifier()); // this one will work
144 }
145
146 return identifiers;
147 }
148
149 // order of preference for the built-in methods
150 const std::vector<std::string> METHOD_TIER_LIST = { "GAFF", "MMFF94", "UFF",
151 "LJ" };
152
153 std::string EnergyManager::recommendedModel(
154 const Core::Molecule& molecule) const
155 {
156 auto identifiers = identifiersForMolecule(molecule);
157 if (identifiers.empty())
158 return "LJ"; // shouldn't really ever happen
159
160 std::string bestOption;
161
162 // first, we look through the identifiers to see if there's
163 // something not in the built-in list
164 // i.e., installed by the user = try that first
165 for (auto option : identifiers) {
166 if (std::find(METHOD_TIER_LIST.begin(), METHOD_TIER_LIST.end(), option) ==
167 METHOD_TIER_LIST.end())
168 return option;
169 }
170
171 // if not, we look through the built-in list in order
172 // of preference (e.g., GAFF > MMFF94 > UFF > LJ)
173 for (auto option : METHOD_TIER_LIST) {
174 if (identifiers.find(option) != identifiers.end()) {
175 bestOption = option;
176 break;
177 }
178 }
179 if (!bestOption.empty())
180 return bestOption;
181 else
182 return "LJ"; // this will always work
183 }
184
185 } // namespace Avogadro::Calc