-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathising_model_2d.c
More file actions
142 lines (119 loc) · 3.67 KB
/
Copy pathising_model_2d.c
File metadata and controls
142 lines (119 loc) · 3.67 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
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
#include <stdio.h>
#include "metropolis.c"
#include "matrixmem.c"
#include "queuearray.c"
void coldStart(int**, int, int);
void hotStart(int**, int, int);
void probLookUp(double, double, double**);
/* coldStart(): initializes spin[][] to all spins = +1.
*
* spin[][]: array containing +/-1 to simulate a 2D configuration of spins
*
* lx: horizontal length of spin[][]
*
* ly: vertical length of spin[][]
*/
void coldStart(int **spin, int lx, int ly)
{
for(int x = 0; x < lx; x++)
{
for(int y = 0; y < ly; y++)
{
spin[x][y] = 1;
}
}
}
/* hotStart(): initializes spin[][] to all spins = +/-1 randomly.
*
* spin[][]: array containing +/-1 to simulate a 2D configuration of spins
*
* lx: horizontal length of spin[][]
*
* ly: vertical length of spin[][]
*/
void hotStart(int **spin, int lx, int ly)
{
gsl_rng *r = gsl_rng_alloc(gsl_rng_taus2);
unsigned long seed = ((unsigned long) time(NULL));
gsl_rng_set(r, seed);
double randNum = 0.;
for(int x = 0; x < lx; x++)
{
for(int y = 0; y < ly; y++)
{
randNum = gsl_rng_uniform(r);
spin[x][y] = (randNum < 0.5) ? 1 : -1;
}
}
gsl_rng_free(r);
}
/*
* Creates look-up table for boltzmann probabilities for spin-flips for 2D square lattice
*/
void probLookUp(double T, double J, double **probs)
{
for(int i = -8; i <= 8; i += 4)
{
probs[i + 8][0] = exp(-(i * J)/T);
probs[i + 8][2] = exp(-(i * J)/T);
}
}
int main(void)
{
int **spin = matrix_allocate_int(XLENGTH, YLENGTH);
double **boltzProbs = matrix_allocate_double(17, 3);
unsigned long seed = ((unsigned long) time(NULL));
gsl_rng *r = gsl_rng_alloc(gsl_rng_taus2); //allocates memory for random number generator
gsl_rng_set(r, seed); //seeds random number generator
double J = 1.;
double tmin = 2.;
double tmax = 3.;
int numPoints= 100;
double tstep = (tmax - tmin)/numPoints;
int ensembleSize = 1024*128;
double energy = 0., magnetization = 0., mags = 0., avEn = 0., avEn2 = 0., avMag = 0., avMag2 = 0., magSus = 0., specHeat = 0., avMagLast10 = 0.;
int thermSteps = ensembleSize/2;
Queue *magLast10;
magLast10 = newQueue();
FILE *fp;
fp = fopen("2DSquareModelResults5x5.dat", "w");
for(double T = tmin; T < tmax; T += tstep)
{
probLookUp(T, J, boltzProbs); //updates lookup table for boltzmann probabilities
if(!isEmpty(magLast10) && avMagLast10 < 0.2)//decides whether or not to hotstart or coldstart the initial spin configuration
hotStart(spin, XLENGTH, YLENGTH);
else
coldStart(spin, XLENGTH, YLENGTH);
for(int ts = 0; ts < thermSteps; ts++)//thermalizes configuration
mc_step_per_spin(spin, boltzProbs, r);
for(int n = 0; n < ensembleSize; n++)//does metropolis sweeps when configuration is in thermal equilibrium
{
mc_step_per_spin(spin, boltzProbs, r);
energy = hamil(J, spin);
mags = mag(spin);
magnetization = fabs(mags);
avEn += energy;
avEn2 += (energy * energy);
avMag += magnetization;
avMag2 += (magnetization * magnetization);
}
avEn /= ensembleSize;
avEn2 /= ensembleSize;
avMag /= ensembleSize;
avMag2 /= ensembleSize;
magSus = 2*(avMag2 - avMag * avMag)/T;
specHeat = (avEn2 - avEn * avEn)/(T * T);
if(magLast10->size == 10)
avMagLast10 -= dequeue(magLast10);
enqueue(magLast10, magSus);
avMagLast10 += avMag;
fprintf(fp, "%f %f %f %f %f\n", T/2.269185, avEn/(XLENGTH*YLENGTH), avMag/(XLENGTH*YLENGTH), magSus/(XLENGTH*YLENGTH), specHeat/(XLENGTH*YLENGTH));
printf("%f %f %f %f %f\n", T/2.269185, avEn/(XLENGTH*YLENGTH), avMag/(XLENGTH*YLENGTH), magSus/(XLENGTH*YLENGTH), specHeat/(XLENGTH*YLENGTH));
}
gsl_rng_free(r);
matrix_free_int(spin);
matrix_free_double(boltzProbs);
free(magLast10);
fclose(fp);
return 0;
}