File: ChiralViolationContribs.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 (167 lines) | stat: -rw-r--r-- 6,788 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
//
//  Copyright (C) 2024 Greg Landrum 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 "ChiralViolationContribs.h"
#include "ChiralSet.h"
#include <ForceField/ForceField.h>

namespace DistGeom {
double calcChiralVolume(const unsigned int idx1, const unsigned int idx2,
                        const unsigned int idx3, const unsigned int idx4,
                        const double *pos, const unsigned int dim) {
  // even if we are minimizing in higher dimension the chiral volume is
  // calculated using only the first 3 dimensions
  RDGeom::Point3D v1(pos[idx1 * dim] - pos[idx4 * dim],
                     pos[idx1 * dim + 1] - pos[idx4 * dim + 1],
                     pos[idx1 * dim + 2] - pos[idx4 * dim + 2]);

  RDGeom::Point3D v2(pos[idx2 * dim] - pos[idx4 * dim],
                     pos[idx2 * dim + 1] - pos[idx4 * dim + 1],
                     pos[idx2 * dim + 2] - pos[idx4 * dim + 2]);

  RDGeom::Point3D v3(pos[idx3 * dim] - pos[idx4 * dim],
                     pos[idx3 * dim + 1] - pos[idx4 * dim + 1],
                     pos[idx3 * dim + 2] - pos[idx4 * dim + 2]);

  RDGeom::Point3D v2xv3 = v2.crossProduct(v3);

  double vol = v1.dotProduct(v2xv3);
  return vol;
}
double calcChiralVolume(const unsigned int idx1, const unsigned int idx2,
                        const unsigned int idx3, const unsigned int idx4,
                        const RDGeom::PointPtrVect &pts) {
  // even if we are minimizing in higher dimension the chiral volume is
  // calculated using only the first 3 dimensions
  RDGeom::Point3D v1((*pts[idx1])[0] - (*pts[idx4])[0],
                     (*pts[idx1])[1] - (*pts[idx4])[1],
                     (*pts[idx1])[2] - (*pts[idx4])[2]);

  RDGeom::Point3D v2((*pts[idx2])[0] - (*pts[idx4])[0],
                     (*pts[idx2])[1] - (*pts[idx4])[1],
                     (*pts[idx2])[2] - (*pts[idx4])[2]);

  RDGeom::Point3D v3((*pts[idx3])[0] - (*pts[idx4])[0],
                     (*pts[idx3])[1] - (*pts[idx4])[1],
                     (*pts[idx3])[2] - (*pts[idx4])[2]);

  RDGeom::Point3D v2xv3 = v2.crossProduct(v3);

  double vol = v1.dotProduct(v2xv3);
  return vol;
}
ChiralViolationContribs::ChiralViolationContribs(
    ForceFields::ForceField *owner) {
  PRECONDITION(owner, "bad owner");
  dp_forceField = owner;
}
void ChiralViolationContribs::addContrib(const ChiralSet *cset, double weight) {
  PRECONDITION(dp_forceField, "no owner");
  PRECONDITION(cset, "bad chiral set");

  URANGE_CHECK(cset->d_idx1, dp_forceField->positions().size());
  URANGE_CHECK(cset->d_idx2, dp_forceField->positions().size());
  URANGE_CHECK(cset->d_idx3, dp_forceField->positions().size());
  URANGE_CHECK(cset->d_idx4, dp_forceField->positions().size());

  d_contribs.emplace_back(cset->d_idx1, cset->d_idx2, cset->d_idx3,
                          cset->d_idx4, cset->getUpperVolumeBound(),
                          cset->getLowerVolumeBound(), weight);
}

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

  const unsigned int dim = dp_forceField->dimension();
  double res = 0.0;
  for (const auto &c : d_contribs) {
    double vol = calcChiralVolume(c.idx1, c.idx2, c.idx3, c.idx4, pos, dim);
    if (vol < c.volLower) {
      res += c.weight * (vol - c.volLower) * (vol - c.volLower);
    } else if (vol > c.volUpper) {
      res += c.weight * (vol - c.volUpper) * (vol - c.volUpper);
    }
  }

