15namespace fs = std::filesystem;
34 std::vector<Matter> all_images_on_path(nimgs + 2, initImg);
35 all_images_on_path.front() =
Matter(initImg);
36 all_images_on_path.back() =
Matter(finalImg);
37 AtomMatrix posInitial = all_images_on_path.front().getPositions();
38 AtomMatrix posFinal = all_images_on_path.back().getPositions();
39 AtomMatrix imageSep = initImg.
pbc(posFinal - posInitial) / (nimgs + 1);
40 imageSep = imageSep.array() * initImg.
getFree().array();
41 for (
auto it{std::next(all_images_on_path.begin())};
42 it != std::prev(all_images_on_path.end()); ++it) {
44 (*it).setPositions(posInitial +
46 int(std::distance(all_images_on_path.begin(), it)));
48 return all_images_on_path;
51std::vector<Matter>
filePathInit(
const std::vector<fs::path> &fsrcs,
52 const Matter &refImg,
const size_t nimgs) {
53 std::vector<Matter> all_images_on_path;
54 if (nimgs + 2 != fsrcs.size()) {
55 throw std::runtime_error(
"Error in filePathInit: Expected " +
56 std::to_string(nimgs + 2) +
" files, but got " +
57 std::to_string(fsrcs.size()) +
".");
59 all_images_on_path.reserve(nimgs + 2);
61 for (
const auto &filePath : fsrcs) {
64 throw std::runtime_error(
"failed to load NEB path frame: " +
67 if (!all_images_on_path.empty()) {
70 all_images_on_path.push_back(img);
72 return all_images_on_path;
76 std::vector<fs::path> paths;
77 std::ifstream inputFile(listFilePath);
79 if (!inputFile.is_open()) {
80 throw std::runtime_error(
"Error: Could not open path list file: " +
85 while (std::getline(inputFile, line)) {
88 paths.emplace_back(line);
99 for (
int i = 0; i < natoms; ++i) {
100 for (
int j = 0; j < natoms; ++j) {
104 d(i, j) = m.
pbc(pos.row(i) - pos.row(j)).norm();
116 QUILL_LOG_INFO(
log,
"Generating initial path using IDPP...");
119 log,
"ZBL Repulsion not implemented for iterative IDPP (idppPath). "
120 "Using standard IDPP.");
124 std::vector<Matter> path =
linearPath(initImg, finalImg, nimgs);
132 for (
size_t i = 1; i <= nimgs; ++i) {
135 double xi =
static_cast<double>(i) / (nimgs + 1);
138 MatrixXd dTarget = (1.0 - xi) * dInit + xi * dFinal;
141 auto idpp_objf = std::make_shared<IDPPObjectiveFunction>(
142 std::make_shared<Matter>(path[i]), params, dTarget);
155 double residual = idpp_objf->getConvergence();
157 "IDPP Image {:2d}/{:2d} | xi: {:.2f} | Residual: {:.4e}", i,
158 nimgs, xi, residual);
161 path[i].setPositionsFreeV(idpp_objf->getPositions());
164 QUILL_LOG_INFO(
log,
"IDPP path generation complete.");
169 const Matter &finalImg,
size_t nimgs,
172 QUILL_LOG_INFO(
log,
"Generating initial path using Collective IDPP-NEB...");
174 std::vector<Matter> path =
linearPath(initImg, finalImg, nimgs);
177 std::shared_ptr<ObjectiveFunction> idpp_objf =
178 std::make_shared<CollectiveIDPPObjectiveFunction>(path, params);
182 QUILL_LOG_INFO(
log,
"Enabling ZBL repulsive penalty for IDPP...");
185 idpp_objf = std::make_shared<ZBLRepulsiveIDPPObjective>(idpp_objf, zbl_pot,
194 int checkInterval = 40;
196 while (currentStep < maxSteps) {
198 currentStep += checkInterval;
200 if (idpp_objf->isConverged()) {
202 "IDPP-NEB converged after {} steps. Max Residual: {:.4f}",
203 currentStep, idpp_objf->getConvergence());
208 QUILL_LOG_WARNING(
log,
209 "IDPP-NEB reached max_iterations ({}) without full "
210 "convergence. Residual: {:.4f}",
211 maxSteps, idpp_objf->getConvergence());
222 diff = diff.array() * A.
getFree().array();
228 size_t target_nimgs,
const Parameters ¶ms,
234 "Generating initial path using S-IDPP{} ({} images, "
235 "alpha={:.2f}, frontier_tol={:.4f})...",
236 use_zbl ?
"-ZBL" :
"", target_nimgs, init.sidpp_alpha,
237 init.sidpp_frontier_tol);
240 std::vector<Matter> path;
241 path.push_back(initImg);
242 path.push_back(finalImg);
244 std::shared_ptr<Potential> zbl_pot =
nullptr;
252 int nIntermediate = 0;
253 bool addToLeft =
true;
256 auto makeIDPP = [&]() -> std::shared_ptr<ObjectiveFunction> {
257 auto objf = std::make_shared<CollectiveIDPPObjectiveFunction>(path, params);
258 if (use_zbl && zbl_pot) {
259 return std::make_shared<ZBLRepulsiveIDPPObjective>(objf, zbl_pot, path,
266 auto relaxPath = [&](
int maxSteps) ->
double {
267 auto objf = makeIDPP();
270 while (step < maxSteps) {
271 optim->run(5, init.max_move);
273 if (objf->isConverged())
276 return objf->getConvergence();
280 while (nIntermediate <
static_cast<int>(target_nimgs)) {
282 if (addToLeft && nIntermediate <
static_cast<int>(target_nimgs)) {
284 Matter &frontier = path[nLeft];
285 Matter &next = path[nLeft + 1];
287 path.insert(path.begin() + nLeft + 1, newImg);
290 QUILL_LOG_DEBUG(
log,
"S-IDPP: +L frontier (nL={}, nR={}, total={})",
291 nLeft, nRight, nIntermediate);
292 }
else if (nIntermediate <
static_cast<int>(target_nimgs)) {
294 int rightIdx =
static_cast<int>(path.size()) - 1 - nRight;
295 Matter &frontier = path[rightIdx];
296 Matter &prev = path[rightIdx - 1];
298 path.insert(path.begin() + rightIdx, newImg);
301 QUILL_LOG_DEBUG(
log,
"S-IDPP: +R frontier (nL={}, nR={}, total={})",
302 nLeft, nRight, nIntermediate);
304 addToLeft = !addToLeft;
307 double residual = relaxPath(init.nsteps);
311 if (residual > init.sidpp_frontier_tol) {
312 double residual2 = relaxPath(init.max_iterations - init.nsteps);
313 QUILL_LOG_DEBUG(
log,
"S-IDPP: Extended relaxation {:.4f} -> {:.4f}",
314 residual, residual2);
315 residual = residual2;
318 QUILL_LOG_DEBUG(
log,
"S-IDPP: {} images | Residual: {:.4f}", nIntermediate,
323 if (init.sidpp_reparam && path.size() > 3) {
324 QUILL_LOG_INFO(
log,
"S-IDPP: Reparameterizing {} images along arc length",
330 QUILL_LOG_INFO(
log,
"S-IDPP: Final relaxation of full path...");
331 double finalResidual = relaxPath(init.max_iterations);
332 QUILL_LOG_INFO(
log,
"S-IDPP: Final residual: {:.4f}", finalResidual);
340 if (path.size() < 2) {
343 if (!(min_sep > 0.0)) {
344 throw std::invalid_argument(
345 "NEB path: min adjacent image separation must be positive");
347 for (
size_t i = 1; i < path.size(); ++i) {
349 path[i].pbc(path[i].getPositions() - path[i - 1].getPositions());
350 const double d = diff.norm();
351 if (!(d > min_sep) || !std::isfinite(d)) {
352 throw std::runtime_error(
353 "NEB path: adjacent images are degenerate (SIDPP collapse)");
365 double h00 = 2 * f3 - 3 * f2 + 1;
366 double h10 = f3 - 2 * f2 + f;
367 double h01 = -2 * f3 + 3 * f2;
368 double h11 = f3 - f2;
370 return h00 * P0 + h10 * T0 + h01 * P1 + h11 * T1;
374 size_t targetCount) {
375 if (densePath.size() == targetCount + 2)
378 size_t n = densePath.size();
381 std::vector<AtomMatrix> origPos(n);
382 origPos[0] = densePath[0].getPositions();
383 std::vector<double> arcLength(n, 0.0);
384 for (
size_t i = 1; i < n; ++i) {
385 AtomMatrix diff = densePath[i].pbc(densePath[i].getPositions() -
386 densePath[i - 1].getPositions());
387 origPos[i] = origPos[i - 1] + diff;
388 arcLength[i] = arcLength[i - 1] + diff.norm();
390 double totalLength = arcLength.back();
393 std::vector<AtomMatrix> tangents(n);
394 for (
size_t i = 0; i < n; ++i) {
397 T = densePath[i].pbc(densePath[i + 1].getPositions() -
398 densePath[i].getPositions());
399 }
else if (i == n - 1) {
400 T = densePath[i].pbc(densePath[i].getPositions() -
401 densePath[i - 1].getPositions());
403 AtomMatrix dNext = densePath[i].pbc(densePath[i + 1].getPositions() -
404 densePath[i].getPositions());
405 AtomMatrix dPrev = densePath[i].pbc(densePath[i].getPositions() -
406 densePath[i - 1].getPositions());
407 T = 0.5 * (dNext + dPrev);
410 if (i > 0 && i < n - 1) {
411 double localScale = (arcLength[i + 1] - arcLength[i - 1]) / 2.0;
412 T = T.normalized() * localScale;
417 std::vector<Matter> resampled;
418 resampled.reserve(targetCount + 2);
419 resampled.push_back(densePath.front());
422 double segmentLength = totalLength / (targetCount + 1);
424 for (
size_t i = 1; i <= targetCount; ++i) {
425 double targetArc = i * segmentLength;
429 for (
size_t j = 1; j < n; ++j) {
430 if (arcLength[j] >= targetArc) {
435 size_t highIdx = lowIdx + 1;
438 double segmentArc = arcLength[highIdx] - arcLength[lowIdx];
439 double f = (segmentArc > 1e-10)
440 ? (targetArc - arcLength[lowIdx]) / segmentArc
443 Matter newImg(densePath[0]);
445 origPos[highIdx], tangents[highIdx],
447 resampled.push_back(newImg);
450 resampled.push_back(densePath.back());
455 size_t n = path.size();
461 std::vector<AtomMatrix> origPos(n);
462 origPos[0] = path[0]->getPositions();
463 std::vector<double> arcLength(n, 0.0);
464 for (
size_t i = 1; i < n; ++i) {
466 path[i]->pbc(path[i]->getPositions() - path[i - 1]->getPositions());
467 origPos[i] = origPos[i - 1] + diff;
468 arcLength[i] = arcLength[i - 1] + diff.norm();
470 double totalLength = arcLength.back();
471 if (totalLength < 1e-12)
475 std::vector<AtomMatrix> tangents(n);
476 for (
size_t i = 0; i < n; ++i) {
479 T = path[i]->pbc(path[i + 1]->getPositions() - path[i]->getPositions());
480 }
else if (i == n - 1) {
481 T = path[i]->pbc(path[i]->getPositions() - path[i - 1]->getPositions());
484 path[i]->pbc(path[i + 1]->getPositions() - path[i]->getPositions());
486 path[i]->pbc(path[i]->getPositions() - path[i - 1]->getPositions());
489 if (i > 0 && i < n - 1) {
490 double localScale = (arcLength[i + 1] - arcLength[i - 1]) / 2.0;
491 T = T.normalized() * localScale;
497 size_t nInterior = n - 2;
498 double segLen = totalLength / (nInterior + 1);
500 for (
size_t i = 1; i <= nInterior; ++i) {
501 double targetArc = i * segLen;
503 for (
size_t j = 1; j < n; ++j) {
504 if (arcLength[j] >= targetArc) {
510 double sArc = arcLength[hi] - arcLength[lo];
511 double f = (sArc > 1e-10) ? (targetArc - arcLength[lo]) / sArc : 0.0;
514 origPos[hi], tangents[hi], f));
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic, eOnStorageOrder > MatrixXd
Eigen::Matrix< double, Eigen::Dynamic, 3, eOnStorageOrder > AtomMatrix
The optimizer class is used to serve as an abstract class for all optimizers, as well as to call an o...
const AtomMatrix & getPositions() const
void setPositions(const AtomMatrix &pos)
AtomMatrix getFree() const
long int numberOfAtoms() const
AtomMatrix pbc(const AtomMatrix &diff) const
io::IoStatus con2matter(std::string filename)
const neb_options_t & neb_options() const
const optimizer_options_t & optimizer_options() const
std::unique_ptr< Optimizer > mkOptim(std::shared_ptr< ObjectiveFunction > a_objf, OptType a_otype, const Parameters &a_params)
AtomMatrix cubicInterpolate(const AtomMatrix &P0, const AtomMatrix &T0, const AtomMatrix &P1, const AtomMatrix &T1, double f)
Interpolates positions using a cubic Hermite spline.
std::vector< Matter > resamplePath(const std::vector< Matter > &densePath, size_t targetCount)
std::vector< Matter > sidppPath(const Matter &initImg, const Matter &finalImg, size_t target_nimgs, const Parameters ¶ms, bool use_zbl)
void resamplePathInPlace(std::span< std::shared_ptr< Matter > > path)
In-place path reparameterization for NEB shared_ptr paths.
Matter interpolateImage(const Matter &A, const Matter &B, double fraction)
std::shared_ptr< Potential > createZBLPotential()
std::vector< Matter > linearPath(const Matter &initImg, const Matter &finalImg, const size_t nimgs)
MatrixXd getDistanceMatrix(const Matter &m)
void ensureDistinctAdjacentImages(const std::vector< Matter > &path, double min_sep)
Adjacent images closer than min_sep (RMSD, PBC) are a collapsed path.
void requireSameAtomCount(const Matter &a, const Matter &b, std::string_view what)
Abort before Eigen subtracts two position matrices of different size.
std::vector< Matter > filePathInit(const std::vector< fs::path > &fsrcs, const Matter &refImg, const size_t nimgs)
std::vector< Matter > idppPath(const Matter &initImg, const Matter &finalImg, const size_t nimgs, const Parameters ¶ms, bool use_zbl)
std::vector< Matter > idppCollectivePath(const Matter &initImg, const Matter &finalImg, size_t nimgs, const Parameters ¶ms, bool use_zbl)
std::vector< fs::path > readFilePaths(const std::string &listFilePath)
Reads a file where each line contains a path to another file.
std::unique_ptr< Potential > makePotential(const Parameters ¶ms)
Exclusive ownership.
std::shared_ptr< Potential > sharePotential(std::unique_ptr< Potential > pot)
Multi-owner boundary: one instance observed by several Matters, images, or a binding.
constexpr bool io_ok(IoStatus s) noexcept
quill::Logger * get() noexcept
Get or create the default "combi" logger.
static potential_options_t & potential_options(Parameters &p)
static zbl_options_t & zbl_options(Parameters &p)
struct eonc::neb_options_t::path_initialization_t initialization