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