-
Notifications
You must be signed in to change notification settings - Fork 0
/
diagram.cpp
316 lines (238 loc) · 9.55 KB
/
diagram.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/**
* @file diagram.cpp
* @brief Definitions file for Diagram and Diagram_core classes
* @author Enrico Pedretti
* @date 2023-08-31
*/
#include <iostream>
#include <diagmc/diagram.h>
#include <stdexcept>
#include <string>
#include <random>
#include <chrono>
#include <limits>
#include <cmath>
#include <algorithm>
#include <list>
#define RNG _uniform_dist(_mt_generator) //extracts a random number uniformly in [0,1]
bool lists_are_float_equal(const std::list<double>& list1, const std::list<double>& list2, double epsilon) {
// Check if lists have the same size
if (list1.size() != list2.size()) {
return false;
}
// Initialize two iterators to run through the two lists
auto it1 = list1.begin();
auto it2 = list2.begin();
// Element-by-element comparison
while (it1 != list1.end() && it2 != list2.end()) {
if (std::fabs(*it1 - *it2) > epsilon) {
return false;
}
++it1;
++it2;
}
// If no differences were found, the lists are equal, so return true
return true;
}
void Diagram_core::assert_parameters_validity(double beta, int s0, double H, double GAMMA, std::list<double> vertices) const
{
if(! (beta > 0))
{
throw std::invalid_argument(
std::string("beta must be > 0, but ")
+ std::to_string(_beta) + std::string(" was provided.")
);
}
if(s0 != 1 && s0 != -1)
{
throw std::invalid_argument(
std::string("The spin can either be +1 or -1, but ")
+ std::to_string(s0) + std::string(" was provided.")
);
}
if(std::abs(GAMMA) < std::numeric_limits<double>::epsilon() )
{
throw std::invalid_argument(
std::string("GAMMA must be different from 0.")
);
}
if(vertices.size() % 2 != 0)
{
throw std::invalid_argument(
std::string("The vertices list must contain an even number of elements.")
);
}
for(auto v : vertices)
{
if (v > beta) throw std::invalid_argument("The vertices list contains values > beta.");
}
if(!std::is_sorted(vertices.begin(), vertices.end()))
{
throw std::invalid_argument("The list used to initialize the diagram was not sorted.");
}
}
//Methods definitions for class Diagram_core -------------------------------------------------------
Diagram_core::Diagram_core(double beta, int s0, double H, double GAMMA, std::list<double> vertices)
: _beta(beta), _s0(s0), _H(H), _GAMMA(GAMMA), _vertices(vertices) {
//check that parameters are in the correct range of values, throwing exception otherwise.
assert_parameters_validity(beta, s0, H, GAMMA, vertices);
}
bool Diagram_core::operator==(const Diagram_core &other) const
{
if ( std::fabs(this->_beta - other._beta ) < EPSILON
&& this->_s0 == other._s0
&& std::fabs(this->_H - other._H) < EPSILON
&& std::fabs(this->_GAMMA - other._GAMMA) < EPSILON
&& lists_are_float_equal(this->_vertices, other._vertices, EPSILON) ) return true;
else return false;
}
bool Diagram_core::operator!=(const Diagram_core &other) const
{
return !(*this == other);
}
double Diagram_core::operator/(const Diagram_core &other) const
{
return this->value()/other.value();
}
double Diagram_core::sum_deltatau() const
{
//sum (... +t4-t3 + t2-t1)
double sum_deltatau = 0;
for (auto it = _vertices.begin(); it != _vertices.end();)
{
sum_deltatau -= *it; //-t1
++it;
sum_deltatau += *it; //+t2
++it;
}
return sum_deltatau;
}
double Diagram_core::value() const
{
double val = std::pow(_GAMMA, order()) * std::exp(_H * _s0 *( -_beta + 2*sum_deltatau()));
return val;
}
size_t Diagram_core::order() const {
return _vertices.size();
}
//acceptance rates for the updates
double Diagram_core::acceptance_rate_add(double tau1, double tau2, double tau2max, double new_segment_spin) const {
return _GAMMA*_GAMMA * std::exp(-2 * _H * new_segment_spin * (tau2-tau1)) * _beta * (tau2max - tau1) / (_vertices.size() + 1);
}
double Diagram_core::acceptance_rate_remove(double tau1, double tau2, double tau2max, double segment_toberemoved_spin) const {
return std::exp(2 * _H * segment_toberemoved_spin * (tau2-tau1)) * (_vertices.size() - 1) / ( _GAMMA*_GAMMA * _beta * (tau2max-tau1) );
}
double Diagram_core::acceptance_rate_flip() const {
return std::exp(2*_H*_s0*(_beta - 2 * sum_deltatau()));
}
//update functions
bool Diagram_core::attempt_add_segment(double RN1, double RN2, double RNacc) {
//extract the time tau1 of the first vertex to be added in uniform([0, _beta])
double tau1 = RN1 * _beta;
//determine the nearest vertex (tau3) after the extracted tau1 (which will become tau3 after adding (tau1, tau2))----------------------
//, and the index of its segment
std::list<double>::iterator tau3_it = _vertices.end(); //iterator to sweep the list and find the nearest vertex with tau > tau1
int new_segment_index = 0; //index that the segment we want to add will have, corresponding to the index of the current tau3 segment
for (auto i = _vertices.begin(); i != _vertices.end(); ++i)
{
if (*i > tau1) //stop at the nearest value of the vertex greater than tau1
{
tau3_it = i;
break;
}
++new_segment_index;
}
double tau2max = tau3_it != _vertices.end() ? *tau3_it : _beta ;
//--------------------------------------------------------------------------------------------------------------------------------------
//select second vertex in uniform([tau1, tau2max])
double tau2 = tau1 + RN2 * (tau2max - tau1);
//spin of the segment that we will add
double new_segment_spin = _s0*std::pow(-1, new_segment_index + 1);
//attempt update, adding segment if accepted (and returning true); doing nothing (and returning false) if rejected
if (RNacc < acceptance_rate_add(tau1, tau2, tau2max, new_segment_spin))
{
_vertices.insert(tau3_it, tau1);
_vertices.insert(tau3_it, tau2);
return true;
}
return false;
}
bool Diagram_core::attempt_remove_segment(double RN1, double RNacc) {
//cannot remove segment if diagram is 0 order, so reject update right away
if (order() == 0) return false;
//randomly choose segment to be removed
int segment_toberemoved_index = RN1 * (order() - 1) + 1; //it starts from 1, since the first segment [0,t1] cannot be removed
//determine the _vertices (tau1, tau2) of the segment to be removed, and their location in the list---
auto tau1_it = _vertices.begin();
std::advance(tau1_it, segment_toberemoved_index - 1); //does not advance if segment 1, since t1 is already the first element of the list
auto tau2_it = tau1_it; ++tau2_it; //tau2 is the vertex just after tau1
double tau1 = *tau1_it;
double tau2 = *tau2_it;
//---------------------------------------------------------------------------------------------------
//tau3 is the vertex just after tau2
auto tau3_it = tau2_it; ++tau3_it;
double tau2max = tau3_it != _vertices.end() ? *tau3_it : _beta;
//spin of the segment to be removed
double segment_toberemoved_spin = _s0 * std::pow(-1, segment_toberemoved_index);
//attempt update, removing segment if accepted (and returning true); doing nothing (and returning false) if rejected
if (RNacc < acceptance_rate_remove(tau1, tau2, tau2max, segment_toberemoved_spin))
{
_vertices.erase(tau1_it, tau3_it); //erase between [1, 3), so 1 and 2
return true;
}
return false;
}
bool Diagram_core::attempt_spin_flip(double RNacc) {
//attempt update, flipping spins of all diagram if accepted (and returning true); doing nothing (and returning false) if rejected
if (RNacc < acceptance_rate_flip())
{
_s0 *= -1;
return true;
}
return false;
}
//END Diagram_core class definition
//--------------------------------------------------------------------------------------------------
//Methods definitions for class Diagram ------------------------------------------------------------
Diagram::Diagram(double beta, int s0, double H, double GAMMA,
std::list<double> vertices,
unsigned int seed)
: Diagram_core(beta, s0, H, GAMMA, vertices) , _uniform_dist(0,1), _mt_generator(seed) {}
//getters
double Diagram_core::get_beta() const {
return _beta;
}
int Diagram_core::get_s0() const {
return _s0;
}
double Diagram_core::get_H() const {
return _H;
}
double Diagram_core::get_GAMMA() const {
return _GAMMA;
}
std::list<double> Diagram_core::get_vertices() const {
return _vertices;
}
//update functions
bool Diagram::attempt_add_segment() {
return Diagram_core::attempt_add_segment(RNG, RNG, RNG);
}
bool Diagram::attempt_remove_segment() {
return Diagram_core::attempt_remove_segment(RNG, RNG);
}
bool Diagram::attempt_spin_flip() {
return Diagram_core::attempt_spin_flip(RNG);
}
void Diagram::reset_diagram(double beta, int s0, double H, double GAMMA, std::list<double> vertices, unsigned int seed) {
//check that parameters are in the correct range of values, throwing exception otherwise.
assert_parameters_validity(beta, s0, H, GAMMA, vertices);
//set the new value
_beta = beta,
_s0 = s0,
_H = H,
_GAMMA = GAMMA,
_vertices = vertices;
_mt_generator.seed(seed);
}
//--------------------------------------------------------------------------------------------------