45#include "applications/staticProps/DensityPlot.hpp"
53#include "types/LennardJonesAdapter.hpp"
54#include "utils/Constants.hpp"
55#include "utils/simError.h"
61 DensityPlot::DensityPlot(SimInfo* info,
const std::string& filename,
62 const std::string& sele,
const std::string& cmSele,
63 RealType len,
int nrbins) :
64 StaticAnalyser(info, filename, nrbins),
65 len_(len), halfLen_(len / 2), nRBins_(nrbins), selectionScript_(sele),
66 seleMan_(info), evaluator_(info), cmSelectionScript_(cmSele),
67 cmSeleMan_(info), cmEvaluator_(info) {
68 setOutputName(
getPrefix(filename) +
".density");
70 deltaR_ = len_ / nRBins_;
71 histogram_.resize(nRBins_);
72 density_.resize(nRBins_);
74 std::fill(histogram_.begin(), histogram_.end(), 0);
76 evaluator_.loadScriptString(sele);
78 if (!evaluator_.isDynamic()) {
79 seleMan_.setSelectionSet(evaluator_.evaluate());
82 cmEvaluator_.loadScriptString(cmSele);
83 if (!cmEvaluator_.isDynamic()) {
84 cmSeleMan_.setSelectionSet(cmEvaluator_.evaluate());
88 void DensityPlot::process() {
89 bool usePeriodicBoundaryConditions_ =
90 info_->getSimParams()->getUsePeriodicBoundaryConditions();
92 DumpReader reader(info_, dumpFilename_);
93 int nFrames = reader.getNFrames();
94 for (
int i = 0; i < nFrames; i += step_) {
96 currentSnapshot_ = info_->getSnapshotManager()->getCurrentSnapshot();
98 if (evaluator_.isDynamic()) {
99 seleMan_.setSelectionSet(evaluator_.evaluate());
102 if (cmEvaluator_.isDynamic()) {
103 cmSeleMan_.setSelectionSet(cmEvaluator_.evaluate());
106 Vector3d origin = calcNewOrigin();
108 Mat3x3d hmat = currentSnapshot_->getHmat();
109 RealType slabVolume = deltaR_ * hmat(0, 0) * hmat(1, 1);
111 for (StuntDouble* sd = seleMan_.beginSelected(k); sd != NULL;
112 sd = seleMan_.nextSelected(k)) {
114 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
115 "Can not calculate electron density if it is not atom\n");
116 painCave.severity = OPENMD_ERROR;
117 painCave.isFatal = 1;
121 Atom* atom =
static_cast<Atom*
>(sd);
122 std::shared_ptr<GenericData> data =
123 atom->getAtomType()->getPropertyByName(
"nelectron");
124 if (data ==
nullptr) {
125 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
126 "Can not find Parameters for nelectron\n");
127 painCave.severity = OPENMD_ERROR;
128 painCave.isFatal = 1;
132 std::shared_ptr<DoubleGenericData> doubleData =
133 std::dynamic_pointer_cast<DoubleGenericData>(data);
134 if (doubleData ==
nullptr) {
135 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
136 "Can not cast GenericData to DoubleGenericData\n");
137 painCave.severity = OPENMD_ERROR;
138 painCave.isFatal = 1;
142 RealType nelectron = doubleData->getData();
143 LennardJonesAdapter lja = LennardJonesAdapter(atom->getAtomType());
144 RealType sigma = lja.getSigma() * 0.5;
145 RealType sigma2 = sigma * sigma;
147 Vector3d pos = sd->getPos() - origin;
148 for (
int j = 0; j < nRBins_; ++j) {
150 RealType zdist = j * deltaR_ - halfLen_;
152 if (usePeriodicBoundaryConditions_) currentSnapshot_->wrapVector(tmp);
154 RealType wrappedZdist = tmp.z() + halfLen_;
155 if (wrappedZdist < 0.0 || wrappedZdist > len_) {
continue; }
157 int which = int(wrappedZdist / deltaR_);
159 nelectron * exp(-zdist * zdist / (sigma2 * 2.0)) /
160 (slabVolume * sqrt(2 * Constants::PI * sigma * sigma));
165 int nProcessed = nFrames / step_;
167 density_.begin(), density_.end(), density_.begin(),
168 std::bind(std::divides<RealType>(), std::placeholders::_1, nProcessed));
172 Vector3d DensityPlot::calcNewOrigin() {
174 Vector3d newOrigin(0.0);
175 RealType totalMass = 0.0;
176 for (StuntDouble* sd = seleMan_.beginSelected(i); sd != NULL;
177 sd = seleMan_.nextSelected(i)) {
178 RealType mass = sd->getMass();
180 newOrigin += sd->getPos() * mass;
182 newOrigin /= totalMass;
186 void DensityPlot::writeDensity() {
187 std::ofstream ofs(outputFilename_.c_str(), std::ios::binary);
189 ofs <<
"#g(x, y, z)\n";
190 ofs <<
"#selection: (" << selectionScript_ <<
")\n";
191 ofs <<
"#cmSelection:(" << cmSelectionScript_ <<
")\n";
192 ofs <<
"#nRBins = " << nRBins_ <<
"\t maxLen = " << len_
193 <<
"\tdeltaR = " << deltaR_ <<
"\n";
194 for (
unsigned int i = 0; i < histogram_.size(); ++i) {
195 ofs << i * deltaR_ - halfLen_ <<
"\t" << density_[i] << std::endl;
198 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
199 "DensityPlot: unable to open %s\n", outputFilename_.c_str());
200 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)