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