| 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/staticProps/RippleOP.hpp" | 
| 43 | #include "utils/simError.h" | 
| 44 | #include "io/DumpReader.hpp" | 
| 45 | #include "primitives/Molecule.hpp" | 
| 46 | #include "utils/NumericConstant.hpp" | 
| 47 | namespace oopse { | 
| 48 |  | 
| 49 |  | 
| 50 | RippleOP::RippleOP(SimInfo* info, const std::string& filename, const std::string& sele1, const std::string& sele2) | 
| 51 | : StaticAnalyser(info, filename), | 
| 52 | selectionScript1_(sele1), selectionScript2_(sele2), evaluator1_(info), evaluator2_(info), | 
| 53 | seleMan1_(info), seleMan2_(info){ | 
| 54 |  | 
| 55 | setOutputName(getPrefix(filename) + ".rp2"); | 
| 56 |  | 
| 57 | evaluator1_.loadScriptString(sele1); | 
| 58 | evaluator2_.loadScriptString(sele2); | 
| 59 |  | 
| 60 | if (!evaluator1_.isDynamic()) { | 
| 61 | seleMan1_.setSelectionSet(evaluator1_.evaluate()); | 
| 62 | }else { | 
| 63 | sprintf( painCave.errMsg, | 
| 64 | "--sele1 must be static selection\n"); | 
| 65 | painCave.severity = OOPSE_ERROR; | 
| 66 | painCave.isFatal = 1; | 
| 67 | simError(); | 
| 68 | } | 
| 69 |  | 
| 70 | if (!evaluator2_.isDynamic()) { | 
| 71 | seleMan2_.setSelectionSet(evaluator2_.evaluate()); | 
| 72 | }else { | 
| 73 | sprintf( painCave.errMsg, | 
| 74 | "--sele2 must be static selection\n"); | 
| 75 | painCave.severity = OOPSE_ERROR; | 
| 76 | painCave.isFatal = 1; | 
| 77 | simError(); | 
| 78 | } | 
| 79 |  | 
| 80 | if (seleMan1_.getSelectionCount() != seleMan2_.getSelectionCount() ) { | 
| 81 | sprintf( painCave.errMsg, | 
| 82 | "The number of selected Stuntdoubles are not the same in --sele1 and sele2\n"); | 
| 83 | painCave.severity = OOPSE_ERROR; | 
| 84 | painCave.isFatal = 1; | 
| 85 | simError(); | 
| 86 |  | 
| 87 | } | 
| 88 |  | 
| 89 | int i; | 
| 90 | int j; | 
| 91 | StuntDouble* sd1; | 
| 92 | StuntDouble* sd2; | 
| 93 | for (sd1 = seleMan1_.beginSelected(i), sd2 = seleMan2_.beginSelected(j); | 
| 94 | sd1 != NULL && sd2 != NULL; | 
| 95 | sd1 = seleMan1_.nextSelected(i), sd2 = seleMan2_.nextSelected(j)) { | 
| 96 |  | 
| 97 | sdPairs_.push_back(std::make_pair(sd1, sd2)); | 
| 98 | } | 
| 99 |  | 
| 100 | } | 
| 101 |  | 
| 102 | void RippleOP::process() { | 
| 103 | Molecule* mol; | 
| 104 | RigidBody* rb; | 
| 105 | SimInfo::MoleculeIterator mi; | 
| 106 | Molecule::RigidBodyIterator rbIter; | 
| 107 |  | 
| 108 | DumpReader reader(info_, dumpFilename_); | 
| 109 | int nFrames = reader.getNFrames(); | 
| 110 |  | 
| 111 | for (int i = 0; i < nFrames; i += step_) { | 
| 112 | reader.readFrame(i); | 
| 113 | currentSnapshot_ = info_->getSnapshotManager()->getCurrentSnapshot(); | 
| 114 |  | 
| 115 |  | 
| 116 | for (mol = info_->beginMolecule(mi); mol != NULL; mol = info_->nextMolecule(mi)) { | 
| 117 | //change the positions of atoms which belong to the rigidbodies | 
| 118 | for (rb = mol->beginRigidBody(rbIter); rb != NULL; rb = mol->nextRigidBody(rbIter)) { | 
| 119 | rb->updateAtoms(); | 
| 120 | } | 
| 121 | } | 
| 122 |  | 
| 123 | Mat3x3d orderTensorHead(0.0), orderTensorTail(0.0); | 
| 124 | for (std::vector<std::pair<StuntDouble*, StuntDouble*> >::iterator j = sdPairs_.begin(); j != sdPairs_.end(); ++j) { | 
| 125 | Vector3d vecHead = j->first->getElectroFrame().getColumn(2); | 
| 126 | Vector3d vecTail = j->second->getA().getColumn(2); | 
| 127 | orderTensorHead +=outProduct(vecHead, vecHead); | 
| 128 | orderTensorTail +=outProduct(vecTail, vecTail); | 
| 129 | } | 
| 130 |  | 
| 131 | orderTensorHead /= sdPairs_.size(); | 
| 132 | orderTensorHead -= (RealType)(1.0/3.0) * Mat3x3d::identity(); | 
| 133 |  | 
| 134 | orderTensorTail /= sdPairs_.size(); | 
| 135 | orderTensorTail -= (RealType)(1.0/3.0) * Mat3x3d::identity(); | 
| 136 |  | 
| 137 | Vector3d eigenvaluesHead, eigenvaluesTail; | 
| 138 | Mat3x3d eigenvectorsHead, eigenvectorsTail; | 
| 139 | Mat3x3d::diagonalize(orderTensorHead, eigenvaluesHead, eigenvectorsHead); | 
| 140 | Mat3x3d::diagonalize(orderTensorTail, eigenvaluesTail, eigenvectorsTail); | 
| 141 |  | 
| 142 | int which; | 
| 143 | RealType maxEval = 0.0; | 
| 144 | for(int k = 0; k< 3; k++){ | 
| 145 | if(fabs(eigenvaluesHead[k]) > maxEval){ | 
| 146 | which = k; | 
| 147 | maxEval = fabs(eigenvaluesHead[k]); | 
| 148 | } | 
| 149 | } | 
| 150 | RealType p2Head = 1.5 * maxEval; | 
| 151 | maxEval = 0.0; | 
| 152 | for(int k = 0; k< 3; k++){ | 
| 153 | if(fabs(eigenvaluesTail[k]) > maxEval){ | 
| 154 | which = k; | 
| 155 | maxEval = fabs(eigenvaluesTail[k]); | 
| 156 | } | 
| 157 | } | 
| 158 | RealType p2Tail = 1.5 * maxEval; | 
| 159 |  | 
| 160 | //the eigen vector is already normalized in SquareMatrix3::diagonalize | 
| 161 | Vector3d directorHead = eigenvectorsHead.getColumn(which); | 
| 162 | if (directorHead[0] < 0) { | 
| 163 | directorHead.negate(); | 
| 164 | } | 
| 165 | Vector3d directorTail = eigenvectorsTail.getColumn(which); | 
| 166 | if (directorTail[0] < 0) { | 
| 167 | directorTail.negate(); | 
| 168 | } | 
| 169 |  | 
| 170 | OrderParam paramHead, paramTail; | 
| 171 | paramHead.p2 = p2Head; | 
| 172 | paramHead.director = directorHead; | 
| 173 | paramTail.p2 = p2Tail; | 
| 174 | paramTail.director = directorTail; | 
| 175 |  | 
| 176 | orderParamsHead_.push_back(paramHead); | 
| 177 | orderParamsTail_.push_back(paramTail); | 
| 178 |  | 
| 179 | } | 
| 180 |  | 
| 181 | OrderParam sumOPHead, errsumOPHead; | 
| 182 | OrderParam sumOPTail, errsumOPTail; | 
| 183 |  | 
| 184 | sumOPHead.p2 = 0.0; | 
| 185 | errsumOPHead.p2 = 0.0; | 
| 186 | for (std::size_t i = 0; i < orderParamsHead_.size(); ++i) { | 
| 187 | sumOPHead.p2 += orderParamsHead_[i].p2; | 
| 188 | sumOPHead.director[0] += orderParamsHead_[i].director[0]; | 
| 189 | sumOPHead.director[1] += orderParamsHead_[i].director[1]; | 
| 190 | sumOPHead.director[2] += orderParamsHead_[i].director[2]; | 
| 191 | } | 
| 192 |  | 
| 193 | avgOPHead.p2 = sumOPHead.p2 / (RealType)orderParamsHead_.size(); | 
| 194 | avgOPHead.director[0] = sumOPHead.director[0] / (RealType)orderParamsHead_.size(); | 
| 195 | avgOPHead.director[1] = sumOPHead.director[1] / (RealType)orderParamsHead_.size(); | 
| 196 | avgOPHead.director[2] = sumOPHead.director[2] / (RealType)orderParamsHead_.size(); | 
| 197 | for (std::size_t i = 0; i < orderParamsHead_.size(); ++i) { | 
| 198 | errsumOPHead.p2 += pow((orderParamsHead_[i].p2 - avgOPHead.p2), 2); | 
| 199 | } | 
| 200 | errOPHead.p2 = sqrt(errsumOPHead.p2 / (RealType)orderParamsHead_.size()); | 
| 201 |  | 
| 202 | sumOPTail.p2 = 0.0; | 
| 203 | errsumOPTail.p2 = 0.0; | 
| 204 | for (std::size_t i = 0; i < orderParamsTail_.size(); ++i) { | 
| 205 | sumOPTail.p2 += orderParamsTail_[i].p2; | 
| 206 | sumOPTail.director[0] += orderParamsTail_[i].director[0]; | 
| 207 | sumOPTail.director[1] += orderParamsTail_[i].director[1]; | 
| 208 | sumOPTail.director[2] += orderParamsTail_[i].director[2]; | 
| 209 | } | 
| 210 |  | 
| 211 | avgOPTail.p2 = sumOPTail.p2 / (RealType)orderParamsTail_.size(); | 
| 212 | avgOPTail.director[0] = sumOPTail.director[0] / (RealType)orderParamsTail_.size(); | 
| 213 | avgOPTail.director[1] = sumOPTail.director[1] / (RealType)orderParamsTail_.size(); | 
| 214 | avgOPTail.director[2] = sumOPTail.director[2] / (RealType)orderParamsTail_.size(); | 
| 215 | for (std::size_t i = 0; i < orderParamsTail_.size(); ++i) { | 
| 216 | errsumOPTail.p2 += pow((orderParamsTail_[i].p2 - avgOPTail.p2), 2); | 
| 217 | } | 
| 218 | errOPTail.p2 = sqrt(errsumOPTail.p2 / (RealType)orderParamsTail_.size()); | 
| 219 |  | 
| 220 | writeP2(); | 
| 221 |  | 
| 222 | } | 
| 223 |  | 
| 224 | void RippleOP::writeP2() { | 
| 225 |  | 
| 226 | std::ofstream os(getOutputFileName().c_str()); | 
| 227 | os<< "#selection1: (" << selectionScript1_ << ")\n"; | 
| 228 | os << "#p2\terror\tdirector_x\tdirector_y\tdiretor_z\n"; | 
| 229 |  | 
| 230 | os <<  avgOPHead.p2 << "\t" | 
| 231 | <<  errOPHead.p2 << "\t" | 
| 232 | <<  avgOPHead.director[0] << "\t" | 
| 233 | <<  avgOPHead.director[1] << "\t" | 
| 234 | <<  avgOPHead.director[2] << "\n"; | 
| 235 |  | 
| 236 | os << "selection2: (" << selectionScript2_ << ")\n"; | 
| 237 | os << "#p2\terror\tdirector_x\tdirector_y\tdiretor_z\n"; | 
| 238 |  | 
| 239 | os <<  avgOPTail.p2 << "\t" | 
| 240 | <<  errOPTail.p2 << "\t" | 
| 241 | <<  avgOPTail.director[0] << "\t" | 
| 242 | <<  avgOPTail.director[1] << "\t" | 
| 243 | <<  avgOPTail.director[2] << "\n"; | 
| 244 | } | 
| 245 |  | 
| 246 | } | 
| 247 |  |