Loading...
Searching...
No Matches
eonc::helpers::neb_paths Namespace Reference

Functions

std::shared_ptr< Potential > createZBLPotential ()
std::vector< Matter > linearPath (const Matter &initImg, const Matter &finalImg, const size_t nimgs)
std::vector< Matter > filePathInit (const std::vector< fs::path > &fsrcs, const Matter &refImg, const size_t nimgs)
std::vector< fs::path > readFilePaths (const std::string &listFilePath)
 Reads a file where each line contains a path to another file.
MatrixXd getDistanceMatrix (const Matter &m)
std::vector< Matter > idppPath (const Matter &initImg, const Matter &finalImg, const size_t nimgs, const Parameters &params, bool use_zbl)
std::vector< Matter > idppCollectivePath (const Matter &initImg, const Matter &finalImg, size_t nimgs, const Parameters &params, bool use_zbl)
Matter interpolateImage (const Matter &A, const Matter &B, double fraction)
std::vector< Matter > sidppPath (const Matter &initImg, const Matter &finalImg, size_t target_nimgs, const Parameters &params, bool use_zbl)
void ensureDistinctAdjacentImages (const std::vector< Matter > &path, double min_sep)
 Adjacent images closer than min_sep (RMSD, PBC) are a collapsed path.
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)
void resamplePathInPlace (std::span< std::shared_ptr< Matter > > path)
 In-place path reparameterization for NEB shared_ptr paths.
void requireSameAtomCount (const Matter &a, const Matter &b, std::string_view what)
 Abort before Eigen subtracts two position matrices of different size.

Function Documentation

◆ createZBLPotential()

std::shared_ptr< Potential > eonc::helpers::neb_paths::createZBLPotential ( )

Definition at line 20 of file NEBInitialPaths.cpp.

20 {
21 auto zbl_params = Parameters{};
23 // Strong short-range repulsion
25 // Cutoff sufficient to push overlapping atoms apart
29}
std::unique_ptr< Potential > makePotential(const Parameters &params)
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.
Definition Potential.h:292
static potential_options_t & potential_options(Parameters &p)
static zbl_options_t & zbl_options(Parameters &p)

◆ cubicInterpolate()

AtomMatrix eonc::helpers::neb_paths::cubicInterpolate ( const AtomMatrix & P0,
const AtomMatrix & T0,
const AtomMatrix & P1,
const AtomMatrix & T1,
double f )

Interpolates positions using a cubic Hermite spline.

Parameters
P0Starting positions
T0Tangent at P0 (scaled by segment length)
P1Ending positions
T1Tangent at P1 (scaled by segment length)
fFraction between 0 and 1

Definition at line 358 of file NEBInitialPaths.cpp.

360 {
361 double f2 = f * f;
362 double f3 = f2 * f;
363
364 // Hermite basis functions
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;
369
370 return h00 * P0 + h10 * T0 + h01 * P1 + h11 * T1;
371}

◆ ensureDistinctAdjacentImages()

void eonc::helpers::neb_paths::ensureDistinctAdjacentImages ( const std::vector< Matter > & path,
double min_sep )

Adjacent images closer than min_sep (RMSD, PBC) are a collapsed path.

SIDPP + resample can emit bit-identical intermediates; those starve climbing_image_converged_only.

Definition at line 338 of file NEBInitialPaths.cpp.

339 {
340 if (path.size() < 2) {
341 return;
342 }
343 if (!(min_sep > 0.0)) {
344 throw std::invalid_argument(
345 "NEB path: min adjacent image separation must be positive");
346 }
347 for (size_t i = 1; i < path.size(); ++i) {
348 const AtomMatrix diff =
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)");
354 }
355 }
356}
Eigen::Matrix< double, Eigen::Dynamic, 3, eOnStorageOrder > AtomMatrix
Definition Eigen.h:37

◆ filePathInit()

std::vector< Matter > eonc::helpers::neb_paths::filePathInit ( const std::vector< fs::path > & fsrcs,
const Matter & refImg,
const size_t nimgs )

Definition at line 51 of file NEBInitialPaths.cpp.

52 {
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()) + ".");
58 }
59 all_images_on_path.reserve(nimgs + 2);
60 // For all images
61 for (const auto &filePath : fsrcs) {
62 Matter img(refImg);
63 if (!eonc::io::io_ok(img.con2matter(filePath.string()))) {
64 throw std::runtime_error("failed to load NEB path frame: " +
65 filePath.string());
66 }
67 if (!all_images_on_path.empty()) {
68 requireSameAtomCount(all_images_on_path.front(), img, "path images");
69 }
70 all_images_on_path.push_back(img);
71 }
72 return all_images_on_path;
73}
void requireSameAtomCount(const Matter &a, const Matter &b, std::string_view what)
Abort before Eigen subtracts two position matrices of different size.
constexpr bool io_ok(IoStatus s) noexcept
Definition ConFileIO.h:38

