| 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, 234107 (2008). | 
| 39 | * [4]  Kuang & Gezelter,  J. Chem. Phys. 133, 164101 (2010). | 
| 40 | * [5]  Vardeman, Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011). | 
| 41 | */ | 
| 42 |  | 
| 43 | #include "restraints/MolecularRestraint.hpp" | 
| 44 | #include "math/SquareMatrix3.hpp" | 
| 45 | #include "math/SVD.hpp" | 
| 46 | #include <utility> | 
| 47 |  | 
| 48 | //using namespace JAMA; | 
| 49 |  | 
| 50 | namespace OpenMD { | 
| 51 |  | 
| 52 |  | 
| 53 | void MolecularRestraint::calcForce(std::vector<Vector3d> struc, | 
| 54 | Vector3d molCom){ | 
| 55 |  | 
| 56 | assert(struc.size() == ref_.size()); | 
| 57 |  | 
| 58 | std::vector<Vector3d>::iterator it; | 
| 59 |  | 
| 60 | // clear out initial values: | 
| 61 | pot_ = 0.0; | 
| 62 | for(it = forces_.begin(); it != forces_.end(); ++it) | 
| 63 | (*it) = 0.0; | 
| 64 |  | 
| 65 |  | 
| 66 | if (restType_ & rtDisplacement) { | 
| 67 | Vector3d del = molCom - refCom_; | 
| 68 |  | 
| 69 | RealType r = del.length(); | 
| 70 | RealType p = 0.5 * kDisp_ * r * r; | 
| 71 |  | 
| 72 | pot_ += p; | 
| 73 |  | 
| 74 | restInfo_[rtDisplacement] = std::make_pair(r, p); | 
| 75 |  | 
| 76 | for(it = forces_.begin(); it != forces_.end(); ++it) | 
| 77 | (*it) = -kDisp_ * del * scaleFactor_; | 
| 78 | } | 
| 79 |  | 
| 80 | for(it = struc.begin(); it != struc.end(); ++it) | 
| 81 | (*it) -= molCom; | 
| 82 |  | 
| 83 | // rtDisplacement = 1, so anything higher than that requires orientations: | 
| 84 | if (restType_ > 1) { | 
| 85 | Vector3d tBody(0.0); | 
| 86 |  | 
| 87 | Mat3x3d R(0.0); | 
| 88 |  | 
| 89 | for (unsigned int n = 0; n < struc.size(); n++){ | 
| 90 |  | 
| 91 | /* | 
| 92 | * correlation matrix R: | 
| 93 | *   R(i,j) = sum(over n): y(n,i) * x(n,j) | 
| 94 | *   where x(n) and y(n) are two vector sets | 
| 95 | */ | 
| 96 |  | 
| 97 | R += outProduct(struc[n], ref_[n]); | 
| 98 | } | 
| 99 |  | 
| 100 | // SVD class uses dynamic matrices, so we must wrap the correlation | 
| 101 | // matrix before calling SVD and then unwrap the results into Mat3x3d | 
| 102 | // and Vector3d before we use them. | 
| 103 |  | 
| 104 | DynamicRectMatrix<RealType> Rtmp(3, 3, 0.0); | 
| 105 | DynamicRectMatrix<RealType> vtmp(3, 3); | 
| 106 | DynamicVector<RealType>     stmp(3); | 
| 107 | DynamicRectMatrix<RealType> wtmp(3, 3); | 
| 108 |  | 
| 109 | Rtmp.setSubMatrix(0, 0, R); | 
| 110 |  | 
| 111 | // Heavy lifting goes here: | 
| 112 |  | 
| 113 | JAMA::SVD<RealType> svd(Rtmp); | 
| 114 |  | 
| 115 | svd.getU(vtmp); | 
| 116 | svd.getSingularValues(stmp); | 
| 117 | svd.getV(wtmp); | 
| 118 |  | 
| 119 | Mat3x3d v; | 
| 120 | Vector3d s; | 
| 121 | Mat3x3d w_tr; | 
| 122 |  | 
| 123 | vtmp.getSubMatrix(0, 0, v); | 
| 124 | stmp.getSubVector(0, s); | 
| 125 | wtmp.getSubMatrix(0, 0, w_tr); | 
| 126 |  | 
| 127 | bool is_reflection = (v.determinant() * w_tr.determinant()) < 0.0; | 
| 128 |  | 
| 129 | if (is_reflection){ | 
| 130 | v(2, 0) = -v(2, 0); | 
| 131 | v(2, 1) = -v(2, 1); | 
| 132 | v(2, 2) = -v(2, 2); | 
| 133 | } | 
| 134 |  | 
| 135 | RotMat3x3d Atrans = v * w_tr.transpose(); | 
| 136 | RotMat3x3d A = Atrans.transpose(); | 
| 137 |  | 
| 138 | Vector3d eularAngles = A.toEulerAngles(); | 
| 139 |  | 
| 140 |  | 
| 141 | RealType twistAngle; | 
| 142 | Vector3d swingAxis; | 
| 143 |  | 
| 144 | Quat4d quat = A.toQuaternion(); | 
| 145 |  | 
| 146 | RealType swingX, swingY; | 
| 147 | quat.toSwingTwist(swingX, swingY, twistAngle); | 
| 148 |  | 
| 149 | RealType dVdtwist, dVdswingX, dVdswingY; | 
| 150 | RealType dTwist, dSwingX, dSwingY; | 
| 151 | RealType p; | 
| 152 |  | 
| 153 | if (restType_ & rtTwist){ | 
| 154 | dTwist = twistAngle - twist0_; | 
| 155 | dVdtwist = kTwist_ * sin(dTwist) ; | 
| 156 | p = kTwist_ * (1.0 - cos(dTwist) ) ; | 
| 157 | pot_ += p; | 
| 158 | tBody -= dVdtwist * V3Z; | 
| 159 | restInfo_[rtTwist] = std::make_pair(twistAngle, p); | 
| 160 | } | 
| 161 |  | 
| 162 | //       if (restType_ & rtSwing){ | 
| 163 | //         dSwing = swingAngle - swing0_; | 
| 164 | //         dVdswing = kSwing_ * 2.0 * sin(2.0 * dSwing); | 
| 165 | //         p = kSwing_ * (1.0 - cos(2.0 * dSwing)); | 
| 166 | //         pot_ += p; | 
| 167 | //         tBody -= dVdswing * swingAxis; | 
| 168 | //         restInfo_[rtSwing] = std::make_pair(swingAngle, p); | 
| 169 | //       } | 
| 170 |  | 
| 171 | if (restType_ & rtSwingX){ | 
| 172 | dSwingX = swingX - swingX0_; | 
| 173 | dVdswingX = kSwingX_ * 2.0 * sin(2.0 * dSwingX); | 
| 174 | p = kSwingX_ * (1.0 - cos(2.0 * dSwingX)); | 
| 175 | pot_ += p; | 
| 176 | tBody -= dVdswingX * V3X; | 
| 177 | restInfo_[rtSwingX] = std::make_pair(swingX, p); | 
| 178 | } | 
| 179 | if (restType_ & rtSwingY){ | 
| 180 | dSwingY = swingY - swingY0_; | 
| 181 | dVdswingY = kSwingY_ * 2.0 * sin(2.0 * dSwingY); | 
| 182 | p = kSwingY_ * (1.0 - cos(2.0 * dSwingY)); | 
| 183 | pot_ += p; | 
| 184 | tBody -= dVdswingY * V3Y; | 
| 185 | restInfo_[rtSwingY] = std::make_pair(swingY, p); | 
| 186 | } | 
| 187 |  | 
| 188 |  | 
| 189 | RealType t2 = dot(tBody, tBody); | 
| 190 |  | 
| 191 | Vector3d rLab, rBody, txr, fBody, fLab; | 
| 192 |  | 
| 193 | for (unsigned int i = 0; i < struc.size(); i++) { | 
| 194 |  | 
| 195 | rLab = struc[i]; | 
| 196 | rBody = A * rLab; | 
| 197 |  | 
| 198 | txr = cross(tBody, rBody); | 
| 199 | fBody = txr * t2; | 
| 200 | fLab = Atrans * fBody; | 
| 201 | fLab *= scaleFactor_; | 
| 202 |  | 
| 203 | forces_[i] += fLab; | 
| 204 | } | 
| 205 |  | 
| 206 | // test the force vectors and see if it is the right orientation | 
| 207 | //       std::cout << struc.size() << std::endl << std::endl; | 
| 208 | //       for (int i = 0; i != struc.size(); ++i){ | 
| 209 | //         std::cout << "H\t" << struc[i].x() << "\t" << struc[i].y() << "\t" << struc[i].z() << "\t"; | 
| 210 | //         std::cout << forces_[i].x() << "\t" << forces_[i].y() << "\t" << forces_[i].z() << std::endl; | 
| 211 | //       } | 
| 212 | } | 
| 213 | } | 
| 214 | } |