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