OpenMD 3.1
Molecular Dynamics in the Open
Loading...
Searching...
No Matches
HBondZvol.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 "HBondZvol.hpp"
46
47#include <fstream>
48#include <string>
49#include <vector>
50
51#include "io/DumpReader.hpp"
53#include "utils/Constants.hpp"
54#include "utils/simError.h"
55
56namespace OpenMD {
57
58 HBondZvol::HBondZvol(SimInfo* info, const std::string& filename,
59 const std::string& sele1, const std::string& sele2,
60 double rCut, double thetaCut, int nzbins, int axis) :
61 StaticAnalyser(info, filename, nzbins),
62 selectionScript1_(sele1), seleMan1_(info), evaluator1_(info),
63 selectionScript2_(sele2), seleMan2_(info), evaluator2_(info),
64 axis_(axis) {
65 ff_ = info_->getForceField();
66
67 evaluator1_.loadScriptString(sele1);
68 if (!evaluator1_.isDynamic()) {
69 seleMan1_.setSelectionSet(evaluator1_.evaluate());
70 }
71 evaluator2_.loadScriptString(sele2);
72 if (!evaluator2_.isDynamic()) {
73 seleMan2_.setSelectionSet(evaluator2_.evaluate());
74 }
75
76 // Set up cutoff values:
77 nBins_ = nzbins;
78 rCut_ = rCut;
79 thetaCut_ = thetaCut;
80
81 // fixed number of bins
82
83 nHBonds_.resize(nBins_);
84 nDonor_.resize(nBins_);
85 nAcceptor_.resize(nBins_);
86 sliceQ_.resize(nBins_);
87 sliceCount_.resize(nBins_);
88 vol_.resize(nBins_);
89 std::fill(sliceQ_.begin(), sliceQ_.end(), 0.0);
90 std::fill(sliceCount_.begin(), sliceCount_.end(), 0);
91
92 switch (axis_) {
93 case 0:
94 axisLabel_ = "x";
95 break;
96 case 1:
97 axisLabel_ = "y";
98 break;
99 case 2:
100 default:
101 axisLabel_ = "z";
102 break;
103 }
104
105 setOutputName(getPrefix(filename) + ".hbondzvol");
106 }
107
108 void HBondZvol::process() {
109 Molecule* mol1;
110 Molecule* mol2;
111 Molecule::HBondDonor* hbd1;
112 Molecule::HBondDonor* hbd2;
113 std::vector<Molecule::HBondDonor*>::iterator hbdi;
114 std::vector<Molecule::HBondDonor*>::iterator hbdj;
115 std::vector<Atom*>::iterator hbai;
116 std::vector<Atom*>::iterator hbaj;
117 Atom* hba1;
118 Atom* hba2;
119 Vector3d dPos;
120 Vector3d aPos;
121 Vector3d hPos;
122 Vector3d DH;
123 Vector3d DA;
124 RealType DAdist, DHdist, theta, ctheta;
125 int ii, jj;
126 int nHB, nA, nD;
127 RealType sliceVol;
128
129 bool usePeriodicBoundaryConditions_ =
130 info_->getSimParams()->getUsePeriodicBoundaryConditions();
131
132 DumpReader reader(info_, dumpFilename_);
133 int nFrames = reader.getNFrames();
134 frameCounter_ = 0;
135
136 for (int istep = 0; istep < nFrames; istep += step_) {
137 reader.readFrame(istep);
138 currentSnapshot_ = info_->getSnapshotManager()->getCurrentSnapshot();
139
140 Mat3x3d hmat = currentSnapshot_->getHmat();
141
142 RealType Lx = hmat(0, 0);
143 RealType Ly = hmat(1, 1);
144 RealType Lz = hmat(2, 2);
145
146 zBox_.push_back(hmat(axis_, axis_));
147 sliceVol = currentSnapshot_->getVolume() / nBins_;
148
149 RealType halfBoxZ_ = hmat(axis_, axis_) / 2.0;
150
151 if (evaluator1_.isDynamic()) {
152 seleMan1_.setSelectionSet(evaluator1_.evaluate());
153 }
154
155 if (evaluator2_.isDynamic()) {
156 seleMan2_.setSelectionSet(evaluator2_.evaluate());
157 }
158
159 for (mol1 = seleMan1_.beginSelectedMolecule(ii); mol1 != NULL;
160 mol1 = seleMan1_.nextSelectedMolecule(ii)) {
161 // We're collecting statistics on the molecules in selection 1:
162 nHB = 0;
163 nA = 0;
164 nD = 0;
165 Vector3d mPos = mol1->getCom();
166
167 for (mol2 = seleMan2_.beginSelectedMolecule(jj); mol2 != NULL;
168 mol2 = seleMan2_.nextSelectedMolecule(jj)) {
169 // loop over the possible donors in molecule 1:
170 for (hbd1 = mol1->beginHBondDonor(hbdi); hbd1 != NULL;
171 hbd1 = mol1->nextHBondDonor(hbdi)) {
172 dPos = hbd1->donorAtom->getPos();
173 hPos = hbd1->donatedHydrogen->getPos();
174 DH = hPos - dPos;
175 currentSnapshot_->wrapVector(DH);
176 DHdist = DH.length();
177
178 // loop over the possible acceptors in molecule 2:
179 for (hba2 = mol2->beginHBondAcceptor(hbaj); hba2 != NULL;
180 hba2 = mol2->nextHBondAcceptor(hbaj)) {
181 aPos = hba2->getPos();
182 DA = aPos - dPos;
183 currentSnapshot_->wrapVector(DA);
184 DAdist = DA.length();
185
186 // Distance criteria: are the donor and acceptor atoms
187 // close enough?
188 if (DAdist < rCut_) {
189 ctheta = dot(DH, DA) / (DHdist * DAdist);
190 theta = acos(ctheta) * 180.0 / Constants::PI;
191
192 // Angle criteria: are the D-H and D-A and vectors close?
193 if (theta < thetaCut_) {
194 // molecule 1 is a Hbond donor:
195 nHB++;
196 nD++;
197 if (usePeriodicBoundaryConditions_)
198 currentSnapshot_->wrapVector(hPos);
199 int binNo = int(nBins_ * (halfBoxZ_ + hPos[axis_]) /
200 hmat(axis_, axis_));
201 sliceQ_[binNo] += 1 / sliceVol;
202 sliceCount_[binNo] += 1;
203 }
204 }
205 }
206 }
207
208 // now loop over the possible acceptors in molecule 1:
209 for (hba1 = mol1->beginHBondAcceptor(hbai); hba1 != NULL;
210 hba1 = mol1->nextHBondAcceptor(hbai)) {
211 aPos = hba1->getPos();
212
213 // loop over the possible donors in molecule 2:
214 for (hbd2 = mol2->beginHBondDonor(hbdj); hbd2 != NULL;
215 hbd2 = mol2->nextHBondDonor(hbdj)) {
216 dPos = hbd2->donorAtom->getPos();
217
218 DA = aPos - dPos;
219 currentSnapshot_->wrapVector(DA);
220 DAdist = DA.length();
221
222 // Distance criteria: are the donor and acceptor atoms
223 // close enough?
224 if (DAdist < rCut_) {
225 hPos = hbd2->donatedHydrogen->getPos();
226 DH = hPos - dPos;
227 currentSnapshot_->wrapVector(DH);
228 DHdist = DH.length();
229 ctheta = dot(DH, DA) / (DHdist * DAdist);
230 theta = acos(ctheta) * 180.0 / Constants::PI;
231 // Angle criteria: are the D-H and D-A and vectors close?
232 if (theta < thetaCut_) {
233 // molecule 1 is a Hbond acceptor:
234 nHB++;
235 nA++;
236 if (usePeriodicBoundaryConditions_)
237 currentSnapshot_->wrapVector(hPos);
238 int binNo = int(nBins_ * (halfBoxZ_ + hPos[axis_]) /
239 hmat(axis_, axis_));
240 sliceQ_[binNo] += 1 / sliceVol;
241 sliceCount_[binNo] += 1;
242 }
243 }
244 }
245 }
246 }
247 }
248 }
249 writeDensity();
250 }
251
252 void HBondZvol::writeDensity() {
253 // compute average box length:
254
255 RealType zSum = 0.0;
256 for (std::vector<RealType>::iterator j = zBox_.begin(); j != zBox_.end();
257 ++j) {
258 zSum += *j;
259 }
260 RealType zAve = zSum / zBox_.size();
261
262 DumpReader reader(info_, dumpFilename_);
263 int nFrames = reader.getNFrames();
264
265 std::ofstream qZstream(outputFilename_.c_str());
266 if (qZstream.is_open()) {
267 qZstream << "#Hydrogen Bonds (" << axisLabel_ << ")\n";
268
269 qZstream << "#nFrames:\t" << zBox_.size() << "\n";
270 qZstream << "#selection 1: (" << selectionScript1_ << ")\n";
271 qZstream << "#selection 2: (" << selectionScript2_ << ")\n";
272 qZstream << "#" << axisLabel_
273 << "\tHydrogen Bond Density (molecules/A^3)\n";
274 for (unsigned int i = 0; i < sliceQ_.size(); ++i) {
275 RealType z = zAve * (i + 0.5) / sliceQ_.size();
276 if (sliceCount_[i] != 0) {
277 qZstream << z << "\t" << sliceQ_[i] / nFrames << "\n";
278 } else
279 qZstream << z << "\t" << 0 << "\n";
280 }
281
282 } else {
283 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
284 "HBondZvol: unable to open %s\n", outputFilename_.c_str());
285 painCave.isFatal = 1;
286 simError();
287 }
288 qZstream.close();
289 }
290} // namespace OpenMD
Real length()
Returns the length of this vector.
Definition Vector.hpp:393
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)