Debian Codegraph @

avogadro/qtplugins/forcefield/obenergy.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 It links to the Open Babel library, which is released under the GNU GPL v2.
5 ******************************************************************************/
6
7 #include "obenergy.h"
8
9 #include <avogadro/core/molecule.h>
10
11 #include <openbabel/babelconfig.h>
12
13 #include <openbabel/atom.h>
14 #include <openbabel/base.h>
15 #include <openbabel/forcefield.h>
16 #include <openbabel/math/vector3.h>
17 #include <openbabel/mol.h>
18 #include <openbabel/obconversion.h>
19 #include <openbabel/obiter.h>
20
21 #include <QCoreApplication>
22 #include <QDebug>
23 #include <QDir>
24
25 using namespace OpenBabel;
26
27 namespace Avogadro::QtPlugins {
28
29 class OBEnergy::Private
30 {
31 public:
32 // OBMol and OBForceField are owned by this class
33 OBMol* m_obmol = nullptr;
34 OBForceField* m_forceField = nullptr;
35 bool setup = false;
36
37 ~Private()
38 {
39 if (m_obmol != nullptr)
40 delete m_obmol;
41 }
42 };
43
44 OBEnergy::OBEnergy(const std::string& method)
45 : m_identifier(method), m_name(method), m_molecule(nullptr)
46 {
47 d = new Private;
48
49 // make sure we set the Open Babel variables for data files
50 #ifdef _WIN32
51 QByteArray dataDir =
52 QString(QCoreApplication::applicationDirPath() + "/data").toLocal8Bit();
53 qputenv("BABEL_DATADIR", dataDir);
54 #else
55 // check if BABEL_DATADIR is set in the environment
56 QStringList filters;
57 filters << "3.*"
58 << "2.*";
59 if (qgetenv("BABEL_DATADIR").isEmpty()) {
60 QDir dir(QCoreApplication::applicationDirPath() + "/../share/openbabel");
61 QStringList dirs = dir.entryList(filters);
62 if (dirs.size() == 1) {
63 // versioned data directory
64 QString dataDir = QCoreApplication::applicationDirPath() +
65 "/../share/openbabel/" + dirs[0];
66 qputenv("BABEL_DATADIR", dataDir.toLocal8Bit());
67 } else {
68 qDebug() << "Error, Open Babel data directory not found.";
69 }
70 }
71
72 // Check if BABEL_LIBDIR is set
73 if (qgetenv("BABEL_LIBDIR").isEmpty()) {
74 QDir dir(QCoreApplication::applicationDirPath() + "/../lib/openbabel");
75 QStringList dirs = dir.entryList(filters);
76 if (dirs.size() == 0) {
77 QString libDir =
78 QCoreApplication::applicationDirPath() + "/../lib/openbabel/";
79 qputenv("BABEL_LIBDIR", libDir.toLocal8Bit());
80 } else if (dirs.size() == 1) {
81 QString libDir =
82 QCoreApplication::applicationDirPath() + "/../lib/openbabel/" + dirs[0];
83 qputenv("BABEL_LIBDIR", libDir.toLocal8Bit());
84 } else {
85 qDebug() << "Error, Open Babel plugins directory not found.";
86 }
87 }
88 #endif
89 // Ensure the plugins are loaded
90 OBPlugin::LoadAllPlugins();
91
92 d->m_forceField = static_cast<OBForceField*>(
93 OBPlugin::GetPlugin("forcefields", method.c_str()));
94
95 #ifndef NDEBUG
96 qDebug() << "OBEnergy: method: " << method.c_str();
97 if (d->m_forceField == nullptr) {
98 qDebug() << "OBEnergy: method not found: " << method.c_str();
99 qDebug() << OBPlugin::ListAsString("forcefields").c_str();
100 }
101 #endif
102
103 if (method == "UFF") {
104 m_description = tr("Universal Force Field");
105 m_elements.reset();
106 for (unsigned int i = 1; i < 102; ++i)
107 m_elements.set(i);
108 } else if (method == "GAFF") {
109 m_description = tr("Generalized Amber Force Field");
110
111 // H, C, N, O, F, P, S, Cl, Br, and I
112 m_elements.set(1);
113 m_elements.set(6);
114 m_elements.set(7);
115 m_elements.set(8);
116 m_elements.set(9);
117 m_elements.set(15);
118 m_elements.set(16);
119 m_elements.set(17);
120 m_elements.set(35);
121 m_elements.set(53);
122 } else if (method == "MMFF94") {
123 m_description = tr("Merck Molecular Force Field 94");
124 m_elements.reset();
125
126 // H, C, N, O, F, Si, P, S, Cl, Br, and I
127 m_elements.set(1);
128 m_elements.set(6);
129 m_elements.set(7);
130 m_elements.set(8);
131 m_elements.set(9);
132 m_elements.set(14);
133 m_elements.set(15);
134 m_elements.set(16);
135 m_elements.set(17);
136 m_elements.set(35);
137 m_elements.set(53);
138 }
139 }
140
141 OBEnergy::~OBEnergy() {}
142
143 bool OBEnergy::acceptsRadicals() const
144 {
145 if (m_identifier == "UFF")
146 return true;
147
148 return false;
149 }
150
151 Calc::EnergyCalculator* OBEnergy::newInstance() const
152 {
153 return new OBEnergy(m_name);
154 }
155
156 void OBEnergy::setMolecule(Core::Molecule* mol)
157 {
158 m_molecule = mol;
159
160 if (mol == nullptr || mol->atomCount() == 0) {
161 return; // nothing to do
162 }
163
164 // set up our internal OBMol
165 d->m_obmol = new OBMol;
166 // copy the atoms, bonds, and coordinates
167 d->m_obmol->BeginModify();
168 for (size_t i = 0; i < mol->atomCount(); ++i) {
169 const Core::Atom& atom = mol->atom(i);
170 OBAtom* obAtom = d->m_obmol->NewAtom();
171 obAtom->SetAtomicNum(atom.atomicNumber());
172 auto pos = atom.position3d().cast<double>();
173 obAtom->SetVector(pos.x(), pos.y(), pos.z());
174 }
175 for (size_t i = 0; i < mol->bondCount(); ++i) {
176 const Core::Bond& bond = mol->bond(i);
177 d->m_obmol->AddBond(bond.atom1().index() + 1, bond.atom2().index() + 1,
178 bond.order());
179 }
180 d->m_obmol->EndModify();
181
182 // make sure we can set up the force field
183 if (d->m_forceField != nullptr) {
184 d->setup = d->m_forceField->Setup(*d->m_obmol);
185 } else {
186 d->m_forceField = static_cast<OBForceField*>(
187 OBPlugin::GetPlugin("forcefields", m_identifier.c_str()));
188 if (d->m_forceField != nullptr) {
189 d->setup = d->m_forceField->Setup(*d->m_obmol);
190 }
191 }
192 }
193
194 Real OBEnergy::value(const Eigen::VectorXd& x)
195 {
196 if (m_molecule == nullptr || m_molecule->atomCount() == 0 ||
197 d->m_obmol == nullptr || !d->setup)
198 return 0.0; // nothing to do
199
200 // update all coordinates at once (SetCoordinates copies the array)
201 d->m_obmol->SetCoordinates(const_cast<double*>(x.data()));
202
203 double energy = 0.0;
204 if (d->m_forceField != nullptr) {
205 d->m_forceField->SetCoordinates(*d->m_obmol);
206 energy = d->m_forceField->Energy(false);
207 }
208
209 // if method is not GAFF, convert to kJ/mol
210 if (m_identifier != "GAFF")
211 energy *= Calc::KCAL_TO_KJ;
212
213 // make sure to add in any constraint penalties
214 energy += constraintEnergies(x);
215
216 return energy;
217 }
218
219 Real OBEnergy::evaluate(const Eigen::VectorXd& x, Eigen::VectorXd* grad)
220 {
221 if (grad == nullptr)
222 return value(x);
223
224 if (m_molecule == nullptr || m_molecule->atomCount() == 0 ||
225 d->m_obmol == nullptr || !d->setup) {
226 return 0.0;
227 }
228
229 d->m_obmol->SetCoordinates(const_cast<double*>(x.data()));
230
231 double energy = 0.0;
232 if (d->m_forceField != nullptr) {
233 d->m_forceField->SetCoordinates(*d->m_obmol);
234 energy = d->m_forceField->Energy(true);
235
236 // GetGradientPtr returns forces (not gradients), so negate
237 const auto n = m_molecule->atomCount();
238 Eigen::Map<const Eigen::VectorXd> obForces(
239 d->m_forceField->GetGradientPtr(), 3 * n);
240 *grad = -obForces;
241
242 // if method is not GAFF, convert to kJ/mol
243 if (m_identifier != "GAFF") {
244 energy *= Calc::KCAL_TO_KJ;
245 *grad *= Calc::KCAL_TO_KJ;
246 }
247
248 cleanGradients(*grad);
249 constraintGradients(x, *grad);
250 }
251
252 energy += constraintEnergies(x);
253 return energy;
254 }
255
256 void OBEnergy::gradient(const Eigen::VectorXd& x, Eigen::VectorXd& grad)
257 {
258 evaluate(x, &grad);
259 }
260
261 } // namespace Avogadro::QtPlugins