Loading...
Searching...
No Matches
eonc::GPSurrogateJob Class Reference

#include <GPSurrogateJob.h>

Inheritance diagram for eonc::GPSurrogateJob:

Public Member Functions

 GPSurrogateJob (std::unique_ptr< Parameters > parameters, Runtime &rt)
 GPSurrogateJob (std::shared_ptr< Potential > potPassed, const Parameters &parameters)
 ~GPSurrogateJob ()=default
std::vector< std::string > run () override
 Virtual run; used solely for dynamic dispatch.
std::shared_ptr< NudgedElasticBand > runFromMatter (std::shared_ptr< Matter > initial, std::shared_ptr< Matter > final_state)
 Matter-first NEB surrogate path (endpoints as Matter).
Public Member Functions inherited from eonc::Job
void adoptRuntime (std::unique_ptr< Runtime > rt)
 Take ownership of a Runtime previously passed as Runtime&.
 Job (std::unique_ptr< Parameters > parameters, Runtime &rt)
 Borrow: caller keeps Runtime alive (CLI stack / Python Session).
 Job (std::unique_ptr< Parameters > parameters, std::unique_ptr< Runtime > rt)
 Own a Runtime (one-shot makeJob / rvalue).
 Job (std::unique_ptr< Parameters > parameters)
 Own a default-constructed Runtime.
 Job (std::shared_ptr< Potential > potPassed, const Parameters &parameters)
virtual ~Job ()=default
JobType getType ()
PotRegistry & pots () noexcept
void releasePotential ()
 Drop the Potential so on_destroyed is recorded before Runtime dies.

Private Member Functions

void saveData (NudgedElasticBand::NEBStatus status, std::unique_ptr< NudgedElasticBand > neb)

Private Attributes

std::vector< std::string > returnFiles

Additional Inherited Members

Protected Attributes inherited from eonc::Job
JobType jtype
Parameters params
std::unique_ptr< Runtime > owned_runtime_
 Non-null when this Job owns the composition root (one-shot makeJob).
Runtime * runtime_
 Always valid: either owned_runtime_.get() or a caller-owned Runtime.
std::shared_ptr< Potential > pot

Detailed Description

Definition at line 26 of file GPSurrogateJob.h.

Constructor & Destructor Documentation

◆ GPSurrogateJob() [1/2]

eonc::GPSurrogateJob::GPSurrogateJob ( std::unique_ptr< Parameters > parameters,
Runtime & rt )
inline

Definition at line 28 of file GPSurrogateJob.h.

29 : Job(std::move(parameters), rt) {
31#ifndef NDEBUG
32 py::module_ sys_mod = py::module_::import("sys");
33 py::module_ ipdb_mod = py::module_::import("ipdb");
34 sys_mod.attr("breakpointhook") = ipdb_mod.attr("set_trace");
35#endif // NDEBUG
36 }
Job(std::unique_ptr< Parameters > parameters, Runtime &rt)
Borrow: caller keeps Runtime alive (CLI stack / Python Session).
Definition Job.h:76
void ensure_interpreter()
Definition NbGuard.h:21

◆ GPSurrogateJob() [2/2]

eonc::GPSurrogateJob::GPSurrogateJob ( std::shared_ptr< Potential > potPassed,
const Parameters & parameters )
inline

Definition at line 37 of file GPSurrogateJob.h.

39 : Job(potPassed, parameters) {
41#ifndef NDEBUG
42 py::module_ sys_mod = py::module_::import("sys");
43 py::module_ ipdb_mod = py::module_::import("ipdb");
44 sys_mod.attr("breakpointhook") = ipdb_mod.attr("set_trace");
45#endif // NDEBUG
46 }

◆ ~GPSurrogateJob()

eonc::GPSurrogateJob::~GPSurrogateJob ( )
default

Member Function Documentation

◆ run()

std::vector< std::string > eonc::GPSurrogateJob::run ( )
overridevirtual

Virtual run; used solely for dynamic dispatch.

Implements eonc::Job.

Definition at line 29 of file GPSurrogateJob.cpp.

