Loading...
Searching...
No Matches
MonteCarlo.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/MonteCarlo.h"
13
14#include <cmath>
15#include <iostream>
16
17using namespace eonc::helpers;
18
19void MonteCarlo::run(int numSteps, double temperature, double stepSize) {
20
21 if (!eonc::io::io_ok(matter->matter2con("movie.con"))) {
22 QUILL_LOG_WARNING(log, "Failed to write movie.con header frame");
23 }
24 AtomMatrix current;
25 AtomMatrix trial;
26 double T = temperature;
27 T = 100.0;
28 numSteps = 100;
29 stepSize = 0.1;
30 int accepts = 0;
31 for (int steps = 0; steps < numSteps; steps++) {
32 current = matter->getPositions();
33 trial = current;
34 double ecurrent, etrial;
35 ecurrent = matter->getPotentialEnergy();
36 if (!eonc::io::io_ok(matter->matter2con("movie.con", true))) {
37 QUILL_LOG_WARNING(log, "Failed to append movie.con frame");
38 }
39 for (int i = 0; i < trial.rows(); i++) {
40 for (int j = 0; j < 3; j++) {
41 double d = gaussRandom(0.0, stepSize);
42 trial(i, j) += d;
43 }
44 }
45 matter->setPositions(trial);
46 etrial = matter->getPotentialEnergy();
47 double de = ecurrent - etrial;
48 QUILL_LOG_INFO(log, "de={}", de);
49 if (de <= 0.0) {
50 QUILL_LOG_INFO(log, "{}: accept de <= 0.0", steps);
51 accepts++;
52 continue;
53 }
54 double r = randomDouble();
55 double kB = params.constants.kB;
56 double arg = -de / (kB * T);
57 QUILL_LOG_DEBUG(log, "arg: {}\n", arg);
58 if (arg < -50.0) {
59 matter->setPositions(current);
60 QUILL_LOG_DEBUG(log, "{}: reject small arg\n", steps);
61 continue;
62 }
63
64 double p = std::exp(arg);
65 if (r < p) {
66 QUILL_LOG_DEBUG(log, "{}: accept r<p\n", steps);
67 accepts++;
68 continue;
69 } else {
70 matter->setPositions(current);
71 QUILL_LOG_DEBUG(log, "{}: reject\n", steps);
72 }
73 }
74 QUILL_LOG_INFO(log, "accepts: {}", accepts);
75}
Eigen::Matrix< double, Eigen::Dynamic, 3, eOnStorageOrder > AtomMatrix
Definition Eigen.h:37
void run(int numSteps, double temperature, double stepSize)
std::shared_ptr< Matter > matter
Definition MonteCarlo.h:33
eonc::log::Scoped log
Definition MonteCarlo.h:35
const Parameters & params
Definition MonteCarlo.h:34
double randomDouble()
double gaussRandom(double avg, double std)
constexpr bool io_ok(IoStatus s) noexcept
Definition ConFileIO.h:38