OpenMD 3.2
Molecular Dynamics in the Open
Loading...
Searching...
No Matches
TetrahedralityParamR.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/TetrahedralityParamR.hpp"
49
50#include <algorithm>
51#include <fstream>
52#include <vector>
53
54#include "io/DumpReader.hpp"
56#include "utils/Revision.hpp"
57#include "utils/simError.h"
58
59#define HONKING_LARGE_VALUE 1.0e10
60
61using namespace std;
62namespace OpenMD {
63 TetrahedralityParamR::TetrahedralityParamR(
64 SimInfo* info, const std::string& filename, const std::string& sele1,
65 const std::string& sele2, const std::string& sele3, RealType rCut,
66 RealType len, int nrbins) :
67 StaticAnalyser(info, filename, nrbins),
68 selectionScript1_(sele1), selectionScript2_(sele2),
69 selectionScript3_(sele3), seleMan1_(info), seleMan2_(info),
70 seleMan3_(info), evaluator1_(info), evaluator2_(info), evaluator3_(info),
71 len_(len), nBins_(nrbins) {
72 setAnalysisType("Tetrahedrality Parameter(r)");
73
74 evaluator1_.loadScriptString(sele1);
75 if (!evaluator1_.isDynamic()) {
76 seleMan1_.setSelectionSet(evaluator1_.evaluate());
77 }
78
79 evaluator2_.loadScriptString(sele2);
80 if (!evaluator2_.isDynamic()) {
81 seleMan2_.setSelectionSet(evaluator2_.evaluate());
82 }
83
84 evaluator3_.loadScriptString(sele3);
85 if (!evaluator3_.isDynamic()) {
86 seleMan3_.setSelectionSet(evaluator3_.evaluate());
87 }
88
89 // Set up cutoff radius:
90 rCut_ = rCut;
91
92 deltaR_ = len_ / nBins_;
93
94 // fixed number of bins
95 sliceQ_.resize(nBins_);
96 sliceCount_.resize(nBins_);
97 std::fill(sliceQ_.begin(), sliceQ_.end(), 0.0);
98 std::fill(sliceCount_.begin(), sliceCount_.end(), 0);
99
100 setOutputName(getPrefix(filename) + ".Qr");
101 }
102
103 void TetrahedralityParamR::process() {
104 StuntDouble* sd;
105 StuntDouble* sd2;
106 StuntDouble* sd3;
107 StuntDouble* sdi;
108 StuntDouble* sdj;
109 int myIndex;
110 Vector3d vec;
111 Vector3d ri, rj, rk, rik, rkj;
112 RealType r;
113 RealType cospsi;
114 RealType Qk;
115 std::vector<std::pair<RealType, StuntDouble*>> myNeighbors;
116 int isd1;
117 int isd2;
118 int isd3;
119 bool usePeriodicBoundaryConditions_ =
120 info_->getSimParams()->getUsePeriodicBoundaryConditions();
121
122 DumpReader reader(info_, dumpFilename_);
123 int nFrames = reader.getNFrames();
124
125 for (int istep = 0; istep < nFrames; istep += step_) {
126 reader.readFrame(istep);
127 currentSnapshot_ = info_->getSnapshotManager()->getCurrentSnapshot();
128
129 if (evaluator1_.isDynamic()) {
130 seleMan1_.setSelectionSet(evaluator1_.evaluate());
131 }
132
133 if (evaluator2_.isDynamic()) {
134 seleMan2_.setSelectionSet(evaluator2_.evaluate());
135 }
136
137 if (evaluator3_.isDynamic()) {
138 seleMan3_.setSelectionSet(evaluator3_.evaluate());
139 }
140
141 if (seleMan3_.getSelectionCount() == 0) {
142 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
143 "TetrahedralityParamR:process: third selection has no objects"
144 "at frame %d, skipping frame.\n",
145 istep);
146 painCave.severity = OPENMD_WARNING;
147 painCave.isFatal = 0;
148 simError();
149 continue;
150 }
151
152 // outer loop is over the selected StuntDoubles:
153 for (sd = seleMan1_.beginSelected(isd1); sd != NULL;
154 sd = seleMan1_.nextSelected(isd1)) {
155 myIndex = sd->getGlobalIndex();
156
157 Qk = 1.0;
158 myNeighbors.clear();
159
160 for (sd2 = seleMan2_.beginSelected(isd2); sd2 != NULL;
161 sd2 = seleMan2_.nextSelected(isd2)) {
162 if (sd2->getGlobalIndex() != myIndex) {
163 vec = sd->getPos() - sd2->getPos();
164
165 if (usePeriodicBoundaryConditions_)
166 currentSnapshot_->wrapVector(vec);
167
168 r = vec.length();
169
170 // Check to see if neighbor is in bond cutoff
171
172 if (r < rCut_) { myNeighbors.push_back(std::make_pair(r, sd2)); }
173 }
174 }
175
176 // Sort the vector using predicate and std::sort
177 std::sort(myNeighbors.begin(), myNeighbors.end());
178
179 // Use only the 4 closest neighbors to do the rest of the work:
180
181 int nbors = myNeighbors.size() > 4 ? 4 : myNeighbors.size();
182 int nang = int(0.5 * (nbors * (nbors - 1)));
183
184 rk = sd->getPos();
185
186 for (int i = 0; i < nbors - 1; i++) {
187 sdi = myNeighbors[i].second;
188 ri = sdi->getPos();
189 rik = rk - ri;
190 if (usePeriodicBoundaryConditions_) currentSnapshot_->wrapVector(rik);
191
192 rik.normalize();
193
194 for (int j = i + 1; j < nbors; j++) {
195 sdj = myNeighbors[j].second;
196 rj = sdj->getPos();
197 rkj = rk - rj;
198 if (usePeriodicBoundaryConditions_)
199 currentSnapshot_->wrapVector(rkj);
200 rkj.normalize();
201
202 cospsi = dot(rik, rkj);
203
204 // Calculates scaled Qk for each molecule using calculated
205 // angles from 4 or fewer nearest neighbors.
206 Qk -= (pow(cospsi + 1.0 / 3.0, 2) * 2.25 / nang);
207 }
208 }
209
210 if (nang > 0) {
211 RealType shortest = HONKING_LARGE_VALUE;
212
213 // loop over selection 3 to find closest atom in selection 3:
214 for (sd3 = seleMan3_.beginSelected(isd3); sd3 != NULL;
215 sd3 = seleMan3_.nextSelected(isd3)) {
216 vec = sd->getPos() - sd3->getPos();
217
218 if (usePeriodicBoundaryConditions_)
219 currentSnapshot_->wrapVector(vec);
220
221 r = vec.length();
222
223 if (r < shortest) shortest = r;
224 }
225
226 int whichBin = int(shortest / deltaR_);
227 if (whichBin < int(nBins_)) {
228 sliceQ_[whichBin] += Qk;
229 sliceCount_[whichBin] += 1;
230 }
231 }
232 }
233 }
234 writeQr();
235 }
236
237 void TetrahedralityParamR::writeQr() {
238 Revision rev;
239 std::ofstream qRstream(outputFilename_.c_str());
240 if (qRstream.is_open()) {
241 qRstream << "# " << getAnalysisType() << "\n";
242 qRstream << "# OpenMD " << rev.getFullRevision() << "\n";
243 qRstream << "# " << rev.getBuildDate() << "\n";
244 qRstream << "#selection 1: (" << selectionScript1_ << ")\n";
245 qRstream << "#selection 2: (" << selectionScript2_ << ")\n";
246 qRstream << "#selection 3: (" << selectionScript3_ << ")\n";
247 if (!paramString_.empty())
248 qRstream << "# parameters: " << paramString_ << "\n";
249
250 qRstream << "#distance"
251 << "\tQk\n";
252 for (unsigned int i = 0; i < sliceQ_.size(); ++i) {
253 RealType Rval = (i + 0.5) * deltaR_;
254 if (sliceCount_[i] != 0) {
255 qRstream << Rval << "\t" << sliceQ_[i] / (RealType)sliceCount_[i]
256 << "\n";
257 }
258 }
259
260 } else {
261 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
262 "TetrahedralityParamR: unable to open %s\n",
263 outputFilename_.c_str());
264 painCave.isFatal = 1;
265 simError();
266 }
267 qRstream.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.
Real dot(const DynamicVector< Real > &v1, const DynamicVector< Real > &v2)
Returns the dot product of two DynamicVectors.
std::string getPrefix(const std::string &str)