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/BaseStructures.h"
14#include "eon/HelperFunctions.h"
15#include "eon/JobResult.h"
16#include "eon/Matter.h"
17#include "eon/MonteCarlo.h"
18#include "eon/PotRegistry.h"
19
20#include <filesystem>
21#include <format>
22#include <fstream>
23#include <stdexcept>
24#include <string>
25
26namespace eonc {
27
28std::vector<std::string> MonteCarloJob::run(void) {
29 std::string posInFilename("pos.con");
30 std::string posOutFilename("out.con");
31
32 if (params.main_options().checkpoint) {
33 if (std::filesystem::exists("pos_cp.con")) {
34 posInFilename = "pos_cp.con";
35 QUILL_LOG_DEBUG(log, "Resuming from checkpoint\n");
36 } else {
37 QUILL_LOG_DEBUG(log, "No checkpoint files found\n");
38 }
39 }
40
41 std::vector<std::string> returnFiles;
42
43 auto matter = std::make_shared<Matter>(pot, params);
44 if (!eonc::io::io_ok(matter->con2matter(posInFilename))) {
45 QUILL_LOG_CRITICAL(log, "Failed to load {}", posInFilename);
46 throw std::runtime_error("failed to load " + posInFilename);
47 }
48
49 MonteCarlo mc = MonteCarlo(matter, params);
50 mc.run(params.monte_carlo_options().steps, params.main_options().temperature,
51 params.monte_carlo_options().step_size);
52
53 QUILL_LOG_DEBUG(log, "Saving result to {}", posOutFilename);
54 if (eonc::io::io_ok(matter->matter2con(posOutFilename))) {
55 returnFiles.push_back(posOutFilename);
56 } else {
57 QUILL_LOG_ERROR(log, "Failed to write {}", posOutFilename);
58 }
59
60 std::string resultsFilename("results.dat");
62 RunStatus::GOOD, params.potential_options().potential,
63 PotRegistry::get().total_force_calls(), true,
64 matter->getPotentialEnergy());
65 env.job_type = "monte_carlo";
66 env.writeResultsDat(resultsFilename);
67 returnFiles.push_back(resultsFilename);
68
69 return returnFiles;
70}
71
72} // namespace eonc
std::shared_ptr< Potential > pot
Definition Job.h:63
Parameters params
Definition Job.h:58
eonc::log::Scoped log
std::vector< std::string > run(void) override
Virtual run; used solely for dynamic dispatch.
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
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