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