Révision 90
| Ising/Serial/Ising2D.py (revision 90) | ||
|---|---|---|
| 1 |
#!/usr/bin/env python |
|
| 2 |
# |
|
| 3 |
# Ising2D model in serial mode |
|
| 4 |
# |
|
| 5 |
# CC BY-NC-SA 2011 : <emmanuel.quemener@ens-lyon.fr> |
|
| 6 |
|
|
| 7 |
import sys |
|
| 8 |
import numpy |
|
| 9 |
from PIL import Image |
|
| 10 |
from math import exp |
|
| 11 |
from random import random |
|
| 12 |
import time |
|
| 13 |
import getopt |
|
| 14 |
import matplotlib.pyplot as plt |
|
| 15 |
|
|
| 16 |
def ImageOutput(sigma,prefix): |
|
| 17 |
Max=sigma.max() |
|
| 18 |
Min=sigma.min() |
|
| 19 |
|
|
| 20 |
# Normalize value as 8bits Integer |
|
| 21 |
SigmaInt=(255*(sigma-Min)/(Max-Min)).astype('uint8')
|
|
| 22 |
image = Image.fromarray(SigmaInt) |
|
| 23 |
image.save("%s.jpg" % prefix)
|
|
| 24 |
|
|
| 25 |
def Metropolis(sigma,J,B,T,iterations): |
|
| 26 |
start=time.time() |
|
| 27 |
|
|
| 28 |
SizeX,SizeY=sigma.shape |
|
| 29 |
|
|
| 30 |
for p in xrange(0,iterations): |
|
| 31 |
# Random access coordonate |
|
| 32 |
X,Y=numpy.random.randint(SizeX),numpy.random.randint(SizeY) |
|
| 33 |
|
|
| 34 |
DeltaE=J*sigma[X,Y]*(2*(sigma[X,(Y+1)%SizeY]+ |
|
| 35 |
sigma[X,(Y-1)%SizeY]+ |
|
| 36 |
sigma[(X-1)%SizeX,Y]+ |
|
| 37 |
sigma[(X+1)%SizeX,Y])+B) |
|
| 38 |
|
|
| 39 |
if DeltaE < 0. or random() < exp(-DeltaE/T): |
|
| 40 |
sigma[X,Y]=-sigma[X,Y] |
|
| 41 |
duration=time.time()-start |
|
| 42 |
return(duration) |
|
| 43 |
|
|
| 44 |
def Magnetization(sigma,M): |
|
| 45 |
return(numpy.sum(sigma)/(sigma.shape[0]*sigma.shape[1]*1.0)) |
|
| 46 |
|
|
| 47 |
def Energy(sigma,J): |
|
| 48 |
# Copier et caster |
|
| 49 |
E=numpy.copy(sigma).astype(numpy.float32) |
|
| 50 |
|
|
| 51 |
# Appel par slice |
|
| 52 |
E[1:-1,1:-1]=-J*E[1:-1,1:-1]*(E[:-2,1:-1]+E[2:,1:-1]+ |
|
| 53 |
E[1:-1,:-2]+E[1:-1,2:]) |
|
| 54 |
|
|
| 55 |
# Bien nettoyer la peripherie |
|
| 56 |
E[:,0]=0 |
|
| 57 |
E[:,-1]=0 |
|
| 58 |
E[0,:]=0 |
|
| 59 |
E[-1,:]=0 |
|
| 60 |
|
|
| 61 |
Energy=numpy.sum(E) |
|
| 62 |
|
|
| 63 |
return(Energy/(E.shape[0]*E.shape[1]*1.0)) |
|
| 64 |
|
|
| 65 |
def DisplayCurves(T,E,M,J,B): |
|
| 66 |
|
|
| 67 |
plt.xlabel("Temperature")
|
|
| 68 |
plt.ylabel("Energy")
|
|
| 69 |
|
|
| 70 |
Experience,=plt.plot(T,E,label="Energy") |
|
| 71 |
|
|
| 72 |
plt.legend() |
|
| 73 |
plt.show() |
|
| 74 |
|
|
| 75 |
|
|
| 76 |
if __name__=='__main__': |
|
| 77 |
|
|
| 78 |
# Set defaults values |
|
| 79 |
# Coupling factor |
|
| 80 |
J=1. |
|
| 81 |
# Magnetic Field |
|
| 82 |
B=0. |
|
| 83 |
# Size of Lattice |
|
| 84 |
Size=256 |
|
| 85 |
# Default Temperatures (start, end, step) |
|
| 86 |
Tmin=0.1 |
|
| 87 |
Tmax=5 |
|
| 88 |
Tstep=0.1 |
|
| 89 |
# Default Number of Iterations |
|
| 90 |
Iterations=Size*Size |
|
| 91 |
|
|
| 92 |
# Curves is True to print the curves |
|
| 93 |
Curves=False |
|
| 94 |
|
|
| 95 |
try: |
|
| 96 |
opts, args = getopt.getopt(sys.argv[1:],"hcj:b:z:i:s:e:p:",["coupling=","magneticfield=","size=","iterations=","tempstart=","tempend=","tempstep="]) |
|
| 97 |
except getopt.GetoptError: |
|
| 98 |
print '%s -j <Coupling Factor> -b <Magnetic Field> -z <Size of Lattice> -i <Iterations> -s <Minimum Temperature> -e <Maximum Temperature> -p <steP Temperature> -c (Print Curves)' % sys.argv[0] |
|
| 99 |
sys.exit(2) |
|
| 100 |
|
|
| 101 |
|
|
| 102 |
for opt, arg in opts: |
|
| 103 |
if opt == '-h': |
|
| 104 |
print '%s -j <Coupling Factor> -b <Magnetic Field> -z <Size of Lattice> -i <Iterations> -s <Minimum Temperature> -e <Maximum Temperature> -p <steP Temperature> -c (Print Curves)' % sys.argv[0] |
|
| 105 |
sys.exit() |
|
| 106 |
elif opt == '-c': |
|
| 107 |
Curves=True |
|
| 108 |
elif opt in ("-j", "--coupling"):
|
|
| 109 |
J = float(arg) |
|
| 110 |
elif opt in ("-b", "--magneticfield"):
|
|
| 111 |
B = float(arg) |
|
| 112 |
elif opt in ("-s", "--tempmin"):
|
|
| 113 |
Tmin = float(arg) |
|
| 114 |
elif opt in ("-e", "--tempmax"):
|
|
| 115 |
Tmax = float(arg) |
|
| 116 |
elif opt in ("-p", "--tempstep"):
|
|
| 117 |
Tstep = float(arg) |
|
| 118 |
elif opt in ("-i", "--iterations"):
|
|
| 119 |
Iterations = int(arg) |
|
| 120 |
elif opt in ("-z", "--size"):
|
|
| 121 |
Size = int(arg) |
|
| 122 |
|
|
| 123 |
print "Coupling Factor : %s" % J |
|
| 124 |
print "Magnetic Field : %s" % B |
|
| 125 |
print "Size of lattice : %s" % Size |
|
| 126 |
print "Iterations : %s" % Iterations |
|
| 127 |
print "Temperature on start : %s" % Tmin |
|
| 128 |
print "Temperature on end : %s" % Tmax |
|
| 129 |
print "Temperature step : %s" % Tstep |
|
| 130 |
|
|
| 131 |
LAPIMAGE=False |
|
| 132 |
|
|
| 133 |
sigmaIn=numpy.where(numpy.random.randn(Size,Size)>0,1,-1).astype(numpy.int8) |
|
| 134 |
|
|
| 135 |
ImageOutput(sigmaIn,"Ising2D_Serial_%i_Initial" % (Size)) |
|
| 136 |
|
|
| 137 |
Trange=numpy.arange(Tmin,Tmax+Tstep,Tstep) |
|
| 138 |
|
|
| 139 |
E=[] |
|
| 140 |
M=[] |
|
| 141 |
|
|
| 142 |
for T in Trange: |
|
| 143 |
# Indispensable d'utiliser copy : [:] ne fonctionne pas avec numpy ! |
|
| 144 |
sigma=numpy.copy(sigmaIn) |
|
| 145 |
duration=Metropolis(sigma,J,B,T,Iterations) |
|
| 146 |
E=numpy.append(E,Energy(sigma,J)) |
|
| 147 |
M=numpy.append(M,Magnetization(sigma,B)) |
|
| 148 |
ImageOutput(sigma,"Ising2D_Serial_%i_%1.1f_Final" % (Size,T)) |
|
| 149 |
|
|
| 150 |
print "CPU Time : %f" % (duration) |
|
| 151 |
print "Total Energy at Temperature %f : %f" % (T,E[-1]) |
|
| 152 |
print "Total Magnetization at Temperature %f : %f" % (T,M[-1]) |
|
| 153 |
|
|
| 154 |
if Curves: |
|
| 155 |
DisplayCurves(Trange,E,M,J,B) |
|
| 156 |
|
|
| 157 |
# Save output |
|
| 158 |
numpy.savez("Ising2D_Serial_%i_%.8i" % (Size,Iterations),(Trange,E,M))
|
|
| 159 |
|
|
Formats disponibles : Unified diff