OpenMD 3.1
Molecular Dynamics in the Open
Loading...
Searching...
No Matches
MomentumHistogram.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 appropriate papers when you publish your
33 * work. Good starting points are:
34 *
35 * [1] Meineke, et al., J. Comp. Chem. 26, 252-271 (2005).
36 * [2] Fennell & Gezelter, J. Chem. Phys. 124, 234104 (2006).
37 * [3] Sun, Lin & Gezelter, J. Chem. Phys. 128, 234107 (2008).
38 * [4] Vardeman, Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011).
39 * [5] Kuang & Gezelter, Mol. Phys., 110, 691-701 (2012).
40 * [6] Lamichhane, Gezelter & Newman, J. Chem. Phys. 141, 134109 (2014).
41 * [7] Lamichhane, Newman & Gezelter, J. Chem. Phys. 141, 134110 (2014).
42 * [8] Bhattarai, Newman & Gezelter, Phys. Rev. B 99, 094106 (2019).
43 */
44
45/*
46 * Calculates the momentum profile for selected atom.
47 * Created by Hemanta Bhattarai on 04/30/19.
48 * @author Hemanta Bhattarai
49 */
50
51#include "applications/staticProps/MomentumHistogram.hpp"
52
53#include <algorithm>
54#include <fstream>
55#include <numeric>
56
57#include "io/DumpReader.hpp"
59#include "utils/simError.h"
60
61namespace OpenMD {
62
63 MomentumHistogram::MomentumHistogram(SimInfo* info,
64 const std::string& filename,
65 const std::string& sele, int nbins,
66 int momentum_type,
67 int momentum_component) :
68 StaticAnalyser(info, filename, nbins),
69 selectionScript_(sele), evaluator_(info), seleMan_(info), nBins_(nbins),
70 mom_type_(momentum_type), mom_comp_(momentum_component) {
71 evaluator_.loadScriptString(sele);
72 if (!evaluator_.isDynamic()) {
73 seleMan_.setSelectionSet(evaluator_.evaluate());
74 }
75
76 switch (mom_type_) {
77 case 1:
78 momentumLabel_ = "Angular Momentum: J";
79 break;
80 case 0:
81 default:
82 momentumLabel_ = "Linear Momentum: P";
83 break;
84 }
85
86 switch (mom_comp_) {
87 case 0:
88 componentLabel_ = "x";
89 break;
90 case 1:
91 componentLabel_ = "y";
92 break;
93 case 2:
94 default:
95 componentLabel_ = "z";
96 break;
97 }
98
99 setOutputName(getPrefix(filename) + ".MomentumHistogram");
100 }
101
102 void MomentumHistogram::process() {
103 StuntDouble* sd;
104 int ii;
105
106 if (evaluator_.isDynamic()) {
107 seleMan_.setSelectionSet(evaluator_.evaluate());
108 }
109
110 DumpReader reader(info_, dumpFilename_);
111 int nFrames = reader.getNFrames();
112 nProcessed_ = nFrames / step_;
113 vector<RealType> momentum;
114
115 nProcessed_ = nFrames / step_;
116
117 for (int istep = 0; istep < nFrames; istep += step_) {
118 reader.readFrame(istep);
119 currentSnapshot_ = info_->getSnapshotManager()->getCurrentSnapshot();
120
121 for (sd = seleMan_.beginSelected(ii); sd != NULL;
122 sd = seleMan_.nextSelected(ii)) {
123 switch (mom_type_) {
124 case 0: {
125 Vector3d linMom = sd->getVel() * sd->getMass();
126 momentum.push_back(linMom[mom_comp_]);
127 break;
128 }
129 case 1:
130 default: {
131 if (sd->isDirectional()) {
132 Vector3d angMom = sd->getJ();
133 momentum.push_back(angMom[mom_comp_]);
134 }
135 } break;
136 }
137 }
138 }
139
140 if (momentum.empty()) {
141 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
142 "Momentum for selected atom not found.\n");
143 painCave.isFatal = 1;
144 simError();
145 }
146
147 std::sort(momentum.begin(), momentum.end());
148
149 RealType min = momentum.front();
150 RealType max = momentum.back();
151
152 RealType delta_momentum = (max - min) / (nBins_);
153
154 if (delta_momentum == 0) {
155 bincenter_.push_back(min);
156 histList_.push_back(momentum.size());
157 } else {
158 // fill the center for histogram
159 for (int j = 0; j < nBins_ + 3; ++j) {
160 bincenter_.push_back(min + (j - 1) * delta_momentum);
161 histList_.push_back(0);
162 }
163
164 // filling up the histogram whith the densities
165 int bin_center_pos = 0;
166 vector<RealType>::iterator index;
167 RealType momentum_length = static_cast<RealType>(momentum.size());
168
169 bool hist_update;
170 for (index = momentum.begin(); index < momentum.end(); index++) {
171 hist_update = true;
172 while (hist_update) {
173 if (*index >= bincenter_[bin_center_pos] &&
174 *index < bincenter_[bin_center_pos + 1]) {
175 histList_[bin_center_pos] +=
176 1.0 / (momentum_length * delta_momentum);
177 hist_update = false;
178 } else {
179 bin_center_pos++;
180 hist_update = true;
181 }
182 }
183 }
184 }
185 write();
186 }
187
188 void MomentumHistogram::write() {
189 std::ofstream rdfStream(outputFilename_.c_str());
190 if (rdfStream.is_open()) {
191 rdfStream << "#" << momentumLabel_ << componentLabel_
192 << " distribtution \n";
193 rdfStream << "# nFrames:\t" << nProcessed_ << "\n";
194 rdfStream << "# selection: (" << selectionScript_ << ")\n";
195 rdfStream << "# "
196 << "Bin_center"
197 << "\tcount\n";
198 for (unsigned int i = 0; i < histList_.size(); ++i) {
199 rdfStream << bincenter_[i] << "\t" << histList_[i] << "\n";
200 }
201
202 } else {
203 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
204 "MomentumHistogram: unable to open %s\n",
205 outputFilename_.c_str());
206 painCave.isFatal = 1;
207 simError();
208 }
209 rdfStream.close();
210 }
211} // namespace OpenMD
This basic Periodic Table class was originally taken from the data.cpp file in OpenBabel.
std::string getPrefix(const std::string &str)