-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod.rs
143 lines (124 loc) · 3.76 KB
/
mod.rs
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
extern crate gnuplot as gn;
extern crate nalgebra as na;
extern crate rand;
extern crate itertools;
use self::na::DMat;
use self::std::vec::Vec;
use self::rand::Rng;
use self::rand::distributions::{Range, Normal, Sample};
#[derive(Debug, Clone)]
struct SpinChain {
len: usize,
spins: Vec<f64>,
coupling: DMat<f64>,
field: f64,
beta: f64,
}
impl SpinChain {
fn new(len: usize, coupling: DMat<f64>, field: f64, beta: f64) -> SpinChain {
SpinChain {
len: len,
coupling: coupling,
field: field,
beta: beta,
spins: rand_spin_chain(len, &mut self::rand::thread_rng(), &mut Range::new(0.0, 1.0)),
}
}
fn set_spins(&self, new_spins: &Vec<f64>) -> SpinChain {
SpinChain {
len: self.len.clone(),
coupling: self.coupling.clone(),
field: self.field.clone(),
beta: self.beta.clone(),
spins: new_spins.clone(),
}
}
fn update(&self) -> SpinChain {
let rand_ind = gen_rand_ind(&self);
let prob = self::rand::thread_rng().gen_range(0.0, 1.0);
let del_e = spin_flip(&self, rand_ind).hamiltonian() - self.hamiltonian();
if del_e <= 0.0 {
spin_flip(&self, rand_ind)
} else if prob <= (-self.beta * del_e).exp() {
spin_flip(&self, rand_ind)
} else {
self.clone()
}
}
fn hamiltonian(&self) -> f64 {
self.beta * mat_sum(hadamard(&self.coupling, &outer(&self.spins, &self.spins))) +
self.field * spin_sum(&self.spins)
}
fn len(&self) -> usize {
self.len
}
fn mean_mag(&self) -> f64 {
1.0 / (self.len as f64) * spin_sum(&self.spins)
}
}
fn rand_spin_chain<R, D>(spins: usize, mut rng: &mut R, mut dist: &mut D) -> Vec<f64>
where R: Rng, D: Sample<f64>
{
let blank = vec![0; spins];
blank.iter()
.map(|_| dist.sample(&mut rng) as i64 as f64)
.collect::<Vec<_>>()
}
fn spin_flip(chain: &SpinChain, spin_ind: usize) -> SpinChain {
let mut ret = vec![];
ret.reserve(chain.len());
for (ind, spin) in chain.spins.iter().enumerate() {
if ind == spin_ind {
ret.push(1.0 - *spin);
} else {
ret.push(*spin);
}
}
chain.set_spins(&ret)
}
fn gen_rand_ind(chain: &SpinChain) -> usize {
self::rand::thread_rng().gen_range(0, chain.len())
}
fn outer(vec_1: &Vec<f64>, vec_2: &Vec<f64>) -> DMat<f64> {
DMat::from_fn(vec_1.len(), vec_2.len(), |i, j| vec_1[i] * vec_2[j])
}
fn hadamard(mat_1: &DMat<f64>, mat_2: &DMat<f64>) -> DMat<f64> {
let vec_1 = mat_1.clone().into_vec();
let vec_2 = mat_2.clone().into_vec();
let zip_ret_vec = vec_1.iter().zip(vec_2);
let mut ret_vec: Vec<f64> = vec![];
ret_vec.reserve(2 * zip_ret_vec.len());
for tup in zip_ret_vec {
ret_vec.push(tup.0 * tup.1);
}
DMat::from_col_vec(mat_1.nrows(), mat_2.ncols(), &ret_vec)
}
fn mat_sum(mat: DMat<f64>) -> f64 {
mat.into_vec().iter().fold(0.0, |sum, x| sum + x)
}
fn spin_sum(vec: &Vec<f64>) -> f64 {
vec.iter().fold(0.0, |sum, x| sum + x)
}
fn main() {
let length = 10;
let field = 1.0;
let beta = 0.0000001;
let n_iters = 100;
let coupling: DMat<f64> = DMat::from_fn(length, length, |i, j| if i == j {
0.0
} else {
1.0
});
let times = itertools::linspace(0.0, 10.0, n_iters).collect::<Vec<f64>>();
let mut mags = vec![];
mags.reserve(n_iters);
let mut sys = SpinChain::new(length, coupling, field, beta);
for _ in times.clone() {
mags.push(sys.mean_mag());
sys = sys.update();
}
let mut fg = Figure::new();
fg.axes2d()
.lines(×.clone(), &mags, &[Caption("A line"), Color("blue")]);
fg.show();
}