File: BondStretch.cpp

package info (click to toggle)
rdkit 202503.1-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 220,160 kB
  • sloc: cpp: 399,240; python: 77,453; ansic: 25,517; java: 8,173; javascript: 4,005; sql: 2,389; yacc: 1,565; lex: 1,263; cs: 1,081; makefile: 580; xml: 229; fortran: 183; sh: 105
file content (113 lines) | stat: -rw-r--r-- 3,485 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
//  Copyright (C) 2013-2025 Paolo Tosco and other RDKit contributors
//
//   @@ All Rights Reserved @@
//  This file is part of the RDKit.
//  The contents are covered by the terms of the BSD license
//  which is included in the file license.txt, found at the root
//  of the RDKit source tree.
//

#include "BondStretch.h"
#include "Params.h"
#include <ForceField/ForceField.h>
#include <RDGeneral/Invariant.h>
#include <RDGeneral/utils.h>

namespace ForceFields {
namespace MMFF {
namespace Utils {

double calcBondRestLength(const MMFFBond *mmffBondParams) {
  PRECONDITION(mmffBondParams, "bond parameters not found");

  return mmffBondParams->r0;
}

double calcBondForceConstant(const MMFFBond *mmffBondParams) {
  PRECONDITION(mmffBondParams, "bond parameters not found");

  return mmffBondParams->kb;
}

double calcBondStretchEnergy(const double r0, const double kb,
                             const double distance) {
  double distTerm = distance - r0;
  double distTerm2 = distTerm * distTerm;
  double const c1 = MDYNE_A_TO_KCAL_MOL;
  double const cs = -2.0;
  double const c3 = 7.0 / 12.0;

  return (0.5 * c1 * kb * distTerm2 *
          (1.0 + cs * distTerm + c3 * cs * cs * distTerm2));
}
}  // end of namespace Utils

BondStretchContrib::BondStretchContrib(ForceField *owner) {
  PRECONDITION(owner, "bad owner");


  dp_forceField = owner;

}

void BondStretchContrib::addTerm(const unsigned int idx1,
                                 const unsigned int idx2,
                                 const ForceFields::MMFF::MMFFBond *mmffBondParams) {
  URANGE_CHECK(idx1, dp_forceField->positions().size());
  URANGE_CHECK(idx2, dp_forceField->positions().size());
  PRECONDITION(mmffBondParams, "bond parameters not found");

  d_at1Idxs.push_back(idx1);
  d_at2Idxs.push_back(idx2);
  d_r0.push_back(mmffBondParams->r0);
  d_kb.push_back(mmffBondParams->kb);
}

double BondStretchContrib::getEnergy(double *pos) const {
  PRECONDITION(dp_forceField, "no owner");
  PRECONDITION(pos, "bad vector");

  const int numTerms = d_at1Idxs.size();
  double energySum = 0.0;
  for (int i =0; i < numTerms; i++) {
    energySum += Utils::calcBondStretchEnergy(
        d_r0[i], d_kb[i], dp_forceField->distance(d_at1Idxs[i], d_at2Idxs[i], pos));
  }
  return energySum;
}

void BondStretchContrib::getGrad(double *pos, double *grad) const {
  PRECONDITION(dp_forceField, "no owner");
  PRECONDITION(pos, "bad vector");
  PRECONDITION(grad, "bad vector");

  const int numTerms = d_at1Idxs.size();
  constexpr double cs = -2.0;
  constexpr double c1 = MDYNE_A_TO_KCAL_MOL;
  constexpr double c3 = 7.0 / 12.0;
  for (int termIdx = 0; termIdx < numTerms; termIdx++) {
    const int d_at1Idx = d_at1Idxs[termIdx];
    const int d_at2Idx = d_at2Idxs[termIdx];

    double dist = dp_forceField->distance(d_at1Idx, d_at2Idx, pos);

    double *at1Coords = &(pos[3 * d_at1Idx]);
    double *at2Coords = &(pos[3 * d_at2Idx]);
    double *g1 = &(grad[3 * d_at1Idx]);
    double *g2 = &(grad[3 * d_at2Idx]);

    double distTerm = dist - d_r0[termIdx];
    double dE_dr =
        c1 * d_kb[termIdx] * distTerm *
        (1.0 + 1.5 * cs * distTerm + 2.0 * c3 * cs * cs * distTerm * distTerm);
    double dGrad;
    for (unsigned int i = 0; i < 3; ++i) {
      dGrad = ((dist > 0.0) ? (dE_dr * (at1Coords[i] - at2Coords[i]) / dist)
                            : d_kb[termIdx] * 0.01);
      g1[i] += dGrad;
      g2[i] -= dGrad;
    }
  }
}
}  // namespace MMFF
}  // namespace ForceFields