◆ getDistanceMatrix()

MatrixXd eonc::helpers::neb_paths::getDistanceMatrix ( const Matter & m)

Definition at line 95 of file NEBInitialPaths.cpp.

95 {
96 int natoms = m.numberOfAtoms();
97 MatrixXd d(natoms, natoms);
98 AtomMatrix pos = m.getPositions();
99 for (int i = 0; i < natoms; ++i) {
100 for (int j = 0; j < natoms; ++j) {
101 if (i == j) {
102 d(i, j) = 0.0;
103 } else {
104 d(i, j) = m.pbc(pos.row(i) - pos.row(j)).norm();
105 }
106 }
107 }
108 return d;
109}
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic, eOnStorageOrder > MatrixXd
Definition Eigen.h:33
const AtomMatrix & getPositions() const
Definition Matter.cpp:378
long int numberOfAtoms() const
Definition Matter.cpp:343
AtomMatrix pbc(const AtomMatrix &diff) const
Definition Matter.cpp:943

◆ idppCollectivePath()

std::vector< Matter > eonc::helpers::neb_paths::idppCollectivePath ( const Matter & initImg,
const Matter & finalImg,
size_t nimgs,
const Parameters & params,
bool use_zbl )

Definition at line 168 of file NEBInitialPaths.cpp.

170 {
171 auto log = eonc::log::get();
172 QUILL_LOG_INFO(log, "Generating initial path using Collective IDPP-NEB...");
173
174 std::vector<Matter> path = linearPath(initImg, finalImg, nimgs);
175
176 // 1. Base Objective
177 std::shared_ptr<ObjectiveFunction> idpp_objf =
178 std::make_shared<CollectiveIDPPObjectiveFunction>(path, params);
179
180 // 2. Optional ZBL Wrapper
181 if (use_zbl) {
182 QUILL_LOG_INFO(log, "Enabling ZBL repulsive penalty for IDPP...");
183 auto zbl_pot = createZBLPotential();
184 // Wrap the IDPP objective with ZBL repulsion (weight = 1.0)
185 idpp_objf = std::make_shared<ZBLRepulsiveIDPPObjective>(idpp_objf, zbl_pot,
186 path, params, 1.0);
187 }
188
190 idpp_objf, params.neb_options().initialization.opt_method, params);
191
192 int maxSteps = params.neb_options().initialization.max_iterations;
193 int currentStep = 0;
194 int checkInterval = 40;
195
196 while (currentStep < maxSteps) {
197 optim->run(checkInterval, params.optimizer_options().max_move);
198 currentStep += checkInterval;
199
200 if (idpp_objf->isConverged()) {
201 QUILL_LOG_INFO(log,
202 "IDPP-NEB converged after {} steps. Max Residual: {:.4f}",
203 currentStep, idpp_objf->getConvergence());
204 return path;
205 }
206 }
207
208 QUILL_LOG_WARNING(log,
209 "IDPP-NEB reached max_iterations ({}) without full "
210 "convergence. Residual: {:.4f}",
211 maxSteps, idpp_objf->getConvergence());
212 return path;
213}
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)
Definition Optimizer.cpp:55
std::shared_ptr< Potential > createZBLPotential()
std::vector< Matter > linearPath(const Matter &initImg, const Matter &finalImg, const size_t nimgs)
quill::Logger * get() noexcept
Get or create the default "combi" logger.
Definition EonLogger.h:111
struct eonc::neb_options_t::path_initialization_t initialization

◆ idppPath()

std::vector< Matter > eonc::helpers::neb_paths::idppPath ( const Matter & initImg,
const Matter & finalImg,
const size_t nimgs,
const Parameters & params,
bool use_zbl )

Definition at line 111 of file NEBInitialPaths.cpp.

