51#include "applications/staticProps/DensityHistogram.hpp"
59#include "utils/simError.h"
63 DensityHistogram::DensityHistogram(SimInfo* info,
const std::string& filename,
64 const std::string& sele,
int nbins) :
65 StaticAnalyser(info, filename, nbins),
66 selectionScript_(sele), evaluator_(info), seleMan_(info), nBins_(nbins) {
67 evaluator_.loadScriptString(sele);
68 if (!evaluator_.isDynamic()) {
69 seleMan_.setSelectionSet(evaluator_.evaluate());
72 setOutputName(
getPrefix(filename) +
".EAMDensity");
75 void DensityHistogram::process() {
79 if (evaluator_.isDynamic()) {
80 seleMan_.setSelectionSet(evaluator_.evaluate());
83 DumpReader reader(info_, dumpFilename_);
84 int nFrames = reader.getNFrames();
85 nProcessed_ = nFrames / step_;
86 vector<RealType> density;
88 nProcessed_ = nFrames / step_;
90 for (
int istep = 0; istep < nFrames; istep += step_) {
91 reader.readFrame(istep);
92 currentSnapshot_ = info_->getSnapshotManager()->getCurrentSnapshot();
94 for (sd = seleMan_.beginSelected(ii); sd != NULL;
95 sd = seleMan_.nextSelected(ii)) {
96 if (sd->getDensity()) density.push_back(sd->getDensity());
100 if (density.empty()) {
101 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
102 "Density for selected atom not found.\n");
103 painCave.isFatal = 1;
107 std::sort(density.begin(), density.end());
109 RealType min = density.front();
110 RealType max = density.back();
112 RealType delta_density = (max - min) / (nBins_);
114 if (delta_density == 0) {
115 bincenter_.push_back(min);
116 histList_.push_back(density.size());
117 averageDensity_ = min;
120 for (
int j = 0; j < nBins_ + 3; ++j) {
121 bincenter_.push_back(min + (j - 1) * delta_density);
122 histList_.push_back(0);
125 int bin_center_pos = 0;
126 vector<RealType>::iterator index;
127 RealType density_length =
static_cast<RealType
>(density.size());
130 for (index = density.begin(); index < density.end(); index++) {
132 while (hist_update) {
133 if (*index >= bincenter_[bin_center_pos] &&
134 *index < bincenter_[bin_center_pos + 1]) {
135 histList_[bin_center_pos] += 1.0 / density_length;
139 histList_[bin_center_pos] * bincenter_[bin_center_pos];
145 averageDensity_ += histList_[bin_center_pos] * bincenter_[bin_center_pos];
150 void DensityHistogram::writeDensity() {
151 std::ofstream rdfStream(outputFilename_.c_str());
152 if (rdfStream.is_open()) {
153 rdfStream <<
"#EAMDensity\n";
154 rdfStream <<
"#nFrames:\t" << nProcessed_ <<
"\n";
155 rdfStream <<
"#selection: (" << selectionScript_ <<
")\n";
156 rdfStream <<
"#Average EAM density: " << averageDensity_ <<
"\n";
160 for (
unsigned int i = 0; i < histList_.size(); ++i) {
161 rdfStream << bincenter_[i] <<
"\t" << histList_[i] <<
"\n";
164 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
165 "EAMDensity: unable to open %s\n", outputFilename_.c_str());
166 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)