OpenMD 3.2
Molecular Dynamics in the Open
Loading...
Searching...
No Matches
VelocityZ.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 Velocity Map, V(axis2,axis3) in the box */
49
50#include "applications/staticProps/VelocityZ.hpp"
51
52#include <algorithm>
53#include <fstream>
54
55#include "io/DumpReader.hpp"
57#include "utils/simError.h"
58
59namespace OpenMD {
60
61 VelocityZ::VelocityZ(SimInfo* info, const std::string& filename,
62 const std::string& sele, int nbins1, int nbins2,
63 int axis1, int axis2) :
64 StaticAnalyser(info, filename, nbins1),
65 selectionScript_(sele), evaluator_(info), seleMan_(info), nBins2_(nbins2),
66 axis1_(axis1), axis2_(axis2) {
67 evaluator_.loadScriptString(sele);
68 if (!evaluator_.isDynamic()) {
69 seleMan_.setSelectionSet(evaluator_.evaluate());
70 }
71
72 /* fixed number of bins:
73 nBins_ is along the primary axis, nBins2_ along the secondary axis
74 there are nBins2_ perpendicular to each slab of nBins_
75 */
76
77 sliceSDLists_.resize(nBins2_);
78 velocity_.resize(nBins2_);
79 for (unsigned int i = 0; i < nBins2_; ++i) {
80 sliceSDLists_[i].resize(nBins_);
81 velocity_[i].resize(nBins_);
82 }
83
84 // Compute complementary axis to the two privileged axis
85 axis3_ = (3 - axis1_ - axis2_);
86
87 // Set the axis labels for the non-privileged axes
88 switch (axis1_) {
89 case 0:
90 axisLabel1_ = "x";
91 if (axis2_ == 1)
92 axisLabel2_ = "y";
93 else if (axis2_ == 2)
94 axisLabel2_ = "z";
95 break;
96 case 1:
97 axisLabel1_ = "y";
98 if (axis2_ == 0)
99 axisLabel2_ = "x";
100 else if (axis2_ == 2)
101 axisLabel2_ = "z";
102 break;
103 case 2:
104 default:
105 axisLabel1_ = "z";
106 if (axis2_ == 0)
107 axisLabel2_ = "x";
108 else if (axis2_ == 1)
109 axisLabel2_ = "y";
110 break;
111 }
112
113 setOutputName(getPrefix(filename) + ".VelocityZ");
114 }
115
116 void VelocityZ::process() {
117 StuntDouble* sd;
118 int ii;
119
120 bool usePeriodicBoundaryConditions_ =
121 info_->getSimParams()->getUsePeriodicBoundaryConditions();
122
123 DumpReader reader(info_, dumpFilename_);
124 int nFrames = reader.getNFrames();
125 nProcessed_ = nFrames / step_;
126
127 for (int istep = 0; istep < nFrames; istep += step_) {
128 reader.readFrame(istep);
129 currentSnapshot_ = info_->getSnapshotManager()->getCurrentSnapshot();
130
131 for (unsigned int i = 0; i < nBins2_; i++) {
132 for (unsigned int j = 0; j < nBins_; j++) {
133 sliceSDLists_[i][j].clear();
134 }
135 }
136
137 Mat3x3d hmat = currentSnapshot_->getHmat();
138
139 zBox_.push_back(hmat(axis2_, axis2_));
140
141 RealType halfBox1_ = hmat(axis1_, axis1_) / 2.0;
142 RealType halfBox2_ = hmat(axis2_, axis2_) / 2.0;
143
144 if (evaluator_.isDynamic()) {
145 seleMan_.setSelectionSet(evaluator_.evaluate());
146 }
147
148 // wrap the stuntdoubles into a cell
149 for (sd = seleMan_.beginSelected(ii); sd != NULL;
150 sd = seleMan_.nextSelected(ii)) {
151 Vector3d pos = sd->getPos();
152 if (usePeriodicBoundaryConditions_) currentSnapshot_->wrapVector(pos);
153 sd->setPos(pos);
154 }
155
156 // determine which atom belongs to which slice
157 for (sd = seleMan_.beginSelected(ii); sd != NULL;
158 sd = seleMan_.nextSelected(ii)) {
159 Vector3d pos = sd->getPos();
160 // shift molecules by half a box to have bins start at 0
161 int binNo1 =
162 int(nBins_ * (halfBox1_ + pos[axis1_]) / hmat(axis1_, axis1_));
163 int binNo2 =
164 int(nBins2_ * (halfBox2_ + pos[axis2_]) / hmat(axis2_, axis2_));
165 sliceSDLists_[binNo2][binNo1].push_back(sd);
166 }
167
168 // loop over the slices to calculate the velocities
169 for (unsigned int i = 0; i < nBins2_; i++) {
170 for (unsigned int j = 0; j < nBins_; j++) {
171 RealType totalVelocity = 0;
172 for (unsigned int k = 0; k < sliceSDLists_[i][j].size(); ++k) {
173 totalVelocity += sliceSDLists_[i][j][k]->getVel()[axis3_];
174 }
175
176 if (sliceSDLists_[i][j].size() > 0)
177 velocity_[i][j] += totalVelocity / sliceSDLists_[i][j].size();
178 }
179 }
180 }
181
182 writeVelocity();
183 }
184
185 void VelocityZ::writeVelocity() {
186 // compute average box length:
187
188 RealType zSum = 0.0;
189 for (std::vector<RealType>::iterator j = zBox_.begin(); j != zBox_.end();
190 ++j) {
191 zSum += *j;
192 }
193 RealType zAve = zSum / zBox_.size();
194
195 std::ofstream rdfStream(outputFilename_.c_str());
196 if (rdfStream.is_open()) {
197 rdfStream << "#VelocityZ\n";
198 rdfStream << "#nFrames:\t" << nProcessed_ << "\n";
199 rdfStream << "#selection: (" << selectionScript_ << ")\n";
200 rdfStream << "#velocity (" << axisLabel1_ << "," << axisLabel2_ << ")\n";
201
202 for (unsigned int i = 0; i < velocity_.size(); ++i) {
203 RealType z = zAve * (i + 0.5) / velocity_.size();
204 rdfStream << z << "\t";
205 for (unsigned int j = 0; j < velocity_[i].size(); ++j) {
206 rdfStream << velocity_[i][j] / nProcessed_;
207 rdfStream << "\t";
208 }
209 rdfStream << "\n";
210 }
211
212 } else {
213 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
214 "VelocityZ: unable to open %s\n", outputFilename_.c_str());
215 painCave.isFatal = 1;
216 simError();
217 }
218
219 rdfStream.close();
220 }
221} // 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)