File: DistViolationContrib.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 (88 lines) | stat: -rw-r--r-- 2,627 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// $Id$
//
//  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 "DistViolationContrib.h"
#include <ForceField/ForceField.h>
#include <RDGeneral/Invariant.h>

namespace DistGeom {
  
  DistViolationContrib::DistViolationContrib(ForceFields::ForceField *owner,unsigned int idx1,
                                             unsigned int idx2,
                                             double ub, double lb, double weight) {
    PRECONDITION(owner,"bad owner");
    RANGE_CHECK(0,idx1,owner->positions().size()-1);
    RANGE_CHECK(0,idx2,owner->positions().size()-1);

    dp_forceField = owner;
    d_end1Idx = idx1;
    d_end2Idx = idx2;
    d_ub = ub;
    d_lb = lb;
    d_weight = weight;
  }

  double DistViolationContrib::getEnergy(double *pos) const {
    PRECONDITION(dp_forceField,"no owner");
    PRECONDITION(pos,"bad vector");
    
    double d = this->dp_forceField->distance(this->d_end1Idx,this->d_end2Idx,pos);
    double val=0.0;
    if (d > d_ub) {
      val = ((d*d)/(d_ub*d_ub)) - 1.0;
    } else if (d < d_lb) {
      val = ((2*d_lb*d_lb)/(d_lb*d_lb + d*d)) - 1.0;
    }
    double res;
    if(val>0.0){
      res=d_weight*val*val;
    }else{
      res=0;
    }
    return res;
  }

  void DistViolationContrib::getGrad(double *pos, double *grad) const {
    PRECONDITION(dp_forceField,"no owner");
    PRECONDITION(pos,"bad vector");
    PRECONDITION(grad,"bad vector");
    unsigned int dim=this->dp_forceField->dimension();
    double d = this->dp_forceField->distance(this->d_end1Idx,this->d_end2Idx,pos);
    double preFactor = 0.0;
    if (d > d_ub) {
      double u2 = d_ub*d_ub;
      preFactor = 4.*(((d*d)/u2) - 1.0)*(d/u2);
    } else if (d < d_lb) {
      double d2 = d*d;
      double l2 = d_lb*d_lb;
      double l2d2 = d2+l2;
      preFactor = 8.*l2*d * (1.-2*l2/l2d2) / (l2d2*l2d2);
      //preFactor = -8.*((l2-d2)/pow(l2+d2,3))*d*l2;
    } else {
      return;
    }

    double *end1Coords = &(pos[dim*this->d_end1Idx]);
    double *end2Coords = &(pos[dim*this->d_end2Idx]);

    for(unsigned int i=0;i<dim;i++){
      double dGrad;
      if(d>0.0){
        dGrad= d_weight*preFactor * (end1Coords[i]-end2Coords[i])/d;
      } else {
        // FIX: this likely isn't right
        dGrad= d_weight*preFactor * (end1Coords[i]-end2Coords[i]);
      }
      grad[dim*this->d_end1Idx+i] += dGrad;
      grad[dim*this->d_end2Idx+i] -= dGrad;
    }
  }
}