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