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