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

Functions

MatrixXd get_features (const std::vector< Matter > &matobjs)
MatrixXd get_features (const std::vector< std::shared_ptr< Matter > > &matobjs)
MatrixXd get_targets (std::vector< Matter > &matobjs, std::shared_ptr< Potential > true_pot)
MatrixXd get_targets (std::vector< std::shared_ptr< Matter > > &matobjs, std::shared_ptr< Potential > true_pot)
std::vector< MattergetMidSlice (const std::vector< Matter > &matobjs)
Eigen::VectorXd make_target (Matter &m1, std::shared_ptr< Potential > true_pot)
std::pair< double, Eigen::VectorXd::Index > getMaxUncertainty (const std::vector< std::shared_ptr< Matter > > &matobjs)
std::pair< Eigen::VectorXd, Eigen::VectorXd > getNewDataPoint (const std::vector< std::shared_ptr< Matter > > &matobjs, std::shared_ptr< Potential > true_pot)
bool accuratePES (std::vector< std::shared_ptr< Matter > > &matobjs, std::shared_ptr< Potential > true_pot)

Function Documentation

◆ accuratePES()

bool eonc::helpers::surrogate::accuratePES ( std::vector< std::shared_ptr< Matter > > & matobjs,
std::shared_ptr< Potential > true_pot )

Definition at line 293 of file GPSurrogateJob.cpp.

294 {
295 Eigen::VectorXd predEnergies{Eigen::VectorXd::Zero(matobjs.size())};
296 Eigen::VectorXd trueEnergies{Eigen::VectorXd::Zero(matobjs.size())};
297 Eigen::VectorXd accuracy{Eigen::VectorXd::Zero(matobjs.size())};
298 for (auto idx{0}; idx < predEnergies.size(); idx++) {
299 predEnergies[idx] = matobjs[idx]->getPotentialEnergy();
300 matobjs[idx]->setPotential(true_pot);
301 trueEnergies[idx] = matobjs[idx]->getPotentialEnergy();
302
303 accuracy[idx] = std::sqrt(predEnergies[idx] * predEnergies[idx] -
304 trueEnergies[idx] * trueEnergies[idx]);
305 }
306 Eigen::VectorXd difference = predEnergies - trueEnergies;
307 auto mae = difference.array()
308 .abs()
309 .maxCoeff(); //.squaredNorm() / predEnergies.size();
310 std::ostringstream oss;
311 oss << "predicted\n"
312 << predEnergies << "\ntrue\n"
313 << trueEnergies << "\ndifference\n"
314 << difference << "\n MAE: " << mae;
315 EONC_LOG_TRACE("{}", oss.str());
316 return mae < 0.05;
317}
#define EONC_LOG_TRACE(...)
Convenience macro for one-shot logging without storing a logger.
Definition EonLogger.h:238

◆ get_features() [1/2]

MatrixXd eonc::helpers::surrogate::get_features ( const std::vector< Matter > & matobjs)

Definition at line 188 of file GPSurrogateJob.cpp.

188 {
189 // Calculate dimensions
190 MatrixXd features(matobjs.size(), matobjs.front().numberOfFreeAtoms() * 3);
191 EONC_LOG_TRACE("rows: {}, cols:{}", matobjs.size(),
192 matobjs.front().numberOfFreeAtoms() * 3);
193 for (long idx{0}; idx < features.rows(); idx++) {
194 features.row(idx) = matobjs[idx].getPositionsFreeV();
195 }
196 std::ostringstream oss;
197 oss << features;
198 EONC_LOG_TRACE("Features\n:{}", oss.str());
199 return features;
200}
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic, eOnStorageOrder > MatrixXd
Definition Eigen.h:33

◆ get_features() [2/2]

MatrixXd eonc::helpers::surrogate::get_features ( const std::vector< std::shared_ptr< Matter > > & matobjs)

Definition at line 201 of file GPSurrogateJob.cpp.

201 {
202 // Calculate dimensions
203 MatrixXd features(matobjs.size(), matobjs.front()->numberOfFreeAtoms() * 3);
204 EONC_LOG_TRACE("rows: {}, cols:{}\n", matobjs.size(),
205 matobjs.front()->numberOfFreeAtoms() * 3);
206 for (long idx{0}; idx < features.rows(); idx++) {
207 features.row(idx) = matobjs[idx]->getPositionsFreeV();
208 }
209 std::ostringstream oss;
210 oss << features;
211 EONC_LOG_TRACE("Features\n:{}", oss.str());
212 return features;
213}

◆ get_targets() [1/2]

MatrixXd eonc::helpers::surrogate::get_targets ( std::vector< Matter > & matobjs,
std::shared_ptr< Potential > true_pot )

Definition at line 214 of file GPSurrogateJob.cpp.

