OpenMD 3.2
Molecular Dynamics in the Open
Loading...
Searching...
No Matches
NumberZ.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 number density distribution along preferred axis for the
50 * selected molecules
51 */
52
53#include "applications/staticProps/NumberZ.hpp"
54
55#include <algorithm>
56#include <fstream>
57
58#include "brains/Thermo.hpp"
59#include "io/DumpReader.hpp"
61#include "utils/simError.h"
62
63namespace OpenMD {
64
65 NumberZ::NumberZ(SimInfo* info, const std::string& filename,
66 const std::string& sele, int nzbins, int axis) :
67 StaticAnalyser(info, filename, nzbins),
68 selectionScript_(sele), evaluator_(info), seleMan_(info), thermo_(info),
69 axis_(axis) {
70 evaluator_.loadScriptString(sele);
71 if (!evaluator_.isDynamic()) {
72 seleMan_.setSelectionSet(evaluator_.evaluate());
73 }
74
75 numberZ_.resize(nBins_);
76
77 switch (axis_) {
78 case 0:
79 axisLabel_ = "x";
80 break;
81 case 1:
82 axisLabel_ = "y";
83 break;
84 case 2:
85 default:
86 axisLabel_ = "z";
87 break;
88 }
89
90 setOutputName(getPrefix(filename) + ".NumberZ");
91 }
92
93 void NumberZ::process() {
94 Molecule* mol;
95 int ii;
96
97 bool usePeriodicBoundaryConditions_ =
98 info_->getSimParams()->getUsePeriodicBoundaryConditions();
99
100 DumpReader reader(info_, dumpFilename_);
101 int nFrames = reader.getNFrames();
102 nProcessed_ = nFrames / step_;
103
104 for (int istep = 0; istep < nFrames; istep += step_) {
105 reader.readFrame(istep);
106 currentSnapshot_ = info_->getSnapshotManager()->getCurrentSnapshot();
107
108 Mat3x3d hmat = currentSnapshot_->getHmat();
109 zBox_.push_back(hmat(axis_, axis_));
110
111 RealType halfBoxZ_ = hmat(axis_, axis_) / 2.0;
112 RealType area = 0.0;
113 switch (axis_) {
114 case 0:
115 area = currentSnapshot_->getYZarea();
116 break;
117 case 1:
118 area = currentSnapshot_->getXZarea();
119 break;
120 case 2:
121 default:
122 area = currentSnapshot_->getXYarea();
123 break;
124 }
125
126 areas_.push_back(area);
127
128 if (evaluator_.isDynamic()) {
129 seleMan_.setSelectionSet(evaluator_.evaluate());
130 }
131
132 for (mol = seleMan_.beginSelectedMolecule(ii); mol != NULL;
133 mol = seleMan_.nextSelectedMolecule(ii)) {
134 Vector3d pos = mol->getCom();
135 if (usePeriodicBoundaryConditions_) currentSnapshot_->wrapVector(pos);
136 // shift molecules by half a box to have bins start at 0
137 int binNo = int(nBins_ * (halfBoxZ_ + pos[axis_]) / hmat(axis_, axis_));
138 numberZ_[binNo]++;
139 }
140 }
141 writeNumberZ();
142 }
143
144 void NumberZ::writeNumberZ() {
145 // compute average box length:
146 std::vector<RealType>::iterator j;
147 RealType zSum = 0.0;
148 for (j = zBox_.begin(); j != zBox_.end(); ++j) {
149 zSum += *j;
150 }
151 RealType zAve = zSum / zBox_.size();
152
153 RealType areaSum = 0.0;
154 for (j = areas_.begin(); j != areas_.end(); ++j) {
155 areaSum += *j;
156 }
157 RealType areaAve = areaSum / areas_.size();
158
159 std::ofstream rdfStream(outputFilename_.c_str());
160 if (rdfStream.is_open()) {
161 rdfStream << "#NumberZ "
162 << "\n";
163 rdfStream << "#selection: (" << selectionScript_ << ")\n";
164 rdfStream << "#" << axisLabel_ << "\tnumber\n";
165 RealType binNumber;
166 for (unsigned int i = 0; i < numberZ_.size(); ++i) {
167 RealType z = zAve * (i + 0.5) / nBins_;
168
169 RealType volSlice = areaAve * zAve / nBins_;
170
171 binNumber = numberZ_[i] / (volSlice * nProcessed_);
172
173 rdfStream << z << "\t" << binNumber << "\n";
174 }
175
176 } else {
177 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
178 "NumberZ: unable to open %s\n", outputFilename_.c_str());
179 painCave.isFatal = 1;
180 simError();
181 }
182
183 rdfStream.close();
184 }
185} // 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)