Loading...
Searching...
No Matches
eonc::pathintegral::RingPolymer Class Reference

Ring-polymer NVT step. More...

#include <PathIntegral.h>

Classes

struct  ModeGle

Public Member Functions

 RingPolymer (long nAtoms, std::vector< double > masses, std::vector< int > atomicNumbers, std::vector< char > free, Options opt)
void setAllBeads (const double *q)
void setBeads (const std::vector< VectorXd > &beads)
 One position vector of length 3 * nAtoms per bead.
const std::vector< VectorXd > & beads () const
const std::vector< VectorXd > & momenta () const
void setMomenta (const std::vector< VectorXd > &momenta)
 One momentum vector of length 3 * nAtoms per bead; fixed coordinates are zeroed.
void thermalMomenta ()
void setHyperplane (const VectorXd &normal, const VectorXd &origin)
 Hold n · (q_centroid - origin) = 0.
void step (Potential &pot, const double *box, bool record)
void nveStep (Potential &pot, const double *box)
 Thermostat-free RPMD step: velocity Verlet with the free ring propagated exactly in normal modes.
double kineticCv () const
double meanForce () const
long batches () const
VectorXd centroid () const
VectorXd centroidVelocity () const
void resetAverages ()
Sample sample (Potential &pot, const double *box, long equilibration, long production)

Private Member Functions

void forces (Potential &pot, const double *box)
void thermostat (double h)
void kick (double h, bool dropParallel)
void propagate (double h)
void projectPosition ()
void projectMomentum ()
void toNormal (const std::vector< VectorXd > &src, std::vector< VectorXd > &dst) const
void fromNormal (const std::vector< VectorXd > &src, std::vector< VectorXd > &dst) const
double gauss ()
void initGle ()

Private Attributes

Options opt_
long nAtoms_ {0}
long nDof_ {0}
long nBeads_ {0}
long nFree_ {0}
std::vector< double > mass_
std::vector< int > atomicNumbers_
std::vector< char > free_
std::vector< long > freeIndex_
MatrixXd modes_
VectorXd omegaK_
std::vector< VectorXd > q_
std::vector< VectorXd > p_
std::vector< VectorXd > f_
std::vector< VectorXd > qnm_
std::vector< VectorXd > pnm_
VectorXd planeNormal_
VectorXd planeOrigin_
bool constrain_ {false}
bool haveForces_ {false}
std::vector< ModeGle > gle_
std::vector< const double * > posPtr_
std::vector< const int * > nrsPtr_
std::vector< double * > frcPtr_
std::vector< const double * > boxPtr_
std::vector< double > energyBuf_
std::vector< double > varianceBuf_
std::vector< long > owners_
VectorXd centroid_
std::vector< double > cosHalf_
std::vector< double > sinHalf_
std::vector< double > cosFull_
std::vector< double > sinFull_
double halfDt_ {0.0}
std::uint64_t rng_
long batches_ {0}
long recorded_ {0}
double kineticSum_ {0.0}
double forceSum_ {0.0}

Detailed Description

Ring-polymer NVT step.

Physical forces on every bead are one forceBatch call, so a calculator group carries the beads together. The classical velocity Verlet step is not used.

Definition at line 92 of file PathIntegral.h.

Constructor & Destructor Documentation

◆ RingPolymer()

eonc::pathintegral::RingPolymer::RingPolymer ( long nAtoms,
std::vector< double > masses,
std::vector< int > atomicNumbers,
std::vector< char > free,
Options opt )

Definition at line 391 of file PathIntegral.cpp.

