OpenMD 3.1
Molecular Dynamics in the Open
Loading...
Searching...
No Matches
HBondPersistence.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 "applications/dynamicProps/HBondPersistence.hpp"
46
47#include <algorithm>
48
49#include "utils/Constants.hpp"
50
51namespace OpenMD {
52 HBondPersistence::HBondPersistence(SimInfo* info, const std::string& filename,
53 const std::string& sele1,
54 const std::string& sele2, double OOcut,
55 double thetaCut, double OHcut) :
56 TimeCorrFunc<RealType>(info, filename, sele1, sele2),
57 OOCut_(OOcut), thetaCut_(thetaCut), OHCut_(OHcut) {
58 setCorrFuncType("HBondPersistence");
59 setOutputName(getPrefix(dumpFilename_) + ".HBpersistence");
60
61 std::stringstream params;
62 params << " OOcut = " << OOCut_ << ", thetacut = " << thetaCut_
63 << ", OHcut = " << OHCut_;
64 const std::string paramString = params.str();
65 setParameterString(paramString);
66
67 // nFrames_ is initialized in MultipassCorrFunc:
68 GIDtoDonor_.resize(nFrames_);
69 DonorToGID_.resize(nFrames_);
70 acceptor_.resize(nFrames_);
71 }
72
73 void HBondPersistence::computeFrame(int istep) {
74 Molecule* mol1;
75 Molecule* mol2;
76 std::vector<Molecule::HBondDonor*>::iterator hbdi;
77 Molecule::HBondDonor* hbd;
78 std::vector<Atom*>::iterator hbai;
79 Atom* hba;
80 Vector3d dPos;
81 Vector3d aPos;
82 Vector3d hPos;
83 Vector3d DH;
84 Vector3d DA;
85 Vector3d HA;
86 Vector3d uDA;
87 RealType DAdist, DHdist, HAdist, theta, ctheta;
88 int ii, jj;
89 int hInd, aInd, index;
90
91 // Map of atomic global IDs to donor atoms:
92 GIDtoDonor_[istep].resize(info_->getNGlobalAtoms(), -1);
93
94 if (!uniqueSelections_) { seleMan2_ = seleMan1_; }
95
96 if (evaluator1_.isDynamic()) {
97 seleMan1_.setSelectionSet(evaluator1_.evaluate());
98 }
99
100 if (uniqueSelections_ && evaluator2_.isDynamic()) {
101 seleMan2_.setSelectionSet(evaluator2_.evaluate());
102 }
103
104 for (mol1 = seleMan1_.beginSelectedMolecule(ii); mol1 != NULL;
105 mol1 = seleMan1_.nextSelectedMolecule(ii)) {
106 for (mol2 = seleMan2_.beginSelectedMolecule(jj); mol2 != NULL;
107 mol2 = seleMan2_.nextSelectedMolecule(jj)) {
108 // loop over the possible donors in molecule 1:
109 for (hbd = mol1->beginHBondDonor(hbdi); hbd != NULL;
110 hbd = mol1->nextHBondDonor(hbdi)) {
111 dPos = hbd->donorAtom->getPos();
112 hPos = hbd->donatedHydrogen->getPos();
113 DH = hPos - dPos;
114 currentSnapshot_->wrapVector(DH);
115 DHdist = DH.length();
116
117 hInd = hbd->donatedHydrogen->getGlobalIndex();
118 aInd = -1;
119
120 // loop over the possible acceptors in molecule 2:
121 for (hba = mol2->beginHBondAcceptor(hbai); hba != NULL;
122 hba = mol2->nextHBondAcceptor(hbai)) {
123 aPos = hba->getPos();
124 DA = aPos - dPos;
125 currentSnapshot_->wrapVector(DA);
126 DAdist = DA.length();
127
128 // Distance criteria: are the donor and acceptor atoms
129 // close enough?
130 if (DAdist < OOCut_) {
131 HA = aPos - hPos;
132 currentSnapshot_->wrapVector(HA);
133 HAdist = HA.length();
134
135 ctheta = dot(DH, DA) / (DHdist * DAdist);
136 theta = acos(ctheta) * 180.0 / Constants::PI;
137
138 // Angle criteria: are the D-H and D-A and vectors close?
139 if (theta < thetaCut_ && HAdist < OHCut_) {
140 // molecule 2 is a Hbond acceptor:
141 aInd = hba->getGlobalIndex();
142
143 index = acceptor_[istep].size();
144 GIDtoDonor_[istep][hInd] = index;
145
146 acceptor_[istep].push_back(aInd);
147 DonorToGID_[istep].push_back(hInd);
148 }
149 }
150 }
151 }
152
153 // loop over the possible donors in molecule 2:
154 for (hbd = mol2->beginHBondDonor(hbdi); hbd != NULL;
155 hbd = mol2->nextHBondDonor(hbdi)) {
156 dPos = hbd->donorAtom->getPos();
157 hPos = hbd->donatedHydrogen->getPos();
158 DH = hPos - dPos;
159 currentSnapshot_->wrapVector(DH);
160 DHdist = DH.length();
161
162 hInd = hbd->donatedHydrogen->getGlobalIndex();
163 aInd = -1;
164
165 // loop over the possible acceptors in molecule 1:
166 for (hba = mol1->beginHBondAcceptor(hbai); hba != NULL;
167 hba = mol1->nextHBondAcceptor(hbai)) {
168 aPos = hba->getPos();
169 DA = aPos - dPos;
170 currentSnapshot_->wrapVector(DA);
171 DAdist = DA.length();
172
173 // Distance criteria: are the donor and acceptor atoms
174 // close enough?
175 if (DAdist < OOCut_) {
176 HA = aPos - hPos;
177 currentSnapshot_->wrapVector(HA);
178 HAdist = HA.length();
179
180 ctheta = dot(DH, DA) / (DHdist * DAdist);
181 theta = acos(ctheta) * 180.0 / Constants::PI;
182
183 // Angle criteria: are the D-H and D-A and vectors close?
184 if (theta < thetaCut_ && HAdist < OHCut_) {
185 // molecule 1 is a Hbond acceptor:
186 aInd = hba->getGlobalIndex();
187 index = acceptor_[istep].size();
188 GIDtoDonor_[istep][hInd] = index;
189
190 acceptor_[istep].push_back(aInd);
191 DonorToGID_[istep].push_back(hInd);
192 }
193 }
194 }
195 }
196 }
197 }
198 }
199
200 void HBondPersistence::correlation() {
201 std::vector<int> s1;
202 std::vector<int>::iterator i1;
203
204 RealType corrVal;
205 int index1, index2, count, gid;
206
207 for (int i = 0; i < nFrames_; ++i) {
208 RealType time1 = times_[i];
209 s1 = DonorToGID_[i];
210
211 for (int j = i; j < nFrames_; ++j) {
212 // Perform a sanity check on the actual configuration times to
213 // make sure the configurations are spaced the same amount the
214 // sample time said they were spaced:
215
216 RealType time2 = times_[j];
217
218 if (fabs((time2 - time1) - (j - i) * deltaTime_) > 1.0e-4) {
219 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
220 "HBondPersistence::correlation Error: sampleTime (%f)\n"
221 "\tin %s does not match actual time-spacing between\n"
222 "\tconfigurations %d (t = %f) and %d (t = %f).\n",
223 deltaTime_, dumpFilename_.c_str(), i, time1, j, time2);
224 painCave.isFatal = 1;
225 simError();
226 }
227
228 int timeBin = int((time2 - time1) / deltaTime_ + 0.5);
229
230 corrVal = 0.0;
231 count = s1.size();
232
233 // loop over the H-bond donors found in frame i:
234
235 for (i1 = s1.begin(); i1 != s1.end(); ++i1) {
236 // gid is the global ID of H-bond donor index1 in frame i:
237 gid = *i1;
238
239 index1 = GIDtoDonor_[i][gid];
240
241 // find matching donor in frame j:
242 index2 = GIDtoDonor_[j][gid];
243
244 // sometimes the donor doesn't have a Hydrogen bond in a
245 // given frame, so the index will default to -1:
246
247 if (index2 < 0) {
248 corrVal += 0;
249 } else {
250 if (acceptor_[i][index1] == acceptor_[j][index2])
251 corrVal += 1;
252 else
253 corrVal += 0;
254 }
255 }
256 histogram_[timeBin] += corrVal;
257 count_[timeBin] += count;
258 }
259 }
260 }
261
262 void HBondPersistence::postCorrelate() {
263 for (unsigned int i = 0; i < nTimeBins_; ++i) {
264 if (count_[i] > 0) {
265 histogram_[i] /= count_[i];
266 } else {
267 histogram_[i] = 0;
268 }
269 }
270 }
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)