OpenMD 3.2
Molecular Dynamics in the Open
Loading...
Searching...
No Matches
TetrahedralityParam.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/TetrahedralityParam.hpp"
49
50#include <vector>
51
52#include "io/DumpReader.hpp"
54#include "utils/simError.h"
55
56namespace OpenMD {
57
58 TetrahedralityParam::TetrahedralityParam(SimInfo* info,
59 const std::string& filename,
60 const std::string& sele, double rCut,
61 int nbins) :
62 StaticAnalyser(info, filename, nbins),
63 selectionScript_(sele), seleMan_(info), evaluator_(info) {
64 setOutputName(getPrefix(filename) + ".q");
65
66 evaluator_.loadScriptString(sele);
67 if (!evaluator_.isDynamic()) {
68 seleMan_.setSelectionSet(evaluator_.evaluate());
69 }
70
71 // Set up cutoff radius:
72
73 rCut_ = rCut;
74
75 Q_histogram_.resize(nBins_);
76
77 // Q can take values from 0 to 1
78
79 MinQ_ = 0.0;
80 MaxQ_ = 1.1;
81 deltaQ_ = (MaxQ_ - MinQ_) / nBins_;
82 }
83
84 void TetrahedralityParam::initializeHistogram() {
85 std::fill(Q_histogram_.begin(), Q_histogram_.end(), 0);
86 }
87
88 void TetrahedralityParam::process() {
89 Molecule* mol;
90 StuntDouble* sd;
91 StuntDouble* sd2;
92 StuntDouble* sdi;
93 StuntDouble* sdj;
94 int myIndex;
95 SimInfo::MoleculeIterator mi;
96 Molecule::IntegrableObjectIterator ioi;
97 Vector3d vec;
98 Vector3d ri, rj, rk, rik, rkj, dposition, tposition;
99 RealType r;
100 RealType cospsi;
101 RealType Qk;
102 std::vector<std::pair<RealType, StuntDouble*>> myNeighbors;
103 int isd;
104 bool usePeriodicBoundaryConditions_ =
105 info_->getSimParams()->getUsePeriodicBoundaryConditions();
106
107 DumpReader reader(info_, dumpFilename_);
108 int nFrames = reader.getNFrames();
109 frameCounter_ = 0;
110
111 Distorted_.clear();
112 Tetrahedral_.clear();
113
114 for (int istep = 0; istep < nFrames; istep += step_) {
115 reader.readFrame(istep);
116 frameCounter_++;
117 currentSnapshot_ = info_->getSnapshotManager()->getCurrentSnapshot();
118
119 if (evaluator_.isDynamic()) {
120 seleMan_.setSelectionSet(evaluator_.evaluate());
121 }
122
123 // outer loop is over the selected StuntDoubles:
124
125 for (sd = seleMan_.beginSelected(isd); sd != NULL;
126 sd = seleMan_.nextSelected(isd)) {
127 myIndex = sd->getGlobalIndex();
128 Qk = 1.0;
129
130 myNeighbors.clear();
131
132 // inner loop is over all StuntDoubles in the system:
133
134 for (mol = info_->beginMolecule(mi); mol != NULL;
135 mol = info_->nextMolecule(mi)) {
136 for (sd2 = mol->beginIntegrableObject(ioi); sd2 != NULL;
137 sd2 = mol->nextIntegrableObject(ioi)) {
138 if (sd2->getGlobalIndex() != myIndex) {
139 vec = sd->getPos() - sd2->getPos();
140
141 if (usePeriodicBoundaryConditions_)
142 currentSnapshot_->wrapVector(vec);
143
144 r = vec.length();
145
146 // Check to see if neighbor is in bond cutoff
147
148 if (r < rCut_) { myNeighbors.push_back(std::make_pair(r, sd2)); }
149 }
150 }
151 }
152
153 // Sort the vector using predicate and std::sort
154 std::sort(myNeighbors.begin(), myNeighbors.end());
155
156 // std::cerr << myNeighbors.size() << " neighbors within "
157 // << rCut_ << " A" << " \n";
158
159 // Use only the 4 closest neighbors to do the rest of the work:
160
161 int nbors = myNeighbors.size() > 4 ? 4 : myNeighbors.size();
162 int nang = int(0.5 * (nbors * (nbors - 1)));
163
164 rk = sd->getPos();
165 // std::cerr<<nbors<<endl;
166 for (int i = 0; i < nbors - 1; i++) {
167 sdi = myNeighbors[i].second;
168 ri = sdi->getPos();
169 rik = rk - ri;
170 if (usePeriodicBoundaryConditions_) currentSnapshot_->wrapVector(rik);
171
172 rik.normalize();
173
174 for (int j = i + 1; j < nbors; j++) {
175 sdj = myNeighbors[j].second;
176 rj = sdj->getPos();
177 rkj = rk - rj;
178 if (usePeriodicBoundaryConditions_)
179 currentSnapshot_->wrapVector(rkj);
180 rkj.normalize();
181
182 cospsi = dot(rik, rkj);
183
184 // std::cerr << "cos(psi) = " << cospsi << " \n";
185
186 // Calculates scaled Qk for each molecule using calculated
187 // angles from 4 or fewer nearest neighbors.
188 Qk = Qk - (pow(cospsi + 1.0 / 3.0, 2) * 2.25 / nang);
189 // std::cerr<<Qk<<"\t"<<nang<<endl;
190 }
191 }
192 // std::cerr<<nang<<endl;
193 if (nang > 0) {
194 collectHistogram(Qk);
195
196 // Saves positions of StuntDoubles & neighbors with distorted
197 // coordination (low Qk value)
198 if ((Qk < 0.55) && (Qk > 0.45)) {
199 // std::cerr<<Distorted_.size()<<endl;
200 Distorted_.push_back(sd);
201 // std::cerr<<Distorted_.size()<<endl;
202 dposition = sd->getPos();
203 // std::cerr << "distorted position \t" << dposition << "\n";
204 }
205
206 // Saves positions of StuntDoubles & neighbors with
207 // tetrahedral coordination (high Qk value)
208 if (Qk > 0.05) {
209 Tetrahedral_.push_back(sd);
210
211 tposition = sd->getPos();
212 // std::cerr << "tetrahedral position \t" << tposition << "\n";
213 }
214
215 // std::cerr<<Tetrahedral_.size()<<endl;
216 }
217 }
218 }
219
220 writeOrderParameter();
221 std::cerr << "number of distorted StuntDoubles = " << Distorted_.size()
222 << "\n";
223 std::cerr << "number of tetrahedral StuntDoubles = " << Tetrahedral_.size()
224 << "\n";
225 }
226
227 void TetrahedralityParam::collectHistogram(RealType Qk) {
228 if (Qk > MinQ_ && Qk < MaxQ_) {
229 int whichBin = int((Qk - MinQ_) / deltaQ_);
230 Q_histogram_[whichBin] += 1;
231 }
232 }
233
234 void TetrahedralityParam::writeOrderParameter() {
235 int nSelected = 0;
236
237 for (int i = 0; i < nBins_; ++i) {
238 nSelected = nSelected + int(Q_histogram_[i] * deltaQ_);
239 }
240
241 std::ofstream osq((getOutputFileName() + "Q").c_str());
242
243 if (osq.is_open()) {
244 osq << "# Tetrahedrality Parameters\n";
245 osq << "# selection: (" << selectionScript_ << ")\n";
246 osq << "# \n";
247 // Normalize by number of frames and write it out:
248 for (int i = 0; i < nBins_; ++i) {
249 RealType Qval = MinQ_ + (i + 0.5) * deltaQ_;
250 osq << Qval;
251 osq << "\t" << (RealType)(Q_histogram_[i] / deltaQ_) / nSelected;
252 osq << "\n";
253 }
254
255 osq.close();
256
257 } else {
258 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
259 "TetrahedralityParam: unable to open %s\n",
260 (getOutputFileName() + "q").c_str());
261 painCave.isFatal = 1;
262 simError();
263 }
264
265 DumpReader reader(info_, dumpFilename_);
266 int nFrames = reader.getNFrames();
267
268 if (nFrames == 1) {
269 std::vector<StuntDouble*>::iterator iter;
270 std::ofstream osd((getOutputFileName() + "dxyz").c_str());
271
272 if (osd.is_open()) {
273 osd << Distorted_.size() << "\n";
274 osd << "\n";
275
276 for (iter = Distorted_.begin(); iter != Distorted_.end(); ++iter) {
277 Vector3d position;
278 position = (*iter)->getPos();
279 osd << "O "
280 << "\t";
281 for (unsigned int z = 0; z < position.size(); z++) {
282 osd << position[z] << " "
283 << "\t";
284 }
285 osd << "\n";
286 }
287 osd.close();
288 }
289
290 std::ofstream ost((getOutputFileName() + "txyz").c_str());
291
292 if (ost.is_open()) {
293 ost << Tetrahedral_.size() << "\n";
294 ost << "\n";
295
296 for (iter = Tetrahedral_.begin(); iter != Tetrahedral_.end(); ++iter) {
297 Vector3d position;
298 position = (*iter)->getPos();
299
300 ost << "O "
301 << "\t";
302
303 for (unsigned int z = 0; z < position.size(); z++) {
304 ost << position[z] << " "
305 << "\t";
306 }
307 ost << "\n";
308 }
309 ost.close();
310 }
311 }
312 }
313} // 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)