OpenMD 3.0
Molecular Dynamics in the Open
Loading...
Searching...
No Matches
HBondRvol.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 "HBondRvol.hpp"
46
47#include <algorithm>
48#include <fstream>
49#include <vector>
50
51#include "io/DumpReader.hpp"
53#include "utils/Constants.hpp"
54#include "utils/simError.h"
55
56namespace OpenMD {
57
58 HBondRvol::HBondRvol(SimInfo* info, const std::string& filename,
59 const std::string& sele1, const std::string& sele2,
60 const std::string& sele3, double rCut, RealType len,
61 double thetaCut, int nrbins) :
62 StaticAnalyser(info, filename, nrbins),
63 selectionScript1_(sele1), seleMan1_(info), evaluator1_(info),
64 selectionScript2_(sele2), seleMan2_(info), evaluator2_(info),
65 selectionScript3_(sele3), seleMan3_(info), evaluator3_(info), len_(len),
66 nBins_(nrbins) {
67 ff_ = info_->getForceField();
68
69 evaluator1_.loadScriptString(sele1);
70 if (!evaluator1_.isDynamic()) {
71 seleMan1_.setSelectionSet(evaluator1_.evaluate());
72 }
73 evaluator2_.loadScriptString(sele2);
74 if (!evaluator2_.isDynamic()) {
75 seleMan2_.setSelectionSet(evaluator2_.evaluate());
76 }
77 evaluator3_.loadScriptString(sele3);
78 if (!evaluator3_.isDynamic()) {
79 seleMan3_.setSelectionSet(evaluator3_.evaluate());
80 }
81
82 // Set up cutoff values:
83
84 rCut_ = rCut;
85 thetaCut_ = thetaCut;
86 deltaR_ = len_ / nBins_;
87 nBins_ = nrbins;
88
89 // fixed number of bins
90
91 nHBonds_.resize(nBins_);
92 nDonor_.resize(nBins_);
93 nAcceptor_.resize(nBins_);
94 sliceQ_.resize(nBins_);
95 binvol_.resize(nBins_);
96 sliceCount_.resize(nBins_);
97 std::fill(sliceQ_.begin(), sliceQ_.end(), 0.0);
98 std::fill(sliceCount_.begin(), sliceCount_.end(), 0);
99
100 setOutputName(getPrefix(filename) + ".hbondrvol");
101 }
102
103 void HBondRvol::process() {
104 Molecule* mol1;
105 Molecule* mol2;
106 Molecule* mol3;
107 Molecule::HBondDonor* hbd1;
108 Molecule::HBondDonor* hbd2;
109 std::vector<Molecule::HBondDonor*>::iterator hbdi;
110 std::vector<Molecule::HBondDonor*>::iterator hbdj;
111 std::vector<Atom*>::iterator hbai;
112 std::vector<Atom*>::iterator hbaj;
113
114 RealType r;
115
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 DumpReader reader(info_, dumpFilename_);
128 int nFrames = reader.getNFrames();
129 frameCounter_ = 0;
130
131 for (int istep = 0; istep < nFrames; istep += step_) {
132 reader.readFrame(istep);
133 currentSnapshot_ = info_->getSnapshotManager()->getCurrentSnapshot();
134
135 if (evaluator1_.isDynamic()) {
136 seleMan1_.setSelectionSet(evaluator1_.evaluate());
137 }
138
139 if (evaluator2_.isDynamic()) {
140 seleMan2_.setSelectionSet(evaluator2_.evaluate());
141 }
142
143 if (evaluator3_.isDynamic()) {
144 seleMan3_.setSelectionSet(evaluator3_.evaluate());
145 }
146
147 for (mol1 = seleMan1_.beginSelectedMolecule(ii); mol1 != NULL;
148 mol1 = seleMan1_.nextSelectedMolecule(ii)) {
149 // We're collecting statistics on the molecules in selection 1:
150 nHB = 0;
151 nA = 0;
152 nD = 0;
153 Vector3d mPos = mol1->getCom();
154
155 for (mol2 = seleMan2_.beginSelectedMolecule(jj); mol2 != NULL;
156 mol2 = seleMan2_.nextSelectedMolecule(jj)) {
157 // loop over the possible donors in molecule 1:
158 for (hbd1 = mol1->beginHBondDonor(hbdi); hbd1 != NULL;
159 hbd1 = mol1->nextHBondDonor(hbdi)) {
160 dPos = hbd1->donorAtom->getPos();
161 hPos = hbd1->donatedHydrogen->getPos();
162 DH = hPos - dPos;
163 currentSnapshot_->wrapVector(DH);
164 DHdist = DH.length();
165
166 // loop over the possible acceptors in molecule 2:
167 for (hba2 = mol2->beginHBondAcceptor(hbaj); hba2 != NULL;
168 hba2 = mol2->nextHBondAcceptor(hbaj)) {
169 aPos = hba2->getPos();
170 DA = aPos - dPos;
171 currentSnapshot_->wrapVector(DA);
172 DAdist = DA.length();
173
174 // Distance criteria: are the donor and acceptor atoms
175 // close enough?
176 if (DAdist < rCut_) {
177 ctheta = dot(DH, DA) / (DHdist * DAdist);
178 theta = acos(ctheta) * 180.0 / Constants::PI;
179
180 // Angle criteria: are the D-H and D-A and vectors close?
181 if (theta < thetaCut_) {
182 // molecule 1 is a Hbond donor:
183 nHB++;
184 nD++;
185 r = hPos.length();
186 int binNo = int(r / deltaR_);
187 sliceQ_[binNo] += 1;
188 sliceCount_[binNo] += 1;
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 r = hPos.length();
223 int binNo = int(r / deltaR_);
224 sliceQ_[binNo] += 1;
225 sliceCount_[binNo] += 1;
226 }
227 }
228 }
229 }
230 }
231 }
232 writeDensityR();
233 }
234 }
235
236 void HBondRvol::writeDensityR() {
237 // compute average box length:
238
239 double pi = acos(-1.0);
240 DumpReader reader(info_, dumpFilename_);
241 int nFrames = reader.getNFrames();
242 std::ofstream qRstream(outputFilename_.c_str());
243 if (qRstream.is_open()) {
244 qRstream << "# " << getAnalysisType() << "\n";
245 qRstream << "#selection 1: (" << selectionScript1_ << ")\n";
246 qRstream << "#selection 2: (" << selectionScript2_ << ")\n";
247 qRstream << "#selection 3: (" << selectionScript3_ << ")\n";
248 if (!paramString_.empty())
249 qRstream << "# parameters: " << paramString_ << "\n";
250
251 qRstream << "#distance"
252 << "\tH Bonds\n";
253 for (unsigned int i = 0; i < sliceQ_.size(); ++i) {
254 RealType Rval = (i + 0.5) * deltaR_;
255 binvol_[i] = (4 * pi * (Rval * Rval) * deltaR_);
256 if (sliceCount_[i] != 0 && binvol_[i] != 0) {
257 qRstream << Rval << "\t" << sliceQ_[i] / (binvol_[i]) / nFrames
258 << "\n";
259 } else {
260 qRstream << Rval << "\t" << 0 << "\n";
261 }
262 }
263
264 } else {
265 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
266 "HBondRvol: unable to open %s\n", outputFilename_.c_str());
267 painCave.isFatal = 1;
268 simError();
269 }
270 qRstream.close();
271 }
272} // 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)