OpenMD 3.2
Molecular Dynamics in the Open
Loading...
Searching...
No Matches
BondAngleDistribution.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/staticProps/BondAngleDistribution.hpp"
49
50#include "io/DumpReader.hpp"
52#include "utils/Constants.hpp"
53#include "utils/Revision.hpp"
54#include "utils/simError.h"
55
56using namespace std;
57namespace OpenMD {
58
59 BondAngleDistribution::BondAngleDistribution(SimInfo* info,
60 const string& filename,
61 const string& sele, double rCut,
62 int nbins) :
63 StaticAnalyser(info, filename, nbins),
64 selectionScript_(sele), seleMan_(info), evaluator_(info) {
65 setAnalysisType("Bond Angle Distribution");
66 setOutputName(getPrefix(filename) + ".bad");
67
68 evaluator_.loadScriptString(sele);
69 if (!evaluator_.isDynamic()) {
70 seleMan_.setSelectionSet(evaluator_.evaluate());
71 }
72
73 // Set up cutoff radius:
74
75 rCut_ = rCut;
76
77 std::stringstream params;
78 params << " rcut = " << rCut_ << ", nbins = " << nBins_;
79 const std::string paramString = params.str();
80 setParameterString(paramString);
81
82 // Theta can take values from 0 to 180
83
84 deltaTheta_ = (180.0) / nBins_;
85 histogram_.resize(nBins_);
86 }
87
88 void BondAngleDistribution::initializeHistogram() {
89 for (int bin = 0; bin < nBins_; bin++) {
90 histogram_[bin] = 0;
91 }
92 }
93
94 void BondAngleDistribution::process() {
95 Molecule* mol;
96 Atom* atom;
97 int myIndex;
98 SimInfo::MoleculeIterator mi;
99 Molecule::AtomIterator ai;
100 StuntDouble* sd;
101 Vector3d vec;
102 std::vector<Vector3d> bondvec;
103 RealType r;
104 int nBonds;
105 int i;
106
107 bool usePeriodicBoundaryConditions_ =
108 info_->getSimParams()->getUsePeriodicBoundaryConditions();
109
110 DumpReader reader(info_, dumpFilename_);
111 int nFrames = reader.getNFrames();
112 frameCounter_ = 0;
113
114 nTotBonds_ = 0;
115
116 for (int istep = 0; istep < nFrames; istep += step_) {
117 reader.readFrame(istep);
118 frameCounter_++;
119 currentSnapshot_ = info_->getSnapshotManager()->getCurrentSnapshot();
120
121 if (evaluator_.isDynamic()) {
122 seleMan_.setSelectionSet(evaluator_.evaluate());
123 }
124
125 // outer loop is over the selected StuntDoubles:
126
127 for (sd = seleMan_.beginSelected(i); sd != NULL;
128 sd = seleMan_.nextSelected(i)) {
129 myIndex = sd->getGlobalIndex();
130 nBonds = 0;
131 bondvec.clear();
132
133 // inner loop is over all other atoms in the system:
134
135 for (mol = info_->beginMolecule(mi); mol != NULL;
136 mol = info_->nextMolecule(mi)) {
137 for (atom = mol->beginAtom(ai); atom != NULL;
138 atom = mol->nextAtom(ai)) {
139 if (atom->getGlobalIndex() != myIndex) {
140 vec = sd->getPos() - atom->getPos();
141
142 if (usePeriodicBoundaryConditions_)
143 currentSnapshot_->wrapVector(vec);
144
145 // Calculate "bonds" and make a pair list
146
147 r = vec.length();
148
149 // Check to see if neighbor is in bond cutoff
150
151 if (r < rCut_) {
152 // Add neighbor to bond list's
153 bondvec.push_back(vec);
154 nBonds++;
155 nTotBonds_++;
156 }
157 }
158 }
159
160 for (int i = 0; i < nBonds - 1; i++) {
161 Vector3d vec1 = bondvec[i];
162 vec1.normalize();
163 for (int j = i + 1; j < nBonds; j++) {
164 Vector3d vec2 = bondvec[j];
165
166 vec2.normalize();
167
168 RealType theta = acos(dot(vec1, vec2)) * 180.0 / Constants::PI;
169
170 if (theta > 180.0) { theta = 360.0 - theta; }
171 int whichBin = int(theta / deltaTheta_);
172
173 histogram_[whichBin] += 2;
174 }
175 }
176 }
177 }
178 }
179
180 writeBondAngleDistribution();
181 }
182
183 void BondAngleDistribution::writeBondAngleDistribution() {
184 RealType norm = (RealType)nTotBonds_ * ((RealType)nTotBonds_ - 1.0) / 2.0;
185
186 std::ofstream ofs(getOutputFileName().c_str());
187
188 if (ofs.is_open()) {
189 Revision r;
190
191 ofs << "# " << getAnalysisType() << "\n";
192 ofs << "# OpenMD " << r.getFullRevision() << "\n";
193 ofs << "# " << r.getBuildDate() << "\n";
194 ofs << "# selection script: \"" << selectionScript_ << "\"\n";
195 if (!paramString_.empty())
196 ofs << "# parameters: " << paramString_ << "\n";
197
198 // Normalize by number of frames and write it out:
199 for (int i = 0; i < nBins_; ++i) {
200 RealType Thetaval = i * deltaTheta_;
201 ofs << Thetaval;
202 ofs << "\t" << (RealType)histogram_[i] / norm / frameCounter_;
203 ofs << "\n";
204 }
205
206 ofs.close();
207
208 } else {
209 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
210 "BondAngleDistribution: unable to open %s\n",
211 (getOutputFileName() + "q").c_str());
212 painCave.isFatal = 1;
213 simError();
214 }
215 }
216} // 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)