113 {
114
115 auto log = eonc::log::get();
116 QUILL_LOG_INFO(log, "Generating initial path using IDPP...");
117 if (use_zbl) {
118 QUILL_LOG_WARNING(
119 log, "ZBL Repulsion not implemented for iterative IDPP (idppPath). "
120 "Using standard IDPP.");
121 }
122
123 // Start with a linear interpolation to get initial Cartesian coordinates
124 std::vector<Matter> path = linearPath(initImg, finalImg, nimgs);
125
126 // Pre-calculate endpoint distance matrices
127 MatrixXd dInit = getDistanceMatrix(initImg);
128 MatrixXd dFinal = getDistanceMatrix(finalImg);
129
130 // Optimize intermediate images
131 // Note: path[0] and path[nimgs+1] are fixed endpoints
132 for (size_t i = 1; i <= nimgs; ++i) {
133
134 // Calculate the interpolation factor (Reaction Coordinate)
135 double xi = static_cast<double>(i) / (nimgs + 1);
136
137 // Linear interpolation of the distance matrix (The "Image Dependent" part)
138 MatrixXd dTarget = (1.0 - xi) * dInit + xi * dFinal;
139
140 // Create the IDPP Objective Function
141 auto idpp_objf = std::make_shared<IDPPObjectiveFunction>(
142 std::make_shared<Matter>(path[i]), params, dTarget);
143
144 // Create an Optimizer
145 // Defaults to taking the same one as optimizer
146 auto idpp_optim = eonc::helpers::create::mkOptim(
147 idpp_objf, params.neb_options().opt_method, params);
148
149 // Run the optimization
150 int status =
151 idpp_optim->run(params.neb_options().initialization.max_iterations,
153
154 // Log progress
155 double residual = idpp_objf->getConvergence();
156 QUILL_LOG_DEBUG(log,
157 "IDPP Image {:2d}/{:2d} | xi: {:.2f} | Residual: {:.4e}", i,
158 nimgs, xi, residual);
159
160 // Explicitly sync positions back to the path vector just to be safe
161 path[i].setPositionsFreeV(idpp_objf->getPositions());
162 }
163
164 QUILL_LOG_INFO(log, "IDPP path generation complete.");
165 return path;
166}
MatrixXd getDistanceMatrix(const Matter &m)

◆ interpolateImage()

Matter eonc::helpers::neb_paths::interpolateImage ( const Matter & A,
const Matter & B,
double fraction )

Definition at line 216 of file NEBInitialPaths.cpp.

216 {
217 requireSameAtomCount(A, B, "path images");
218 Matter newImg(A);
219 AtomMatrix posA = A.getPositions();
220 AtomMatrix posB = B.getPositions();
221 AtomMatrix diff = A.pbc(posB - posA);
222 diff = diff.array() * A.getFree().array();
223 newImg.setPositions(posA + fraction * diff);
224 return newImg;
225}
AtomMatrix getFree() const
Definition Matter.cpp:799

◆ linearPath()

std::vector< Matter > eonc::helpers::neb_paths::linearPath ( const Matter & initImg,
const Matter & finalImg,
const size_t nimgs )

Definition at line 31 of file NEBInitialPaths.cpp.

32 {
33 requireSameAtomCount(initImg, finalImg, "reactant and product");
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) {
43 *it = Matter(initImg);
44 (*it).setPositions(posInitial +
45 imageSep *
46 int(std::distance(all_images_on_path.begin(), it)));
47 }
48 return all_images_on_path;
49}

◆ readFilePaths()

std::vector< std::filesystem::path > eonc::helpers::neb_paths::readFilePaths ( const std::string & listFilePath)

Reads a file where each line contains a path to another file.

Parameters
listFilePathThe path to the file containing the list of file paths.
Returns
A vector of filesystem paths. Returns an empty vector if the file cannot be opened.

Definition at line 75 of file NEBInitialPaths.cpp.

75 {
76 std::vector<fs::path> paths;
77 std::ifstream inputFile(listFilePath);
78
79 if (!inputFile.is_open()) {
80 throw std::runtime_error("Error: Could not open path list file: " +
81 listFilePath);
82 }
83
84 std::string line;
85 while (std::getline(inputFile, line)) {
86 // Skip any empty lines in the input file
87 if (!line.empty()) {
88 paths.emplace_back(line);
89 }
90 }
91
92 return paths;
93}

◆ requireSameAtomCount()

void eonc::helpers::neb_paths::requireSameAtomCount ( const Matter & a,
const Matter & b,
std::string_view what )
inline

Abort before Eigen subtracts two position matrices of different size.

Definition at line 16 of file NEBInitialPaths.hpp.

17 {
18 if (a.numberOfAtoms() == b.numberOfAtoms()) {
19 return;
20 }
21 throw std::invalid_argument(
22 std::format("NEB: {} do not have the same number of atoms ({} vs {})",
23 what, a.numberOfAtoms(), b.numberOfAtoms()));
24}

◆ resamplePath()

