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