45#include "applications/dynamicProps/TimeCorrFunc.hpp"
51#include "utils/Revision.hpp"
52#include "utils/simError.h"
58 TimeCorrFunc<T>::TimeCorrFunc(SimInfo* info,
const string& filename,
59 const string& sele1,
const string& sele2) :
61 dumpFilename_(filename), seleMan1_(info_), seleMan2_(info_),
62 selectionScript1_(sele1), selectionScript2_(sele2), evaluator1_(info_),
63 evaluator2_(info_), autoCorrFunc_(false), doSystemProperties_(false),
64 doMolecularProperties_(false), doObjectProperties_(false),
65 doBondProperties_(false) {
66 reader_ =
new DumpReader(info_, dumpFilename_);
68 uniqueSelections_ = (sele1.compare(sele2) != 0) ?
true : false;
70 Globals* simParams = info_->getSimParams();
71 if (simParams->haveSampleTime()) {
72 deltaTime_ = simParams->getSampleTime();
74 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
75 "TimeCorrFunc Error: can not figure out deltaTime\n");
80 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
"Scanning for frames.");
82 painCave.severity = OPENMD_INFO;
85 nFrames_ = reader_->getNFrames();
86 nTimeBins_ = nFrames_;
89 histogram_.resize(nTimeBins_, zeroType);
90 count_.resize(nTimeBins_, 0);
92 times_.resize(nFrames_);
93 sele1ToIndex_.resize(nFrames_);
94 if (uniqueSelections_) { sele2ToIndex_.resize(nFrames_); }
96 progressBar_ = std::make_unique<ProgressBar>();
100 void TimeCorrFunc<T>::preCorrelate() {
101 evaluator1_.loadScriptString(selectionScript1_);
103 if (!evaluator1_.isDynamic()) {
104 seleMan1_.setSelectionSet(evaluator1_.evaluate());
105 validateSelection(seleMan1_);
108 if (uniqueSelections_) {
109 evaluator2_.loadScriptString(selectionScript2_);
110 if (!evaluator2_.isDynamic()) {
111 seleMan2_.setSelectionSet(evaluator2_.evaluate());
112 validateSelection(seleMan2_);
116 progressBar_->clear();
118 for (
int istep = 0; istep < nFrames_; istep++) {
119 reader_->readFrame(istep);
120 currentSnapshot_ = info_->getSnapshotManager()->getCurrentSnapshot();
121 times_[istep] = currentSnapshot_->getTime();
123 progressBar_->setStatus(istep + 1, nFrames_);
124 progressBar_->update();
131 void TimeCorrFunc<T>::computeFrame(
int istep) {
135 int imol1, imol2, isd1, isd2, ibond1, ibond2;
138 if (evaluator1_.isDynamic()) {
139 seleMan1_.setSelectionSet(evaluator1_.evaluate());
140 validateSelection(seleMan1_);
143 if (uniqueSelections_ && evaluator2_.isDynamic()) {
144 seleMan2_.setSelectionSet(evaluator2_.evaluate());
145 validateSelection(seleMan2_);
148 if (doSystemProperties_) {
149 computeProperty1(istep);
150 if (!autoCorrFunc_) computeProperty2(istep);
153 if (doMolecularProperties_) {
154 for (mol = seleMan1_.beginSelectedMolecule(imol1); mol != NULL;
155 mol = seleMan1_.nextSelectedMolecule(imol1)) {
156 index = computeProperty1(istep, mol);
158 if (index == sele1ToIndex_[istep].size()) {
159 sele1ToIndex_[istep].push_back(mol->getGlobalIndex());
161 sele1ToIndex_[istep].resize(index + 1);
162 sele1ToIndex_[istep][index] = mol->getGlobalIndex();
165 if (!uniqueSelections_) { index = computeProperty2(istep, mol); }
168 if (uniqueSelections_) {
169 for (mol = seleMan2_.beginSelectedMolecule(imol2); mol != NULL;
170 mol = seleMan2_.nextSelectedMolecule(imol2)) {
172 index = computeProperty1(istep, mol);
174 index = computeProperty2(istep, mol);
176 if (index == sele2ToIndex_[istep].size()) {
177 sele2ToIndex_[istep].push_back(mol->getGlobalIndex());
179 sele2ToIndex_[istep].resize(index + 1);
180 sele2ToIndex_[istep][index] = mol->getGlobalIndex();
186 if (doObjectProperties_) {
187 for (sd = seleMan1_.beginSelected(isd1); sd != NULL;
188 sd = seleMan1_.nextSelected(isd1)) {
189 index = computeProperty1(istep, sd);
191 if (index == sele1ToIndex_[istep].size()) {
192 sele1ToIndex_[istep].push_back(sd->getGlobalIndex());
194 sele1ToIndex_[istep].resize(index + 1);
195 sele1ToIndex_[istep][index] = sd->getGlobalIndex();
198 if (!uniqueSelections_) { index = computeProperty2(istep, sd); }
201 if (uniqueSelections_) {
202 for (sd = seleMan2_.beginSelected(isd2); sd != NULL;
203 sd = seleMan2_.nextSelected(isd2)) {
205 index = computeProperty1(istep, sd);
207 index = computeProperty2(istep, sd);
209 if (index == sele2ToIndex_[istep].size()) {
210 sele2ToIndex_[istep].push_back(sd->getGlobalIndex());
212 sele2ToIndex_[istep].resize(index + 1);
213 sele2ToIndex_[istep][index] = sd->getGlobalIndex();
219 if (doBondProperties_) {
220 for (bond = seleMan1_.beginSelectedBond(ibond1); bond != NULL;
221 bond = seleMan1_.nextSelectedBond(ibond1)) {
222 index = computeProperty1(istep, bond);
224 if (index == sele1ToIndex_[istep].size()) {
225 sele1ToIndex_[istep].push_back(bond->getGlobalIndex());
227 sele1ToIndex_[istep].resize(index + 1);
228 sele1ToIndex_[istep][index] = bond->getGlobalIndex();
231 if (!uniqueSelections_) { index = computeProperty2(istep, bond); }
234 if (uniqueSelections_) {
235 for (bond = seleMan2_.beginSelectedBond(ibond2); bond != NULL;
236 bond = seleMan2_.nextSelectedBond(ibond2)) {
238 index = computeProperty1(istep, bond);
240 index = computeProperty2(istep, bond);
242 if (index == sele2ToIndex_[istep].size()) {
243 sele2ToIndex_[istep].push_back(bond->getGlobalIndex());
245 sele2ToIndex_[istep].resize(index + 1);
246 sele2ToIndex_[istep][index] = bond->getGlobalIndex();
254 void TimeCorrFunc<T>::doCorrelate() {
255 painCave.isFatal = 0;
256 painCave.severity = OPENMD_INFO;
257 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
258 "Starting pre-correlate scan.");
262 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
263 "Calculating correlation function.");
267 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
268 "Doing post-correlation calculations.");
272 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
"Writing output.");
278 void TimeCorrFunc<T>::correlation() {
280 for (
unsigned int i = 0; i < nTimeBins_; ++i) {
281 histogram_[i] = zeroType;
285 progressBar_->clear();
286 RealType samples = 0.5 * (nFrames_ + 1) * nFrames_;
289 for (
int i = 0; i < nFrames_; ++i) {
290 RealType time1 = times_[i];
292 for (
int j = i; j < nFrames_; ++j) {
294 progressBar_->setStatus(visited, samples);
295 progressBar_->update();
301 RealType time2 = times_[j];
303 if (fabs((time2 - time1) - (j - i) * deltaTime_) > 1.0e-4) {
304 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
305 "TimeCorrFunc::correlateBlocks Error: sampleTime (%f)\n"
306 "\tin %s does not match actual time-spacing between\n"
307 "\tconfigurations %d (t = %f) and %d (t = %f).\n",
308 deltaTime_, dumpFilename_.c_str(), i, time1, j, time2);
309 painCave.isFatal = 1;
313 int timeBin = int((time2 - time1) / deltaTime_ + 0.5);
314 correlateFrames(i, j, timeBin);
326 void TimeCorrFunc<T>::correlateFrames(
int frame1,
int frame2,
int timeBin) {
330 std::vector<int>::iterator i1;
331 std::vector<int>::iterator i2;
335 if (doSystemProperties_) {
336 corrVal = calcCorrVal(frame1, frame2);
337 histogram_[timeBin] += corrVal;
341 s1 = sele1ToIndex_[frame1];
343 if (uniqueSelections_)
344 s2 = sele2ToIndex_[frame2];
346 s2 = sele1ToIndex_[frame2];
348 for (i1 = s1.begin(), i2 = s2.begin(); i1 != s1.end() && i2 != s2.end();
355 while (i1 != s1.end() && *i1 < *i2) {
359 while (i2 != s2.end() && *i2 < *i1) {
363 if (i1 == s1.end() || i2 == s2.end())
break;
365 corrVal = calcCorrVal(frame1, frame2, i1 - s1.begin(), i2 - s2.begin());
366 histogram_[timeBin] += corrVal;
373 void TimeCorrFunc<T>::postCorrelate() {
375 for (
unsigned int i = 0; i < nTimeBins_; ++i) {
377 histogram_[i] /= count_[i];
379 histogram_[i] = zeroType;
385 void TimeCorrFunc<T>::validateSelection(SelectionManager&) {}
388 void TimeCorrFunc<T>::writeCorrelate() {
389 ofstream ofs(outputFilename_.c_str());
394 ofs <<
"# " << getCorrFuncType() <<
"\n";
395 ofs <<
"# OpenMD " << r.getFullRevision() <<
"\n";
396 ofs <<
"# " << r.getBuildDate() <<
"\n";
397 ofs <<
"# selection script1: \"" << selectionScript1_;
398 ofs <<
"\"\tselection script2: \"" << selectionScript2_ <<
"\"\n";
399 if (!paramString_.empty())
400 ofs <<
"# parameters: " << paramString_ <<
"\n";
401 if (!labelString_.empty())
402 ofs <<
"#time\t" << labelString_ <<
"\n";
404 ofs <<
"#time\tcorrVal\n";
406 for (
unsigned int i = 0; i < nTimeBins_; ++i) {
407 ofs << times_[i] - times_[0] <<
"\t" << histogram_[i] <<
"\n";
411 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
412 "TimeCorrFunc::writeCorrelate Error: fail to open %s\n",
413 outputFilename_.c_str());
414 painCave.isFatal = 1;
423 void TimeCorrFunc<Vector3d>::writeCorrelate() {
424 ofstream ofs(outputFilename_.c_str());
429 ofs <<
"# " << getCorrFuncType() <<
"\n";
430 ofs <<
"# OpenMD " << r.getFullRevision() <<
"\n";
431 ofs <<
"# " << r.getBuildDate() <<
"\n";
432 ofs <<
"# selection script1: \"" << selectionScript1_;
433 ofs <<
"\"\tselection script2: \"" << selectionScript2_ <<
"\"\n";
434 if (!paramString_.empty())
435 ofs <<
"# parameters: " << paramString_ <<
"\n";
436 if (!labelString_.empty())
437 ofs <<
"#time\t" << labelString_ <<
"\n";
439 ofs <<
"#time\tcorrVal\n";
441 for (
unsigned int i = 0; i < nTimeBins_; ++i) {
442 ofs << times_[i] - times_[0] <<
"\t";
443 for (
int j = 0; j < 3; j++) {
444 ofs << histogram_[i](j) <<
'\t';
450 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
451 "TimeCorrFunc::writeCorrelate Error: fail to open %s\n",
452 outputFilename_.c_str());
453 painCave.isFatal = 1;
462 void TimeCorrFunc<Mat3x3d>::writeCorrelate() {
463 ofstream ofs(outputFilename_.c_str());
468 ofs <<
"# " << getCorrFuncType() <<
"\n";
469 ofs <<
"# OpenMD " << r.getFullRevision() <<
"\n";
470 ofs <<
"# " << r.getBuildDate() <<
"\n";
471 ofs <<
"# selection script1: \"" << selectionScript1_;
472 ofs <<
"\"\tselection script2: \"" << selectionScript2_ <<
"\"\n";
473 if (!paramString_.empty())
474 ofs <<
"# parameters: " << paramString_ <<
"\n";
475 if (!labelString_.empty())
476 ofs <<
"#time\t" << labelString_ <<
"\n";
478 ofs <<
"#time\tcorrVal\n";
480 for (
unsigned int i = 0; i < nTimeBins_; ++i) {
481 ofs << times_[i] - times_[0] <<
"\t";
482 for (
int j = 0; j < 3; j++) {
483 for (
int k = 0; k < 3; k++) {
484 ofs << histogram_[i](j, k) <<
'\t';
491 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
492 "TimeCorrFunc::writeCorrelate Error: fail to open %s\n",
493 outputFilename_.c_str());
494 painCave.isFatal = 1;
504 CrossCorrFunc<T>::CrossCorrFunc(SimInfo* info,
const std::string& filename,
505 const std::string& sele1,
506 const std::string& sele2) :
507 TimeCorrFunc<T>(info, filename, sele1, sele2) {
508 this->autoCorrFunc_ =
false;
512 AutoCorrFunc<T>::AutoCorrFunc(SimInfo* info,
const std::string& filename,
513 const std::string& sele1,
514 const std::string& sele2) :
515 TimeCorrFunc<T>(info, filename, sele1, sele2) {
516 this->autoCorrFunc_ =
true;
520 SystemACF<T>::SystemACF(SimInfo* info,
const std::string& filename,
521 const std::string& sele1,
const std::string& sele2) :
522 AutoCorrFunc<T>(info, filename, sele1, sele2) {
523 this->autoCorrFunc_ =
true;
524 this->doSystemProperties_ =
true;
525 this->doMolecularProperties_ =
false;
526 this->doObjectProperties_ =
false;
527 this->doBondProperties_ =
false;
531 SystemCCF<T>::SystemCCF(SimInfo* info,
const std::string& filename,
532 const std::string& sele1,
const std::string& sele2) :
533 CrossCorrFunc<T>(info, filename, sele1, sele2) {
534 this->autoCorrFunc_ =
false;
535 this->doSystemProperties_ =
true;
536 this->doMolecularProperties_ =
false;
537 this->doObjectProperties_ =
false;
538 this->doBondProperties_ =
false;
542 ObjectACF<T>::ObjectACF(SimInfo* info,
const std::string& filename,
543 const std::string& sele1,
const std::string& sele2) :
544 AutoCorrFunc<T>(info, filename, sele1, sele2) {
545 this->autoCorrFunc_ =
true;
546 this->doSystemProperties_ =
false;
547 this->doMolecularProperties_ =
false;
548 this->doObjectProperties_ =
true;
549 this->doBondProperties_ =
false;
553 ObjectCCF<T>::ObjectCCF(SimInfo* info,
const std::string& filename,
554 const std::string& sele1,
const std::string& sele2) :
555 CrossCorrFunc<T>(info, filename, sele1, sele2) {
556 this->autoCorrFunc_ =
false;
557 this->doSystemProperties_ =
false;
558 this->doMolecularProperties_ =
false;
559 this->doObjectProperties_ =
true;
560 this->doBondProperties_ =
false;
564 MoleculeACF<T>::MoleculeACF(SimInfo* info,
const std::string& filename,
565 const std::string& sele1,
566 const std::string& sele2) :
567 AutoCorrFunc<T>(info, filename, sele1, sele2) {
568 this->autoCorrFunc_ =
true;
569 this->doSystemProperties_ =
false;
570 this->doMolecularProperties_ =
true;
571 this->doObjectProperties_ =
false;
572 this->doBondProperties_ =
false;
576 MoleculeCCF<T>::MoleculeCCF(SimInfo* info,
const std::string& filename,
577 const std::string& sele1,
578 const std::string& sele2) :
579 CrossCorrFunc<T>(info, filename, sele1, sele2) {
580 this->autoCorrFunc_ =
false;
581 this->doSystemProperties_ =
false;
582 this->doMolecularProperties_ =
true;
583 this->doObjectProperties_ =
false;
584 this->doBondProperties_ =
false;
587 template class AutoCorrFunc<RealType>;
588 template class TimeCorrFunc<RealType>;
589 template class CrossCorrFunc<RealType>;
591 template class AutoCorrFunc<Vector3d>;
592 template class TimeCorrFunc<Vector3d>;
593 template class CrossCorrFunc<Vector3d>;
595 template class AutoCorrFunc<Mat3x3d>;
596 template class TimeCorrFunc<Mat3x3d>;
597 template class CrossCorrFunc<Mat3x3d>;
599 template class TimeCorrFunc<DynamicVector<RealType>>;
601 template class AutoCorrFunc<Vector<RealType, 4>>;
602 template class TimeCorrFunc<Vector<RealType, 4>>;
603 template class CrossCorrFunc<Vector<RealType, 4>>;
605 template class SystemACF<RealType>;
606 template class SystemACF<Vector3d>;
607 template class SystemACF<Mat3x3d>;
609 template class SystemCCF<RealType>;
610 template class SystemCCF<Vector3d>;
611 template class SystemCCF<Mat3x3d>;
613 template class ObjectACF<RealType>;
614 template class ObjectACF<Vector3d>;
615 template class ObjectACF<Mat3x3d>;
617 template class ObjectCCF<RealType>;
618 template class ObjectCCF<Vector3d>;
619 template class ObjectCCF<Mat3x3d>;
620 template class ObjectCCF<int>;
622 template class MoleculeACF<RealType>;
623 template class MoleculeACF<Vector3d>;
624 template class MoleculeACF<Mat3x3d>;
625 template class MoleculeACF<Vector<RealType, 4>>;
627 template class MoleculeCCF<RealType>;
628 template class MoleculeCCF<Vector3d>;
629 template class MoleculeCCF<Mat3x3d>;
This basic Periodic Table class was originally taken from the data.cpp file in OpenBabel.