OpenMD 3.1
Molecular Dynamics in the Open
Loading...
Searching...
No Matches
TranslationalOrderParamZ.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/TranslationalOrderParamZ.hpp"
46
47#include <algorithm>
48#include <fstream>
49#include <vector>
50
51#include "io/DumpReader.hpp"
53#include "utils/simError.h"
54
55using namespace std;
56namespace OpenMD {
57 TranslationalOrderParamZ::TranslationalOrderParamZ(
58 SimInfo* info, const std::string& filename, const std::string& sele1,
59 const std::string& sele2, double rCut, int nrbins, int nzbins,
60 RealType len, RealType zlen, int axis) :
61 RadialDistrFunc(info, filename, sele1, sele2, nrbins),
62 rCut_(rCut), nZBins_(nzbins), len_(len), zLen_(zlen), axis_(axis) {
63 setOutputName(getPrefix(filename) + ".Tz");
64
65 deltaR_ = len_ / (double)nBins_;
66 deltaZ_ = zLen_ / (double)nZBins_;
67
68 histogram_.resize(nBins_);
69 avgGofr_.resize(nBins_);
70 for (unsigned int i = 0; i < nBins_; ++i) {
71 histogram_[i].resize(nZBins_);
72 avgGofr_[i].resize(nZBins_);
73 }
74 Tz_.resize(nZBins_);
75
76 // Set up cutoff radius:
77 rCut_ = rCut;
78
79 // Compute complementary axes to the privileged axis
80 xaxis_ = (axis_ + 1) % 3;
81 yaxis_ = (axis_ + 2) % 3;
82
83 // Set the axis label for the privileged axis
84 switch (axis_) {
85 case 0:
86 axisLabel_ = "x";
87 break;
88 case 1:
89 axisLabel_ = "y";
90 break;
91 case 2:
92 default:
93 axisLabel_ = "z";
94 break;
95 }
96 }
97
98 void TranslationalOrderParamZ::preProcess() {
99 for (unsigned int i = 0; i < avgGofr_.size(); ++i) {
100 std::fill(avgGofr_[i].begin(), avgGofr_[i].end(), 0);
101 }
102 std::fill(Tz_.begin(), Tz_.end(), 0);
103 }
104
105 void TranslationalOrderParamZ::initializeHistogram() {
106 for (unsigned int i = 0; i < histogram_.size(); ++i) {
107 std::fill(histogram_[i].begin(), histogram_[i].end(), 0);
108 }
109 Mat3x3d hmat = currentSnapshot_->getHmat();
110 zBox_.push_back(hmat(axis_, axis_));
111 }
112
113 void TranslationalOrderParamZ::collectHistogram(StuntDouble* sd1,
114 StuntDouble* sd2) {
115 if (sd1 == sd2) { return; }
116
117 bool usePeriodicBoundaryConditions_ =
118 info_->getSimParams()->getUsePeriodicBoundaryConditions();
119 RealType boxZ = zBox_.back();
120
121 Vector3d pos1 = sd1->getPos();
122 Vector3d pos2 = sd2->getPos();
123 Vector3d r12 = pos2 - pos1;
124 if (usePeriodicBoundaryConditions_) {
125 currentSnapshot_->wrapVector(r12);
126 currentSnapshot_->wrapVector(pos1);
127 currentSnapshot_->wrapVector(pos2);
128 }
129
130 RealType distance = r12.length();
131
132 if (distance < len_) {
133 int whichBin = int(distance / deltaR_);
134 int zBin1 = int(nZBins_ * (0.5 * boxZ + pos1[axis_]) / boxZ);
135 int zBin2 = int(nZBins_ * (0.5 * boxZ + pos2[axis_]) / boxZ);
136
137 histogram_[whichBin][zBin1] += 1;
138 histogram_[whichBin][zBin2] += 1;
139 }
140 }
141
142 void TranslationalOrderParamZ::processHistogram() {
143 int nPairs = getNPairs();
144 RealType volume =
145 info_->getSnapshotManager()->getCurrentSnapshot()->getVolume();
146 RealType pairDensity = 2 * nPairs / volume;
147
148 for (unsigned int i = 0; i < histogram_.size(); ++i) {
149 RealType rLower = i * deltaR_;
150 RealType rUpper = rLower + deltaR_;
151 RealType volSlice =
152 4.0 * Constants::PI * (pow(rUpper, 3) - pow(rLower, 3)) / 3.0;
153 RealType nIdeal = volSlice * pairDensity / nZBins_;
154
155 for (unsigned int j = 0; j < histogram_[i].size(); ++j) {
156 avgGofr_[i][j] += histogram_[i][j] / nIdeal;
157 }
158 }
159 }
160
161 void TranslationalOrderParamZ::postProcess() {
162 for (unsigned int i = 0; i < avgGofr_.size(); ++i) {
163 for (unsigned int j = 0; j < avgGofr_[i].size(); ++j) {
164 avgGofr_[i][j] /= nProcessed_;
165 }
166 }
167
168 for (unsigned int i = 0; i < avgGofr_.size(); ++i) {
169 RealType rLower = i * deltaR_;
170 RealType rUpper = rLower + deltaR_;
171
172 for (unsigned int j = 0; j < avgGofr_[i].size(); ++j) {
173 if (rUpper < rCut_) Tz_[j] += std::fabs(avgGofr_[i][j] - 1.0) * deltaR_;
174 }
175 }
176
177 // normalize by cutoff radius
178 for (unsigned int j = 0; j < Tz_.size(); ++j) {
179 Tz_[j] /= rCut_;
180 }
181 }
182
183 void TranslationalOrderParamZ::writeRdf() {
184 // compute average box length:
185
186 RealType zSum = 0.0;
187 for (std::vector<RealType>::iterator j = zBox_.begin(); j != zBox_.end();
188 ++j) {
189 zSum += *j;
190 }
191 RealType zAve = zSum / zBox_.size();
192
193 std::ofstream tZstream(outputFilename_.c_str());
194 if (tZstream.is_open()) {
195 tZstream << "#Translational Order Parameters (" << axisLabel_ << ")\n";
196
197 tZstream << "#nFrames:\t" << zBox_.size() << "\n";
198 tZstream << "#selection 1: (" << selectionScript1_ << ")\n";
199 tZstream << "#selection 2: (" << selectionScript2_ << ")\n";
200 tZstream << "#" << axisLabel_ << "\tT\n";
201
202 // for (unsigned int i = 0; i < avgGofr_.size(); ++i) {
203 // RealType r = i * deltaR_;
204 // tZstream << r << "\t" ;
205 // for (unsigned int j = 0; j < avgGofr_[i].size(); ++j) {
206 // tZstream << "\t" << avgGofr_[i][j];
207 // }
208 // tZstream << "\n";
209 // }
210
211 for (unsigned int i = 0; i < Tz_.size(); ++i) {
212 RealType z = zAve * (i + 0.5) / Tz_.size();
213 tZstream << z << "\t" << Tz_[i] << "\n";
214 }
215
216 } else {
217 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
218 "TranslationalOrderParamZ: unable to open %s\n",
219 outputFilename_.c_str());
220 painCave.isFatal = 1;
221 simError();
222 }
223 tZstream.close();
224 }
225} // namespace OpenMD
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.