std::vector< Matter > eonc::helpers::neb_paths::resamplePath ( const std::vector< Matter > & densePath,
size_t targetCount )

Definition at line 373 of file NEBInitialPaths.cpp.

374 {
375 if (densePath.size() == targetCount + 2)
376 return densePath;
377
378 size_t n = densePath.size();
379
380 // Arc length and interpolation share a continuous coordinate chart.
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();
389 }
390 double totalLength = arcLength.back();
391
392 // Calculate tangents for cubic interpolation
393 std::vector<AtomMatrix> tangents(n);
394 for (size_t i = 0; i < n; ++i) {
395 AtomMatrix T;
396 if (i == 0) {
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());
402 } else {
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);
408 }
409 // Scale tangent by local segment length for proper spline parameterization
410 if (i > 0 && i < n - 1) {
411 double localScale = (arcLength[i + 1] - arcLength[i - 1]) / 2.0;
412 T = T.normalized() * localScale;
413 }
414 tangents[i] = T;
415 }
416
417 std::vector<Matter> resampled;
418 resampled.reserve(targetCount + 2);
419 resampled.push_back(densePath.front());
420
421 // Place new images at equal arc-length intervals
422 double segmentLength = totalLength / (targetCount + 1);
423
424 for (size_t i = 1; i <= targetCount; ++i) {
425 double targetArc = i * segmentLength;
426
427 // Find the segment containing this arc length
428 size_t lowIdx = 0;
429 for (size_t j = 1; j < n; ++j) {
430 if (arcLength[j] >= targetArc) {
431 lowIdx = j - 1;
432 break;
433 }
434 }
435 size_t highIdx = lowIdx + 1;
436
437 // Interpolation parameter within this segment
438 double segmentArc = arcLength[highIdx] - arcLength[lowIdx];
439 double f = (segmentArc > 1e-10)
440 ? (targetArc - arcLength[lowIdx]) / segmentArc
441 : 0.0;
442
443 Matter newImg(densePath[0]);
444 newImg.setPositions(cubicInterpolate(origPos[lowIdx], tangents[lowIdx],
445 origPos[highIdx], tangents[highIdx],
446 f));
447 resampled.push_back(newImg);
448 }
449
450 resampled.push_back(densePath.back());
451 return resampled;
452}
AtomMatrix cubicInterpolate(const AtomMatrix &P0, const AtomMatrix &T0, const AtomMatrix &P1, const AtomMatrix &T1, double f)
Interpolates positions using a cubic Hermite spline.

◆ resamplePathInPlace()

void eonc::helpers::neb_paths::resamplePathInPlace ( std::span< std::shared_ptr< Matter > > path)

In-place path reparameterization for NEB shared_ptr paths.

Redistributes interior images at equal arc-length intervals using cubic Hermite interpolation, without allocating new Matter objects.

Parameters
pathThe FULL path including endpoints. path[0] and path[n-1] are treated as fixed endpoints and are never modified. Interior images path[1] through path[n-2] are repositioned. Pass the entire NEB path vector, not a sub-span of intermediate images.

Definition at line 454 of file NEBInitialPaths.cpp.

454 {
455 size_t n = path.size();
456 if (n < 3)
457 return;
458
459 // Interpolation and arc length use the same continuous coordinate chart.
460 // Wrapped endpoints of an interior segment must not span the whole cell.
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) {
465 AtomMatrix diff =
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();
469 }
470 double totalLength = arcLength.back();
471 if (totalLength < 1e-12)
472 return;
473
474 // Tangents for cubic interpolation
475 std::vector<AtomMatrix> tangents(n);
476 for (size_t i = 0; i < n; ++i) {
477 AtomMatrix T;
478 if (i == 0) {
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());
482 } else {
483 AtomMatrix dN =
484 path[i]->pbc(path[i + 1]->getPositions() - path[i]->getPositions());
485 AtomMatrix dP =
486 path[i]->pbc(path[i]->getPositions() - path[i - 1]->getPositions());
487 T = 0.5 * (dN + dP);
488 }
489 if (i > 0 && i < n - 1) {
490 double localScale = (arcLength[i + 1] - arcLength[i - 1]) / 2.0;
491 T = T.normalized() * localScale;
492 }
493 tangents[i] = T;
494 }
495
496 // Redistribute interior images at equal arc-length intervals (in-place)
497 size_t nInterior = n - 2;
498 double segLen = totalLength / (nInterior + 1);
499
500 for (size_t i = 1; i <= nInterior; ++i) {
501 double targetArc = i * segLen;
502 size_t lo = 0;
503 for (size_t j = 1; j < n; ++j) {
504 if (arcLength[j] >= targetArc) {
505 lo = j - 1;
506 break;
507 }
508 }
509 size_t hi = lo + 1;
510 double sArc = arcLength[hi] - arcLength[lo];
511 double f = (sArc > 1e-10) ? (targetArc - arcLength[lo]) / sArc : 0.0;
512
513 path[i]->setPositions(cubicInterpolate(origPos[lo], tangents[lo],
514 origPos[hi], tangents[hi], f));
515 }
516}

