Loading...
Searching...
No Matches
MinimizationJob.cpp
Go to the documentation of this file.
1/*
2** This file is part of eOn.
3**
4** SPDX-License-Identifier: BSD-3-Clause
5**
6** Copyright (c) 2010--present, eOn Development Team
7** All rights reserved.
8**
9** Repo:
10** https://github.com/TheochemUI/eOn
11*/
12#include "eon/MinimizationJob.h"
13#include "eon/BaseStructures.h"
14#include "eon/HelperFunctions.h"
15#include "eon/JobResult.h"
16#include "eon/Matter.h"
17#include "eon/Optimizer.h"
18#include "eon/XtsciEindir.h"
19
20#include <filesystem>
21#include <format>
22#include <fstream>
23#include <iostream>
24#include <stdexcept>
25#include <string>
26
27namespace eonc {
28
29std::vector<std::string> MinimizationJob::run() {
30 std::string posInFilename("pos.con");
31 std::string posOutFilename("min.con");
32
33 if (params.main_options().checkpoint) {
34 if (std::filesystem::exists("pos_cp.con")) {
35 posInFilename = "pos_cp.con";
36 QUILL_LOG_DEBUG(log, "[Minimization] Resuming from checkpoint");
37 } else {
38 QUILL_LOG_DEBUG(log, "[Minimization] No checkpoint files found");
39 }
40 }
41
42 std::vector<std::string> returnFiles;
43 returnFiles.push_back(posOutFilename);
44
45 auto pos = std::make_shared<Matter>(pot, params);
46 if (!eonc::io::io_ok(pos->con2matter(posInFilename))) {
47 QUILL_LOG_CRITICAL(log, "Failed to load {}", posInFilename);
48 throw std::runtime_error("failed to load " + posInFilename);
49 }
50
51 QUILL_LOG_DEBUG(log, "\nBeginning minimization of {}", posInFilename);
52
53 bool converged;
54 try {
55 converged =
56 pos->relax(false, params.debug_options().write_movies,
57 params.main_options().checkpoint, "minimization", "pos");
58 if (converged) {
60 QUILL_LOG_DEBUG(log, "Minimization converged within tolerence");
61 } else {
63 QUILL_LOG_DEBUG(log, "Minimization did not converge to tolerence!"
64 "Maybe try to increase max_iterations?");
65 }
66 } catch (int e) {
67 if (e == 100) {
69 } else {
70 throw e;
71 }
72 } catch (const std::exception &e) {
73 QUILL_LOG_ERROR(log, "Minimization potential failed: {}", e.what());
75 }
76
77 QUILL_LOG_DEBUG(log, "Saving result to {}", posOutFilename);
78 if (!eonc::io::io_ok(pos->matter2con(posOutFilename))) {
79 QUILL_LOG_ERROR(log, "Failed to write {}", posOutFilename);
80 }
82 QUILL_LOG_DEBUG(log, "Final Energy: {}", pos->getPotentialEnergy());
83 }
84
85 std::filesystem::path resultsFilename("results.dat");
86 returnFiles.push_back(resultsFilename.string());
87
88 const bool hasE = status != RunStatus::FAIL_POTENTIAL_FAILED;
89 const double energy = hasE ? pos->getPotentialEnergy() : 0.0;
91 status, params.potential_options().potential,
92 this->pot->forceCallCounter.load(), hasE, energy);
93 env.tags.emplace_back("optimizer", std::string(magic_enum::enum_name(
94 params.optimizer_options().method)));
95 if (params.optimizer_options().method == OptType::XTSCI) {
96 env.tags.emplace_back("xtsci_method",
97 params.optimizer_options().xtsci.method);
98 }
99 if (!xtsci_eindir::provenance().empty()) {
100 env.tags.emplace_back("eindir_abi", xtsci_eindir::provenance());
101 }
102 env.writeResultsDat(resultsFilename.string());
103
104 return returnFiles;
105}
106
107} // namespace eonc
The optimizer class is used to serve as an abstract class for all optimizers, as well as to call an o...
std::shared_ptr< Potential > pot
Definition Job.h:63
Parameters params
Definition Job.h:58
std::vector< std::string > run(void) override
Virtual run; used solely for dynamic dispatch.
eonc::log::Scoped log
constexpr bool io_ok(IoStatus s) noexcept
Definition ConFileIO.h:38
const std::string & provenance()
Stamp text from the last successful eindir bind. Empty when unused.
RAII resource manager for the ARTn C library with global synchronization.
static JobResultEnvelope fromMinimization(RunStatus status, PotType pot, std::uint64_t fcalls, bool hasE, double energy)
Definition JobResult.h:101