| 1 | gezelter | 1078 | /* | 
| 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 | gezelter | 1390 | * 1. Redistributions of source code must retain the above copyright | 
| 10 | gezelter | 1078 | *    notice, this list of conditions and the following disclaimer. | 
| 11 |  |  | * | 
| 12 | gezelter | 1390 | * 2. Redistributions in binary form must reproduce the above copyright | 
| 13 | gezelter | 1078 | *    notice, this list of conditions and the following disclaimer in the | 
| 14 |  |  | *    documentation and/or other materials provided with the | 
| 15 |  |  | *    distribution. | 
| 16 |  |  | * | 
| 17 |  |  | * This software is provided "AS IS," without a warranty of any | 
| 18 |  |  | * kind. All express or implied conditions, representations and | 
| 19 |  |  | * warranties, including any implied warranty of merchantability, | 
| 20 |  |  | * fitness for a particular purpose or non-infringement, are hereby | 
| 21 |  |  | * excluded.  The University of Notre Dame and its licensors shall not | 
| 22 |  |  | * be liable for any damages suffered by licensee as a result of | 
| 23 |  |  | * using, modifying or distributing the software or its | 
| 24 |  |  | * derivatives. In no event will the University of Notre Dame or its | 
| 25 |  |  | * licensors be liable for any lost revenue, profit or data, or for | 
| 26 |  |  | * direct, indirect, special, consequential, incidental or punitive | 
| 27 |  |  | * damages, however caused and regardless of the theory of liability, | 
| 28 |  |  | * arising out of the use of or inability to use software, even if the | 
| 29 |  |  | * University of Notre Dame has been advised of the possibility of | 
| 30 |  |  | * such damages. | 
| 31 | gezelter | 1390 | * | 
| 32 |  |  | * SUPPORT OPEN SCIENCE!  If you use OpenMD or its source code in your | 
| 33 |  |  | * research, please cite the appropriate papers when you publish your | 
| 34 |  |  | * work.  Good starting points are: | 
| 35 |  |  | * | 
| 36 |  |  | * [1]  Meineke, et al., J. Comp. Chem. 26, 252-271 (2005). | 
| 37 |  |  | * [2]  Fennell & Gezelter, J. Chem. Phys. 124, 234104 (2006). | 
| 38 |  |  | * [3]  Sun, Lin & Gezelter, J. Chem. Phys. 128, 24107 (2008). | 
| 39 | gezelter | 1782 | * [4]  Kuang & Gezelter,  J. Chem. Phys. 133, 164101 (2010). | 
| 40 |  |  | * [5]  Vardeman, Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011). | 
| 41 | gezelter | 1078 | */ | 
| 42 |  |  |  | 
| 43 |  |  | #include "applications/staticProps/SCDOrderParameter.hpp" | 
| 44 |  |  | #include "utils/simError.h" | 
| 45 |  |  | #include "io/DumpReader.hpp" | 
| 46 |  |  | #include "primitives/Molecule.hpp" | 
| 47 |  |  | #include "utils/NumericConstant.hpp" | 
| 48 | gezelter | 1390 | namespace OpenMD { | 
| 49 | gezelter | 1078 |  | 
| 50 |  |  | SCDElem::SCDElem(SimInfo* info, const std::string& sele1, | 
| 51 |  |  | const std::string& sele2, const std::string& sele3) : sele1_(sele1), sele2_(sele2), sele3_(sele3){ | 
| 52 |  |  |  | 
| 53 |  |  | usePeriodicBoundaryConditions_ = info->getSimParams()->getUsePeriodicBoundaryConditions(); | 
| 54 |  |  |  | 
| 55 |  |  | SelectionManager seleMan1_(info); | 
| 56 |  |  | SelectionManager seleMan2_(info); | 
| 57 |  |  | SelectionManager seleMan3_(info); | 
| 58 |  |  | SelectionEvaluator evaluator1_(info); | 
| 59 |  |  | SelectionEvaluator evaluator2_(info); | 
| 60 |  |  | SelectionEvaluator evaluator3_(info); | 
| 61 |  |  |  | 
| 62 |  |  | evaluator1_.loadScriptString(sele1_); | 
| 63 |  |  | evaluator2_.loadScriptString(sele2_); | 
| 64 |  |  | evaluator3_.loadScriptString(sele3_); | 
| 65 |  |  |  | 
| 66 |  |  | if (!evaluator1_.isDynamic()) { | 
| 67 |  |  | seleMan1_.setSelectionSet(evaluator1_.evaluate()); | 
| 68 |  |  | }else { | 
| 69 |  |  | sprintf( painCave.errMsg, | 
| 70 |  |  | "dynamic selection is not allowed\n"); | 
| 71 | gezelter | 1390 | painCave.severity = OPENMD_ERROR; | 
| 72 | gezelter | 1078 | painCave.isFatal = 1; | 
| 73 |  |  | simError(); | 
| 74 |  |  | } | 
| 75 |  |  |  | 
| 76 |  |  | if (!evaluator2_.isDynamic()) { | 
| 77 |  |  | seleMan2_.setSelectionSet(evaluator2_.evaluate()); | 
| 78 |  |  | }else { | 
| 79 |  |  | sprintf( painCave.errMsg, | 
| 80 |  |  | "dynamic selection is not allowed\n"); | 
| 81 | gezelter | 1390 | painCave.severity = OPENMD_ERROR; | 
| 82 | gezelter | 1078 | painCave.isFatal = 1; | 
| 83 |  |  | simError(); | 
| 84 |  |  | } | 
| 85 |  |  |  | 
| 86 |  |  | if (!evaluator3_.isDynamic()) { | 
| 87 |  |  | seleMan3_.setSelectionSet(evaluator3_.evaluate()); | 
| 88 |  |  | }else { | 
| 89 |  |  | sprintf( painCave.errMsg, | 
| 90 |  |  | "dynamic selection is not allowed\n"); | 
| 91 | gezelter | 1390 | painCave.severity = OPENMD_ERROR; | 
| 92 | gezelter | 1078 | painCave.isFatal = 1; | 
| 93 |  |  | simError(); | 
| 94 |  |  | } | 
| 95 |  |  |  | 
| 96 |  |  | int nselected1 = seleMan1_.getSelectionCount(); | 
| 97 |  |  | int nselected2 = seleMan2_.getSelectionCount(); | 
| 98 |  |  | int nselected3 = seleMan3_.getSelectionCount(); | 
| 99 |  |  |  | 
| 100 |  |  | if ( nselected1 != nselected2 || nselected1 != nselected3 ) { | 
| 101 |  |  | sprintf( painCave.errMsg, | 
| 102 |  |  | "The number of selected Stuntdoubles must be the same\n"); | 
| 103 | gezelter | 1390 | painCave.severity = OPENMD_ERROR; | 
| 104 | gezelter | 1078 | painCave.isFatal = 1; | 
| 105 |  |  | simError(); | 
| 106 |  |  | } | 
| 107 |  |  |  | 
| 108 |  |  | int i; | 
| 109 |  |  | int j; | 
| 110 |  |  | int k; | 
| 111 |  |  | StuntDouble* sd1; | 
| 112 |  |  | StuntDouble* sd2; | 
| 113 |  |  | StuntDouble* sd3; | 
| 114 |  |  | for (sd1 = seleMan1_.beginSelected(i), sd2 = seleMan2_.beginSelected(j), sd3 = seleMan3_.beginSelected(k); | 
| 115 |  |  | sd1 != NULL && sd2 != NULL && sd3 != NULL; | 
| 116 |  |  | sd1 = seleMan1_.nextSelected(i), sd2 = seleMan2_.nextSelected(j), sd3 = seleMan3_.nextSelected(k)) { | 
| 117 |  |  | tuples_.push_back(make_tuple3(sd1, sd2, sd3)); | 
| 118 |  |  | } | 
| 119 |  |  |  | 
| 120 |  |  | } | 
| 121 |  |  |  | 
| 122 |  |  | RealType SCDElem::calcSCD(Snapshot* snapshot) { | 
| 123 |  |  | std::vector<SDTuple3>::iterator i; | 
| 124 |  |  | Vector3d normal(0.0, 0.0, 1.0); | 
| 125 |  |  | RealType scd = 0.0; | 
| 126 |  |  | for (i = tuples_.begin(); i != tuples_.end(); ++i) { | 
| 127 |  |  | //Egberts B. and Berendsen H.J.C, J.Chem.Phys. 89(6), 3718-3732, 1988 | 
| 128 |  |  |  | 
| 129 |  |  | Vector3d zAxis =   i->third->getPos() - i->first->getPos(); | 
| 130 |  |  | if (usePeriodicBoundaryConditions_) | 
| 131 |  |  | snapshot->wrapVector(zAxis); | 
| 132 |  |  | Vector3d v12 =  i->second->getPos() - i->first->getPos(); | 
| 133 |  |  | if (usePeriodicBoundaryConditions_) | 
| 134 |  |  | snapshot->wrapVector(v12); | 
| 135 |  |  | Vector3d xAxis = cross(v12, zAxis); | 
| 136 |  |  | Vector3d yAxis = cross(zAxis, xAxis); | 
| 137 |  |  |  | 
| 138 |  |  | xAxis.normalize(); | 
| 139 |  |  | yAxis.normalize(); | 
| 140 |  |  | zAxis.normalize(); | 
| 141 |  |  | RealType cosThetaX = dot(xAxis, normal); | 
| 142 |  |  | RealType sxx = 0.5*(3*cosThetaX * cosThetaX - 1.0); | 
| 143 |  |  | RealType cosThetaY = dot(yAxis, normal); | 
| 144 |  |  | RealType syy = 0.5*(3*cosThetaY * cosThetaY - 1.0); | 
| 145 |  |  | scd += 2.0/3.0*sxx + 1.0/3.0*syy; | 
| 146 |  |  | } | 
| 147 |  |  | scd /= tuples_.size(); | 
| 148 |  |  | return scd; | 
| 149 |  |  |  | 
| 150 |  |  | } | 
| 151 |  |  |  | 
| 152 |  |  | SCDOrderParameter::SCDOrderParameter(SimInfo* info, const std::string& filename, | 
| 153 |  |  | const std::string& sele1, const std::string& sele2, const std::string& sele3) | 
| 154 |  |  | : StaticAnalyser(info, filename) { | 
| 155 |  |  |  | 
| 156 |  |  | setOutputName(getPrefix(filename) + ".scd"); | 
| 157 |  |  |  | 
| 158 |  |  | scdElems_.push_back(SCDElem(info, sele1, sele2, sele3)); | 
| 159 |  |  | scdParam_.resize(scdElems_.size()); | 
| 160 |  |  | std::fill(scdParam_.begin(), scdParam_.end(), 0.0); | 
| 161 |  |  |  | 
| 162 |  |  | } | 
| 163 |  |  |  | 
| 164 |  |  | SCDOrderParameter::SCDOrderParameter(SimInfo* info, const std::string& filename, | 
| 165 |  |  | const std::string& molname, int beginIndex, int endIndex) | 
| 166 |  |  | : StaticAnalyser(info, filename) { | 
| 167 |  |  |  | 
| 168 |  |  | setOutputName(getPrefix(filename) + ".scd"); | 
| 169 |  |  |  | 
| 170 |  |  | assert(beginIndex >=0 && endIndex >=0 && beginIndex <= endIndex - 2); | 
| 171 |  |  | for (int i = beginIndex; i <= endIndex -2 ; ++i) { | 
| 172 |  |  | std::string selectionTemplate = "select " + molname + "."; | 
| 173 | gezelter | 1390 | std::string sele1 = selectionTemplate + OpenMD_itoa(i); | 
| 174 |  |  | std::string sele2 = selectionTemplate + OpenMD_itoa(i+1); | 
| 175 |  |  | std::string sele3 = selectionTemplate + OpenMD_itoa(i+2); | 
| 176 | gezelter | 1078 |  | 
| 177 |  |  | scdElems_.push_back(SCDElem(info, sele1, sele2, sele3)); | 
| 178 |  |  | } | 
| 179 |  |  |  | 
| 180 |  |  | scdParam_.resize(scdElems_.size()); | 
| 181 |  |  | std::fill(scdParam_.begin(), scdParam_.end(), 0.0); | 
| 182 |  |  | } | 
| 183 |  |  |  | 
| 184 |  |  |  | 
| 185 |  |  | void SCDOrderParameter::process() { | 
| 186 |  |  | Molecule* mol; | 
| 187 |  |  | RigidBody* rb; | 
| 188 |  |  | SimInfo::MoleculeIterator mi; | 
| 189 |  |  | Molecule::RigidBodyIterator rbIter; | 
| 190 |  |  |  | 
| 191 |  |  | DumpReader reader(info_, dumpFilename_); | 
| 192 |  |  | int nFrames = reader.getNFrames(); | 
| 193 |  |  |  | 
| 194 |  |  | for (int i = 0; i < nFrames; i += step_) { | 
| 195 |  |  | reader.readFrame(i); | 
| 196 |  |  | currentSnapshot_ = info_->getSnapshotManager()->getCurrentSnapshot(); | 
| 197 |  |  |  | 
| 198 |  |  |  | 
| 199 |  |  | for (mol = info_->beginMolecule(mi); mol != NULL; mol = info_->nextMolecule(mi)) { | 
| 200 |  |  | //change the positions of atoms which belong to the rigidbodies | 
| 201 |  |  | for (rb = mol->beginRigidBody(rbIter); rb != NULL; rb = mol->nextRigidBody(rbIter)) { | 
| 202 |  |  | rb->updateAtoms(); | 
| 203 |  |  | } | 
| 204 |  |  |  | 
| 205 |  |  | } | 
| 206 |  |  |  | 
| 207 |  |  | for (std::size_t j = 0; j < scdElems_.size(); ++j) { | 
| 208 |  |  | scdParam_[j] += scdElems_[j].calcSCD(currentSnapshot_); | 
| 209 |  |  | } | 
| 210 |  |  |  | 
| 211 |  |  | } | 
| 212 |  |  |  | 
| 213 |  |  | int nProcessed = nFrames /step_; | 
| 214 |  |  | for (std::size_t j = 0; j < scdElems_.size(); ++j) { | 
| 215 |  |  | scdParam_[j] /= nProcessed; | 
| 216 |  |  | } | 
| 217 |  |  |  | 
| 218 |  |  | writeSCD(); | 
| 219 |  |  |  | 
| 220 |  |  | } | 
| 221 |  |  |  | 
| 222 |  |  | void SCDOrderParameter::writeSCD() { | 
| 223 |  |  |  | 
| 224 |  |  | std::ofstream os(getOutputFileName().c_str()); | 
| 225 |  |  | os << "#scd parameter\n"; | 
| 226 |  |  | for (std::size_t i = 0; i < scdElems_.size(); ++i) { | 
| 227 |  |  | os <<  "#[column " << i+1 << "]\t" | 
| 228 |  |  | <<  "sele1: \"" << scdElems_[i].getSelection1() << "\",\t" | 
| 229 |  |  | <<  "sele2: \"" << scdElems_[i].getSelection2() << "\",\t" | 
| 230 |  |  | <<  "sele3: \"" << scdElems_[i].getSelection3() << "\"\n"; | 
| 231 |  |  | } | 
| 232 |  |  |  | 
| 233 |  |  | for (std::size_t i = 0; i < scdElems_.size(); ++i) { | 
| 234 |  |  | os <<  scdParam_[i]<< "\t"; | 
| 235 |  |  | } | 
| 236 |  |  | os << std::endl; | 
| 237 |  |  | } | 
| 238 |  |  |  | 
| 239 |  |  |  | 
| 240 |  |  | } | 
| 241 |  |  |  | 
| 242 |  |  |  |