| 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 "applications/dynamicProps/TimeCorrFunc.hpp" |
| 43 |
#include "utils/simError.h" |
| 44 |
#include "primitives/Molecule.hpp" |
| 45 |
namespace oopse { |
| 46 |
|
| 47 |
TimeCorrFunc::TimeCorrFunc(SimInfo* info, const std::string& filename, |
| 48 |
const std::string& sele1, const std::string& sele2, int storageLayout) |
| 49 |
: info_(info), storageLayout_(storageLayout), dumpFilename_(filename), selectionScript1_(sele1), |
| 50 |
selectionScript2_(sele2), evaluator1_(info), evaluator2_(info), seleMan1_(info), seleMan2_(info){ |
| 51 |
|
| 52 |
int nAtoms = info->getNGlobalAtoms(); |
| 53 |
int nRigidBodies = info->getNGlobalRigidBodies(); |
| 54 |
int nStuntDoubles = nAtoms + nRigidBodies; |
| 55 |
|
| 56 |
std::set<AtomType*> atomTypes = info->getUniqueAtomTypes(); |
| 57 |
std::set<AtomType*>::iterator i; |
| 58 |
bool hasDirectionalAtom = false; |
| 59 |
bool hasMultipole = false; |
| 60 |
for (i = atomTypes.begin(); i != atomTypes.end(); ++i) { |
| 61 |
if ((*i)->isDirectional()){ |
| 62 |
hasDirectionalAtom = true; |
| 63 |
} |
| 64 |
if ((*i)->isMultipole()){ |
| 65 |
hasMultipole = true; |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
if (nRigidBodies > 0 || hasDirectionalAtom) { |
| 70 |
storageLayout_ |= DataStorage::dslAmat; |
| 71 |
} |
| 72 |
if (hasMultipole) { |
| 73 |
storageLayout_ |= DataStorage::dslElectroFrame; |
| 74 |
if (nRigidBodies > 0) { |
| 75 |
storageLayout_ |= DataStorage::dslAngularMomentum; |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
bsMan_ = new BlockSnapshotManager(info, dumpFilename_, storageLayout_); |
| 80 |
info_->setSnapshotManager(bsMan_); |
| 81 |
|
| 82 |
//if selection is static, we only need to evaluate it once |
| 83 |
if (!evaluator1_.isDynamic()) { |
| 84 |
seleMan1_.setSelectionSet(evaluator1_.evaluate()); |
| 85 |
validateSelection(seleMan1_); |
| 86 |
} else { |
| 87 |
sprintf(painCave.errMsg, |
| 88 |
"TimeCorrFunc Error: dynamic selection is not supported\n"); |
| 89 |
painCave.isFatal = 1; |
| 90 |
simError(); |
| 91 |
} |
| 92 |
|
| 93 |
if (!evaluator2_.isDynamic()) { |
| 94 |
seleMan2_.setSelectionSet(evaluator2_.evaluate()); |
| 95 |
validateSelection(seleMan2_); |
| 96 |
} else { |
| 97 |
sprintf(painCave.errMsg, |
| 98 |
"TimeCorrFunc Error: dynamic selection is not supported\n"); |
| 99 |
painCave.isFatal = 1; |
| 100 |
simError(); |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
/**@todo Fixed Me */ |
| 106 |
Globals* simParams = info_->getSimParams(); |
| 107 |
if (simParams->haveSampleTime()){ |
| 108 |
deltaTime_ = simParams->getSampleTime(); |
| 109 |
} else { |
| 110 |
sprintf(painCave.errMsg, |
| 111 |
"TimeCorrFunc::writeCorrelate Error: can not figure out deltaTime\n"); |
| 112 |
painCave.isFatal = 1; |
| 113 |
simError(); |
| 114 |
} |
| 115 |
|
| 116 |
int nframes = bsMan_->getNFrames(); |
| 117 |
nTimeBins_ = nframes; |
| 118 |
histogram_.resize(nTimeBins_); |
| 119 |
count_.resize(nTimeBins_); |
| 120 |
time_.resize(nTimeBins_); |
| 121 |
|
| 122 |
for (int i = 0; i < nTimeBins_; ++i) { |
| 123 |
time_[i] = i * deltaTime_; |
| 124 |
} |
| 125 |
|
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
void TimeCorrFunc::doCorrelate() { |
| 130 |
preCorrelate(); |
| 131 |
|
| 132 |
int nblocks = bsMan_->getNBlocks(); |
| 133 |
|
| 134 |
for (int i = 0; i < nblocks; ++i) { |
| 135 |
bsMan_->loadBlock(i); |
| 136 |
for (int j = i; j < nblocks; ++j) { |
| 137 |
bsMan_->loadBlock(j); |
| 138 |
correlateBlocks(i, j); |
| 139 |
|
| 140 |
if (i != j) { |
| 141 |
//if i equals to j, the block is still used, should not unload it |
| 142 |
bsMan_->unloadBlock(j); |
| 143 |
} |
| 144 |
} |
| 145 |
bsMan_->unloadBlock(i); |
| 146 |
} |
| 147 |
|
| 148 |
postCorrelate(); |
| 149 |
|
| 150 |
writeCorrelate(); |
| 151 |
} |
| 152 |
|
| 153 |
void TimeCorrFunc::correlateBlocks(int block1, int block2) { |
| 154 |
|
| 155 |
int jstart, jend; |
| 156 |
|
| 157 |
assert(bsMan_->isBlockActive(block1) && bsMan_->isBlockActive(block2)); |
| 158 |
|
| 159 |
SnapshotBlock snapshotBlock1 = bsMan_->getSnapshotBlock(block1); |
| 160 |
SnapshotBlock snapshotBlock2 = bsMan_->getSnapshotBlock(block2); |
| 161 |
|
| 162 |
jend = snapshotBlock2.second; |
| 163 |
|
| 164 |
for (int i = snapshotBlock1.first; i < snapshotBlock1.second; ++i) { |
| 165 |
|
| 166 |
//evaluate selection if it is dynamic |
| 167 |
if (evaluator1_.isDynamic()) { |
| 168 |
seleMan1_.setSelectionSet(evaluator1_.evaluate()); |
| 169 |
validateSelection(seleMan1_); |
| 170 |
} |
| 171 |
|
| 172 |
//update the position or velocity of the atoms belong to rigid bodies |
| 173 |
updateFrame(i); |
| 174 |
|
| 175 |
// if the two blocks are the same, we don't want to correlate |
| 176 |
// backwards in time, so start j at the same frame as i |
| 177 |
|
| 178 |
if (block1 == block2) |
| 179 |
jstart = i; |
| 180 |
else |
| 181 |
jstart = snapshotBlock2.first; |
| 182 |
|
| 183 |
for(int j = jstart; j < jend; ++j) { |
| 184 |
//evaluate selection |
| 185 |
if (!evaluator2_.isDynamic()) { |
| 186 |
seleMan2_.setSelectionSet(evaluator2_.evaluate()); |
| 187 |
validateSelection(seleMan2_); |
| 188 |
} |
| 189 |
//update the position or velocity of the atoms belong to rigid bodies |
| 190 |
updateFrame(j); |
| 191 |
|
| 192 |
correlateFrames(i, j); |
| 193 |
} |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
void TimeCorrFunc::updateFrame(int frame){ |
| 198 |
Molecule* mol; |
| 199 |
RigidBody* rb; |
| 200 |
SimInfo::MoleculeIterator mi; |
| 201 |
Molecule::RigidBodyIterator rbIter; |
| 202 |
|
| 203 |
/** @todo need improvement */ |
| 204 |
if (storageLayout_ & DataStorage::dslPosition) { |
| 205 |
for (mol = info_->beginMolecule(mi); mol != NULL; mol = info_->nextMolecule(mi)) { |
| 206 |
|
| 207 |
//change the positions of atoms which belong to the rigidbodies |
| 208 |
for (rb = mol->beginRigidBody(rbIter); rb != NULL; rb = mol->nextRigidBody(rbIter)) { |
| 209 |
rb->updateAtoms(frame); |
| 210 |
} |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
if (storageLayout_ & DataStorage::dslVelocity) { |
| 215 |
for (mol = info_->beginMolecule(mi); mol != NULL; mol = info_->nextMolecule(mi)) { |
| 216 |
|
| 217 |
//change the positions of atoms which belong to the rigidbodies |
| 218 |
for (rb = mol->beginRigidBody(rbIter); rb != NULL; rb = mol->nextRigidBody(rbIter)) { |
| 219 |
rb->updateAtomVel(frame); |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
} |
| 224 |
|
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
void TimeCorrFunc::preCorrelate() { |
| 229 |
std::fill(histogram_.begin(), histogram_.end(), 0.0); |
| 230 |
std::fill(count_.begin(), count_.end(), 0); |
| 231 |
} |
| 232 |
|
| 233 |
void TimeCorrFunc::postCorrelate() { |
| 234 |
for (int i =0 ; i < nTimeBins_; ++i) { |
| 235 |
if (count_[i] > 0) { |
| 236 |
histogram_[i] /= count_[i]; |
| 237 |
} |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
|
| 242 |
void TimeCorrFunc::writeCorrelate() { |
| 243 |
std::ofstream ofs(outputFilename_.c_str()); |
| 244 |
|
| 245 |
if (ofs.is_open()) { |
| 246 |
|
| 247 |
ofs << "#" << getCorrFuncType() << "\n"; |
| 248 |
ofs << "#selection script1: \"" << selectionScript1_ <<"\"\tselection script2: \"" << selectionScript2_ << "\"\n"; |
| 249 |
ofs << "#extra information: " << extra_ << "\n"; |
| 250 |
ofs << "#time\tcorrVal\n"; |
| 251 |
|
| 252 |
for (int i = 0; i < nTimeBins_; ++i) { |
| 253 |
ofs << time_[i] << "\t" << histogram_[i] << "\n"; |
| 254 |
} |
| 255 |
|
| 256 |
} else { |
| 257 |
sprintf(painCave.errMsg, |
| 258 |
"TimeCorrFunc::writeCorrelate Error: fail to open %s\n", outputFilename_.c_str()); |
| 259 |
painCave.isFatal = 1; |
| 260 |
simError(); |
| 261 |
} |
| 262 |
|
| 263 |
ofs.close(); |
| 264 |
} |
| 265 |
|
| 266 |
} |