OpenMD 3.2
Molecular Dynamics in the Open
Loading...
Searching...
No Matches
CurrentDensity.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 current density for the selected atom
50 * Created by Cody R. Drisko on 06/14/19.
51 */
52
53#include "applications/staticProps/CurrentDensity.hpp"
54
55#include <algorithm>
56#include <fstream>
57#include <string>
58#include <vector>
59
60#include "brains/Thermo.hpp"
61#include "io/DumpReader.hpp"
63#include "types/FixedChargeAdapter.hpp"
64#include "types/FluctuatingChargeAdapter.hpp"
65#include "utils/StringUtils.hpp"
66#include "utils/simError.h"
67
68namespace OpenMD {
69
70 CurrentDensity::CurrentDensity(SimInfo* info, const std::string& filename,
71 const std::string& sele, int nbins, int axis) :
72 StaticAnalyser(info, filename, nbins),
73 selectionScript_(sele), evaluator_(info), seleMan_(info), thermo_(info),
74 axis_(axis) {
75 evaluator_.loadScriptString(sele);
76 if (!evaluator_.isDynamic()) {
77 seleMan_.setSelectionSet(evaluator_.evaluate());
78 }
79
80 // fixed number of bins
81 sliceSDLists_.resize(nBins_);
82 currentDensity_.resize(nBins_);
83
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 setOutputName(getPrefix(filename) + ".Jc");
98 }
99
100 void CurrentDensity::process() {
101 StuntDouble* sd;
102 int ii;
103
104 bool usePeriodicBoundaryConditions_ =
105 info_->getSimParams()->getUsePeriodicBoundaryConditions();
106
107 DumpReader reader(info_, dumpFilename_);
108 int nFrames = reader.getNFrames();
109 nProcessed_ = nFrames / step_;
110 overallCurrentDensity_ = 0;
111
112 for (int istep = 0; istep < nFrames; istep += step_) {
113 reader.readFrame(istep);
114 currentSnapshot_ = info_->getSnapshotManager()->getCurrentSnapshot();
115 Vector3d COMvel = thermo_.getComVel();
116
117 for (unsigned int i = 0; i < nBins_; i++) {
118 sliceSDLists_[i].clear();
119 }
120
121 RealType boxVolume = currentSnapshot_->getVolume();
122 RealType sliceVolume = boxVolume / nBins_;
123 Mat3x3d hmat = currentSnapshot_->getHmat();
124 zBox_.push_back(hmat(axis_, axis_));
125
126 if (evaluator_.isDynamic()) {
127 seleMan_.setSelectionSet(evaluator_.evaluate());
128 }
129
130 // determine which atom belongs to which slice
131 for (sd = seleMan_.beginSelected(ii); sd != NULL;
132 sd = seleMan_.nextSelected(ii)) {
133 int binNo;
134 Vector3d pos = sd->getPos();
135
136 if (usePeriodicBoundaryConditions_) {
137 currentSnapshot_->wrapVector(pos);
138 binNo =
139 int(nBins_ * (pos[axis_] / hmat(axis_, axis_) + 0.5)) % nBins_;
140 sliceSDLists_[binNo].push_back(sd);
141 }
142 }
143
144 // loop over the slices to calculate the densities
145 for (unsigned int i = 0; i < nBins_; i++) {
146 RealType binJc = 0;
147
148 for (unsigned int j = 0; j < sliceSDLists_[i].size(); ++j) {
149 RealType q = 0.0;
150 Atom* atom = static_cast<Atom*>(sliceSDLists_[i][j]);
151
152 AtomType* atomType = atom->getAtomType();
153
154 if (sliceSDLists_[i][j]->isAtom()) {
155 FixedChargeAdapter fca = FixedChargeAdapter(atomType);
156 if (fca.isFixedCharge()) q = fca.getCharge();
157
158 FluctuatingChargeAdapter fqa = FluctuatingChargeAdapter(atomType);
159 if (fqa.isFluctuatingCharge()) q += atom->getFlucQPos();
160
161 Vector3d vel = sliceSDLists_[i][j]->getVel();
162 binJc += q * (vel[axis_] - COMvel[axis_]);
163 }
164 }
165
166 // Units of (e / Ang^2 / fs)
167 currentDensity_[i] += binJc / sliceVolume;
168 overallCurrentDensity_ += binJc / boxVolume;
169 }
170 }
171
172 writeCurrentDensity();
173 }
174
175 void CurrentDensity::writeCurrentDensity() {
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 << "#Current Density = "
187 << overallCurrentDensity_ / nProcessed_
188 << " e / Ang^2 / fs.\n";
189 rdfStream << "#J_c(" << axisLabel_ << ")\n";
190 rdfStream << "#nFrames:\t" << nProcessed_ << "\n";
191 rdfStream << "#selection: (" << selectionScript_ << ")\n";
192 rdfStream << "#" << axisLabel_ << "\tcurrent density\n";
193
194 for (unsigned int i = 0; i < currentDensity_.size(); ++i) {
195 RealType z = zAve * (i + 0.5) / currentDensity_.size();
196 rdfStream << z << "\t" << currentDensity_[i] / nProcessed_ << "\n";
197 }
198
199 } else {
200 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
201 "CurrentDensity: unable to open %s\n", outputFilename_.c_str());
202 painCave.isFatal = 1;
203 simError();
204 }
205
206 rdfStream.close();
207 }
208} // 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)