ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/OpenMD/trunk/src/restraints/MolecularRestraint.cpp
Revision: 1407
Committed: Wed Jan 20 16:04:40 2010 UTC (15 years, 3 months ago) by cli2
File size: 6972 byte(s)
Log Message:
Fixed bugs in Restraint, refactored RestReader and RestWriter

File Contents

# Content
1 /*
2 * Copyright (c) 2009 The University of Notre Dame. All Rights Reserved.
3 *
4 * The University of Notre Dame grants you ("Licensee") a
5 * non-exclusive, royalty free, license to use, modify and
6 * redistribute this software in source and binary code form, provided
7 * that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the
15 * distribution.
16 *
17 * This software is provided "AS IS," without a warranty of any
18 * kind. All express or implied conditions, representations and
19 * warranties, including any implied warranty of merchantability,
20 * fitness for a particular purpose or non-infringement, are hereby
21 * excluded. The University of Notre Dame and its licensors shall not
22 * be liable for any damages suffered by licensee as a result of
23 * using, modifying or distributing the software or its
24 * derivatives. In no event will the University of Notre Dame or its
25 * licensors be liable for any lost revenue, profit or data, or for
26 * direct, indirect, special, consequential, incidental or punitive
27 * damages, however caused and regardless of the theory of liability,
28 * arising out of the use of or inability to use software, even if the
29 * University of Notre Dame has been advised of the possibility of
30 * such damages.
31 *
32 * SUPPORT OPEN SCIENCE! If you use OpenMD or its source code in your
33 * research, please cite the appropriate papers when you publish your
34 * work. Good starting points are:
35 *
36 * [1] Meineke, et al., J. Comp. Chem. 26, 252-271 (2005).
37 * [2] Fennell & Gezelter, J. Chem. Phys. 124, 234104 (2006).
38 * [3] Sun, Lin & Gezelter, J. Chem. Phys. 128, 24107 (2008).
39 * [4] Vardeman & Gezelter, in progress (2009).
40 */
41
42 #include "restraints/MolecularRestraint.hpp"
43 #include "math/SquareMatrix3.hpp"
44 #include "math/SVD.hpp"
45 #include <utility>
46
47 //using namespace JAMA;
48
49 namespace OpenMD {
50
51
52 void MolecularRestraint::calcForce(std::vector<Vector3d> struc,
53 Vector3d molCom){
54
55 assert(struc.size() == ref_.size());
56
57 std::vector<Vector3d>::iterator it;
58
59 // clear out initial values:
60 pot_ = 0.0;
61 for(it = forces_.begin(); it != forces_.end(); ++it)
62 (*it) = 0.0;
63
64
65 if (restType_ & rtDisplacement) {
66 Vector3d del = molCom - refCom_;
67
68 RealType r = del.length();
69 RealType p = 0.5 * kDisp_ * r * r;
70
71 pot_ += p;
72
73 restInfo_[rtDisplacement] = std::make_pair(r, p);
74
75 for(it = forces_.begin(); it != forces_.end(); ++it)
76 (*it) = -kDisp_ * del * scaleFactor_;
77 }
78
79 for(it = struc.begin(); it != struc.end(); ++it)
80 (*it) -= molCom;
81
82 // rtDisplacement = 1, so anything higher than that requires orientations:
83 if (restType_ > 1) {
84 Vector3d tBody(0.0);
85
86 Mat3x3d R(0.0);
87
88 for (int n = 0; n < struc.size(); n++){
89
90 /*
91 * correlation matrix R:
92 * R(i,j) = sum(over n): y(n,i) * x(n,j)
93 * where x(n) and y(n) are two vector sets
94 */
95
96 R += outProduct(struc[n], ref_[n]);
97 }
98
99 // SVD class uses dynamic matrices, so we must wrap the correlation
100 // matrix before calling SVD and then unwrap the results into Mat3x3d
101 // and Vector3d before we use them.
102
103 DynamicRectMatrix<RealType> Rtmp(3, 3, 0.0);
104 DynamicRectMatrix<RealType> vtmp(3, 3);
105 DynamicVector<RealType> stmp(3);
106 DynamicRectMatrix<RealType> wtmp(3, 3);
107
108 Rtmp.setSubMatrix(0, 0, R);
109
110 // Heavy lifting goes here:
111
112 JAMA::SVD<RealType> svd(Rtmp);
113
114 svd.getU(vtmp);
115 svd.getSingularValues(stmp);
116 svd.getV(wtmp);
117
118 Mat3x3d v;
119 Vector3d s;
120 Mat3x3d w_tr;
121
122 vtmp.getSubMatrix(0, 0, v);
123 stmp.getSubVector(0, s);
124 wtmp.getSubMatrix(0, 0, w_tr);
125
126 bool is_reflection = (v.determinant() * w_tr.determinant()) < 0.0;
127
128 if (is_reflection){
129 v(2, 0) = -v(2, 0);
130 v(2, 1) = -v(2, 1);
131 v(2, 2) = -v(2, 2);
132 }
133
134 RotMat3x3d Atrans = v * w_tr.transpose();
135 RotMat3x3d A = Atrans.transpose();
136
137 Vector3d eularAngles = A.toEulerAngles();
138
139
140 RealType twistAngle, swingAngle;
141 Vector3d swingAxis;
142
143 Quat4d quat = A.toQuaternion();
144
145 RealType tw, sx, sy, ttw, swingX, swingY;
146 quat.toSwingTwist(swingX, swingY, twistAngle);
147
148 RealType dVdtwist, dVdswing, dVdswingX, dVdswingY;
149 RealType dTwist, dSwing, dSwingX, dSwingY;
150 RealType p;
151
152 if (restType_ & rtTwist){
153 dTwist = twistAngle - twist0_;
154 dVdtwist = kTwist_ * sin(dTwist) ;
155 p = kTwist_ * (1.0 - cos(dTwist) ) ;
156 pot_ += p;
157 tBody -= dVdtwist * V3Z;
158 restInfo_[rtTwist] = std::make_pair(twistAngle, p);
159 }
160
161 // if (restType_ & rtSwing){
162 // dSwing = swingAngle - swing0_;
163 // dVdswing = kSwing_ * 2.0 * sin(2.0 * dSwing);
164 // p = kSwing_ * (1.0 - cos(2.0 * dSwing));
165 // pot_ += p;
166 // tBody -= dVdswing * swingAxis;
167 // restInfo_[rtSwing] = std::make_pair(swingAngle, p);
168 // }
169
170 if (restType_ & rtSwingX){
171 dSwingX = swingX - swingX0_;
172 dVdswingX = kSwingX_ * 2.0 * sin(2.0 * dSwingX);
173 p = kSwingX_ * (1.0 - cos(2.0 * dSwingX));
174 pot_ += p;
175 tBody -= dVdswingX * V3X;
176 restInfo_[rtSwingX] = std::make_pair(swingX, p);
177 }
178 if (restType_ & rtSwingY){
179 dSwingY = swingY - swingY0_;
180 dVdswingY = kSwingY_ * 2.0 * sin(2.0 * dSwingY);
181 p = kSwingY_ * (1.0 - cos(2.0 * dSwingY));
182 pot_ += p;
183 tBody -= dVdswingY * V3Y;
184 restInfo_[rtSwingY] = std::make_pair(swingY, p);
185 }
186
187
188 RealType t2 = dot(tBody, tBody);
189
190 Vector3d rLab, rBody, txr, fBody, fLab;
191
192 for (int i = 0; i < struc.size(); i++) {
193
194 rLab = struc[i];
195 rBody = A * rLab;
196
197 txr = cross(tBody, rBody);
198 fBody = txr * t2;
199 fLab = Atrans * fBody;
200 fLab *= scaleFactor_;
201
202 forces_[i] += fLab;
203 }
204
205 // test the force vectors and see if it is the right orientation
206 // std::cout << struc.size() << std::endl << std::endl;
207 // for (int i = 0; i != struc.size(); ++i){
208 // std::cout << "H\t" << struc[i].x() << "\t" << struc[i].y() << "\t" << struc[i].z() << "\t";
209 // std::cout << forces_[i].x() << "\t" << forces_[i].y() << "\t" << forces_[i].z() << std::endl;
210 // }
211 }
212 }
213 }