| 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 "uff.h" |
| 7 |
#include "uffdata.h" |
| 8 |
#include "gradients.h" |
| 9 |
|
| 10 |
#include <Eigen/src/Core/util/Meta.h> |
| 11 |
#include <avogadro/core/angleiterator.h> |
| 12 |
#include <avogadro/core/angletools.h> |
| 13 |
#include <avogadro/core/array.h> |
| 14 |
#include <avogadro/core/dihedraliterator.h> |
| 15 |
#include <avogadro/core/elements.h> |
| 16 |
#include <avogadro/core/molecule.h> |
| 17 |
#include <avogadro/core/unitcell.h> |
| 18 |
|
| 19 |
#include <cstdint> |
| 20 |
#include <unordered_set> |
| 21 |
|
| 22 |
namespace Avogadro::Calc { |
| 23 |
|
| 24 |
using namespace Core; |
| 25 |
using Eigen::Vector3d; |
| 26 |
|
| 27 |
enum Coordination |
| 28 |
{ |
| 29 |
Resonant = 0, // conjugated / aromatic |
| 30 |
Linear = 1, // sp linear |
| 31 |
Trigonal = 2, // sp2 planar |
| 32 |
Tetrahedral = 3, // sp3 tetrahedral |
| 33 |
SquarePlanar = 4, // square planar |
| 34 |
TrigonalBipyramidal = 5, // trigonal bipyramidal |
| 35 |
Octahedral = 6, // octahedral |
| 36 |
TrigonalBipentagonal = 7, // trigonal bipentagonal |
| 37 |
Other = 8 // including higher coordination |
| 38 |
}; |
| 39 |
|
| 40 |
class UFFBond // track the bond parameters |
| 41 |
{ |
| 42 |
public: |
| 43 |
Index _atom1; |
| 44 |
Index _atom2; |
| 45 |
Real _r0; |
| 46 |
Real _kb; |
| 47 |
}; |
| 48 |
|
| 49 |
class UFFAngle |
| 50 |
{ |
| 51 |
public: |
| 52 |
Index _atom1; |
| 53 |
Index _atom2; |
| 54 |
Index _atom3; |
| 55 |
Real _theta0; |
| 56 |
Real _kijk; |
| 57 |
Real _c0; |
| 58 |
Real _c1; |
| 59 |
Real _c2; |
| 60 |
Coordination coordination; |
| 61 |
}; |
| 62 |
|
| 63 |
class UFFTorsion |
| 64 |
{ |
| 65 |
public: |
| 66 |
Index _atom1; |
| 67 |
Index _atom2; |
| 68 |
Index _atom3; |
| 69 |
Index _atom4; |
| 70 |
Real _cos_phi0; |
| 71 |
Real _ijkl; |
| 72 |
short _n; // periodicity |
| 73 |
}; |
| 74 |
|
| 75 |
class UFFOOP |
| 76 |
{ |
| 77 |
public: |
| 78 |
Index _atom1; // central atom |
| 79 |
Index _atom2; |
| 80 |
Index _atom3; |
| 81 |
Index _atom4; |
| 82 |
Real _c0; |
| 83 |
Real _c1; |
| 84 |
Real _c2; |
| 85 |
Real _koop; |
| 86 |
}; |
| 87 |
|
| 88 |
class UFFVdW |
| 89 |
{ |
| 90 |
public: |
| 91 |
Index _atom1; |
| 92 |
Index _atom2; |
| 93 |
Real _depth; |
| 94 |
Real _x; |
| 95 |
Real _x6; // precomputed x^6 |
| 96 |
Real _x12; // precomputed x^12 |
| 97 |
}; |
| 98 |
|
| 99 |
// VdW distance cutoff squared (10 Angstrom cutoff). |
| 100 |
// UFF equilibrium VdW distances are 2.5-4.5 Angstroms, |
| 101 |
// so at ~2.5x the largest, LJ energy is negligible. |
| 102 |
static constexpr Real VDW_CUTOFF_SQ = 100.0; |
| 103 |
// Rebuild VdW pair list when max atom displacement exceeds 2 Angstroms |
| 104 |
static constexpr Real VDW_REBUILD_THRESHOLD_SQ = 4.0; |
| 105 |
// Only check for rebuild every N evaluations |
| 106 |
static constexpr int VDW_REBUILD_CHECK_INTERVAL = 10; |
| 107 |
|
| 108 |
class UFFPrivate // track the particular calculations for a molecule |
| 109 |
{ |
| 110 |
public: |
| 111 |
std::vector<int> m_atomTypes; // entry row in uffparams |
| 112 |
std::vector<UFFBond> m_bonds; |
| 113 |
std::vector<UFFAngle> m_angles; |
| 114 |
std::vector<UFFOOP> m_oops; |
| 115 |
std::vector<UFFTorsion> m_torsions; |
| 116 |
std::vector<UFFVdW> m_vdws; |
| 117 |
std::unordered_set<uint64_t> m_excludedPairs; // 1-2 and 1-3 pairs |
| 118 |
Eigen::VectorXd m_lastVdWPositions; // positions when VdW list was last built |
| 119 |
int m_vdwCallCount = 0; // counter for rebuild check interval |
| 120 |
Core::Molecule* m_molecule; |
| 121 |
Core::UnitCell* m_cell; |
| 122 |
|
| 123 |
static uint64_t pairKey(Index i, Index j) |
| 124 |
{ |
| 125 |
return i < j ? (uint64_t(i) << 32 | uint64_t(j)) |
| 126 |
: (uint64_t(j) << 32 | uint64_t(i)); |
| 127 |
} |
| 128 |
|
| 129 |
UFFPrivate(Core::Molecule* mol = nullptr) : m_molecule(mol), m_cell(nullptr) |
| 130 |
{ |
| 131 |
if (mol == nullptr || mol->atomCount() < 2) { |
| 132 |
return; // nothing to do |
| 133 |
} |
| 134 |
|
| 135 |
m_cell = mol->unitCell(); // could be nullptr |
| 136 |
|
| 137 |
setAtomTypes(); |
| 138 |
setBonds(); |
| 139 |
setAngles(); |
| 140 |
setOOPs(); |
| 141 |
setTorsions(); |
| 142 |
buildExclusionSet(); |
| 143 |
|
| 144 |
// Extract initial positions for VdW cutoff |
| 145 |
Core::Array<Vector3> pos = mol->atomPositions3d(); |
| 146 |
Eigen::Map<Eigen::VectorXd> posMap(pos[0].data(), 3 * mol->atomCount()); |
| 147 |
setVdWs(posMap); |
| 148 |
} |
| 149 |
|
| 150 |
void setAtomTypes() |
| 151 |
{ |
| 152 |
// set up the calculations including atom types and parameters |
| 153 |
// loop through atoms to get the atom types |
| 154 |
m_atomTypes.reserve(m_molecule->atomCount()); |
| 155 |
for (Index i = 0; i < m_molecule->atomCount(); ++i) { |
| 156 |
const Core::Atom& atom = m_molecule->atom(i); |
| 157 |
int atomicNumber = atom.atomicNumber(); |
| 158 |
int atomType = -1; |
| 159 |
for (int j = 0; j < 126; ++j) { |
| 160 |
if (uffparams[j].element == atomicNumber) { |
| 161 |
atomType = j; |
| 162 |
// not guaranteed - only the first type for this element |
| 163 |
// we can peek ahead to see if there are any more for this element |
| 164 |
if (j < 125 && uffparams[j + 1].element == atomicNumber) { |
| 165 |
// there are multiple types for this element |
| 166 |
const Array<Bond> bonds = m_molecule->bonds(i); |
| 167 |
if (atomicNumber == 1) { |
| 168 |
// hydrogen has two types, so count the bonds |
| 169 |
if (bonds.size() == 1) { |
| 170 |
atomType = j; // hydrogen with one bond |
| 171 |
break; |
| 172 |
} else { // hydrogen with more than one bond = bridging type |
| 173 |
atomType = j + 1; |
| 174 |
break; |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
// remaining elements, we want the coordination number |
| 179 |
// and possibly formal charge |
| 180 |
[[maybe_unused]] int charge = m_molecule->formalCharge(i); |
| 181 |
// UFF fortunately only has square planar as special cases |
| 182 |
// so if it's more than 4 bonds, we just use that type |
| 183 |
if (bonds.size() > 4) { |
| 184 |
// check the atom type symbols |
| 185 |
char coord = '4' + (bonds.size() - 4); |
| 186 |
if (uffparams[j].label[2] == coord) { |
| 187 |
atomType = j; |
| 188 |
break; |
| 189 |
} else { |
| 190 |
// check the next one |
| 191 |
if (uffparams[j + 1].label[2] == coord) { |
| 192 |
atomType = j + 1; |
| 193 |
break; |
| 194 |
} |
| 195 |
} |
| 196 |
} else { |
| 197 |
// count double and triple bonds |
| 198 |
int doubleBonds = 0; |
| 199 |
int tripleBonds = 0; |
| 200 |
for (const Bond bond : bonds) { |
| 201 |
unsigned char order = bond.order(); |
| 202 |
if (order == 2) { |
| 203 |
++doubleBonds; |
| 204 |
} else if (order == 3) { |
| 205 |
++tripleBonds; |
| 206 |
} |
| 207 |
} |
| 208 |
char coord = '3'; |
| 209 |
if (tripleBonds > 0 || doubleBonds > 1) |
| 210 |
coord = '1'; // sp linear |
| 211 |
else if (doubleBonds == 1) |
| 212 |
coord = '2'; // sp2 trigonal |
| 213 |
else if (doubleBonds == 0 && tripleBonds == 0) |
| 214 |
coord = '3'; // sp3 tetrahedral |
| 215 |
|
| 216 |
// check the atom type symbols |
| 217 |
if (uffparams[j].label[2] == coord) { |
| 218 |
atomType = j; |
| 219 |
break; |
| 220 |
} else { |
| 221 |
// bump up until we find one |
| 222 |
while (j < 125 && uffparams[j].label[2] != coord) { |
| 223 |
++j; |
| 224 |
} |
| 225 |
atomType = j; |
| 226 |
break; |
| 227 |
} |
| 228 |
} |
| 229 |
break; |
| 230 |
} |
| 231 |
|
| 232 |
// nope it's the only one for this element |
| 233 |
// we're done |
| 234 |
break; |
| 235 |
} |
| 236 |
} |
| 237 |
m_atomTypes.push_back(atomType); |
| 238 |
} |
| 239 |
// we need to do one more pass to set aromatic / resonance types |
| 240 |
// i.e., if an sp2 atom has neighbors, mark them as resonant |
| 241 |
for (Index i = 0; i < m_molecule->atomCount(); ++i) { |
| 242 |
const Core::Atom& atom = m_molecule->atom(i); |
| 243 |
int atomicNumber = atom.atomicNumber(); |
| 244 |
// carbon, nitrogen, oxygen and sulfur sp2 atoms |
| 245 |
if (atomicNumber == 6 || atomicNumber == 7 || atomicNumber == 8 || |
| 246 |
atomicNumber == 16) { |
| 247 |
|
| 248 |
const char symbolChar = uffparams[m_atomTypes[i]].label[2]; |
| 249 |
|
| 250 |
// we might have an aromatic / resonant N, O, or S |
| 251 |
// e.g., furan, thiophene .. we also mark amide N |
| 252 |
// if it's an sp3 carbon, skip it |
| 253 |
if (atomicNumber == 6 && symbolChar == '3') |
| 254 |
continue; |
| 255 |
|
| 256 |
// check the neighbors |
| 257 |
const std::vector<Index>& neighbors = m_molecule->graph().neighbors(i); |
| 258 |
|
| 259 |
// we can skip carbonyl oxygen (i.e., only one neighbor) |
| 260 |
if ((atomicNumber == 8 || atomicNumber == 16) && neighbors.size() == 1) |
| 261 |
continue; |
| 262 |
|
| 263 |
bool resonant = false; |
| 264 |
for (Index j : neighbors) { |
| 265 |
auto symbolLabel = uffparams[m_atomTypes[j]].label; |
| 266 |
if (symbolLabel.size() < 3) |
| 267 |
continue; // not a resonant type |
| 268 |
|
| 269 |
if (symbolLabel[2] == '2' || symbolLabel[2] == 'R') { |
| 270 |
resonant = true; |
| 271 |
break; |
| 272 |
} |
| 273 |
} |
| 274 |
if (resonant) { |
| 275 |
// set the resonant type |
| 276 |
if (atomicNumber == 7 && symbolChar == '3') |
| 277 |
m_atomTypes[i] = m_atomTypes[i] + 1; // N_R after N_3 |
| 278 |
else if (atomicNumber == 8 && symbolChar == '3') |
| 279 |
m_atomTypes[i] = m_atomTypes[i] + 2; // O_R after O_3 and O_3_z |
| 280 |
else if (atomicNumber == 16 && symbolChar == '3') { |
| 281 |
// loop until we find 'R' .. might be a few different S types |
| 282 |
while (uffparams[m_atomTypes[i]].label[2] != 'R') |
| 283 |
++m_atomTypes[i]; |
| 284 |
} else |
| 285 |
m_atomTypes[i] = m_atomTypes[i] - 1; // C_R before C_2 |
| 286 |
} |
| 287 |
} |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
// calculate the ideal bond distance between two atoms |
| 292 |
// used in a few places |
| 293 |
Real calculateRij(Index atom1, Index atom2) |
| 294 |
{ |
| 295 |
Real ri = uffparams[m_atomTypes[atom1]].r1; |
| 296 |
Real rj = uffparams[m_atomTypes[atom2]].r1; |
| 297 |
Real r0 = ri + rj; |
| 298 |
// bond order correction |
| 299 |
Bond bond = m_molecule->bond(atom1, atom2); |
| 300 |
Real order = static_cast<Real>(bond.order()); |
| 301 |
// check if it's a resonant / aromatic bond |
| 302 |
auto symbol1 = uffparams[m_atomTypes[atom1]].label; |
| 303 |
auto symbol2 = uffparams[m_atomTypes[atom2]].label; |
| 304 |
if (symbol1.size() > 2 && symbol1[2] == 'R' && symbol2.size() > 2 && |
| 305 |
symbol2[2] == 'R') { |
| 306 |
order = 1.5; |
| 307 |
// tweak for amide |
| 308 |
if ((symbol1[0] == 'N' && symbol2[0] == 'C') || |
| 309 |
(symbol1[0] == 'C' && symbol2[0] == 'N')) |
| 310 |
order = 1.41; |
| 311 |
} |
| 312 |
Real rbo = -0.1332 * r0 * log(order); |
| 313 |
|
| 314 |
// electronegativity correction |
| 315 |
Real chi1 = uffparams[m_atomTypes[atom1]].Xi; |
| 316 |
Real chi2 = uffparams[m_atomTypes[atom2]].Xi; |
| 317 |
Real ren = |
| 318 |
ri * rj * pow((sqrt(chi1) - sqrt(chi2)), 2) / (chi1 * ri + chi2 * rj); |
| 319 |
|
| 320 |
// UFF publication has an error (electronegativity correction): |
| 321 |
// https://towhee.sourceforge.net/forcefields/uff.html |
| 322 |
return r0 + rbo - ren; |
| 323 |
} |
| 324 |
|
| 325 |
void setBonds() |
| 326 |
{ |
| 327 |
// loop through the bonds |
| 328 |
for (Index i = 0; i < m_molecule->bondCount(); ++i) { |
| 329 |
const Core::Bond& bond = m_molecule->bond(i); |
| 330 |
Index atom1 = bond.atom1().index(); |
| 331 |
Index atom2 = bond.atom2().index(); |
| 332 |
UFFBond b; |
| 333 |
b._atom1 = atom1; |
| 334 |
b._atom2 = atom2; |
| 335 |
|
| 336 |
b._r0 = calculateRij(atom1, atom2); |
| 337 |
Real z1 = uffparams[m_atomTypes[atom1]].Z1; |
| 338 |
Real z2 = uffparams[m_atomTypes[atom2]].Z1; |
| 339 |
b._kb = 664.12 * z1 * z2 / pow((b._r0), 3); |
| 340 |
m_bonds.push_back(b); |
| 341 |
|
| 342 |
/* |
| 343 |
std::cout << " bond " << atom1 << " " |
| 344 |
<< uffparams[m_atomTypes[atom1]].label << " " << atom2 << " " |
| 345 |
<< uffparams[m_atomTypes[atom2]].label << " " << b._r0 << " " |
| 346 |
<< b._kb << std::endl; |
| 347 |
*/ |
| 348 |
} |
| 349 |
} |
| 350 |
|
| 351 |
void setAngles() |
| 352 |
{ |
| 353 |
AngleIterator ai(m_molecule); |
| 354 |
auto angle = ai.begin(); |
| 355 |
while (angle != ai.end()) { |
| 356 |
Index i = std::get<0>(angle); |
| 357 |
Index j = std::get<1>(angle); |
| 358 |
Index k = std::get<2>(angle); |
| 359 |
UFFAngle a; |
| 360 |
a._atom1 = i; |
| 361 |
a._atom2 = j; |
| 362 |
a._atom3 = k; |
| 363 |
|
| 364 |
Real theta0 = uffparams[m_atomTypes[j]].theta0 * DEG_TO_RAD; |
| 365 |
a._theta0 = theta0 * RAD_TO_DEG; // store in degrees for consistency |
| 366 |
|
| 367 |
// calculate the kijk |
| 368 |
Real rij = calculateRij(i, j); |
| 369 |
Real rjk = calculateRij(j, k); |
| 370 |
|
| 371 |
/* |
| 372 |
std::cout << " Angle " << i << " " << j << " " << k << " " << rij << " " |
| 373 |
<< rjk << " " << theta0 << std::endl; |
| 374 |
*/ |
| 375 |
|
| 376 |
Real rik = sqrt(rij * rij + rjk * rjk - 2 * rij * rjk * cos(theta0)); |
| 377 |
Real Zi = uffparams[m_atomTypes[i]].Z1; |
| 378 |
Real Zk = uffparams[m_atomTypes[k]].Z1; |
| 379 |
Real cosTheta0 = cos(theta0); |
| 380 |
Real cosTheta0Sq = cosTheta0 * cosTheta0; |
| 381 |
// see https://towhee.sourceforge.net/forcefields/uff.html |
| 382 |
// e.g., original paper had some typos |
| 383 |
a._kijk = 664.12 / (rij * rjk) * (Zi * Zk) / (pow(rik, 5)) * rij * rjk; |
| 384 |
a._kijk *= (3 * rij * rjk * (1 - cosTheta0Sq) - rik * rik * cosTheta0); |
| 385 |
|
| 386 |
// calculate the c0, c1, c2 terms |
| 387 |
// based on coordination of the central atom |
| 388 |
std::string label = uffparams[m_atomTypes[j]].label; |
| 389 |
auto neighbors = m_molecule->graph().neighbors(j); |
| 390 |
a._c0 = 0.0; |
| 391 |
a._c1 = 0.0; |
| 392 |
a._c2 = 0.0; |
| 393 |
|
| 394 |
if (label.size() < 3 || neighbors.size() == 1 || label[2] == '1') { |
| 395 |
// linear |
| 396 |
a.coordination = Linear; |
| 397 |
a._c0 = 1.0; |
| 398 |
} else if ((label[2] == '2' || label[2] == 'R') && |
| 399 |
neighbors.size() == 3) { |
| 400 |
a.coordination = Trigonal; |
| 401 |
a._kijk = a._kijk / 9.0; // divide by n**2 |
| 402 |
a._c0 = 3.0; |
| 403 |
} else if (label[2] == '4') { |
| 404 |
a.coordination = SquarePlanar; |
| 405 |
a._kijk = a._kijk / 16.0; |
| 406 |
a._c0 = 4.0; |
| 407 |
} else if (label[2] == '5') { |
| 408 |
// TODO |
| 409 |
a.coordination = TrigonalBipyramidal; |
| 410 |
} else if (label[2] == '6') { |
| 411 |
a.coordination = Octahedral; |
| 412 |
a._kijk = a._kijk / 16.0; |
| 413 |
a._c0 = 4.0; |
| 414 |
} else if (neighbors.size() > 6) { |
| 415 |
// TODO - trigonal bipentagonal and higher coordination |
| 416 |
// (e.g., as a repulsion between the other atoms) |
| 417 |
a.coordination = Other; |
| 418 |
} else { |
| 419 |
a.coordination = Tetrahedral; |
| 420 |
Real sinTheta0 = sin(theta0); |
| 421 |
a._c2 = 1.0 / (4.0 * sinTheta0 * sinTheta0); |
| 422 |
a._c1 = -4.0 * a._c2 * cosTheta0; |
| 423 |
a._c0 = a._c2 * (2.0 * cosTheta0 * cosTheta0 + 1.0); |
| 424 |
} |
| 425 |
|
| 426 |
m_angles.push_back(a); |
| 427 |
angle = ++ai; |
| 428 |
} |
| 429 |
} |
| 430 |
|
| 431 |
void setOOPs() |
| 432 |
{ |
| 433 |
// loop through atoms checking for cases with 3 neighbors |
| 434 |
for (Index i = 0; i < m_molecule->atomCount(); ++i) { |
| 435 |
auto neighbors = m_molecule->graph().neighbors(i); |
| 436 |
if (neighbors.size() != 3) |
| 437 |
continue; |
| 438 |
|
| 439 |
// also only for certain elements |
| 440 |
const Core::Atom& atom = m_molecule->atom(i); |
| 441 |
int atomicNumber = atom.atomicNumber(); |
| 442 |
switch (atomicNumber) { |
| 443 |
case 6: // carbon |
| 444 |
case 7: // nitrogen |
| 445 |
case 8: // oxygen |
| 446 |
case 15: // phos. |
| 447 |
case 33: // as |
| 448 |
case 51: // sb |
| 449 |
case 83: // bi |
| 450 |
break; |
| 451 |
default: // no inversion term for this element |
| 452 |
continue; |
| 453 |
} |
| 454 |
|
| 455 |
UFFOOP oop; |
| 456 |
oop._atom1 = i; |
| 457 |
oop._atom2 = neighbors[0]; |
| 458 |
oop._atom3 = neighbors[1]; |
| 459 |
oop._atom4 = neighbors[2]; |
| 460 |
|
| 461 |
std::string symbol = uffparams[m_atomTypes[i]].label; |
| 462 |
if (symbol == "N_R" || symbol == "N_2" || symbol == "O_R" || |
| 463 |
symbol == "O_2") { |
| 464 |
oop._c0 = 1.0; |
| 465 |
oop._c1 = -1.0; |
| 466 |
oop._c2 = 0.0; |
| 467 |
oop._koop = 6.0; |
| 468 |
} else if (symbol == "P_3+3" || symbol == "As3+3" || symbol == "Sb3+3" || |
| 469 |
symbol == "Bi3+3") { |
| 470 |
|
| 471 |
Real phi; |
| 472 |
switch (atomicNumber) { |
| 473 |
case 15: // P |
| 474 |
phi = 84.4339 * DEG_TO_RAD; |
| 475 |
break; |
| 476 |
case 33: // As |
| 477 |
phi = 86.9735 * DEG_TO_RAD; |
| 478 |
break; |
| 479 |
case 51: // Sb |
| 480 |
phi = 87.7047 * DEG_TO_RAD; |
| 481 |
break; |
| 482 |
case 83: // Bi |
| 483 |
default: |
| 484 |
phi = 90.0; |
| 485 |
} |
| 486 |
oop._c1 = -4.0 * cos(phi); |
| 487 |
oop._c2 = 1.0; |
| 488 |
oop._c0 = -1.0 * oop._c1 * cos(phi) + oop._c2 * cos(2.0 * phi); |
| 489 |
oop._koop = 22.0; |
| 490 |
} else if (symbol == "C_2" || symbol == "C_R") { |
| 491 |
oop._c0 = 1.0; |
| 492 |
oop._c1 = -1.0; |
| 493 |
oop._c2 = 0.0; |
| 494 |
oop._koop = 6.0; |
| 495 |
// check if one of the other atoms is "O_2" |
| 496 |
if (uffparams[m_atomTypes[neighbors[0]]].label == "O_2" || |
| 497 |
uffparams[m_atomTypes[neighbors[1]]].label == "O_2" || |
| 498 |
uffparams[m_atomTypes[neighbors[2]]].label == "O_2") { |
| 499 |
oop._koop = 50.0; |
| 500 |
} |
| 501 |
} else { |
| 502 |
continue; |
| 503 |
} |
| 504 |
|
| 505 |
m_oops.push_back(oop); |
| 506 |
} |
| 507 |
} |
| 508 |
|
| 509 |
void setTorsions() |
| 510 |
{ |
| 511 |
DihedralIterator di(m_molecule); |
| 512 |
auto dihedral = di.begin(); |
| 513 |
while (dihedral != di.end()) { |
| 514 |
Index i = std::get<0>(dihedral); |
| 515 |
Index j = std::get<1>(dihedral); |
| 516 |
Index k = std::get<2>(dihedral); |
| 517 |
Index l = std::get<3>(dihedral); |
| 518 |
|
| 519 |
// check the bond order of j-k |
| 520 |
// (if it's not rotatable, we can skip this one) |
| 521 |
Bond bond = m_molecule->bond(j, k); |
| 522 |
if (bond.order() != 1) { |
| 523 |
dihedral = ++di; |
| 524 |
continue; |
| 525 |
} |
| 526 |
|
| 527 |
UFFTorsion t; |
| 528 |
t._atom1 = i; |
| 529 |
t._atom2 = j; |
| 530 |
t._atom3 = k; |
| 531 |
t._atom4 = l; |
| 532 |
|
| 533 |
// default is for sp3-sp3 |
| 534 |
Real order = static_cast<Real>(bond.order()); |
| 535 |
|
| 536 |
auto symbol1 = uffparams[m_atomTypes[j]].label; |
| 537 |
auto symbol2 = uffparams[m_atomTypes[k]].label; |
| 538 |
|
| 539 |
// TODO: a bunch of special cases |
| 540 |
if (symbol1.size() < 3 || symbol2.size() < 3 || symbol1[2] == '3' || |
| 541 |
symbol2[2] == '3') { |
| 542 |
// default is sp3-sp3 |
| 543 |
t._n = 3; |
| 544 |
Real Vi_j = uffparams[m_atomTypes[j]].Vi; |
| 545 |
Real Vi_k = uffparams[m_atomTypes[k]].Vi; |
| 546 |
Real phi0 = 60.0; |
| 547 |
|
| 548 |
// handle some special cases |
| 549 |
// e.g. a pair of group 6 sp3 atoms |
| 550 |
auto atomicNumberJ = m_molecule->atom(j).atomicNumber(); |
| 551 |
auto atomicNumberK = m_molecule->atom(k).atomicNumber(); |
| 552 |
switch (atomicNumberJ) { |
| 553 |
case 8: // oxygen |
| 554 |
t._n = 2; |
| 555 |
phi0 = 90.0; // hydrogen peroxide H-O-O-H |
| 556 |
Vi_j = 2.0; |
| 557 |
break; |
| 558 |
case 16: // sulfur |
| 559 |
case 34: // selenium |
| 560 |
case 52: // tellurium |
| 561 |
case 84: // polonium |
| 562 |
Vi_j = 6.8; |
| 563 |
t._n = 2; |
| 564 |
phi0 = 90.0; |
| 565 |
default: |
| 566 |
break; |
| 567 |
} |
| 568 |
switch (atomicNumberK) { |
| 569 |
case 8: // oxygen |
| 570 |
t._n = 2; |
| 571 |
phi0 = 90.0; // hydrogen peroxide H-O-O-H |
| 572 |
Vi_k = 2.0; |
| 573 |
break; |
| 574 |
case 16: // sulfur |
| 575 |
case 34: // selenium |
| 576 |
case 52: // tellurium |
| 577 |
case 84: // polonium |
| 578 |
Vi_k = 6.8; |
| 579 |
t._n = 2; |
| 580 |
phi0 = 90.0; |
| 581 |
default: |
| 582 |
break; |
| 583 |
} |
| 584 |
|
| 585 |
t._cos_phi0 = cos(t._n * phi0 * DEG_TO_RAD); |
| 586 |
// geometric mean of the two V1 parameters |
| 587 |
t._ijkl = 0.5 * sqrt(Vi_j * Vi_k); |
| 588 |
} else if (symbol1[2] == 'R' && symbol2[2] == 'R') { |
| 589 |
order = 1.5; |
| 590 |
// tweak for amide |
| 591 |
if ((symbol1[0] == 'N' && symbol2[0] == 'C') || |
| 592 |
(symbol1[0] == 'C' && symbol2[0] == 'N')) |
| 593 |
order = 1.41; |
| 594 |
t._n = 2; |
| 595 |
t._cos_phi0 = cos(t._n * 180.0 * DEG_TO_RAD); |
| 596 |
t._ijkl = 5.0 * sqrt(uffparams[m_atomTypes[j]].Uj * |
| 597 |
uffparams[m_atomTypes[k]].Uj); |
| 598 |
t._ijkl *= 0.5 * (1.0 + 4.18 * log(order)); |
| 599 |
} else if ((symbol1[2] == '2' && symbol2[2] == '3') || |
| 600 |
(symbol1[2] == '3' && symbol2[2] == '2')) { |
| 601 |
// sp2-sp3 |
| 602 |
t._cos_phi0 = cos(0.0 * DEG_TO_RAD); |
| 603 |
t._n = 6; |
| 604 |
t._ijkl = 0.5; |
| 605 |
|
| 606 |
// exceptions for Oxygen, Sulfur, Selenium, Tellurium, Polonium |
| 607 |
auto atomicNumberJ = m_molecule->atom(j).atomicNumber(); |
| 608 |
auto atomicNumberK = m_molecule->atom(k).atomicNumber(); |
| 609 |
if (atomicNumberJ == 8 || atomicNumberK == 8 || atomicNumberJ == 16 || |
| 610 |
atomicNumberK == 16 || atomicNumberJ == 34 || atomicNumberK == 34 || |
| 611 |
atomicNumberJ == 52 || atomicNumberK == 52 || atomicNumberJ == 84 || |
| 612 |
atomicNumberK == 84) { |
| 613 |
t._n = 2; |
| 614 |
t._cos_phi0 = cos(90.0 * DEG_TO_RAD); |
| 615 |
} |
| 616 |
|
| 617 |
} else { |
| 618 |
dihedral = ++di; |
| 619 |
continue; |
| 620 |
} |
| 621 |
|
| 622 |
m_torsions.push_back(t); |
| 623 |
dihedral = ++di; |
| 624 |
} |
| 625 |
} |
| 626 |
|
| 627 |
// Build a hash set of all 1-2 and 1-3 atom pairs for O(1) exclusion lookup |
| 628 |
void buildExclusionSet() |
| 629 |
{ |
| 630 |
const auto& graph = m_molecule->graph(); |
| 631 |
for (Index i = 0; i < m_molecule->atomCount(); ++i) { |
| 632 |
const std::vector<Index>& neighbors = graph.neighbors(i); |
| 633 |
for (Index j : neighbors) { |
| 634 |
// 1-2 pair |
| 635 |
m_excludedPairs.insert(pairKey(i, j)); |
| 636 |
// 1-3 pairs: neighbors of j that aren't i |
| 637 |
const std::vector<Index>& neighbors2 = graph.neighbors(j); |
| 638 |
for (Index k : neighbors2) { |
| 639 |
if (k != i) |
| 640 |
m_excludedPairs.insert(pairKey(i, k)); |
| 641 |
} |
| 642 |
} |
| 643 |
} |
| 644 |
} |
| 645 |
|
| 646 |
// Check if atoms i and j are 1-2 or 1-3 connected (O(1) lookup) |
| 647 |
bool areExcluded(Index i, Index j) const |
| 648 |
{ |
| 649 |
return m_excludedPairs.count(pairKey(i, j)) > 0; |
| 650 |
} |
| 651 |
|
| 652 |
// Build or rebuild the VdW pair list with distance cutoff |
| 653 |
void setVdWs(const Eigen::VectorXd& x) |
| 654 |
{ |
| 655 |
m_vdws.clear(); |
| 656 |
m_lastVdWPositions = x; |
| 657 |
|
| 658 |
const Index n = m_molecule->atomCount(); |
| 659 |
for (Index i = 0; i < n; ++i) { |
| 660 |
for (Index j = i + 1; j < n; ++j) { |
| 661 |
if (areExcluded(i, j)) |
| 662 |
continue; |
| 663 |
|
| 664 |
// Apply distance cutoff |
| 665 |
Real r2; |
| 666 |
if (m_cell == nullptr) { |
| 667 |
const Vector3d diff = x.segment<3>(3 * i) - x.segment<3>(3 * j); |
| 668 |
r2 = diff.squaredNorm(); |
| 669 |
} else { |
| 670 |
r2 = m_cell->distanceSquared(Vector3(x.segment<3>(3 * i)), |
| 671 |
Vector3(x.segment<3>(3 * j))); |
| 672 |
} |
| 673 |
if (r2 > VDW_CUTOFF_SQ) |
| 674 |
continue; |
| 675 |
|
| 676 |
UFFVdW v; |
| 677 |
v._atom1 = i; |
| 678 |
v._atom2 = j; |
| 679 |
v._depth = |
| 680 |
sqrt(uffparams[m_atomTypes[i]].D1 * uffparams[m_atomTypes[j]].D1); |
| 681 |
v._x = |
| 682 |
sqrt(uffparams[m_atomTypes[i]].x1 * uffparams[m_atomTypes[j]].x1); |
| 683 |
Real x3 = v._x * v._x * v._x; |
| 684 |
v._x6 = x3 * x3; |
| 685 |
v._x12 = v._x6 * v._x6; |
| 686 |
m_vdws.push_back(v); |
| 687 |
} |
| 688 |
} |
| 689 |
} |
| 690 |
|
| 691 |
// Rebuild the VdW pair list if any atom has moved significantly. |
| 692 |
// Only checks every N calls to avoid O(N) overhead on every evaluation. |
| 693 |
void rebuildVdWsIfNeeded(const Eigen::VectorXd& x) |
| 694 |
{ |
| 695 |
if (m_lastVdWPositions.size() != x.size()) { |
| 696 |
setVdWs(x); |
| 697 |
return; |
| 698 |
} |
| 699 |
|
| 700 |
if (++m_vdwCallCount % VDW_REBUILD_CHECK_INTERVAL != 0) |
| 701 |
return; |
| 702 |
|
| 703 |
// Check max squared displacement across all atoms |
| 704 |
const Index n = m_molecule->atomCount(); |
| 705 |
for (Index i = 0; i < n; ++i) { |
| 706 |
const Vector3d disp = |
| 707 |
x.segment<3>(3 * i) - m_lastVdWPositions.segment<3>(3 * i); |
| 708 |
if (disp.squaredNorm() > VDW_REBUILD_THRESHOLD_SQ) { |
| 709 |
setVdWs(x); |
| 710 |
return; |
| 711 |
} |
| 712 |
} |
| 713 |
} |
| 714 |
|
| 715 |
void bondEvaluate(const Eigen::VectorXd& x, Real* energy, |
| 716 |
Eigen::VectorXd* grad) |
| 717 |
{ |
| 718 |
for (const UFFBond& bond : m_bonds) { |
| 719 |
const Index i = bond._atom1; |
| 720 |
const Index j = bond._atom2; |
| 721 |
const Vector3d diff = x.segment<3>(3 * i) - x.segment<3>(3 * j); |
| 722 |
const Real r = diff.norm(); |
| 723 |
const Real dr = r - bond._r0; |
| 724 |
|
| 725 |
if (energy != nullptr) { |
| 726 |
// the 0.5 * kb is already in kb to save a multiplication |
| 727 |
*energy += bond._kb * dr * dr; |
| 728 |
} |
| 729 |
|
| 730 |
if (grad == nullptr || r < 1e-3) |
| 731 |
continue; |
| 732 |
|
| 733 |
const Real dEdr = 2.0 * bond._kb * dr; |
| 734 |
const Vector3d direction = diff / r; |
| 735 |
grad->segment<3>(3 * i) += dEdr * direction; |
| 736 |
grad->segment<3>(3 * j) -= dEdr * direction; |
| 737 |
} |
| 738 |
} |
| 739 |
|
| 740 |
void angleEvaluate(const Eigen::VectorXd& x, Real* energy, |
| 741 |
Eigen::VectorXd* grad) |
| 742 |
{ |
| 743 |
const bool needsGradient = grad != nullptr; |
| 744 |
const bool needsEnergy = energy != nullptr; |
| 745 |
|
| 746 |
for (const UFFAngle& angle : m_angles) { |
| 747 |
const Index i = angle._atom1; |
| 748 |
const Index j = angle._atom2; |
| 749 |
const Index k = angle._atom3; |
| 750 |
const Real theta0 = angle._theta0 * DEG_TO_RAD; |
| 751 |
const Real kijk = angle._kijk; |
| 752 |
const Real c0 = angle._c0; |
| 753 |
const Real c1 = angle._c1; |
| 754 |
const Real c2 = angle._c2; |
| 755 |
|
| 756 |
const Vector3d vi = x.segment<3>(3 * i); |
| 757 |
const Vector3d vj = x.segment<3>(3 * j); |
| 758 |
const Vector3d vk = x.segment<3>(3 * k); |
| 759 |
|
| 760 |
Real thetaGradient = 0.0; |
| 761 |
Vector3d grad_i = Vector3d::Zero(); |
| 762 |
Vector3d grad_j = Vector3d::Zero(); |
| 763 |
Vector3d grad_k = Vector3d::Zero(); |
| 764 |
if (needsGradient) { |
| 765 |
thetaGradient = |
| 766 |
::Avogadro::Calc::angleGradient(vi, vj, vk, grad_i, grad_j, grad_k); |
| 767 |
} |
| 768 |
|
| 769 |
if (needsEnergy) { |
| 770 |
// Match the legacy value() path: use acos-based angle in [0, pi]. |
| 771 |
const Vector3d ij = vi - vj; |
| 772 |
const Vector3d kj = vk - vj; |
| 773 |
const Real r1 = ij.norm(); |
| 774 |
const Real r2 = kj.norm(); |
| 775 |
const Real thetaEnergy = |
| 776 |
acos(std::clamp(ij.dot(kj) / (r1 * r2), -1.0, 1.0)); |
| 777 |
switch (angle.coordination) { |
| 778 |
case Linear: |
| 779 |
// fixed typo in UFF paper (it's 1+cos(theta), not 1-cos(theta)) |
| 780 |
*energy += kijk * (1 + cos(c0 * thetaEnergy)); |
| 781 |
break; |
| 782 |
case Trigonal: |
| 783 |
case Resonant: |
| 784 |
case SquarePlanar: |
| 785 |
case Octahedral: |
| 786 |
*energy += kijk * (1 - cos(c0 * thetaEnergy)) + |
| 787 |
exp(-20.0 * (thetaEnergy - theta0 + 0.25)); |
| 788 |
break; |
| 789 |
case Tetrahedral: { |
| 790 |
const Real cosTheta = cos(thetaEnergy); |
| 791 |
// use cos(2t) = 2cos^2(t) - 1 |
| 792 |
*energy += |
| 793 |
kijk * (c0 + c1 * cosTheta + c2 * (2 * cosTheta * cosTheta - 1)); |
| 794 |
break; |
| 795 |
} |
| 796 |
case TrigonalBipyramidal: |
| 797 |
case TrigonalBipentagonal: |
| 798 |
case Other: |
| 799 |
default: |
| 800 |
// fallback to harmonic potential for unsupported coordinations |
| 801 |
*energy += kijk * (thetaEnergy - theta0) * (thetaEnergy - theta0); |
| 802 |
break; |
| 803 |
} |
| 804 |
} |
| 805 |
|
| 806 |
if (!needsGradient || thetaGradient <= 0.1) |
| 807 |
continue; |
| 808 |
|
| 809 |
Real dEdTheta = 0.0; |
| 810 |
switch (angle.coordination) { |
| 811 |
case Linear: |
| 812 |
dEdTheta = -kijk * c0 * sin(c0 * thetaGradient); |
| 813 |
break; |
| 814 |
case Trigonal: |
| 815 |
case Resonant: |
| 816 |
case SquarePlanar: |
| 817 |
case Octahedral: |
| 818 |
dEdTheta = kijk * c0 * sin(c0 * thetaGradient) - |
| 819 |
20.0 * exp(-20.0 * (thetaGradient - theta0 + 0.25)); |
| 820 |
break; |
| 821 |
case Tetrahedral: { |
| 822 |
const Real cosTheta = cos(thetaGradient); |
| 823 |
const Real sinTheta = sin(thetaGradient); |
| 824 |
dEdTheta = |
| 825 |
-kijk * (c1 * sinTheta + c2 * 2 * (2 * cosTheta * sinTheta)); |
| 826 |
break; |
| 827 |
} |
| 828 |
case TrigonalBipyramidal: |
| 829 |
case TrigonalBipentagonal: |
| 830 |
case Other: |
| 831 |
default: |
| 832 |
dEdTheta = 2.0 * kijk * (thetaGradient - theta0) * sin(thetaGradient); |
| 833 |
break; |
| 834 |
} |
| 835 |
|
| 836 |
if (std::isnan(dEdTheta)) |
| 837 |
continue; |
| 838 |
|
| 839 |
grad->segment<3>(3 * i) += dEdTheta * grad_i; |
| 840 |
grad->segment<3>(3 * j) += dEdTheta * grad_j; |
| 841 |
grad->segment<3>(3 * k) += dEdTheta * grad_k; |
| 842 |
} |
| 843 |
} |
| 844 |
|
| 845 |
void oopEvaluate(const Eigen::VectorXd& x, Real* energy, |
| 846 |
Eigen::VectorXd* grad) |
| 847 |
{ |
| 848 |
const bool needsGradient = grad != nullptr; |
| 849 |
|
| 850 |
for (const UFFOOP& oop : m_oops) { |
| 851 |
// for UFF, i is the central atom |
| 852 |
const Index i = oop._atom1; |
| 853 |
const Index j = oop._atom2; |
| 854 |
const Index k = oop._atom3; |
| 855 |
const Index l = oop._atom4; |
| 856 |
|
| 857 |
const Real koop = oop._koop; |
| 858 |
const Real c0 = oop._c0; |
| 859 |
const Real c1 = oop._c1; |
| 860 |
const Real c2 = oop._c2; |
| 861 |
|
| 862 |
const Vector3d vi = x.segment<3>(3 * i); |
| 863 |
const Vector3d vj = x.segment<3>(3 * j); |
| 864 |
const Vector3d vk = x.segment<3>(3 * k); |
| 865 |
const Vector3d vl = x.segment<3>(3 * l); |
| 866 |
|
| 867 |
Real angleGradient = 0.0; |
| 868 |
Vector3d grad_i = Vector3d::Zero(); |
| 869 |
Vector3d grad_j = Vector3d::Zero(); |
| 870 |
Vector3d grad_k = Vector3d::Zero(); |
| 871 |
Vector3d grad_l = Vector3d::Zero(); |
| 872 |
if (needsGradient) { |
| 873 |
angleGradient = ::Avogadro::Calc::outOfPlaneGradient( |
| 874 |
vi, vj, vk, vl, grad_i, grad_j, grad_k, grad_l); |
| 875 |
} |
| 876 |
|
| 877 |
if (energy != nullptr) { |
| 878 |
// Match the legacy value() path. |
| 879 |
const Real angleEnergy = outOfPlaneAngle(vi, vj, vk, vl) * DEG_TO_RAD; |
| 880 |
*energy += |
| 881 |
koop * (c0 + c1 * cos(angleEnergy) + c2 * cos(2 * angleEnergy)); |
| 882 |
} |
| 883 |
|
| 884 |
if (!needsGradient) |
| 885 |
continue; |
| 886 |
|
| 887 |
const Real dE = |
| 888 |
koop * (-c1 * sin(angleGradient) - 2.0 * c2 * sin(2.0 * angleGradient)); |
| 889 |
if (std::isnan(dE)) |
| 890 |
continue; |
| 891 |
|
| 892 |
grad->segment<3>(3 * i) += dE * grad_i; |
| 893 |
grad->segment<3>(3 * j) += dE * grad_j; |
| 894 |
grad->segment<3>(3 * k) += dE * grad_k; |
| 895 |
grad->segment<3>(3 * l) += dE * grad_l; |
| 896 |
} |
| 897 |
} |
| 898 |
|
| 899 |
void torsionEvaluate(const Eigen::VectorXd& x, Real* energy, |
| 900 |
Eigen::VectorXd* grad) |
| 901 |
{ |
| 902 |
const bool needsGradient = grad != nullptr; |
| 903 |
|
| 904 |
for (const UFFTorsion& torsion : m_torsions) { |
| 905 |
const Index i = torsion._atom1; |
| 906 |
const Index j = torsion._atom2; |
| 907 |
const Index k = torsion._atom3; |
| 908 |
const Index l = torsion._atom4; |
| 909 |
|
| 910 |
const Vector3d vi = x.segment<3>(3 * i); |
| 911 |
const Vector3d vj = x.segment<3>(3 * j); |
| 912 |
const Vector3d vk = x.segment<3>(3 * k); |
| 913 |
const Vector3d vl = x.segment<3>(3 * l); |
| 914 |
|
| 915 |
Real phiGradient = 0.0; |
| 916 |
Vector3d grad_i = Vector3d::Zero(); |
| 917 |
Vector3d grad_j = Vector3d::Zero(); |
| 918 |
Vector3d grad_k = Vector3d::Zero(); |
| 919 |
Vector3d grad_l = Vector3d::Zero(); |
| 920 |
if (needsGradient) { |
| 921 |
phiGradient = |
| 922 |
dihedralGradient(vi, vj, vk, vl, grad_i, grad_j, grad_k, grad_l); |
| 923 |
} |
| 924 |
|
| 925 |
const Real cosPhi0 = torsion._cos_phi0; |
| 926 |
const Real kijkl = torsion._ijkl; |
| 927 |
if (energy != nullptr) { |
| 928 |
// Match the legacy value() path. |
| 929 |
const Real phiEnergy = calculateDihedral(vi, vj, vk, vl) * DEG_TO_RAD; |
| 930 |
*energy += kijkl * (1.0 - cosPhi0 * cos(torsion._n * phiEnergy)); |
| 931 |
} |
| 932 |
|
| 933 |
if (!needsGradient) |
| 934 |
continue; |
| 935 |
|
| 936 |
const Real dE = |
| 937 |
kijkl * torsion._n * sin(torsion._n * phiGradient) * cosPhi0; |
| 938 |
if (std::isnan(dE)) |
| 939 |
continue; |
| 940 |
|
| 941 |
grad->segment<3>(3 * i) += dE * grad_i; |
| 942 |
grad->segment<3>(3 * j) += dE * grad_j; |
| 943 |
grad->segment<3>(3 * k) += dE * grad_k; |
| 944 |
grad->segment<3>(3 * l) += dE * grad_l; |
| 945 |
} |
| 946 |
} |
| 947 |
|
| 948 |
void vdwEvaluate(const Eigen::VectorXd& x, Real* energy, |
| 949 |
Eigen::VectorXd* grad) |
| 950 |
{ |
| 951 |
rebuildVdWsIfNeeded(x); |
| 952 |
|
| 953 |
const bool needsGradient = grad != nullptr; |
| 954 |
|
| 955 |
for (const UFFVdW& vdw : m_vdws) { |
| 956 |
const Index i = vdw._atom1; |
| 957 |
const Index j = vdw._atom2; |
| 958 |
const Vector3d vi = x.segment<3>(3 * i); |
| 959 |
const Vector3d vj = x.segment<3>(3 * j); |
| 960 |
|
| 961 |
Vector3d diff = Vector3d::Zero(); |
| 962 |
Real r2 = 0.0; |
| 963 |
if (m_cell == nullptr) { |
| 964 |
diff = vi - vj; |
| 965 |
r2 = diff.squaredNorm(); |
| 966 |
} else if (needsGradient) { |
| 967 |
diff = m_cell->minimumImage(vi - vj); |
| 968 |
r2 = diff.squaredNorm(); |
| 969 |
} else { |
| 970 |
r2 = m_cell->distanceSquared(vi, vj); |
| 971 |
} |
| 972 |
|
| 973 |
// Skip pairs that moved beyond the cutoff since the list was built |
| 974 |
if (r2 > VDW_CUTOFF_SQ) |
| 975 |
continue; |
| 976 |
|
| 977 |
const Real r6 = r2 * r2 * r2; |
| 978 |
if (energy != nullptr) { |
| 979 |
const Real r12 = r6 * r6; |
| 980 |
*energy += vdw._depth * (vdw._x12 / r12 - 2 * vdw._x6 / r6); |
| 981 |
} |
| 982 |
|
| 983 |
if (!needsGradient) |
| 984 |
continue; |
| 985 |
|
| 986 |
const Real r = sqrt(r2); |
| 987 |
if (r < 1e-3) |
| 988 |
continue; |
| 989 |
|
| 990 |
const Vector3d direction = diff / r; |
| 991 |
const Real r7 = r6 * r; |
| 992 |
const Real dEdr = 12 * vdw._depth * vdw._x6 / r7 * (1 - vdw._x6 / r6); |
| 993 |
grad->segment<3>(3 * i) += dEdr * direction; |
| 994 |
grad->segment<3>(3 * j) -= dEdr * direction; |
| 995 |
} |
| 996 |
} |
| 997 |
|
| 998 |
void evaluate(const Eigen::VectorXd& x, Real* energy, Eigen::VectorXd* grad) |
| 999 |
{ |
| 1000 |
bondEvaluate(x, energy, grad); |
| 1001 |
angleEvaluate(x, energy, grad); |
| 1002 |
torsionEvaluate(x, energy, grad); |
| 1003 |
oopEvaluate(x, energy, grad); |
| 1004 |
vdwEvaluate(x, energy, grad); |
| 1005 |
} |
| 1006 |
|
| 1007 |
Real bondEnergies(const Eigen::VectorXd& x) |
| 1008 |
{ |
| 1009 |
Real energy = 0.0; |
| 1010 |
bondEvaluate(x, &energy, nullptr); |
| 1011 |
return energy; |
| 1012 |
} |
| 1013 |
|
| 1014 |
Real angleEnergies(const Eigen::VectorXd& x) |
| 1015 |
{ |
| 1016 |
Real energy = 0.0; |
| 1017 |
angleEvaluate(x, &energy, nullptr); |
| 1018 |
return energy; |
| 1019 |
} |
| 1020 |
|
| 1021 |
Real oopEnergies(const Eigen::VectorXd& x) |
| 1022 |
{ |
| 1023 |
Real energy = 0.0; |
| 1024 |
oopEvaluate(x, &energy, nullptr); |
| 1025 |
return energy; |
| 1026 |
} |
| 1027 |
|
| 1028 |
Real torsionEnergies(const Eigen::VectorXd& x) |
| 1029 |
{ |
| 1030 |
Real energy = 0.0; |
| 1031 |
torsionEvaluate(x, &energy, nullptr); |
| 1032 |
return energy; |
| 1033 |
} |
| 1034 |
|
| 1035 |
Real vdwEnergies(const Eigen::VectorXd& x) |
| 1036 |
{ |
| 1037 |
Real energy = 0.0; |
| 1038 |
vdwEvaluate(x, &energy, nullptr); |
| 1039 |
return energy; |
| 1040 |
} |
| 1041 |
|
| 1042 |
void bondGradient(const Eigen::VectorXd& x, Eigen::VectorXd& grad) |
| 1043 |
{ |
| 1044 |
bondEvaluate(x, nullptr, &grad); |
| 1045 |
} |
| 1046 |
|
| 1047 |
void angleGradient(const Eigen::VectorXd& x, Eigen::VectorXd& grad) |
| 1048 |
{ |
| 1049 |
angleEvaluate(x, nullptr, &grad); |
| 1050 |
} |
| 1051 |
|
| 1052 |
void oopGradient(const Eigen::VectorXd& x, Eigen::VectorXd& grad) |
| 1053 |
{ |
| 1054 |
oopEvaluate(x, nullptr, &grad); |
| 1055 |
} |
| 1056 |
|
| 1057 |
void torsionGradient(const Eigen::VectorXd& x, Eigen::VectorXd& grad) |
| 1058 |
{ |
| 1059 |
torsionEvaluate(x, nullptr, &grad); |
| 1060 |
} |
| 1061 |
|
| 1062 |
void vdwGradient(const Eigen::VectorXd& x, Eigen::VectorXd& grad) |
| 1063 |
{ |
| 1064 |
vdwEvaluate(x, nullptr, &grad); |
| 1065 |
} |
| 1066 |
}; |
| 1067 |
|
| 1068 |
UFF::UFF() : d(nullptr) |
| 1069 |
{ |
| 1070 |
// defined for 1-102 |
| 1071 |
for (unsigned int i = 0; i <= 102; ++i) { |
| 1072 |
m_elements.set(i); |
| 1073 |
} |
| 1074 |
} |
| 1075 |
|
| 1076 |
UFF::~UFF() |
| 1077 |
{ |
| 1078 |
delete d; |
| 1079 |
d = nullptr; |
| 1080 |
} |
| 1081 |
|
| 1082 |
void UFF::setMolecule(Core::Molecule* mol) |
| 1083 |
{ |
| 1084 |
if (d != nullptr) { |
| 1085 |
delete d; |
| 1086 |
d = nullptr; |
| 1087 |
} |
| 1088 |
|
| 1089 |
m_molecule = mol; |
| 1090 |
|
| 1091 |
if (mol == nullptr) { |
| 1092 |
return; // nothing to do |
| 1093 |
} |
| 1094 |
|
| 1095 |
int numAtoms = mol->atomCount(); |
| 1096 |
if (numAtoms < 2) |
| 1097 |
return; // nothing to do for single atoms |
| 1098 |
|
| 1099 |
// start with assigning atom types |
| 1100 |
d = new UFFPrivate(mol); |
| 1101 |
} |
| 1102 |
|
| 1103 |
Real UFF::value(const Eigen::VectorXd& x) |
| 1104 |
{ |
| 1105 |
if (!m_molecule || !d || |
| 1106 |
x.size() != static_cast<Eigen::Index>(3 * m_molecule->atomCount())) |
| 1107 |
return 0.0; |
| 1108 |
if (m_molecule->atomCount() < 2) |
| 1109 |
return 0.0; // no bonds |
| 1110 |
|
| 1111 |
Real energy = 0.0; |
| 1112 |
d->evaluate(x, &energy, nullptr); |
| 1113 |
energy += constraintEnergies(x); |
| 1114 |
return energy * KCAL_TO_KJ; |
| 1115 |
} |
| 1116 |
|
| 1117 |
Real UFF::evaluate(const Eigen::VectorXd& x, Eigen::VectorXd* grad) |
| 1118 |
{ |
| 1119 |
if (grad != nullptr) { |
| 1120 |
if (grad->rows() != x.rows()) |
| 1121 |
grad->resize(x.rows()); |
| 1122 |
grad->setZero(); |
| 1123 |
} |
| 1124 |
|
| 1125 |
if (!m_molecule || !d || |
| 1126 |
x.size() != static_cast<Eigen::Index>(3 * m_molecule->atomCount())) |
| 1127 |
return 0.0; |
| 1128 |
if (m_molecule->atomCount() < 2) |
| 1129 |
return 0.0; // no bonds |
| 1130 |
|
| 1131 |
Real energy = 0.0; |
| 1132 |
d->evaluate(x, &energy, grad); |
| 1133 |
|
| 1134 |
// Add constraint energies |
| 1135 |
energy += constraintEnergies(x); |
| 1136 |
|
| 1137 |
if (grad != nullptr) { |
| 1138 |
cleanGradients(*grad); |
| 1139 |
constraintGradients(x, *grad); |
| 1140 |
// convert from kcal/mol to kJ/mol |
| 1141 |
*grad *= KCAL_TO_KJ; |
| 1142 |
} |
| 1143 |
|
| 1144 |
return energy * KCAL_TO_KJ; |
| 1145 |
} |
| 1146 |
|
| 1147 |
Real UFF::bondEnergy(const Eigen::VectorXd& x) |
| 1148 |
{ |
| 1149 |
Real energy = 0.0; |
| 1150 |
|
| 1151 |
if (!m_molecule || !d || |
| 1152 |
x.size() != static_cast<Eigen::Index>(3 * m_molecule->atomCount())) |
| 1153 |
return energy; // nothing to do |
| 1154 |
if (m_molecule->atomCount() < 2) |
| 1155 |
return energy; // no bonds |
| 1156 |
|
| 1157 |
energy = d->bondEnergies(x); |
| 1158 |
return energy; |
| 1159 |
} |
| 1160 |
|
| 1161 |
Real UFF::angleEnergy(const Eigen::VectorXd& x) |
| 1162 |
{ |
| 1163 |
Real energy = 0.0; |
| 1164 |
|
| 1165 |
if (!m_molecule || !d || |
| 1166 |
x.size() != static_cast<Eigen::Index>(3 * m_molecule->atomCount())) |
| 1167 |
return energy; // nothing to do |
| 1168 |
if (m_molecule->atomCount() < 3) |
| 1169 |
return energy; // no angle |
| 1170 |
|
| 1171 |
energy = d->angleEnergies(x); |
| 1172 |
return energy; |
| 1173 |
} |
| 1174 |
|
| 1175 |
Real UFF::oopEnergy(const Eigen::VectorXd& x) |
| 1176 |
{ |
| 1177 |
Real energy = 0.0; |
| 1178 |
|
| 1179 |
if (!m_molecule || !d || |
| 1180 |
x.size() != static_cast<Eigen::Index>(3 * m_molecule->atomCount())) |
| 1181 |
return energy; // nothing to do |
| 1182 |
if (m_molecule->atomCount() < 4) |
| 1183 |
return energy; // no oop |
| 1184 |
|
| 1185 |
energy = d->oopEnergies(x); |
| 1186 |
return energy; |
| 1187 |
} |
| 1188 |
|
| 1189 |
Real UFF::torsionEnergy(const Eigen::VectorXd& x) |
| 1190 |
{ |
| 1191 |
Real energy = 0.0; |
| 1192 |
|
| 1193 |
if (!m_molecule || !d || |
| 1194 |
x.size() != static_cast<Eigen::Index>(3 * m_molecule->atomCount())) |
| 1195 |
return energy; // nothing to do |
| 1196 |
if (m_molecule->atomCount() < 4) |
| 1197 |
return energy; // no torsion |
| 1198 |
|
| 1199 |
energy = d->torsionEnergies(x); |
| 1200 |
return energy; |
| 1201 |
} |
| 1202 |
|
| 1203 |
Real UFF::vdwEnergy(const Eigen::VectorXd& x) |
| 1204 |
{ |
| 1205 |
Real energy = 0.0; |
| 1206 |
|
| 1207 |
if (!m_molecule || !d || |
| 1208 |
x.size() != static_cast<Eigen::Index>(3 * m_molecule->atomCount())) |
| 1209 |
return energy; // nothing to do |
| 1210 |
if (m_molecule->atomCount() < 2) |
| 1211 |
return energy; // nothing to do |
| 1212 |
|
| 1213 |
energy = d->vdwEnergies(x); |
| 1214 |
return energy; |
| 1215 |
} |
| 1216 |
|
| 1217 |
void UFF::gradient(const Eigen::VectorXd& x, Eigen::VectorXd& grad) |
| 1218 |
{ |
| 1219 |
evaluate(x, &grad); |
| 1220 |
} |
| 1221 |
|
| 1222 |
void UFF::bondGradient(const Eigen::VectorXd& x, Eigen::VectorXd& grad) |
| 1223 |
{ |
| 1224 |
if (!m_molecule || !d || |
| 1225 |
x.size() != static_cast<Eigen::Index>(3 * m_molecule->atomCount())) |
| 1226 |
return; // nothing to do |
| 1227 |
if (m_molecule->atomCount() < 2) |
| 1228 |
return; // no bonds |
| 1229 |
|
| 1230 |
d->bondGradient(x, grad); |
| 1231 |
} |
| 1232 |
|
| 1233 |
void UFF::angleGradient(const Eigen::VectorXd& x, Eigen::VectorXd& grad) |
| 1234 |
{ |
| 1235 |
if (!m_molecule || !d || |
| 1236 |
x.size() != static_cast<Eigen::Index>(3 * m_molecule->atomCount())) |
| 1237 |
return; // nothing to do |
| 1238 |
if (m_molecule->atomCount() < 3) |
| 1239 |
return; // no bonds |
| 1240 |
|
| 1241 |
d->angleGradient(x, grad); |
| 1242 |
} |
| 1243 |
|
| 1244 |
void UFF::oopGradient(const Eigen::VectorXd& x, Eigen::VectorXd& grad) |
| 1245 |
{ |
| 1246 |
if (!m_molecule || !d || |
| 1247 |
x.size() != static_cast<Eigen::Index>(3 * m_molecule->atomCount())) |
| 1248 |
return; // nothing to do |
| 1249 |
if (m_molecule->atomCount() < 4) |
| 1250 |
return; // no bonds |
| 1251 |
|
| 1252 |
d->oopGradient(x, grad); |
| 1253 |
} |
| 1254 |
|
| 1255 |
void UFF::torsionGradient(const Eigen::VectorXd& x, Eigen::VectorXd& grad) |
| 1256 |
{ |
| 1257 |
if (!m_molecule || !d || |
| 1258 |
x.size() != static_cast<Eigen::Index>(3 * m_molecule->atomCount())) |
| 1259 |
return; // nothing to do |
| 1260 |
if (m_molecule->atomCount() < 4) |
| 1261 |
return; // no bonds |
| 1262 |
|
| 1263 |
d->torsionGradient(x, grad); |
| 1264 |
} |
| 1265 |
|
| 1266 |
void UFF::vdwGradient(const Eigen::VectorXd& x, Eigen::VectorXd& grad) |
| 1267 |
{ |
| 1268 |
if (!m_molecule || !d || |
| 1269 |
x.size() != static_cast<Eigen::Index>(3 * m_molecule->atomCount())) |
| 1270 |
return; // nothing to do |
| 1271 |
if (m_molecule->atomCount() < 2) |
| 1272 |
return; // no bonds |
| 1273 |
|
| 1274 |
d->vdwGradient(x, grad); |
| 1275 |
} |
| 1276 |
|
| 1277 |
} // namespace Avogadro::Calc |