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/Matter.h"
16#include "eon/Optimizer.h"
17
18#include <filesystem>
19#include <format>
20#include <fstream>
21#include <iostream>
22#include <stdexcept>
23
24std::vector<std::string> MinimizationJob::run() {
25 std::string posInFilename("pos.con");
26 std::string posOutFilename("min.con");
27
28 if (params.main_options.checkpoint) {
29 if (std::filesystem::exists("pos_cp.con")) {
30 posInFilename = "pos_cp.con";
31 QUILL_LOG_DEBUG(log, "[Minimization] Resuming from checkpoint");
32 } else {
33 QUILL_LOG_DEBUG(log, "[Minimization] No checkpoint files found");
34 }
35 }
36
37 std::vector<std::string> returnFiles;
38 returnFiles.push_back(posOutFilename);
39
40 auto pos = std::make_shared<Matter>(pot, params);
41 if (!eonc::io::io_ok(pos->con2matter(posInFilename))) {
42 QUILL_LOG_CRITICAL(log, "Failed to load {}", posInFilename);
43 throw std::runtime_error("failed to load " + posInFilename);
44 }
45
46 QUILL_LOG_DEBUG(log, "\nBeginning minimization of {}", posInFilename);
47
48 bool converged;
49 try {
50 converged =
51 pos->relax(false, params.debug_options.write_movies,
52 params.main_options.checkpoint, "minimization", "pos");
53 if (converged) {
55 QUILL_LOG_DEBUG(log, "Minimization converged within tolerence");
56 } else {
58 QUILL_LOG_DEBUG(log, "Minimization did not converge to tolerence!"
59 "Maybe try to increase max_iterations?");
60 }
61 } catch (int e) {
62 if (e == 100) {
64 } else {
65 throw e;
66 }
67 }
68
69 QUILL_LOG_DEBUG(log, "Saving result to {}", posOutFilename);
70 if (!eonc::io::io_ok(pos->matter2con(posOutFilename))) {
71 QUILL_LOG_ERROR(log, "Failed to write {}", posOutFilename);
72 }
74 QUILL_LOG_DEBUG(log, "Final Energy: {}", pos->getPotentialEnergy());
75 }
76
77 std::filesystem::path resultsFilename("results.dat");
78 returnFiles.push_back(resultsFilename.string());
79
80 std::ofstream fileResults(resultsFilename, std::ios::binary);
81
82 if (!fileResults.is_open()) {
83 std::cerr << "Error opening file " << resultsFilename << ": "
84 << std::strerror(errno) << std::endl;
85 throw std::runtime_error("Failed to open results file: " +
86 std::string(std::strerror(errno)));
87 return returnFiles;
88 }
89
90 fileResults << static_cast<int>(status) << " termination_reason\n";
91 fileResults << magic_enum::enum_name<RunStatus>(status)
92 << " termination_reason_text\n";
93 fileResults << "minimization job_type\n";
94 fileResults << magic_enum::enum_name<PotType>(
95 params.potential_options.potential)
96 << " potential_type\n";
97 fileResults << this->pot->forceCallCounter.load() << " total_force_calls\n";
98
100 fileResults << std::format("{:.12e} potential_energy\n",
101 pos->getPotentialEnergy());
102 }
103
104 // No explicit fclose needed; RAII handles it.
105 if (!fileResults.good()) {
106 std::cerr << "Error writing to file " << resultsFilename
107 << ": May be incomplete." << std::endl;
108 // Consider throwing, depending on the severity.
109 }
110
111 return returnFiles;
112}
The optimizer class is used to serve as an abstract class for all optimizers, as well as to call an o...
std::vector< std::string > run(void)
Virtual run; used solely for dynamic dispatch.
std::shared_ptr< Potential > pot
Definition Job.h:55
Parameters params
Definition Job.h:54
eonc::log::Scoped log
constexpr bool io_ok(IoStatus s) noexcept
Definition ConFileIO.h:38