| 1 | gezelter | 507 | /* | 
| 2 | gezelter | 246 | * 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 <cmath> | 
| 43 |  |  | #include "constraints/ZconstraintForceManager.hpp" | 
| 44 |  |  | #include "integrators/Integrator.hpp" | 
| 45 |  |  | #include "utils/simError.h" | 
| 46 |  |  | #include "utils/OOPSEConstant.hpp" | 
| 47 |  |  | #include "utils/StringUtils.hpp" | 
| 48 |  |  | namespace oopse { | 
| 49 | gezelter | 507 | ZconstraintForceManager::ZconstraintForceManager(SimInfo* info): ForceManager(info), infiniteTime(1e31) { | 
| 50 | gezelter | 246 | currSnapshot_ = info_->getSnapshotManager()->getCurrentSnapshot(); | 
| 51 |  |  | Globals* simParam = info_->getSimParams(); | 
| 52 |  |  |  | 
| 53 |  |  | if (simParam->haveDt()){ | 
| 54 | gezelter | 507 | dt_ = simParam->getDt(); | 
| 55 | gezelter | 246 | } else { | 
| 56 | gezelter | 507 | sprintf(painCave.errMsg, | 
| 57 |  |  | "Integrator Error: dt is not set\n"); | 
| 58 |  |  | painCave.isFatal = 1; | 
| 59 |  |  | simError(); | 
| 60 | gezelter | 246 | } | 
| 61 |  |  |  | 
| 62 | tim | 665 | if (simParam->haveZconsTime()){ | 
| 63 | gezelter | 507 | zconsTime_ = simParam->getZconsTime(); | 
| 64 | gezelter | 246 | } | 
| 65 |  |  | else{ | 
| 66 | gezelter | 507 | sprintf(painCave.errMsg, | 
| 67 |  |  | "ZConstraint error: If you use a ZConstraint,\n" | 
| 68 |  |  | "\tyou must set zconsTime.\n"); | 
| 69 |  |  | painCave.isFatal = 1; | 
| 70 |  |  | simError(); | 
| 71 | gezelter | 246 | } | 
| 72 |  |  |  | 
| 73 |  |  | if (simParam->haveZconsTol()){ | 
| 74 | gezelter | 507 | zconsTol_ = simParam->getZconsTol(); | 
| 75 | gezelter | 246 | } | 
| 76 |  |  | else{ | 
| 77 | gezelter | 507 | zconsTol_ = 0.01; | 
| 78 |  |  | sprintf(painCave.errMsg, | 
| 79 |  |  | "ZConstraint Warning: Tolerance for z-constraint method is not specified.\n" | 
| 80 |  |  | "\tOOPSE will use a default value of %f.\n" | 
| 81 |  |  | "\tTo set the tolerance, use the zconsTol variable.\n", | 
| 82 |  |  | zconsTol_); | 
| 83 |  |  | painCave.isFatal = 0; | 
| 84 |  |  | simError(); | 
| 85 | gezelter | 246 | } | 
| 86 |  |  |  | 
| 87 |  |  | //set zcons gap | 
| 88 | tim | 665 | if (simParam->haveZconsGap()){ | 
| 89 | gezelter | 507 | usingZconsGap_ = true; | 
| 90 |  |  | zconsGap_ = simParam->getZconsGap(); | 
| 91 | gezelter | 246 | }else { | 
| 92 | gezelter | 507 | usingZconsGap_ = false; | 
| 93 |  |  | zconsGap_ = 0.0; | 
| 94 | gezelter | 246 | } | 
| 95 |  |  |  | 
| 96 |  |  | //set zcons fixtime | 
| 97 | tim | 665 | if (simParam->haveZconsFixtime()){ | 
| 98 | gezelter | 507 | zconsFixingTime_ = simParam->getZconsFixtime(); | 
| 99 | gezelter | 246 | } else { | 
| 100 | gezelter | 507 | zconsFixingTime_ = infiniteTime; | 
| 101 | gezelter | 246 | } | 
| 102 |  |  |  | 
| 103 |  |  | //set zconsUsingSMD | 
| 104 | tim | 665 | if (simParam->haveZconsUsingSMD()){ | 
| 105 | gezelter | 507 | usingSMD_ = simParam->getZconsUsingSMD(); | 
| 106 | gezelter | 246 | }else { | 
| 107 | gezelter | 507 | usingSMD_ =false; | 
| 108 | gezelter | 246 | } | 
| 109 |  |  |  | 
| 110 |  |  | zconsOutput_ = getPrefix(info_->getFinalConfigFileName()) + ".fz"; | 
| 111 |  |  |  | 
| 112 |  |  | //estimate the force constant of harmonical potential | 
| 113 |  |  | Mat3x3d hmat = currSnapshot_->getHmat(); | 
| 114 | tim | 963 | RealType halfOfLargestBox = std::max(hmat(0, 0), std::max(hmat(1, 1), hmat(2, 2))) /2; | 
| 115 |  |  | RealType targetTemp; | 
| 116 | gezelter | 246 | if (simParam->haveTargetTemp()) { | 
| 117 | gezelter | 507 | targetTemp = simParam->getTargetTemp(); | 
| 118 | gezelter | 246 | } else { | 
| 119 | gezelter | 507 | targetTemp = 298.0; | 
| 120 | gezelter | 246 | } | 
| 121 | tim | 963 | RealType zforceConstant = OOPSEConstant::kb * targetTemp / (halfOfLargestBox * halfOfLargestBox); | 
| 122 | gezelter | 246 |  | 
| 123 | tim | 770 | int nZconstraints = simParam->getNZconsStamps(); | 
| 124 |  |  | std::vector<ZConsStamp*> stamp = simParam->getZconsStamps(); | 
| 125 | gezelter | 246 | // | 
| 126 |  |  | for (int i = 0; i < nZconstraints; i++){ | 
| 127 |  |  |  | 
| 128 | gezelter | 507 | ZconstraintParam param; | 
| 129 |  |  | int zmolIndex = stamp[i]->getMolIndex(); | 
| 130 |  |  | if (stamp[i]->haveZpos()) { | 
| 131 |  |  | param.zTargetPos = stamp[i]->getZpos(); | 
| 132 |  |  | } else { | 
| 133 |  |  | param.zTargetPos = getZTargetPos(zmolIndex); | 
| 134 |  |  | } | 
| 135 | gezelter | 246 |  | 
| 136 | gezelter | 507 | param.kz = zforceConstant * stamp[i]->getKratio(); | 
| 137 | gezelter | 246 |  | 
| 138 | gezelter | 507 | if (stamp[i]->haveCantVel()) { | 
| 139 |  |  | param.cantVel = stamp[i]->getCantVel(); | 
| 140 |  |  | } else { | 
| 141 |  |  | param.cantVel = 0.0; | 
| 142 |  |  | } | 
| 143 | gezelter | 246 |  | 
| 144 | gezelter | 507 | allZMolIndices_.insert(std::make_pair(zmolIndex, param)); | 
| 145 | gezelter | 246 | } | 
| 146 |  |  |  | 
| 147 |  |  | //create fixedMols_, movingMols_ and unconsMols lists | 
| 148 |  |  | update(); | 
| 149 |  |  |  | 
| 150 |  |  | //calculate masss of unconstraint molecules in the whole system (never change during the simulation) | 
| 151 | tim | 963 | RealType totMassUnconsMols_local = 0.0; | 
| 152 | gezelter | 246 | std::vector<Molecule*>::iterator j; | 
| 153 |  |  | for ( j = unzconsMols_.begin(); j !=  unzconsMols_.end(); ++j) { | 
| 154 | gezelter | 507 | totMassUnconsMols_local += (*j)->getMass(); | 
| 155 | gezelter | 246 | } | 
| 156 |  |  | #ifndef IS_MPI | 
| 157 |  |  | totMassUnconsMols_ = totMassUnconsMols_local; | 
| 158 |  |  | #else | 
| 159 | tim | 963 | MPI_Allreduce(&totMassUnconsMols_local, &totMassUnconsMols_, 1, MPI_REALTYPE, | 
| 160 | gezelter | 507 | MPI_SUM, MPI_COMM_WORLD); | 
| 161 | gezelter | 246 | #endif | 
| 162 |  |  |  | 
| 163 |  |  | // creat zconsWriter | 
| 164 |  |  | fzOut = new ZConsWriter(info_, zconsOutput_.c_str()); | 
| 165 |  |  |  | 
| 166 |  |  | if (!fzOut){ | 
| 167 |  |  | sprintf(painCave.errMsg, "Fail to create ZConsWriter\n"); | 
| 168 |  |  | painCave.isFatal = 1; | 
| 169 |  |  | simError(); | 
| 170 |  |  | } | 
| 171 |  |  |  | 
| 172 | gezelter | 507 | } | 
| 173 | gezelter | 246 |  | 
| 174 | gezelter | 507 | ZconstraintForceManager::~ZconstraintForceManager(){ | 
| 175 | gezelter | 246 |  | 
| 176 | gezelter | 507 | if (fzOut){ | 
| 177 |  |  | delete fzOut; | 
| 178 |  |  | } | 
| 179 |  |  |  | 
| 180 | gezelter | 246 | } | 
| 181 |  |  |  | 
| 182 | gezelter | 507 | void ZconstraintForceManager::update(){ | 
| 183 | gezelter | 246 | fixedZMols_.clear(); | 
| 184 |  |  | movingZMols_.clear(); | 
| 185 |  |  | unzconsMols_.clear(); | 
| 186 |  |  |  | 
| 187 |  |  | for (std::map<int, ZconstraintParam>::iterator i = allZMolIndices_.begin(); i != allZMolIndices_.end(); ++i) { | 
| 188 |  |  | #ifdef IS_MPI | 
| 189 | gezelter | 507 | if (info_->getMolToProc(i->first) == worldRank) { | 
| 190 | gezelter | 246 | #endif | 
| 191 | gezelter | 507 | ZconstraintMol zmol; | 
| 192 |  |  | zmol.mol = info_->getMoleculeByGlobalIndex(i->first); | 
| 193 |  |  | assert(zmol.mol); | 
| 194 |  |  | zmol.param = i->second; | 
| 195 |  |  | zmol.cantPos = zmol.param.zTargetPos; /**@todo fixed me when zmol migrate, it is incorrect*/ | 
| 196 |  |  | Vector3d com = zmol.mol->getCom(); | 
| 197 | tim | 963 | RealType diff = fabs(zmol.param.zTargetPos - com[whichDirection]); | 
| 198 | gezelter | 507 | if (diff < zconsTol_) { | 
| 199 |  |  | fixedZMols_.push_back(zmol); | 
| 200 |  |  | } else { | 
| 201 |  |  | movingZMols_.push_back(zmol); | 
| 202 |  |  | } | 
| 203 | gezelter | 246 |  | 
| 204 |  |  | #ifdef IS_MPI | 
| 205 | gezelter | 507 | } | 
| 206 | gezelter | 246 | #endif | 
| 207 |  |  | } | 
| 208 |  |  |  | 
| 209 |  |  | calcTotalMassMovingZMols(); | 
| 210 |  |  |  | 
| 211 |  |  | std::set<int> zmolSet; | 
| 212 |  |  | for (std::list<ZconstraintMol>::iterator i = movingZMols_.begin(); i !=  movingZMols_.end(); ++i) { | 
| 213 | gezelter | 507 | zmolSet.insert(i->mol->getGlobalIndex()); | 
| 214 | gezelter | 246 | } | 
| 215 |  |  |  | 
| 216 |  |  | for (std::list<ZconstraintMol>::iterator i = fixedZMols_.begin(); i !=  fixedZMols_.end(); ++i) { | 
| 217 | gezelter | 507 | zmolSet.insert(i->mol->getGlobalIndex()); | 
| 218 | gezelter | 246 | } | 
| 219 |  |  |  | 
| 220 |  |  | SimInfo::MoleculeIterator mi; | 
| 221 |  |  | Molecule* mol; | 
| 222 |  |  | for(mol = info_->beginMolecule(mi); mol != NULL; mol = info_->nextMolecule(mi)) { | 
| 223 | gezelter | 507 | if (zmolSet.find(mol->getGlobalIndex()) == zmolSet.end()) { | 
| 224 |  |  | unzconsMols_.push_back(mol); | 
| 225 |  |  | } | 
| 226 | gezelter | 246 | } | 
| 227 |  |  |  | 
| 228 | gezelter | 507 | } | 
| 229 | gezelter | 246 |  | 
| 230 | gezelter | 507 | bool ZconstraintForceManager::isZMol(Molecule* mol){ | 
| 231 | gezelter | 246 | return allZMolIndices_.find(mol->getGlobalIndex()) == allZMolIndices_.end() ? false : true; | 
| 232 | gezelter | 507 | } | 
| 233 | gezelter | 246 |  | 
| 234 | gezelter | 507 | void ZconstraintForceManager::init(){ | 
| 235 | gezelter | 246 |  | 
| 236 | gezelter | 507 | //zero out the velocities of center of mass of unconstrained molecules | 
| 237 |  |  | //and the velocities of center of mass of every single z-constrained molecueles | 
| 238 |  |  | zeroVelocity(); | 
| 239 | gezelter | 246 |  | 
| 240 | gezelter | 507 | currZconsTime_ = currSnapshot_->getTime(); | 
| 241 |  |  | } | 
| 242 | gezelter | 246 |  | 
| 243 | gezelter | 507 | void ZconstraintForceManager::calcForces(bool needPotential, bool needStress){ | 
| 244 | gezelter | 246 | ForceManager::calcForces(needPotential, needStress); | 
| 245 |  |  |  | 
| 246 |  |  | if (usingZconsGap_){ | 
| 247 | gezelter | 507 | updateZPos(); | 
| 248 | gezelter | 246 | } | 
| 249 |  |  |  | 
| 250 |  |  | if (checkZConsState()){ | 
| 251 | gezelter | 507 | zeroVelocity(); | 
| 252 |  |  | calcTotalMassMovingZMols(); | 
| 253 | gezelter | 246 | } | 
| 254 |  |  |  | 
| 255 |  |  | //do zconstraint force; | 
| 256 |  |  | if (haveFixedZMols()){ | 
| 257 | gezelter | 507 | doZconstraintForce(); | 
| 258 | gezelter | 246 | } | 
| 259 |  |  |  | 
| 260 |  |  | //use external force to move the molecules to the specified positions | 
| 261 |  |  | if (haveMovingZMols()){ | 
| 262 | gezelter | 507 | doHarmonic(); | 
| 263 | gezelter | 246 | } | 
| 264 |  |  |  | 
| 265 |  |  | //write out forces and current positions of z-constraint molecules | 
| 266 |  |  | if (currSnapshot_->getTime() >= currZconsTime_){ | 
| 267 | gezelter | 507 | std::list<ZconstraintMol>::iterator i; | 
| 268 |  |  | Vector3d com; | 
| 269 |  |  | for(i = fixedZMols_.begin(); i != fixedZMols_.end(); ++i) { | 
| 270 |  |  | com = i->mol->getCom(); | 
| 271 |  |  | i->zpos = com[whichDirection]; | 
| 272 |  |  | } | 
| 273 | gezelter | 246 |  | 
| 274 | gezelter | 507 | fzOut->writeFZ(fixedZMols_); | 
| 275 |  |  | currZconsTime_ += zconsTime_; | 
| 276 | gezelter | 246 | } | 
| 277 | gezelter | 507 | } | 
| 278 | gezelter | 246 |  | 
| 279 | gezelter | 507 | void ZconstraintForceManager::zeroVelocity(){ | 
| 280 | gezelter | 246 |  | 
| 281 |  |  | Vector3d comVel; | 
| 282 |  |  | Vector3d vel; | 
| 283 |  |  | std::list<ZconstraintMol>::iterator i; | 
| 284 |  |  | Molecule* mol; | 
| 285 |  |  | StuntDouble* integrableObject; | 
| 286 |  |  | Molecule::IntegrableObjectIterator ii; | 
| 287 |  |  |  | 
| 288 |  |  | //zero out the velocities of center of mass of fixed z-constrained molecules | 
| 289 |  |  | for(i = fixedZMols_.begin(); i != fixedZMols_.end(); ++i) { | 
| 290 | gezelter | 507 | mol = i->mol; | 
| 291 |  |  | comVel = mol->getComVel(); | 
| 292 |  |  | for(integrableObject = mol->beginIntegrableObject(ii); integrableObject != NULL; | 
| 293 |  |  | integrableObject = mol->nextIntegrableObject(ii)) { | 
| 294 |  |  | vel = integrableObject->getVel(); | 
| 295 |  |  | vel[whichDirection] -= comVel[whichDirection]; | 
| 296 |  |  | integrableObject->setVel(vel); | 
| 297 |  |  | } | 
| 298 | gezelter | 246 | } | 
| 299 |  |  |  | 
| 300 |  |  | // calculate the vz of center of mass of moving molecules(include unconstrained molecules | 
| 301 |  |  | // and moving z-constrained molecules) | 
| 302 | tim | 963 | RealType pzMovingMols_local = 0.0; | 
| 303 |  |  | RealType pzMovingMols; | 
| 304 | gezelter | 246 |  | 
| 305 |  |  | for ( i = movingZMols_.begin(); i !=  movingZMols_.end(); ++i) { | 
| 306 | gezelter | 507 | mol = i->mol; | 
| 307 |  |  | comVel = mol->getComVel(); | 
| 308 |  |  | pzMovingMols_local +=  mol->getMass() * comVel[whichDirection]; | 
| 309 | gezelter | 246 | } | 
| 310 |  |  |  | 
| 311 |  |  | std::vector<Molecule*>::iterator j; | 
| 312 |  |  | for ( j = unzconsMols_.begin(); j !=  unzconsMols_.end(); ++j) { | 
| 313 | gezelter | 507 | mol =*j; | 
| 314 |  |  | comVel = mol->getComVel(); | 
| 315 |  |  | pzMovingMols_local += mol->getMass() * comVel[whichDirection]; | 
| 316 | gezelter | 246 | } | 
| 317 |  |  |  | 
| 318 |  |  | #ifndef IS_MPI | 
| 319 |  |  | pzMovingMols = pzMovingMols_local; | 
| 320 |  |  | #else | 
| 321 | tim | 963 | MPI_Allreduce(&pzMovingMols_local, &pzMovingMols, 1, MPI_REALTYPE, | 
| 322 | gezelter | 507 | MPI_SUM, MPI_COMM_WORLD); | 
| 323 | gezelter | 246 | #endif | 
| 324 |  |  |  | 
| 325 | tim | 963 | RealType vzMovingMols = pzMovingMols / (totMassMovingZMols_ + totMassUnconsMols_); | 
| 326 | gezelter | 246 |  | 
| 327 |  |  | //modify the velocities of moving z-constrained molecuels | 
| 328 |  |  | for ( i = movingZMols_.begin(); i !=  movingZMols_.end(); ++i) { | 
| 329 | gezelter | 507 | mol = i->mol; | 
| 330 |  |  | for(integrableObject = mol->beginIntegrableObject(ii); integrableObject != NULL; | 
| 331 |  |  | integrableObject = mol->nextIntegrableObject(ii)) { | 
| 332 | gezelter | 246 |  | 
| 333 | gezelter | 507 | vel = integrableObject->getVel(); | 
| 334 |  |  | vel[whichDirection] -= vzMovingMols; | 
| 335 |  |  | integrableObject->setVel(vel); | 
| 336 |  |  | } | 
| 337 | gezelter | 246 | } | 
| 338 |  |  |  | 
| 339 |  |  | //modify the velocites of unconstrained molecules | 
| 340 |  |  | for ( j = unzconsMols_.begin(); j !=  unzconsMols_.end(); ++j) { | 
| 341 | gezelter | 507 | mol =*j; | 
| 342 |  |  | for(integrableObject = mol->beginIntegrableObject(ii); integrableObject != NULL; | 
| 343 |  |  | integrableObject = mol->nextIntegrableObject(ii)) { | 
| 344 | gezelter | 246 |  | 
| 345 | gezelter | 507 | vel = integrableObject->getVel(); | 
| 346 |  |  | vel[whichDirection] -= vzMovingMols; | 
| 347 |  |  | integrableObject->setVel(vel); | 
| 348 |  |  | } | 
| 349 | gezelter | 246 | } | 
| 350 |  |  |  | 
| 351 | gezelter | 507 | } | 
| 352 | gezelter | 246 |  | 
| 353 |  |  |  | 
| 354 | gezelter | 507 | void ZconstraintForceManager::doZconstraintForce(){ | 
| 355 | tim | 963 | RealType totalFZ; | 
| 356 |  |  | RealType totalFZ_local; | 
| 357 | gezelter | 507 | Vector3d com; | 
| 358 |  |  | Vector3d force(0.0); | 
| 359 | gezelter | 246 |  | 
| 360 | gezelter | 507 | //constrain the molecules which do not reach the specified positions | 
| 361 | gezelter | 246 |  | 
| 362 | gezelter | 507 | //Zero Out the force of z-contrained molecules | 
| 363 |  |  | totalFZ_local = 0; | 
| 364 | gezelter | 246 |  | 
| 365 |  |  |  | 
| 366 |  |  | //calculate the total z-contrained force of fixed z-contrained molecules | 
| 367 |  |  | std::list<ZconstraintMol>::iterator i; | 
| 368 |  |  | Molecule* mol; | 
| 369 |  |  | StuntDouble* integrableObject; | 
| 370 |  |  | Molecule::IntegrableObjectIterator ii; | 
| 371 |  |  |  | 
| 372 |  |  | for ( i = fixedZMols_.begin(); i !=  fixedZMols_.end(); ++i) { | 
| 373 | gezelter | 507 | mol = i->mol; | 
| 374 |  |  | i->fz = 0.0; | 
| 375 |  |  | for(integrableObject = mol->beginIntegrableObject(ii); integrableObject != NULL; | 
| 376 |  |  | integrableObject = mol->nextIntegrableObject(ii)) { | 
| 377 | gezelter | 246 |  | 
| 378 | gezelter | 507 | force = integrableObject->getFrc(); | 
| 379 |  |  | i->fz += force[whichDirection]; | 
| 380 |  |  | } | 
| 381 |  |  | totalFZ_local += i->fz; | 
| 382 | gezelter | 246 | } | 
| 383 |  |  |  | 
| 384 | gezelter | 507 | //calculate total z-constraint force | 
| 385 | gezelter | 246 | #ifdef IS_MPI | 
| 386 | tim | 963 | MPI_Allreduce(&totalFZ_local, &totalFZ, 1, MPI_REALTYPE, MPI_SUM, MPI_COMM_WORLD); | 
| 387 | gezelter | 246 | #else | 
| 388 | gezelter | 507 | totalFZ = totalFZ_local; | 
| 389 | gezelter | 246 | #endif | 
| 390 |  |  |  | 
| 391 |  |  |  | 
| 392 | gezelter | 507 | // apply negative to fixed z-constrained molecues; | 
| 393 | gezelter | 246 | for ( i = fixedZMols_.begin(); i !=  fixedZMols_.end(); ++i) { | 
| 394 | gezelter | 507 | mol = i->mol; | 
| 395 |  |  | for(integrableObject = mol->beginIntegrableObject(ii); integrableObject != NULL; | 
| 396 |  |  | integrableObject = mol->nextIntegrableObject(ii)) { | 
| 397 | gezelter | 246 |  | 
| 398 | gezelter | 507 | force[whichDirection] = -getZFOfFixedZMols(mol, integrableObject, i->fz); | 
| 399 |  |  | integrableObject->addFrc(force); | 
| 400 |  |  | } | 
| 401 | gezelter | 246 | } | 
| 402 |  |  |  | 
| 403 | gezelter | 507 | //modify the forces of moving z-constrained molecules | 
| 404 | gezelter | 246 | for ( i = movingZMols_.begin(); i !=  movingZMols_.end(); ++i) { | 
| 405 | gezelter | 507 | mol = i->mol; | 
| 406 |  |  | for(integrableObject = mol->beginIntegrableObject(ii); integrableObject != NULL; | 
| 407 |  |  | integrableObject = mol->nextIntegrableObject(ii)) { | 
| 408 | gezelter | 246 |  | 
| 409 | gezelter | 507 | force[whichDirection] = -getZFOfMovingMols(mol,totalFZ); | 
| 410 |  |  | integrableObject->addFrc(force); | 
| 411 |  |  | } | 
| 412 | gezelter | 246 | } | 
| 413 |  |  |  | 
| 414 | gezelter | 507 | //modify the forces of unconstrained molecules | 
| 415 | gezelter | 246 | std::vector<Molecule*>::iterator j; | 
| 416 |  |  | for ( j = unzconsMols_.begin(); j !=  unzconsMols_.end(); ++j) { | 
| 417 | gezelter | 507 | mol =*j; | 
| 418 |  |  | for(integrableObject = mol->beginIntegrableObject(ii); integrableObject != NULL; | 
| 419 |  |  | integrableObject = mol->nextIntegrableObject(ii)) { | 
| 420 | gezelter | 246 |  | 
| 421 | gezelter | 507 | force[whichDirection] = -getZFOfMovingMols(mol, totalFZ); | 
| 422 |  |  | integrableObject->addFrc(force); | 
| 423 |  |  | } | 
| 424 | gezelter | 246 | } | 
| 425 |  |  |  | 
| 426 | gezelter | 507 | } | 
| 427 | gezelter | 246 |  | 
| 428 |  |  |  | 
| 429 | gezelter | 507 | void ZconstraintForceManager::doHarmonic(){ | 
| 430 | tim | 963 | RealType totalFZ; | 
| 431 | gezelter | 246 | Vector3d force(0.0); | 
| 432 |  |  | Vector3d com; | 
| 433 | tim | 963 | RealType totalFZ_local = 0; | 
| 434 | gezelter | 246 | std::list<ZconstraintMol>::iterator i; | 
| 435 |  |  | StuntDouble* integrableObject; | 
| 436 |  |  | Molecule::IntegrableObjectIterator ii; | 
| 437 |  |  | Molecule* mol; | 
| 438 |  |  | for ( i = movingZMols_.begin(); i !=  movingZMols_.end(); ++i) { | 
| 439 | gezelter | 507 | mol = i->mol; | 
| 440 |  |  | com = mol->getCom(); | 
| 441 | tim | 963 | RealType resPos = usingSMD_? i->cantPos : i->param.zTargetPos; | 
| 442 |  |  | RealType diff = com[whichDirection] - resPos; | 
| 443 |  |  | RealType harmonicU = 0.5 * i->param.kz * diff * diff; | 
| 444 | gezelter | 507 | currSnapshot_->statData[Stats::LONG_RANGE_POTENTIAL] += harmonicU; | 
| 445 | tim | 963 | RealType harmonicF = -i->param.kz * diff; | 
| 446 | gezelter | 507 | totalFZ_local += harmonicF; | 
| 447 | gezelter | 246 |  | 
| 448 | gezelter | 507 | //adjust force | 
| 449 |  |  | for(integrableObject = mol->beginIntegrableObject(ii); integrableObject != NULL; | 
| 450 |  |  | integrableObject = mol->nextIntegrableObject(ii)) { | 
| 451 | gezelter | 246 |  | 
| 452 | gezelter | 507 | force[whichDirection] = getHFOfFixedZMols(mol, integrableObject, harmonicF); | 
| 453 |  |  | integrableObject->addFrc(force); | 
| 454 |  |  | } | 
| 455 | gezelter | 246 | } | 
| 456 |  |  |  | 
| 457 |  |  | #ifndef IS_MPI | 
| 458 | gezelter | 507 | totalFZ = totalFZ_local; | 
| 459 | gezelter | 246 | #else | 
| 460 | tim | 963 | MPI_Allreduce(&totalFZ_local, &totalFZ, 1, MPI_REALTYPE, MPI_SUM, MPI_COMM_WORLD); | 
| 461 | gezelter | 246 | #endif | 
| 462 |  |  |  | 
| 463 |  |  | //modify the forces of unconstrained molecules | 
| 464 |  |  | std::vector<Molecule*>::iterator j; | 
| 465 |  |  | for ( j = unzconsMols_.begin(); j !=  unzconsMols_.end(); ++j) { | 
| 466 | gezelter | 507 | mol = *j; | 
| 467 |  |  | for(integrableObject = mol->beginIntegrableObject(ii); integrableObject != NULL; | 
| 468 |  |  | integrableObject = mol->nextIntegrableObject(ii)) { | 
| 469 | gezelter | 246 |  | 
| 470 | gezelter | 507 | force[whichDirection] = getHFOfUnconsMols(mol, totalFZ); | 
| 471 |  |  | integrableObject->addFrc(force); | 
| 472 |  |  | } | 
| 473 | gezelter | 246 | } | 
| 474 |  |  |  | 
| 475 | gezelter | 507 | } | 
| 476 | gezelter | 246 |  | 
| 477 | gezelter | 507 | bool ZconstraintForceManager::checkZConsState(){ | 
| 478 | gezelter | 246 | Vector3d com; | 
| 479 | tim | 963 | RealType diff; | 
| 480 | gezelter | 246 | int changed_local = 0; | 
| 481 |  |  |  | 
| 482 |  |  | std::list<ZconstraintMol>::iterator i; | 
| 483 |  |  | std::list<ZconstraintMol>::iterator j; | 
| 484 |  |  |  | 
| 485 |  |  | std::list<ZconstraintMol> newMovingZMols; | 
| 486 |  |  | for ( i = fixedZMols_.begin(); i !=  fixedZMols_.end();) { | 
| 487 | gezelter | 507 | com = i->mol->getCom(); | 
| 488 |  |  | diff = fabs(com[whichDirection] - i->param.zTargetPos); | 
| 489 |  |  | if (diff > zconsTol_) { | 
| 490 |  |  | if (usingZconsGap_) { | 
| 491 |  |  | i->endFixingTime = infiniteTime; | 
| 492 |  |  | } | 
| 493 |  |  | j = i++; | 
| 494 |  |  | newMovingZMols.push_back(*j); | 
| 495 |  |  | fixedZMols_.erase(j); | 
| 496 | gezelter | 246 |  | 
| 497 | gezelter | 507 | changed_local = 1; | 
| 498 |  |  | }else { | 
| 499 |  |  | ++i; | 
| 500 |  |  | } | 
| 501 | gezelter | 246 | } | 
| 502 |  |  |  | 
| 503 |  |  | std::list<ZconstraintMol> newFixedZMols; | 
| 504 |  |  | for ( i = movingZMols_.begin(); i !=  movingZMols_.end();) { | 
| 505 | gezelter | 507 | com = i->mol->getCom(); | 
| 506 |  |  | diff = fabs(com[whichDirection] - i->param.zTargetPos); | 
| 507 |  |  | if (diff <= zconsTol_) { | 
| 508 |  |  | if (usingZconsGap_) { | 
| 509 |  |  | i->endFixingTime = currSnapshot_->getTime() + zconsFixingTime_; | 
| 510 |  |  | } | 
| 511 |  |  | //this moving zconstraint molecule is about to fixed | 
| 512 |  |  | //moved this molecule to | 
| 513 |  |  | j = i++; | 
| 514 |  |  | newFixedZMols.push_back(*j); | 
| 515 |  |  | movingZMols_.erase(j); | 
| 516 |  |  | changed_local = 1; | 
| 517 |  |  | }else { | 
| 518 |  |  | ++i; | 
| 519 |  |  | } | 
| 520 | gezelter | 246 | } | 
| 521 |  |  |  | 
| 522 |  |  | //merge the lists | 
| 523 |  |  | fixedZMols_.insert(fixedZMols_.end(), newFixedZMols.begin(), newFixedZMols.end()); | 
| 524 |  |  | movingZMols_.insert(movingZMols_.end(), newMovingZMols.begin(), newMovingZMols.end()); | 
| 525 |  |  |  | 
| 526 |  |  | int changed; | 
| 527 |  |  | #ifndef IS_MPI | 
| 528 |  |  | changed = changed_local; | 
| 529 |  |  | #else | 
| 530 |  |  | MPI_Allreduce(&changed_local, &changed, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD); | 
| 531 |  |  | #endif | 
| 532 |  |  |  | 
| 533 |  |  | return (changed > 0); | 
| 534 | gezelter | 507 | } | 
| 535 | gezelter | 246 |  | 
| 536 | gezelter | 507 | bool ZconstraintForceManager::haveFixedZMols(){ | 
| 537 |  |  | int havingFixed; | 
| 538 |  |  | int havingFixed_local = fixedZMols_.empty() ? 0 : 1; | 
| 539 | gezelter | 246 |  | 
| 540 |  |  | #ifndef IS_MPI | 
| 541 | gezelter | 507 | havingFixed = havingFixed_local; | 
| 542 | gezelter | 246 | #else | 
| 543 | gezelter | 507 | MPI_Allreduce(&havingFixed_local, &havingFixed, 1, MPI_INT, MPI_SUM, | 
| 544 |  |  | MPI_COMM_WORLD); | 
| 545 | gezelter | 246 | #endif | 
| 546 |  |  |  | 
| 547 | gezelter | 507 | return havingFixed > 0; | 
| 548 |  |  | } | 
| 549 | gezelter | 246 |  | 
| 550 |  |  |  | 
| 551 | gezelter | 507 | bool ZconstraintForceManager::haveMovingZMols(){ | 
| 552 |  |  | int havingMoving_local; | 
| 553 |  |  | int havingMoving; | 
| 554 | gezelter | 246 |  | 
| 555 | gezelter | 507 | havingMoving_local = movingZMols_.empty()? 0 : 1; | 
| 556 | gezelter | 246 |  | 
| 557 |  |  | #ifndef IS_MPI | 
| 558 | gezelter | 507 | havingMoving = havingMoving_local; | 
| 559 | gezelter | 246 | #else | 
| 560 | gezelter | 507 | MPI_Allreduce(&havingMoving_local, &havingMoving, 1, MPI_INT, MPI_SUM, | 
| 561 |  |  | MPI_COMM_WORLD); | 
| 562 | gezelter | 246 | #endif | 
| 563 |  |  |  | 
| 564 | gezelter | 507 | return havingMoving > 0; | 
| 565 |  |  | } | 
| 566 | gezelter | 246 |  | 
| 567 | gezelter | 507 | void ZconstraintForceManager::calcTotalMassMovingZMols(){ | 
| 568 | gezelter | 246 |  | 
| 569 | tim | 963 | RealType totMassMovingZMols_local = 0.0; | 
| 570 | gezelter | 246 | std::list<ZconstraintMol>::iterator i; | 
| 571 |  |  | for ( i = movingZMols_.begin(); i !=  movingZMols_.end(); ++i) { | 
| 572 | gezelter | 507 | totMassMovingZMols_local += i->mol->getMass(); | 
| 573 | gezelter | 246 | } | 
| 574 |  |  |  | 
| 575 |  |  | #ifdef IS_MPI | 
| 576 | tim | 963 | MPI_Allreduce(&totMassMovingZMols_local, &totMassMovingZMols_, 1, MPI_REALTYPE, | 
| 577 | gezelter | 507 | MPI_SUM, MPI_COMM_WORLD); | 
| 578 | gezelter | 246 | #else | 
| 579 |  |  | totMassMovingZMols_ = totMassMovingZMols_local; | 
| 580 |  |  | #endif | 
| 581 |  |  |  | 
| 582 | gezelter | 507 | } | 
| 583 | gezelter | 246 |  | 
| 584 | tim | 963 | RealType ZconstraintForceManager::getZFOfFixedZMols(Molecule* mol, StuntDouble* sd, RealType totalForce){ | 
| 585 | gezelter | 507 | return totalForce * sd->getMass() / mol->getMass(); | 
| 586 |  |  | } | 
| 587 | gezelter | 246 |  | 
| 588 | tim | 963 | RealType ZconstraintForceManager::getZFOfMovingMols(Molecule* mol, RealType totalForce){ | 
| 589 | gezelter | 507 | return totalForce * mol->getMass() / (totMassUnconsMols_ + totMassMovingZMols_); | 
| 590 |  |  | } | 
| 591 | gezelter | 246 |  | 
| 592 | tim | 963 | RealType ZconstraintForceManager::getHFOfFixedZMols(Molecule* mol, StuntDouble*sd, RealType totalForce){ | 
| 593 | gezelter | 507 | return totalForce * sd->getMass() / mol->getMass(); | 
| 594 |  |  | } | 
| 595 | gezelter | 246 |  | 
| 596 | tim | 963 | RealType ZconstraintForceManager::getHFOfUnconsMols(Molecule* mol, RealType totalForce){ | 
| 597 | gezelter | 507 | return totalForce * mol->getMass() / totMassUnconsMols_; | 
| 598 |  |  | } | 
| 599 | gezelter | 246 |  | 
| 600 | gezelter | 507 | void ZconstraintForceManager::updateZPos(){ | 
| 601 | tim | 963 | RealType curTime = currSnapshot_->getTime(); | 
| 602 | gezelter | 246 | std::list<ZconstraintMol>::iterator i; | 
| 603 |  |  | for ( i = fixedZMols_.begin(); i !=  fixedZMols_.end(); ++i) { | 
| 604 | gezelter | 507 | i->param.zTargetPos += zconsGap_; | 
| 605 | gezelter | 246 | } | 
| 606 | gezelter | 507 | } | 
| 607 | gezelter | 246 |  | 
| 608 | gezelter | 507 | void ZconstraintForceManager::updateCantPos(){ | 
| 609 | gezelter | 246 | std::list<ZconstraintMol>::iterator i; | 
| 610 |  |  | for ( i = movingZMols_.begin(); i !=  movingZMols_.end(); ++i) { | 
| 611 | gezelter | 507 | i->cantPos += i->param.cantVel * dt_; | 
| 612 | gezelter | 246 | } | 
| 613 | gezelter | 507 | } | 
| 614 | gezelter | 246 |  | 
| 615 | tim | 963 | RealType ZconstraintForceManager::getZTargetPos(int index){ | 
| 616 |  |  | RealType zTargetPos; | 
| 617 | gezelter | 246 | #ifndef IS_MPI | 
| 618 |  |  | Molecule* mol = info_->getMoleculeByGlobalIndex(index); | 
| 619 |  |  | assert(mol); | 
| 620 |  |  | Vector3d com = mol->getCom(); | 
| 621 |  |  | zTargetPos = com[whichDirection]; | 
| 622 |  |  | #else | 
| 623 |  |  | int whicProc = info_->getMolToProc(index); | 
| 624 | tim | 963 | MPI_Bcast(&zTargetPos, 1, MPI_REALTYPE, whicProc, MPI_COMM_WORLD); | 
| 625 | gezelter | 246 | #endif | 
| 626 |  |  | return zTargetPos; | 
| 627 | gezelter | 507 | } | 
| 628 | gezelter | 246 |  | 
| 629 |  |  | } |