| 1 | /* | 
| 2 | * Copyright (c) 2005 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. Acknowledgement of the program authors must be made in any | 
| 10 | *    publication of scientific results based in part on use of the | 
| 11 | *    program.  An acceptable form of acknowledgement is citation of | 
| 12 | *    the article in which the program was described (Matthew | 
| 13 | *    A. Meineke, Charles F. Vardeman II, Teng Lin, Christopher | 
| 14 | *    J. Fennell and J. Daniel Gezelter, "OOPSE: An Object-Oriented | 
| 15 | *    Parallel Simulation Engine for Molecular Dynamics," | 
| 16 | *    J. Comput. Chem. 26, pp. 252-271 (2005)) | 
| 17 | * | 
| 18 | * 2. Redistributions of source code must retain the above copyright | 
| 19 | *    notice, this list of conditions and the following disclaimer. | 
| 20 | * | 
| 21 | * 3. Redistributions in binary form must reproduce the above copyright | 
| 22 | *    notice, this list of conditions and the following disclaimer in the | 
| 23 | *    documentation and/or other materials provided with the | 
| 24 | *    distribution. | 
| 25 | * | 
| 26 | * This software is provided "AS IS," without a warranty of any | 
| 27 | * kind. All express or implied conditions, representations and | 
| 28 | * warranties, including any implied warranty of merchantability, | 
| 29 | * fitness for a particular purpose or non-infringement, are hereby | 
| 30 | * excluded.  The University of Notre Dame and its licensors shall not | 
| 31 | * be liable for any damages suffered by licensee as a result of | 
| 32 | * using, modifying or distributing the software or its | 
| 33 | * derivatives. In no event will the University of Notre Dame or its | 
| 34 | * licensors be liable for any lost revenue, profit or data, or for | 
| 35 | * direct, indirect, special, consequential, incidental or punitive | 
| 36 | * damages, however caused and regardless of the theory of liability, | 
| 37 | * arising out of the use of or inability to use software, even if the | 
| 38 | * University of Notre Dame has been advised of the possibility of | 
| 39 | * such damages. | 
| 40 | */ | 
| 41 |  | 
| 42 | #include <iostream> | 
| 43 |  | 
| 44 | #include <cstdlib> | 
| 45 | #include <cmath> | 
| 46 |  | 
| 47 | #include "utils/simError.h" | 
| 48 | #include "utils/MoLocator.hpp" | 
| 49 | #include "types/AtomType.hpp" | 
| 50 | namespace oopse { | 
| 51 | MoLocator::MoLocator( MoleculeStamp* theStamp, ForceField* theFF){ | 
| 52 |  | 
| 53 | myStamp = theStamp; | 
| 54 | myFF = theFF; | 
| 55 | nIntegrableObjects = myStamp->getNIntegrable(); | 
| 56 | calcRef(); | 
| 57 | } | 
| 58 |  | 
| 59 | void MoLocator::placeMol( const Vector3d& offset, const Vector3d& ort, Molecule* mol){ | 
| 60 | Vector3d newCoor; | 
| 61 | Vector3d curRefCoor; | 
| 62 | RotMat3x3d rotMat = latVec2RotMat(ort); | 
| 63 |  | 
| 64 | if(mol->getNIntegrableObjects() != nIntegrableObjects){ | 
| 65 | sprintf( painCave.errMsg, | 
| 66 | "MoLocator error.\n" | 
| 67 | "  The number of integrable objects of MoleculeStamp is not the same as  that of Molecule\n"); | 
| 68 | painCave.isFatal = 1; | 
| 69 | simError(); | 
| 70 | } | 
| 71 |  | 
| 72 | Molecule::IntegrableObjectIterator ii; | 
| 73 | StuntDouble* integrableObject; | 
| 74 | int i; | 
| 75 | for (integrableObject = mol->beginIntegrableObject(ii), i = 0; integrableObject != NULL; | 
| 76 | integrableObject = mol->nextIntegrableObject(ii), ++i) { | 
| 77 |  | 
| 78 | newCoor = rotMat * refCoords[i]; | 
| 79 | newCoor += offset; | 
| 80 |  | 
| 81 | integrableObject->setPos( newCoor); | 
| 82 | integrableObject->setVel(V3Zero); | 
| 83 |  | 
| 84 | if(integrableObject->isDirectional()){ | 
| 85 | integrableObject->setA(rotMat * integrableObject->getA()); | 
| 86 | integrableObject->setJ(V3Zero); | 
| 87 | } | 
| 88 | } | 
| 89 | } | 
| 90 |  | 
| 91 | void MoLocator::calcRef( void ){ | 
| 92 | AtomStamp* currAtomStamp; | 
| 93 | int nAtoms; | 
| 94 | int nRigidBodies; | 
| 95 | std::vector<double> mass; | 
| 96 | Vector3d coor; | 
| 97 | Vector3d refMolCom; | 
| 98 | int nAtomsInRb; | 
| 99 | double totMassInRb; | 
| 100 | double currAtomMass; | 
| 101 | double molMass; | 
| 102 |  | 
| 103 | nAtoms= myStamp->getNAtoms(); | 
| 104 | nRigidBodies = myStamp->getNRigidBodies(); | 
| 105 |  | 
| 106 | for(size_t i=0; i<nAtoms; i++){ | 
| 107 |  | 
| 108 | currAtomStamp = myStamp->getAtom(i); | 
| 109 |  | 
| 110 | if( !currAtomStamp->havePosition() ){ | 
| 111 | sprintf( painCave.errMsg, | 
| 112 | "MoLocator error.\n" | 
| 113 | "  Component %s, atom %s does not have a position specified.\n" | 
| 114 | "  This means MoLocator cannot initalize it's position.\n", | 
| 115 | myStamp->getID(), | 
| 116 | currAtomStamp->getType() ); | 
| 117 |  | 
| 118 | painCave.isFatal = 1; | 
| 119 | simError(); | 
| 120 | } | 
| 121 |  | 
| 122 | //if atom belongs to rigidbody, just skip it | 
| 123 | if(myStamp->isAtomInRigidBody(i)) | 
| 124 | continue; | 
| 125 | //get mass and the reference coordinate | 
| 126 | else{ | 
| 127 | currAtomMass = getAtomMass(currAtomStamp->getType(), myFF); | 
| 128 | mass.push_back(currAtomMass); | 
| 129 | coor.x() = currAtomStamp->getPosX(); | 
| 130 | coor.y() = currAtomStamp->getPosY(); | 
| 131 | coor.z() = currAtomStamp->getPosZ(); | 
| 132 | refCoords.push_back(coor); | 
| 133 |  | 
| 134 | } | 
| 135 | } | 
| 136 |  | 
| 137 | for(int i = 0; i < nRigidBodies; i++){ | 
| 138 | coor.x() = 0; | 
| 139 | coor.y() = 0; | 
| 140 | coor.z() = 0; | 
| 141 | totMassInRb = 0; | 
| 142 |  | 
| 143 | for(int j = 0; j < nAtomsInRb; j++){ | 
| 144 |  | 
| 145 | currAtomMass = getAtomMass(currAtomStamp->getType(), myFF); | 
| 146 | totMassInRb +=  currAtomMass; | 
| 147 |  | 
| 148 | coor.x() += currAtomStamp->getPosX() * currAtomMass; | 
| 149 | coor.y() += currAtomStamp->getPosY() * currAtomMass; | 
| 150 | coor.z() += currAtomStamp->getPosZ() * currAtomMass; | 
| 151 | } | 
| 152 |  | 
| 153 | mass.push_back(totMassInRb); | 
| 154 | coor /= totMassInRb; | 
| 155 | refCoords.push_back(coor); | 
| 156 | } | 
| 157 |  | 
| 158 |  | 
| 159 | //calculate the reference center of mass | 
| 160 | molMass = 0; | 
| 161 | refMolCom.x() = 0; | 
| 162 | refMolCom.y() = 0; | 
| 163 | refMolCom.z() = 0; | 
| 164 |  | 
| 165 | for(int i = 0; i < nIntegrableObjects; i++){ | 
| 166 | refMolCom += refCoords[i] * mass[i]; | 
| 167 | molMass += mass[i]; | 
| 168 | } | 
| 169 |  | 
| 170 | refMolCom /= molMass; | 
| 171 |  | 
| 172 | //move the reference center of mass to (0,0,0) and adjust the reference coordinate | 
| 173 | //of the integrabel objects | 
| 174 | for(int i = 0; i < nIntegrableObjects; i++) | 
| 175 | refCoords[i] -= refMolCom; | 
| 176 | } | 
| 177 |  | 
| 178 |  | 
| 179 |  | 
| 180 | double getAtomMass(const std::string& at, ForceField* myFF) { | 
| 181 | double mass; | 
| 182 | AtomType* atomType= myFF->getAtomType(at); | 
| 183 | if (atomType != NULL) { | 
| 184 | mass =     atomType->getMass(); | 
| 185 | } else { | 
| 186 | mass = 0.0; | 
| 187 | std::cerr << "Can not find AtomType: " << at << std::endl; | 
| 188 | } | 
| 189 | return mass; | 
| 190 | } | 
| 191 |  | 
| 192 | double getMolMass(MoleculeStamp *molStamp, ForceField *myFF) { | 
| 193 | int nAtoms; | 
| 194 | double totMass = 0; | 
| 195 | nAtoms = molStamp->getNAtoms(); | 
| 196 |  | 
| 197 | for(size_t i = 0; i < nAtoms; i++) { | 
| 198 | AtomStamp *currAtomStamp = molStamp->getAtom(i); | 
| 199 | totMass += getAtomMass(currAtomStamp->getType(), myFF); | 
| 200 | } | 
| 201 | return totMass; | 
| 202 | } | 
| 203 | RotMat3x3d latVec2RotMat(const Vector3d& lv){ | 
| 204 |  | 
| 205 | double theta =acos(lv[2]); | 
| 206 | double phi = atan2(lv[1], lv[0]); | 
| 207 | double psi = 0; | 
| 208 |  | 
| 209 | return RotMat3x3d(phi, theta, psi); | 
| 210 |  | 
| 211 | } | 
| 212 | } | 
| 213 |  |