◆ sidppPath()

std::vector< Matter > eonc::helpers::neb_paths::sidppPath ( const Matter & initImg,
const Matter & finalImg,
size_t target_nimgs,
const Parameters & params,
bool use_zbl )

Definition at line 227 of file NEBInitialPaths.cpp.

229 {
230
231 auto log = eonc::log::get();
232 const auto &init = params.neb_options().initialization;
233 QUILL_LOG_INFO(log,
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);
238
239 // 1. Start with endpoints [Reactant, Product]
240 std::vector<Matter> path;
241 path.push_back(initImg);
242 path.push_back(finalImg);
243
244 std::shared_ptr<Potential> zbl_pot = nullptr;
245 if (use_zbl) {
246 zbl_pot = createZBLPotential();
247 }
248
249 // Track frontier counts: nLeft images from reactant, nRight from product
250 int nLeft = 0;
251 int nRight = 0;
252 int nIntermediate = 0;
253 bool addToLeft = true; // Alternate L/R, starting with left
254
255 // Helper: create IDPP objective with optional ZBL wrapping
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,
260 params, 1.0);
261 }
262 return objf;
263 };
264
265 // Helper: relax current path on IDPP surface
266 auto relaxPath = [&](int maxSteps) -> double {
267 auto objf = makeIDPP();
268 auto optim = eonc::helpers::create::mkOptim(objf, init.opt_method, params);
269 int step = 0;
270 while (step < maxSteps) {
271 optim->run(5, init.max_move);
272 step += 5;
273 if (objf->isConverged())
274 break;
275 }
276 return objf->getConvergence();
277 };
278
279 // 2. Sequential growth loop: alternate adding images from L and R
280 while (nIntermediate < static_cast<int>(target_nimgs)) {
281
282 if (addToLeft && nIntermediate < static_cast<int>(target_nimgs)) {
283 // Add to left (reactant) frontier
284 Matter &frontier = path[nLeft];
285 Matter &next = path[nLeft + 1];
286 Matter newImg = interpolateImage(frontier, next, init.sidpp_alpha);
287 path.insert(path.begin() + nLeft + 1, newImg);
288 nLeft++;
289 nIntermediate++;
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)) {
293 // Add to right (product) frontier
294 int rightIdx = static_cast<int>(path.size()) - 1 - nRight;
295 Matter &frontier = path[rightIdx];
296 Matter &prev = path[rightIdx - 1];
297 Matter newImg = interpolateImage(frontier, prev, init.sidpp_alpha);
298 path.insert(path.begin() + rightIdx, newImg);
299 nRight++;
300 nIntermediate++;
301 QUILL_LOG_DEBUG(log, "S-IDPP: +R frontier (nL={}, nR={}, total={})",
302 nLeft, nRight, nIntermediate);
303 }
304 addToLeft = !addToLeft; // Alternate sides
305
306 // Optimize current path on IDPP surface
307 double residual = relaxPath(init.nsteps);
308
309 // Frontier convergence gating: if not converged, keep relaxing
310 // before adding more images (up to max_iterations total)
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;
316 }
317
318 QUILL_LOG_DEBUG(log, "S-IDPP: {} images | Residual: {:.4f}", nIntermediate,
319 residual);
320 }
321
322 // 3. Reparameterize: redistribute images evenly along arc length
323 if (init.sidpp_reparam && path.size() > 3) {
324 QUILL_LOG_INFO(log, "S-IDPP: Reparameterizing {} images along arc length",
325 path.size() - 2);
326 path = resamplePath(path, target_nimgs);
327 }
328
329 // 4. Final relaxation of the complete path
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);
333
334 ensureDistinctAdjacentImages(path, 1.0e-6);
335 return path;
336}
std::vector< Matter > resamplePath(const std::vector< Matter > &densePath, size_t targetCount)
Matter interpolateImage(const Matter &A, const Matter &B, double fraction)
void ensureDistinctAdjacentImages(const std::vector< Matter > &path, double min_sep)
Adjacent images closer than min_sep (RMSD, PBC) are a collapsed path.