OpenMD 3.1
Molecular Dynamics in the Open
Loading...
Searching...
No Matches
HBondZ.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 "HBondZ.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 HBondZ::HBondZ(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 std::fill(sliceQ_.begin(), sliceQ_.end(), 0.0);
89 std::fill(sliceCount_.begin(), sliceCount_.end(), 0);
90
91 switch (axis_) {
92 case 0:
93 axisLabel_ = "x";
94 break;
95 case 1:
96 axisLabel_ = "y";
97 break;
98 case 2:
99 default:
100 axisLabel_ = "z";
101 break;
102 }
103
104 setOutputName(getPrefix(filename) + ".hbondz");
105 }
106
107 void HBondZ::process() {
108 Molecule* mol1;
109 Molecule* mol2;
110 Molecule::HBondDonor* hbd1;
111 Molecule::HBondDonor* hbd2;
112 std::vector<Molecule::HBondDonor*>::iterator hbdi;
113 std::vector<Molecule::HBondDonor*>::iterator hbdj;
114 std::vector<Atom*>::iterator hbai;
115 std::vector<Atom*>::iterator hbaj;
116 Atom* hba1;
117 Atom* hba2;
118 Vector3d dPos;
119 Vector3d aPos;
120 Vector3d hPos;
121 Vector3d DH;
122 Vector3d DA;
123 RealType DAdist, DHdist, theta, ctheta;
124 int ii, jj;
125 int nHB, nA, nD;
126
127 bool usePeriodicBoundaryConditions_ =
128 info_->getSimParams()->getUsePeriodicBoundaryConditions();
129
130 DumpReader reader(info_, dumpFilename_);
131 int nFrames = reader.getNFrames();
132 frameCounter_ = 0;
133
134 for (int istep = 0; istep < nFrames; istep += step_) {
135 reader.readFrame(istep);
136 currentSnapshot_ = info_->getSnapshotManager()->getCurrentSnapshot();
137
138 Mat3x3d hmat = currentSnapshot_->getHmat();
139 zBox_.push_back(hmat(axis_, axis_));
140
141 RealType halfBoxZ_ = hmat(axis_, axis_) / 2.0;
142
143 if (evaluator1_.isDynamic()) {
144 seleMan1_.setSelectionSet(evaluator1_.evaluate());
145 }
146
147 if (evaluator2_.isDynamic()) {
148 seleMan2_.setSelectionSet(evaluator2_.evaluate());
149 }
150
151 for (mol1 = seleMan1_.beginSelectedMolecule(ii); mol1 != NULL;
152 mol1 = seleMan1_.nextSelectedMolecule(ii)) {
153 // We're collecting statistics on the molecules in selection 1:
154 nHB = 0;
155 nA = 0;
156 nD = 0;
157 Vector3d mPos = mol1->getCom();
158
159 for (mol2 = seleMan2_.beginSelectedMolecule(jj); mol2 != NULL;
160 mol2 = seleMan2_.nextSelectedMolecule(jj)) {
161 // loop over the possible donors in molecule 1:
162 for (hbd1 = mol1->beginHBondDonor(hbdi); hbd1 != NULL;
163 hbd1 = mol1->nextHBondDonor(hbdi)) {
164 dPos = hbd1->donorAtom->getPos();
165 hPos = hbd1->donatedHydrogen->getPos();
166 DH = hPos - dPos;
167 currentSnapshot_->wrapVector(DH);
168 DHdist = DH.length();
169
170 // loop over the possible acceptors in molecule 2:
171 for (hba2 = mol2->beginHBondAcceptor(hbaj); hba2 != NULL;
172 hba2 = mol2->nextHBondAcceptor(hbaj)) {
173 aPos = hba2->getPos();
174 DA = aPos - dPos;
175 currentSnapshot_->wrapVector(DA);
176 DAdist = DA.length();
177
178 // Distance criteria: are the donor and acceptor atoms
179 // close enough?
180 if (DAdist < rCut_) {
181 ctheta = dot(DH, DA) / (DHdist * DAdist);
182 theta = acos(ctheta) * 180.0 / Constants::PI;
183
184 // Angle criteria: are the D-H and D-A and vectors close?
185 if (theta < thetaCut_) {
186 // molecule 1 is a Hbond donor:
187 nHB++;
188 nD++;
189 }
190 }
191 }
192 }
193
194 // now loop over the possible acceptors in molecule 1:
195 for (hba1 = mol1->beginHBondAcceptor(hbai); hba1 != NULL;
196 hba1 = mol1->nextHBondAcceptor(hbai)) {
197 aPos = hba1->getPos();
198
199 // loop over the possible donors in molecule 2:
200 for (hbd2 = mol2->beginHBondDonor(hbdj); hbd2 != NULL;
201 hbd2 = mol2->nextHBondDonor(hbdj)) {
202 dPos = hbd2->donorAtom->getPos();
203
204 DA = aPos - dPos;
205 currentSnapshot_->wrapVector(DA);
206 DAdist = DA.length();
207
208 // Distance criteria: are the donor and acceptor atoms
209 // close enough?
210 if (DAdist < rCut_) {
211 hPos = hbd2->donatedHydrogen->getPos();
212 DH = hPos - dPos;
213 currentSnapshot_->wrapVector(DH);
214 DHdist = DH.length();
215 ctheta = dot(DH, DA) / (DHdist * DAdist);
216 theta = acos(ctheta) * 180.0 / Constants::PI;
217 // Angle criteria: are the D-H and D-A and vectors close?
218 if (theta < thetaCut_) {
219 // molecule 1 is a Hbond acceptor:
220 nHB++;
221 nA++;
222 }
223 }
224 }
225 }
226 }
227 if (usePeriodicBoundaryConditions_) currentSnapshot_->wrapVector(mPos);
228 int binNo =
229 int(nBins_ * (halfBoxZ_ + mPos[axis_]) / hmat(axis_, axis_));
230 sliceQ_[binNo] += nHB;
231 sliceCount_[binNo] += 1;
232 }
233 }
234 writeDensity();
235 }
236
237 void HBondZ::writeDensity() {
238 // compute average box length:
239
240 RealType zSum = 0.0;
241 for (std::vector<RealType>::iterator j = zBox_.begin(); j != zBox_.end();
242 ++j) {
243 zSum += *j;
244 }
245 RealType zAve = zSum / zBox_.size();
246
247 std::ofstream qZstream(outputFilename_.c_str());
248 if (qZstream.is_open()) {
249 qZstream << "#Hydrogen Bonds (" << axisLabel_ << ")\n";
250
251 qZstream << "#nFrames:\t" << zBox_.size() << "\n";
252 qZstream << "#selection 1: (" << selectionScript1_ << ")\n";
253 qZstream << "#selection 2: (" << selectionScript2_ << ")\n";
254 qZstream << "#" << axisLabel_ << "\tHydrogen Bonds\n";
255 for (unsigned int i = 0; i < sliceQ_.size(); ++i) {
256 RealType z = zAve * (i + 0.5) / sliceQ_.size();
257 if (sliceCount_[i] != 0) {
258 qZstream << z << "\t" << sliceQ_[i] / sliceCount_[i] << "\n";
259 } else
260 qZstream << z << "\t" << 0 << "\n";
261 }
262
263 } else {
264 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
265 "HBondZ: unable to open %s\n", outputFilename_.c_str());
266 painCave.isFatal = 1;
267 simError();
268 }
269 qZstream.close();
270 }
271} // 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)