54#include "applications/staticProps/MomentumHistogram.hpp"
62#include "utils/simError.h"
66 MomentumHistogram::MomentumHistogram(
SimInfo* info,
67 const std::string& filename,
68 const std::string& sele,
int nbins,
70 int momentum_component) :
72 selectionScript_(sele), evaluator_(info), seleMan_(info), nBins_(nbins),
73 mom_type_(momentum_type), mom_comp_(momentum_component) {
74 evaluator_.loadScriptString(sele);
75 if (!evaluator_.isDynamic()) {
76 seleMan_.setSelectionSet(evaluator_.evaluate());
81 momentumLabel_ =
"Angular Momentum: J";
85 momentumLabel_ =
"Linear Momentum: P";
91 componentLabel_ =
"x";
94 componentLabel_ =
"y";
98 componentLabel_ =
"z";
102 setOutputName(
getPrefix(filename) +
".MomentumHistogram");
105 void MomentumHistogram::process() {
109 if (evaluator_.isDynamic()) {
110 seleMan_.setSelectionSet(evaluator_.evaluate());
113 DumpReader reader(info_, dumpFilename_);
114 int nFrames = reader.getNFrames();
115 nProcessed_ = nFrames / step_;
116 vector<RealType> momentum;
118 nProcessed_ = nFrames / step_;
120 for (
int istep = 0; istep < nFrames; istep += step_) {
121 reader.readFrame(istep);
122 currentSnapshot_ = info_->getSnapshotManager()->getCurrentSnapshot();
124 for (sd = seleMan_.beginSelected(ii); sd != NULL;
125 sd = seleMan_.nextSelected(ii)) {
128 Vector3d linMom = sd->getVel() * sd->getMass();
129 momentum.push_back(linMom[mom_comp_]);
134 if (sd->isDirectional()) {
135 Vector3d angMom = sd->getJ();
136 momentum.push_back(angMom[mom_comp_]);
143 if (momentum.empty()) {
144 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
145 "Momentum for selected atom not found.\n");
146 painCave.isFatal = 1;
150 std::sort(momentum.begin(), momentum.end());
152 RealType min = momentum.front();
153 RealType max = momentum.back();
155 RealType delta_momentum = (max - min) / (nBins_);
157 if (delta_momentum == 0) {
158 bincenter_.push_back(min);
159 histList_.push_back(momentum.size());
162 for (
int j = 0; j < nBins_ + 3; ++j) {
163 bincenter_.push_back(min + (j - 1) * delta_momentum);
164 histList_.push_back(0);
168 int bin_center_pos = 0;
169 vector<RealType>::iterator index;
170 RealType momentum_length =
static_cast<RealType
>(momentum.size());
173 for (index = momentum.begin(); index < momentum.end(); index++) {
175 while (hist_update) {
176 if (*index >= bincenter_[bin_center_pos] &&
177 *index < bincenter_[bin_center_pos + 1]) {
178 histList_[bin_center_pos] +=
179 1.0 / (momentum_length * delta_momentum);
191 void MomentumHistogram::write() {
192 std::ofstream rdfStream(outputFilename_.c_str());
193 if (rdfStream.is_open()) {
194 rdfStream <<
"#" << momentumLabel_ << componentLabel_
195 <<
" distribtution \n";
196 rdfStream <<
"# nFrames:\t" << nProcessed_ <<
"\n";
197 rdfStream <<
"# selection: (" << selectionScript_ <<
")\n";
201 for (
unsigned int i = 0; i < histList_.size(); ++i) {
202 rdfStream << bincenter_[i] <<
"\t" << histList_[i] <<
"\n";
206 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
207 "MomentumHistogram: unable to open %s\n",
208 outputFilename_.c_str());
209 painCave.isFatal = 1;
One of the heavy-weight classes of OpenMD, SimInfo maintains objects and variables relating to the cu...
This basic Periodic Table class was originally taken from the data.cpp file in OpenBabel.
std::string getPrefix(const std::string &str)