OpenMD 3.2
Molecular Dynamics in the Open
Loading...
Searching...
No Matches
RhoR.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/* Calculates Rho(R) for nanoparticle with radius R*/
49#include "applications/staticProps/RhoR.hpp"
50
51#include <algorithm>
52#include <cmath>
53#include <fstream>
54
55#include "brains/Thermo.hpp"
56#include "io/DumpReader.hpp"
58#include "utils/Constants.hpp"
59#include "utils/simError.h"
60
61namespace OpenMD {
62
63 RhoR::RhoR(SimInfo* info, const std::string& filename,
64 const std::string& sele, RealType len, int nrbins,
65 RealType particleR) :
66 StaticAnalyser(info, filename, nrbins),
67 selectionScript_(sele), evaluator_(info), seleMan_(info), len_(len) {
68 evaluator_.loadScriptString(sele);
69 if (!evaluator_.isDynamic()) {
70 seleMan_.setSelectionSet(evaluator_.evaluate());
71 }
72
73 deltaR_ = len_ / nBins_;
74
75 histogram_.resize(nBins_);
76 avgRhoR_.resize(nBins_);
77 particleR_ = particleR;
78 setOutputName(getPrefix(filename) + ".RhoR");
79 }
80
81 void RhoR::process() {
82 Thermo thermo(info_);
83 DumpReader reader(info_, dumpFilename_);
84 int nFrames = reader.getNFrames();
85 nProcessed_ = nFrames / step_;
86
87 std::fill(avgRhoR_.begin(), avgRhoR_.end(), 0.0);
88 std::fill(histogram_.begin(), histogram_.end(), 0);
89
90 for (int istep = 0; istep < nFrames; istep += step_) {
91 int i;
92 StuntDouble* sd;
93 reader.readFrame(istep);
94 currentSnapshot_ = info_->getSnapshotManager()->getCurrentSnapshot();
95 Vector3d CenterOfMass = thermo.getCom();
96
97 if (evaluator_.isDynamic()) {
98 seleMan_.setSelectionSet(evaluator_.evaluate());
99 }
100
101 // determine which atom belongs to which slice
102 for (sd = seleMan_.beginSelected(i); sd != NULL;
103 sd = seleMan_.nextSelected(i)) {
104 Vector3d pos = sd->getPos();
105 Vector3d r12 = CenterOfMass - pos;
106
107 RealType distance = r12.length();
108
109 if (distance < len_) {
110 int whichBin = int(distance / deltaR_);
111 histogram_[whichBin] += 1;
112 }
113 }
114 }
115
116 processHistogram();
117 writeRhoR();
118 }
119
120 void RhoR::processHistogram() {
121 RealType particleDensity = 3.0 * info_->getNGlobalMolecules() /
122 (4.0 * Constants::PI * pow(particleR_, 3));
123 RealType pairConstant = (4.0 * Constants::PI * particleDensity) / 3.0;
124
125 for (unsigned int i = 0; i < histogram_.size(); ++i) {
126 RealType rLower = i * deltaR_;
127 RealType rUpper = rLower + deltaR_;
128 RealType volSlice =
129 (rUpper * rUpper * rUpper) - (rLower * rLower * rLower);
130 RealType nIdeal = volSlice * pairConstant;
131
132 avgRhoR_[i] += histogram_[i] / nIdeal;
133 }
134 }
135
136 void RhoR::writeRhoR() {
137 std::ofstream rdfStream(outputFilename_.c_str());
138 if (rdfStream.is_open()) {
139 rdfStream << "#radial density function rho(r)\n";
140 rdfStream << "#r\tcorrValue\n";
141 for (unsigned int i = 0; i < avgRhoR_.size(); ++i) {
142 RealType r = deltaR_ * (i + 0.5);
143 rdfStream << r << "\t" << avgRhoR_[i] / nProcessed_ << "\n";
144 }
145
146 } else {
147 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
148 "RhoR: unable to open %s\n", outputFilename_.c_str());
149 painCave.isFatal = 1;
150 simError();
151 }
152
153 rdfStream.close();
154 }
155
156} // 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.
std::string getPrefix(const std::string &str)
Real distance(const DynamicVector< Real > &v1, const DynamicVector< Real > &v2)
Returns the distance between two DynamicVectors.