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