47#include "applications/staticProps/AngleR.hpp"
54#include "brains/Thermo.hpp"
57#include "utils/Revision.hpp"
58#include "utils/simError.h"
62 AngleR::AngleR(SimInfo* info,
const std::string& filename,
63 const std::string& sele1, RealType len,
int nrbins) :
64 StaticAnalyser(info, filename, nrbins),
65 doVect_(true), doOffset_(false), selectionScript1_(sele1),
66 seleMan1_(info), seleMan2_(info), evaluator1_(info), evaluator2_(info),
67 len_(len), nRBins_(nrbins) {
68 evaluator1_.loadScriptString(sele1);
69 if (!evaluator1_.isDynamic()) {
70 seleMan1_.setSelectionSet(evaluator1_.evaluate());
73 deltaR_ = len_ / nRBins_;
75 histogram_.resize(nRBins_);
76 count_.resize(nRBins_);
77 avgAngleR_.resize(nRBins_);
79 setAnalysisType(
"radial density function Angle(r)");
80 setOutputName(
getPrefix(filename) +
".AngleR");
81 std::stringstream params;
82 params <<
" len = " << len_ <<
", nrbins = " << nRBins_;
83 const std::string paramString = params.str();
84 setParameterString(paramString);
87 AngleR::AngleR(SimInfo* info,
const std::string& filename,
88 const std::string& sele1,
const std::string& sele2,
89 RealType len,
int nrbins) :
90 StaticAnalyser(info, filename, nrbins),
91 doVect_(false), doOffset_(false), selectionScript1_(sele1),
92 selectionScript2_(sele2), seleMan1_(info), seleMan2_(info),
93 evaluator1_(info), evaluator2_(info), len_(len), nRBins_(nrbins) {
94 setOutputName(
getPrefix(filename) +
".AngleR");
96 evaluator1_.loadScriptString(sele1);
97 if (!evaluator1_.isDynamic()) {
98 seleMan1_.setSelectionSet(evaluator1_.evaluate());
101 evaluator2_.loadScriptString(sele2);
102 if (!evaluator2_.isDynamic()) {
103 seleMan2_.setSelectionSet(evaluator2_.evaluate());
106 deltaR_ = len_ / nRBins_;
108 histogram_.resize(nRBins_);
109 count_.resize(nRBins_);
110 avgAngleR_.resize(nRBins_);
112 setAnalysisType(
"radial density function Angle(r)");
113 setOutputName(
getPrefix(filename) +
".AngleR");
114 std::stringstream params;
115 params <<
" len = " << len_ <<
", nrbins = " << nRBins_;
116 const std::string paramString = params.str();
117 setParameterString(paramString);
120 AngleR::AngleR(SimInfo* info,
const std::string& filename,
121 const std::string& sele1,
int seleOffset, RealType len,
123 StaticAnalyser(info, filename, nrbins),
124 doVect_(false), doOffset_(true), doOffset2_(false),
125 selectionScript1_(sele1), seleMan1_(info), seleMan2_(info),
126 evaluator1_(info), evaluator2_(info), seleOffset_(seleOffset), len_(len),
128 setOutputName(
getPrefix(filename) +
".AngleR");
130 evaluator1_.loadScriptString(sele1);
131 if (!evaluator1_.isDynamic()) {
132 seleMan1_.setSelectionSet(evaluator1_.evaluate());
135 deltaR_ = len_ / nRBins_;
137 histogram_.resize(nRBins_);
138 count_.resize(nRBins_);
139 avgAngleR_.resize(nRBins_);
141 setAnalysisType(
"radial density function Angle(r)");
142 setOutputName(
getPrefix(filename) +
".AngleR");
143 std::stringstream params;
144 params <<
" len = " << len_ <<
", nrbins = " << nRBins_;
145 const std::string paramString = params.str();
146 setParameterString(paramString);
149 AngleR::AngleR(SimInfo* info,
const std::string& filename,
150 const std::string& sele1,
int seleOffset,
int seleOffset2,
151 RealType len,
int nrbins) :
152 StaticAnalyser(info, filename, nrbins),
153 doVect_(false), doOffset_(true), doOffset2_(true),
154 selectionScript1_(sele1), seleMan1_(info), seleMan2_(info),
155 evaluator1_(info), evaluator2_(info), seleOffset_(seleOffset),
156 seleOffset2_(seleOffset2), len_(len), nRBins_(nrbins) {
157 setOutputName(
getPrefix(filename) +
".AngleR");
159 evaluator1_.loadScriptString(sele1);
160 if (!evaluator1_.isDynamic()) {
161 seleMan1_.setSelectionSet(evaluator1_.evaluate());
164 histogram_.resize(nRBins_);
165 count_.resize(nRBins_);
166 avgAngleR_.resize(nRBins_);
168 setAnalysisType(
"radial density function Angle(r)");
169 setOutputName(
getPrefix(filename) +
".AngleR");
170 std::stringstream params;
171 params <<
" len = " << len_ <<
", nrbins = " << nRBins_;
172 const std::string paramString = params.str();
173 setParameterString(paramString);
176 void AngleR::process() {
182 bool usePeriodicBoundaryConditions_ =
183 info_->getSimParams()->getUsePeriodicBoundaryConditions();
185 Thermo thermo(info_);
186 DumpReader reader(info_, dumpFilename_);
187 int nFrames = reader.getNFrames();
189 nProcessed_ = nFrames / step_;
191 std::fill(avgAngleR_.begin(), avgAngleR_.end(), 0.0);
192 std::fill(histogram_.begin(), histogram_.end(), 0.0);
193 std::fill(count_.begin(), count_.end(), 0);
195 for (
int istep = 0; istep < nFrames; istep += step_) {
196 reader.readFrame(istep);
197 currentSnapshot_ = info_->getSnapshotManager()->getCurrentSnapshot();
199 Vector3d CenterOfMass = thermo.getCom();
201 if (evaluator1_.isDynamic()) {
202 seleMan1_.setSelectionSet(evaluator1_.evaluate());
206 for (sd1 = seleMan1_.beginSelected(ii); sd1 != NULL;
207 sd1 = seleMan1_.nextSelected(ii)) {
208 Vector3d pos = sd1->getPos();
210 Vector3d r1 = CenterOfMass - pos;
214 if (sd1->isDirectional()) {
215 Vector3d vec = sd1->getA().transpose() * V3Z;
218 RealType cosangle =
dot(r1, vec);
220 if (distance < len_) {
221 int whichBin = int(distance / deltaR_);
222 histogram_[whichBin] += cosangle;
223 count_[whichBin] += 1;
229 for (sd1 = seleMan1_.beginSelected(ii); sd1 != NULL;
230 sd1 = seleMan1_.nextSelected(ii)) {
237 int sd1Aind = sd1->getGlobalIndex() + seleOffset2_;
238 StuntDouble* sd1A = info_->getIOIndexToIntegrableObject(sd1Aind);
239 r1 = CenterOfMass - sd1A->getPos();
241 r1 = CenterOfMass - sd1->getPos();
244 if (usePeriodicBoundaryConditions_)
245 currentSnapshot_->wrapVector(r1);
247 int sd2Index = sd1->getGlobalIndex() + seleOffset_;
248 sd2 = info_->getIOIndexToIntegrableObject(sd2Index);
250 Vector3d r2 = CenterOfMass - sd2->getPos();
251 if (usePeriodicBoundaryConditions_)
252 currentSnapshot_->wrapVector(r1);
254 Vector3d rc = 0.5 * (r1 + r2);
255 if (usePeriodicBoundaryConditions_)
256 currentSnapshot_->wrapVector(rc);
260 Vector3d vec = r1 - r2;
261 if (usePeriodicBoundaryConditions_)
262 currentSnapshot_->wrapVector(vec);
266 RealType cosangle =
dot(rc, vec);
267 if (distance < len_) {
268 int whichBin = int(distance / deltaR_);
269 histogram_[whichBin] += cosangle;
270 count_[whichBin] += 1;
274 if (evaluator2_.isDynamic()) {
275 seleMan2_.setSelectionSet(evaluator2_.evaluate());
278 if (seleMan1_.getSelectionCount() != seleMan2_.getSelectionCount()) {
279 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
280 "In frame %d, the number of selected StuntDoubles are\n"
281 "\tnot the same in --sele1 and sele2\n",
283 painCave.severity = OPENMD_INFO;
284 painCave.isFatal = 0;
288 for (sd1 = seleMan1_.beginSelected(ii),
289 sd2 = seleMan2_.beginSelected(jj);
290 sd1 != NULL && sd2 != NULL; sd1 = seleMan1_.nextSelected(ii),
291 sd2 = seleMan2_.nextSelected(jj)) {
292 Vector3d r1 = CenterOfMass - sd1->getPos();
293 if (usePeriodicBoundaryConditions_)
294 currentSnapshot_->wrapVector(r1);
296 Vector3d r2 = CenterOfMass - sd2->getPos();
297 if (usePeriodicBoundaryConditions_)
298 currentSnapshot_->wrapVector(r1);
300 Vector3d rc = 0.5 * (r1 + r2);
301 if (usePeriodicBoundaryConditions_)
302 currentSnapshot_->wrapVector(rc);
304 Vector3d vec = r1 - r2;
305 if (usePeriodicBoundaryConditions_)
306 currentSnapshot_->wrapVector(vec);
312 RealType cosangle =
dot(rc, vec);
313 if (distance < len_) {
314 int whichBin = int(distance / deltaR_);
315 histogram_[whichBin] += cosangle;
316 count_[whichBin] += 1;
326 void AngleR::processHistogram() {
327 for (
unsigned int i = 0; i < histogram_.size(); ++i) {
329 avgAngleR_[i] += histogram_[i] / count_[i];
333 std::cerr <<
" count = " << count_[i] <<
" avgAngle = " << avgAngleR_[i]
338 void AngleR::writeAngleR() {
339 std::ofstream ofs(outputFilename_.c_str());
343 ofs <<
"# " << getAnalysisType() <<
"\n";
344 ofs <<
"# OpenMD " << rev.getFullRevision() <<
"\n";
345 ofs <<
"# " << rev.getBuildDate() <<
"\n";
346 ofs <<
"#nFrames:\t" << nProcessed_ <<
"\n";
347 ofs <<
"#selection1: (" << selectionScript1_ <<
")";
348 if (!doVect_) { ofs <<
"\tselection2: (" << selectionScript2_ <<
")"; }
349 if (!paramString_.empty())
350 ofs <<
"# parameters: " << paramString_ <<
"\n";
352 ofs <<
"#r\tcorrValue\n";
353 for (
unsigned int i = 0; i < avgAngleR_.size(); ++i) {
354 RealType r = deltaR_ * (i + 0.5);
355 ofs << r <<
"\t" << avgAngleR_[i] <<
"\n";
359 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
360 "AngleR: unable to open %s\n", outputFilename_.c_str());
361 painCave.isFatal = 1;
void normalize()
Normalizes this vector in place.
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)
Real distance(const DynamicVector< Real > &v1, const DynamicVector< Real > &v2)
Returns the distance between two DynamicVectors.