-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathonespin.cpp
More file actions
71 lines (58 loc) · 2.28 KB
/
Copy pathonespin.cpp
File metadata and controls
71 lines (58 loc) · 2.28 KB
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
/*
********************************************************************************
* onespin.c: Uses Metropolis algorithm to generate thermal "ensemble" for a *
* single spin. *
* *
********************************************************************************
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
// update state by flipping spin, accept new state if E_new<E_old
// or rand[0,1)< P(new)/P(old)
// s: is the inputs spin
// h: is the environment (= \beta H)
void update(int *s, double h) {
int spin = *s;
int newspin = -spin; // trial spin flip
double DeltaBetaE = -(newspin-spin)*h; // beta*(E(new) - E(old))
// if the new state is at lower energy, accept it
// if not, accept it with a probability decreasing w/ exp(-DeltaBetaE)
if ( DeltaBetaE <= 0 || drand48() < exp(-DeltaBetaE) ) *s = newspin; // Metropolis method
}
int main(int argc, char *argv[]) {
int nsweep;
double h;
printf("Program generates a thermal ensemble for states with one spin.\n\n");
srand48((long)(time(0)));
if (argc==3){
nsweep=atoi(argv[1]);
h=atof(argv[2]);
}
else{
printf("Enter total number of spin configurations (sweeps) generated:\n");
scanf("%d", &nsweep);
// h > 1 means that effect of H field is larger that temperature effects
printf("Enter value of magnetic field parameter h=H/(k_bT):\n");
scanf("%lf", &h);
}
// our initial state (arbitrary value, could be -1 as well)
int spin = 1;
// Metropolis update loop
int nplus=0;
int nminus=0;
for(int n=0; n<nsweep; n++) {
update(&spin,h); // try a Metropolis update
if (spin == 1) nplus++;
if (spin == -1) nminus++;
}
printf("Calculations %d %d\n",nplus,nminus);
printf("P(+ state) = %13.10lf\n", (double)nplus/(nplus+nminus) );
printf("P(- state) = %13.10lf\n", (double)nminus/(nplus+nminus) );
// Write <magnetization>
printf("<sigma> = %13.10lf\n", (double)(nplus-nminus)/(nplus+nminus) );
/* Write theoretical prediction for comparison */
printf("\nTheory prediction: sigma = tanh(h) = %12.10lf\n", tanh(h));
return(0);
}