48#include "applications/dynamicProps/TimeCorrFunc.hpp"
54#include "utils/Revision.hpp"
55#include "utils/simError.h"
61 TimeCorrFunc<T>::TimeCorrFunc(
SimInfo* info,
const string& filename,
62 const string& sele1,
const string& sele2) :
64 dumpFilename_(filename), seleMan1_(info_), seleMan2_(info_),
65 selectionScript1_(sele1), selectionScript2_(sele2), evaluator1_(info_),
66 evaluator2_(info_), autoCorrFunc_(false), doSystemProperties_(false),
67 doMolecularProperties_(false), doObjectProperties_(false),
68 doBondProperties_(false), allowTimeFuzz_(false) {
69 reader_ =
new DumpReader(info_, dumpFilename_);
71 uniqueSelections_ = (sele1.compare(sele2) != 0) ? true :
false;
73 Globals* simParams = info_->getSimParams();
74 if (simParams->haveSampleTime()) {
75 deltaTime_ = simParams->getSampleTime();
77 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
78 "TimeCorrFunc Error: can not figure out deltaTime\n");
83 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
"Scanning for frames.");
85 painCave.severity = OPENMD_INFO;
88 nFrames_ = reader_->getNFrames();
89 nTimeBins_ = nFrames_;
92 histogram_.resize(nTimeBins_, zeroType);
93 count_.resize(nTimeBins_, 0);
95 times_.resize(nFrames_);
96 sele1ToIndex_.resize(nFrames_);
97 if (uniqueSelections_) { sele2ToIndex_.resize(nFrames_); }
99 progressBar_ = std::make_unique<ProgressBar>();
103 void TimeCorrFunc<T>::preCorrelate() {
104 evaluator1_.loadScriptString(selectionScript1_);
106 if (!evaluator1_.isDynamic()) {
107 seleMan1_.setSelectionSet(evaluator1_.evaluate());
108 validateSelection(seleMan1_);
111 if (uniqueSelections_) {
112 evaluator2_.loadScriptString(selectionScript2_);
113 if (!evaluator2_.isDynamic()) {
114 seleMan2_.setSelectionSet(evaluator2_.evaluate());
115 validateSelection(seleMan2_);
119 progressBar_->clear();
121 for (
int istep = 0; istep < nFrames_; istep++) {
122 reader_->readFrame(istep);
123 currentSnapshot_ = info_->getSnapshotManager()->getCurrentSnapshot();
124 times_[istep] = currentSnapshot_->getTime();
126 progressBar_->setStatus(istep + 1, nFrames_);
127 progressBar_->update();
132 RealType dt2Avg(0.0);
133 for (
int istep = 1; istep < nFrames_; istep++) {
134 RealType dt = times_[istep] - times_[istep - 1];
138 dtMean_ /= RealType(nFrames_ - 1);
139 dt2Avg /= RealType(nFrames_ - 1);
140 dtSigma_ = std::sqrt(dt2Avg - dtMean_ * dtMean_);
142 if (dtSigma_ > 1.0e-6) {
143 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
144 "TimeCorrFunc::preCorrelate: sampleTime (%f) does not match\n"
145 "\tthe mean spacing between configurations (%f), with\n"
146 "\tsigma (%f). Proceeding with the mean value.\n",
147 deltaTime_, dtMean_, dtSigma_);
148 allowTimeFuzz_ =
true;
149 painCave.isFatal = 0;
150 painCave.severity = OPENMD_INFO;
156 void TimeCorrFunc<T>::computeFrame(
int istep) {
160 int imol1, imol2, isd1, isd2, ibond1, ibond2;
163 if (evaluator1_.isDynamic()) {
164 seleMan1_.setSelectionSet(evaluator1_.evaluate());
165 validateSelection(seleMan1_);
168 if (uniqueSelections_ && evaluator2_.isDynamic()) {
169 seleMan2_.setSelectionSet(evaluator2_.evaluate());
170 validateSelection(seleMan2_);
173 if (doSystemProperties_) {
174 computeProperty1(istep);
175 if (!autoCorrFunc_) computeProperty2(istep);
178 if (doMolecularProperties_) {
179 for (mol = seleMan1_.beginSelectedMolecule(imol1); mol != NULL;
180 mol = seleMan1_.nextSelectedMolecule(imol1)) {
181 index = computeProperty1(istep, mol);
183 if (index == sele1ToIndex_[istep].size()) {
184 sele1ToIndex_[istep].push_back(mol->getGlobalIndex());
186 sele1ToIndex_[istep].resize(index + 1);
187 sele1ToIndex_[istep][index] = mol->getGlobalIndex();
190 if (!uniqueSelections_) { index = computeProperty2(istep, mol); }
193 if (uniqueSelections_) {
194 for (mol = seleMan2_.beginSelectedMolecule(imol2); mol != NULL;
195 mol = seleMan2_.nextSelectedMolecule(imol2)) {
197 index = computeProperty1(istep, mol);
199 index = computeProperty2(istep, mol);
201 if (index == sele2ToIndex_[istep].size()) {
202 sele2ToIndex_[istep].push_back(mol->getGlobalIndex());
204 sele2ToIndex_[istep].resize(index + 1);
205 sele2ToIndex_[istep][index] = mol->getGlobalIndex();
211 if (doObjectProperties_) {
212 for (sd = seleMan1_.beginSelected(isd1); sd != NULL;
213 sd = seleMan1_.nextSelected(isd1)) {
214 index = computeProperty1(istep, sd);
216 if (index == sele1ToIndex_[istep].size()) {
217 sele1ToIndex_[istep].push_back(sd->getGlobalIndex());
219 sele1ToIndex_[istep].resize(index + 1);
220 sele1ToIndex_[istep][index] = sd->getGlobalIndex();
223 if (!uniqueSelections_) { index = computeProperty2(istep, sd); }
226 if (uniqueSelections_) {
227 for (sd = seleMan2_.beginSelected(isd2); sd != NULL;
228 sd = seleMan2_.nextSelected(isd2)) {
230 index = computeProperty1(istep, sd);
232 index = computeProperty2(istep, sd);
234 if (index == sele2ToIndex_[istep].size()) {
235 sele2ToIndex_[istep].push_back(sd->getGlobalIndex());
237 sele2ToIndex_[istep].resize(index + 1);
238 sele2ToIndex_[istep][index] = sd->getGlobalIndex();
244 if (doBondProperties_) {
245 for (bond = seleMan1_.beginSelectedBond(ibond1); bond != NULL;
246 bond = seleMan1_.nextSelectedBond(ibond1)) {
247 index = computeProperty1(istep, bond);
249 if (index == sele1ToIndex_[istep].size()) {
250 sele1ToIndex_[istep].push_back(bond->getGlobalIndex());
252 sele1ToIndex_[istep].resize(index + 1);
253 sele1ToIndex_[istep][index] = bond->getGlobalIndex();
256 if (!uniqueSelections_) { index = computeProperty2(istep, bond); }
259 if (uniqueSelections_) {
260 for (bond = seleMan2_.beginSelectedBond(ibond2); bond != NULL;
261 bond = seleMan2_.nextSelectedBond(ibond2)) {
263 index = computeProperty1(istep, bond);
265 index = computeProperty2(istep, bond);
267 if (index == sele2ToIndex_[istep].size()) {
268 sele2ToIndex_[istep].push_back(bond->getGlobalIndex());
270 sele2ToIndex_[istep].resize(index + 1);
271 sele2ToIndex_[istep][index] = bond->getGlobalIndex();
279 void TimeCorrFunc<T>::doCorrelate() {
280 painCave.isFatal = 0;
281 painCave.severity = OPENMD_INFO;
282 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
283 "Starting pre-correlate scan.");
287 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
288 "Calculating correlation function.");
292 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
293 "Doing post-correlation calculations.");
297 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
"Writing output.");
303 void TimeCorrFunc<T>::correlation() {
305 for (
unsigned int i = 0; i < nTimeBins_; ++i) {
306 histogram_[i] = zeroType;
310 progressBar_->clear();
311 RealType samples = 0.5 * (nFrames_ + 1) * nFrames_;
314 for (
int i = 0; i < nFrames_; ++i) {
315 RealType time1 = times_[i];
317 for (
int j = i; j < nFrames_; ++j) {
319 progressBar_->setStatus(visited, samples);
320 progressBar_->update();
326 RealType time2 = times_[j];
328 if (std::fabs((time2 - time1) - (j - i) * dtMean_) >
329 6 * dtSigma_ * (j - i)) {
330 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
331 "TimeCorrFunc::correlateBlocks: mean sampleTime (%f)\n"
332 "\tin %s does not match actual time-spacing between\n"
333 "\tconfigurations %d (t = %f) and %d (t = %f).\n",
334 dtMean_, dumpFilename_.c_str(), i, time1, j, time2);
335 if (allowTimeFuzz_) {
336 painCave.isFatal = 0;
337 painCave.severity = OPENMD_INFO;
339 painCave.isFatal = 1;
340 painCave.severity = OPENMD_ERROR;
345 int timeBin = int((time2 - time1) / dtMean_ + 0.5);
346 correlateFrames(i, j, timeBin);
358 void TimeCorrFunc<T>::correlateFrames(
int frame1,
int frame2,
int timeBin) {
362 std::vector<int>::iterator i1;
363 std::vector<int>::iterator i2;
367 if (doSystemProperties_) {
368 corrVal = calcCorrVal(frame1, frame2);
369 histogram_[timeBin] += corrVal;
373 s1 = sele1ToIndex_[frame1];
375 if (uniqueSelections_)
376 s2 = sele2ToIndex_[frame2];
378 s2 = sele1ToIndex_[frame2];
380 for (i1 = s1.begin(), i2 = s2.begin(); i1 != s1.end() && i2 != s2.end();
387 while (i1 != s1.end() && *i1 < *i2) {
391 while (i2 != s2.end() && *i2 < *i1) {
395 if (i1 == s1.end() || i2 == s2.end())
break;
397 corrVal = calcCorrVal(frame1, frame2, i1 - s1.begin(), i2 - s2.begin());
398 histogram_[timeBin] += corrVal;
405 void TimeCorrFunc<T>::postCorrelate() {
407 for (
unsigned int i = 0; i < nTimeBins_; ++i) {
409 histogram_[i] /= count_[i];
411 histogram_[i] = zeroType;
417 void TimeCorrFunc<T>::validateSelection(SelectionManager&) {}
420 void TimeCorrFunc<T>::writeCorrelate() {
421 ofstream ofs(outputFilename_.c_str());
426 ofs <<
"# " << getCorrFuncType() <<
"\n";
427 ofs <<
"# OpenMD " << r.getFullRevision() <<
"\n";
428 ofs <<
"# " << r.getBuildDate() <<
"\n";
429 ofs <<
"# selection script1: \"" << selectionScript1_;
430 ofs <<
"\"\tselection script2: \"" << selectionScript2_ <<
"\"\n";
431 if (!paramString_.empty())
432 ofs <<
"# parameters: " << paramString_ <<
"\n";
433 if (!labelString_.empty())
434 ofs <<
"#time\t" << labelString_ <<
"\n";
436 ofs <<
"#time\tcorrVal\n";
438 for (
unsigned int i = 0; i < nTimeBins_; ++i) {
439 ofs << times_[i] - times_[0] <<
"\t" << histogram_[i] <<
"\n";
443 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
444 "TimeCorrFunc::writeCorrelate Error: fail to open %s\n",
445 outputFilename_.c_str());
446 painCave.isFatal = 1;
455 void TimeCorrFunc<Vector3d>::writeCorrelate() {
456 ofstream ofs(outputFilename_.c_str());
461 ofs <<
"# " << getCorrFuncType() <<
"\n";
462 ofs <<
"# OpenMD " << r.getFullRevision() <<
"\n";
463 ofs <<
"# " << r.getBuildDate() <<
"\n";
464 ofs <<
"# selection script1: \"" << selectionScript1_;
465 ofs <<
"\"\tselection script2: \"" << selectionScript2_ <<
"\"\n";
466 if (!paramString_.empty())
467 ofs <<
"# parameters: " << paramString_ <<
"\n";
468 if (!labelString_.empty())
469 ofs <<
"#time\t" << labelString_ <<
"\n";
471 ofs <<
"#time\tcorrVal\n";
473 for (
unsigned int i = 0; i < nTimeBins_; ++i) {
474 ofs << times_[i] - times_[0] <<
"\t";
475 for (
int j = 0; j < 3; j++) {
476 ofs << histogram_[i](j) <<
'\t';
482 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
483 "TimeCorrFunc::writeCorrelate Error: fail to open %s\n",
484 outputFilename_.c_str());
485 painCave.isFatal = 1;
494 void TimeCorrFunc<Mat3x3d>::writeCorrelate() {
495 ofstream ofs(outputFilename_.c_str());
500 ofs <<
"# " << getCorrFuncType() <<
"\n";
501 ofs <<
"# OpenMD " << r.getFullRevision() <<
"\n";
502 ofs <<
"# " << r.getBuildDate() <<
"\n";
503 ofs <<
"# selection script1: \"" << selectionScript1_;
504 ofs <<
"\"\tselection script2: \"" << selectionScript2_ <<
"\"\n";
505 if (!paramString_.empty())
506 ofs <<
"# parameters: " << paramString_ <<
"\n";
507 if (!labelString_.empty())
508 ofs <<
"#time\t" << labelString_ <<
"\n";
510 ofs <<
"#time\tcorrVal\n";
512 for (
unsigned int i = 0; i < nTimeBins_; ++i) {
513 ofs << times_[i] - times_[0] <<
"\t";
514 for (
int j = 0; j < 3; j++) {
515 for (
int k = 0; k < 3; k++) {
516 ofs << histogram_[i](j, k) <<
'\t';
523 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
524 "TimeCorrFunc::writeCorrelate Error: fail to open %s\n",
525 outputFilename_.c_str());
526 painCave.isFatal = 1;
536 CrossCorrFunc<T>::CrossCorrFunc(SimInfo* info,
const std::string& filename,
537 const std::string& sele1,
538 const std::string& sele2) :
539 TimeCorrFunc<T>(info, filename, sele1, sele2) {
540 this->autoCorrFunc_ =
false;
544 AutoCorrFunc<T>::AutoCorrFunc(SimInfo* info,
const std::string& filename,
545 const std::string& sele1,
546 const std::string& sele2) :
547 TimeCorrFunc<T>(info, filename, sele1, sele2) {
548 this->autoCorrFunc_ =
true;
552 SystemACF<T>::SystemACF(SimInfo* info,
const std::string& filename,
553 const std::string& sele1,
const std::string& sele2) :
554 AutoCorrFunc<T>(info, filename, sele1, sele2) {
555 this->autoCorrFunc_ =
true;
556 this->doSystemProperties_ =
true;
557 this->doMolecularProperties_ =
false;
558 this->doObjectProperties_ =
false;
559 this->doBondProperties_ =
false;
563 SystemCCF<T>::SystemCCF(SimInfo* info,
const std::string& filename,
564 const std::string& sele1,
const std::string& sele2) :
565 CrossCorrFunc<T>(info, filename, sele1, sele2) {
566 this->autoCorrFunc_ =
false;
567 this->doSystemProperties_ =
true;
568 this->doMolecularProperties_ =
false;
569 this->doObjectProperties_ =
false;
570 this->doBondProperties_ =
false;
574 ObjectACF<T>::ObjectACF(SimInfo* info,
const std::string& filename,
575 const std::string& sele1,
const std::string& sele2) :
576 AutoCorrFunc<T>(info, filename, sele1, sele2) {
577 this->autoCorrFunc_ =
true;
578 this->doSystemProperties_ =
false;
579 this->doMolecularProperties_ =
false;
580 this->doObjectProperties_ =
true;
581 this->doBondProperties_ =
false;
585 ObjectCCF<T>::ObjectCCF(SimInfo* info,
const std::string& filename,
586 const std::string& sele1,
const std::string& sele2) :
587 CrossCorrFunc<T>(info, filename, sele1, sele2) {
588 this->autoCorrFunc_ =
false;
589 this->doSystemProperties_ =
false;
590 this->doMolecularProperties_ =
false;
591 this->doObjectProperties_ =
true;
592 this->doBondProperties_ =
false;
596 MoleculeACF<T>::MoleculeACF(SimInfo* info,
const std::string& filename,
597 const std::string& sele1,
598 const std::string& sele2) :
599 AutoCorrFunc<T>(info, filename, sele1, sele2) {
600 this->autoCorrFunc_ =
true;
601 this->doSystemProperties_ =
false;
602 this->doMolecularProperties_ =
true;
603 this->doObjectProperties_ =
false;
604 this->doBondProperties_ =
false;
608 MoleculeCCF<T>::MoleculeCCF(SimInfo* info,
const std::string& filename,
609 const std::string& sele1,
610 const std::string& sele2) :
611 CrossCorrFunc<T>(info, filename, sele1, sele2) {
612 this->autoCorrFunc_ =
false;
613 this->doSystemProperties_ =
false;
614 this->doMolecularProperties_ =
true;
615 this->doObjectProperties_ =
false;
616 this->doBondProperties_ =
false;
619 template class AutoCorrFunc<RealType>;
620 template class TimeCorrFunc<RealType>;
621 template class CrossCorrFunc<RealType>;
623 template class AutoCorrFunc<Vector3d>;
624 template class TimeCorrFunc<Vector3d>;
625 template class CrossCorrFunc<Vector3d>;
627 template class AutoCorrFunc<Mat3x3d>;
628 template class TimeCorrFunc<Mat3x3d>;
629 template class CrossCorrFunc<Mat3x3d>;
631 template class TimeCorrFunc<DynamicVector<RealType>>;
633 template class AutoCorrFunc<Vector<RealType, 4>>;
634 template class TimeCorrFunc<Vector<RealType, 4>>;
635 template class CrossCorrFunc<Vector<RealType, 4>>;
637 template class SystemACF<RealType>;
638 template class SystemACF<Vector3d>;
639 template class SystemACF<Mat3x3d>;
641 template class SystemCCF<RealType>;
642 template class SystemCCF<Vector3d>;
643 template class SystemCCF<Mat3x3d>;
645 template class ObjectACF<RealType>;
646 template class ObjectACF<Vector3d>;
647 template class ObjectACF<Mat3x3d>;
649 template class ObjectCCF<RealType>;
650 template class ObjectCCF<Vector3d>;
651 template class ObjectCCF<Mat3x3d>;
652 template class ObjectCCF<int>;
654 template class MoleculeACF<RealType>;
655 template class MoleculeACF<Vector3d>;
656 template class MoleculeACF<Mat3x3d>;
657 template class MoleculeACF<Vector<RealType, 4>>;
659 template class MoleculeCCF<RealType>;
660 template class MoleculeCCF<Vector3d>;
661 template class MoleculeCCF<Mat3x3d>;
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.