  return res;
}

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

  const unsigned int dim = dp_forceField->dimension();

  for (const auto &c : d_contribs) {
    // even if we are minimizing in higher dimension the chiral volume is
    // calculated using only the first 3 dimensions
    RDGeom::Point3D v1(pos[c.idx1 * dim] - pos[c.idx4 * dim],
                       pos[c.idx1 * dim + 1] - pos[c.idx4 * dim + 1],
                       pos[c.idx1 * dim + 2] - pos[c.idx4 * dim + 2]);

    RDGeom::Point3D v2(pos[c.idx2 * dim] - pos[c.idx4 * dim],
                       pos[c.idx2 * dim + 1] - pos[c.idx4 * dim + 1],
                       pos[c.idx2 * dim + 2] - pos[c.idx4 * dim + 2]);

    RDGeom::Point3D v3(pos[c.idx3 * dim] - pos[c.idx4 * dim],
                       pos[c.idx3 * dim + 1] - pos[c.idx4 * dim + 1],
                       pos[c.idx3 * dim + 2] - pos[c.idx4 * dim + 2]);

    RDGeom::Point3D v2xv3 = v2.crossProduct(v3);

    double vol = v1.dotProduct(v2xv3);
    double preFactor;

    if (vol < c.volLower) {
      preFactor = c.weight * (vol - c.volLower);
    } else if (vol > c.volUpper) {
      preFactor = c.weight * (vol - c.volUpper);
    } else {
      continue;
    }

    // now comes the hard part - there are a total of 12 variables involved
    // 4 x 3 - four points and 3 dimensions
    //
    grad[dim * c.idx1] += preFactor * ((v2.y) * (v3.z) - (v3.y) * (v2.z));
    grad[dim * c.idx1 + 1] += preFactor * ((v3.x) * (v2.z) - (v2.x) * (v3.z));
    grad[dim * c.idx1 + 2] += preFactor * ((v2.x) * (v3.y) - (v3.x) * (v2.y));

    grad[dim * c.idx2] += preFactor * ((v3.y) * (v1.z) - (v3.z) * (v1.y));
    grad[dim * c.idx2 + 1] += preFactor * ((v3.z) * (v1.x) - (v3.x) * (v1.z));
    grad[dim * c.idx2 + 2] += preFactor * ((v3.x) * (v1.y) - (v3.y) * (v1.x));

    grad[dim * c.idx3] += preFactor * ((v2.z) * (v1.y) - (v2.y) * (v1.z));
    grad[dim * c.idx3 + 1] += preFactor * ((v2.x) * (v1.z) - (v2.z) * (v1.x));
    grad[dim * c.idx3 + 2] += preFactor * ((v2.y) * (v1.x) - (v2.x) * (v1.y));

    grad[dim * c.idx4] +=
        preFactor * (pos[c.idx1 * dim + 2] *
                         (pos[c.idx2 * dim + 1] - pos[c.idx3 * dim + 1]) +
                     pos[c.idx2 * dim + 2] *
                         (pos[c.idx3 * dim + 1] - pos[c.idx1 * dim + 1]) +
                     pos[c.idx3 * dim + 2] *
                         (pos[c.idx1 * dim + 1] - pos[c.idx2 * dim + 1]));

    grad[dim * c.idx4 + 1] +=
        preFactor *
        (pos[c.idx1 * dim] * (pos[c.idx2 * dim + 2] - pos[c.idx3 * dim + 2]) +
         pos[c.idx2 * dim] * (pos[c.idx3 * dim + 2] - pos[c.idx1 * dim + 2]) +
         pos[c.idx3 * dim] * (pos[c.idx1 * dim + 2] - pos[c.idx2 * dim + 2]));

    grad[dim * c.idx4 + 2] +=
        preFactor *
        (pos[c.idx1 * dim + 1] * (pos[c.idx2 * dim] - pos[c.idx3 * dim]) +
         pos[c.idx2 * dim + 1] * (pos[c.idx3 * dim] - pos[c.idx1 * dim]) +
         pos[c.idx3 * dim + 1] * (pos[c.idx1 * dim] - pos[c.idx2 * dim]));
  }
}
}  // namespace DistGeom