Statistiques
| Révision :

root / Ising / Serial / Ising2D-MWC.py @ 308

Historique | Voir | Annoter | Télécharger (2,4 ko)

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
# Thanks to George Marsaglia : http://en.wikipedia.org/wiki/Multiply-with-carry
8

    
9
import sys
10
import numpy
11
#import pylab
12
from PIL import Image
13
from math import exp
14
from random import random
15
import time
16

    
17
z=0
18
w=0
19

    
20
def ImageOutput(sigma,prefix):
21
        Max=sigma.max()
22
        Min=sigma.min()
23

    
24
        # Normalize value as 8bits Integer
25
        SigmaInt=(255*(sigma-Min)/(Max-Min)).astype('uint8')
26
        image = Image.fromarray(SigmaInt)
27
        image.save("%s.jpg" % prefix)
28

    
29
SIZE=256
30

    
31
def Metropolis(sigma,J,T,iterations,seed_w,seed_z):
32
        Energy=0
33
        
34
        global z
35
        global w
36

    
37
        z=seed_z
38
        w=seed_w
39
        
40
        for p in range(0,iterations):
41
                # Random access coordonate
42
                #X,Y=numpy.random.randint(SIZE),numpy.random.randint(SIZE)
43
                
44
                # def znew():
45
                #         global z
46
                #         z=numpy.uint32(36969*(z&65535)+(z>>16))
47
                #         return(z)
48
                # def wnew():
49
                #         global w
50
                #         w=numpy.uint32(18000*(w&65535)+(w>>16))
51
                #         return(w)
52
                # def MWC(): return(numpy.uint32((znew()<<16)+wnew()))
53
                # def MWCfp(): return((MWC() + 1.0) * 2.328306435454494e-10)
54
                
55
                def MWC(): 
56
                        global w
57
                        global z
58
                        z=numpy.uint32(36969*(z&65535)+(z>>16))
59
                        w=numpy.uint32(18000*(w&65535)+(w>>16))
60
                        return(numpy.uint32((z<<16)+w))
61

    
62
                def MWCfp(): 
63
                        global w
64
                        global z
65
                        z=numpy.uint32(36969*(z&65535)+(z>>16))
66
                        w=numpy.uint32(18000*(w&65535)+(w>>16))
67
                        return(((numpy.uint32((z<<16)+w))+1.0)*
68
                               2.328306435454494e-10)
69

    
70
                X=numpy.uint32(SIZE*MWCfp())
71
                Y=numpy.uint32(SIZE*MWCfp())
72

    
73
                #print X,Y
74
                
75
                DeltaE=2.*J*sigma[X,Y]*(sigma[X,(Y+1)%SIZE]+
76
                                        sigma[X,(Y-1)%SIZE]+
77
                                        sigma[(X-1)%SIZE,Y]+
78
                                        sigma[(X+1)%SIZE,Y])
79
                
80
                if DeltaE < 0. or random() < exp(-DeltaE/T):
81
                        sigma[X,Y]=-sigma[X,Y]
82
                        Energy+=DeltaE
83
                        
84
        return(Energy)
85

    
86
if __name__=='__main__':
87

    
88
    J=1.
89
    T=0.5
90

    
91
    iterations=numpy.uint32(SIZE*SIZE*SIZE)
92
    
93
    sigma=numpy.where(numpy.random.randn(SIZE,SIZE)>0,1,-1).astype(numpy.int8)
94

    
95
    ImageOutput(sigma,"Ising2D_OpenCL_%i_Initial" % (SIZE))
96

    
97
    seed_w=numpy.uint32(37)
98
    seed_z=numpy.uint32(91)
99
        
100
    i=0
101
    step=262144
102
    while (step*i < iterations):
103
        start=time.time()
104
        Metropolis(sigma,J,T,step,seed_w*(i+1),seed_z*((i+1)%SIZE))
105
        stop=time.time()
106
        elapsed = (stop-start)
107
        print "Iteration %i in %f: " % (i,elapsed)
108
        ImageOutput(sigma,"Ising2D_OpenCL_%i_%.3i_Final" % (SIZE,i))
109
        i=i+1
110