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