ReferenceBondIxn.cpp 7.75 KB
Newer Older
1

2
/* Portions copyright (c) 2006-2016 Stanford University and Simbios.
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
 * Contributors: Pande Group
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject
 * to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS, CONTRIBUTORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#include <string.h>
#include <sstream>

28
#include "SimTKOpenMMUtilities.h"
29
30
31
#include "ReferenceForce.h"
#include "ReferenceBondIxn.h"

32
using std::vector;
33
using namespace OpenMM;
34

35
36
37
38
39
40
/**---------------------------------------------------------------------------------------

   ReferenceBondIxn constructor

   --------------------------------------------------------------------------------------- */

41
ReferenceBondIxn::ReferenceBondIxn() {
42
43
44
45
46
47
48
49
}

/**---------------------------------------------------------------------------------------

   ReferenceBondIxn destructor

   --------------------------------------------------------------------------------------- */

50
ReferenceBondIxn::~ReferenceBondIxn() {
51
52
53
54
55
56
57
58
59
60
}

/**---------------------------------------------------------------------------------------
      
   Calculate Bond Ixn -- virtual method -- does nothing
      
   @param atomIndices      bond indices
   @param atomCoordinates  atom coordinates
   @param parameters       parameters
   @param forces           force array (forces added)
61
   @param totalEnergy      if not null, the energy will be added to this
62
63
64
      
   --------------------------------------------------------------------------------------- */
     
peastman's avatar
peastman committed
65
66
67
   void ReferenceBondIxn::calculateBondIxn(int* atomIndices, vector<Vec3>& atomCoordinates,
                                           double* parameters, vector<Vec3>& forces,
                                           double* totalEnergy, double* energyParamDerivs) {
68
69
70
71
72
73
74
75
}
 
/**---------------------------------------------------------------------------------------

   Get normed dot product between two vectors

   Do computation in double?

76
77
   @param  vector1            first vector
   @param  vector2            second vector
78
79
80
81
82
83
84
   @param  hasREntry          if set, then vector1[ReferenceForce::RIndex] = norm of vector
                              defaults to 0 (i.e., R unavailable)

   @return dot product

   --------------------------------------------------------------------------------------- */

peastman's avatar
peastman committed
85
86
double ReferenceBondIxn::getNormedDotProduct(double* vector1, double* vector2,
                                             int hasREntry = 0) {
87

peastman's avatar
peastman committed
88
89
   double dotProduct = DOT3(vector1, vector2);
   if (dotProduct != 0.0) {
90
91
      if (hasREntry) {
         dotProduct       /= (vector1[ReferenceForce::RIndex]*vector2[ReferenceForce::RIndex]);
92
      } else {
peastman's avatar
peastman committed
93
94
95
         double norm1  = DOT3(vector1, vector1);
         double norm2  = DOT3(vector2, vector2);
         dotProduct   /= sqrt(norm1*norm2);
96
97
98
99
100
      }
   }
      
   // clamp dot product to [-1,1]

peastman's avatar
peastman committed
101
102
103
104
   if (dotProduct > 1.0) {
      dotProduct = 1.0;
   } else if (dotProduct < -1.0) {
      dotProduct = -1.0;
105
106
107
108
109
110
111
112
113
114
   }

   return dotProduct;

}

/**---------------------------------------------------------------------------------------

   Get angle between two vectors

115
116
   @param  vector1            first vector
   @param  vector2            second vector
117
118
119
120
121
122
123
124
   @param  outputDotProduct   output cosine of angle between two vectors (optional)
   @param  hasREntry          if set, then vector1[ReferenceForce::RIndex] = norm of vector
                              defaults to 0 -> R unavailable

   @return cosine of angles in radians

   --------------------------------------------------------------------------------------- */

peastman's avatar
peastman committed
125
126
127
double ReferenceBondIxn::getAngleBetweenTwoVectors(double* vector1, double* vector2, 
                                                   double* outputDotProduct = NULL,
                                                   int hasREntry = 0) {
128

peastman's avatar
peastman committed
129
    // get dot product betweenn vectors and then angle
130

peastman's avatar
peastman committed
131
   double dotProduct = getNormedDotProduct(vector1, vector2, hasREntry);
132

peastman's avatar
peastman committed
133
134
   double angle;
   if (dotProduct > 0.99 || dotProduct < -0.99) {
135
136
       // We're close to the singularity in acos(), so take the cross product and use asin() instead.

peastman's avatar
peastman committed
137
       double cross[3];
138
       SimTKOpenMMUtilities::crossProductVector3(vector1, vector2, cross);
peastman's avatar
peastman committed
139
140
141
142
       double scale = DOT3(vector1, vector1)*DOT3(vector2, vector2);
       angle = asin(sqrt(DOT3(cross, cross)/scale));
       if (dotProduct < 0.0)
           angle = M_PI-angle;
143
   } else {
peastman's avatar
peastman committed
144
      angle = acos(dotProduct);
145
146
   }

147
   if (outputDotProduct) {
148
149
150
151
152
153
154
155
156
157
158
      *outputDotProduct = dotProduct;
   }

   return angle;

}

/**---------------------------------------------------------------------------------------

   Get dihedral angle between three vectors

159
160
161
   @param  vector1            first vector
   @param  vector2            second vector
   @param  vector3            third vector
162
163
164
165
166
167
168
169
170
171
172
   @param  outputCrossProduct output cross product vectors
   @param  cosineOfAngle      cosine of angle (output)
   @param  signVector         vector to test sign (optional)
   @param  signOfAngle        sign of angle (output) (optional)
   @param  hasREntry          if set, then vector1[ReferenceForce::RIndex] = norm of vector
                              defaults to 0

   @return cosine of dihedral angle in radians

   --------------------------------------------------------------------------------------- */

peastman's avatar
peastman committed
173
174
175
176
177
178
179
180
double ReferenceBondIxn::getDihedralAngleBetweenThreeVectors(double*  vector1,
                                                             double*  vector2, 
                                                             double*  vector3, 
                                                             double** outputCrossProduct  = NULL, 
                                                             double*  cosineOfAngle       = NULL, 
                                                             double*  signVector          = NULL, 
                                                             double*  signOfAngle         = NULL, 
                                                             int          hasREntry = 0) {
181

peastman's avatar
peastman committed
182
   double   tempVectors[6]         = { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 };
183
184
185

   // get cross products between vectors and then angle between cross product vectors

peastman's avatar
peastman committed
186
   double* crossProduct[2];
187
   if (outputCrossProduct) {
188
189
190
191
192
193
194
      crossProduct[0] = outputCrossProduct[0];
      crossProduct[1] = outputCrossProduct[1];
   } else {
      crossProduct[0] = tempVectors;
      crossProduct[1] = tempVectors + 3;
   }
   
195
196
   SimTKOpenMMUtilities::crossProductVector3(vector1, vector2, crossProduct[0]);
   SimTKOpenMMUtilities::crossProductVector3(vector2, vector3, crossProduct[1]);
197

peastman's avatar
peastman committed
198
   double angle = getAngleBetweenTwoVectors(crossProduct[0], crossProduct[1], cosineOfAngle, 0);
199
200
201

   // take care of sign of angle

202
   if (signVector) {
peastman's avatar
peastman committed
203
204
      double dotProduct = DOT3(signVector, crossProduct[1]);
      double sign       = dotProduct < 0.0 ? -1.0 : 1.0; 
205
      if (signOfAngle) {
206
207
208
209
210
211
212
213
         *signOfAngle = sign;
      }
      angle *= sign;
   }

   return angle;

}