Loading...
Searching...
No Matches
MonteCarloJob.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/MonteCarloJob.h"
13#include "eon/HelperFunctions.h"
14#include "eon/Matter.h"
15#include "eon/MonteCarlo.h"
16
17#include <filesystem>
18#include <format>
19#include <fstream>
20#include <stdexcept>
21#include <string>
22
23std::vector<std::string> MonteCarloJob::run(void) {
24 std::string posInFilename("pos.con");
25 std::string posOutFilename("out.con");
26
27 if (params.main_options.checkpoint) {
28 if (std::filesystem::exists("pos_cp.con")) {
29 posInFilename = "pos_cp.con";
30 QUILL_LOG_DEBUG(log, "Resuming from checkpoint\n");
31 } else {
32 QUILL_LOG_DEBUG(log, "No checkpoint files found\n");
33 }
34 }
35
36 std::vector<std::string> returnFiles;
37
38 auto matter = std::make_shared<Matter>(pot, params);
39 if (!eonc::io::io_ok(matter->con2matter(posInFilename))) {
40 QUILL_LOG_CRITICAL(log, "Failed to load {}", posInFilename);
41 throw std::runtime_error("failed to load " + posInFilename);
42 }
43
44 MonteCarlo mc = MonteCarlo(matter, params);
45 mc.run(params.monte_carlo_options.steps, params.main_options.temperature,
46 params.monte_carlo_options.step_size);
47
48 QUILL_LOG_DEBUG(log, "Saving result to {}", posOutFilename);
49 if (eonc::io::io_ok(matter->matter2con(posOutFilename))) {
50 returnFiles.push_back(posOutFilename);
51 } else {
52 QUILL_LOG_ERROR(log, "Failed to write {}", posOutFilename);
53 }
54
55 std::string resultsFilename("results.dat");
56
57 std::ofstream out(resultsFilename, std::ios::binary);
58 if (!out) {
59 QUILL_LOG_CRITICAL(log, "Failed to open {}", resultsFilename);
60 throw std::runtime_error("failed to open " + resultsFilename);
61 }
62 out << std::format(
63 "{} potential_type\n",
64 magic_enum::enum_name<PotType>(params.potential_options.potential));
65 out << std::format("{} total_force_calls\n",
66 PotRegistry::get().total_force_calls());
67 out << std::format("{:f} potential_energy\n", matter->getPotentialEnergy());
68 out.close();
69 if (!out) {
70 QUILL_LOG_CRITICAL(log, "Failed to write {}", resultsFilename);
71 throw std::runtime_error("failed to write " + resultsFilename);
72 }
73 returnFiles.push_back(resultsFilename);
74
75 return returnFiles;
76}
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
void run(int numSteps, double temperature, double stepSize)
static PotRegistry & get() noexcept
Process-lifetime singleton.
constexpr bool io_ok(IoStatus s) noexcept
Definition ConFileIO.h:38