OpenMD 3.2
Molecular Dynamics in the Open
Loading...
Searching...
No Matches
GofRAngle.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/GofRAngle.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 GofRAngle::GofRAngle(SimInfo* info, const std::string& filename,
62 const std::string& sele1, const std::string& sele2,
63 RealType len, int nrbins, int nangleBins) :
64 RadialDistrFunc(info, filename, sele1, sele2, nrbins),
65 nAngleBins_(nangleBins), len_(len), doSele3_(false), seleMan3_(info),
66 evaluator3_(info) {
67 deltaR_ = len_ / (double)nBins_;
68 deltaCosAngle_ = 2.0 / (double)nAngleBins_;
69 histogram_.resize(nBins_);
70 avgGofr_.resize(nBins_);
71 for (unsigned int i = 0; i < nBins_; ++i) {
72 histogram_[i].resize(nAngleBins_);
73 avgGofr_[i].resize(nAngleBins_);
74 }
75
76 setAnalysisType("Radial Distribution Function");
77
78 std::stringstream params;
79 params << " nBins = " << nBins_ << ", maxLen = " << len_
80 << ", deltaR = " << deltaR_ << ", nAngleBins = " << nAngleBins_
81 << ", deltaCosAngle = " << deltaCosAngle_;
82 const std::string paramString = params.str();
83 setParameterString(paramString);
84 }
85
86 GofRAngle::GofRAngle(SimInfo* info, const std::string& filename,
87 const std::string& sele1, const std::string& sele2,
88 const std::string& sele3, RealType len, int nrbins,
89 int nangleBins) :
90 RadialDistrFunc(info, filename, sele1, sele2, nrbins),
91 nAngleBins_(nangleBins), len_(len), doSele3_(true),
92 selectionScript3_(sele3), seleMan3_(info), evaluator3_(info) {
93 deltaR_ = len_ / (double)nBins_;
94 deltaCosAngle_ = 2.0 / (double)nAngleBins_;
95 histogram_.resize(nBins_);
96 avgGofr_.resize(nBins_);
97 for (unsigned int i = 0; i < nBins_; ++i) {
98 histogram_[i].resize(nAngleBins_);
99 avgGofr_[i].resize(nAngleBins_);
100 }
101
102 evaluator3_.loadScriptString(sele3);
103 if (!evaluator3_.isDynamic()) {
104 seleMan3_.setSelectionSet(evaluator3_.evaluate());
105 }
106 }
107
108 void GofRAngle::processNonOverlapping(SelectionManager& sman1,
109 SelectionManager& sman2) {
110 StuntDouble* sd1;
111 StuntDouble* sd2;
112 StuntDouble* sd3;
113 int i;
114 int j;
115 int k;
116
117 // This is the same as a non-overlapping pairwise loop structure:
118 // for (int i = 0; i < ni ; ++i ) {
119 // for (int j = 0; j < nj; ++j) {}
120 // }
121
122 if (doSele3_) {
123 if (evaluator3_.isDynamic()) {
124 seleMan3_.setSelectionSet(evaluator3_.evaluate());
125 }
126 if (sman1.getSelectionCount() != seleMan3_.getSelectionCount()) {
127 RadialDistrFunc::processNonOverlapping(sman1, sman2);
128 }
129
130 for (sd1 = sman1.beginSelected(i), sd3 = seleMan3_.beginSelected(k);
131 sd1 != NULL && sd3 != NULL;
132 sd1 = sman1.nextSelected(i), sd3 = seleMan3_.nextSelected(k)) {
133 for (sd2 = sman2.beginSelected(j); sd2 != NULL;
134 sd2 = sman2.nextSelected(j)) {
135 collectHistogram(sd1, sd2, sd3);
136 }
137 }
138 } else {
139 RadialDistrFunc::processNonOverlapping(sman1, sman2);
140 }
141 }
142
143 void GofRAngle::processOverlapping(SelectionManager& sman) {
144 StuntDouble* sd1;
145 StuntDouble* sd2;
146 StuntDouble* sd3;
147 int i;
148 int j;
149 int k;
150
151 // This is the same as a pairwise loop structure:
152 // for (int i = 0; i < n-1 ; ++i ) {
153 // for (int j = i + 1; j < n; ++j) {}
154 // }
155
156 if (doSele3_) {
157 if (evaluator3_.isDynamic()) {
158 seleMan3_.setSelectionSet(evaluator3_.evaluate());
159 }
160 if (sman.getSelectionCount() != seleMan3_.getSelectionCount()) {
161 RadialDistrFunc::processOverlapping(sman);
162 }
163 for (sd1 = sman.beginSelected(i), sd3 = seleMan3_.beginSelected(k);
164 sd1 != NULL && sd3 != NULL;
165 sd1 = sman.nextSelected(i), sd3 = seleMan3_.nextSelected(k)) {
166 for (j = i, sd2 = sman.nextSelected(j); sd2 != NULL;
167 sd2 = sman.nextSelected(j)) {
168 collectHistogram(sd1, sd2, sd3);
169 }
170 }
171 } else {
172 RadialDistrFunc::processOverlapping(sman);
173 }
174 }
175
176 void GofRAngle::preProcess() {
177 for (unsigned int i = 0; i < avgGofr_.size(); ++i) {
178 std::fill(avgGofr_[i].begin(), avgGofr_[i].end(), 0);
179 }
180 }
181
182 void GofRAngle::initializeHistogram() {
183 npairs_ = 0;
184 for (unsigned int i = 0; i < histogram_.size(); ++i) {
185 std::fill(histogram_[i].begin(), histogram_[i].end(), 0);
186 }
187 }
188
189 void GofRAngle::processHistogram() {
190 int nPairs = getNPairs();
191 RealType volume =
192 info_->getSnapshotManager()->getCurrentSnapshot()->getVolume();
193 RealType pairDensity = nPairs / volume;
194 RealType pairConstant = (4.0 * Constants::PI * pairDensity) / 3.0;
195
196 for (unsigned int i = 0; i < histogram_.size(); ++i) {
197 RealType rLower = i * deltaR_;
198 RealType rUpper = rLower + deltaR_;
199 RealType volSlice =
200 (rUpper * rUpper * rUpper) - (rLower * rLower * rLower);
201 RealType nIdeal = volSlice * pairConstant;
202
203 for (unsigned int j = 0; j < histogram_[i].size(); ++j) {
204 avgGofr_[i][j] += histogram_[i][j] / nIdeal;
205 }
206 }
207 }
208
209 void GofRTheta::processHistogram() {
210 int nPairs = getNPairs();
211 RealType volume =
212 info_->getSnapshotManager()->getCurrentSnapshot()->getVolume();
213 RealType pairDensity = nPairs / volume;
214 RealType pairConstant =
215 (4.0 * Constants::PI * pairDensity) / (3.0 * (double)nAngleBins_);
216
217 for (unsigned int i = 0; i < histogram_.size(); ++i) {
218 RealType rLower = i * deltaR_;
219 RealType rUpper = rLower + deltaR_;
220 RealType volSlice =
221 (rUpper * rUpper * rUpper) - (rLower * rLower * rLower);
222 RealType nIdeal = volSlice * pairConstant;
223
224 for (unsigned int j = 0; j < histogram_[i].size(); ++j) {
225 avgGofr_[i][j] += histogram_[i][j] / nIdeal;
226 }
227 }
228 }
229
230 void GofRAngle::collectHistogram(StuntDouble* sd1, StuntDouble* sd2) {
231 if (sd1 == sd2) { return; }
232 bool usePeriodicBoundaryConditions_ =
233 info_->getSimParams()->getUsePeriodicBoundaryConditions();
234
235 Vector3d pos1 = sd1->getPos();
236 Vector3d pos2 = sd2->getPos();
237 Vector3d r12 = pos2 - pos1;
238 if (usePeriodicBoundaryConditions_) currentSnapshot_->wrapVector(r12);
239
240 RealType distance = r12.length();
241 int whichRBin = int(distance / deltaR_);
242
243 if (distance <= len_) {
244 RealType cosAngle = evaluateAngle(sd1, sd2);
245 RealType halfBin = (nAngleBins_ - 1) * 0.5;
246 int whichThetaBin = int(halfBin * (cosAngle + 1.0));
247 ++histogram_[whichRBin][whichThetaBin];
248
249 ++npairs_;
250 }
251 }
252
253 void GofRAngle::collectHistogram(StuntDouble* sd1, StuntDouble* sd2,
254 StuntDouble* sd3) {
255 if (sd1 == sd2) { return; }
256 bool usePeriodicBoundaryConditions_ =
257 info_->getSimParams()->getUsePeriodicBoundaryConditions();
258
259 Vector3d p1 = sd1->getPos();
260 Vector3d p3 = sd3->getPos();
261
262 Vector3d c = 0.5 * (p1 + p3);
263 Vector3d r13 = p3 - p1;
264
265 Vector3d r12 = sd2->getPos() - c;
266
267 if (usePeriodicBoundaryConditions_) {
268 currentSnapshot_->wrapVector(r12);
269 currentSnapshot_->wrapVector(r13);
270 }
271
272 RealType distance = r12.length();
273 int whichRBin = int(distance / deltaR_);
274
275 if (distance <= len_) {
276 RealType cosAngle = evaluateAngle(sd1, sd2, sd3);
277 RealType halfBin = (nAngleBins_ - 1) * 0.5;
278 int whichThetaBin = int(halfBin * (cosAngle + 1.0));
279 ++histogram_[whichRBin][whichThetaBin];
280
281 ++npairs_;
282 }
283 }
284
285 void GofRAngle::writeRdf() {
286 std::ofstream ofs(outputFilename_.c_str());
287 if (ofs.is_open()) {
288 Revision r;
289 ofs << "# " << getAnalysisType() << "\n";
290 ofs << "# OpenMD " << r.getFullRevision() << "\n";
291 ofs << "# " << r.getBuildDate() << "\n";
292 ofs << "# selection script1: \"" << selectionScript1_;
293 ofs << "\"\tselection script2: \"" << selectionScript2_ << "\"";
294 if (doSele3_) {
295 ofs << "\tselection script3: \"" << selectionScript3_ << "\"\n";
296 } else {
297 ofs << "\n";
298 }
299
300 if (!paramString_.empty())
301 ofs << "# parameters: " << paramString_ << "\n";
302
303 for (unsigned int i = 0; i < avgGofr_.size(); ++i) {
304 // RealType r = deltaR_ * (i + 0.5);
305
306 for (unsigned int j = 0; j < avgGofr_[i].size(); ++j) {
307 // RealType cosAngle = -1.0 + (j + 0.5)*deltaCosAngle_;
308 ofs << avgGofr_[i][j] / nProcessed_ << "\t";
309 }
310
311 ofs << "\n";
312 }
313
314 } else {
315 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
316 "GofRAngle: unable to open %s\n", outputFilename_.c_str());
317 painCave.isFatal = 1;
318 simError();
319 }
320
321 ofs.close();
322 }
323
324 RealType GofRTheta::evaluateAngle(StuntDouble* sd1, StuntDouble* sd2) {
325 bool usePeriodicBoundaryConditions_ =
326 info_->getSimParams()->getUsePeriodicBoundaryConditions();
327
328 Vector3d pos1 = sd1->getPos();
329 Vector3d pos2 = sd2->getPos();
330 Vector3d r12 = pos2 - pos1;
331
332 if (usePeriodicBoundaryConditions_) currentSnapshot_->wrapVector(r12);
333
334 r12.normalize();
335
336 Vector3d vec;
337
338 if (!sd1->isDirectional()) {
339 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
340 "GofRTheta: attempted to use a non-directional object: %s\n",
341 sd1->getType().c_str());
342 painCave.isFatal = 1;
343 simError();
344 }
345
346 if (sd1->isAtom()) {
347 AtomType* atype1 = static_cast<Atom*>(sd1)->getAtomType();
348 MultipoleAdapter ma1 = MultipoleAdapter(atype1);
349
350 if (ma1.isDipole())
351 vec = sd1->getDipole();
352 else
353 vec = sd1->getA().transpose() * V3Z;
354 } else {
355 vec = sd1->getA().transpose() * V3Z;
356 }
357
358 vec.normalize();
359
360 return dot(r12, vec);
361 }
362
363 RealType GofRTheta::evaluateAngle(StuntDouble* sd1, StuntDouble* sd2,
364 StuntDouble* sd3) {
365 bool usePeriodicBoundaryConditions_ =
366 info_->getSimParams()->getUsePeriodicBoundaryConditions();
367
368 Vector3d p1 = sd1->getPos();
369 Vector3d p3 = sd3->getPos();
370
371 Vector3d c = 0.5 * (p1 + p3);
372 Vector3d r13 = p3 - p1;
373
374 Vector3d r12 = sd2->getPos() - c;
375
376 if (usePeriodicBoundaryConditions_) {
377 currentSnapshot_->wrapVector(r12);
378 currentSnapshot_->wrapVector(r13);
379 }
380
381 r12.normalize();
382 r13.normalize();
383
384 return dot(r12, r13);
385 }
386
387 RealType GofROmega::evaluateAngle(StuntDouble* sd1, StuntDouble* sd2) {
388 Vector3d v1, v2;
389
390 if (!sd1->isDirectional()) {
391 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
392 "GofROmega: attempted to use a non-directional object: %s\n",
393 sd1->getType().c_str());
394 painCave.isFatal = 1;
395 simError();
396 }
397
398 if (sd1->isAtom()) {
399 AtomType* atype1 = static_cast<Atom*>(sd1)->getAtomType();
400 MultipoleAdapter ma1 = MultipoleAdapter(atype1);
401 if (ma1.isDipole())
402 v1 = sd1->getDipole();
403 else
404 v1 = sd1->getA().transpose() * V3Z;
405 } else {
406 v1 = sd1->getA().transpose() * V3Z;
407 }
408
409 if (!sd2->isDirectional()) {
410 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
411 "GofROmega attempted to use a non-directional object: %s\n",
412 sd2->getType().c_str());
413 painCave.isFatal = 1;
414 simError();
415 }
416
417 if (sd2->isAtom()) {
418 AtomType* atype2 = static_cast<Atom*>(sd2)->getAtomType();
419 MultipoleAdapter ma2 = MultipoleAdapter(atype2);
420
421 if (ma2.isDipole())
422 v2 = sd2->getDipole();
423 else
424 v2 = sd2->getA().transpose() * V3Z;
425 } else {
426 v2 = sd2->getA().transpose() * V3Z;
427 }
428
429 v1.normalize();
430 v2.normalize();
431 return dot(v1, v2);
432 }
433
434 RealType GofROmega::evaluateAngle(StuntDouble* sd1, StuntDouble* sd2,
435 StuntDouble* sd3) {
436 bool usePeriodicBoundaryConditions_ =
437 info_->getSimParams()->getUsePeriodicBoundaryConditions();
438
439 Vector3d v1;
440 Vector3d v2;
441
442 v1 = sd3->getPos() - sd1->getPos();
443 if (usePeriodicBoundaryConditions_) currentSnapshot_->wrapVector(v1);
444
445 if (!sd2->isDirectional()) {
446 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
447 "GofROmega: attempted to use a non-directional object: %s\n",
448 sd2->getType().c_str());
449 painCave.isFatal = 1;
450 simError();
451 }
452
453 if (sd2->isAtom()) {
454 AtomType* atype2 = static_cast<Atom*>(sd2)->getAtomType();
455 MultipoleAdapter ma2 = MultipoleAdapter(atype2);
456
457 if (ma2.isDipole())
458 v2 = sd2->getDipole();
459 else
460 v2 = sd2->getA().transpose() * V3Z;
461 } else {
462 v2 = sd2->getA().transpose() * V3Z;
463 }
464
465 v1.normalize();
466 v2.normalize();
467 return dot(v1, v2);
468 }
469} // 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.
Real distance(const DynamicVector< Real > &v1, const DynamicVector< Real > &v2)
Returns the distance between two DynamicVectors.