41 bool tsInterpolate =
false;
42 std::shared_ptr<Matter> transitionState =
nullptr;
44 std::filesystem::exists(
"ts.con")) {
45 std::string transitionStateFilename =
47 transitionState = std::make_shared<Matter>(
pot,
params);
50 transitionState->con2matter(transitionStateFilename))) {
52 throw std::runtime_error(
"failed to load " + transitionStateFilename);
56 auto initial = std::make_shared<Matter>(
pot,
params);
57 auto final_state = std::make_shared<Matter>(
pot,
params);
62 const bool usePathEndpoints =
64 !
params.neb_options().initialization.input_path.empty();
66 if (usePathEndpoints) {
68 params.neb_options().initialization.input_path);
69 if (file_paths.size() < 2) {
70 throw std::runtime_error(
71 "NEB initial_path_in must list at least two frames "
72 "(reactant and product endpoints)");
74 if (!
eonc::io::io_ok(initial->con2matter(file_paths.front().string()))) {
76 file_paths.front().string());
77 throw std::runtime_error(
"failed to load NEB path reactant frame");
79 if (!
eonc::io::io_ok(final_state->con2matter(file_paths.back().string()))) {
81 file_paths.back().string());
82 throw std::runtime_error(
"failed to load NEB path product frame");
85 "NEB endpoints from initial path list (skipped {} / {})",
86 reactantFilename, productFilename);
90 throw std::runtime_error(
"failed to load reactant.con");
94 throw std::runtime_error(
"failed to load product.con");
99 "reactant and product");
102 "reactant and ts.con");
114 bool shouldMinimizeEndpoints =
false;
115 if (!
params.neb_options().endpoints.minimize) {
116 QUILL_LOG_DEBUG(
m_log,
117 "minimize_endpoints == false: not minimizing endpoints.");
118 shouldMinimizeEndpoints =
false;
120 if (
params.neb_options().initialization.input_path.empty()) {
121 QUILL_LOG_DEBUG(
m_log,
"minimize_endpoints == true and nebIpath "
122 "empty: minimizing endpoints.");
123 shouldMinimizeEndpoints =
true;
127 if (
params.neb_options().endpoints.use_path_file) {
130 "minimize_endpoints == true and nebIpath provided, but "
131 "minimize_endpoints_for_ipath == true: minimizing endpoints.");
132 shouldMinimizeEndpoints =
true;
136 "minimize_endpoints == true but nebIpath provided and "
137 "minimize_endpoints_for_ipath == false: not minimizing endpoints.");
138 shouldMinimizeEndpoints =
false;
143 if (shouldMinimizeEndpoints) {
144 QUILL_LOG_DEBUG(
m_log,
"Minimizing reactant");
145 initial->relax(
false,
params.debug_options().write_movies,
146 params.main_options().checkpoint,
"react_neb",
"react_neb");
147 QUILL_LOG_DEBUG(
m_log,
"Minimized reactant");
148 QUILL_LOG_DEBUG(
m_log,
"Minimizing product");
149 final_state->relax(
false,
params.debug_options().write_movies,
150 params.main_options().checkpoint,
"prod_neb",
155 std::make_unique<NudgedElasticBand>(initial, final_state,
params,
pot);
158 AtomMatrix reactantToTS = transitionState->pbc(
159 transitionState->getPositions() - initial->getPositions());
160 AtomMatrix TSToProduct = transitionState->pbc(
161 final_state->getPositions() - transitionState->getPositions());
162 for (
int image = 1; image <=
neb->numImages; image++) {
163 int mid =
neb->numImages / 2 + 1;
165 double frac =
static_cast<double>(image) /
static_cast<double>(mid);
166 neb->path[image]->setPositions(initial->getPositions() +
167 frac * reactantToTS);
168 }
else if (image > mid) {
169 double frac =
static_cast<double>(image - mid) /
170 static_cast<double>(
neb->numImages - mid + 1);
171 neb->path[image]->setPositions(transitionState->getPositions() +
173 }
else if (image == mid) {
174 neb->path[image]->setPositions(transitionState->getPositions());
180 status =
neb->compute();
184 neb->printImageData();
186 if (
params.saddle_search_options().prfo_refine.enabled) {
199 if (
neb.climbingImage <= 0) {
200 QUILL_LOG_WARNING(
m_log,
"prfo_refine: the band has no climbing image");
204 const auto ci =
static_cast<size_t>(
neb.climbingImage);
208 saddle, tangent,
params.main_options().finiteDifference);
209 if (curvature < 0.0) {
211 const std::string refinedFilename(
"sp_refined.con");
215 QUILL_LOG_ERROR(
m_log,
"Failed to write {}", refinedFilename);
218 QUILL_LOG_WARNING(
m_log,
219 "prfo_refine: curvature {:.4f} along the climbing "
220 "image tangent is not negative, no refinement",
228 std::string resultsFilename(
"results.dat");
233 std::optional<eonc::tunneling::BandWalls> walls;
234 if (
const long points =
params.neb_options().tunnel_wall_points;
237 const std::vector<std::shared_ptr<Matter>> band(
239 neb->path.begin() +
static_cast<std::ptrdiff_t
>(
neb->numImages + 2));
241 }
catch (
const std::invalid_argument &ex) {
242 QUILL_LOG_WARNING(
m_log,
"No tunnelling walls for this band: {}",
252 params.potential_options().potential,
pots().total_force_calls(),
true,
253 neb->reactantEnergy);
255 env.job_type =
"neb";
256 env.extras.emplace_back(
"force_calls_neb",
static_cast<double>(
fCallsNEB));
257 env.extras.emplace_back(
"energy_reference",
neb->reactantEnergy);
258 env.extras.emplace_back(
"number_of_images",
259 static_cast<double>(
neb->numImages));
260 if (
params.saddle_search_options().prfo_refine.enabled) {
261 env.extras.emplace_back(
"force_calls_prfo_refine",
265 env.extras.emplace_back(
"prfo_refine_converged",
267 env.extras.emplace_back(
"prfo_refine_max_force",
refined_->maxForce);
268 env.extras.emplace_back(
"prfo_refine_energy",
271 env.writeResultsDat(resultsFilename);
272 std::ofstream out(resultsFilename, std::ios::binary | std::ios::app);
274 QUILL_LOG_ERROR(
m_log,
"Failed to reopen {} for image keys",
279 for (
long i = 0; i <=
neb->numImages + 1; i++) {
281 "{:f} image{}_energy\n",
282 neb->path[i]->getPotentialEnergy() -
neb->reactantEnergy, i);
283 out << std::format(
"{:f} image{}_force\n",
284 neb->path[i]->getForces().norm(), i);
285 double proj_norm = (i >= 1 && i <=
neb->numImages)
286 ?
neb->projectedForce[i]->norm()
288 out << std::format(
"{:f} image{}_projected_force\n", proj_norm, i);
291 long safeNumExtrema = std::min(
292 neb->numExtrema,
static_cast<long>(
neb->extremumPosition.size()));
293 out << std::format(
"{} number_of_extrema\n", safeNumExtrema);
294 for (
long i = 0; i < safeNumExtrema; i++) {
295 out << std::format(
"{:f} extremum{}_position\n",
neb->extremumPosition[i],
297 out << std::format(
"{:f} extremum{}_energy\n",
neb->extremumEnergy[i], i);
302 std::string nebFilename(
"neb.con");
304 std::vector<double> quantumFreeEnergy;
305 const double quantumTemperature =
params.neb_options().quantum_temperature;
306 const std::vector<double> *quantumPtr =
nullptr;
307 if (quantumTemperature > 0.0) {
309 quantumTemperature,
params);
310 quantumPtr = &quantumFreeEnergy;
313 neb->path,
neb->tangent,
neb->eigenmode_solvers,
neb->numImages,
314 params.debug_options().estimate_neb_eigenvalues, nebFilename,
315 std::nullopt,
neb->reactantEnergy, quantumPtr,
316 walls ? &*walls :
nullptr))) {
317 QUILL_LOG_ERROR(
m_log,
"Failed to write {}", nebFilename);
321 std::string spFilename(
"sp.con");
323 neb->path[
neb->maxEnergyImage]->matter2con(spFilename))) {
324 QUILL_LOG_ERROR(
m_log,
"Failed to write {}", spFilename);
329 if (
params.neb_options().mmf_peaks.enabled &&
neb->numExtrema > 0) {
331 for (
long i = 0; i <
neb->numExtrema; i++) {
335 double relativeEnergy =
neb->extremumEnergy[i] -
neb->reactantEnergy;
337 if (
neb->extremumCurvature[i] < 0 &&
338 relativeEnergy >
params.neb_options().mmf_peaks.tolerance) {
339 double posFraction =
neb->extremumPosition[i];
340 int leftIdx =
static_cast<int>(std::floor(posFraction));
341 double f = posFraction - leftIdx;
343 if (leftIdx < 0 || leftIdx >=
neb->numImages + 1)
348 *
neb->path[leftIdx], *
neb->path[leftIdx + 1], f);
349 std::string peakPosFile = std::format(
"peak{:02d}_pos.con", peakCount);
351 QUILL_LOG_ERROR(
m_log,
"Failed to write {}", peakPosFile);
357 neb->path,
neb->tangent,
neb->numImages, posFraction);
359 std::string peakModeFile =
360 std::format(
"peak{:02d}_mode.dat", peakCount);
362 std::ofstream modeOut(peakModeFile);
364 for (
long row = 0; row < peakMode.rows(); ++row) {
365 modeOut << std::format(
"{:.17g} {:.17g} {:.17g}\n",
366 peakMode(row, 0), peakMode(row, 1),
375 "Generated MMF peak {:02d} at position {:.3f} (Energy: {:.3f} eV)",
376 peakCount, posFraction, relativeEnergy);
383 neb->printImageData(
true, std::numeric_limits<size_t>::max());
387 QUILL_LOG_DEBUG(
m_log,
"Final state: ");
389 QUILL_LOG_DEBUG(
m_log,
"Nudged elastic band, successful.");
391 QUILL_LOG_DEBUG(
m_log,
"Nudged elastic band, too many iterations.");
393 QUILL_LOG_WARNING(
m_log,
"Unknown status: {}!",
static_cast<int>(status));
Direct optimization for energy minimization.
Eigen::Matrix< double, Eigen::Dynamic, 3, eOnStorageOrder > AtomMatrix
#define EONC_LOG_CRITICAL(...)
PotRegistry & pots() noexcept
std::shared_ptr< Potential > pot
Set when one Potential is shared across Matters, images, or a caller.
io::IoStatus matter2con(std::string filename, bool append=false, const io::ConFrameMetadata *metadata=nullptr)
std::vector< std::string > run(void)
Virtual run; used solely for dynamic dispatch.
void printEndState(NudgedElasticBand::NEBStatus status)
std::vector< std::string > returnFiles
void saveData(NudgedElasticBand::NEBStatus status, NudgedElasticBand *neb)
std::optional< PrfoRefineResult > refined_
void refineClimbingImage(NudgedElasticBand &neb)
size_t total_force_calls() const noexcept
Matter interpolateImage(const Matter &A, const Matter &B, double fraction)
void requireSameAtomCount(const Matter &a, const Matter &b, std::string_view what)
Abort before Eigen subtracts two position matrices of different size.
std::vector< fs::path > readFilePaths(const std::string &listFilePath)
Reads a file where each line contains a path to another file.
std::string getRelevantFile(std::string filename)
constexpr bool io_ok(IoStatus s) noexcept
AtomMatrix interpolatedPeakMode(const std::vector< std::shared_ptr< Matter > > &path, const std::vector< std::shared_ptr< AtomMatrix > > &tangent, long numImages, double posFraction)
Unit tangent at a fractional image index.
eonc::io::IoStatus writePathCon(const std::vector< std::shared_ptr< Matter > > &path, const std::vector< std::shared_ptr< AtomMatrix > > &tangent, const std::vector< std::shared_ptr< EigenmodeStrategy > > &eigenmode_solvers, long numImages, bool estimateEigenvalues, std::string filename, std::optional< size_t > bandIndex, double referenceEnergy, const std::vector< double > *quantumFreeEnergy, const eonc::tunneling::BandWalls *walls)
Write a NEB band as a multi-frame .con via readcon ConFrameBuilder::clone().
BandWalls bandWalls(const std::vector< std::shared_ptr< Matter > > &band, double referenceEnergy, long points)
The walls of both ends of a band, after Khomenko et al.
RAII resource manager for the ARTn C library with global synchronization.
PrfoRefineResult prfoRefineSaddle(Matter &, const AtomMatrix &, double, const Parameters &)
Partitioned rational function optimization (P-RFO) of a first-order saddle in the rgsaddle Sella orde...
JobResultProvenance provenanceForJob(const Parameters ¶ms)
Optimizer backend, engine identity, rgpot pin, and xts ABI from a job.
std::vector< double > quantumFreeEnergies(const std::vector< std::shared_ptr< Matter > > &path, const std::vector< std::shared_ptr< AtomMatrix > > &tangent, double temperature, const Parameters ¶ms)
F(s) = V(s) + the harmonic free energy of the vibrations, one entry per image.
void requirePrfoRefine(const Parameters ¶ms)
Does nothing unless [Saddle Search] prfo_refine is on.
double modeCurvature(const Matter &matter, const AtomMatrix &mode, double dr)
Curvature along mode from one forward difference of the forces over dr Angstrom.
static JobResultEnvelope fromMinimization(RunStatus status, PotType pot, std::uint64_t fcalls, bool hasE, double energy)