394 : opt_(std::move(opt)),
395 nAtoms_(nAtoms),
396 nDof_(3 * nAtoms),
397 nBeads_(opt_.beads),
398 atomicNumbers_(std::move(atomicNumbers)),
399 free_(std::move(free)),
400 rng_(opt_.seed == 0 ? 1 : opt_.seed) {
401 if (nAtoms_ < 1 || nBeads_ < 1) {
402 throw std::invalid_argument("path integral needs atoms and beads");
403 }
404 if (static_cast<long>(masses.size()) != nAtoms_ ||
405 static_cast<long>(atomicNumbers_.size()) != nAtoms_ ||
406 static_cast<long>(free_.size()) != nDof_) {
407 throw std::invalid_argument("path integral mass, number or mask size");
408 }
409 if (!(opt_.temperature > 0.0) || !(opt_.kB > 0.0) || !(opt_.hbar > 0.0) ||
410 !(opt_.dt > 0.0) || !(opt_.pileTau > 0.0) || !(opt_.pileScale > 0.0)) {
411 throw std::invalid_argument(
412 "path integral temperature, timestep and damping must be positive");
413 }
414 if (opt_.springs == Springs::Eco && opt_.thermostat == Thermostat::Piglet) {
415 throw std::invalid_argument(
416 "economised springs cannot be combined with a normal-mode GLE");
417 }
418 mass_.assign(static_cast<size_t>(nDof_), 0.0);
419 nFree_ = 0;
420 for (long i = 0; i < nAtoms_; ++i) {
421 if (!(masses[static_cast<size_t>(i)] > 0.0)) {
422 throw std::invalid_argument("path integral atom has no mass");
423 }
424 for (int axis = 0; axis < 3; ++axis) {
425 const long a = 3 * i + axis;
426 mass_[static_cast<size_t>(a)] = masses[static_cast<size_t>(i)];
427 if (free_[static_cast<size_t>(a)]) {
428 freeIndex_.push_back(a);
429 ++nFree_;
430 }
431 }
432 }
433 if (nFree_ < 1) {
434 throw std::invalid_argument("path integral has no free coordinate");
435 }
437 const double omegan =
438 static_cast<double>(nBeads_) * opt_.kB * opt_.temperature / opt_.hbar;
439 if (opt_.springs == Springs::Eco) {
440 const double xmax =
441 opt_.ecoOmegaMax * opt_.hbar / (opt_.kB * opt_.temperature);
442 omegaK_ = omegan * ecoEigenvalues(nBeads_, xmax);
443 } else {
445 }
446 halfDt_ = 0.5 * opt_.dt;
447 cosHalf_.resize(static_cast<size_t>(nBeads_));
448 sinHalf_.resize(static_cast<size_t>(nBeads_));
449 cosFull_.resize(static_cast<size_t>(nBeads_));
450 sinFull_.resize(static_cast<size_t>(nBeads_));
451 for (long k = 0; k < nBeads_; ++k) {
452 const double w = omegaK_[k];
453 cosHalf_[static_cast<size_t>(k)] = std::cos(w * halfDt_);
454 sinHalf_[static_cast<size_t>(k)] = std::sin(w * halfDt_);
455 cosFull_[static_cast<size_t>(k)] = std::cos(w * opt_.dt);
456 sinFull_[static_cast<size_t>(k)] = std::sin(w * opt_.dt);
457 }
458 q_.assign(static_cast<size_t>(nBeads_), VectorXd::Zero(nDof_));
459 p_.assign(static_cast<size_t>(nBeads_), VectorXd::Zero(nDof_));
460 f_.assign(static_cast<size_t>(nBeads_), VectorXd::Zero(nDof_));
461 qnm_.assign(static_cast<size_t>(nBeads_), VectorXd::Zero(nDof_));
462 pnm_.assign(static_cast<size_t>(nBeads_), VectorXd::Zero(nDof_));
463 if (opt_.thermostat == Thermostat::Piglet) {
464 initGle();
465 }
467}
std::vector< int > atomicNumbers_
std::vector< long > freeIndex_
std::vector< VectorXd > pnm_
std::vector< double > mass_
std::vector< VectorXd > f_
std::vector< double > cosFull_
std::vector< VectorXd > p_
std::vector< VectorXd > q_
std::vector< double > sinFull_
std::vector< double > sinHalf_
std::vector< VectorXd > qnm_
std::vector< double > cosHalf_
MatrixXd normalModeMatrix(long nBeads)
Orthogonal bead-to-normal-mode matrix. Row k is mode k.
VectorXd trotterEigenvalues(long nBeads)
Dimensionless free-ring eigenvalues, mode 0 equal to 0.
VectorXd ecoEigenvalues(long nBeads, double xmax)

Member Function Documentation

◆ batches()

long eonc::pathintegral::RingPolymer::batches ( ) const
inlinenodiscard

Definition at line 120 of file PathIntegral.h.

◆ beads()

const std::vector< VectorXd > & eonc::pathintegral::RingPolymer::beads ( ) const
inlinenodiscard

Definition at line 102 of file PathIntegral.h.

102{ return q_; }

◆ centroid()

VectorXd eonc::pathintegral::RingPolymer::centroid ( ) const
nodiscard

Definition at line 695 of file PathIntegral.cpp.

695 {
696 VectorXd c = VectorXd::Zero(nDof_);
697 for (long bead = 0; bead < nBeads_; ++bead) {
698 c += q_[static_cast<size_t>(bead)];
699 }
700 c /= static_cast<double>(nBeads_);
701 return c;
702}

◆ centroidVelocity()

VectorXd eonc::pathintegral::RingPolymer::centroidVelocity ( ) const
nodiscard

Definition at line 704 of file PathIntegral.cpp.

704 {
705 std::vector<VectorXd> pnm(static_cast<size_t>(nBeads_),
706 VectorXd::Zero(nDof_));
707 toNormal(p_, pnm);
708 VectorXd v = VectorXd::Zero(nDof_);
709 const double scale = std::sqrt(static_cast<double>(nBeads_));
710 for (long a = 0; a < nDof_; ++a) {
711 const double m = mass_[static_cast<size_t>(a)];
712 if (m > 0.0) {
713 v[a] = pnm[0][a] / (scale * m);
714 }
715 }
716 return v;
717}
void toNormal(const std::vector< VectorXd > &src, std::vector< VectorXd > &dst) const

◆ forces()

void eonc::pathintegral::RingPolymer::forces ( Potential & pot,
const double * box )
private