29 {
30 std::string reactantFilename = eonc::helpers::getRelevantFile("reactant.con");
31 std::string productFilename = eonc::helpers::getRelevantFile("product.con");
32 auto true_params = std::make_shared<Parameters>(params);
34 params.gp_surrogate_options().sub_job;
35 auto initial = std::make_shared<Matter>(pot, *true_params);
36 if (!eonc::io::io_ok(initial->con2matter(reactantFilename))) {
37 EONC_LOG_CRITICAL("Failed to load {}", reactantFilename);
38 throw std::runtime_error("failed to load " + reactantFilename);
39 }
40 auto final_state = std::make_shared<Matter>(pot, *true_params);
41 if (!eonc::io::io_ok(final_state->con2matter(productFilename))) {
42 EONC_LOG_CRITICAL("Failed to load {}", productFilename);
43 throw std::runtime_error("failed to load " + productFilename);
44 }
45 (void)runFromMatter(initial, final_state);
46 return returnFiles;
47}
#define EONC_LOG_CRITICAL(...)
Definition EonLogger.h:267
std::shared_ptr< NudgedElasticBand > runFromMatter(std::shared_ptr< Matter > initial, std::shared_ptr< Matter > final_state)
Matter-first NEB surrogate path (endpoints as Matter).
std::vector< std::string > returnFiles
std::shared_ptr< Potential > pot
Definition Job.h:63
Parameters params
Definition Job.h:58
std::string getRelevantFile(std::string filename)
constexpr bool io_ok(IoStatus s) noexcept
Definition ConFileIO.h:38
static main_options_t & main_options(Parameters &p)

◆ runFromMatter()

std::shared_ptr< NudgedElasticBand > eonc::GPSurrogateJob::runFromMatter ( std::shared_ptr< Matter > initial,
std::shared_ptr< Matter > final_state )

Matter-first NEB surrogate path (endpoints as Matter).

Definition at line 50 of file GPSurrogateJob.cpp.

51 {
52 if (!initial || !final_state) {
53 throw std::runtime_error("GPSurrogateJob::runFromMatter: null Matter");
54 }
55 // Clone and setup "true" params
56 auto true_params = std::make_shared<Parameters>(params);
58 params.gp_surrogate_options().sub_job;
59 auto true_job = eonc::helpers::makeJob(
60 std::make_unique<Parameters>(*true_params), *runtime_);
61 auto pyparams = std::make_shared<Parameters>(params);
64
65 initial->setPotential(pot);
66 final_state->setPotential(pot);
68 *initial, *final_state, params.neb_options().image_count);
69 auto init_data = eonc::helpers::surrogate::getMidSlice(init_path);
70 auto features = eonc::helpers::surrogate::get_features(init_data);
71 EONC_LOG_TRACE("Potential is {}",
72 magic_enum::enum_name<PotType>(pot->getType()));
73 auto targets = eonc::helpers::surrogate::get_targets(init_data, pot);
74
75 // Setup a GPR Potential
77 params.gp_surrogate_options().potential, params);
78 surpot->train_optimize(features, targets);
79 auto neb = std::make_unique<NudgedElasticBand>(initial, final_state,
80 *pyparams, surpot);
81 auto status_neb{neb->compute()};
82 bool job_not_finished{true};
83 size_t n_gp{0};
84 while (job_not_finished) { // outer loop?
85 n_gp++;
86 if (n_gp > 750) {
87 EONC_LOG_CRITICAL("Whoops, power level of problem too high!!");
88 break;
89 }
90 EONC_LOG_TRACE("Must handle update to the GP, update number {}", n_gp);
91 auto [maxUnc, maxIndex] =
93 auto [feature, target] =
95 eonc::helpers::eigen::addVectorRow(features, feature);
97 surpot->train_optimize(features, targets);
100 params.optimizer_options().converged_force * 0.8;
101 for (auto &&obj : neb->path) {
102 obj->setPotential(surpot);
103 }
104 if (!(pyparams->gp_surrogate_options().linear_path_always)) {
105 EONC_LOG_TRACE("Using previous path");
106 std::vector<Matter> previous;
107 previous.reserve(neb->path.size());
108 for (const auto &image : neb->path) {
109 previous.push_back(*image);
110 }
111 neb = std::make_unique<NudgedElasticBand>(std::move(previous), *pyparams,
112 surpot);
113 } else {
114 EONC_LOG_TRACE("Using linear interpolation");
115 neb = std::make_unique<NudgedElasticBand>(initial, final_state, *pyparams,
116 surpot);
117 }
118 status_neb = neb->compute();
119
120 std::string nebFilename(std::format("neb_final_gpr_{:03d}.con", n_gp));
121 returnFiles.push_back(nebFilename);
123 neb->path, neb->tangent, neb->eigenmode_solvers, neb->numImages,
124 params.debug_options().estimate_neb_eigenvalues, nebFilename,
125 static_cast<size_t>(n_gp), neb->reactantEnergy))) {
126 throw std::runtime_error("Failed to write file: " + nebFilename);
127 }
128 if (status_neb == NudgedElasticBand::NEBStatus::GOOD &&
130 break;
131 } else {
132 continue;
133 }
134 }
135 neb->printImageData();
136 neb->findExtrema();
137 // Keep a shared view for the caller before saveData takes ownership of unique
138 std::shared_ptr<NudgedElasticBand> out(neb.release());
139 // saveData expects unique_ptr - rebuild unique from shared is unsafe.
140 // Write results without consuming: call saveData on a temporary unique wrap
141 // fails. Instead write via a clone path: only return the band; file artifacts
142 // optional.
143 return out;
144}
#define EONC_LOG_TRACE(...)
Convenience macro for one-shot logging without storing a logger.
Definition EonLogger.h:237
Runtime * runtime_
Always valid: either owned_runtime_.get() or a caller-owned Runtime.
Definition Job.h:62
void addVectorRow(MatrixXd &data, const Eigen::VectorXd &newrow)
std::vector< Matter > linearPath(const Matter &initImg, const Matter &finalImg, const size_t nimgs)
MatrixXd get_targets(std::vector< Matter > &matobjs, std::shared_ptr< Potential > true_pot)
MatrixXd get_features(const std::vector< Matter > &matobjs)
bool accuratePES(std::vector< std::shared_ptr< Matter > > &matobjs, std::shared_ptr< Potential > true_pot)
std::pair< double, Eigen::VectorXd::Index > getMaxUncertainty(const std::vector< std::shared_ptr< Matter > > &matobjs)
std::vector< Matter > getMidSlice(const std::vector< Matter > &matobjs)
std::pair< Eigen::VectorXd, Eigen::VectorXd > getNewDataPoint(const std::vector< std::shared_ptr< Matter > > &matobjs, std::shared_ptr< Potential > true_pot)
std::unique_ptr< Job > makeJob(std::unique_ptr< Parameters > params, Runtime &runtime)
Borrow: caller keeps Runtime alive.
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)
Write a NEB band as a multi-frame .con via readcon ConFrameBuilder::clone().
std::shared_ptr< eonc::SurrogatePotential > makeSurrogatePotential(eonc::PotType a_ptype, const eonc::Parameters &a_params, Args &&...a_args)
Definition Create.hpp:22
static neb_options_t & neb_options(Parameters &p)
static optimizer_options_t & optimizer_options(Parameters &p)
static potential_options_t & potential_options(Parameters &p)
struct eonc::neb_options_t::climbing_image_options_t climbing_image