215 {
216 // Always with derivatives for now
217 // Energy + Derivatives for each row
218 const auto nrows = matobjs.size();
219 const auto ncols = (matobjs.front().numberOfFreeAtoms() * 3) + 1;
220 MatrixXd targets(nrows, ncols);
221 for (long idx{0}; idx < targets.rows(); idx++) {
222 matobjs[idx].setPotential(true_pot);
223 targets.row(idx)[0] = matobjs[idx].getPotentialEnergy();
224 targets.block(idx, 1, 1, ncols - 1) =
225 matobjs[idx].getForcesFree().array() * -1;
226 }
227 std::ostringstream oss;
228 oss << targets;
229 EONC_LOG_TRACE("Targets\n:{}", oss.str());
230 return targets;
231}

◆ get_targets() [2/2]

MatrixXd eonc::helpers::surrogate::get_targets ( std::vector< std::shared_ptr< Matter > > & matobjs,
std::shared_ptr< Potential > true_pot )

Definition at line 232 of file GPSurrogateJob.cpp.

233 {
234 const auto nrows = matobjs.size();
235 const auto ncols = (matobjs.front()->numberOfFreeAtoms() * 3) + 1;
236 MatrixXd targets(nrows, ncols);
237 for (long idx{0}; idx < targets.rows(); idx++) {
238 matobjs[idx]->setPotential(true_pot);
239 targets.row(idx)[0] = matobjs[idx]->getPotentialEnergy();
240 targets.block(idx, 1, 1, ncols - 1) =
241 matobjs[idx]->getForcesFree().array() * -1;
242 }
243 std::ostringstream oss;
244 oss << targets;
245 EONC_LOG_TRACE("Targets\n:{}", oss.str());
246 return targets;
247}

◆ getMaxUncertainty()

std::pair< double, Eigen::VectorXd::Index > eonc::helpers::surrogate::getMaxUncertainty ( const std::vector< std::shared_ptr< Matter > > & matobjs)

Definition at line 272 of file GPSurrogateJob.cpp.

272 {
273 Eigen::VectorXd pathUncertainty{Eigen::VectorXd::Zero(matobjs.size() - 2)};
274 for (auto idx{0}; idx < pathUncertainty.size(); idx++) {
275 pathUncertainty[idx] = matobjs[idx + 1]->getEnergyVariance();
276 }
277 Eigen::VectorXd::Index maxIndex;
278 double maxUnc{pathUncertainty.maxCoeff()};
279 pathUncertainty.maxCoeff(&maxIndex);
280 // EONC_LOG_TRACE("Uncertainty along path
281 // is {}\nmax_index: {}, maxVal: {}",
282 // fmt::streamed(pathUncertainty), maxIndex, maxUnc);
283 return std::make_pair(maxUnc, maxIndex);
284}

◆ getMidSlice()

std::vector< Matter > eonc::helpers::surrogate::getMidSlice ( const std::vector< Matter > & matobjs)

Definition at line 248 of file GPSurrogateJob.cpp.

248 {
249 // Used to get the initial data slice, endpoints and the midpoint
250 std::vector<Matter> res;
251 res.reserve(3);
252 res.push_back(matobjs.front());
253 // BUG: THIS ISN'T THE MIDDLE!!!!
254 // XXX: Why does this have to be in the same order?
255 // front mid back doesn't work
256 // front back mid works
257 res.push_back(matobjs.back());
258 res.push_back(matobjs[((matobjs.size() - 2) * 2.0 / 3.0) + 1]);
259 return res;
260}

◆ getNewDataPoint()

std::pair< Eigen::VectorXd, Eigen::VectorXd > eonc::helpers::surrogate::getNewDataPoint ( const std::vector< std::shared_ptr< Matter > > & matobjs,
std::shared_ptr< Potential > true_pot )

Definition at line 286 of file GPSurrogateJob.cpp.

287 {
288 auto [maxUnc, maxIndex] = getMaxUncertainty(matobjs);
289 Matter candidate{*matobjs[maxIndex + 1]};
290 return std::make_pair<Eigen::VectorXd, Eigen::VectorXd>(
291 candidate.getPositionsFreeV(), make_target(candidate, true_pot));
292}
VectorXd getPositionsFreeV() const
Definition Matter.cpp:268
std::pair< double, Eigen::VectorXd::Index > getMaxUncertainty(const std::vector< std::shared_ptr< Matter > > &matobjs)
Eigen::VectorXd make_target(Matter &m1, std::shared_ptr< Potential > true_pot)

◆ make_target()

Eigen::VectorXd eonc::helpers::surrogate::make_target ( Matter & m1,
std::shared_ptr< Potential > true_pot )

Definition at line 261 of file GPSurrogateJob.cpp.

261 {
262 const auto ncols = (m1.numberOfFreeAtoms() * 3) + 1;
263 Eigen::VectorXd target(ncols);
264 m1.setPotential(true_pot);
265 target(0) = m1.getPotentialEnergy();
266 target.segment(1, ncols - 1) = m1.getForcesFreeV() * -1;
267 // EONC_LOG_TRACE("Generated Target:\n{}",
268 // fmt::streamed(target));
269 return target;
270}
double getPotentialEnergy() const
Definition Matter.cpp:446
VectorXd getForcesFreeV() const
Definition Matter.cpp:354
long int numberOfFreeAtoms() const
Definition Matter.cpp:475
void setPotential(std::shared_ptr< Potential > pot)
Definition Matter.cpp:625