Advertisement
Guest User

fixed lattice

a guest
Oct 20th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.11 KB | None | 0 0
  1. import scipy.integrate as spi
  2. import numpy as np
  3. import pylab as pl  
  4. import time, os
  5.  
  6. ### Select if you want to make a video (faster way)
  7. #video=1
  8. ### or just0in-time plotting
  9. video=0
  10. t = time.time()
  11.  
  12. n=25;
  13. beta=1.0;
  14. gamma=0.1;
  15. mu=0.0001;
  16. rho=0.1;
  17. X0=0.1;
  18. nI=4.;
  19. N0=1.0;
  20. nu=mu
  21. timestep=1.;
  22. ND=MaxTime=500;
  23. C=np.arange(0.0, 1.0, 0.001)
  24. X=X0*np.ones((n,n)); Y=np.zeros(n*n);
  25. ind=np.ceil(n*n*np.random.uniform(size=int(nI)))
  26. for i in range(len(ind)):
  27.     Y[int(ind[i])]=0.001*X0;
  28.  
  29. INPUT1=np.hstack((X0*np.ones((n*n)),Y))
  30.  
  31. Y3=Y
  32. Y=np.reshape(Y, ((n,n)))
  33. Y2=Y
  34.  
  35. INPUT3=np.hstack((X,Y));
  36.  
  37. INPUT2=np.reshape(INPUT3, ((2*n*n,1)))
  38.  
  39. for i in range(len(INPUT1)):
  40.     INPUT1[i]=INPUT2[i]
  41.  
  42. INPUT=INPUT1
  43. ndem=np.zeros((n,n))
  44. Size=n
  45. N=N0
  46. ### Note the size loop
  47. for i in range(Size):
  48.     for j in range(Size):
  49.         ndem[i][j]=(1-4*rho)*N
  50.         if i>0 : ndem[i][j] += rho*N
  51.         if i<(Size-1) : ndem[i][j] += rho*N
  52.         if j>0 : ndem[i][j] += rho*N
  53.         if j<(Size-1) : ndem[i][j] += rho*N
  54.            
  55. def diff_eqs(INP,t):
  56.     V=INP
  57.     Y=np.zeros(2*n*n)
  58.  
  59.     ### internal dynamics
  60.     for i in range(Size):
  61.         for j in range(Size):
  62.             ss=i+j*Size*2
  63.             ii=Size+i+j*Size*2
  64.             Y[ss]=nu - beta*(1-4*rho)*V[ss]*V[ii]/ndem[i][j] - mu * V[ss]
  65.             Y[ii]=beta*(1-4*rho)*V[ss]*V[ii]/ndem[i][j] - (gamma + mu) * V[ii]
  66.        
  67.             ### Interactions with four neighbours
  68.             if i>0:
  69.                 FoI = beta*V[ss]*rho*V[ii-1]/ndem[i][j]
  70.                 Y[ss]-=FoI
  71.                 Y[ii]+=FoI
  72.            
  73.             if i<(Size-1):
  74.                 FoI = beta*V[ss]*rho*V[ii+1]/ndem[i][j]
  75.                 Y[ss]-=FoI
  76.                 Y[ii]+=FoI
  77.            
  78.             if j>0:
  79.                 FoI = beta*V[ss]*rho*V[ii-Size*2]/ndem[i][j]
  80.                 Y[ss]-=FoI
  81.                 Y[ii]+=FoI
  82.            
  83.             if j<(Size-1):
  84.                 FoI = beta*V[ss]*rho*V[ii+Size*2]/ndem[i][j]
  85.                 Y[ss]-=FoI
  86.                 Y[ii]+=FoI
  87.            
  88.     return Y
  89.  
  90.  
  91. ola=np.zeros((n*n), dtype=np.integer)
  92. for i in range(Size):
  93.     for j in range(Size):
  94.         ola[i*n+j]=i*Size*2+j
  95.  
  96. ola1=ola+n
  97. tcS=[(n*n*X0)]
  98. tcI=[sum(Y3[range(n*n)])]
  99. tc22=np.zeros((n,n))
  100. if video==1:
  101.     if os.path.exists('movie')==True: pass
  102.     else: os.mkdir('movie')
  103.  
  104.     for k in range(ND):
  105.         t_range = np.arange(2.0)   
  106.         RES = spi.odeint(diff_eqs,INPUT,t_range)
  107.         INPUT=RES[-1]
  108.  
  109.         tcS.append(sum(RES[-1][ola]))
  110.         tcI.append(sum(RES[-1][ola1]))
  111.  
  112.         if k%1==0:
  113.             pl.clf()
  114.             tc22=np.reshape(RES[-1][ola1], (n,n))
  115.             pl.subplot(211)
  116.             pl.pcolor(tc22, cmap='Spectral')
  117.             pl.title('Coupled Lattice Model')
  118.             pl.colorbar()  
  119.  
  120.             pl.subplot(413)
  121.             pl.plot(tcS, color='b')
  122.             pl.ylabel('Susceptible')
  123.             pl.subplot(414)
  124.             pl.plot(tcI, color='r')
  125.             pl.ylabel('Infected')
  126.             pl.xlabel('Time (days)')
  127.             pl.savefig("movie/frame_%04d.png" %k)
  128.             ## watch the progress
  129.             print(k)
  130.  
  131.     ## You will mencoder from mplayer for this to work
  132.     ## With windows you have to modify the path
  133.     ## With linux if you have mencoder istall usually it should work
  134.     ## Format is windows media player - plays on windows
  135.     ## You could try other formats also
  136.     print('Please wait... Converting pictures to avis....')
  137.     ## Select different video speed
  138. #    os.system('mencoder "mf://movie/*.png" -mf fps=5:type=png -ovc lavc -lavcopts vcodec=wmv1 -of avi -o movie/movie_very_slow.avi')
  139. #    os.system('mencoder "mf://movie/*.png" -mf fps=10:type=png -ovc lavc -lavcopts vcodec=wmv1 -of avi -o movie/movie_slow.avi')
  140.     os.system('mencoder "mf://movie/*.png" -mf fps=25:type=png -ovc lavc -lavcopts vcodec=wmv1 -of avi -o movie/movie_fast.avi')
  141.     print('Convertion completed. Hope it worked...')
  142.     ## Delete images to save work space
  143.     os.system('rm movie/*.png')
  144.  
  145.     print("Operation took %g seconds" % (time.time()-t))
  146.  
  147. else:
  148.     ### You could also try plotting at each step but it is slow
  149.     pl.ion()
  150.     for k in range(ND):
  151.         t_range = np.arange(2.0)   
  152.         RES = spi.odeint(diff_eqs,INPUT,t_range)
  153.         INPUT=RES[-1]
  154.  
  155.         tcS.append(sum(RES[-1][ola]))
  156.         tcI.append(sum(RES[-1][ola1]))
  157.        
  158.         ### Changing the k%50 parameter you change the frames you are watching
  159.         ###  With 1 you can see all the graphs
  160.         if k%10==0:
  161.             pl.clf()
  162.             tc22=np.reshape(RES[-1][ola1], (n,n))
  163.             pl.subplot(211)
  164.             pl.pcolor(tc22, cmap='Spectral')
  165.             pl.title('Coupled Lattice Model')
  166.             pl.colorbar()  
  167.  
  168.             pl.subplot(413)
  169.             pl.plot(tcS, color='b')
  170.             pl.ylabel('Susceptible')
  171.            
  172.             pl.subplot(414)
  173.             pl.plot(tcI, color='r')
  174.             pl.ylabel('Infected')
  175.             pl.xlabel('Time (days)')
  176.            
  177.             ### watch the progress
  178.             print(k)
  179.             pl.draw()
  180.     pl.ioff()
  181.     pl.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement