OpenMD 3.0
Molecular Dynamics in the Open
Loading...
Searching...
No Matches
cOHz.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 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] Vardeman, Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011).
39 * [5] Kuang & Gezelter, Mol. Phys., 110, 691-701 (2012).
40 * [6] Lamichhane, Gezelter & Newman, J. Chem. Phys. 141, 134109 (2014).
41 * [7] Lamichhane, Newman & Gezelter, J. Chem. Phys. 141, 134110 (2014).
42 * [8] Bhattarai, Newman & Gezelter, Phys. Rev. B 99, 094106 (2019).
43 */
44
45#include "applications/dynamicProps/cOHz.hpp"
46
47#include <sstream>
48
50#include "utils/Revision.hpp"
51#include "utils/simError.h"
52
53namespace OpenMD {
54 COHZ::COHZ(SimInfo* info, const std::string& filename,
55 const std::string& sele1, const std::string& sele2, int order,
56 int nZbins, int axis) :
57 MoleculeACF<Vector<RealType, 4>>(info, filename, sele1, sele2),
58 nZBins_(nZbins), axis_(axis) {
59 setCorrFuncType("Legendre Correlation Function");
60 setOutputName(getPrefix(dumpFilename_) + ".lcorr");
61
62 std::stringstream params;
63 params << " order = " << order << ", nzbins = " << nZBins_;
64 const std::string paramString = params.str();
65 setParameterString(paramString);
66
67 if (!uniqueSelections_) { seleMan2_ = seleMan1_; }
68
69 // Compute complementary axes to the privileged axis
70 xaxis_ = (axis_ + 1) % 3;
71 yaxis_ = (axis_ + 2) % 3;
72
73 switch (axis_) {
74 case 0:
75 axisLabel_ = "x";
76 break;
77 case 1:
78 axisLabel_ = "y";
79 break;
80 case 2:
81 default:
82 axisLabel_ = "z";
83 break;
84 }
85
86 rotMats_.resize(nTimeBins_);
87 zbin_.resize(nTimeBins_);
88 histogram_.resize(nTimeBins_);
89 counts_.resize(nTimeBins_);
90 for (unsigned int i = 0; i < nTimeBins_; i++) {
91 histogram_[i].resize(nZBins_);
92 std::fill(histogram_[i].begin(), histogram_[i].end(),
93 Vector<RealType, 4>(0.0));
94 counts_[i].resize(nZBins_);
95 std::fill(counts_[i].begin(), counts_[i].end(), 0);
96 }
97 LegendrePolynomial polynomial(order);
98 legendre_ = polynomial.getLegendrePolynomial(order);
99 }
100
101 void COHZ::computeFrame(int frame) {
102 Mat3x3d hmat = currentSnapshot_->getHmat();
103 boxZ_ = hmat(axis_, axis_);
104 halfBoxZ_ = boxZ_ / 2.0;
105
106 MoleculeACF<Vector<RealType, 4>>::computeFrame(frame);
107 }
108
109 int COHZ::computeProperty1(int frame, Molecule* mol) {
110 RotMat3x3d A = mol->getRigidBodyAt(0)->getA();
111 rotMats_[frame].push_back(A);
112
113 Vector3d pos = mol->getCom();
114 if (info_->getSimParams()->getUsePeriodicBoundaryConditions())
115 currentSnapshot_->wrapVector(pos);
116 int zBin = int(nZBins_ * (halfBoxZ_ + pos[axis_]) / boxZ_);
117 zbin_[frame].push_back(zBin);
118
119 return rotMats_[frame].size() - 1;
120 }
121
122 Vector<RealType, 4> COHZ::calcCorrVal(int frame1, int frame2, int id1,
123 int id2) {
124 // Vectors v1x, v1y, and v1z are the body-fixed axes on the
125 // molecule in frame 1 in the laboratory frame.
126
127 // Vectors v2x, v2y, and v2z are the body-fixed axes on the
128 // molecule in frame 2 in the laboratory frame.
129
130 // Vectors u1 & u2 are the first OH bond vector in frames 1 & 2
131 // respectively. Here we assume SPC/E geometry.
132
133 // Vectors w1 & w2 are the second OH bond vector in frames 1 & 2
134 // respectively. Here we assume SPC/E geometry.
135
136 // Vectors h1 & h2 are the HH bond vectors in frames 1 & 2
137 // respectively. Here we assume SPC/E geometry again.
138
139 // Vector3d v1x = sd1->getA(frame1).getRow(0);
140 // Vector3d v2x = sd2->getA(frame2).getRow(0);
141
142 Vector3d v1y = rotMats_[frame1][id1].getRow(yaxis_);
143 Vector3d v1z = rotMats_[frame1][id1].getRow(axis_);
144
145 Vector3d v2y = rotMats_[frame2][id2].getRow(yaxis_);
146 Vector3d v2z = rotMats_[frame2][id2].getRow(axis_);
147
148 Vector3d u1 = 0.81649 * v1y + 0.57736 * v1z;
149 Vector3d u2 = 0.81649 * v2y + 0.57736 * v2z;
150
151 Vector3d w1 = -0.81649 * v1y + 0.57736 * v1z;
152 Vector3d w2 = -0.81649 * v2y + 0.57736 * v2z;
153
154 Vector3d h1 = 1.63298 * v1y;
155 Vector3d h2 = 1.63298 * v2y;
156
157 // result is a Vector<RealType, 4> with Dipole, OH1, OH2, and HH:
158 Vector<RealType, 4> r(0.0);
159 r[0] = legendre_.evaluate(dot(v1z, v2z) / (v1z.length() * v2z.length()));
160 r[1] = legendre_.evaluate(dot(u1, u2) / (u1.length() * u2.length()));
161 r[2] = legendre_.evaluate(dot(w1, w2) / (w1.length() * w2.length()));
162 r[3] = legendre_.evaluate(dot(h1, h2) / (h1.length() * h2.length()));
163 return r;
164 }
165
166 void COHZ::correlateFrames(int frame1, int frame2, int timeBin) {
167 std::vector<int> s1;
168 std::vector<int> s2;
169
170 std::vector<int>::iterator i1;
171 std::vector<int>::iterator i2;
172
173 Vector<RealType, 4> corrVal(0.0);
174
175 s1 = sele1ToIndex_[frame1];
176
177 if (uniqueSelections_)
178 s2 = sele2ToIndex_[frame2];
179 else
180 s2 = sele1ToIndex_[frame2];
181
182 for (i1 = s1.begin(), i2 = s2.begin(); i1 != s1.end() && i2 != s2.end();
183 ++i1, ++i2) {
184 // If the selections are dynamic, they might not have the
185 // same objects in both frames, so we need to roll either of
186 // the selections until we have the same object to
187 // correlate.
188
189 while (i1 != s1.end() && *i1 < *i2) {
190 ++i1;
191 }
192
193 while (i2 != s2.end() && *i2 < *i1) {
194 ++i2;
195 }
196
197 if (i1 == s1.end() || i2 == s2.end()) break;
198
199 corrVal = calcCorrVal(frame1, frame2, i1 - s1.begin(), i2 - s2.begin());
200 int zBin = zbin_[frame1][i1 - s1.begin()];
201
202 histogram_[timeBin][zBin] += corrVal;
203 counts_[timeBin][zBin]++;
204 }
205 }
206
207 void COHZ::postCorrelate() {
208 for (unsigned int i = 0; i < nTimeBins_; ++i) {
209 for (unsigned int j = 0; j < nZBins_; ++j) {
210 if (counts_[i][j] > 0) { histogram_[i][j] /= counts_[i][j]; }
211 }
212 }
213 }
214
215 void COHZ::validateSelection(SelectionManager&) {
216 Molecule* mol;
217 int i;
218 for (mol = seleMan1_.beginSelectedMolecule(i); mol != NULL;
219 mol = seleMan1_.nextSelectedMolecule(i)) {
220 if (mol->getNRigidBodies() < 1) {
221 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
222 "COHZ::validateSelection Error: "
223 "at least one selected molecule does not have a rigid body\n");
224 painCave.isFatal = 1;
225 simError();
226 }
227 }
228 }
229
230 void COHZ::writeCorrelate() {
231 std::string Dfile = getOutputFileName() + "D";
232 std::string OHfile = getOutputFileName() + "OH";
233 std::string HHfile = getOutputFileName() + "HH";
234
235 std::ofstream ofs1(Dfile.c_str());
236 std::ofstream ofs2(OHfile.c_str());
237 std::ofstream ofs3(HHfile.c_str());
238
239 if (ofs1.is_open()) {
240 Revision r;
241
242 ofs1 << "# " << getCorrFuncType() << " for dipole vectors in water\n";
243 ofs1 << "# OpenMD " << r.getFullRevision() << "\n";
244 ofs1 << "# " << r.getBuildDate() << "\n";
245 ofs1 << "# selection script1: \"" << selectionScript1_;
246 ofs1 << "\"\tselection script2: \"" << selectionScript2_ << "\"\n";
247 ofs1 << "# privilegedAxis computed as " << axisLabel_ << " axis \n";
248 if (!paramString_.empty())
249 ofs1 << "# parameters: " << paramString_ << "\n";
250
251 ofs1 << "#time\tPn(costheta_z)\n";
252
253 for (unsigned int i = 0; i < nTimeBins_; ++i) {
254 ofs1 << times_[i] - times_[0];
255
256 for (unsigned int j = 0; j < nZBins_; ++j) {
257 ofs1 << "\t" << histogram_[i][j][0];
258 }
259 ofs1 << "\n";
260 }
261
262 } else {
263 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
264 "COHz::writeCorrelate Error: failed to open %s\n",
265 Dfile.c_str());
266 painCave.isFatal = 1;
267 simError();
268 }
269 ofs1.close();
270
271 if (ofs2.is_open()) {
272 Revision r;
273
274 ofs2 << "# " << getCorrFuncType() << " for OH bond vectors in water\n";
275 ofs2 << "# OpenMD " << r.getFullRevision() << "\n";
276 ofs2 << "# " << r.getBuildDate() << "\n";
277 ofs2 << "# selection script1: \"" << selectionScript1_;
278 ofs2 << "\"\tselection script2: \"" << selectionScript2_ << "\"\n";
279 ofs2 << "# privilegedAxis computed as " << axisLabel_ << " axis \n";
280 if (!paramString_.empty())
281 ofs2 << "# parameters: " << paramString_ << "\n";
282
283 ofs2 << "#time\tPn(costheta_z)\n";
284
285 for (unsigned int i = 0; i < nTimeBins_; ++i) {
286 ofs2 << times_[i] - times_[0];
287
288 for (unsigned int j = 0; j < nZBins_; ++j) {
289 ofs2 << "\t" << 0.5 * (histogram_[i][j][1] + histogram_[i][j][2]);
290 }
291 ofs2 << "\n";
292 }
293
294 } else {
295 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
296 "COHz::writeCorrelate Error: failed to open %s\n",
297 OHfile.c_str());
298 painCave.isFatal = 1;
299 simError();
300 }
301 ofs2.close();
302
303 if (ofs3.is_open()) {
304 Revision r;
305
306 ofs3 << "# " << getCorrFuncType() << " for HH bond vectors in water\n";
307 ofs3 << "# OpenMD " << r.getFullRevision() << "\n";
308 ofs3 << "# " << r.getBuildDate() << "\n";
309 ofs3 << "# selection script1: \"" << selectionScript1_;
310 ofs3 << "\"\tselection script2: \"" << selectionScript2_ << "\"\n";
311 ofs3 << "# privilegedAxis computed as " << axisLabel_ << " axis \n";
312 if (!paramString_.empty())
313 ofs3 << "# parameters: " << paramString_ << "\n";
314
315 ofs3 << "#time\tPn(costheta_z)\n";
316
317 for (unsigned int i = 0; i < nTimeBins_; ++i) {
318 ofs3 << times_[i] - times_[0];
319
320 for (unsigned int j = 0; j < nZBins_; ++j) {
321 ofs3 << "\t" << histogram_[i][j][3];
322 }
323 ofs3 << "\n";
324 }
325
326 } else {
327 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
328 "COHz::writeCorrelate Error: failed to open %s\n",
329 HHfile.c_str());
330 painCave.isFatal = 1;
331 simError();
332 }
333 ofs3.close();
334 }
335} // namespace OpenMD
A collection of Legendre Polynomials.
size_t getNRigidBodies()
Returns the total number of rigid bodies in this molecule.
Definition Molecule.hpp:176
Vector3d getCom()
Returns the current center of mass position of this molecule.
Definition Molecule.cpp:298
Real evaluate(const Real &x)
Calculates the value of this Polynomial evaluated at the given x value.
Molecule * nextSelectedMolecule(int &i)
Finds the next selected Molecule in the selection.
Molecule * beginSelectedMolecule(int &i)
Finds the first selected Molecule in the selection.
One of the heavy-weight classes of OpenMD, SimInfo maintains objects and variables relating to the cu...
Definition SimInfo.hpp:93
Mat3x3d getHmat()
Returns the H-Matrix.
Definition Snapshot.cpp:214
void wrapVector(Vector3d &v)
Wrapping the vector according to periodic boundary condition.
Definition Snapshot.cpp:337
RotMat3x3d getA()
Returns the current rotation matrix of this stuntDouble.
Fix length vector class.
Definition Vector.hpp:78
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)