OpenMD 3.2
Molecular Dynamics in the Open
Loading...
Searching...
No Matches
HBondZvol.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 "HBondZvol.hpp"
49
50#include <fstream>
51#include <string>
52#include <vector>
53
54#include "io/DumpReader.hpp"
56#include "utils/Constants.hpp"
57#include "utils/simError.h"
58
59namespace OpenMD {
60
61 HBondZvol::HBondZvol(SimInfo* info, const std::string& filename,
62 const std::string& sele1, const std::string& sele2,
63 double rCut, double thetaCut, int nzbins, int axis) :
64 StaticAnalyser(info, filename, nzbins),
65 selectionScript1_(sele1), seleMan1_(info), evaluator1_(info),
66 selectionScript2_(sele2), seleMan2_(info), evaluator2_(info),
67 axis_(axis) {
68 ff_ = info_->getForceField();
69
70 evaluator1_.loadScriptString(sele1);
71 if (!evaluator1_.isDynamic()) {
72 seleMan1_.setSelectionSet(evaluator1_.evaluate());
73 }
74 evaluator2_.loadScriptString(sele2);
75 if (!evaluator2_.isDynamic()) {
76 seleMan2_.setSelectionSet(evaluator2_.evaluate());
77 }
78
79 // Set up cutoff values:
80 nBins_ = nzbins;
81 rCut_ = rCut;
82 thetaCut_ = thetaCut;
83
84 // fixed number of bins
85
86 nHBonds_.resize(nBins_);
87 nDonor_.resize(nBins_);
88 nAcceptor_.resize(nBins_);
89 sliceQ_.resize(nBins_);
90 sliceCount_.resize(nBins_);
91 vol_.resize(nBins_);
92 std::fill(sliceQ_.begin(), sliceQ_.end(), 0.0);
93 std::fill(sliceCount_.begin(), sliceCount_.end(), 0);
94
95 switch (axis_) {
96 case 0:
97 axisLabel_ = "x";
98 break;
99 case 1:
100 axisLabel_ = "y";
101 break;
102 case 2:
103 default:
104 axisLabel_ = "z";
105 break;
106 }
107
108 setOutputName(getPrefix(filename) + ".hbondzvol");
109 }
110
111 void HBondZvol::process() {
112 Molecule* mol1;
113 Molecule* mol2;
114 Molecule::HBondDonor* hbd1;
115 Molecule::HBondDonor* hbd2;
116 std::vector<Molecule::HBondDonor*>::iterator hbdi;
117 std::vector<Molecule::HBondDonor*>::iterator hbdj;
118 std::vector<Atom*>::iterator hbai;
119 std::vector<Atom*>::iterator hbaj;
120 Atom* hba1;
121 Atom* hba2;
122 Vector3d dPos;
123 Vector3d aPos;
124 Vector3d hPos;
125 Vector3d DH;
126 Vector3d DA;
127 RealType DAdist, DHdist, theta, ctheta;
128 int ii, jj;
129 int nHB, nA, nD;
130 RealType sliceVol;
131
132 bool usePeriodicBoundaryConditions_ =
133 info_->getSimParams()->getUsePeriodicBoundaryConditions();
134
135 DumpReader reader(info_, dumpFilename_);
136 int nFrames = reader.getNFrames();
137 frameCounter_ = 0;
138
139 for (int istep = 0; istep < nFrames; istep += step_) {
140 reader.readFrame(istep);
141 currentSnapshot_ = info_->getSnapshotManager()->getCurrentSnapshot();
142
143 Mat3x3d hmat = currentSnapshot_->getHmat();
144
145 RealType Lx = hmat(0, 0);
146 RealType Ly = hmat(1, 1);
147 RealType Lz = hmat(2, 2);
148
149 zBox_.push_back(hmat(axis_, axis_));
150 sliceVol = currentSnapshot_->getVolume() / nBins_;
151
152 RealType halfBoxZ_ = hmat(axis_, axis_) / 2.0;
153
154 if (evaluator1_.isDynamic()) {
155 seleMan1_.setSelectionSet(evaluator1_.evaluate());
156 }
157
158 if (evaluator2_.isDynamic()) {
159 seleMan2_.setSelectionSet(evaluator2_.evaluate());
160 }
161
162 for (mol1 = seleMan1_.beginSelectedMolecule(ii); mol1 != NULL;
163 mol1 = seleMan1_.nextSelectedMolecule(ii)) {
164 // We're collecting statistics on the molecules in selection 1:
165 nHB = 0;
166 nA = 0;
167 nD = 0;
168 Vector3d mPos = mol1->getCom();
169
170 for (mol2 = seleMan2_.beginSelectedMolecule(jj); mol2 != NULL;
171 mol2 = seleMan2_.nextSelectedMolecule(jj)) {
172 // loop over the possible donors in molecule 1:
173 for (hbd1 = mol1->beginHBondDonor(hbdi); hbd1 != NULL;
174 hbd1 = mol1->nextHBondDonor(hbdi)) {
175 dPos = hbd1->donorAtom->getPos();
176 hPos = hbd1->donatedHydrogen->getPos();
177 DH = hPos - dPos;
178 currentSnapshot_->wrapVector(DH);
179 DHdist = DH.length();
180
181 // loop over the possible acceptors in molecule 2:
182 for (hba2 = mol2->beginHBondAcceptor(hbaj); hba2 != NULL;
183 hba2 = mol2->nextHBondAcceptor(hbaj)) {
184 aPos = hba2->getPos();
185 DA = aPos - dPos;
186 currentSnapshot_->wrapVector(DA);
187 DAdist = DA.length();
188
189 // Distance criteria: are the donor and acceptor atoms
190 // close enough?
191 if (DAdist < rCut_) {
192 ctheta = dot(DH, DA) / (DHdist * DAdist);
193 theta = acos(ctheta) * 180.0 / Constants::PI;
194
195 // Angle criteria: are the D-H and D-A and vectors close?
196 if (theta < thetaCut_) {
197 // molecule 1 is a Hbond donor:
198 nHB++;
199 nD++;
200 if (usePeriodicBoundaryConditions_)
201 currentSnapshot_->wrapVector(hPos);
202 int binNo = int(nBins_ * (halfBoxZ_ + hPos[axis_]) /
203 hmat(axis_, axis_));
204 sliceQ_[binNo] += 1 / sliceVol;
205 sliceCount_[binNo] += 1;
206 }
207 }
208 }
209 }
210
211 // now loop over the possible acceptors in molecule 1:
212 for (hba1 = mol1->beginHBondAcceptor(hbai); hba1 != NULL;
213 hba1 = mol1->nextHBondAcceptor(hbai)) {
214 aPos = hba1->getPos();
215
216 // loop over the possible donors in molecule 2:
217 for (hbd2 = mol2->beginHBondDonor(hbdj); hbd2 != NULL;
218 hbd2 = mol2->nextHBondDonor(hbdj)) {
219 dPos = hbd2->donorAtom->getPos();
220
221 DA = aPos - dPos;
222 currentSnapshot_->wrapVector(DA);
223 DAdist = DA.length();
224
225 // Distance criteria: are the donor and acceptor atoms
226 // close enough?
227 if (DAdist < rCut_) {
228 hPos = hbd2->donatedHydrogen->getPos();
229 DH = hPos - dPos;
230 currentSnapshot_->wrapVector(DH);
231 DHdist = DH.length();
232 ctheta = dot(DH, DA) / (DHdist * DAdist);
233 theta = acos(ctheta) * 180.0 / Constants::PI;
234 // Angle criteria: are the D-H and D-A and vectors close?
235 if (theta < thetaCut_) {
236 // molecule 1 is a Hbond acceptor:
237 nHB++;
238 nA++;
239 if (usePeriodicBoundaryConditions_)
240 currentSnapshot_->wrapVector(hPos);
241 int binNo = int(nBins_ * (halfBoxZ_ + hPos[axis_]) /
242 hmat(axis_, axis_));
243 sliceQ_[binNo] += 1 / sliceVol;
244 sliceCount_[binNo] += 1;
245 }
246 }
247 }
248 }
249 }
250 }
251 }
252 writeDensity();
253 }
254
255 void HBondZvol::writeDensity() {
256 // compute average box length:
257
258 RealType zSum = 0.0;
259 for (std::vector<RealType>::iterator j = zBox_.begin(); j != zBox_.end();
260 ++j) {
261 zSum += *j;
262 }
263 RealType zAve = zSum / zBox_.size();
264
265 DumpReader reader(info_, dumpFilename_);
266 int nFrames = reader.getNFrames();
267
268 std::ofstream qZstream(outputFilename_.c_str());
269 if (qZstream.is_open()) {
270 qZstream << "#Hydrogen Bonds (" << axisLabel_ << ")\n";
271
272 qZstream << "#nFrames:\t" << zBox_.size() << "\n";
273 qZstream << "#selection 1: (" << selectionScript1_ << ")\n";
274 qZstream << "#selection 2: (" << selectionScript2_ << ")\n";
275 qZstream << "#" << axisLabel_
276 << "\tHydrogen Bond Density (molecules/A^3)\n";
277 for (unsigned int i = 0; i < sliceQ_.size(); ++i) {
278 RealType z = zAve * (i + 0.5) / sliceQ_.size();
279 if (sliceCount_[i] != 0) {
280 qZstream << z << "\t" << sliceQ_[i] / nFrames << "\n";
281 } else
282 qZstream << z << "\t" << 0 << "\n";
283 }
284
285 } else {
286 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
287 "HBondZvol: unable to open %s\n", outputFilename_.c_str());
288 painCave.isFatal = 1;
289 simError();
290 }
291 qZstream.close();
292 }
293} // 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)