File: PositionConstraint.cpp

package info (click to toggle)
rdkit 201403-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 62,288 kB
  • ctags: 15,156
  • sloc: cpp: 125,376; python: 55,674; java: 4,831; ansic: 4,178; xml: 2,499; sql: 1,775; yacc: 1,551; lex: 1,051; makefile: 353; fortran: 183; sh: 148; cs: 93
file content (73 lines) | stat: -rw-r--r-- 2,124 bytes parent folder | download
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
// $Id$
//
//  Copyright (C) 2013 Paolo Tosco
//
//  Copyright (C) 2004-2006 Rational Discovery LLC
//
//   @@ 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 "PositionConstraint.h"
#include <cmath>
#include <ForceField/ForceField.h>
#include <RDGeneral/Invariant.h>

namespace ForceFields {
  namespace MMFF {
    PositionConstraintContrib::PositionConstraintContrib(ForceField *owner,
      unsigned int idx, double maxDispl, double forceConst)
    {
      PRECONDITION(owner,"bad owner");
      const RDGeom::PointPtrVect &pos = owner->positions();
      RANGE_CHECK(0, idx, pos.size() - 1);

      dp_forceField = owner;
      d_atIdx = idx;
      d_maxDispl = maxDispl;
      d_pos0 = *((RDGeom::Point3D *)pos[idx]);
      d_forceConstant = forceConst;
    }

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

      RDGeom::Point3D p(pos[3 * d_atIdx],
			  pos[3 * d_atIdx + 1], pos[3 * d_atIdx + 2]);
      double dist = (p - d_pos0).length();
      double distTerm = (dist > d_maxDispl) ? dist - d_maxDispl : 0.0;
      double res = 0.5 * d_forceConstant * distTerm * distTerm;

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

      RDGeom::Point3D p(pos[3 * d_atIdx],
			  pos[3 * d_atIdx + 1], pos[3 * d_atIdx + 2]);
      double dist = (p - d_pos0).length();

      double preFactor = 0.0;
      if (dist > d_maxDispl) {
        preFactor = dist - d_maxDispl;
      }
      else {
        return;
      }
      preFactor *= d_forceConstant;
    
      for (unsigned int i = 0; i < 3; ++i) {
        double dGrad = preFactor * (p[i] - d_pos0[i]) / std::max(dist, 1.0e-8);
        grad[3 * d_atIdx + i] += dGrad;
      }    
    }
  }
}