OpenMD 3.2
Molecular Dynamics in the Open
Loading...
Searching...
No Matches
NanoLength.cpp
1/*
2 * Copyright (c) 2004-present, The University of Notre Dame. All rights
3 * reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 *
31 * SUPPORT OPEN SCIENCE! If you use OpenMD or its source code in your
32 * research, please cite the following paper when you publish your work:
33 *
34 * [1] Drisko et al., J. Open Source Softw. 9, 7004 (2024).
35 *
36 * Good starting points for code and simulation methodology are:
37 *
38 * [2] Meineke, et al., J. Comp. Chem. 26, 252-271 (2005).
39 * [3] Fennell & Gezelter, J. Chem. Phys. 124, 234104 (2006).
40 * [4] Sun, Lin & Gezelter, J. Chem. Phys. 128, 234107 (2008).
41 * [5] Vardeman, Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011).
42 * [6] Kuang & Gezelter, Mol. Phys., 110, 691-701 (2012).
43 * [7] Lamichhane, Gezelter & Newman, J. Chem. Phys. 141, 134109 (2014).
44 * [8] Bhattarai, Newman & Gezelter, Phys. Rev. B 99, 094106 (2019).
45 * [9] Drisko & Gezelter, J. Chem. Theory Comput. 20, 4986-4997 (2024).
46 */
47
48#include "applications/staticProps/NanoLength.hpp"
49
50#include "io/DumpReader.hpp"
52#include "utils/simError.h"
53
54using namespace OpenMD;
55
56bool pairComparator(const evIndex& l, const evIndex& r) {
57 return l.first < r.first;
58}
59
60NanoLength::NanoLength(SimInfo* info, const std::string& filename,
61 const std::string& sele) :
62 StaticAnalyser(info, filename, 1),
63 selectionScript_(sele), seleMan_(info), evaluator_(info) {
64 setOutputName(getPrefix(filename) + ".length");
65
66 osq.open(getOutputFileName().c_str());
67
68 evaluator_.loadScriptString(sele);
69 if (!evaluator_.isDynamic()) {
70 seleMan_.setSelectionSet(evaluator_.evaluate());
71 }
72 frameCounter_ = 0;
73}
74
75void NanoLength::process() {
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 // outer loop is over the selected StuntDoubles:
100
101 for (sd = seleMan_.beginSelected(i); sd != NULL;
102 sd = seleMan_.nextSelected(i)) {
103 theAtoms_.push_back(sd);
104 }
105
106 RealType rodLength = getLength(theAtoms_);
107
108 osq.precision(7);
109 if (osq.is_open()) { osq << time << "\t" << rodLength << std::endl; }
110 }
111 osq.close();
112}
113
114RealType NanoLength::getLength(std::vector<StuntDouble*> atoms) {
115 Vector3d COM(0.0);
116 RealType mass = 0.0;
117 RealType mtmp;
118 for (std::vector<StuntDouble*>::iterator i = atoms.begin(); i != atoms.end();
119 ++i) {
120 mtmp = (*i)->getMass();
121 mass += mtmp;
122 COM += (*i)->getPos() * mtmp;
123 }
124 COM /= mass;
125
126 // Moment of Inertia calculation
127 Mat3x3d Itmp(0.0);
128 for (std::vector<StuntDouble*>::iterator i = atoms.begin(); i != atoms.end();
129 ++i) {
130 Mat3x3d IAtom(0.0);
131 mtmp = (*i)->getMass();
132 Vector3d delta = (*i)->getPos() - COM;
133 IAtom -= outProduct(delta, delta) * mtmp;
134 RealType r2 = delta.lengthSquare();
135 IAtom(0, 0) += mtmp * r2;
136 IAtom(1, 1) += mtmp * r2;
137 IAtom(2, 2) += mtmp * r2;
138 Itmp += IAtom;
139 }
140
141 // diagonalize
142 Vector3d evals;
143 Mat3x3d evects;
144 Mat3x3d::diagonalize(Itmp, evals, evects);
145
146 // we need to re-order the axes so that the smallest moment of
147 // inertia (which corresponds to the long axis of the rod) is
148 // along the z-axis. We'll just reverse the order of the three
149 // axes. Python has an argsort function, but we had to invent our
150 // own:
151
152 std::vector<evIndex> evals_prime;
153 for (int i = 0; i < 3; i++)
154 evals_prime.push_back(std::make_pair(evals[i], i));
155 std::sort(evals_prime.begin(), evals_prime.end(), pairComparator);
156
157 RotMat3x3d A;
158 Mat3x3d I;
159
160 for (int i = 0; i < 3; i++) {
161 int index = evals_prime[2 - i].second;
162 A.setColumn(i, evects.getColumn(index));
163 I(i, i) = evals[index];
164 }
165
166 // now project the delta from the center of mass onto the long
167 // axis of the object
168
169 Vector3d longAxis = A.getColumn(2);
170 RealType axisLength = longAxis.length();
171 RealType projmin = 0.0;
172 RealType projmax = 0.0;
173
174 for (std::vector<StuntDouble*>::iterator i = atoms.begin(); i != atoms.end();
175 ++i) {
176 Vector3d delta = (*i)->getPos() - COM;
177 RealType projection = dot(delta, longAxis) / axisLength;
178 if (projection > projmax) projmax = projection;
179 if (projection < projmin) projmin = projection;
180 }
181
182 return projmax - projmin;
183}
void setColumn(unsigned int col, const Vector< Real, Col > &v)
Sets a column of this matrix.
Vector< Real, Col > getColumn(unsigned int col)
Returns a column of this matrix as a vector.
One of the heavy-weight classes of OpenMD, SimInfo maintains objects and variables relating to the cu...
Definition SimInfo.hpp:96
Real length() const
Returns the length of this vector.
Definition Vector.hpp:397
Real lengthSquare() const
Returns the squared length of this vector.
Definition Vector.hpp:403
This basic Periodic Table class was originally taken from the data.cpp file in OpenBabel.
Real dot(const DynamicVector< Real > &v1, const DynamicVector< Real > &v2)
Returns the dot product of two DynamicVectors.
std::string getPrefix(const std::string &str)