| 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 "obmmenergy.h" |
| 7 |
|
| 8 |
#include <avogadro/core/molecule.h> |
| 9 |
|
| 10 |
#include <avogadro/io/cmlformat.h> |
| 11 |
|
| 12 |
#include <QtCore/QDebug> |
| 13 |
#include <QtCore/QDir> |
| 14 |
#include <QtCore/QScopedPointer> |
| 15 |
#include <QtCore/QThread> |
| 16 |
#include <QtCore/QTimer> |
| 17 |
|
| 18 |
namespace Avogadro::QtPlugins { |
| 19 |
|
| 20 |
OBMMEnergy::OBMMEnergy(const std::string& method) |
| 21 |
: m_molecule(nullptr), m_process(nullptr), m_executable( |
| 22 |
#if defined(_WIN32) |
| 23 |
"obmm.exe" |
| 24 |
#else |
| 25 |
"obmm" |
| 26 |
#endif |
| 27 |
), |
| 28 |
m_identifier(method), m_name(method) |
| 29 |
{ |
| 30 |
// eventually CJSON might be nice |
| 31 |
m_inputFormat = new Io::CmlFormat; |
| 32 |
|
| 33 |
if (method == "UFF") { |
| 34 |
m_description = tr("Universal Force Field"); |
| 35 |
m_elements.reset(); |
| 36 |
for (unsigned int i = 1; i < 102; ++i) |
| 37 |
m_elements.set(i); |
| 38 |
} else if (method == "GAFF") { |
| 39 |
m_description = tr("Generalized Amber Force Field"); |
| 40 |
|
| 41 |
// H, C, N, O, F, P, S, Cl, Br, and I |
| 42 |
m_elements.set(1); |
| 43 |
m_elements.set(6); |
| 44 |
m_elements.set(7); |
| 45 |
m_elements.set(8); |
| 46 |
m_elements.set(9); |
| 47 |
m_elements.set(15); |
| 48 |
m_elements.set(16); |
| 49 |
m_elements.set(17); |
| 50 |
m_elements.set(35); |
| 51 |
m_elements.set(53); |
| 52 |
} else if (method == "MMFF94") { |
| 53 |
m_description = tr("Merck Molecular Force Field 94"); |
| 54 |
m_elements.reset(); |
| 55 |
|
| 56 |
// H, C, N, O, F, Si, P, S, Cl, Br, and I |
| 57 |
m_elements.set(1); |
| 58 |
m_elements.set(6); |
| 59 |
m_elements.set(7); |
| 60 |
m_elements.set(8); |
| 61 |
m_elements.set(9); |
| 62 |
m_elements.set(14); |
| 63 |
m_elements.set(15); |
| 64 |
m_elements.set(16); |
| 65 |
m_elements.set(17); |
| 66 |
m_elements.set(35); |
| 67 |
m_elements.set(53); |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
OBMMEnergy::~OBMMEnergy() |
| 72 |
{ |
| 73 |
delete m_inputFormat; |
| 74 |
if (m_process != nullptr) |
| 75 |
delete m_process; |
| 76 |
} |
| 77 |
|
| 78 |
bool OBMMEnergy::acceptsRadicals() const |
| 79 |
{ |
| 80 |
// UFF will figure something out |
| 81 |
if (m_identifier == "UFF") |
| 82 |
return true; |
| 83 |
|
| 84 |
return false; |
| 85 |
} |
| 86 |
|
| 87 |
QByteArray OBMMEnergy::writeAndRead(const QByteArray& input) |
| 88 |
{ |
| 89 |
if (m_process == nullptr) |
| 90 |
return QByteArray(); |
| 91 |
|
| 92 |
QByteArray result, line; |
| 93 |
m_process->write(input + "\n"); |
| 94 |
QThread::msleep(1); |
| 95 |
m_process->waitForReadyRead(5); |
| 96 |
while (m_process->canReadLine() && !line.startsWith("command >")) { |
| 97 |
line = m_process->readLine(); |
| 98 |
result += line; |
| 99 |
} |
| 100 |
// check if we've really flushed the output |
| 101 |
if (!result.contains("invalid command\n command >")) { |
| 102 |
m_process->write(" \n"); |
| 103 |
QThread::msleep(1); |
| 104 |
m_process->waitForReadyRead(5); |
| 105 |
while (m_process->canReadLine()) { |
| 106 |
line = m_process->readLine(); |
| 107 |
result += line; |
| 108 |
} |
| 109 |
} |
| 110 |
result += m_process->readAllStandardOutput(); |
| 111 |
return result; |
| 112 |
} |
| 113 |
|
| 114 |
void OBMMEnergy::setupProcess() |
| 115 |
{ |
| 116 |
if (m_process != nullptr) { |
| 117 |
m_process->kill(); |
| 118 |
delete m_process; |
| 119 |
} |
| 120 |
|
| 121 |
m_process = new QProcess(); |
| 122 |
|
| 123 |
// Read the AVO_OBMM_EXECUTABLE env var to optionally override the |
| 124 |
// executable used. |
| 125 |
QByteArray obmmExec = qgetenv("AVO_OBMM_EXECUTABLE"); |
| 126 |
if (!obmmExec.isEmpty()) { |
| 127 |
m_executable = obmmExec; |
| 128 |
} else { |
| 129 |
// If not overridden, look for an obmm next to the executable. |
| 130 |
QDir baseDir(QCoreApplication::applicationDirPath()); |
| 131 |
if (!baseDir.absolutePath().startsWith("/usr/") && |
| 132 |
QFileInfo(baseDir.absolutePath() + '/' + m_executable).exists()) { |
| 133 |
m_executable = baseDir.absolutePath() + '/' + m_executable; |
| 134 |
QProcessEnvironment env = QProcessEnvironment::systemEnvironment(); |
| 135 |
#if defined(_WIN32) |
| 136 |
env.insert("BABEL_DATADIR", |
| 137 |
QCoreApplication::applicationDirPath() + "/data"); |
| 138 |
#else |
| 139 |
QDir dir(QCoreApplication::applicationDirPath() + "/../share/openbabel"); |
| 140 |
QStringList filters; |
| 141 |
filters << "3.*"; |
| 142 |
QStringList dirs = dir.entryList(filters); |
| 143 |
if (dirs.size() == 1) { |
| 144 |
env.insert("BABEL_DATADIR", QCoreApplication::applicationDirPath() + |
| 145 |
"/../share/openbabel/" + dirs[0]); |
| 146 |
} else { |
| 147 |
qDebug() << "Error, Open Babel data directory not found."; |
| 148 |
} |
| 149 |
dir.setPath(QCoreApplication::applicationDirPath() + "/../lib/openbabel"); |
| 150 |
dirs = dir.entryList(filters); |
| 151 |
if (dirs.size() == 1) { |
| 152 |
env.insert("BABEL_LIBDIR", QCoreApplication::applicationDirPath() + |
| 153 |
"/../lib/openbabel/" + dirs[0]); |
| 154 |
} else { |
| 155 |
env.insert("BABEL_LIBDIR", QCoreApplication::applicationDirPath() + |
| 156 |
"/../lib/openbabel/"); |
| 157 |
qDebug() << "Error, Open Babel plugins directory not found."; |
| 158 |
} |
| 159 |
#endif |
| 160 |
m_process->setProcessEnvironment(env); |
| 161 |
} |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
Calc::EnergyCalculator* OBMMEnergy::newInstance() const |
| 166 |
{ |
| 167 |
return new OBMMEnergy(m_name); |
| 168 |
} |
| 169 |
|
| 170 |
void OBMMEnergy::setMolecule(Core::Molecule* mol) |
| 171 |
{ |
| 172 |
m_molecule = mol; |
| 173 |
|
| 174 |
// should check if the molecule is valid for this script |
| 175 |
// .. this should never happen, but let's be defensive |
| 176 |
if (mol == nullptr || mol->atomCount() == 0) { |
| 177 |
return; // nothing to do |
| 178 |
} |
| 179 |
|
| 180 |
setupProcess(); |
| 181 |
|
| 182 |
// start the process |
| 183 |
// we need a tempory file to write the molecule |
| 184 |
// get a temporary filename |
| 185 |
QString tempPath = QDir::tempPath(); |
| 186 |
if (!tempPath.endsWith(QDir::separator())) |
| 187 |
tempPath += QDir::separator(); |
| 188 |
QString tempPattern = tempPath + "avogadroOBMMXXXXXX.cml"; |
| 189 |
m_tempFile.setFileTemplate(tempPattern); |
| 190 |
if (!m_tempFile.open()) { |
| 191 |
// appendError("Error creating temporary file."); |
| 192 |
return; |
| 193 |
} |
| 194 |
|
| 195 |
// write the molecule |
| 196 |
m_inputFormat->writeFile(m_tempFile.fileName().toStdString(), *mol); |
| 197 |
m_tempFile.close(); |
| 198 |
|
| 199 |
// start the process |
| 200 |
m_process->start(m_executable, QStringList() << m_tempFile.fileName()); |
| 201 |
if (!m_process->waitForStarted()) { |
| 202 |
// appendError("Error starting process."); |
| 203 |
qDebug() << "OBMM: Error starting process."; |
| 204 |
return; |
| 205 |
} |
| 206 |
|
| 207 |
QByteArray input, line, result; |
| 208 |
m_process->waitForReadyRead(); |
| 209 |
result.clear(); |
| 210 |
while (!result.contains("command >")) { |
| 211 |
result += m_process->readLine(); |
| 212 |
if (!m_process->canReadLine()) |
| 213 |
break; |
| 214 |
} |
| 215 |
result += m_process->readAllStandardOutput(); |
| 216 |
|
| 217 |
// set the method m_identifier.c_str() + |
| 218 |
input = QByteArray("ff ") + m_identifier.c_str(); |
| 219 |
result = writeAndRead(input); |
| 220 |
|
| 221 |
// okay, we need to write "load <filename>" to the interpreter |
| 222 |
// and then read the response |
| 223 |
input = "load " + m_tempFile.fileName().toLocal8Bit(); |
| 224 |
result = writeAndRead(input); |
| 225 |
} |
| 226 |
|
| 227 |
Real OBMMEnergy::value(const Eigen::VectorXd& x) |
| 228 |
{ |
| 229 |
if (m_molecule == nullptr || m_process == nullptr) |
| 230 |
return 0.0; // nothing to do |
| 231 |
|
| 232 |
QByteArray input, result; |
| 233 |
|
| 234 |
// write the new coordinates and read the energy |
| 235 |
input = "coord\n"; |
| 236 |
for (Eigen::Index i = 0; i < x.size(); i += 3) { |
| 237 |
// write as x y z (space separated) |
| 238 |
input += QString::number(x[i]).toUtf8() + " " + |
| 239 |
QString::number(x[i + 1]).toUtf8() + " " + |
| 240 |
QString::number(x[i + 2]).toUtf8() + "\n"; |
| 241 |
} |
| 242 |
|
| 243 |
result = writeAndRead(input); |
| 244 |
|
| 245 |
// now ask for the energy |
| 246 |
input = "energy\n"; |
| 247 |
result = writeAndRead(input); |
| 248 |
|
| 249 |
// go through lines in result until we see "total energy" |
| 250 |
QStringList lines = QString(result).remove('\r').split('\n'); |
| 251 |
double energy = 0.0; |
| 252 |
for (auto line : lines) { |
| 253 |
if (line.contains("total energy =")) { |
| 254 |
QStringList items = line.split(" ", Qt::SkipEmptyParts); |
| 255 |
if (items.size() > 4) |
| 256 |
energy = items[3].toDouble(); |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
// if method is not GAFF, convert to kJ/mol |
| 261 |
if (m_identifier != "GAFF") |
| 262 |
energy *= Calc::KCAL_TO_KJ; |
| 263 |
|
| 264 |
energy += constraintEnergies(x); |
| 265 |
return energy; // if conversion fails, returns 0.0 |
| 266 |
} |
| 267 |
|
| 268 |
void OBMMEnergy::gradient(const Eigen::VectorXd& x, Eigen::VectorXd& grad) |
| 269 |
{ |
| 270 |
if (m_molecule == nullptr || m_process == nullptr) |
| 271 |
return; |
| 272 |
|
| 273 |
// write the new coordinates and read the energy |
| 274 |
QByteArray result, input = "coord\n"; |
| 275 |
for (Eigen::Index i = 0; i < x.size(); i += 3) { |
| 276 |
// write as x y z (space separated) |
| 277 |
input += QString::number(x[i]).toUtf8() + " " + |
| 278 |
QString::number(x[i + 1]).toUtf8() + " " + |
| 279 |
QString::number(x[i + 2]).toUtf8() + "\n"; |
| 280 |
} |
| 281 |
|
| 282 |
result = writeAndRead(input); |
| 283 |
|
| 284 |
// now ask for the energy |
| 285 |
input = "grad"; |
| 286 |
result = writeAndRead(input); |
| 287 |
|
| 288 |
// go through lines in result until we see "gradient " |
| 289 |
QStringList lines = QString(result).remove('\r').split('\n'); |
| 290 |
bool readingGradient = false; |
| 291 |
unsigned int i = 0; |
| 292 |
for (auto line : lines) { |
| 293 |
if (line.contains("gradient")) { |
| 294 |
readingGradient = true; |
| 295 |
continue; |
| 296 |
} |
| 297 |
if (readingGradient) { |
| 298 |
QStringList items = line.split(" ", Qt::SkipEmptyParts); |
| 299 |
if (items.size() == 3) { |
| 300 |
grad[3 * i] = items[0].toDouble(); |
| 301 |
grad[3 * i + 1] = items[1].toDouble(); |
| 302 |
grad[3 * i + 2] = items[2].toDouble(); |
| 303 |
++i; |
| 304 |
} |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
grad *= -1; // OpenBabel outputs forces, not grads |
| 309 |
|
| 310 |
cleanGradients(grad); |
| 311 |
// if method is not GAFF, convert to kJ/mol |
| 312 |
if (m_identifier != "GAFF") |
| 313 |
grad *= Calc::KCAL_TO_KJ; |
| 314 |
|
| 315 |
constraintGradients(x, grad); |
| 316 |
} |
| 317 |
|
| 318 |
} // namespace Avogadro::QtPlugins |