OpenMD 3.2
Molecular Dynamics in the Open
Loading...
Searching...
No Matches
SpatialStatistics.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/SpatialStatistics.hpp"
49
50#include <string>
51#include <vector>
52
53#include "applications/staticProps/StaticAnalyser.hpp"
54#include "brains/SimInfo.hpp"
55#include "io/DumpReader.hpp"
56#include "io/Globals.hpp"
57#include "math/Vector3.hpp"
59#include "rnemd/RNEMDParameters.hpp"
60#include "utils/Accumulator.hpp"
61#include "utils/AccumulatorView.hpp"
62#include "utils/StringUtils.hpp"
63
64namespace OpenMD {
65
66 SpatialStatistics::SpatialStatistics(SimInfo* info,
67 const std::string& filename,
68 const std::string& sele, int nbins) :
69 StaticAnalyser(info, filename, nbins),
70 selectionScript_(sele), evaluator_(info), seleMan_(info) {
71 evaluator_.loadScriptString(sele);
72 if (!evaluator_.isDynamic()) {
73 seleMan_.setSelectionSet(evaluator_.evaluate());
74 }
75
76 setOutputName(getPrefix(filename) + ".spst");
77 }
78
79 void SpatialStatistics::process() {
80 DumpReader reader(info_, dumpFilename_);
81 int nFrames = reader.getNFrames();
82 nProcessed_ = nFrames / step_;
83
84 for (int istep = 0; istep < nFrames; istep += step_) {
85 reader.readFrame(istep);
86 currentSnapshot_ = info_->getSnapshotManager()->getCurrentSnapshot();
87 processFrame(istep);
88 }
89
90 writeOutput();
91 }
92
93 void SpatialStatistics::processFrame(int) {
94 StuntDouble* sd;
95 int i;
96
97 if (evaluator_.isDynamic()) {
98 seleMan_.setSelectionSet(evaluator_.evaluate());
99 }
100
101 // loop over the selected atoms:
102 for (sd = seleMan_.beginSelected(i); sd != NULL;
103 sd = seleMan_.nextSelected(i)) {
104 // figure out where that object is:
105 Vector3d pos = sd->getPos();
106
107 int bin = getBin(pos);
108
109 // forward the work of statistics on to the subclass:
110 processStuntDouble(sd, bin);
111 }
112 }
113
114 SlabStatistics::SlabStatistics(SimInfo* info, const std::string& filename,
115 const std::string& sele, int nbins, int axis) :
116 SpatialStatistics(info, filename, sele, nbins),
117 axis_(axis) {
118 // Set the axis label for the privileged axis
119 switch (axis_) {
120 case 0:
121 axisLabel_ = "x";
122 break;
123 case 1:
124 axisLabel_ = "y";
125 break;
126 case 2:
127 default:
128 axisLabel_ = "z";
129 break;
130 }
131 }
132
133 void SlabStatistics::processFrame(int istep) {
134 RealType z;
135 hmat_ = currentSnapshot_->getHmat();
136
137 volume_ = currentSnapshot_->getVolume();
138
139 SpatialStatistics::processFrame(istep);
140 }
141
142 int SlabStatistics::getBin(Vector3d pos) {
143 currentSnapshot_->wrapVector(pos);
144 // which bin is this stuntdouble in?
145 // wrapped positions are in the range [-0.5*hmat(2,2), +0.5*hmat(2,2)]
146 // Shift molecules by half a box to have bins start at 0
147 // The modulo operator is used to wrap the case when we are
148 // beyond the end of the bins back to the beginning.
149 return int(nBins_ * (pos[axis_] / hmat_(axis_, axis_) + 0.5)) % nBins_;
150 }
151
152 ShellStatistics::ShellStatistics(SimInfo* info, const std::string& filename,
153 const std::string& sele,
154 const std::string& comsele, int nbins,
155 RealType binWidth) :
156 SpatialStatistics(info, filename, sele, nbins),
157 coordinateOrigin_(V3Zero), comSele_(comsele), comSeleMan_(info),
158 comEvaluator_(info), binWidth_(binWidth) {
159 Globals* simParams = info->getSimParams();
160 RNEMD::RNEMDParameters* rnemdParams = simParams->getRNEMDParameters();
161 bool hasCoordinateOrigin = rnemdParams->haveCoordinateOrigin();
162
163 if (hasCoordinateOrigin) {
164 std::vector<RealType> co = rnemdParams->getCoordinateOrigin();
165 if (co.size() != 3) {
166 snprintf(
167 painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
168 "ShellStatistics: Incorrect number of parameters specified for "
169 "coordinateOrigin.\n"
170 "\tthere should be 3 parameters, but %zu were specified.\n",
171 co.size());
172 painCave.isFatal = 1;
173 simError();
174 }
175 coordinateOrigin_.x() = co[0];
176 coordinateOrigin_.y() = co[1];
177 coordinateOrigin_.z() = co[2];
178 } else {
179 if (!comSele_.empty()) {
180 comEvaluator_.loadScriptString(comSele_);
181 if (!comEvaluator_.isDynamic()) {
182 comSeleMan_.setSelectionSet(comEvaluator_.evaluate());
183 if (comSeleMan_.getSelectionCount() != 1) {
184 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
185 "ShellStatistics: More than one selected object in "
186 "comsele.\n"
187 "\tThere are %d selected objects.\n",
188 comSeleMan_.getSelectionCount());
189 painCave.isFatal = 1;
190 simError();
191 }
192 int isd;
193 StuntDouble* sd = comSeleMan_.beginSelected(isd);
194 coordinateOrigin_ = sd->getPos();
195 }
196 } else {
197 coordinateOrigin_ = V3Zero;
198 }
199 }
200 }
201
202 void ShellStatistics::processFrame(int istep) {
203 if (comEvaluator_.isDynamic()) {
204 comSeleMan_.setSelectionSet(comEvaluator_.evaluate());
205 if (comSeleMan_.getSelectionCount() != 1) {
206 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
207 "ShellStatistics: More than one selected object in "
208 "comsele.\n"
209 "\tThere are %d selected objects.\n",
210 comSeleMan_.getSelectionCount());
211 painCave.isFatal = 1;
212 simError();
213 }
214 int isd;
215 StuntDouble* sd = comSeleMan_.beginSelected(isd);
216 coordinateOrigin_ = sd->getPos();
217
218 SpatialStatistics::processFrame(istep);
219 }
220 }
221
222 int ShellStatistics::getBin(Vector3d pos) {
223 Vector3d rPos = pos - coordinateOrigin_;
224 return int(rPos.length() / binWidth_);
225 }
226} // 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)