Definition at line 658 of file PathIntegral.cpp.

658 {
659 double zeroBox[9] = {};
660 const double *cell = box != nullptr ? box : zeroBox;
661 const auto n = static_cast<size_t>(nBeads_);
662 posPtr_.resize(n);
663 nrsPtr_.resize(n);
664 frcPtr_.resize(n);
665 boxPtr_.assign(n, cell);
666 energyBuf_.assign(n, 0.0);
667 varianceBuf_.assign(n, 0.0);
668 for (long bead = 0; bead < nBeads_; ++bead) {
669 posPtr_[static_cast<size_t>(bead)] = q_[static_cast<size_t>(bead)].data();
670 nrsPtr_[static_cast<size_t>(bead)] = atomicNumbers_.data();
671 frcPtr_[static_cast<size_t>(bead)] = f_[static_cast<size_t>(bead)].data();
672 }
673 // The bead index is each system's identity across steps: a potential
674 // that spreads the ring over calculators keeps a bead on one calculator,
675 // and one that stores per-system state (cpmdc orbitals) keeps it with
676 // the bead.
677 if (owners_.size() != n) {
678 owners_.resize(n);
679 std::iota(owners_.begin(), owners_.end(), 0L);
680 }
681 pot.forceBatchOwned(nBeads_, nAtoms_, posPtr_.data(), nrsPtr_.data(),
682 frcPtr_.data(), energyBuf_.data(), varianceBuf_.data(),
683 boxPtr_.data(), owners_.data());
684 ++batches_;
685 for (long bead = 0; bead < nBeads_; ++bead) {
686 for (long a = 0; a < nDof_; ++a) {
687 if (!free_[static_cast<size_t>(a)]) {
688 f_[static_cast<size_t>(bead)][a] = 0.0;
689 }
690 }
691 }
692 haveForces_ = true;
693}
std::vector< double > varianceBuf_
std::vector< const int * > nrsPtr_
std::vector< double * > frcPtr_
std::vector< const double * > posPtr_
std::vector< double > energyBuf_
std::vector< const double * > boxPtr_

◆ fromNormal()

void eonc::pathintegral::RingPolymer::fromNormal ( const std::vector< VectorXd > & src,
std::vector< VectorXd > & dst ) const
private

Definition at line 643 of file PathIntegral.cpp.

644 {
645 const double *mode = modes_.data();
646 const long n = nBeads_;
647 for (long a : freeIndex_) {
648 for (long j = 0; j < n; ++j) {
649 double sum = 0.0;
650 for (long k = 0; k < n; ++k) {
651 sum += src[static_cast<size_t>(k)][a] * mode[k * n + j];
652 }
653 dst[static_cast<size_t>(j)][a] = sum;
654 }
655 }
656}
double sum(const double *x, std::size_t n)
Round-to-nearest-even value of the exact sum of x[i].

◆ gauss()

double eonc::pathintegral::RingPolymer::gauss ( )
private

Definition at line 572 of file PathIntegral.cpp.

572 {
573 rng_ = rng_ * 6364136223846793005ULL + 1ULL;
574 const double u1 =
575 std::max((rng_ >> 11) * (1.0 / 9007199254740992.0), 1.0e-16);
576 rng_ = rng_ * 6364136223846793005ULL + 1ULL;
577 const double u2 = (rng_ >> 11) * (1.0 / 9007199254740992.0);
578 return std::sqrt(-2.0 * std::log(u1)) * std::cos(2.0 * kPi * u2);
579}

◆ initGle()

void eonc::pathintegral::RingPolymer::initGle ( )
private

Definition at line 469 of file PathIntegral.cpp.

469 {
470 if (nBeads_ == 1 && opt_.gleFile.empty()) {
471 gle_.resize(1);
472 return;
473 }
474 if (opt_.gleFile.empty()) {
475 throw std::invalid_argument("normal-mode GLE needs a matrix file");
476 }
477 const GleFile file = readGle(opt_.gleFile);
478 const long first = file.nModes == nBeads_ ? 0 : 1;
479 if (file.nModes != nBeads_ && file.nModes != nBeads_ - 1) {
480 throw std::invalid_argument(
481 "GLE matrix count must be the bead count or one less");
482 }
483 const double h = 0.5 * opt_.dt;
484 gle_.resize(static_cast<size_t>(nBeads_));
485 for (long k = first; k < nBeads_; ++k) {
486 const long src = k - first;
487 ModeGle mode;
488 mode.drift = file.drift[static_cast<size_t>(src)];
489 mode.covariance = file.covariance[static_cast<size_t>(src)];
490 mode.propagate = (-mode.drift * h).exp();
491 const MatrixXd cov =
492 opt_.kB * (mode.covariance - mode.propagate * mode.covariance *
493 mode.propagate.transpose());
494 mode.noise = factorCovariance(cov);
495 mode.extended = MatrixXd::Zero(file.dim, nFree_);
496 mode.draw = MatrixXd::Zero(file.dim, nFree_);
497 mode.acc = MatrixXd::Zero(file.dim, nFree_);
498 gle_[static_cast<size_t>(k)] = std::move(mode);
499 }
500}
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic, eOnStorageOrder > MatrixXd
Definition Eigen.h:33
std::vector< ModeGle > gle_

