48#include "applications/dynamicProps/LegendreCorrFuncZ.hpp"
53#include "utils/Revision.hpp"
54#include "utils/simError.h"
57 LegendreCorrFuncZ::LegendreCorrFuncZ(
SimInfo* info,
58 const std::string& filename,
59 const std::string& sele1,
60 const std::string& sele2,
int order,
61 int nZbins,
int axis) :
62 ObjectACF<Vector3d>(info, filename, sele1, sele2),
63 nZBins_(nZbins), axis_(axis) {
64 setCorrFuncType(
"Legendre Correlation Function of Z");
65 setOutputName(
getPrefix(dumpFilename_) +
".lcorrZ");
67 std::stringstream params;
68 params <<
" order = " << order <<
", nzbins = " << nZBins_;
69 const std::string paramString = params.str();
70 setParameterString(paramString);
72 if (!uniqueSelections_) { seleMan2_ = seleMan1_; }
75 xaxis_ = (axis_ + 1) % 3;
76 yaxis_ = (axis_ + 2) % 3;
91 rotMats_.resize(nTimeBins_);
92 zbin_.resize(nTimeBins_);
93 histogram_.resize(nTimeBins_);
94 counts_.resize(nTimeBins_);
95 for (
unsigned int i = 0; i < nTimeBins_; i++) {
96 histogram_[i].resize(nZBins_);
97 std::fill(histogram_[i].begin(), histogram_[i].end(), 0.0);
98 counts_[i].resize(nZBins_);
99 std::fill(counts_[i].begin(), counts_[i].end(), 0);
101 LegendrePolynomial polynomial(order);
102 legendre_ = polynomial.getLegendrePolynomial(order);
105 void LegendreCorrFuncZ::computeFrame(
int frame) {
106 Mat3x3d hmat = currentSnapshot_->getHmat();
107 boxZ_ = hmat(axis_, axis_);
108 halfBoxZ_ = boxZ_ / 2.0;
110 ObjectACF<Vector3d>::computeFrame(frame);
113 int LegendreCorrFuncZ::computeProperty1(
int frame, StuntDouble* sd) {
114 RotMat3x3d A = sd->getA();
115 rotMats_[frame].push_back(A);
117 Vector3d pos = sd->getPos();
118 if (info_->getSimParams()->getUsePeriodicBoundaryConditions())
119 currentSnapshot_->wrapVector(pos);
120 int zBin = int(nZBins_ * (halfBoxZ_ + pos[axis_]) / boxZ_);
121 zbin_[frame].push_back(zBin);
123 return rotMats_[frame].size() - 1;
126 Vector3d LegendreCorrFuncZ::calcCorrVal(
int frame1,
int frame2,
int id1,
128 Vector3d v1x = rotMats_[frame1][id1].getRow(xaxis_);
129 Vector3d v1y = rotMats_[frame1][id1].getRow(yaxis_);
130 Vector3d v1z = rotMats_[frame1][id1].getRow(axis_);
132 Vector3d v2x = rotMats_[frame2][id2].getRow(xaxis_);
133 Vector3d v2y = rotMats_[frame2][id2].getRow(yaxis_);
134 Vector3d v2z = rotMats_[frame2][id2].getRow(axis_);
137 legendre_.evaluate(
dot(v1x, v2x) / (v1x.length() * v2x.length()));
139 legendre_.evaluate(
dot(v1y, v2y) / (v1y.length() * v2y.length()));
141 legendre_.evaluate(
dot(v1z, v2z) / (v1z.length() * v2z.length()));
143 return Vector3d(uxprod, uyprod, uzprod);
146 void LegendreCorrFuncZ::correlateFrames(
int frame1,
int frame2,
int timeBin) {
150 std::vector<int>::iterator i1;
151 std::vector<int>::iterator i2;
153 Vector3d corrVal(0.0);
155 s1 = sele1ToIndex_[frame1];
157 if (uniqueSelections_)
158 s2 = sele2ToIndex_[frame2];
160 s2 = sele1ToIndex_[frame2];
162 for (i1 = s1.begin(), i2 = s2.begin(); i1 != s1.end() && i2 != s2.end();
169 while (i1 != s1.end() && *i1 < *i2) {
173 while (i2 != s2.end() && *i2 < *i1) {
177 if (i1 == s1.end() || i2 == s2.end())
break;
179 corrVal = calcCorrVal(frame1, frame2, i1 - s1.begin(), i2 - s2.begin());
180 int zBin = zbin_[frame1][i1 - s1.begin()];
181 histogram_[timeBin][zBin] += corrVal;
182 counts_[timeBin][zBin]++;
186 void LegendreCorrFuncZ::postCorrelate() {
187 for (
unsigned int i = 0; i < nTimeBins_; ++i) {
188 for (
unsigned int j = 0; j < nZBins_; ++j) {
189 if (counts_[i][j] > 0) { histogram_[i][j] /= counts_[i][j]; }
194 void LegendreCorrFuncZ::validateSelection(SelectionManager&) {
197 for (sd = seleMan1_.beginSelected(i); sd != NULL;
198 sd = seleMan1_.nextSelected(i)) {
199 if (!sd->isDirectional()) {
200 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
201 "LegendreCorrFuncZ::validateSelection Error: "
202 "at least one of the selected objects is not Directional\n");
203 painCave.isFatal = 1;
209 void LegendreCorrFuncZ::writeCorrelate() {
210 std::ofstream ofs(getOutputFileName().c_str());
215 ofs <<
"# " << getCorrFuncType() <<
"\n";
216 ofs <<
"# OpenMD " << r.getFullRevision() <<
"\n";
217 ofs <<
"# " << r.getBuildDate() <<
"\n";
218 ofs <<
"# selection script1: \"" << selectionScript1_;
219 ofs <<
"\"\tselection script2: \"" << selectionScript2_ <<
"\"\n";
220 ofs <<
"# privilegedAxis computed as " << axisLabel_ <<
" axis \n";
221 if (!paramString_.empty())
222 ofs <<
"# parameters: " << paramString_ <<
"\n";
224 ofs <<
"#time\tPn(costheta_z)\n";
226 for (
unsigned int i = 0; i < nTimeBins_; ++i) {
227 ofs << times_[i] - times_[0];
229 for (
unsigned int j = 0; j < nZBins_; ++j) {
230 ofs <<
"\t" << histogram_[i][j](2);
236 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
237 "LegendreCorrFuncZ::writeCorrelate Error: failed to open %s\n",
238 getOutputFileName().c_str());
239 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.
Real dot(const DynamicVector< Real > &v1, const DynamicVector< Real > &v2)
Returns the dot product of two DynamicVectors.
std::string getPrefix(const std::string &str)