◆ saveData()

void eonc::GPSurrogateJob::saveData ( NudgedElasticBand::NEBStatus status,
std::unique_ptr< NudgedElasticBand > neb )
private

Definition at line 146 of file GPSurrogateJob.cpp.

147 {
148 std::string resultsFilename = "results.dat";
149 returnFiles.push_back(resultsFilename);
150
151 std::ofstream fileResults(resultsFilename);
152 if (!fileResults) {
153 // Handle file open error
154 throw std::runtime_error("Failed to open file: " + resultsFilename);
155 }
156
157 fileResults << static_cast<int>(status) << " termination_reason\n";
158 fileResults << magic_enum::enum_name(status) << " termination_reason_text\n";
159 fileResults << magic_enum::enum_name<PotType>(
160 params.potential_options().potential)
161 << " potential_type\n";
162 fileResults << std::format("{:.6f} energy_reference\n", neb->reactantEnergy);
163 fileResults << neb->numImages << " number_of_images\n";
164
165 for (long i = 0; i <= neb->numImages + 1; i++) {
166 fileResults << std::format(
167 "{:.6f} image{}_energy\n",
168 neb->path[i]->getPotentialEnergy() - neb->reactantEnergy, i);
169 fileResults << std::format("{:.6f} image{}_force\n",
170 neb->path[i]->getForces().norm(), i);
171 fileResults << std::format("{:.6f} image{}_projected_force\n",
172 neb->projectedForce[i]->norm(), i);
173 }
174
175 fileResults << neb->numExtrema << " number_of_extrema\n";
176 for (long i = 0; i < neb->numExtrema; i++) {
177 fileResults << std::format("{:.6f} extremum{}_position\n",
178 neb->extremumPosition[i], i);
179 fileResults << std::format("{:.6f} extremum{}_energy\n",
180 neb->extremumEnergy[i], i);
181 }
182
183 fileResults.close();
184
185 std::string nebFilename = "neb.con";
186 returnFiles.push_back(nebFilename);
187
189 neb->path, neb->tangent, neb->eigenmode_solvers, neb->numImages,
190 params.debug_options().estimate_neb_eigenvalues, nebFilename,
191 std::nullopt, neb->reactantEnergy))) {
192 throw std::runtime_error("Failed to write file: " + nebFilename);
193 }
194
195 returnFiles.push_back("neb.dat");
196 neb->printImageData(true);
197}

Member Data Documentation

◆ returnFiles

std::vector<std::string> eonc::GPSurrogateJob::returnFiles
private

Definition at line 57 of file GPSurrogateJob.h.


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