-
Notifications
You must be signed in to change notification settings - Fork 0
/
math.cpp
113 lines (107 loc) · 3.21 KB
/
math.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include "math.hpp"
#include "spins.hpp"
#include <numeric>
#include <cmath>
#include <random>
#include <chrono>
double math::node_energy(phys::Spin const &spin, phys::Board const &board, double const &field)
{
/*int loc{0};
for (int b : {-1, 1})
{
auto x = spin.get_x() + b;
auto y = spin.get_y() + b;
loc += spin.get_state() * (board.get_spin_const(x, spin.get_y())).get_state();
loc += spin.get_state() * (board.get_spin_const(spin.get_x(), y)).get_state();
}*/
// return -loc / 2 - field * spin.get_state();
// return -spin.get_state() * (board.get_neighbour(spin, 't').get_state() + board.get_neighbour(spin, 'b').get_state() + board.get_neighbour(spin, 'l').get_state() + board.get_neighbour(spin, 'r').get_state()) / 2 - field * spin.get_state();
int sum{0};
if (spin.get_y() < 2)
{
sum += board.get_spin_const(spin.get_x(), 500).get_state();
}
else
{
sum += board.get_spin_const(spin.get_x(), spin.get_y() - 1).get_state();
}
if (spin.get_y() > 499)
{
sum += board.get_spin_const(spin.get_x(), 1).get_state();
}
else
{
sum += board.get_spin_const(spin.get_x(), spin.get_y() + 1).get_state();
}
if (spin.get_x() < 2)
{
sum += board.get_spin_const(500, spin.get_y()).get_state();
}
else
{
sum += board.get_spin_const(spin.get_x() - 1, spin.get_y()).get_state();
}
if (spin.get_x() > 499)
{
sum += board.get_spin_const(1, spin.get_y()).get_state();
}
else
{
sum += board.get_spin_const(spin.get_x() + 1, spin.get_y()).get_state();
}
return -spin.get_state() * (sum) / 2 - field * spin.get_state();
}
double math::energy_calc(phys::Board const &board, double const &field)
{
int spin_cum{0};
auto spin_sum = [&](double n, phys::Spin const spin2) -> double
{
spin_cum += spin2.get_state();
return n + math::node_energy(spin2, board, field);
};
double sum = std::accumulate((board.spins_const()).begin(), (board.spins_const()).end(), 0, spin_sum);
return -sum - field * spin_cum;
}
bool math::probability_calc(double const &en)
{
std::random_device ran;
std::uniform_real_distribution<double> ar(0, 1);
double pk = std::exp(-(en));
if (ar(ran) <= pk)
{
return true;
}
else
{
return false;
}
}
void math::evolution(phys::Board &board, double const &field, double const &temp, int const &a, int const &b)
{
/*for (phys::Spin &spin : board.spins())
{*/
std::random_device ran;
std::mt19937_64 eng(ran());
std::uniform_int_distribution<> dist(a, b);
auto init = std::chrono::steady_clock::now();
// for (int counter = 1; counter <= 5000; ++counter)
while (std::chrono::steady_clock::now() - init < std::chrono::milliseconds(20))
{
auto spin = board.spins().begin() + dist(eng);
double dE = -2 * math::node_energy(*spin, board, field);
spin->flip_state();
if (dE <= 0)
{
}
else
{
if (math::probability_calc((dE) / temp) == true)
{
}
else
{
spin->flip_state();
}
}
}
}