| 1 | /* Copyright (c) 2006, 2009, 2010 The University of Notre Dame. All Rights Reserved. | 
| 2 | * | 
| 3 | * The University of Notre Dame grants you ("Licensee") a | 
| 4 | * non-exclusive, royalty free, license to use, modify and | 
| 5 | * redistribute this software in source and binary code form, provided | 
| 6 | * that the following conditions are met: | 
| 7 | * | 
| 8 | * 1. Redistributions of source code must retain the above copyright | 
| 9 | *    notice, this list of conditions and the following disclaimer. | 
| 10 | * | 
| 11 | * 2. Redistributions in binary form must reproduce the above copyright | 
| 12 | *    notice, this list of conditions and the following disclaimer in the | 
| 13 | *    documentation and/or other materials provided with the | 
| 14 | *    distribution. | 
| 15 | * | 
| 16 | * This software is provided "AS IS," without a warranty of any | 
| 17 | * kind. All express or implied conditions, representations and | 
| 18 | * warranties, including any implied warranty of merchantability, | 
| 19 | * fitness for a particular purpose or non-infringement, are hereby | 
| 20 | * excluded.  The University of Notre Dame and its licensors shall not | 
| 21 | * be liable for any damages suffered by licensee as a result of | 
| 22 | * using, modifying or distributing the software or its | 
| 23 | * derivatives. In no event will the University of Notre Dame or its | 
| 24 | * licensors be liable for any lost revenue, profit or data, or for | 
| 25 | * direct, indirect, special, consequential, incidental or punitive | 
| 26 | * damages, however caused and regardless of the theory of liability, | 
| 27 | * arising out of the use of or inability to use software, even if the | 
| 28 | * University of Notre Dame has been advised of the possibility of | 
| 29 | * such damages. | 
| 30 | * | 
| 31 | * SUPPORT OPEN SCIENCE!  If you use OpenMD or its source code in your | 
| 32 | * research, please cite the appropriate papers when you publish your | 
| 33 | * work.  Good starting points are: | 
| 34 | * | 
| 35 | * [1]  Meineke, et al., J. Comp. Chem. 26, 252-271 (2005). | 
| 36 | * [2]  Fennell & Gezelter, J. Chem. Phys. 124, 234104 (2006). | 
| 37 | * [3]  Sun, Lin & Gezelter, J. Chem. Phys. 128, 234107 (2008). | 
| 38 | * [4]  Kuang & Gezelter,  J. Chem. Phys. 133, 164101 (2010). | 
| 39 | * [5]  Vardeman, Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011). | 
| 40 | */ | 
| 41 |  | 
| 42 | #include "applications/staticProps/NanoLength.hpp" | 
| 43 | #include "utils/simError.h" | 
| 44 | #include "io/DumpReader.hpp" | 
| 45 | #include "primitives/Molecule.hpp" | 
| 46 | #include "utils/NumericConstant.hpp" | 
| 47 |  | 
| 48 | using namespace OpenMD; | 
| 49 |  | 
| 50 | bool pairComparator( const evIndex& l, const evIndex& r) { | 
| 51 | return l.first < r.first; | 
| 52 | } | 
| 53 |  | 
| 54 | NanoLength::NanoLength(SimInfo* info, | 
| 55 | const std::string& filename, | 
| 56 | const std::string& sele) | 
| 57 | : StaticAnalyser(info, filename), selectionScript_(sele), seleMan_(info), | 
| 58 | evaluator_(info) { | 
| 59 |  | 
| 60 | setOutputName(getPrefix(filename) + ".length"); | 
| 61 |  | 
| 62 | osq.open(getOutputFileName().c_str()); | 
| 63 |  | 
| 64 | evaluator_.loadScriptString(sele); | 
| 65 | if (!evaluator_.isDynamic()) { | 
| 66 | seleMan_.setSelectionSet(evaluator_.evaluate()); | 
| 67 | } | 
| 68 | frameCounter_ = 0; | 
| 69 | } | 
| 70 |  | 
| 71 | void NanoLength::process() { | 
| 72 | Molecule* mol; | 
| 73 | RigidBody* rb; | 
| 74 | SimInfo::MoleculeIterator mi; | 
| 75 | Molecule::RigidBodyIterator rbIter; | 
| 76 | StuntDouble* sd; | 
| 77 | Vector3d vec; | 
| 78 | int i; | 
| 79 |  | 
| 80 | DumpReader reader(info_, dumpFilename_); | 
| 81 | int nFrames = reader.getNFrames(); | 
| 82 | frameCounter_ = 0; | 
| 83 |  | 
| 84 | theAtoms_.reserve(info_->getNGlobalAtoms()); | 
| 85 |  | 
| 86 | for (int istep = 0; istep < nFrames; istep += step_) { | 
| 87 | reader.readFrame(istep); | 
| 88 | frameCounter_++; | 
| 89 | currentSnapshot_ = info_->getSnapshotManager()->getCurrentSnapshot(); | 
| 90 | RealType time = currentSnapshot_->getTime(); | 
| 91 |  | 
| 92 | // Clear pos vector between each frame. | 
| 93 | theAtoms_.clear(); | 
| 94 |  | 
| 95 | if (evaluator_.isDynamic()) { | 
| 96 | seleMan_.setSelectionSet(evaluator_.evaluate()); | 
| 97 | } | 
| 98 |  | 
| 99 | // update the positions of atoms which belong to the rigidbodies | 
| 100 |  | 
| 101 | for (mol = info_->beginMolecule(mi); mol != NULL; | 
| 102 | mol = info_->nextMolecule(mi)) { | 
| 103 | for (rb = mol->beginRigidBody(rbIter); rb != NULL; | 
| 104 | rb = mol->nextRigidBody(rbIter)) { | 
| 105 | rb->updateAtoms(); | 
| 106 | } | 
| 107 | } | 
| 108 |  | 
| 109 | // outer loop is over the selected StuntDoubles: | 
| 110 |  | 
| 111 | for (sd = seleMan_.beginSelected(i); sd != NULL; | 
| 112 | sd = seleMan_.nextSelected(i)) { | 
| 113 | theAtoms_.push_back(sd); | 
| 114 | } | 
| 115 |  | 
| 116 | RealType rodLength = getLength(theAtoms_); | 
| 117 |  | 
| 118 | osq.precision(7); | 
| 119 | if (osq.is_open()){ | 
| 120 | osq << time << "\t" << rodLength << std::endl; | 
| 121 | } | 
| 122 | } | 
| 123 | osq.close(); | 
| 124 | } | 
| 125 |  | 
| 126 | RealType NanoLength::getLength(std::vector<StuntDouble*> atoms) { | 
| 127 | Vector3d COM(0.0); | 
| 128 | RealType mass = 0.0; | 
| 129 | RealType mtmp; | 
| 130 | for (std::vector<StuntDouble*>::iterator i = atoms.begin(); | 
| 131 | i != atoms.end(); ++i) { | 
| 132 | mtmp = (*i)->getMass(); | 
| 133 | mass += mtmp; | 
| 134 | COM += (*i)->getPos() * mtmp; | 
| 135 | } | 
| 136 | COM /= mass; | 
| 137 |  | 
| 138 | // Moment of Inertia calculation | 
| 139 | Mat3x3d Itmp(0.0); | 
| 140 | for (std::vector<StuntDouble*>::iterator i = atoms.begin(); | 
| 141 | i != atoms.end(); ++i) { | 
| 142 |  | 
| 143 | Mat3x3d IAtom(0.0); | 
| 144 | mtmp = (*i)->getMass(); | 
| 145 | Vector3d delta = (*i)->getPos() - COM; | 
| 146 | IAtom -= outProduct(delta, delta) * mtmp; | 
| 147 | RealType r2 = delta.lengthSquare(); | 
| 148 | IAtom(0, 0) += mtmp * r2; | 
| 149 | IAtom(1, 1) += mtmp * r2; | 
| 150 | IAtom(2, 2) += mtmp * r2; | 
| 151 | Itmp += IAtom; | 
| 152 | } | 
| 153 |  | 
| 154 | //diagonalize | 
| 155 | Vector3d evals; | 
| 156 | Mat3x3d evects; | 
| 157 | Mat3x3d::diagonalize(Itmp, evals, evects); | 
| 158 |  | 
| 159 | // we need to re-order the axes so that the smallest moment of | 
| 160 | // inertia (which corresponds to the long axis of the rod) is | 
| 161 | // along the z-axis. We'll just reverse the order of the three | 
| 162 | // axes.  Python has an argsort function, but we had to invent our | 
| 163 | // own: | 
| 164 |  | 
| 165 | std::vector<evIndex> evals_prime; | 
| 166 | for (int i = 0; i < 3; i++) | 
| 167 | evals_prime.push_back(std::make_pair(evals[i], i)); | 
| 168 | std::sort(evals_prime.begin(), evals_prime.end(), pairComparator); | 
| 169 |  | 
| 170 | RotMat3x3d A; | 
| 171 | Mat3x3d I; | 
| 172 |  | 
| 173 | for (int i = 0; i < 3; i++) { | 
| 174 | int index = evals_prime[2-i].second; | 
| 175 | A.setColumn(i, evects.getColumn(index)); | 
| 176 | I(i,i) = evals[index]; | 
| 177 | } | 
| 178 |  | 
| 179 | // now project the delta from the center of mass onto the long | 
| 180 | // axis of the object | 
| 181 |  | 
| 182 | Vector3d longAxis = A.getColumn(2); | 
| 183 | RealType axisLength = longAxis.length(); | 
| 184 | RealType projmin = 0.0; | 
| 185 | RealType projmax = 0.0; | 
| 186 |  | 
| 187 | for (std::vector<StuntDouble*>::iterator i = atoms.begin(); | 
| 188 | i != atoms.end(); ++i) { | 
| 189 | Vector3d delta = (*i)->getPos() - COM; | 
| 190 | RealType projection = dot(delta, longAxis) / axisLength; | 
| 191 | if (projection > projmax) projmax = projection; | 
| 192 | if (projection < projmin) projmin = projection; | 
| 193 | } | 
| 194 |  | 
| 195 | return projmax - projmin; | 
| 196 | } | 
| 197 |  | 
| 198 |  |