-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMCIsing.java
More file actions
123 lines (107 loc) · 2.9 KB
/
MCIsing.java
File metadata and controls
123 lines (107 loc) · 2.9 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
//Monte Carlo simulations of Ising model using spin flip algorithm
import java.io.*;
import java.util.*;
public class MCIsing {
int L; // lattice size
double JT; // ratio interaction temperature J/T
double M,AbsM; // magnetization of a specific configuration and average of |M|
int spin[][]; // stores the value of the spin
double Z=0.0; //counter(partition function)
double Wsteps; //number of steps after which we write on file
double MCstep=0.0;
//constructor
public MCIsing()
throws FileNotFoundException{
}
//initialization
public void initialization()
throws FileNotFoundException{
PrintWriter pw = new PrintWriter (new FileOutputStream(new File("Magnetization.txt"),false));
Scanner input = new Scanner(new File("parameters.txt"));
L = (int)returnValue(input);
JT = returnValue(input);
int order=(int)returnValue(input); // 0=disordered, 1=ordered
Wsteps=returnValue(input);
spin = new int[L][L];
initSpinCnf(order);
}
public void initSpinCnf(int order)
throws FileNotFoundException{
if(order==1){
for(int i=0; i<L;i++){
for(int j=0;j<L;j++){
spin[i][j]=1;
}
}
M=L*L;
}
else{
for(int i=0; i<L;i++){
for(int j=0;j<L;j++){
if(Math.random()<0.5){
spin[i][j]=1;
M=M+1;
}
else{
spin[i][j]=-1;
M=M-1;
}
}
}
}
//do not initialize if do thermalization
// AbsM=Math.abs(M);
//Z=1.0;//counter initialized to 1 since we include the initial configuration in the calculation of |M|
MCstep=0.0;
// results();
}
public int randomCoord(){
int i = (int)(Math.random()*L);
return i;
}
public double energyDiff(int i, int j){
int nnr = i+1 ; int nnl = i-1 ; // right and left nn neighbors
int nnu = j+1 ; int nnd = j-1 ; // up and down nn neighbors
//check for PBC below
if(nnr==L){nnr=0;}
if(nnl==-1){nnl=L-1;}
if(nnu==L){nnu=0;}
if(nnd==-1){nnd=L-1;}
//calculation of the energy change if spin is flipped
double dE=2.0*spin[i][j]*(spin[nnr][j]+spin[nnl][j]+spin[i][nnu]+spin[i][nnd]);
return dE;
}
public boolean decision(int i, int j, double dE){
boolean yes = false;
double R = Math.exp(-JT*dE);
if(R>1.0 || Math.random()<=R){
yes = true ;
}
return yes;
}
public void update(int i, int j){
M=M-2.0*spin[i][j];
spin[i][j]=-spin[i][j];
}
public void updateCounters(){
MCstep++;
if(MCstep>L*L*10){
Z=Z+1.0;
AbsM=AbsM+Math.abs(M);}
}
public void results()
throws FileNotFoundException{
try{
PrintWriter pw = new PrintWriter (new FileOutputStream(new File("Magnetization.txt"),true));
pw.printf("%-10.7f\n",AbsM/(L*L*Z));
pw.close();}
catch(IOException ioe){
System.err.println("IOException: " + ioe.getMessage());
}
}
public double returnValue(Scanner input){
double dummy=input.nextDouble();
input.nextLine();
return dummy;
}
}