◆ kick()

void eonc::pathintegral::RingPolymer::kick ( double h,
bool dropParallel )
private

Definition at line 801 of file PathIntegral.cpp.

801 {
802 if (!(dropParallel && constrain_)) {
803 for (long bead = 0; bead < nBeads_; ++bead) {
804 for (long a : freeIndex_) {
805 p_[static_cast<size_t>(bead)][a] +=
806 f_[static_cast<size_t>(bead)][a] * h;
807 }
808 }
809 return;
810 }
811 VectorXd removal = VectorXd::Zero(nDof_);
812 if (dropParallel && constrain_) {
813 VectorXd fc = VectorXd::Zero(nDof_);
814 for (long bead = 0; bead < nBeads_; ++bead) {
815 fc += f_[static_cast<size_t>(bead)];
816 }
817 fc /= static_cast<double>(nBeads_);
818 double along = 0.0;
819 for (long a : freeIndex_) {
820 along += planeNormal_[a] * fc[a];
821 }
822 removal = along * planeNormal_;
823 }
824 for (long bead = 0; bead < nBeads_; ++bead) {
825 for (long a : freeIndex_) {
826 p_[static_cast<size_t>(bead)][a] +=
827 (f_[static_cast<size_t>(bead)][a] - removal[a]) * h;
828 }
829 }
830}

◆ kineticCv()

double eonc::pathintegral::RingPolymer::kineticCv ( ) const
nodiscard

Definition at line 719 of file PathIntegral.cpp.

719 {
720 double k = 0.5 * static_cast<double>(nFree_) * opt_.kB * opt_.temperature;
721 if (!haveForces_) {
722 return k;
723 }
724 if (centroid_.size() != nDof_) {
725 centroid_.resize(nDof_);
726 }
727 centroid_.setZero();
728 for (long bead = 0; bead < nBeads_; ++bead) {
729 centroid_ += q_[static_cast<size_t>(bead)];
730 }
731 centroid_ /= static_cast<double>(nBeads_);
732 double virial = 0.0;
733 for (long bead = 0; bead < nBeads_; ++bead) {
734 for (long a : freeIndex_) {
735 virial += (q_[static_cast<size_t>(bead)][a] - centroid_[a]) *
736 f_[static_cast<size_t>(bead)][a];
737 }
738 }
739 k += -0.5 / static_cast<double>(nBeads_) * virial;
740 return k;
741}

◆ meanForce()

double eonc::pathintegral::RingPolymer::meanForce ( ) const
nodiscard

Definition at line 743 of file PathIntegral.cpp.

743 {
744 if (!constrain_ || recorded_ < 1) {
745 return 0.0;
746 }
747 return forceSum_ / static_cast<double>(recorded_);
748}

◆ momenta()

const std::vector< VectorXd > & eonc::pathintegral::RingPolymer::momenta ( ) const
inlinenodiscard

Definition at line 103 of file PathIntegral.h.

103{ return p_; }

◆ nveStep()

void eonc::pathintegral::RingPolymer::nveStep ( Potential & pot,
const double * box )

Thermostat-free RPMD step: velocity Verlet with the free ring propagated exactly in normal modes.

Refused while a hyperplane is set.

Definition at line 961 of file PathIntegral.cpp.

961 {
962 if (constrain_) {
963 throw std::logic_error("path integral NVE step with a hyperplane set");
964 }
965 const double half = 0.5 * opt_.dt;
966 if (!haveForces_) {
967 forces(pot, box);
968 }
969 kick(half, false);
970 propagate(opt_.dt);
971 forces(pot, box);
972 kick(half, false);
973}
void forces(Potential &pot, const double *box)
void kick(double h, bool dropParallel)

◆ projectMomentum()

void eonc::pathintegral::RingPolymer::projectMomentum ( )
private

Definition at line 879 of file PathIntegral.cpp.

879 {
880 if (!constrain_) {
881 return;
882 }
883 VectorXd sum = VectorXd::Zero(nDof_);
884 for (long bead = 0; bead < nBeads_; ++bead) {
885 sum += p_[static_cast<size_t>(bead)];
886 }
887 // p_0 = sum / sqrt(P); remove lambda n from p_0.
888 const double scale = std::sqrt(static_cast<double>(nBeads_));
889 double num = 0.0;
890 double den = 0.0;
891 for (long a : freeIndex_) {
892 const double m = mass_[static_cast<size_t>(a)];
893 num += planeNormal_[a] * (sum[a] / scale) / m;
894 den += planeNormal_[a] * planeNormal_[a] / m;
895 }
896 if (den > 0.0) {
897 const double shift = num / den / scale;
898 for (long bead = 0; bead < nBeads_; ++bead) {
899 for (long a : freeIndex_) {
900 p_[static_cast<size_t>(bead)][a] -= shift * planeNormal_[a];
901 }
902 }
903 if (opt_.thermostat == Thermostat::Piglet && !gle_.empty()) {
904 auto &state = gle_[0].extended;
905 // The spatial constraint projector acts on every auxiliary row.
906 // It commutes with the OU matrix, which acts on auxiliary indices.
907 for (long row = 0; row < state.rows(); ++row) {
908 double along = 0.0;
909 for (long col = 0; col < nFree_; ++col) {
910 const long a = freeIndex_[static_cast<size_t>(col)];
911 along += state(row, col) * planeNormal_[a] /
912 std::sqrt(mass_[static_cast<size_t>(a)]);
913 }
914 for (long col = 0; col < nFree_; ++col) {
915 const long a = freeIndex_[static_cast<size_t>(col)];
916 state(row, col) -= along / den * planeNormal_[a] /
917 std::sqrt(mass_[static_cast<size_t>(a)]);
918 }
919 }
920 }
921 }
922}

