OpenMD 3.2
Molecular Dynamics in the Open
Loading...
Searching...
No Matches
Displacement.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/Displacement.hpp"
49
50#include <sstream>
51
52#include "utils/Revision.hpp"
53
54namespace OpenMD {
55 Displacement::Displacement(SimInfo* info, const std::string& filename,
56 const std::string& sele1,
57 const std::string& sele2) :
58 ObjectACF<Vector3d>(info, filename, sele1, sele2) {
59 setCorrFuncType("Displacement");
60 setOutputName(getPrefix(dumpFilename_) + ".disp");
61
62 positions_.resize(nFrames_);
63 }
64
65 DisplacementZ::DisplacementZ(SimInfo* info, const std::string& filename,
66 const std::string& sele1,
67 const std::string& sele2, int nZbins, int axis) :
68 ObjectACF<Vector3d>(info, filename, sele1, sele2) {
69 setCorrFuncType("Displacement binned by Z");
70 setOutputName(getPrefix(dumpFilename_) + ".dispZ");
71
72 positions_.resize(nFrames_);
73 zBins_.resize(nFrames_);
74 nZBins_ = nZbins;
75 axis_ = axis;
76
77 switch (axis_) {
78 case 0:
79 axisLabel_ = "x";
80 break;
81 case 1:
82 axisLabel_ = "y";
83 break;
84 case 2:
85 default:
86 axisLabel_ = "z";
87 break;
88 }
89
90 std::stringstream params;
91 params << " nzbins = " << nZBins_;
92 const std::string paramString = params.str();
93 setParameterString(paramString);
94
95 histograms_.resize(nTimeBins_);
96 counts_.resize(nTimeBins_);
97 for (unsigned int i = 0; i < nTimeBins_; i++) {
98 histograms_[i].resize(nZBins_);
99 counts_[i].resize(nZBins_);
100 std::fill(histograms_[i].begin(), histograms_[i].end(), Vector3d(0.0));
101 std::fill(counts_[i].begin(), counts_[i].end(), 0);
102 }
103 }
104
105 int Displacement::computeProperty1(int frame, StuntDouble* sd) {
106 positions_[frame].push_back(sd->getPos());
107 return positions_[frame].size() - 1;
108 }
109
110 Vector3d Displacement::calcCorrVal(int frame1, int frame2, int id1, int id2) {
111 return positions_[frame2][id2] - positions_[frame1][id1];
112 }
113
114 void DisplacementZ::computeFrame(int istep) {
115 hmat_ = currentSnapshot_->getHmat();
116 halfBoxZ_ = hmat_(axis_, axis_) / 2.0;
117
118 StuntDouble* sd;
119
120 int isd1, isd2;
121 unsigned int index;
122
123 if (evaluator1_.isDynamic()) {
124 seleMan1_.setSelectionSet(evaluator1_.evaluate());
125 }
126
127 if (uniqueSelections_ && evaluator2_.isDynamic()) {
128 seleMan2_.setSelectionSet(evaluator2_.evaluate());
129 }
130
131 for (sd = seleMan1_.beginSelected(isd1); sd != NULL;
132 sd = seleMan1_.nextSelected(isd1)) {
133 index = computeProperty1(istep, sd);
134 if (index == sele1ToIndex_[istep].size()) {
135 sele1ToIndex_[istep].push_back(sd->getGlobalIndex());
136 } else {
137 sele1ToIndex_[istep].resize(index + 1);
138 sele1ToIndex_[istep][index] = sd->getGlobalIndex();
139 }
140 }
141
142 if (uniqueSelections_) {
143 for (sd = seleMan2_.beginSelected(isd2); sd != NULL;
144 sd = seleMan2_.nextSelected(isd2)) {
145 index = computeProperty1(istep, sd);
146
147 if (index == sele2ToIndex_[istep].size()) {
148 sele2ToIndex_[istep].push_back(sd->getGlobalIndex());
149 } else {
150 sele2ToIndex_[istep].resize(index + 1);
151 sele2ToIndex_[istep][index] = sd->getGlobalIndex();
152 }
153 }
154 }
155 }
156
157 int DisplacementZ::computeProperty1(int frame, StuntDouble* sd) {
158 Vector3d pos = sd->getPos();
159 // we need the raw (not wrapped) positions for RMSD:
160 positions_[frame].push_back(sd->getPos());
161
162 if (info_->getSimParams()->getUsePeriodicBoundaryConditions()) {
163 currentSnapshot_->wrapVector(pos);
164 }
165 int zBin = int(nZBins_ * (halfBoxZ_ + pos[axis_]) / hmat_(axis_, axis_));
166 zBins_[frame].push_back(zBin);
167
168 return positions_[frame].size() - 1;
169 }
170
171 void DisplacementZ::correlateFrames(int frame1, int frame2, int timeBin) {
172 std::vector<int> s1;
173 std::vector<int> s2;
174
175 std::vector<int>::iterator i1;
176 std::vector<int>::iterator i2;
177
178 s1 = sele1ToIndex_[frame1];
179
180 if (uniqueSelections_)
181 s2 = sele2ToIndex_[frame2];
182 else
183 s2 = sele1ToIndex_[frame2];
184
185 for (i1 = s1.begin(), i2 = s2.begin(); i1 != s1.end() && i2 != s2.end();
186 ++i1, ++i2) {
187 // If the selections are dynamic, they might not have the
188 // same objects in both frames, so we need to roll either of
189 // the selections until we have the same object to
190 // correlate.
191
192 while (i1 != s1.end() && *i1 < *i2) {
193 ++i1;
194 }
195
196 while (i2 != s2.end() && *i2 < *i1) {
197 ++i2;
198 }
199
200 if (i1 == s1.end() || i2 == s2.end()) break;
201
202 calcCorrValImpl(frame1, frame2, i1 - s1.begin(), i2 - s2.begin(),
203 timeBin);
204 }
205 }
206
207 Vector3d DisplacementZ::calcCorrValImpl(int frame1, int frame2, int id1,
208 int id2, int timeBin) {
209 int zBin1 = zBins_[frame1][id1];
210 int zBin2 = zBins_[frame2][id2];
211
212 if (zBin1 == zBin2) {
213 Vector3d diff = positions_[frame2][id2] - positions_[frame1][id1];
214 histograms_[timeBin][zBin1] += diff;
215 counts_[timeBin][zBin1]++;
216 }
217 return Vector3d(0.0);
218 }
219
220 void DisplacementZ::postCorrelate() {
221 for (unsigned int i = 0; i < nTimeBins_; ++i) {
222 for (unsigned int j = 0; j < nZBins_; ++j) {
223 if (counts_[i][j] > 0) {
224 histograms_[i][j] /= counts_[i][j];
225 } else {
226 histograms_[i][j] = Vector3d(0.0);
227 }
228 }
229 }
230 }
231 void DisplacementZ::writeCorrelate() {
232 std::ofstream ofs(getOutputFileName().c_str());
233
234 if (ofs.is_open()) {
235 Revision r;
236
237 ofs << "# " << getCorrFuncType() << "\n";
238 ofs << "# OpenMD " << r.getFullRevision() << "\n";
239 ofs << "# " << r.getBuildDate() << "\n";
240 ofs << "# selection script1: \"" << selectionScript1_;
241 ofs << "\"\tselection script2: \"" << selectionScript2_ << "\"\n";
242 ofs << "# privilegedAxis computed as " << axisLabel_ << " axis \n";
243 if (!paramString_.empty())
244 ofs << "# parameters: " << paramString_ << "\n";
245
246 ofs << "#time\tcorrVal\n";
247
248 for (unsigned int i = 0; i < nTimeBins_; ++i) {
249 ofs << times_[i] - times_[0] << "\t";
250
251 for (unsigned int j = 0; j < nZBins_; ++j) {
252 for (int k = 0; k < 3; k++) {
253 ofs << histograms_[i][j](k) << '\t';
254 }
255 }
256
257 ofs << "\n";
258 }
259
260 } else {
261 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
262 "DisplacementZ::writeCorrelate Error: fail to open %s\n",
263 getOutputFileName().c_str());
264 painCave.isFatal = 1;
265 simError();
266 }
267 ofs.close();
268 }
269} // 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.
std::string getPrefix(const std::string &str)