Guest User

Untitled

a guest
Dec 13th, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.67 KB | None | 0 0
  1. from __future__ import print_function
  2. import time
  3. import random
  4. cimport cython
  5. import numpy as np
  6. cimport numpy as np
  7. import matplotlib.pyplot as plt
  8. import matplotlib.animation as animation
  9.  
  10. from numpy import int32
  11. from numpy cimport int32_t
  12.  
  13. from libcpp cimport bool
  14. from libc.math cimport sqrt, cos, sin, pi, exp
  15. from libc.stdlib cimport rand, RAND_MAX, srand
  16.  
  17. srand(420)
  18.  
  19. cdef float prec = 10 ** 3
  20. cdef int picture = 350
  21. cdef int matrix_size = 1000
  22. ctypedef np.uint8_t uint8
  23.  
  24. @cython.boundscheck(False)
  25. @cython.wraparound(False)
  26. @cython.cdivision(True)
  27. def slope(float I, float E, int rivers, float D):
  28. cdef int x, y, i, j, k, step
  29. cdef float dif, prob_sum, prob_0, direction, temp
  30.  
  31. cdef int x_dim = 300
  32. cdef int y_dim = 200
  33.  
  34. cdef float prob[4]
  35. cdef float prob_partial[4]
  36.  
  37. start = time.time()
  38. ims = []
  39. fig = plt.figure()
  40.  
  41. cdef int moves[4][2]
  42. moves[0][:] = [1, 0]
  43. moves[1][:] = [0, 1]
  44. moves[2][:] = [-1, 0]
  45. moves[3][:] = [0, -1]
  46.  
  47. grid_h0 = np.zeros((300, 200), dtype = float)
  48. for i in range(300):
  49. grid_h0[i][:] = (<float>i) * I
  50. cdef double[:, :] grid_h = grid_h0
  51. # print(grid_h0)
  52.  
  53. grid_dh_x0 = np.zeros((300, 200), dtype = np.float)
  54. grid_dh_y0 = np.zeros((300, 200), dtype = np.float)
  55. cdef double[:, :] grid_dh_x = grid_dh_x0
  56. cdef double[:, :] grid_dh_y = grid_dh_y0
  57.  
  58. grid_m0 = np.zeros((300, 200), dtype = np.float)
  59. cdef double[:, :] grid_m = grid_m0
  60.  
  61.  
  62. for i in range(rivers):
  63. for j in range(50):
  64. #grid_dh_x = grid_h - np.roll(grid_h, + 1, axis = 0) #difference in x dimension between
  65. #grid_dh_y = grid_h - np.roll(grid_h, + 1, axis = 1)
  66.  
  67. x = rand() % 300
  68. y = rand() % 200
  69. grid_m[x][y] = 1
  70. print(grid_m)
  71. while x > 2:
  72. print(x, " ", y)
  73. prob[:] = [0, 0, 0, 0]
  74. prob_partial[:] = [0, 0, 0, 0]
  75. prob_sum = 0
  76.  
  77. prob[0] = grid_h[x][y] - grid_h[(x + 1) % x_dim][y]
  78. prob[1] = grid_h[x][y] - grid_h[x][(y + 1) % y_dim]
  79. prob[2] = grid_h[x][y] - grid_h[(x - 1) % x_dim][y]
  80. prob[3] = grid_h[x][y] - grid_h[x][(y - 1) % y_dim]
  81. print(prob)
  82. for k in range(4):
  83. temp = 0
  84. prob_partial[k] = prob_sum
  85. if prob[k] >= 0:
  86. temp = exp(E * prob[k])
  87. prob_sum += temp
  88.  
  89. # if grid_dh_x[x][y] >= 0:
  90. # prob_sum += exp(E * grid_dh_x[x][y])
  91. # if grid_dh_y[x][y] >= 0:
  92. # prob[1] = prob_sum
  93. # prob_sum += exp(E * grid_dh_y[x][y])
  94. # if grid_dh_x[x - 1][y] <= 0:
  95. # prob[2] = prob_sum
  96. # prob_sum += exp(-E * grid_dh_x[x - 1][y])
  97. # if grid_dh_y[x][y - 1] <= 0:
  98. # prob[3] = prob_sum
  99. # prob_sum += exp(-E * grid_dh_x[x][y - 1])
  100. # print(prob)
  101. # print(prob_sum)
  102. step = 0
  103. direction = (<float>rand() / RAND_MAX) * prob_sum
  104. for k in range(4):
  105. if direction > prob_partial[k]:
  106. step = k
  107. x = (x + moves[step][0]) % x_dim
  108. y = (y + moves[step][1]) % y_dim
  109. #print(x, y)
  110. grid_m[x][y] = 1
  111.  
  112.  
  113. grid_h0 -= D * grid_m0
  114. grid_m[:][:] = 0
  115.  
  116. print(grid_h0)
  117. plt.matshow(grid_h0)
  118. plt.show()
  119.  
  120.  
  121. # if particles % 50 == 0:
  122. # print(" " + str(format(time.time() - start, ".1f")) + "s: " + str(particles) + "/" + str(max_par), end = '\r')
  123. # ttl = plt.text(450, 0, str(particles) + "/" + str(max_par), horizontalalignment = 'center', verticalalignment = 'bottom')
  124. # im = plt.imshow(grid0[offset - picture:offset + picture, offset - picture:offset + picture], animated = True, interpolation = "none", extent = [-picture, picture, -picture, picture])
  125. # ims.append([im, ttl], )
  126. #
  127. # print("Making an animation...")
  128. # ani = animation.ArtistAnimation(fig, ims, interval = 50, blit = True, repeat_delay = 100000)
  129. # plt.title("DLA growth for " + str(max_par) + " particles with probability of 1/" + str(probability) + " to stick")
  130. # ani.save(str(max_par) + "/" + str(max_par) + "_growth_" + str(probability) + ".mp4", dpi = 150)
  131. #
  132. # plt.matshow(grid[offset - picture:offset + picture, offset - picture:offset + picture], extent = [-picture, picture, -picture, picture])
  133. # plt.title("DLA growth for " + str(max_par) + " particles with probability of 1/" + str(probability) + " to stick", fontsize = '10')
  134. # plt.savefig(str(max_par) + "/" + str(max_par) + "_" + str(probability) + ".png", dpi = 600)
  135. # plt.close()
  136. # exec_time = time.time() - start
  137. # print("Calculated " + str(max_par) + " particle aggregation with probability of 1/" + str(probability) + " to stick in " + str(format(exec_time, ".1f")) + "s")
  138. # return 0
Advertisement
Add Comment
Please, Sign In to add comment