◆ projectPosition()

void eonc::pathintegral::RingPolymer::projectPosition ( )
private

Definition at line 863 of file PathIntegral.cpp.

863 {
864 if (!constrain_) {
865 return;
866 }
867 const VectorXd c = centroid();
868 double sigma = 0.0;
869 for (long a : freeIndex_) {
870 sigma += planeNormal_[a] * (c[a] - planeOrigin_[a]);
871 }
872 for (long bead = 0; bead < nBeads_; ++bead) {
873 for (long a : freeIndex_) {
874 q_[static_cast<size_t>(bead)][a] -= sigma * planeNormal_[a];
875 }
876 }
877}

◆ propagate()

void eonc::pathintegral::RingPolymer::propagate ( double h)
private

Definition at line 832 of file PathIntegral.cpp.

832 {
833 toNormal(q_, qnm_);
834 toNormal(p_, pnm_);
835 const double *cosH = nullptr;
836 const double *sinH = nullptr;
837 if (h == halfDt_) {
838 cosH = cosHalf_.data();
839 sinH = sinHalf_.data();
840 } else if (h == opt_.dt) {
841 cosH = cosFull_.data();
842 sinH = sinFull_.data();
843 }
844 for (long a : freeIndex_) {
845 const double m = mass_[static_cast<size_t>(a)];
846 qnm_[0][a] += pnm_[0][a] / m * h;
847 for (long k = 1; k < nBeads_; ++k) {
848 const double omega = omegaK_[k];
849 const double c = cosH != nullptr ? cosH[k] : std::cos(omega * h);
850 const double s = sinH != nullptr ? sinH[k] : std::sin(omega * h);
851 const double pk = pnm_[static_cast<size_t>(k)][a];
852 const double qk = qnm_[static_cast<size_t>(k)][a];
853 pnm_[static_cast<size_t>(k)][a] = c * pk - m * omega * s * qk;
854 qnm_[static_cast<size_t>(k)][a] = c * qk + s * pk / (omega * m);
855 }
856 }
859}
void fromNormal(const std::vector< VectorXd > &src, std::vector< VectorXd > &dst) const

◆ resetAverages()

void eonc::pathintegral::RingPolymer::resetAverages ( )

Definition at line 750 of file PathIntegral.cpp.

750 {
751 recorded_ = 0;
752 kineticSum_ = 0.0;
753 forceSum_ = 0.0;
754}

◆ sample()

Sample eonc::pathintegral::RingPolymer::sample ( Potential & pot,
const double * box,
long equilibration,
long production )

Definition at line 975 of file PathIntegral.cpp.

976 {
977 if (equilibration < 0 || production < 1) {
978 throw std::invalid_argument("path integral sample length");
979 }
980 for (long step = 0; step < equilibration; ++step) {
981 this->step(pot, box, false);
982 }
984 const long batchesBefore = batches_;
985 for (long step = 0; step < production; ++step) {
986 this->step(pot, box, true);
987 }
988 Sample out;
989 out.kineticCv = kineticSum_ / static_cast<double>(recorded_);
990 out.meanForce = meanForce();
991 out.batches = batches_ - batchesBefore;
992 return out;
993}
void step(Potential &pot, const double *box, bool record)

◆ setAllBeads()

void eonc::pathintegral::RingPolymer::setAllBeads ( const double * q)

Definition at line 502 of file PathIntegral.cpp.

502 {
503 if (q == nullptr) {
504 throw std::invalid_argument("path integral positions are missing");
505 }
506 for (long bead = 0; bead < nBeads_; ++bead) {
507 for (long a = 0; a < nDof_; ++a) {
508 q_[static_cast<size_t>(bead)][a] = q[a];
509 }
510 }
511 if (constrain_) {
513 }
514 haveForces_ = false;
515}

◆ setBeads()

void eonc::pathintegral::RingPolymer::setBeads ( const std::vector< VectorXd > & beads)

One position vector of length 3 * nAtoms per bead.

The centroid is projected onto the hyperplane when one is set.

Definition at line 517 of file PathIntegral.cpp.

