54#include "applications/staticProps/ChargeHistogram.hpp"
62#include "types/FixedChargeAdapter.hpp"
63#include "types/FluctuatingChargeAdapter.hpp"
64#include "utils/simError.h"
68 ChargeHistogram::ChargeHistogram(
SimInfo* info,
const std::string& filename,
69 const std::string& sele,
int nbins) :
71 selectionScript_(sele), evaluator_(info), seleMan_(info), nBins_(nbins) {
72 evaluator_.loadScriptString(sele);
73 if (!evaluator_.isDynamic()) {
74 seleMan_.setSelectionSet(evaluator_.evaluate());
77 setOutputName(
getPrefix(filename) +
".Chargehist");
80 void ChargeHistogram::process() {
84 if (evaluator_.isDynamic()) {
85 seleMan_.setSelectionSet(evaluator_.evaluate());
88 DumpReader reader(info_, dumpFilename_);
89 int nFrames = reader.getNFrames();
90 nProcessed_ = nFrames / step_;
91 vector<RealType> charge;
93 nProcessed_ = nFrames / step_;
95 for (
int istep = 0; istep < nFrames; istep += step_) {
96 reader.readFrame(istep);
97 currentSnapshot_ = info_->getSnapshotManager()->getCurrentSnapshot();
99 for (sd = seleMan_.beginSelected(ii); sd != NULL;
100 sd = seleMan_.nextSelected(ii)) {
102 Atom* atom =
static_cast<Atom*
>(sd);
104 AtomType* atomType = atom->getAtomType();
106 FixedChargeAdapter fca = FixedChargeAdapter(atomType);
107 if (fca.isFixedCharge()) { q += fca.getCharge(); }
109 FluctuatingChargeAdapter fqa = FluctuatingChargeAdapter(atomType);
110 if (fqa.isFluctuatingCharge()) { q += atom->getFlucQPos(); }
116 if (charge.empty()) {
117 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
118 "Selected atom not found.\n");
119 painCave.isFatal = 1;
123 std::sort(charge.begin(), charge.end());
125 RealType min = charge.front();
126 RealType max = charge.back();
128 RealType delta_charge = (max - min) / (nBins_);
130 if (delta_charge == 0) {
131 bincenter_.push_back(min);
132 histList_.push_back(1);
135 for (
int j = 0; j < nBins_ + 3; ++j) {
136 bincenter_.push_back(min + (j - 1) * delta_charge);
137 histList_.push_back(0);
140 int bin_center_pos = 0;
141 vector<RealType>::iterator index;
142 RealType charge_length =
static_cast<RealType
>(charge.size());
145 for (index = charge.begin(); index < charge.end(); index++) {
147 while (hist_update) {
148 if (*index >= bincenter_[bin_center_pos] &&
149 *index < bincenter_[bin_center_pos + 1]) {
150 histList_[bin_center_pos] += 1.0 / charge_length;
162 void ChargeHistogram::writeCharge() {
163 std::ofstream rdfStream(outputFilename_.c_str());
164 if (rdfStream.is_open()) {
165 rdfStream <<
"#Charges for selection\n";
166 rdfStream <<
"#nFrames:\t" << nProcessed_ <<
"\n";
167 rdfStream <<
"#selection: (" << selectionScript_ <<
")\n";
171 for (
unsigned int i = 0; i < histList_.size(); ++i) {
172 rdfStream << bincenter_[i] <<
"\t" << histList_[i] <<
"\n";
175 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
176 "ChargeHistogram: unable to open %s\n", outputFilename_.c_str());
177 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)