File: DistanceConstraint.cpp

package info (click to toggle)
rdkit 202503.1-4
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • 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: 578; xml: 229; fortran: 183; sh: 105
file content (97 lines) | stat: -rw-r--r-- 2,978 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
//
//  Copyright (C) 2013-2024 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 "ForceField.h"
#include "DistanceConstraint.h"
#include <RDGeneral/Invariant.h>
#include <cmath>

namespace ForceFields {
DistanceConstraintContrib::DistanceConstraintContrib(
    ForceField *owner, unsigned int idx1, unsigned int idx2, double minLen,
    double maxLen, double forceConst) {
  PRECONDITION(owner, "bad owner");
  URANGE_CHECK(idx1, owner->positions().size());
  URANGE_CHECK(idx2, owner->positions().size());
  PRECONDITION(maxLen >= minLen, "bad bounds");

  dp_forceField = owner;
  d_end1Idx = idx1;
  d_end2Idx = idx2;
  d_minLen = minLen;
  d_maxLen = maxLen;
  d_forceConstant = forceConst;
}

DistanceConstraintContrib::DistanceConstraintContrib(
    ForceField *owner, unsigned int idx1, unsigned int idx2, bool relative,
    double minLen, double maxLen, double forceConst) {
  PRECONDITION(owner, "bad owner");
  const RDGeom::PointPtrVect &pos = owner->positions();
  URANGE_CHECK(idx1, pos.size());
  URANGE_CHECK(idx2, pos.size());
  PRECONDITION(maxLen >= minLen, "bad bounds");
  if (relative) {
    RDGeom::Point3D &p1 = *((RDGeom::Point3D *)pos[idx1]);
    RDGeom::Point3D &p2 = *((RDGeom::Point3D *)pos[idx2]);
    const auto dist = (p1 - p2).length();
    minLen = std::max(dist + minLen, 0.0);
    maxLen = std::max(dist + maxLen, 0.0);
  }
  d_minLen = minLen;
  d_maxLen = maxLen;
  dp_forceField = owner;
  d_end1Idx = idx1;
  d_end2Idx = idx2;
  d_forceConstant = forceConst;
}

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

  double dist = dp_forceField->distance(d_end1Idx, d_end2Idx, pos);
  double distTerm = 0.0;
  if (dist < d_minLen) {
    distTerm = d_minLen - dist;
  } else if (dist > d_maxLen) {
    distTerm = dist - d_maxLen;
  }
  double res = 0.5 * d_forceConstant * distTerm * distTerm;

  return res;
}

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

  double dist = dp_forceField->distance(d_end1Idx, d_end2Idx, pos);

  double preFactor = 0.0;
  if (dist < d_minLen) {
    preFactor = dist - d_minLen;
  } else if (dist > d_maxLen) {
    preFactor = dist - d_maxLen;
  } else {
    return;
  }
  preFactor *= d_forceConstant;

  double *end1Coords = &(pos[3 * d_end1Idx]);
  double *end2Coords = &(pos[3 * d_end2Idx]);
  for (unsigned int i = 0; i < 3; ++i) {
    double dGrad =
        preFactor * (end1Coords[i] - end2Coords[i]) / std::max(dist, 1.0e-8);
    grad[3 * d_end1Idx + i] += dGrad;
    grad[3 * d_end2Idx + i] -= dGrad;
  }
}
}  // namespace ForceFields