51#include "applications/staticProps/MomentumHistogram.hpp"
59#include "utils/simError.h"
63 MomentumHistogram::MomentumHistogram(SimInfo* info,
64 const std::string& filename,
65 const std::string& sele,
int nbins,
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());
78 momentumLabel_ =
"Angular Momentum: J";
82 momentumLabel_ =
"Linear Momentum: P";
88 componentLabel_ =
"x";
91 componentLabel_ =
"y";
95 componentLabel_ =
"z";
99 setOutputName(
getPrefix(filename) +
".MomentumHistogram");
102 void MomentumHistogram::process() {
106 if (evaluator_.isDynamic()) {
107 seleMan_.setSelectionSet(evaluator_.evaluate());
110 DumpReader reader(info_, dumpFilename_);
111 int nFrames = reader.getNFrames();
112 nProcessed_ = nFrames / step_;
113 vector<RealType> momentum;
115 nProcessed_ = nFrames / step_;
117 for (
int istep = 0; istep < nFrames; istep += step_) {
118 reader.readFrame(istep);
119 currentSnapshot_ = info_->getSnapshotManager()->getCurrentSnapshot();
121 for (sd = seleMan_.beginSelected(ii); sd != NULL;
122 sd = seleMan_.nextSelected(ii)) {
125 Vector3d linMom = sd->getVel() * sd->getMass();
126 momentum.push_back(linMom[mom_comp_]);
131 if (sd->isDirectional()) {
132 Vector3d angMom = sd->getJ();
133 momentum.push_back(angMom[mom_comp_]);
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;
147 std::sort(momentum.begin(), momentum.end());
149 RealType min = momentum.front();
150 RealType max = momentum.back();
152 RealType delta_momentum = (max - min) / (nBins_);
154 if (delta_momentum == 0) {
155 bincenter_.push_back(min);
156 histList_.push_back(momentum.size());
159 for (
int j = 0; j < nBins_ + 3; ++j) {
160 bincenter_.push_back(min + (j - 1) * delta_momentum);
161 histList_.push_back(0);
165 int bin_center_pos = 0;
166 vector<RealType>::iterator index;
167 RealType momentum_length =
static_cast<RealType
>(momentum.size());
170 for (index = momentum.begin(); index < momentum.end(); index++) {
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);
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";
198 for (
unsigned int i = 0; i < histList_.size(); ++i) {
199 rdfStream << bincenter_[i] <<
"\t" << histList_[i] <<
"\n";
203 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
204 "MomentumHistogram: unable to open %s\n",
205 outputFilename_.c_str());
206 painCave.isFatal = 1;
This basic Periodic Table class was originally taken from the data.cpp file in OpenBabel.
std::string getPrefix(const std::string &str)