517 {
518 if (static_cast<long>(beads.size()) != nBeads_) {
519 throw std::invalid_argument("path integral bead count mismatch");
520 }
521 for (long bead = 0; bead < nBeads_; ++bead) {
522 if (beads[static_cast<size_t>(bead)].size() != nDof_) {
523 throw std::invalid_argument("path integral bead has the wrong length");
524 }
525 q_[static_cast<size_t>(bead)] = beads[static_cast<size_t>(bead)];
526 }
527 if (constrain_) {
529 }
530 haveForces_ = false;
531}
const std::vector< VectorXd > & beads() const

◆ setHyperplane()

void eonc::pathintegral::RingPolymer::setHyperplane ( const VectorXd & normal,
const VectorXd & origin )

Hold n · (q_centroid - origin) = 0.

n is normalised on the free coordinates. origin has length 3 * nAtoms.

Definition at line 550 of file PathIntegral.cpp.

551 {
552 if (normal.size() != nDof_ || origin.size() != nDof_) {
553 throw std::invalid_argument("hyperplane vectors have the wrong length");
554 }
555 planeNormal_ = VectorXd::Zero(nDof_);
556 planeOrigin_ = origin;
557 double norm = 0.0;
558 for (long a : freeIndex_) {
559 planeNormal_[a] = normal[a];
560 norm += normal[a] * normal[a];
561 }
562 if (!(norm > 0.0)) {
563 throw std::invalid_argument("hyperplane normal has no free component");
564 }
565 planeNormal_ /= std::sqrt(norm);
566 constrain_ = true;
569 haveForces_ = false;
570}
double norm(const Eigen::MatrixBase< A > &x)
sqrt of the correctly rounded squared norm; IEEE sqrt is itself correctly rounded,...

◆ setMomenta()

void eonc::pathintegral::RingPolymer::setMomenta ( const std::vector< VectorXd > & momenta)

One momentum vector of length 3 * nAtoms per bead; fixed coordinates are zeroed.

Definition at line 533 of file PathIntegral.cpp.

533 {
534 if (static_cast<long>(momenta.size()) != nBeads_) {
535 throw std::invalid_argument("path integral bead count mismatch");
536 }
537 for (long bead = 0; bead < nBeads_; ++bead) {
538 if (momenta[static_cast<size_t>(bead)].size() != nDof_) {
539 throw std::invalid_argument(
540 "path integral momentum has the wrong length");
541 }
542 for (long a = 0; a < nDof_; ++a) {
543 p_[static_cast<size_t>(bead)][a] =
544 free_[static_cast<size_t>(a)] ? momenta[static_cast<size_t>(bead)][a]
545 : 0.0;
546 }
547 }
548}
const std::vector< VectorXd > & momenta() const

◆ step()

void eonc::pathintegral::RingPolymer::step ( Potential & pot,
const double * box,
bool record )

Definition at line 924 of file PathIntegral.cpp.

924 {
925 const double half = 0.5 * opt_.dt;
926 thermostat(half);
928 // The thermostat and the momentum projection leave the beads where the
929 // last step evaluated them.
930 if (!haveForces_) {
931 forces(pot, box);
932 }
933 kick(half, true);
935 // Two free half-steps are one full step of the exact ring propagator.
936 propagate(opt_.dt);
938 forces(pot, box);
939 if (record) {
941 if (constrain_) {
942 VectorXd fc = VectorXd::Zero(nDof_);
943 for (long bead = 0; bead < nBeads_; ++bead) {
944 fc += f_[static_cast<size_t>(bead)];
945 }
946 fc /= static_cast<double>(nBeads_);
947 double along = 0.0;
948 for (long a : freeIndex_) {
949 along += planeNormal_[a] * fc[a];
950 }
951 forceSum_ += along;
952 }
953 ++recorded_;
954 }
955 kick(half, true);
957 thermostat(half);
959}

◆ thermalMomenta()

void eonc::pathintegral::RingPolymer::thermalMomenta ( )

Definition at line 581 of file PathIntegral.cpp.

581 {
582 for (long bead = 0; bead < nBeads_; ++bead) {
583 p_[static_cast<size_t>(bead)].setZero();
584 }
585 toNormal(p_, pnm_);
586 const double tSim = static_cast<double>(nBeads_) * opt_.kB * opt_.temperature;
587 for (long k = 0; k < nBeads_; ++k) {
588 const bool gleMode = opt_.thermostat == Thermostat::Piglet &&
589 gle_[static_cast<size_t>(k)].extended.rows() > 0;
590 if (gleMode) {
591 continue;
592 }
593 for (long a : freeIndex_) {
594 pnm_[static_cast<size_t>(k)][a] =
595 gauss() * std::sqrt(mass_[static_cast<size_t>(a)] * tSim);
596 }
597 }
598 if (opt_.thermostat == Thermostat::Piglet) {
599 for (long k = 0; k < nBeads_; ++k) {
600 ModeGle &mode = gle_[static_cast<size_t>(k)];
601 if (mode.extended.rows() == 0) {
602 continue;
603 }
604 const MatrixXd factor = factorCovariance(opt_.kB * mode.covariance);
605 MatrixXd noise(mode.extended.rows(), mode.extended.cols());
606 for (long r = 0; r < noise.rows(); ++r) {
607 for (long c = 0; c < noise.cols(); ++c) {
608 noise(r, c) = gauss();
609 }
610 }
611 mode.extended = factor * noise;
612 for (long col = 0; col < nFree_; ++col) {
613 const long a = freeIndex_[static_cast<size_t>(col)];
614 pnm_[static_cast<size_t>(k)][a] =
615 mode.extended(0, col) * std::sqrt(mass_[static_cast<size_t>(a)]);
616 }
617 }
618 }
621 haveForces_ = false;
622}

◆ thermostat()

void eonc::pathintegral::RingPolymer::thermostat ( double h)
private

Definition at line 756 of file PathIntegral.cpp.

756 {
757 toNormal(p_, pnm_);
758 const double tSim = static_cast<double>(nBeads_) * opt_.kB * opt_.temperature;
759 auto langevin = [&](long k, double tau) {
760 const double damp = std::exp(-h / tau);
761 const double noise = std::sqrt(tSim * (1.0 - damp * damp));
762 for (long a : freeIndex_) {
763 const double sm = std::sqrt(mass_[static_cast<size_t>(a)]);
764 double pms = pnm_[static_cast<size_t>(k)][a] / sm;
765 pms = damp * pms + noise * gauss();
766 pnm_[static_cast<size_t>(k)][a] = pms * sm;
767 }
768 };
769 for (long k = 0; k < nBeads_; ++k) {
770 if (opt_.thermostat == Thermostat::Piglet &&
771 gle_[static_cast<size_t>(k)].extended.rows() > 0) {
772 ModeGle &mode = gle_[static_cast<size_t>(k)];
773 for (long col = 0; col < nFree_; ++col) {
774 const long a = freeIndex_[static_cast<size_t>(col)];
775 mode.extended(0, col) = pnm_[static_cast<size_t>(k)][a] /
776 std::sqrt(mass_[static_cast<size_t>(a)]);
777 }
778 MatrixXd &draw = mode.draw;
779 for (long r = 0; r < draw.rows(); ++r) {
780 for (long c = 0; c < draw.cols(); ++c) {
781 draw(r, c) = gauss();
782 }
783 }
784 mode.acc.noalias() = mode.propagate * mode.extended;
785 mode.acc.noalias() += mode.noise * draw;
786 mode.extended.swap(mode.acc);
787 for (long col = 0; col < nFree_; ++col) {
788 const long a = freeIndex_[static_cast<size_t>(col)];
789 pnm_[static_cast<size_t>(k)][a] =
790 mode.extended(0, col) * std::sqrt(mass_[static_cast<size_t>(a)]);
791 }
792 } else {
793 const double tau =
794 k == 0 ? opt_.pileTau : 1.0 / (2.0 * opt_.pileScale * omegaK_[k]);
795 langevin(k, tau);
796 }
797 }
799}

◆ toNormal()

void eonc::pathintegral::RingPolymer::toNormal ( const std::vector< VectorXd > & src,
std::vector< VectorXd > & dst ) const
private

Definition at line 624 of file PathIntegral.cpp.

625 {
626 // modes_ is row-major: modes(k, j) lives at k * nBeads + j.
627 // Fixed coordinates are not ring degrees of freedom, so they stay in
628 // bead space and are left out of the multiply.
629 const double *mode = modes_.data();
630 const long n = nBeads_;
631 for (long a : freeIndex_) {
632 for (long k = 0; k < n; ++k) {
633 double sum = 0.0;
634 const double *row = mode + k * n;
635 for (long j = 0; j < n; ++j) {
636 sum += src[static_cast<size_t>(j)][a] * row[j];
637 }
638 dst[static_cast<size_t>(k)][a] = sum;
639 }
640 }
641}

Member Data Documentation

◆ atomicNumbers_

std::vector<int> eonc::pathintegral::RingPolymer::atomicNumbers_
private

Definition at line 149 of file PathIntegral.h.

◆ batches_

long eonc::pathintegral::RingPolymer::batches_ {0}
private

Definition at line 189 of file PathIntegral.h.

189{0};

◆ boxPtr_

std::vector<const double *> eonc::pathintegral::RingPolymer::boxPtr_
private

Definition at line 177 of file PathIntegral.h.

◆ centroid_

VectorXd eonc::pathintegral::RingPolymer::centroid_
mutableprivate

Definition at line 181 of file PathIntegral.h.

◆ constrain_

bool eonc::pathintegral::RingPolymer::constrain_ {false}
private

Definition at line 161 of file PathIntegral.h.

161{false};

◆ cosFull_

std::vector<double> eonc::pathintegral::RingPolymer::cosFull_
private

Definition at line 184 of file PathIntegral.h.

◆ cosHalf_

std::vector<double> eonc::pathintegral::RingPolymer::cosHalf_
private

