| 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 | /** | 
| 43 | * @file ForceManager.cpp | 
| 44 | * @author tlin | 
| 45 | * @date 11/09/2004 | 
| 46 | * @time 10:39am | 
| 47 | * @version 1.0 | 
| 48 | */ | 
| 49 |  | 
| 50 | #include "brains/ForceManager.hpp" | 
| 51 | #include "primitives/Molecule.hpp" | 
| 52 | #include "UseTheForce/doForces_interface.h" | 
| 53 | #define __C | 
| 54 | #include "UseTheForce/DarkSide/fInteractionMap.h" | 
| 55 | #include "utils/simError.h" | 
| 56 | #include "primitives/Bond.hpp" | 
| 57 | #include "primitives/Bend.hpp" | 
| 58 | namespace oopse { | 
| 59 |  | 
| 60 | void ForceManager::calcForces(bool needPotential, bool needStress) { | 
| 61 |  | 
| 62 | if (!info_->isFortranInitialized()) { | 
| 63 | info_->update(); | 
| 64 | } | 
| 65 |  | 
| 66 | preCalculation(); | 
| 67 |  | 
| 68 | calcShortRangeInteraction(); | 
| 69 |  | 
| 70 | calcLongRangeInteraction(needPotential, needStress); | 
| 71 |  | 
| 72 | postCalculation(needStress); | 
| 73 |  | 
| 74 | } | 
| 75 |  | 
| 76 | void ForceManager::preCalculation() { | 
| 77 | SimInfo::MoleculeIterator mi; | 
| 78 | Molecule* mol; | 
| 79 | Molecule::AtomIterator ai; | 
| 80 | Atom* atom; | 
| 81 | Molecule::RigidBodyIterator rbIter; | 
| 82 | RigidBody* rb; | 
| 83 |  | 
| 84 | // forces are zeroed here, before any are accumulated. | 
| 85 | // NOTE: do not rezero the forces in Fortran. | 
| 86 |  | 
| 87 | for (mol = info_->beginMolecule(mi); mol != NULL; | 
| 88 | mol = info_->nextMolecule(mi)) { | 
| 89 | for(atom = mol->beginAtom(ai); atom != NULL; atom = mol->nextAtom(ai)) { | 
| 90 | atom->zeroForcesAndTorques(); | 
| 91 | } | 
| 92 |  | 
| 93 | //change the positions of atoms which belong to the rigidbodies | 
| 94 | for (rb = mol->beginRigidBody(rbIter); rb != NULL; | 
| 95 | rb = mol->nextRigidBody(rbIter)) { | 
| 96 | rb->zeroForcesAndTorques(); | 
| 97 | } | 
| 98 |  | 
| 99 | } | 
| 100 |  | 
| 101 | // Zero out the stress tensor | 
| 102 | tau *= 0.0; | 
| 103 |  | 
| 104 | } | 
| 105 |  | 
| 106 | void ForceManager::calcShortRangeInteraction() { | 
| 107 | Molecule* mol; | 
| 108 | RigidBody* rb; | 
| 109 | Bond* bond; | 
| 110 | Bend* bend; | 
| 111 | Torsion* torsion; | 
| 112 | SimInfo::MoleculeIterator mi; | 
| 113 | Molecule::RigidBodyIterator rbIter; | 
| 114 | Molecule::BondIterator bondIter;; | 
| 115 | Molecule::BendIterator  bendIter; | 
| 116 | Molecule::TorsionIterator  torsionIter; | 
| 117 | RealType bondPotential = 0.0; | 
| 118 | RealType bendPotential = 0.0; | 
| 119 | RealType torsionPotential = 0.0; | 
| 120 |  | 
| 121 | //calculate short range interactions | 
| 122 | for (mol = info_->beginMolecule(mi); mol != NULL; | 
| 123 | mol = info_->nextMolecule(mi)) { | 
| 124 |  | 
| 125 | //change the positions of atoms which belong to the rigidbodies | 
| 126 | for (rb = mol->beginRigidBody(rbIter); rb != NULL; | 
| 127 | rb = mol->nextRigidBody(rbIter)) { | 
| 128 | rb->updateAtoms(); | 
| 129 | } | 
| 130 |  | 
| 131 | for (bond = mol->beginBond(bondIter); bond != NULL; | 
| 132 | bond = mol->nextBond(bondIter)) { | 
| 133 | bond->calcForce(); | 
| 134 | bondPotential += bond->getPotential(); | 
| 135 | } | 
| 136 |  | 
| 137 | for (bend = mol->beginBend(bendIter); bend != NULL; | 
| 138 | bend = mol->nextBend(bendIter)) { | 
| 139 |  | 
| 140 | RealType angle; | 
| 141 | bend->calcForce(angle); | 
| 142 | RealType currBendPot = bend->getPotential(); | 
| 143 | bendPotential += bend->getPotential(); | 
| 144 | std::map<Bend*, BendDataSet>::iterator i = bendDataSets.find(bend); | 
| 145 | if (i == bendDataSets.end()) { | 
| 146 | BendDataSet dataSet; | 
| 147 | dataSet.prev.angle = dataSet.curr.angle = angle; | 
| 148 | dataSet.prev.potential = dataSet.curr.potential = currBendPot; | 
| 149 | dataSet.deltaV = 0.0; | 
| 150 | bendDataSets.insert(std::map<Bend*, BendDataSet>::value_type(bend, dataSet)); | 
| 151 | }else { | 
| 152 | i->second.prev.angle = i->second.curr.angle; | 
| 153 | i->second.prev.potential = i->second.curr.potential; | 
| 154 | i->second.curr.angle = angle; | 
| 155 | i->second.curr.potential = currBendPot; | 
| 156 | i->second.deltaV =  fabs(i->second.curr.potential - | 
| 157 | i->second.prev.potential); | 
| 158 | } | 
| 159 | } | 
| 160 |  | 
| 161 | for (torsion = mol->beginTorsion(torsionIter); torsion != NULL; | 
| 162 | torsion = mol->nextTorsion(torsionIter)) { | 
| 163 | RealType angle; | 
| 164 | torsion->calcForce(angle); | 
| 165 | RealType currTorsionPot = torsion->getPotential(); | 
| 166 | torsionPotential += torsion->getPotential(); | 
| 167 | std::map<Torsion*, TorsionDataSet>::iterator i = torsionDataSets.find(torsion); | 
| 168 | if (i == torsionDataSets.end()) { | 
| 169 | TorsionDataSet dataSet; | 
| 170 | dataSet.prev.angle = dataSet.curr.angle = angle; | 
| 171 | dataSet.prev.potential = dataSet.curr.potential = currTorsionPot; | 
| 172 | dataSet.deltaV = 0.0; | 
| 173 | torsionDataSets.insert(std::map<Torsion*, TorsionDataSet>::value_type(torsion, dataSet)); | 
| 174 | }else { | 
| 175 | i->second.prev.angle = i->second.curr.angle; | 
| 176 | i->second.prev.potential = i->second.curr.potential; | 
| 177 | i->second.curr.angle = angle; | 
| 178 | i->second.curr.potential = currTorsionPot; | 
| 179 | i->second.deltaV =  fabs(i->second.curr.potential - | 
| 180 | i->second.prev.potential); | 
| 181 | } | 
| 182 | } | 
| 183 | } | 
| 184 |  | 
| 185 | RealType  shortRangePotential = bondPotential + bendPotential + | 
| 186 | torsionPotential; | 
| 187 | Snapshot* curSnapshot = info_->getSnapshotManager()->getCurrentSnapshot(); | 
| 188 | curSnapshot->statData[Stats::SHORT_RANGE_POTENTIAL] = shortRangePotential; | 
| 189 | curSnapshot->statData[Stats::BOND_POTENTIAL] = bondPotential; | 
| 190 | curSnapshot->statData[Stats::BEND_POTENTIAL] = bendPotential; | 
| 191 | curSnapshot->statData[Stats::DIHEDRAL_POTENTIAL] = torsionPotential; | 
| 192 |  | 
| 193 | } | 
| 194 |  | 
| 195 | void ForceManager::calcLongRangeInteraction(bool needPotential, | 
| 196 | bool needStress) { | 
| 197 | Snapshot* curSnapshot; | 
| 198 | DataStorage* config; | 
| 199 | RealType* frc; | 
| 200 | RealType* pos; | 
| 201 | RealType* trq; | 
| 202 | RealType* A; | 
| 203 | RealType* electroFrame; | 
| 204 | RealType* rc; | 
| 205 | RealType* particlePot; | 
| 206 |  | 
| 207 | //get current snapshot from SimInfo | 
| 208 | curSnapshot = info_->getSnapshotManager()->getCurrentSnapshot(); | 
| 209 |  | 
| 210 | //get array pointers | 
| 211 | config = &(curSnapshot->atomData); | 
| 212 | frc = config->getArrayPointer(DataStorage::dslForce); | 
| 213 | pos = config->getArrayPointer(DataStorage::dslPosition); | 
| 214 | trq = config->getArrayPointer(DataStorage::dslTorque); | 
| 215 | A   = config->getArrayPointer(DataStorage::dslAmat); | 
| 216 | electroFrame = config->getArrayPointer(DataStorage::dslElectroFrame); | 
| 217 | particlePot = config->getArrayPointer(DataStorage::dslParticlePot); | 
| 218 |  | 
| 219 | //calculate the center of mass of cutoff group | 
| 220 | SimInfo::MoleculeIterator mi; | 
| 221 | Molecule* mol; | 
| 222 | Molecule::CutoffGroupIterator ci; | 
| 223 | CutoffGroup* cg; | 
| 224 | Vector3d com; | 
| 225 | std::vector<Vector3d> rcGroup; | 
| 226 |  | 
| 227 | if(info_->getNCutoffGroups() > 0){ | 
| 228 |  | 
| 229 | for (mol = info_->beginMolecule(mi); mol != NULL; | 
| 230 | mol = info_->nextMolecule(mi)) { | 
| 231 | for(cg = mol->beginCutoffGroup(ci); cg != NULL; | 
| 232 | cg = mol->nextCutoffGroup(ci)) { | 
| 233 | cg->getCOM(com); | 
| 234 | rcGroup.push_back(com); | 
| 235 | } | 
| 236 | }// end for (mol) | 
| 237 |  | 
| 238 | rc = rcGroup[0].getArrayPointer(); | 
| 239 | } else { | 
| 240 | // center of mass of the group is the same as position of the atom | 
| 241 | // if cutoff group does not exist | 
| 242 | rc = pos; | 
| 243 | } | 
| 244 |  | 
| 245 | //initialize data before passing to fortran | 
| 246 | RealType longRangePotential[LR_POT_TYPES]; | 
| 247 | RealType lrPot = 0.0; | 
| 248 | Vector3d totalDipole; | 
| 249 | short int passedCalcPot = needPotential; | 
| 250 | short int passedCalcStress = needStress; | 
| 251 | int isError = 0; | 
| 252 |  | 
| 253 | for (int i=0; i<LR_POT_TYPES;i++){ | 
| 254 | longRangePotential[i]=0.0; //Initialize array | 
| 255 | } | 
| 256 |  | 
| 257 | doForceLoop(pos, | 
| 258 | rc, | 
| 259 | A, | 
| 260 | electroFrame, | 
| 261 | frc, | 
| 262 | trq, | 
| 263 | tau.getArrayPointer(), | 
| 264 | longRangePotential, | 
| 265 | particlePot, | 
| 266 | &passedCalcPot, | 
| 267 | &passedCalcStress, | 
| 268 | &isError ); | 
| 269 |  | 
| 270 | if( isError ){ | 
| 271 | sprintf( painCave.errMsg, | 
| 272 | "Error returned from the fortran force calculation.\n" ); | 
| 273 | painCave.isFatal = 1; | 
| 274 | simError(); | 
| 275 | } | 
| 276 | for (int i=0; i<LR_POT_TYPES;i++){ | 
| 277 | lrPot += longRangePotential[i]; //Quick hack | 
| 278 | } | 
| 279 |  | 
| 280 | // grab the simulation box dipole moment if specified | 
| 281 | if (info_->getCalcBoxDipole()){ | 
| 282 | getAccumulatedBoxDipole(totalDipole.getArrayPointer()); | 
| 283 |  | 
| 284 | curSnapshot->statData[Stats::BOX_DIPOLE_X] = totalDipole(0); | 
| 285 | curSnapshot->statData[Stats::BOX_DIPOLE_Y] = totalDipole(1); | 
| 286 | curSnapshot->statData[Stats::BOX_DIPOLE_Z] = totalDipole(2); | 
| 287 | } | 
| 288 |  | 
| 289 | //store the tau and long range potential | 
| 290 | curSnapshot->statData[Stats::LONG_RANGE_POTENTIAL] = lrPot; | 
| 291 | curSnapshot->statData[Stats::VANDERWAALS_POTENTIAL] = longRangePotential[VDW_POT]; | 
| 292 | curSnapshot->statData[Stats::ELECTROSTATIC_POTENTIAL] = longRangePotential[ELECTROSTATIC_POT]; | 
| 293 | } | 
| 294 |  | 
| 295 |  | 
| 296 | void ForceManager::postCalculation(bool needStress) { | 
| 297 | SimInfo::MoleculeIterator mi; | 
| 298 | Molecule* mol; | 
| 299 | Molecule::RigidBodyIterator rbIter; | 
| 300 | RigidBody* rb; | 
| 301 | Snapshot* curSnapshot = info_->getSnapshotManager()->getCurrentSnapshot(); | 
| 302 |  | 
| 303 | // collect the atomic forces onto rigid bodies | 
| 304 |  | 
| 305 | for (mol = info_->beginMolecule(mi); mol != NULL; | 
| 306 | mol = info_->nextMolecule(mi)) { | 
| 307 | for (rb = mol->beginRigidBody(rbIter); rb != NULL; | 
| 308 | rb = mol->nextRigidBody(rbIter)) { | 
| 309 | if (needStress) { | 
| 310 | Mat3x3d rbTau = rb->calcForcesAndTorquesAndVirial(); | 
| 311 | tau += rbTau; | 
| 312 | } else{ | 
| 313 | rb->calcForcesAndTorques(); | 
| 314 | } | 
| 315 | } | 
| 316 | } | 
| 317 |  | 
| 318 | if (needStress) { | 
| 319 | #ifdef IS_MPI | 
| 320 | Mat3x3d tmpTau(tau); | 
| 321 | MPI_Allreduce(tmpTau.getArrayPointer(), tau.getArrayPointer(), | 
| 322 | 9, MPI_REALTYPE, MPI_SUM, MPI_COMM_WORLD); | 
| 323 | #endif | 
| 324 | curSnapshot->statData.setTau(tau); | 
| 325 | } | 
| 326 | } | 
| 327 |  | 
| 328 | } //end namespace oopse |