48#include "applications/staticProps/DensityPlot.hpp"
56#include "types/LennardJonesAdapter.hpp"
57#include "utils/Constants.hpp"
58#include "utils/simError.h"
64 DensityPlot::DensityPlot(
SimInfo* info,
const std::string& filename,
65 const std::string& sele,
const std::string& cmSele,
66 RealType len,
int nrbins) :
68 len_(len), halfLen_(len / 2), nRBins_(nrbins), selectionScript_(sele),
69 seleMan_(info), evaluator_(info), cmSelectionScript_(cmSele),
70 cmSeleMan_(info), cmEvaluator_(info) {
71 setOutputName(
getPrefix(filename) +
".density");
73 deltaR_ = len_ / nRBins_;
74 histogram_.resize(nRBins_);
75 density_.resize(nRBins_);
77 std::fill(histogram_.begin(), histogram_.end(), 0);
79 evaluator_.loadScriptString(sele);
81 if (!evaluator_.isDynamic()) {
82 seleMan_.setSelectionSet(evaluator_.evaluate());
85 cmEvaluator_.loadScriptString(cmSele);
86 if (!cmEvaluator_.isDynamic()) {
87 cmSeleMan_.setSelectionSet(cmEvaluator_.evaluate());
91 void DensityPlot::process() {
92 bool usePeriodicBoundaryConditions_ =
93 info_->getSimParams()->getUsePeriodicBoundaryConditions();
95 DumpReader reader(info_, dumpFilename_);
96 int nFrames = reader.getNFrames();
97 for (
int i = 0; i < nFrames; i += step_) {
99 currentSnapshot_ = info_->getSnapshotManager()->getCurrentSnapshot();
101 if (evaluator_.isDynamic()) {
102 seleMan_.setSelectionSet(evaluator_.evaluate());
105 if (cmEvaluator_.isDynamic()) {
106 cmSeleMan_.setSelectionSet(cmEvaluator_.evaluate());
109 Vector3d origin = calcNewOrigin();
111 Mat3x3d hmat = currentSnapshot_->getHmat();
112 RealType slabVolume = deltaR_ * hmat(0, 0) * hmat(1, 1);
114 for (StuntDouble* sd = seleMan_.beginSelected(k); sd != NULL;
115 sd = seleMan_.nextSelected(k)) {
117 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
118 "Can not calculate electron density if it is not atom\n");
119 painCave.severity = OPENMD_ERROR;
120 painCave.isFatal = 1;
124 Atom* atom =
static_cast<Atom*
>(sd);
125 std::shared_ptr<GenericData> data =
126 atom->getAtomType()->getPropertyByName(
"nelectron");
127 if (data ==
nullptr) {
128 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
129 "Can not find Parameters for nelectron\n");
130 painCave.severity = OPENMD_ERROR;
131 painCave.isFatal = 1;
135 std::shared_ptr<DoubleGenericData> doubleData =
136 std::dynamic_pointer_cast<DoubleGenericData>(data);
137 if (doubleData ==
nullptr) {
138 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
139 "Can not cast GenericData to DoubleGenericData\n");
140 painCave.severity = OPENMD_ERROR;
141 painCave.isFatal = 1;
145 RealType nelectron = doubleData->getData();
146 LennardJonesAdapter lja = LennardJonesAdapter(atom->getAtomType());
147 RealType sigma = lja.getSigma() * 0.5;
148 RealType sigma2 = sigma * sigma;
150 Vector3d pos = sd->getPos() - origin;
151 for (
int j = 0; j < nRBins_; ++j) {
153 RealType zdist = j * deltaR_ - halfLen_;
155 if (usePeriodicBoundaryConditions_) currentSnapshot_->wrapVector(tmp);
157 RealType wrappedZdist = tmp.z() + halfLen_;
158 if (wrappedZdist < 0.0 || wrappedZdist > len_) {
continue; }
160 int which = int(wrappedZdist / deltaR_);
162 nelectron * exp(-zdist * zdist / (sigma2 * 2.0)) /
163 (slabVolume * sqrt(2 * Constants::PI * sigma * sigma));
168 int nProcessed = nFrames / step_;
170 density_.begin(), density_.end(), density_.begin(),
171 std::bind(std::divides<RealType>(), std::placeholders::_1, nProcessed));
175 Vector3d DensityPlot::calcNewOrigin() {
177 Vector3d newOrigin(0.0);
178 RealType totalMass = 0.0;
179 for (StuntDouble* sd = seleMan_.beginSelected(i); sd != NULL;
180 sd = seleMan_.nextSelected(i)) {
181 RealType mass = sd->getMass();
183 newOrigin += sd->getPos() * mass;
185 newOrigin /= totalMass;
189 void DensityPlot::writeDensity() {
190 std::ofstream ofs(outputFilename_.c_str(), std::ios::binary);
192 ofs <<
"#g(x, y, z)\n";
193 ofs <<
"#selection: (" << selectionScript_ <<
")\n";
194 ofs <<
"#cmSelection:(" << cmSelectionScript_ <<
")\n";
195 ofs <<
"#nRBins = " << nRBins_ <<
"\t maxLen = " << len_
196 <<
"\tdeltaR = " << deltaR_ <<
"\n";
197 for (
unsigned int i = 0; i < histogram_.size(); ++i) {
198 ofs << i * deltaR_ - halfLen_ <<
"\t" << density_[i] << std::endl;
201 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
202 "DensityPlot: unable to open %s\n", outputFilename_.c_str());
203 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)