Loading...
Searching...
No Matches
PrefactorJob.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/PrefactorJob.h"
13#include "eon/BaseStructures.h"
14#include "eon/EonLogger.h"
15#include "eon/HelperFunctions.h"
16#include "eon/Hessian.h"
17#include "eon/JobResult.h"
18#include "eon/Matter.h"
19#include "eon/PotRegistry.h"
20#include "eon/Potential.h"
21#include "eon/Prefactor.h"
22
23#include <cmath>
24#include <filesystem>
25#include <format>
26#include <fstream>
27#include <stdexcept>
28#include <string>
29
30namespace eonc {
31
32std::vector<std::string> PrefactorJob::run() {
33 std::vector<std::string> returnFiles;
34 VectorXd freqs;
35
36 std::string reactantFilename = eonc::helpers::getRelevantFile("reactant.con");
37 std::string saddleFilename = eonc::helpers::getRelevantFile("saddle.con");
38 std::string productFilename = eonc::helpers::getRelevantFile("product.con");
39
40 auto reactant = std::make_unique<Matter>(pot, params);
41 auto saddle = std::make_unique<Matter>(pot, params);
42 auto product = std::make_unique<Matter>(pot, params);
43
44 if (!eonc::io::io_ok(reactant->con2matter(reactantFilename)) ||
45 !eonc::io::io_ok(saddle->con2matter(saddleFilename)) ||
46 !eonc::io::io_ok(product->con2matter(productFilename))) {
47 EONC_LOG_CRITICAL("Failed to load reactant/saddle/product for prefactor");
48 throw std::runtime_error("failed to load prefactor geometries");
49 }
50 double pref1 = 0.0, pref2 = 0.0;
51 const int prefStatus = eonc::Prefactor::getPrefactors(
52 params, reactant.get(), saddle.get(), product.get(), pref1, pref2);
53
54 VectorXi atoms;
55 if (params.prefactor_options().all_free_atoms) {
56 std::string matterFilename;
57 if (params.prefactor_options().configuration ==
59 matterFilename = reactantFilename;
60 } else if (params.prefactor_options().configuration ==
62 matterFilename = saddleFilename;
63 } else if (params.prefactor_options().configuration ==
65 matterFilename = productFilename;
66 }
67 if (!eonc::io::io_ok(reactant->con2matter(matterFilename)) ||
68 !eonc::io::io_ok(saddle->con2matter(matterFilename)) ||
69 !eonc::io::io_ok(product->con2matter(matterFilename))) {
70 EONC_LOG_CRITICAL("Failed to reload {} for all-free-atoms prefactor",
71 matterFilename);
72 throw std::runtime_error("failed to load prefactor configuration");
73 }
74
75 atoms = eonc::Prefactor::allFreeAtoms(reactant.get());
76 } else {
77 if (!eonc::io::io_ok(reactant->con2matter(reactantFilename)) ||
78 !eonc::io::io_ok(saddle->con2matter(saddleFilename)) ||
79 !eonc::io::io_ok(product->con2matter(productFilename))) {
81 "Failed to reload reactant/saddle/product for prefactor");
82 throw std::runtime_error("failed to load prefactor geometries");
83 }
84
85 atoms = eonc::Prefactor::movedAtoms(params, reactant.get(), saddle.get(),
86 product.get());
87 }
88 bool failed = (prefStatus == -1) || (atoms.rows() == 0);
89
90 if (!failed) {
91 if (params.prefactor_options().configuration ==
93 Hessian hessian(params, reactant.get());
94 freqs = hessian.getFreqs(reactant.get(), atoms);
95 } else if (params.prefactor_options().configuration ==
97 Hessian hessian(params, saddle.get());
98 freqs = hessian.getFreqs(saddle.get(), atoms);
99 } else if (params.prefactor_options().configuration ==
101 Hessian hessian(params, product.get());
102 freqs = hessian.getFreqs(product.get(), atoms);
103 }
104 }
105
106 if (!failed) {
107 failed = freqs.size() != 3 * atoms.rows();
108 }
109
110 std::string results_file("results.dat");
111 std::string freq_file("freq.dat");
112 returnFiles.push_back(results_file);
113 returnFiles.push_back(freq_file);
114
115 std::ofstream outFreq(freq_file, std::ios::binary);
116
119 params.potential_options().potential,
120 PotRegistry::get().total_force_calls(), false, 0.0);
121 env.job_type = "prefactor";
122 env.tags.emplace_back("good", failed ? "false" : "true");
123 env.extras.emplace_back("force_calls", static_cast<double>(env.force_calls));
124 if (!failed) {
125 env.extras.emplace_back("prefactor_reactant_to_product", pref1);
126 env.extras.emplace_back("prefactor_product_to_reactant", pref2);
127 }
128 env.writeResultsDat(results_file);
129
130 if (outFreq && !failed) {
131 for (int i = 0; i < freqs.size(); i++) {
132 if (0. < freqs[i]) {
133 outFreq << std::format("{:f}\n",
134 std::sqrt(freqs[i]) /
135 (2 * eonc::helpers::pi * 10.18e-15));
136 } else {
137 outFreq << std::format("{:f}\n",
138 -std::sqrt(-freqs[i]) /
139 (2 * eonc::helpers::pi * 10.18e-15));
140 }
141 }
142 }
143
144 if (std::filesystem::exists("freqs.dat")) {
145 returnFiles.push_back("freqs.dat");
146 }
147 if (std::filesystem::exists("hessian.dat")) {
148 returnFiles.push_back("hessian.dat");
149 }
150
151 return returnFiles;
152}
153
154} // namespace eonc
#define EONC_LOG_CRITICAL(...)
Definition EonLogger.h:267
VectorXd getFreqs(Matter *matterIn, const VectorXi &atomsIn)
Definition Hessian.cpp:102
std::shared_ptr< Potential > pot
Definition Job.h:63
Parameters params
Definition Job.h:58
static PotRegistry & get() noexcept
Process-lifetime singleton.
std::vector< std::string > run() override
Virtual run; used solely for dynamic dispatch.
static constexpr std::string_view PREFACTOR_PRODUCT
static constexpr std::string_view PREFACTOR_SADDLE
static constexpr std::string_view PREFACTOR_REACTANT
VectorXi allFreeAtoms(Matter *matter)
VectorXi movedAtoms(const Parameters &parameters, Matter *min1, Matter *saddle, Matter *min2)
int getPrefactors(const Parameters &parameters, Matter *min1, Matter *saddle, Matter *min2, double &pref1, double &pref2)
Definition Prefactor.cpp:24
std::string getRelevantFile(std::string filename)
constexpr double pi
constexpr bool io_ok(IoStatus s) noexcept
Definition ConFileIO.h:38
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