Advertisement
Guest User

Untitled

a guest
Nov 18th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.31 KB | None | 0 0
  1. import numpy as np
  2. import numpy.random as npr
  3. import math
  4. import matplotlib.pyplot as plt
  5. import time
  6.  
  7. start_time = time.time()
  8. Lx = 20  # rozmiar boxa w gore/w dol
  9. Ly = 20  # rozmiar boxa lewo/prawo
  10. c = 0.5  # stezenie czastek w boxie
  11. n = c * (Lx * Ly)
  12. n = int(n)
  13. shakes = 100  # ilosc krokow MCS
  14. MCS = 100  # ilosc symulacji monte carlo
  15. MCS_counter = MCS  # do wykresu, da sie pewnie lepiej ale dziala
  16. Box = np.zeros((Lx, Ly))
  17. class Point(object):
  18.     def __init__(self, x, y):
  19.         x = int(x)
  20.         y = int(y)
  21.         self.X = x
  22.         self.Y = y
  23.         self.xcounter = 0.0
  24.         self.xcounter = float(self.xcounter)
  25.         self.ycounter = 0.0
  26.         self.ycounter = float(self.ycounter)
  27.  
  28.     def __repr__(self):
  29.         return "%d, %d" % (self.X, self.Y)
  30.  
  31.     def move(self, dx, dy):
  32.  
  33.         self.X = self.X + dx
  34.         self.Y = self.Y + dy
  35.         if dx != 0:
  36.             self.xcounter += dx
  37.         if dy != 0:
  38.             self.ycounter += dy
  39.  
  40.     def random_walk(self):
  41.         walk = npr.randint(1, 5)
  42.  
  43.         if walk == 1:  # ruch w gore
  44.             if self.X == 0 and filled_box[Lx - 1, self.Y] != 1:
  45.                 filled_box[self.X, self.Y] = 0
  46.                 self.X = Lx - 1
  47.                 filled_box[self.X, self.Y] = 1
  48.                 self.xcounter += -1
  49.  
  50.             elif self.X == 0 and filled_box[Lx - 1, self.Y] == 1:
  51.                 pass
  52.  
  53.             elif filled_box[self.X - 1, self.Y] != 1:
  54.                 filled_box[self.X, self.Y] = 0
  55.                 self.move(-1, 0)
  56.                 filled_box[self.X, self.Y] = 1
  57.  
  58.         if walk == 3:  # ruch w dol
  59.  
  60.             if self.X == Lx - 1 and filled_box[0, self.Y] != 1:
  61.                 filled_box[self.X, self.Y] = 0
  62.                 self.X = 0
  63.                 filled_box[self.X, self.Y] = 1
  64.                 self.xcounter += 1
  65.  
  66.  
  67.             elif self.X == Lx - 1 and filled_box[0, self.Y] == 1:
  68.                 pass
  69.  
  70.             elif filled_box[self.X + 1, self.Y] != 1:
  71.                 filled_box[self.X, self.Y] = 0
  72.                 self.move(1, 0)
  73.                 filled_box[self.X, self.Y] = 1
  74.  
  75.         if walk == 2:  # ruch w lewo
  76.             if self.Y == 0 and filled_box[self.X, Ly - 1] != 1:
  77.                 filled_box[self.X, self.Y] = 0
  78.                 self.Y = Ly - 1
  79.                 filled_box[self.X, self.Y] = 1
  80.                 self.ycounter += -1
  81.  
  82.             elif self.Y == 0 and filled_box[self.X, Ly - 1] == 1:
  83.                 pass
  84.  
  85.             elif filled_box[self.X, self.Y - 1] != 1:
  86.                 filled_box[self.X, self.Y] = 0
  87.                 self.move(0, -1)
  88.                 filled_box[self.X, self.Y] = 1
  89.  
  90.         if walk == 4:  # ruch w prawo
  91.             if self.Y == Ly - 1 and filled_box[self.X, 0] != 1:
  92.                 filled_box[self.X, self.Y] = 0
  93.                 self.Y = 0
  94.                 filled_box[self.X, self.Y] = 1
  95.                 self.ycounter += 1
  96.  
  97.             elif self.Y == Ly - 1 and filled_box[self.X, 0] == 1:
  98.                 pass
  99.  
  100.             elif filled_box[self.X, self.Y + 1] != 1:
  101.                 filled_box[self.X, self.Y] = 0
  102.                 self.move(0, 1)
  103.                 filled_box[self.X, self.Y] = 1
  104. def population(n):
  105.     coords = []
  106.     for j in range(0, Lx):
  107.         for k in range(0, Ly):
  108.             coords.append(Point(j, k))
  109.  
  110.     i = 0
  111.     atoms = []
  112.     while i < n:
  113.         atoms.append(coords.pop(npr.randint(0, len(coords))))
  114.         i += 1
  115.  
  116.     return atoms
  117. def intothebox(atoms, box):
  118.     for i in range(0, n):
  119.         box[(atoms[i]).X, (atoms[i]).Y] = 1
  120.     return box
  121. def shake(pop):
  122.     for i in pop:
  123.         i.random_walk()
  124.     return pop
  125. R_vs_shakes = np.zeros((MCS, shakes))
  126. while MCS >= 1:
  127.     pop = []
  128.     filled_box = []
  129.     Box = np.zeros((Lx, Ly))
  130.  
  131.     pop = population(n)
  132.     filled_box = intothebox(pop, Box)
  133.     D = []
  134.  
  135.     for i in range(1, shakes + 1):  # robie tyle krokow monte carlo - tyle razy ruszam atomami
  136.  
  137.         dr = []
  138.         pop = shake(pop)
  139.  
  140.         filled_box = intothebox(pop, Box)
  141.  
  142.         r = 0
  143.  
  144.         for j in range(0, n):  # licze dlugosc wektora do kwadratu dla kazdego atomu
  145.  
  146.             r = (math.sqrt((pop[j].xcounter ** 2) + (pop[j].ycounter ** 2))) ** 2
  147.             dr.append(r)
  148.  
  149.         R_vs_shakes[MCS - 1, i - 1] = sum(dr) / len(
  150.             dr)  # nparray w ktorym zapisuje sredni kwadrat wektora przemieszczenia dla kazdej symulacji po i krokach (numerowanie od 0 w nparray - dlatego -1)
  151.  
  152.     MCS -= 1
  153. # tu licze srednie D dla (MCS) symulacji po (i) krokach - z R_vs_shakes jak wyzej
  154. R_vs_shakes = np.sum(R_vs_shakes, axis=0) / np.size(R_vs_shakes, axis=0)
  155. D_final = [0] * np.size(R_vs_shakes)
  156. i = 0
  157. for i in range(0, np.size(R_vs_shakes)):
  158.     D_final[i] = 0.25 * R_vs_shakes[i] / (i + 1)
  159.  
  160. # rysuje srednie D dla ilus MCS
  161. elapsed_time = time.time() - start_time
  162. print elapsed_time
  163.  
  164. #zapisuje do pliku
  165. output=dict(zip(range(1, len(D_final) + 1),D_final))
  166.  
  167. with open('099.csv', 'w') as f:
  168.     for key in output.keys():
  169.         f.write("%f \t %f\n" %(key, output[key]))
  170.  
  171. # plt.plot(range(1, len(D_final) + 1), D_final, 'ro')
  172. # plt.xlabel('Ilosc krokow ')
  173. # plt.ylabel('Wspolczynnik dyfuzji D')
  174. # plt.title('Box ' + str(Lx) + 'x' + str(Ly) + ', ' + str(n) + ' czastek,' + str(MCS_counter) + ' symulacji,' + str(
  175. #     shakes) + ' krokow MCS')
  176. #
  177. # plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement