Virtual run; used solely for dynamic dispatch.
307 {
309 auto product = std::make_shared<Matter>(
pot,
params);
311 reactant->con2matter(
params.oh_tst_options().reactant_filename))) {
313 params.oh_tst_options().reactant_filename);
314 throw std::runtime_error("oh_tst: failed to load reactant");
315 }
317 product->con2matter(
params.oh_tst_options().product_filename))) {
319 params.oh_tst_options().product_filename);
320 throw std::runtime_error("oh_tst: failed to load product");
321 }
322
323 const double temperature =
params.main_options().temperature;
325 "[oh_tst] thermostat = {}{}",
params.oh_tst_options().thermostat,
326 params.oh_tst_options().thermostat ==
"gle"
327 ? std::string(
" (drift: ") +
params.oh_tst_options().gle_a_file +
")"
328 : std::string());
332 ?
params.main_options().randomSeed
333 : 12345;
334
335 const double tcol =
params.thermostat_options().andersen_tcol_input /
336 params.constants().timeUnit;
338
339
340 const long nAtoms =
reactant->numberOfAtoms();
341 auto masses =
reactant->getMasses();
342 std::vector<double> m3;
343 m3.reserve(3 * nAtoms);
344 for (long i = 0; i < nAtoms; ++i) {
346 for (int j = 0; j < 3; ++j)
347 m3.push_back(masses[i]);
348 }
349 }
350 m_masses3N = VectorXd::Map(m3.data(),
static_cast<long>(m3.size()));
351
352
353
354 const VectorXd xR =
reactant->getPositionsFreeV();
355 VectorXd diff = product->getPositionsFreeV() - xR;
356 {
357 AtomMatrix d(AtomMatrix::Map(diff.data(), diff.size() / 3, 3));
358 Eigen::RowVector3d total_drift = Eigen::RowVector3d::Zero();
360 EONC_LOG_INFO(
"[oh_tst] rigid drift removed: ({:.4f}, {:.4f}, "
361 "{:.4f}) A per atom",
362 total_drift[0], total_drift[1], total_drift[2]);
363 diff = VectorXd::Map(d.data(), diff.size());
364 }
365 const double guideLen = diff.norm();
366 EONC_LOG_INFO(
"[oh_tst] guideline length |P - R| = {:.4f} A over {} free "
367 "DOF",
368 guideLen, xR.size());
369
370
371
372 if (guideLen < 0.5) {
374 "translation-class endpoint pair",
375 guideLen);
376 throw std::runtime_error("oh_tst: degenerate guideline");
377 }
378 const VectorXd u = diff / guideLen;
379
380
381
385 if (!
params.oh_tst_options().symmetry_products.empty()) {
386 std::string rest =
params.oh_tst_options().symmetry_products;
387 while (!rest.empty()) {
388 const auto comma = rest.find(',');
389 std::string fname = rest.substr(0, comma);
390 rest = (comma == std::string::npos) ? "" : rest.substr(comma + 1);
391 if (fname.empty()) {
392 continue;
393 }
396 EONC_LOG_CRITICAL("OH-TST failed to load symmetry product {}", fname);
397 throw std::runtime_error("oh_tst: failed to load symmetry product");
398 }
399 VectorXd d = other.getPositionsFreeV() - xR;
400 AtomMatrix dm(AtomMatrix::Map(d.data(), d.size() / 3, 3));
402 d = VectorXd::Map(dm.data(), d.size());
403 const double dn = d.norm();
404 if (dn > 1e-8) {
406 }
407 }
408 EONC_LOG_INFO(
"[oh_tst] symmetry restriction active over {} product "
409 "directions",
411 }
412
413
414
415
416 double s =
params.oh_tst_options().s_init * guideLen;
417 double vS = 0.0;
418 VectorXd n = u;
419 VectorXd omega = VectorXd::Zero(n.size());
420 const double mS =
params.oh_tst_options().plane_mass;
421 const double dtPlane =
params.oh_tst_options().plane_time_step;
422 const double dsMax =
params.oh_tst_options().ds_max;
423 const double dThetaMax =
params.oh_tst_options().dtheta_max;
424 const double fTol =
params.oh_tst_options().force_tol;
425
426 Matter walker(*reactant);
427
428
429 VectorXd xFree = walker.getPositionsFreeV();
430
431
432
433 double aTrans = 0.0, aRot = 0.0, aBest = 0.0, sBest = s;
434 VectorXd nBest = n;
435 bool havePrev = false;
436 double fnPrev = 0.0;
437 VectorXd rotRawPrev, posPrev, nPrev;
438 double gSPrev = 0.0;
439 VectorXd gRotPrev;
440 int sideSign = 0;
441
442
443
444 std::ofstream prog("oh_tst_progression.dat");
445 if (prog) {
446 prog << "# plane s/L <F.n> (eV/A) dA_trans (eV) dA_rot (eV) "
447 "A (eV) n.u\n";
448 } else {
450 }
451
452 const bool scanMode =
params.oh_tst_options().pmf_scan;
453 const long nScan = std::max(2L,
params.oh_tst_options().scan_planes);
454 const long nPlanes = scanMode ? nScan :
params.oh_tst_options().max_planes;
455
456
457 if (scanMode) {
459 sBest = s;
460 }
461 long plane = 0;
462 bool converged = false;
463
464
465
466
467
468 bool guidelineMoving = false;
469 VectorXd gOrigin = xR;
470 VectorXd gDir = u;
471 long rotOnlySteps = 0;
472 for (; plane < nPlanes; ++plane) {
473 if (scanMode) {
474 s =
pmfScanS(plane, nScan, guideLen);
475 }
476 const VectorXd gamma = gOrigin + s * gDir;
478
479
480
481
482
483 const double gS = -avg.fn / mS;
484 VectorXd gRot = avg.rotNorm - n * n.dot(avg.rotNorm);
485
486 if (plane == 0) {
487 sideSign = (avg.fn < 0.0) ? -1 : 1;
488 }
489
490
491
492
493
494
495
496 if (havePrev) {
497
498
499 const bool sameSide = scanMode || ((fnPrev < 0.0 ? -1 : 1) == sideSign &&
500 (avg.fn < 0.0 ? -1 : 1) == sideSign);
501 if (sameSide) {
502 const VectorXd fParMean = 0.5 * (fnPrev * nPrev + avg.fn * n);
503 aTrans += -fParMean.dot(avg.pos - posPrev);
504 const VectorXd rotMean = 0.5 * (rotRawPrev + avg.rotRaw);
505 aRot += rotMean.dot(n - nPrev);
506 }
507 }
508
509 const double aTotal = aTrans + aRot;
510
511
512 if (plane != 0 && !guidelineMoving &&
513 ((avg.fn < 0.0) ? -1 : 1) != sideSign &&
515 guidelineMoving = true;
517 "sign; guideline now follows <r> along the normal",
518 plane);
519 }
520 if (aTotal > aBest) {
521 aBest = aTotal;
522 sBest = s;
523 nBest = n;
524 }
525 if (prog) {
526 prog << std::format("{:6} {:10.6f} {:14.6e} {:12.6f} {:12.6f} {:12.6f} "
527 "{:10.6f}\n",
528 plane, s / guideLen, avg.fn, aTrans, aRot, aTotal,
529 n.dot(u));
530 prog.flush();
531 }
532 EONC_LOG_DEBUG(
"[oh_tst] plane {} s/L {:.4f} <F.n> {:.4e} A {:.4f} eV",
533 plane, s / guideLen, avg.fn, aTotal);
534
535
536
537
538
539 if (aTotal >
params.oh_tst_options().max_delta_a) {
541 "[oh_tst] accumulated work {:.2f} eV exceeds max_delta_a "
542 "{:.2f} eV at plane {} -- endpoints likely unminimized",
543 aTotal,
params.oh_tst_options().max_delta_a, plane);
544 throw std::runtime_error("oh_tst: diverging reversible work");
545 }
546
547
548
549
550
551 if (!scanMode && plane > 2 && aTotal > 2.0 *
m_kbt &&
552 std::fabs(avg.fn) < fTol &&
553 gRot.norm() *
params.oh_tst_options().alpha_rot < fTol) {
554 converged = true;
555
556
557 aBest = aTotal;
558 sBest = s;
559 nBest = n;
560 if (prog) {
561 prog << std::format("# converged at plane {}\n", plane);
562 }
563 break;
564 }
565
566 if (scanMode) {
567
568
569
570
571 fnPrev = avg.fn;
572 nPrev = n;
573 rotRawPrev = avg.rotRaw;
574 posPrev = avg.pos;
575 gSPrev = gS;
576 gRotPrev = gRot;
577 havePrev = true;
578 s =
pmfScanS(plane + 1, nScan, guideLen);
579 const VectorXd gammaNext = xR + s * u;
580 xFree -= u * (u.dot(xFree - gammaNext));
581 walker.setPositionsFreeV(xFree);
582 continue;
583 }
584
585
586
587
588
589
590
591
592 const bool rotationOnly =
593 guidelineMoving &&
594 gRot.norm() *
params.oh_tst_options().alpha_rot > 5.0 * fTol &&
595 rotOnlySteps < 10;
596 if (rotationOnly) {
597 ++rotOnlySteps;
598 } else {
599 rotOnlySteps = 0;
600 }
601 if (havePrev) {
602 vS += 0.5 * dtPlane * (gS + gSPrev);
603 } else {
604 vS += dtPlane * gS;
605 }
606 if (vS * gS < 0.0)
607 vS = 0.0;
608 double ds = dtPlane * vS + 0.5 * dtPlane * dtPlane * gS;
609 ds = std::clamp(ds, -dsMax, dsMax);
610 if (rotationOnly) {
611 ds = 0.0;
612 vS = 0.0;
613 }
614 const double sNew =
615 guidelineMoving ? s + ds : std::clamp(s + ds, 0.0, guideLen);
616
617
618
619
620 if (havePrev && gRotPrev.size() == gRot.size()) {
621 omega += 0.5 * dtPlane * (gRot + gRotPrev);
622 } else {
623 omega += dtPlane * gRot;
624 }
625 omega -= n * n.dot(omega);
626 const double gNorm = gRot.norm();
627 if (gNorm > 1e-14) {
628 const VectorXd gHat = gRot / gNorm;
629 const double along = omega.dot(gHat);
630 if (along > 0.0) {
631 omega = gHat * along;
632 } else {
633 omega.setZero();
634 }
635 } else {
636 omega.setZero();
637 }
638 VectorXd dn = dtPlane * omega + 0.5 * dtPlane * dtPlane * gRot;
639 const double dTheta = dn.norm();
640 if (dTheta > dThetaMax) {
641 dn *= dThetaMax / dTheta;
642 }
643 const VectorXd nOld = n;
645 if (n.dot(gDir) < 0.0) {
646
647 n = -n;
648 }
649
650
651
652
653 VectorXd arm = avg.pos - gamma;
654 VectorXd armNew = arm - nOld * arm.dot(n - nOld);
655 const double armLen = arm.norm();
656 const double armNewLen = armNew.norm();
657 if (armLen > 1e-12 && armNewLen > 1e-12) {
658 armNew *= armLen / armNewLen;
659 }
660 VectorXd gammaNew;
661 if (guidelineMoving) {
662
663
664
665
666
667
668 gOrigin = avg.pos;
669 gDir = n;
670 s = ds;
671 gammaNew = gOrigin + s * gDir;
672 } else {
673 gammaNew = gOrigin + sNew * gDir;
674 }
675 xFree = gammaNew + armNew;
676 xFree -= n * n.dot(xFree - gammaNew);
677 walker.setPositionsFreeV(xFree);
678
679 fnPrev = avg.fn;
680 nPrev = nOld;
681 rotRawPrev = avg.rotRaw;
682 posPrev = avg.pos;
683 gSPrev = gS;
684 gRotPrev = gRot;
685 havePrev = true;
686 if (!guidelineMoving) {
687 s = sNew;
688 }
689 }
690 bool progOk = prog.is_open();
691 if (progOk) {
692 prog.close();
693 progOk = static_cast<bool>(prog);
694 if (!progOk) {
695 EONC_LOG_ERROR(
"[oh_tst] failed to write oh_tst_progression.dat");
696 }
697 }
698
699
700
701 if (scanMode && plane >= nPlanes)
702 converged = true;
703
704
705
706
707 const long planesUsed = (plane < nPlanes) ? plane + 1 : plane;
708
709
710
711 const double mu = (
m_masses3N.array() * nBest.array().square()).sum();
713
714
715
716 Matter rWalker(*reactant);
717 const double sFirst = scanMode ?
pmfScanS(0, nScan, guideLen)
718 :
params.oh_tst_options().s_init * guideLen;
719 const VectorXd gammaR = xR + sFirst * u;
721
722
723 const double kInternal = vFlux * qRatio * std::exp(-aBest /
m_kbt);
724 const double kSI = kInternal / (
params.constants().timeUnit * 1.0e-15);
725
726 std::vector<std::string> returnFiles;
727 std::ofstream out("results.dat");
728 if (!out) {
730 throw std::runtime_error("oh_tst: cannot open results.dat");
731 }
732 out << "oh_tst job_type\n";
733 out << std::format("{} converged\n", converged ? 1 : 0);
734 out << std::format("{} planes_used\n", planesUsed);
735 out << std::format("{:.8f} free_energy_barrier_eV\n", aBest);
736 out << std::format("{:.8f} delta_a_trans_eV\n", aTrans);
737 out << std::format("{:.8f} delta_a_rot_eV\n", aRot);
738 out << std::format("{:.8f} s_star_over_L\n", sBest / guideLen);
739 out << std::format("{:.8f} guideline_length_A\n", guideLen);
740 out << std::format("{:.8f} normal_overlap_with_guideline\n", nBest.dot(u));
741 out << std::format("{:.8e} effective_mass_amu\n", mu);
742 out << std::format("{:.8e} q_ratio_per_A\n", qRatio);
743 out << std::format("{:.8e} rate_ohtst_per_s\n", kSI);
744 out << std::format("{:.4f} temperature_K\n", temperature);
745 out.close();
746 if (!out) {
748 throw std::runtime_error("oh_tst: failed to write results.dat");
749 }
750 returnFiles.push_back("results.dat");
751 if (progOk) {
752 returnFiles.push_back("oh_tst_progression.dat");
753 }
754 EONC_LOG_INFO(
"[oh_tst] {} after {} planes: A = {:.4f} eV at s/L = {:.4f}, "
755 "k = {:.4e} 1/s at {:.1f} K",
756 converged ? "converged" : "max planes", planesUsed, aBest,
757 sBest / guideLen, kSI, temperature);
758 return returnFiles;
759}
Eigen::Matrix< double, Eigen::Dynamic, 3, eOnStorageOrder > AtomMatrix
#define EONC_LOG_DEBUG(...)
#define EONC_LOG_ERROR(...)
#define EONC_LOG_INFO(...)
#define EONC_LOG_CRITICAL(...)
std::shared_ptr< Potential > pot
Set when one Potential is shared across Matters, images, or a caller.
double reactantQRatio(Matter &matter, const VectorXd &gammaR, const VectorXd &normal)
PlaneAverages samplePlane(Matter &matter, VectorXd &x, const VectorXd &gamma, const VectorXd &normal)
long m_seedState
LCG state for the thermostat draws.
std::vector< VectorXd > m_symDirs
p-hat_i, index 0 = primary
A::PlainObject normalized(const Eigen::MatrixBase< A > &x, double min_norm=1e-300)
x / norm(x), or zero below min_norm (the safemath convention).
constexpr bool io_ok(IoStatus s) noexcept
AtomMatrix minImageRemoveRigidDrift(const Matter &reference, AtomMatrix diff, Eigen::RowVector3d *totalDrift)
Minimum-image a free-atom difference and remove rigid translation.
double pmfScanS(long plane, long nScan, double guideLen)
Uniform PMF-scan coordinate.
bool reversibleWorkPastRidge(double reversibleWork, double thermalEnergy)
The reactant basin is force-free.