OpenMD 3.2
Molecular Dynamics in the Open
Loading...
Searching...
No Matches
LegendreCorrFuncZ.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/dynamicProps/LegendreCorrFuncZ.hpp"
49
50#include <sstream>
51
53#include "utils/Revision.hpp"
54#include "utils/simError.h"
55
56namespace OpenMD {
57 LegendreCorrFuncZ::LegendreCorrFuncZ(SimInfo* info,
58 const std::string& filename,
59 const std::string& sele1,
60 const std::string& sele2, int order,
61 int nZbins, int axis) :
62 ObjectACF<Vector3d>(info, filename, sele1, sele2),
63 nZBins_(nZbins), axis_(axis) {
64 setCorrFuncType("Legendre Correlation Function of Z");
65 setOutputName(getPrefix(dumpFilename_) + ".lcorrZ");
66
67 std::stringstream params;
68 params << " order = " << order << ", nzbins = " << nZBins_;
69 const std::string paramString = params.str();
70 setParameterString(paramString);
71
72 if (!uniqueSelections_) { seleMan2_ = seleMan1_; }
73
74 // Compute complementary axes to the privileged axis
75 xaxis_ = (axis_ + 1) % 3;
76 yaxis_ = (axis_ + 2) % 3;
77
78 switch (axis_) {
79 case 0:
80 axisLabel_ = "x";
81 break;
82 case 1:
83 axisLabel_ = "y";
84 break;
85 case 2:
86 default:
87 axisLabel_ = "z";
88 break;
89 }
90
91 rotMats_.resize(nTimeBins_);
92 zbin_.resize(nTimeBins_);
93 histogram_.resize(nTimeBins_);
94 counts_.resize(nTimeBins_);
95 for (unsigned int i = 0; i < nTimeBins_; i++) {
96 histogram_[i].resize(nZBins_);
97 std::fill(histogram_[i].begin(), histogram_[i].end(), 0.0);
98 counts_[i].resize(nZBins_);
99 std::fill(counts_[i].begin(), counts_[i].end(), 0);
100 }
101 LegendrePolynomial polynomial(order);
102 legendre_ = polynomial.getLegendrePolynomial(order);
103 }
104
105 void LegendreCorrFuncZ::computeFrame(int frame) {
106 Mat3x3d hmat = currentSnapshot_->getHmat();
107 boxZ_ = hmat(axis_, axis_);
108 halfBoxZ_ = boxZ_ / 2.0;
109
110 ObjectACF<Vector3d>::computeFrame(frame);
111 }
112
113 int LegendreCorrFuncZ::computeProperty1(int frame, StuntDouble* sd) {
114 RotMat3x3d A = sd->getA();
115 rotMats_[frame].push_back(A);
116
117 Vector3d pos = sd->getPos();
118 if (info_->getSimParams()->getUsePeriodicBoundaryConditions())
119 currentSnapshot_->wrapVector(pos);
120 int zBin = int(nZBins_ * (halfBoxZ_ + pos[axis_]) / boxZ_);
121 zbin_[frame].push_back(zBin);
122
123 return rotMats_[frame].size() - 1;
124 }
125
126 Vector3d LegendreCorrFuncZ::calcCorrVal(int frame1, int frame2, int id1,
127 int id2) {
128 Vector3d v1x = rotMats_[frame1][id1].getRow(xaxis_);
129 Vector3d v1y = rotMats_[frame1][id1].getRow(yaxis_);
130 Vector3d v1z = rotMats_[frame1][id1].getRow(axis_);
131
132 Vector3d v2x = rotMats_[frame2][id2].getRow(xaxis_);
133 Vector3d v2y = rotMats_[frame2][id2].getRow(yaxis_);
134 Vector3d v2z = rotMats_[frame2][id2].getRow(axis_);
135
136 RealType uxprod =
137 legendre_.evaluate(dot(v1x, v2x) / (v1x.length() * v2x.length()));
138 RealType uyprod =
139 legendre_.evaluate(dot(v1y, v2y) / (v1y.length() * v2y.length()));
140 RealType uzprod =
141 legendre_.evaluate(dot(v1z, v2z) / (v1z.length() * v2z.length()));
142
143 return Vector3d(uxprod, uyprod, uzprod);
144 }
145
146 void LegendreCorrFuncZ::correlateFrames(int frame1, int frame2, int timeBin) {
147 std::vector<int> s1;
148 std::vector<int> s2;
149
150 std::vector<int>::iterator i1;
151 std::vector<int>::iterator i2;
152
153 Vector3d corrVal(0.0);
154
155 s1 = sele1ToIndex_[frame1];
156
157 if (uniqueSelections_)
158 s2 = sele2ToIndex_[frame2];
159 else
160 s2 = sele1ToIndex_[frame2];
161
162 for (i1 = s1.begin(), i2 = s2.begin(); i1 != s1.end() && i2 != s2.end();
163 ++i1, ++i2) {
164 // If the selections are dynamic, they might not have the
165 // same objects in both frames, so we need to roll either of
166 // the selections until we have the same object to
167 // correlate.
168
169 while (i1 != s1.end() && *i1 < *i2) {
170 ++i1;
171 }
172
173 while (i2 != s2.end() && *i2 < *i1) {
174 ++i2;
175 }
176
177 if (i1 == s1.end() || i2 == s2.end()) break;
178
179 corrVal = calcCorrVal(frame1, frame2, i1 - s1.begin(), i2 - s2.begin());
180 int zBin = zbin_[frame1][i1 - s1.begin()];
181 histogram_[timeBin][zBin] += corrVal;
182 counts_[timeBin][zBin]++;
183 }
184 }
185
186 void LegendreCorrFuncZ::postCorrelate() {
187 for (unsigned int i = 0; i < nTimeBins_; ++i) {
188 for (unsigned int j = 0; j < nZBins_; ++j) {
189 if (counts_[i][j] > 0) { histogram_[i][j] /= counts_[i][j]; }
190 }
191 }
192 }
193
194 void LegendreCorrFuncZ::validateSelection(SelectionManager&) {
195 StuntDouble* sd;
196 int i;
197 for (sd = seleMan1_.beginSelected(i); sd != NULL;
198 sd = seleMan1_.nextSelected(i)) {
199 if (!sd->isDirectional()) {
200 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
201 "LegendreCorrFuncZ::validateSelection Error: "
202 "at least one of the selected objects is not Directional\n");
203 painCave.isFatal = 1;
204 simError();
205 }
206 }
207 }
208
209 void LegendreCorrFuncZ::writeCorrelate() {
210 std::ofstream ofs(getOutputFileName().c_str());
211
212 if (ofs.is_open()) {
213 Revision r;
214
215 ofs << "# " << getCorrFuncType() << "\n";
216 ofs << "# OpenMD " << r.getFullRevision() << "\n";
217 ofs << "# " << r.getBuildDate() << "\n";
218 ofs << "# selection script1: \"" << selectionScript1_;
219 ofs << "\"\tselection script2: \"" << selectionScript2_ << "\"\n";
220 ofs << "# privilegedAxis computed as " << axisLabel_ << " axis \n";
221 if (!paramString_.empty())
222 ofs << "# parameters: " << paramString_ << "\n";
223
224 ofs << "#time\tPn(costheta_z)\n";
225
226 for (unsigned int i = 0; i < nTimeBins_; ++i) {
227 ofs << times_[i] - times_[0];
228
229 for (unsigned int j = 0; j < nZBins_; ++j) {
230 ofs << "\t" << histogram_[i][j](2);
231 }
232 ofs << "\n";
233 }
234
235 } else {
236 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
237 "LegendreCorrFuncZ::writeCorrelate Error: failed to open %s\n",
238 getOutputFileName().c_str());
239 painCave.isFatal = 1;
240 simError();
241 }
242 ofs.close();
243 }
244} // namespace OpenMD
One of the heavy-weight classes of OpenMD, SimInfo maintains objects and variables relating to the cu...
Definition SimInfo.hpp:96
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)