OpenMD 3.2
Molecular Dynamics in the Open
Loading...
Searching...
No Matches
PositionZ.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/*
49 * Computes the position distribution along preferred axis for the selected atom
50 * Created by Hemanta Bhattarai on 02/20/20.
51 */
52
53#include "applications/staticProps/PositionZ.hpp"
54
55#include <algorithm>
56#include <fstream>
57
58#include "brains/Thermo.hpp"
59#include "io/DumpReader.hpp"
61#include "types/FixedChargeAdapter.hpp"
62#include "types/FluctuatingChargeAdapter.hpp"
63#include "utils/simError.h"
64
65namespace OpenMD {
66
67 PositionZ::PositionZ(SimInfo* info, const std::string& filename,
68 const std::string& sele, int nzbins, int axis) :
69 StaticAnalyser(info, filename, nzbins),
70 selectionScript_(sele), evaluator_(info), seleMan_(info), thermo_(info),
71 axis_(axis) {
72 evaluator_.loadScriptString(sele);
73 if (!evaluator_.isDynamic()) {
74 seleMan_.setSelectionSet(evaluator_.evaluate());
75 }
76
77 // fixed number of bins
78
79 sliceSDCount_.resize(nBins_);
80 flucSliceSDCount_.resize(nBins_);
81 std::fill(sliceSDCount_.begin(), sliceSDCount_.end(), 0);
82 std::fill(flucSliceSDCount_.begin(), flucSliceSDCount_.end(), 0);
83
84 positionZ_.resize(nBins_);
85
86 switch (axis_) {
87 case 0:
88 axisLabel_ = "x";
89 break;
90 case 1:
91 axisLabel_ = "y";
92 break;
93 case 2:
94 default:
95 axisLabel_ = "z";
96 break;
97 }
98
99 setOutputName(getPrefix(filename) + ".CountZ");
100 }
101
102 void PositionZ::process() {
103 StuntDouble* sd;
104 int ii;
105
106 bool usePeriodicBoundaryConditions_ =
107 info_->getSimParams()->getUsePeriodicBoundaryConditions();
108
109 DumpReader reader(info_, dumpFilename_);
110 int nFrames = reader.getNFrames();
111 nProcessed_ = nFrames / step_;
112
113 for (int istep = 0; istep < nFrames; istep += step_) {
114 reader.readFrame(istep);
115 currentSnapshot_ = info_->getSnapshotManager()->getCurrentSnapshot();
116
117 Mat3x3d hmat = currentSnapshot_->getHmat();
118 zBox_.push_back(hmat(axis_, axis_));
119
120 RealType halfBoxZ_ = hmat(axis_, axis_) / 2.0;
121
122 if (evaluator_.isDynamic()) {
123 seleMan_.setSelectionSet(evaluator_.evaluate());
124 }
125
126 // wrap the stuntdoubles into a cell
127 for (sd = seleMan_.beginSelected(ii); sd != NULL;
128 sd = seleMan_.nextSelected(ii)) {
129 Vector3d pos = sd->getPos();
130 if (usePeriodicBoundaryConditions_) currentSnapshot_->wrapVector(pos);
131 sd->setPos(pos);
132 }
133
134 // determine which atom belongs to which slice
135 for (sd = seleMan_.beginSelected(ii); sd != NULL;
136 sd = seleMan_.nextSelected(ii)) {
137 Vector3d pos = sd->getPos();
138 // shift molecules by half a box to have bins start at 0
139 int binNo = int(nBins_ * (halfBoxZ_ + pos[axis_]) / hmat(axis_, axis_));
140 sliceSDCount_[binNo]++;
141 }
142
143 // loop over the slices to calculate the charge
144 }
145
146 for (int istep = 0; istep < nFrames; istep += step_) {
147 std::map<int, RealType> countInBin;
148
149 reader.readFrame(istep);
150 currentSnapshot_ = info_->getSnapshotManager()->getCurrentSnapshot();
151 Mat3x3d hmat = currentSnapshot_->getHmat();
152 RealType halfBoxZ_ = hmat(axis_, axis_) / 2.0;
153
154 // determine which atom belongs to which slice
155 for (sd = seleMan_.beginSelected(ii); sd != NULL;
156 sd = seleMan_.nextSelected(ii)) {
157 Vector3d pos = sd->getPos();
158 // shift molecules by half a box to have bins start at 0
159 int binNo = int(nBins_ * (halfBoxZ_ + pos[axis_]) / hmat(axis_, axis_));
160 countInBin[binNo]++;
161 }
162
163 // loop over the slices to calculate the charge
164
165 for (unsigned int index = 0; index < flucSliceSDCount_.size(); ++index) {
166 RealType flucCount =
167 (countInBin[index] - (sliceSDCount_[index] / nProcessed_));
168 flucSliceSDCount_[index] += flucCount * flucCount;
169 }
170 }
171
172 writePositionZ();
173 }
174
175 void PositionZ::writePositionZ() {
176 // compute average box length:
177 std::vector<RealType>::iterator j;
178 RealType zSum = 0.0;
179 for (j = zBox_.begin(); j != zBox_.end(); ++j) {
180 zSum += *j;
181 }
182 RealType zAve = zSum / zBox_.size();
183
184 std::ofstream rdfStream(outputFilename_.c_str());
185 if (rdfStream.is_open()) {
186 rdfStream << "#position count "
187 << "\n";
188 rdfStream << "#selection: (" << selectionScript_ << ")\n";
189 rdfStream << "#" << axisLabel_
190 << "\tAverage Number\tFluctations_in_count\n";
191 for (unsigned int i = 0; i < positionZ_.size(); ++i) {
192 RealType z = zAve * (i + 0.5) / positionZ_.size();
193 rdfStream << z << "\t" << sliceSDCount_[i] / nProcessed_ << "\t"
194 << sqrt(flucSliceSDCount_[i]) / nProcessed_ << "\n";
195 }
196
197 } else {
198 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
199 "ChargeZ: unable to open %s\n", outputFilename_.c_str());
200 painCave.isFatal = 1;
201 simError();
202 }
203
204 rdfStream.close();
205 }
206} // 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)