1 |
/* |
2 |
* Copyright (c) 2005 The University of Notre Dame. All Rights Reserved. |
3 |
* |
4 |
* The University of Notre Dame grants you ("Licensee") a |
5 |
* non-exclusive, royalty free, license to use, modify and |
6 |
* redistribute this software in source and binary code form, provided |
7 |
* that the following conditions are met: |
8 |
* |
9 |
* 1. Redistributions of source code must retain the above copyright |
10 |
* notice, this list of conditions and the following disclaimer. |
11 |
* |
12 |
* 2. Redistributions in binary form must reproduce the above copyright |
13 |
* notice, this list of conditions and the following disclaimer in the |
14 |
* documentation and/or other materials provided with the |
15 |
* distribution. |
16 |
* |
17 |
* This software is provided "AS IS," without a warranty of any |
18 |
* kind. All express or implied conditions, representations and |
19 |
* warranties, including any implied warranty of merchantability, |
20 |
* fitness for a particular purpose or non-infringement, are hereby |
21 |
* excluded. The University of Notre Dame and its licensors shall not |
22 |
* be liable for any damages suffered by licensee as a result of |
23 |
* using, modifying or distributing the software or its |
24 |
* derivatives. In no event will the University of Notre Dame or its |
25 |
* licensors be liable for any lost revenue, profit or data, or for |
26 |
* direct, indirect, special, consequential, incidental or punitive |
27 |
* damages, however caused and regardless of the theory of liability, |
28 |
* arising out of the use of or inability to use software, even if the |
29 |
* University of Notre Dame has been advised of the possibility of |
30 |
* such damages. |
31 |
* |
32 |
* SUPPORT OPEN SCIENCE! If you use OpenMD or its source code in your |
33 |
* research, please cite the appropriate papers when you publish your |
34 |
* work. Good starting points are: |
35 |
* |
36 |
* [1] Meineke, et al., J. Comp. Chem. 26, 252-271 (2005). |
37 |
* [2] Fennell & Gezelter, J. Chem. Phys. 124, 234104 (2006). |
38 |
* [3] Sun, Lin & Gezelter, J. Chem. Phys. 128, 234107 (2008). |
39 |
* [4] Kuang & Gezelter, J. Chem. Phys. 133, 164101 (2010). |
40 |
* [5] Vardeman, Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011). |
41 |
* [6] Kuang & Gezelter, Mol. Phys., 110, 691-701 (2012). |
42 |
*/ |
43 |
|
44 |
#include "applications/staticProps/TetrahedralityParamZ.hpp" |
45 |
#include "utils/simError.h" |
46 |
#include "io/DumpReader.hpp" |
47 |
#include "primitives/Molecule.hpp" |
48 |
#include "utils/NumericConstant.hpp" |
49 |
#include <vector> |
50 |
#include <algorithm> |
51 |
#include <fstream> |
52 |
|
53 |
using namespace std; |
54 |
namespace OpenMD { |
55 |
TetrahedralityParamZ::TetrahedralityParamZ(SimInfo* info, |
56 |
const std::string& filename, |
57 |
const std::string& sele1, |
58 |
const std::string& sele2, |
59 |
double rCut, int nzbins) |
60 |
: StaticAnalyser(info, filename), selectionScript1_(sele1), |
61 |
evaluator1_(info), seleMan1_(info), selectionScript2_(sele2), |
62 |
evaluator2_(info), seleMan2_(info), nZBins_(nzbins) { |
63 |
|
64 |
evaluator1_.loadScriptString(sele1); |
65 |
if (!evaluator1_.isDynamic()) { |
66 |
seleMan1_.setSelectionSet(evaluator1_.evaluate()); |
67 |
} |
68 |
evaluator2_.loadScriptString(sele2); |
69 |
if (!evaluator2_.isDynamic()) { |
70 |
seleMan2_.setSelectionSet(evaluator2_.evaluate()); |
71 |
} |
72 |
|
73 |
// Set up cutoff radius: |
74 |
rCut_ = rCut; |
75 |
|
76 |
// fixed number of bins |
77 |
sliceQ_.resize(nZBins_); |
78 |
sliceCount_.resize(nZBins_); |
79 |
std::fill(sliceQ_.begin(), sliceQ_.end(), 0.0); |
80 |
std::fill(sliceCount_.begin(), sliceCount_.end(), 0); |
81 |
|
82 |
setOutputName(getPrefix(filename) + ".Qz"); |
83 |
} |
84 |
|
85 |
TetrahedralityParamZ::~TetrahedralityParamZ() { |
86 |
sliceQ_.clear(); |
87 |
sliceCount_.clear(); |
88 |
zBox_.clear(); |
89 |
} |
90 |
|
91 |
void TetrahedralityParamZ::process() { |
92 |
Molecule* mol; |
93 |
StuntDouble* sd; |
94 |
StuntDouble* sd2; |
95 |
StuntDouble* sdi; |
96 |
StuntDouble* sdj; |
97 |
RigidBody* rb; |
98 |
int myIndex; |
99 |
SimInfo::MoleculeIterator mi; |
100 |
Molecule::RigidBodyIterator rbIter; |
101 |
Vector3d vec; |
102 |
Vector3d ri, rj, rk, rik, rkj; |
103 |
RealType r; |
104 |
RealType cospsi; |
105 |
RealType Qk; |
106 |
std::vector<std::pair<RealType,StuntDouble*> > myNeighbors; |
107 |
int isd1; |
108 |
int isd2; |
109 |
|
110 |
DumpReader reader(info_, dumpFilename_); |
111 |
int nFrames = reader.getNFrames(); |
112 |
|
113 |
for (int istep = 0; istep < nFrames; istep += step_) { |
114 |
reader.readFrame(istep); |
115 |
currentSnapshot_ = info_->getSnapshotManager()->getCurrentSnapshot(); |
116 |
|
117 |
Mat3x3d hmat = currentSnapshot_->getHmat(); |
118 |
zBox_.push_back(hmat(2,2)); |
119 |
|
120 |
RealType halfBoxZ_ = hmat(2,2) / 2.0; |
121 |
|
122 |
if (evaluator1_.isDynamic()) { |
123 |
seleMan1_.setSelectionSet(evaluator1_.evaluate()); |
124 |
} |
125 |
|
126 |
if (evaluator2_.isDynamic()) { |
127 |
seleMan2_.setSelectionSet(evaluator2_.evaluate()); |
128 |
} |
129 |
|
130 |
// update the positions of atoms which belong to the rigidbodies |
131 |
for (mol = info_->beginMolecule(mi); mol != NULL; |
132 |
mol = info_->nextMolecule(mi)) { |
133 |
for (rb = mol->beginRigidBody(rbIter); rb != NULL; |
134 |
rb = mol->nextRigidBody(rbIter)) { |
135 |
rb->updateAtoms(); |
136 |
} |
137 |
} |
138 |
|
139 |
// outer loop is over the selected StuntDoubles: |
140 |
for (sd = seleMan1_.beginSelected(isd1); sd != NULL; |
141 |
sd = seleMan1_.nextSelected(isd1)) { |
142 |
|
143 |
myIndex = sd->getGlobalIndex(); |
144 |
|
145 |
Qk = 1.0; |
146 |
myNeighbors.clear(); |
147 |
|
148 |
for (sd2 = seleMan2_.beginSelected(isd2); sd2 != NULL; |
149 |
sd2 = seleMan2_.nextSelected(isd2)) { |
150 |
|
151 |
if (sd2->getGlobalIndex() != myIndex) { |
152 |
|
153 |
vec = sd->getPos() - sd2->getPos(); |
154 |
|
155 |
if (usePeriodicBoundaryConditions_) |
156 |
currentSnapshot_->wrapVector(vec); |
157 |
|
158 |
r = vec.length(); |
159 |
|
160 |
// Check to see if neighbor is in bond cutoff |
161 |
|
162 |
if (r < rCut_) { |
163 |
myNeighbors.push_back(std::make_pair(r,sd2)); |
164 |
} |
165 |
} |
166 |
} |
167 |
|
168 |
// Sort the vector using predicate and std::sort |
169 |
std::sort(myNeighbors.begin(), myNeighbors.end()); |
170 |
|
171 |
// Use only the 4 closest neighbors to do the rest of the work: |
172 |
|
173 |
int nbors = myNeighbors.size()> 4 ? 4 : myNeighbors.size(); |
174 |
int nang = int (0.5 * (nbors * (nbors - 1))); |
175 |
|
176 |
rk = sd->getPos(); |
177 |
|
178 |
for (int i = 0; i < nbors-1; i++) { |
179 |
|
180 |
sdi = myNeighbors[i].second; |
181 |
ri = sdi->getPos(); |
182 |
rik = rk - ri; |
183 |
if (usePeriodicBoundaryConditions_) |
184 |
currentSnapshot_->wrapVector(rik); |
185 |
|
186 |
rik.normalize(); |
187 |
|
188 |
for (int j = i+1; j < nbors; j++) { |
189 |
|
190 |
sdj = myNeighbors[j].second; |
191 |
rj = sdj->getPos(); |
192 |
rkj = rk - rj; |
193 |
if (usePeriodicBoundaryConditions_) |
194 |
currentSnapshot_->wrapVector(rkj); |
195 |
rkj.normalize(); |
196 |
|
197 |
cospsi = dot(rik,rkj); |
198 |
|
199 |
// Calculates scaled Qk for each molecule using calculated |
200 |
// angles from 4 or fewer nearest neighbors. |
201 |
Qk -= (pow(cospsi + 1.0 / 3.0, 2) * 2.25 / nang); |
202 |
} |
203 |
} |
204 |
|
205 |
if (nang > 0) { |
206 |
if (usePeriodicBoundaryConditions_) |
207 |
currentSnapshot_->wrapVector(rk); |
208 |
|
209 |
int binNo = int(nZBins_ * (halfBoxZ_ + rk.z()) / hmat(2,2)); |
210 |
sliceQ_[binNo] += Qk; |
211 |
sliceCount_[binNo] += 1; |
212 |
} |
213 |
} |
214 |
} |
215 |
writeQz(); |
216 |
} |
217 |
|
218 |
void TetrahedralityParamZ::writeQz() { |
219 |
|
220 |
// compute average box length: |
221 |
|
222 |
RealType zSum = 0.0; |
223 |
for (std::vector<RealType>::iterator j = zBox_.begin(); |
224 |
j != zBox_.end(); ++j) { |
225 |
zSum += *j; |
226 |
} |
227 |
RealType zAve = zSum / zBox_.size(); |
228 |
|
229 |
std::ofstream qZstream(outputFilename_.c_str()); |
230 |
if (qZstream.is_open()) { |
231 |
qZstream << "#Tetrahedrality Parameters (z)\n"; |
232 |
qZstream << "#nFrames:\t" << zBox_.size() << "\n"; |
233 |
qZstream << "#selection 1: (" << selectionScript1_ << ")\n"; |
234 |
qZstream << "#selection 2: (" << selectionScript2_ << ")\n"; |
235 |
qZstream << "#z\tQk\n"; |
236 |
for (unsigned int i = 0; i < sliceQ_.size(); ++i) { |
237 |
RealType z = zAve * (i+0.5) / sliceQ_.size(); |
238 |
if (sliceCount_[i] != 0) { |
239 |
qZstream << z << "\t" << sliceQ_[i] / sliceCount_[i] << "\n"; |
240 |
} |
241 |
} |
242 |
|
243 |
} else { |
244 |
sprintf(painCave.errMsg, "TetrahedralityParamZ: unable to open %s\n", |
245 |
outputFilename_.c_str()); |
246 |
painCave.isFatal = 1; |
247 |
simError(); |
248 |
} |
249 |
qZstream.close(); |
250 |
} |
251 |
} |
252 |
|
253 |
|
254 |
|