OpenMD 3.2
Molecular Dynamics in the Open
Loading...
Searching...
No Matches
TetrahedralityParamDens.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/TetrahedralityParamDens.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 TetrahedralityParamDens::TetrahedralityParamDens(SimInfo* info,
61 const std::string& filename,
62 const std::string& sele1,
63 const std::string& sele2,
64 double rCut, int ndensbins) :
65 StaticAnalyser(info, filename, ndensbins),
66 selectionScript1_(sele1), selectionScript2_(sele2), seleMan1_(info),
67 seleMan2_(info), evaluator1_(info), evaluator2_(info), rCut_(rCut) {
68 evaluator1_.loadScriptString(sele1);
69 if (!evaluator1_.isDynamic()) {
70 seleMan1_.setSelectionSet(evaluator1_.evaluate());
71 }
72 evaluator2_.loadScriptString(sele2);
73 if (!evaluator2_.isDynamic()) {
74 seleMan2_.setSelectionSet(evaluator2_.evaluate());
75 }
76
77 // Q can take values of 0 to 1.
78 MinQ_ = -3.0;
79 MaxQ_ = 1.1;
80 deltaQ_ = (MaxQ_ - MinQ_) / nBins_;
81
82 // zeroing the number of molecules investigated counter
83 count_ = 0;
84
85 // fixed number of bins
86 sliceCount_.resize(nBins_);
87 std::fill(sliceCount_.begin(), sliceCount_.end(), 0);
88
89 setOutputName(getPrefix(filename) + ".Qdens");
90 }
91
92 void TetrahedralityParamDens::process() {
93 StuntDouble* sd;
94 StuntDouble* sd2;
95 StuntDouble* sdi;
96 StuntDouble* sdj;
97 int myIndex;
98 Vector3d vec;
99 Vector3d ri, rj, rk, rik, rkj;
100 RealType r;
101 RealType cospsi;
102 RealType Qk;
103 std::vector<std::pair<RealType, StuntDouble*>> myNeighbors;
104 int isd1;
105 int isd2;
106 bool usePeriodicBoundaryConditions_ =
107 info_->getSimParams()->getUsePeriodicBoundaryConditions();
108
109 DumpReader reader(info_, dumpFilename_);
110 int nFrames = reader.getNFrames();
111
112 for (int istep = 0; istep < nFrames; istep += step_) {
113 reader.readFrame(istep);
114 currentSnapshot_ = info_->getSnapshotManager()->getCurrentSnapshot();
115
116 // Mat3x3d hmat = currentSnapshot_->getHmat();
117 // zBox_.push_back(hmat(2,2));
118
119 // RealType halfBoxZ_ = hmat(2,2) / 2.0;
120
121 if (evaluator1_.isDynamic()) {
122 seleMan1_.setSelectionSet(evaluator1_.evaluate());
123 }
124
125 if (evaluator2_.isDynamic()) {
126 seleMan2_.setSelectionSet(evaluator2_.evaluate());
127 }
128
129 // outer loop is over the selected StuntDoubles:
130 for (sd = seleMan1_.beginSelected(isd1); sd != NULL;
131 sd = seleMan1_.nextSelected(isd1)) {
132 myIndex = sd->getGlobalIndex();
133
134 Qk = 1.0;
135 myNeighbors.clear();
136
137 for (sd2 = seleMan2_.beginSelected(isd2); sd2 != NULL;
138 sd2 = seleMan2_.nextSelected(isd2)) {
139 if (sd2->getGlobalIndex() != myIndex) {
140 vec = sd->getPos() - sd2->getPos();
141
142 if (usePeriodicBoundaryConditions_)
143 currentSnapshot_->wrapVector(vec);
144
145 r = vec.length();
146
147 // Check to see if neighbor is in bond cutoff
148
149 if (r < rCut_) {
150 myNeighbors.push_back(std::make_pair(r, sd2));
151 if (myIndex == 513) {
152 // std::cerr<< "sd2 index = " << sd2->getGlobalIndex() << "\n";
153 }
154 }
155 }
156 }
157
158 // Sort the vector using predicate and std::sort
159 std::sort(myNeighbors.begin(), myNeighbors.end());
160
161 // Use only the 4 closest neighbors to do the rest of the work:
162
163 int nbors = myNeighbors.size() > 4 ? 4 : myNeighbors.size();
164 int nang = int(0.5 * (nbors * (nbors - 1)));
165 if (nang < 4) {
166 // std::cerr << "nbors = " << nbors << " nang = " << nang << "\n";
167 }
168 rk = sd->getPos();
169
170 for (int i = 0; i < nbors - 1; i++) {
171 sdi = myNeighbors[i].second;
172 ri = sdi->getPos();
173 rik = rk - ri;
174 if (usePeriodicBoundaryConditions_) currentSnapshot_->wrapVector(rik);
175
176 rik.normalize();
177
178 for (int j = i + 1; j < nbors; j++) {
179 sdj = myNeighbors[j].second;
180 rj = sdj->getPos();
181 rkj = rk - rj;
182 if (usePeriodicBoundaryConditions_)
183 currentSnapshot_->wrapVector(rkj);
184 rkj.normalize();
185
186 cospsi = dot(rik, rkj);
187
188 // Calculates scaled Qk for each molecule using calculated
189 // angles from 4 or fewer nearest neighbors.
190 Qk -= (pow(cospsi + 1.0 / 3.0, 2) * 2.25 / nang);
191 /*if (Qk < 0 ) {
192 std::cerr << "(pow(cospsi + 1.0 / 3.0, 2) * 2.25 / nang)" <<
193 (pow(cospsi + 1.0 / 3.0, 2) * 2.25 / nang) << "\n"; std::cerr << "Qk
194 = " << Qk << " nang = " << nang << " nbors = " << nbors << "\n";
195 std::cerr << "myIndex = " << myIndex <<
196 "\n";
197 }*/
198 }
199 }
200
201 // Based on the molecule's q value, populate the histogram.
202 if (nang > 0) {
203 int binNo = int((Qk - MinQ_) / deltaQ_);
204
205 if (binNo < int(sliceCount_.size()) && binNo >= 0) {
206 sliceCount_[binNo] += 1;
207 } else {
208 std::cerr << "binNo = " << binNo << " Qk = " << Qk << "\n";
209 // std::cerr << "nbors = " << nbors << "\n";
210 }
211
212 // std::cout << "count = " << count_ << "\n";
213 count_++;
214 }
215 }
216 }
217 writeQdens();
218 }
219
220 void TetrahedralityParamDens::writeQdens() {
221 std::ofstream qdensstream(outputFilename_.c_str());
222 if (qdensstream.is_open()) {
223 qdensstream << "#Tetrahedrality Parameters \n";
224 qdensstream << "#nMolecules:\t" << count_ << " \n";
225 qdensstream << "#selection 1: (" << selectionScript1_ << ")\n";
226 qdensstream << "#selection 2: (" << selectionScript2_ << ")\n";
227 qdensstream << "#Qk\tfractional probability \n";
228
229 for (unsigned int i = 0; i < sliceCount_.size(); ++i) {
230 RealType q = MinQ_ + (i + 0.5) * deltaQ_;
231 if (count_ != 0) {
232 qdensstream << q << "\t" << sliceCount_[i] / RealType(count_) << "\n";
233 }
234 }
235
236 } else {
237 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
238 "TetrahedralityParamDens: unable to open %s\n",
239 outputFilename_.c_str());
240 painCave.isFatal = 1;
241 simError();
242 }
243 qdensstream.close();
244 }
245} // 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)