OpenMD 3.2
Molecular Dynamics in the Open
Loading...
Searching...
No Matches
GofAngle2.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/GofAngle2.hpp"
49
50#include <algorithm>
51#include <fstream>
52#include <sstream>
53
54#include "primitives/Atom.hpp"
55#include "types/MultipoleAdapter.hpp"
56#include "utils/Revision.hpp"
57#include "utils/simError.h"
58
59namespace OpenMD {
60
61 GofAngle2::GofAngle2(SimInfo* info, const std::string& filename,
62 const std::string& sele1, const std::string& sele2,
63 int nangleBins) :
64 RadialDistrFunc(info, filename, sele1, sele2, nangleBins),
65 doSele3_(false), seleMan3_(info), evaluator3_(info) {
66 setAnalysisType("Radial Distribution Function");
67 setOutputName(getPrefix(filename) + ".gto");
68
69 deltaCosAngle_ = 2.0 / nBins_;
70
71 std::stringstream params;
72 params << " nAngleBins = " << nBins_
73 << ", deltaCosAngle = " << deltaCosAngle_;
74 const std::string paramString = params.str();
75 setParameterString(paramString);
76
77 histogram_.resize(nBins_);
78 avgGofr_.resize(nBins_);
79 for (unsigned int i = 0; i < nBins_; ++i) {
80 histogram_[i].resize(nBins_);
81 avgGofr_[i].resize(nBins_);
82 }
83 }
84
85 GofAngle2::GofAngle2(SimInfo* info, const std::string& filename,
86 const std::string& sele1, const std::string& sele2,
87 const std::string& sele3, int nangleBins) :
88 RadialDistrFunc(info, filename, sele1, sele2, nangleBins),
89 doSele3_(true), seleMan3_(info), evaluator3_(info),
90 selectionScript3_(sele3) {
91 setOutputName(getPrefix(filename) + ".gto");
92
93 deltaCosAngle_ = 2.0 / nBins_;
94
95 histogram_.resize(nBins_);
96 avgGofr_.resize(nBins_);
97 for (unsigned int i = 0; i < nBins_; ++i) {
98 histogram_[i].resize(nBins_);
99 avgGofr_[i].resize(nBins_);
100 }
101 evaluator3_.loadScriptString(sele3);
102 if (!evaluator3_.isDynamic()) {
103 seleMan3_.setSelectionSet(evaluator3_.evaluate());
104 }
105 }
106
107 void GofAngle2::processNonOverlapping(SelectionManager& sman1,
108 SelectionManager& sman2) {
109 StuntDouble* sd1;
110 StuntDouble* sd2;
111 StuntDouble* sd3;
112 int i;
113 int j;
114 int k;
115
116 // This is the same as a non-overlapping pairwise loop structure:
117 // for (int i = 0; i < ni ; ++i ) {
118 // for (int j = 0; j < nj; ++j) {}
119 // }
120
121 if (doSele3_) {
122 if (evaluator3_.isDynamic()) {
123 seleMan3_.setSelectionSet(evaluator3_.evaluate());
124 }
125 if (sman1.getSelectionCount() != seleMan3_.getSelectionCount()) {
126 RadialDistrFunc::processNonOverlapping(sman1, sman2);
127 }
128
129 for (sd1 = sman1.beginSelected(i), sd3 = seleMan3_.beginSelected(k);
130 sd1 != NULL && sd3 != NULL;
131 sd1 = sman1.nextSelected(i), sd3 = seleMan3_.nextSelected(k)) {
132 for (sd2 = sman2.beginSelected(j); sd2 != NULL;
133 sd2 = sman2.nextSelected(j)) {
134 collectHistogram(sd1, sd2, sd3);
135 }
136 }
137 } else {
138 RadialDistrFunc::processNonOverlapping(sman1, sman2);
139 }
140 }
141
142 void GofAngle2::processOverlapping(SelectionManager& sman) {
143 StuntDouble* sd1;
144 StuntDouble* sd2;
145 StuntDouble* sd3;
146 int i;
147 int j;
148 int k;
149
150 // This is the same as a pairwise loop structure:
151 // for (int i = 0; i < n-1 ; ++i ) {
152 // for (int j = i + 1; j < n; ++j) {}
153 // }
154
155 if (doSele3_) {
156 if (evaluator3_.isDynamic()) {
157 seleMan3_.setSelectionSet(evaluator3_.evaluate());
158 }
159 if (sman.getSelectionCount() != seleMan3_.getSelectionCount()) {
160 RadialDistrFunc::processOverlapping(sman);
161 }
162 for (sd1 = sman.beginSelected(i), sd3 = seleMan3_.beginSelected(k);
163 sd1 != NULL && sd3 != NULL;
164 sd1 = sman.nextSelected(i), sd3 = seleMan3_.nextSelected(k)) {
165 for (j = i, sd2 = sman.nextSelected(j); sd2 != NULL;
166 sd2 = sman.nextSelected(j)) {
167 collectHistogram(sd1, sd2, sd3);
168 }
169 }
170 } else {
171 RadialDistrFunc::processOverlapping(sman);
172 }
173 }
174
175 void GofAngle2::preProcess() {
176 for (unsigned int i = 0; i < avgGofr_.size(); ++i) {
177 std::fill(avgGofr_[i].begin(), avgGofr_[i].end(), 0);
178 }
179 }
180
181 void GofAngle2::initializeHistogram() {
182 npairs_ = 0;
183 for (unsigned int i = 0; i < histogram_.size(); ++i)
184 std::fill(histogram_[i].begin(), histogram_[i].end(), 0);
185 }
186
187 void GofAngle2::processHistogram() {
188 // std::for_each(avgGofr_.begin(), avgGofr_.end(),
189 // std::plus<std::vector<int>>)
190 }
191
192 void GofAngle2::collectHistogram(StuntDouble* sd1, StuntDouble* sd2) {
193 bool usePeriodicBoundaryConditions_ =
194 info_->getSimParams()->getUsePeriodicBoundaryConditions();
195
196 if (sd1 == sd2) { return; }
197
198 Vector3d pos1 = sd1->getPos();
199 Vector3d pos2 = sd2->getPos();
200 Vector3d r12 = pos1 - pos2;
201 if (usePeriodicBoundaryConditions_) currentSnapshot_->wrapVector(r12);
202
203 AtomType* atype1 = static_cast<Atom*>(sd1)->getAtomType();
204 AtomType* atype2 = static_cast<Atom*>(sd2)->getAtomType();
205 MultipoleAdapter ma1 = MultipoleAdapter(atype1);
206 MultipoleAdapter ma2 = MultipoleAdapter(atype2);
207
208 if (!sd1->isDirectional()) {
209 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
210 "GofAngle2: attempted to use a non-directional object: %s\n",
211 sd1->getType().c_str());
212 painCave.isFatal = 1;
213 simError();
214 }
215
216 if (!sd2->isDirectional()) {
217 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
218 "GofAngle2: attempted to use a non-directional object: %s\n",
219 sd2->getType().c_str());
220 painCave.isFatal = 1;
221 simError();
222 }
223
224 Vector3d dipole1, dipole2;
225 if (ma1.isDipole())
226 dipole1 = sd1->getDipole();
227 else
228 dipole1 = sd1->getA().transpose() * V3Z;
229
230 if (ma2.isDipole())
231 dipole2 = sd2->getDipole();
232 else
233 dipole2 = sd2->getA().transpose() * V3Z;
234
235 r12.normalize();
236 dipole1.normalize();
237 dipole2.normalize();
238
239 RealType cosAngle1 = dot(r12, dipole1);
240 RealType cosAngle2 = dot(dipole1, dipole2);
241
242 RealType halfBin = (nBins_ - 1) * 0.5;
243 int angleBin1 = int(halfBin * (cosAngle1 + 1.0));
244 int angleBin2 = int(halfBin * (cosAngle2 + 1.0));
245
246 ++histogram_[angleBin1][angleBin2];
247 ++npairs_;
248 }
249
250 void GofAngle2::collectHistogram(StuntDouble* sd1, StuntDouble* sd2,
251 StuntDouble* sd3) {
252 bool usePeriodicBoundaryConditions_ =
253 info_->getSimParams()->getUsePeriodicBoundaryConditions();
254
255 if (sd1 == sd2) { return; }
256
257 Vector3d p1 = sd1->getPos();
258 Vector3d p3 = sd3->getPos();
259
260 Vector3d c = 0.5 * (p1 + p3);
261 Vector3d r13 = p3 - p1;
262
263 Vector3d r12 = sd2->getPos() - c;
264
265 if (usePeriodicBoundaryConditions_) {
266 currentSnapshot_->wrapVector(r12);
267 currentSnapshot_->wrapVector(r13);
268 }
269 r12.normalize();
270 r13.normalize();
271
272 if (!sd2->isDirectional()) {
273 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
274 "GofAngle2: attempted to use a non-directional object: %s\n",
275 sd2->getType().c_str());
276 painCave.isFatal = 1;
277 simError();
278 }
279
280 AtomType* atype2 = static_cast<Atom*>(sd2)->getAtomType();
281 MultipoleAdapter ma2 = MultipoleAdapter(atype2);
282
283 Vector3d dipole2;
284 if (ma2.isDipole())
285 dipole2 = sd2->getDipole();
286 else
287 dipole2 = sd2->getA().transpose() * V3Z;
288
289 dipole2.normalize();
290
291 RealType cosAngle1 = dot(r12, r13);
292 RealType cosAngle2 = dot(r13, dipole2);
293
294 RealType halfBin = (nBins_ - 1) * 0.5;
295 int angleBin1 = int(halfBin * (cosAngle1 + 1.0));
296 int angleBin2 = int(halfBin * (cosAngle2 + 1.0));
297
298 ++histogram_[angleBin1][angleBin2];
299 ++npairs_;
300 }
301
302 void GofAngle2::writeRdf() {
303 std::ofstream ofs(outputFilename_.c_str());
304 if (ofs.is_open()) {
305 Revision r;
306 ofs << "# " << getAnalysisType() << "\n";
307 ofs << "# OpenMD " << r.getFullRevision() << "\n";
308 ofs << "# " << r.getBuildDate() << "\n";
309 ofs << "# selection script1: \"" << selectionScript1_;
310 ofs << "\"\tselection script2: \"" << selectionScript2_ << "\"";
311 if (doSele3_) {
312 ofs << "\tselection script3: \"" << selectionScript3_ << "\"\n";
313 } else {
314 ofs << "\n";
315 }
316
317 if (!paramString_.empty())
318 ofs << "# parameters: " << paramString_ << "\n";
319
320 for (unsigned int i = 0; i < avgGofr_.size(); ++i) {
321 // RealType cosAngle1 = -1.0 + (i + 0.5)*deltaCosAngle_;
322
323 for (unsigned int j = 0; j < avgGofr_[i].size(); ++j) {
324 // RealType cosAngle2 = -1.0 + (j + 0.5)*deltaCosAngle_;
325 ofs << avgGofr_[i][j] / nProcessed_ << "\t";
326 }
327 ofs << "\n";
328 }
329
330 } else {
331 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
332 "GofAngle2: unable to open %s\n", outputFilename_.c_str());
333 painCave.isFatal = 1;
334 simError();
335 }
336
337 ofs.close();
338 }
339
340} // namespace OpenMD
Radial Distribution Function.
One of the heavy-weight classes of OpenMD, SimInfo maintains objects and variables relating to the cu...
Definition SimInfo.hpp:96
void normalize()
Normalizes this vector in place.
Definition Vector.hpp:406
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)