| 1 |
gezelter |
1585 |
/* 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, 24107 (2008). |
| 38 |
|
|
* [4] Vardeman & Gezelter, in progress (2009). |
| 39 |
|
|
*/ |
| 40 |
|
|
|
| 41 |
|
|
#include "applications/staticProps/NanoLength.hpp" |
| 42 |
|
|
#include "utils/simError.h" |
| 43 |
|
|
#include "io/DumpReader.hpp" |
| 44 |
|
|
#include "primitives/Molecule.hpp" |
| 45 |
|
|
#include "utils/NumericConstant.hpp" |
| 46 |
|
|
|
| 47 |
|
|
using namespace OpenMD; |
| 48 |
|
|
|
| 49 |
|
|
bool pairComparator( const evIndex& l, const evIndex& r) { |
| 50 |
|
|
return l.first < r.first; |
| 51 |
|
|
} |
| 52 |
|
|
|
| 53 |
|
|
NanoLength::NanoLength(SimInfo* info, |
| 54 |
|
|
const std::string& filename, |
| 55 |
|
|
const std::string& sele) |
| 56 |
|
|
: StaticAnalyser(info, filename), selectionScript_(sele), evaluator_(info), seleMan_(info) { |
| 57 |
|
|
setOutputName(getPrefix(filename) + ".length"); |
| 58 |
|
|
|
| 59 |
|
|
osq.open(getOutputFileName().c_str()); |
| 60 |
|
|
|
| 61 |
|
|
evaluator_.loadScriptString(sele); |
| 62 |
|
|
if (!evaluator_.isDynamic()) { |
| 63 |
|
|
seleMan_.setSelectionSet(evaluator_.evaluate()); |
| 64 |
|
|
} |
| 65 |
|
|
frameCounter_ = 0; |
| 66 |
|
|
} |
| 67 |
|
|
|
| 68 |
|
|
void NanoLength::process() { |
| 69 |
|
|
Molecule* mol; |
| 70 |
|
|
Atom* atom; |
| 71 |
|
|
RigidBody* rb; |
| 72 |
|
|
int myIndex; |
| 73 |
|
|
SimInfo::MoleculeIterator mi; |
| 74 |
|
|
Molecule::RigidBodyIterator rbIter; |
| 75 |
|
|
Molecule::AtomIterator ai; |
| 76 |
|
|
StuntDouble* sd; |
| 77 |
|
|
Vector3d vec; |
| 78 |
|
|
int i,j; |
| 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 |
|
|
} |
| 124 |
|
|
|
| 125 |
|
|
RealType NanoLength::getLength(std::vector<StuntDouble*> atoms) { |
| 126 |
|
|
Vector3d COM(0.0); |
| 127 |
|
|
RealType mass = 0.0; |
| 128 |
|
|
RealType mtmp; |
| 129 |
|
|
for (std::vector<StuntDouble*>::iterator i = atoms.begin(); |
| 130 |
|
|
i != atoms.end(); ++i) { |
| 131 |
|
|
mtmp = (*i)->getMass(); |
| 132 |
|
|
mass += mtmp; |
| 133 |
|
|
COM += (*i)->getPos() * mtmp; |
| 134 |
|
|
} |
| 135 |
|
|
COM /= mass; |
| 136 |
|
|
|
| 137 |
|
|
// Moment of Inertia calculation |
| 138 |
|
|
Mat3x3d Itmp(0.0); |
| 139 |
|
|
for (std::vector<StuntDouble*>::iterator i = atoms.begin(); |
| 140 |
|
|
i != atoms.end(); ++i) { |
| 141 |
|
|
|
| 142 |
|
|
Mat3x3d IAtom(0.0); |
| 143 |
|
|
mtmp = (*i)->getMass(); |
| 144 |
|
|
Vector3d delta = (*i)->getPos() - COM; |
| 145 |
|
|
IAtom -= outProduct(delta, delta) * mtmp; |
| 146 |
|
|
RealType r2 = delta.lengthSquare(); |
| 147 |
|
|
IAtom(0, 0) += mtmp * r2; |
| 148 |
|
|
IAtom(1, 1) += mtmp * r2; |
| 149 |
|
|
IAtom(2, 2) += mtmp * r2; |
| 150 |
|
|
Itmp += IAtom; |
| 151 |
|
|
} |
| 152 |
|
|
|
| 153 |
|
|
//diagonalize |
| 154 |
|
|
Vector3d evals; |
| 155 |
|
|
Mat3x3d evects; |
| 156 |
|
|
Mat3x3d::diagonalize(Itmp, evals, evects); |
| 157 |
|
|
|
| 158 |
|
|
// we need to re-order the axes so that the smallest moment of |
| 159 |
|
|
// inertia (which corresponds to the long axis of the rod) is |
| 160 |
|
|
// along the z-axis. We'll just reverse the order of the three |
| 161 |
|
|
// axes. Python has an argsort function, but we had to invent our |
| 162 |
|
|
// own: |
| 163 |
|
|
|
| 164 |
|
|
std::vector<evIndex> evals_prime; |
| 165 |
|
|
for (int i = 0; i < 3; i++) |
| 166 |
|
|
evals_prime.push_back(std::make_pair(evals[i], i)); |
| 167 |
|
|
std::sort(evals_prime.begin(), evals_prime.end(), pairComparator); |
| 168 |
|
|
|
| 169 |
|
|
RotMat3x3d A; |
| 170 |
|
|
Mat3x3d I; |
| 171 |
|
|
|
| 172 |
|
|
for (int i = 0; i < 3; i++) { |
| 173 |
|
|
int index = evals_prime[2-i].second; |
| 174 |
|
|
A.setColumn(i, evects.getColumn(index)); |
| 175 |
|
|
I(i,i) = evals[index]; |
| 176 |
|
|
} |
| 177 |
|
|
|
| 178 |
|
|
// now project the delta from the center of mass onto the long |
| 179 |
|
|
// axis of the object |
| 180 |
|
|
|
| 181 |
|
|
Vector3d longAxis = A.getColumn(2); |
| 182 |
|
|
RealType axisLength = longAxis.length(); |
| 183 |
|
|
RealType projmin = 0.0; |
| 184 |
|
|
RealType projmax = 0.0; |
| 185 |
|
|
|
| 186 |
|
|
for (std::vector<StuntDouble*>::iterator i = atoms.begin(); |
| 187 |
|
|
i != atoms.end(); ++i) { |
| 188 |
|
|
Vector3d delta = (*i)->getPos() - COM; |
| 189 |
|
|
RealType projection = dot(delta, longAxis) / axisLength; |
| 190 |
|
|
if (projection > projmax) projmax = projection; |
| 191 |
|
|
if (projection < projmin) projmin = projection; |
| 192 |
|
|
} |
| 193 |
|
|
|
| 194 |
|
|
return projmax - projmin; |
| 195 |
|
|
} |
| 196 |
|
|
|
| 197 |
|
|
|