Definition at line 182 of file PathIntegral.h.

◆ energyBuf_

std::vector<double> eonc::pathintegral::RingPolymer::energyBuf_
private

Definition at line 178 of file PathIntegral.h.

◆ f_

std::vector<VectorXd> eonc::pathintegral::RingPolymer::f_
private

Definition at line 156 of file PathIntegral.h.

◆ forceSum_

double eonc::pathintegral::RingPolymer::forceSum_ {0.0}
private

Definition at line 192 of file PathIntegral.h.

192{0.0};

◆ frcPtr_

std::vector<double *> eonc::pathintegral::RingPolymer::frcPtr_
private

Definition at line 176 of file PathIntegral.h.

◆ free_

std::vector<char> eonc::pathintegral::RingPolymer::free_
private

Definition at line 150 of file PathIntegral.h.

◆ freeIndex_

std::vector<long> eonc::pathintegral::RingPolymer::freeIndex_
private

Definition at line 151 of file PathIntegral.h.

◆ gle_

std::vector<ModeGle> eonc::pathintegral::RingPolymer::gle_
private

Definition at line 173 of file PathIntegral.h.

◆ halfDt_

double eonc::pathintegral::RingPolymer::halfDt_ {0.0}
private

Definition at line 186 of file PathIntegral.h.

186{0.0};

◆ haveForces_

bool eonc::pathintegral::RingPolymer::haveForces_ {false}
private

Definition at line 162 of file PathIntegral.h.

162{false};

◆ kineticSum_

double eonc::pathintegral::RingPolymer::kineticSum_ {0.0}
private

Definition at line 191 of file PathIntegral.h.

191{0.0};

◆ mass_

std::vector<double> eonc::pathintegral::RingPolymer::mass_
private

Definition at line 148 of file PathIntegral.h.

◆ modes_

MatrixXd eonc::pathintegral::RingPolymer::modes_
private

Definition at line 152 of file PathIntegral.h.

◆ nAtoms_

long eonc::pathintegral::RingPolymer::nAtoms_ {0}
private

Definition at line 144 of file PathIntegral.h.

144{0};

◆ nBeads_

long eonc::pathintegral::RingPolymer::nBeads_ {0}
private

Definition at line 146 of file PathIntegral.h.

146{0};

◆ nDof_

long eonc::pathintegral::RingPolymer::nDof_ {0}
private

Definition at line 145 of file PathIntegral.h.

145{0};

◆ nFree_

long eonc::pathintegral::RingPolymer::nFree_ {0}
private

Definition at line 147 of file PathIntegral.h.

147{0};

◆ nrsPtr_

std::vector<const int *> eonc::pathintegral::RingPolymer::nrsPtr_
private

Definition at line 175 of file PathIntegral.h.

◆ omegaK_

VectorXd eonc::pathintegral::RingPolymer::omegaK_
private

Definition at line 153 of file PathIntegral.h.

◆ opt_

Options eonc::pathintegral::RingPolymer::opt_
private

Definition at line 143 of file PathIntegral.h.

◆ owners_

std::vector<long> eonc::pathintegral::RingPolymer::owners_
private

Definition at line 180 of file PathIntegral.h.

◆ p_

std::vector<VectorXd> eonc::pathintegral::RingPolymer::p_
private

Definition at line 155 of file PathIntegral.h.

◆ planeNormal_

VectorXd eonc::pathintegral::RingPolymer::planeNormal_
private

Definition at line 159 of file PathIntegral.h.

◆ planeOrigin_

VectorXd eonc::pathintegral::RingPolymer::planeOrigin_
private

Definition at line 160 of file PathIntegral.h.

◆ pnm_

std::vector<VectorXd> eonc::pathintegral::RingPolymer::pnm_
private

Definition at line 158 of file PathIntegral.h.

◆ posPtr_

std::vector<const double *> eonc::pathintegral::RingPolymer::posPtr_
private

Definition at line 174 of file PathIntegral.h.

◆ q_

std::vector<VectorXd> eonc::pathintegral::RingPolymer::q_
private

Definition at line 154 of file PathIntegral.h.

◆ qnm_

std::vector<VectorXd> eonc::pathintegral::RingPolymer::qnm_
private

Definition at line 157 of file PathIntegral.h.

◆ recorded_

long eonc::pathintegral::RingPolymer::recorded_ {0}
private

Definition at line 190 of file PathIntegral.h.

190{0};

◆ rng_

std::uint64_t eonc::pathintegral::RingPolymer::rng_
private

Definition at line 188 of file PathIntegral.h.

◆ sinFull_

std::vector<double> eonc::pathintegral::RingPolymer::sinFull_
private

Definition at line 185 of file PathIntegral.h.

◆ sinHalf_

std::vector<double> eonc::pathintegral::RingPolymer::sinHalf_
private

Definition at line 183 of file PathIntegral.h.

◆ varianceBuf_

std::vector<double> eonc::pathintegral::RingPolymer::varianceBuf_
private

Definition at line 179 of file PathIntegral.h.


The documentation for this class was generated from the following files: