OpenMD 3.2
Molecular Dynamics in the Open
Loading...
Searching...
No Matches
DensityPlot.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#include "applications/staticProps/DensityPlot.hpp"
49
50#include <algorithm>
51#include <functional>
52#include <memory>
53
54#include "io/DumpReader.hpp"
56#include "types/LennardJonesAdapter.hpp"
57#include "utils/Constants.hpp"
58#include "utils/simError.h"
59
60using namespace std;
61
62namespace OpenMD {
63
64 DensityPlot::DensityPlot(SimInfo* info, const std::string& filename,
65 const std::string& sele, const std::string& cmSele,
66 RealType len, int nrbins) :
67 StaticAnalyser(info, filename, nrbins),
68 len_(len), halfLen_(len / 2), nRBins_(nrbins), selectionScript_(sele),
69 seleMan_(info), evaluator_(info), cmSelectionScript_(cmSele),
70 cmSeleMan_(info), cmEvaluator_(info) {
71 setOutputName(getPrefix(filename) + ".density");
72
73 deltaR_ = len_ / nRBins_;
74 histogram_.resize(nRBins_);
75 density_.resize(nRBins_);
76
77 std::fill(histogram_.begin(), histogram_.end(), 0);
78
79 evaluator_.loadScriptString(sele);
80
81 if (!evaluator_.isDynamic()) {
82 seleMan_.setSelectionSet(evaluator_.evaluate());
83 }
84
85 cmEvaluator_.loadScriptString(cmSele);
86 if (!cmEvaluator_.isDynamic()) {
87 cmSeleMan_.setSelectionSet(cmEvaluator_.evaluate());
88 }
89 }
90
91 void DensityPlot::process() {
92 bool usePeriodicBoundaryConditions_ =
93 info_->getSimParams()->getUsePeriodicBoundaryConditions();
94
95 DumpReader reader(info_, dumpFilename_);
96 int nFrames = reader.getNFrames();
97 for (int i = 0; i < nFrames; i += step_) {
98 reader.readFrame(i);
99 currentSnapshot_ = info_->getSnapshotManager()->getCurrentSnapshot();
100
101 if (evaluator_.isDynamic()) {
102 seleMan_.setSelectionSet(evaluator_.evaluate());
103 }
104
105 if (cmEvaluator_.isDynamic()) {
106 cmSeleMan_.setSelectionSet(cmEvaluator_.evaluate());
107 }
108
109 Vector3d origin = calcNewOrigin();
110
111 Mat3x3d hmat = currentSnapshot_->getHmat();
112 RealType slabVolume = deltaR_ * hmat(0, 0) * hmat(1, 1);
113 int k;
114 for (StuntDouble* sd = seleMan_.beginSelected(k); sd != NULL;
115 sd = seleMan_.nextSelected(k)) {
116 if (!sd->isAtom()) {
117 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
118 "Can not calculate electron density if it is not atom\n");
119 painCave.severity = OPENMD_ERROR;
120 painCave.isFatal = 1;
121 simError();
122 }
123
124 Atom* atom = static_cast<Atom*>(sd);
125 std::shared_ptr<GenericData> data =
126 atom->getAtomType()->getPropertyByName("nelectron");
127 if (data == nullptr) {
128 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
129 "Can not find Parameters for nelectron\n");
130 painCave.severity = OPENMD_ERROR;
131 painCave.isFatal = 1;
132 simError();
133 }
134
135 std::shared_ptr<DoubleGenericData> doubleData =
136 std::dynamic_pointer_cast<DoubleGenericData>(data);
137 if (doubleData == nullptr) {
138 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
139 "Can not cast GenericData to DoubleGenericData\n");
140 painCave.severity = OPENMD_ERROR;
141 painCave.isFatal = 1;
142 simError();
143 }
144
145 RealType nelectron = doubleData->getData();
146 LennardJonesAdapter lja = LennardJonesAdapter(atom->getAtomType());
147 RealType sigma = lja.getSigma() * 0.5;
148 RealType sigma2 = sigma * sigma;
149
150 Vector3d pos = sd->getPos() - origin;
151 for (int j = 0; j < nRBins_; ++j) {
152 Vector3d tmp(pos);
153 RealType zdist = j * deltaR_ - halfLen_;
154 tmp[2] += zdist;
155 if (usePeriodicBoundaryConditions_) currentSnapshot_->wrapVector(tmp);
156
157 RealType wrappedZdist = tmp.z() + halfLen_;
158 if (wrappedZdist < 0.0 || wrappedZdist > len_) { continue; }
159
160 int which = int(wrappedZdist / deltaR_);
161 density_[which] +=
162 nelectron * exp(-zdist * zdist / (sigma2 * 2.0)) /
163 (slabVolume * sqrt(2 * Constants::PI * sigma * sigma));
164 }
165 }
166 }
167
168 int nProcessed = nFrames / step_;
169 std::transform(
170 density_.begin(), density_.end(), density_.begin(),
171 std::bind(std::divides<RealType>(), std::placeholders::_1, nProcessed));
172 writeDensity();
173 }
174
175 Vector3d DensityPlot::calcNewOrigin() {
176 int i;
177 Vector3d newOrigin(0.0);
178 RealType totalMass = 0.0;
179 for (StuntDouble* sd = seleMan_.beginSelected(i); sd != NULL;
180 sd = seleMan_.nextSelected(i)) {
181 RealType mass = sd->getMass();
182 totalMass += mass;
183 newOrigin += sd->getPos() * mass;
184 }
185 newOrigin /= totalMass;
186 return newOrigin;
187 }
188
189 void DensityPlot::writeDensity() {
190 std::ofstream ofs(outputFilename_.c_str(), std::ios::binary);
191 if (ofs.is_open()) {
192 ofs << "#g(x, y, z)\n";
193 ofs << "#selection: (" << selectionScript_ << ")\n";
194 ofs << "#cmSelection:(" << cmSelectionScript_ << ")\n";
195 ofs << "#nRBins = " << nRBins_ << "\t maxLen = " << len_
196 << "\tdeltaR = " << deltaR_ << "\n";
197 for (unsigned int i = 0; i < histogram_.size(); ++i) {
198 ofs << i * deltaR_ - halfLen_ << "\t" << density_[i] << std::endl;
199 }
200 } else {
201 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
202 "DensityPlot: unable to open %s\n", outputFilename_.c_str());
203 painCave.isFatal = 1;
204 simError();
205 }
206
207 ofs.close();
208 }
209
210} // 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)