OpenMD 3.2
Molecular Dynamics in the Open
Loading...
Searching...
No Matches
TetrahedralityParamZ.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/TetrahedralityParamZ.hpp"
49
50#include <algorithm>
51#include <fstream>
52#include <vector>
53
54#include "io/DumpReader.hpp"
56#include "utils/simError.h"
57
58using namespace std;
59namespace OpenMD {
60 TetrahedralityParamZ::TetrahedralityParamZ(
61 SimInfo* info, const std::string& filename, const std::string& sele1,
62 const std::string& sele2, double rCut, int nzbins, int axis) :
63 StaticAnalyser(info, filename, nzbins),
64 selectionScript1_(sele1), selectionScript2_(sele2), seleMan1_(info),
65 seleMan2_(info), evaluator1_(info), evaluator2_(info), axis_(axis) {
66 evaluator1_.loadScriptString(sele1);
67 if (!evaluator1_.isDynamic()) {
68 seleMan1_.setSelectionSet(evaluator1_.evaluate());
69 }
70 evaluator2_.loadScriptString(sele2);
71 if (!evaluator2_.isDynamic()) {
72 seleMan2_.setSelectionSet(evaluator2_.evaluate());
73 }
74
75 // Set up cutoff radius:
76 rCut_ = rCut;
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 // fixed number of bins
92 sliceQ_.resize(nBins_);
93 sliceQ2_.resize(nBins_);
94 sliceCount_.resize(nBins_);
95 std::fill(sliceQ_.begin(), sliceQ_.end(), 0.0);
96 std::fill(sliceQ2_.begin(), sliceQ2_.end(), 0.0);
97 std::fill(sliceCount_.begin(), sliceCount_.end(), 0);
98
99 setOutputName(getPrefix(filename) + ".Qz");
100 }
101
102 void TetrahedralityParamZ::process() {
103 StuntDouble* sd;
104 StuntDouble* sd2;
105 StuntDouble* sdi;
106 StuntDouble* sdj;
107 int myIndex;
108 Vector3d vec;
109 Vector3d ri, rj, rk, rik, rkj;
110 RealType r;
111 RealType cospsi;
112 RealType Qk;
113 std::vector<std::pair<RealType, StuntDouble*>> myNeighbors;
114 int isd1;
115 int isd2;
116 bool usePeriodicBoundaryConditions_ =
117 info_->getSimParams()->getUsePeriodicBoundaryConditions();
118
119 DumpReader reader(info_, dumpFilename_);
120 int nFrames = reader.getNFrames();
121
122 for (int istep = 0; istep < nFrames; istep += step_) {
123 reader.readFrame(istep);
124 currentSnapshot_ = info_->getSnapshotManager()->getCurrentSnapshot();
125
126 Mat3x3d hmat = currentSnapshot_->getHmat();
127 zBox_.push_back(hmat(axis_, axis_));
128
129 RealType halfBoxZ_ = hmat(axis_, axis_) / 2.0;
130
131 if (evaluator1_.isDynamic()) {
132 seleMan1_.setSelectionSet(evaluator1_.evaluate());
133 }
134
135 if (evaluator2_.isDynamic()) {
136 seleMan2_.setSelectionSet(evaluator2_.evaluate());
137 }
138
139 // outer loop is over the selected StuntDoubles:
140 for (sd = seleMan1_.beginSelected(isd1); sd != NULL;
141 sd = seleMan1_.nextSelected(isd1)) {
142 myIndex = sd->getGlobalIndex();
143
144 Qk = 1.0;
145 myNeighbors.clear();
146
147 for (sd2 = seleMan2_.beginSelected(isd2); sd2 != NULL;
148 sd2 = seleMan2_.nextSelected(isd2)) {
149 if (sd2->getGlobalIndex() != myIndex) {
150 vec = sd->getPos() - sd2->getPos();
151
152 if (usePeriodicBoundaryConditions_)
153 currentSnapshot_->wrapVector(vec);
154
155 r = vec.length();
156
157 // Check to see if neighbor is in bond cutoff
158
159 if (r < rCut_) { myNeighbors.push_back(std::make_pair(r, sd2)); }
160 }
161 }
162
163 // Sort the vector using predicate and std::sort
164 std::sort(myNeighbors.begin(), myNeighbors.end());
165
166 // Use only the 4 closest neighbors to do the rest of the work:
167
168 int nbors = myNeighbors.size() > 4 ? 4 : myNeighbors.size();
169 int nang = int(0.5 * (nbors * (nbors - 1)));
170
171 rk = sd->getPos();
172
173 for (int i = 0; i < nbors - 1; i++) {
174 sdi = myNeighbors[i].second;
175 ri = sdi->getPos();
176 rik = rk - ri;
177 if (usePeriodicBoundaryConditions_) currentSnapshot_->wrapVector(rik);
178
179 rik.normalize();
180
181 for (int j = i + 1; j < nbors; j++) {
182 sdj = myNeighbors[j].second;
183 rj = sdj->getPos();
184 rkj = rk - rj;
185 if (usePeriodicBoundaryConditions_)
186 currentSnapshot_->wrapVector(rkj);
187 rkj.normalize();
188
189 cospsi = dot(rik, rkj);
190
191 // Calculates scaled Qk for each molecule using calculated
192 // angles from 4 or fewer nearest neighbors.
193 Qk -= (pow(cospsi + 1.0 / 3.0, 2) * 2.25 / nang);
194 }
195 }
196
197 if (nang > 0) {
198 if (usePeriodicBoundaryConditions_) currentSnapshot_->wrapVector(rk);
199
200 int binNo =
201 int(nBins_ * (halfBoxZ_ + rk[axis_]) / hmat(axis_, axis_));
202 sliceQ_[binNo] += Qk;
203 sliceQ2_[binNo] += Qk * Qk;
204 sliceCount_[binNo] += 1;
205 }
206 }
207 }
208 writeQz();
209 }
210
211 void TetrahedralityParamZ::writeQz() {
212 // compute average box length:
213
214 RealType zSum = 0.0;
215 for (std::vector<RealType>::iterator j = zBox_.begin(); j != zBox_.end();
216 ++j) {
217 zSum += *j;
218 }
219 RealType zAve = zSum / zBox_.size();
220
221 std::ofstream qZstream(outputFilename_.c_str());
222 if (qZstream.is_open()) {
223 qZstream << "#Tetrahedrality Parameters (" << axisLabel_ << ")\n";
224
225 qZstream << "#nFrames:\t" << zBox_.size() << "\n";
226 qZstream << "#selection 1: (" << selectionScript1_ << ")\n";
227 qZstream << "#selection 2: (" << selectionScript2_ << ")\n";
228 qZstream << "#" << axisLabel_ << "\tQk\n";
229 for (unsigned int i = 0; i < sliceQ_.size(); ++i) {
230 RealType z = zAve * (i + 0.5) / sliceQ_.size();
231 if (sliceCount_[i] != 0) {
232 RealType mean = sliceQ_[i] / sliceCount_[i];
233 RealType stdDev = sqrt(sliceQ2_[i] / sliceCount_[i] - mean * mean);
234 if (sliceCount_[i] == 1) {
235 qZstream << z << "\t" << mean << "\n";
236 } else {
237 RealType e95 = 1.96 * stdDev / sqrt(sliceCount_[i] - 1);
238 qZstream << z << "\t" << mean << "\t" << e95 << "\n";
239 }
240 }
241 }
242
243 } else {
244 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
245 "TetrahedralityParamZ: unable to open %s\n",
246 outputFilename_.c_str());
247 painCave.isFatal = 1;
248 simError();
249 }
250 qZstream.close();
251 }
252} // 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)