| 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 "chargemanager.h" |
| 7 |
#include "chargemodel.h" |
| 8 |
#include "defaultmodel.h" |
| 9 |
|
| 10 |
namespace Avogadro::Calc { |
| 11 |
|
| 12 |
// Helper function to convert a string to lowercase |
| 13 |
// to register all lower-case identifiers |
| 14 |
std::string toLower(const std::string& str) |
| 15 |
{ |
| 16 |
std::string result = str; |
| 17 |
std::transform(result.begin(), result.end(), result.begin(), ::tolower); |
| 18 |
return result; |
| 19 |
} |
| 20 |
|
| 21 |
ChargeManager& ChargeManager::instance() |
| 22 |
{ |
| 23 |
static ChargeManager instance; |
| 24 |
return instance; |
| 25 |
} |
| 26 |
|
| 27 |
void ChargeManager::appendError(const std::string& errorMessage) |
| 28 |
{ |
| 29 |
m_error += errorMessage + "\n"; |
| 30 |
} |
| 31 |
|
| 32 |
bool ChargeManager::registerModel(ChargeModel* model) |
| 33 |
{ |
| 34 |
return instance().addModel(model); |
| 35 |
} |
| 36 |
|
| 37 |
bool ChargeManager::unregisterModel(const std::string& identifier) |
| 38 |
{ |
| 39 |
return instance().removeModel(identifier); |
| 40 |
} |
| 41 |
|
| 42 |
bool ChargeManager::addModel(ChargeModel* model) |
| 43 |
{ |
| 44 |
if (model == nullptr) { |
| 45 |
appendError("Supplied model was null."); |
| 46 |
return false; |
| 47 |
} |
| 48 |
|
| 49 |
if (m_identifiers.find(model->identifier()) != m_identifiers.end()) { |
| 50 |
appendError("Model " + model->identifier() + " already loaded."); |
| 51 |
return false; |
| 52 |
} |
| 53 |
|
| 54 |
// If we got here then the format is unique enough to be added. |
| 55 |
size_t index = m_models.size(); |
| 56 |
m_models.push_back(model); |
| 57 |
std::string lowerId = toLower(model->identifier()); |
| 58 |
m_identifiers[lowerId] = index; |
| 59 |
m_identifierToName[lowerId] = model->name(); |
| 60 |
|
| 61 |
return true; |
| 62 |
} |
| 63 |
|
| 64 |
bool ChargeManager::removeModel(const std::string& identifier) |
| 65 |
{ |
| 66 |
std::string lowerId = toLower(identifier); |
| 67 |
|
| 68 |
auto ids = m_identifiers[lowerId]; |
| 69 |
m_identifiers.erase(lowerId); |
| 70 |
m_identifierToName.erase(lowerId); |
| 71 |
|
| 72 |
ChargeModel* model = m_models[ids]; |
| 73 |
|
| 74 |
if (model != nullptr) { |
| 75 |
m_models[ids] = nullptr; |
| 76 |
delete model; |
| 77 |
} |
| 78 |
|
| 79 |
return true; |
| 80 |
} |
| 81 |
|
| 82 |
std::string ChargeManager::nameForModel(const std::string& identifier) const |
| 83 |
{ |
| 84 |
std::string lowerId = toLower(identifier); |
| 85 |
|
| 86 |
auto it = m_identifierToName.find(lowerId); |
| 87 |
if (it == m_identifierToName.end()) { |
| 88 |
return identifier; |
| 89 |
} |
| 90 |
return it->second; |
| 91 |
} |
| 92 |
|
| 93 |
ChargeManager::ChargeManager() |
| 94 |
{ |
| 95 |
// add any default models here (EEM maybe?) |
| 96 |
} |
| 97 |
|
| 98 |
ChargeManager::~ChargeManager() |
| 99 |
{ |
| 100 |
// Delete the models that were loaded. |
| 101 |
for (auto& m_model : m_models) { |
| 102 |
delete m_model; |
| 103 |
} |
| 104 |
m_models.clear(); |
| 105 |
} |
| 106 |
|
| 107 |
std::set<std::string> ChargeManager::identifiersForMolecule( |
| 108 |
const Core::Molecule& molecule) const |
| 109 |
{ |
| 110 |
// start with the types already in the molecule |
| 111 |
std::set<std::string> identifiers = molecule.partialChargeTypes(); |
| 112 |
|
| 113 |
// check our models for compatibility |
| 114 |
for (auto* m_model : m_models) { |
| 115 |
if (m_model == nullptr) |
| 116 |
continue; |
| 117 |
// We check that every element in the molecule |
| 118 |
// is handled by the model |
| 119 |
auto mask = m_model->elements() & molecule.elements(); |
| 120 |
if (mask.count() == molecule.elements().count()) |
| 121 |
identifiers.insert(m_model->identifier()); // this one will work |
| 122 |
} |
| 123 |
|
| 124 |
return identifiers; |
| 125 |
} |
| 126 |
|
| 127 |
// order of preference for the built-in methods |
| 128 |
const std::vector<std::string> METHOD_TIER_LIST = { "eem", "mmff94", |
| 129 |
"gasteiger" }; |
| 130 |
|
| 131 |
std::string ChargeManager::recommendedModel( |
| 132 |
const Core::Molecule& molecule) const |
| 133 |
{ |
| 134 |
auto identifiers = identifiersForMolecule(molecule); |
| 135 |
|
| 136 |
// first, we look through the identifiers to see if there's |
| 137 |
// something not in the built-in list |
| 138 |
// i.e., read from a file or installed by a user, try that first |
| 139 |
for (auto option : identifiers) { |
| 140 |
if (std::find(METHOD_TIER_LIST.begin(), METHOD_TIER_LIST.end(), option) == |
| 141 |
METHOD_TIER_LIST.end()) |
| 142 |
return option; |
| 143 |
} |
| 144 |
|
| 145 |
// if not, we look through the built-in list in order |
| 146 |
// of preference (e.g., eem > mmff94 > gasteiger) |
| 147 |
for (auto option : METHOD_TIER_LIST) { |
| 148 |
if (identifiers.find(option) != identifiers.end()) { |
| 149 |
return option; |
| 150 |
} |
| 151 |
} |
| 152 |
return ""; |
| 153 |
} |
| 154 |
|
| 155 |
MatrixX ChargeManager::partialCharges(const std::string& identifier, |
| 156 |
Core::Molecule& molecule) const |
| 157 |
{ |
| 158 |
// first check if the type is found in the molecule |
| 159 |
// (i.e., read from a file not computed dynamically) |
| 160 |
auto molIdentifiers = molecule.partialChargeTypes(); |
| 161 |
std::string lowerId = toLower(identifier); |
| 162 |
|
| 163 |
if (molIdentifiers.find(lowerId) != molIdentifiers.end()) { |
| 164 |
return molecule.partialCharges(lowerId); |
| 165 |
} |
| 166 |
|
| 167 |
// otherwise go through our list |
| 168 |
if (m_identifiers.find(lowerId) == m_identifiers.end()) { |
| 169 |
MatrixX charges(molecule.atomCount(), |
| 170 |
1); // we have to return something, so zeros |
| 171 |
return charges; |
| 172 |
} |
| 173 |
|
| 174 |
const auto id = m_identifiers[lowerId]; |
| 175 |
const ChargeModel* model = m_models[id]; |
| 176 |
return model->partialCharges(molecule); |
| 177 |
} |
| 178 |
|
| 179 |
Vector3 ChargeManager::dipoleMoment(const std::string& identifier, |
| 180 |
const Core::Molecule& molecule) const |
| 181 |
{ |
| 182 |
// If the type is found in the molecule |
| 183 |
// we'll use the DefaultModel to handle the dipole moment |
| 184 |
auto molIdentifiers = molecule.partialChargeTypes(); |
| 185 |
std::string lowerId = toLower(identifier); |
| 186 |
|
| 187 |
if (molIdentifiers.find(lowerId) != molIdentifiers.end()) { |
| 188 |
DefaultModel model(lowerId); // so it knows which charges to use |
| 189 |
return model.dipoleMoment(molecule); |
| 190 |
} |
| 191 |
|
| 192 |
// otherwise go through our list |
| 193 |
if (m_identifiers.find(lowerId) == m_identifiers.end()) { |
| 194 |
return Vector3(0.0, 0.0, 0.0); |
| 195 |
} |
| 196 |
|
| 197 |
if (molecule.atomCount() < 2) { |
| 198 |
return Vector3(0.0, 0.0, 0.0); |
| 199 |
} |
| 200 |
|
| 201 |
const auto id = m_identifiers[lowerId]; |
| 202 |
const ChargeModel* model = m_models[id]; |
| 203 |
return model->dipoleMoment(molecule); |
| 204 |
} |
| 205 |
|
| 206 |
double ChargeManager::potential(const std::string& identifier, |
| 207 |
Core::Molecule& molecule, |
| 208 |
const Vector3& point) const |
| 209 |
{ |
| 210 |
// If the type is found in the molecule |
| 211 |
// we'll use the DefaultModel to handle the potential |
| 212 |
auto molIdentifiers = molecule.partialChargeTypes(); |
| 213 |
|
| 214 |
if (molIdentifiers.find(identifier) != molIdentifiers.end()) { |
| 215 |
DefaultModel model(identifier); // so it knows which charges to use |
| 216 |
return model.potential(molecule, point); |
| 217 |
} |
| 218 |
|
| 219 |
// otherwise go through our list |
| 220 |
if (m_identifiers.find(identifier) == m_identifiers.end()) { |
| 221 |
return 0.0; |
| 222 |
} |
| 223 |
|
| 224 |
const auto id = m_identifiers[identifier]; |
| 225 |
const ChargeModel* model = m_models[id]; |
| 226 |
return model->potential(molecule, point); |
| 227 |
} |
| 228 |
|
| 229 |
Core::Array<double> ChargeManager::potentials( |
| 230 |
const std::string& identifier, Core::Molecule& molecule, |
| 231 |
const Core::Array<Vector3>& points) const |
| 232 |
{ |
| 233 |
// As above |
| 234 |
auto molIdentifiers = molecule.partialChargeTypes(); |
| 235 |
|
| 236 |
if (molIdentifiers.find(identifier) != molIdentifiers.end()) { |
| 237 |
DefaultModel model(identifier); // so it knows which charges to use |
| 238 |
return model.potentials(molecule, points); |
| 239 |
} |
| 240 |
|
| 241 |
if (m_identifiers.find(identifier) == m_identifiers.end()) { |
| 242 |
Core::Array<double> potentials(points.size(), 0.0); |
| 243 |
return potentials; |
| 244 |
} |
| 245 |
|
| 246 |
const auto id = m_identifiers[identifier]; |
| 247 |
const ChargeModel* model = m_models[id]; |
| 248 |
return model->potentials(molecule, points); |
| 249 |
} |
| 250 |
|
| 251 |
} // namespace Avogadro::Calc |