Advertisement
Guest User

CommentedCodeModule4_Phys212_AA

a guest
Apr 21st, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.49 KB | None | 0 0
  1. Created on Sat Apr 21 19:42:16 2018
  2.  
  3. @author: alinaulezko
  4. """
  5. #ignore this
  6. #side = int(trueRand/(side_length-1))
  7. #displacement = trueRand%(side_length-1)
  8.  
  9.  
  10. #Import Modules
  11. import numpy as np
  12. import numpy.random as nprnd
  13. import matplotlib.pyplot as plt
  14.  
  15. #Define variables. The side length of a square matrix is variable.
  16. #The center will be located at the index as specified by the formula.
  17. #A matrix of dimensions side length suqared is created, where all numbers are (for now) zeros.
  18. #At the center position, a 1 is placed. 1 indicates STATIONARY DOT.
  19. #In the beggining, there is only one stationary dot (center)
  20. #StatCounter counts how many stationary points there are.
  21. #logically, in the beggining there is only one.
  22. #reqStat is the maximum amount of stationary points that we want (which is what was specified in the instructions you provided as 1000)
  23. side_length = 1000
  24. center = int((side_length - 1) / 2)
  25. matrix = np.zeros((side_length,side_length))
  26. matrix[center,center] = 1
  27. statCounter = 1
  28. reqStat = 5
  29.  
  30.  
  31. #This is to fill the boundaries of the matrix with numbers that go from 2 (because 1 means stationary and 0 means unocuppied)
  32. #to increasing numbers, like a snake that surrounds the matrix in a clockwise fashion. Please run this part and click "matrix" for clarification
  33. for i in range(side_length):
  34. matrix[i, side_length - 1] = i + side_length + 1
  35. matrix[side_length - 1, -i] = i + (2 * side_length - 1)
  36. matrix[-i, 0] = i + (3 * side_length - 2)
  37. matrix[0, i] = i + 2
  38.  
  39.  
  40.  
  41. #Defines function that will spawn (i.e. generate) a dot (NewDot) form the boundary. This is why the random number "rand" is in the range 2 to boundlength -3
  42. #Once the random number is generated, a new object "NewDot" is created, which is placed at the random number in the matrix boundary as generated in the previous line
  43. def spawn_dot():
  44. bound_length = 4 * side_length - 3
  45. rand = nprnd.randint(2,bound_length+1)
  46. newDot = [np.where(matrix==rand)[0][0],np.where(matrix==rand)[1][0]]
  47. return newDot
  48.  
  49.  
  50. #This function checks whether the new dot is/is not next to a stationary dot.
  51. # The for loop has i's that can be -1, 0, or 1.
  52. #If the x parameter of NewDot is in the first row and it the i = 1 (i.e. the third run of the for loop),
  53. #then keep running the for loop and don't do anything (because moving to the right in this case is illogical)
  54. #The second for loop has j's that can be -1, 0, and 0
  55. #If the y parameter of NewDot is in the first column and and j = 1, don't do anything because you can't move upwards.
  56. #The print statement is here because we were checking that these loops work
  57. #The next statement says that if the neighbouring dot is stationary (aka equals to 1), then make this dot stationary again
  58. #once you make it stationary, add one to the statCounter (stationary dot counter). We made the StatCounter a global variable because it is used in another function below.
  59. def stat_check():
  60. for i in range(-1,2):
  61. if (newDot[0] == (side_length-1) and i == 1):
  62. continue
  63. for j in range (-1,2):
  64. if (newDot[1] == (side_length-1) and j == 1):
  65. continue
  66. print(j, newDot[1], i, newDot[0])
  67. if (matrix[newDot[0]+i,newDot[1]+j] == 1):
  68. #make newDot stationary:
  69. matrix[newDot[0],newDot[1]] = 1
  70. global statCounter
  71. statCounter += 1
  72. return True
  73. return False
  74.  
  75. #This is the function that does the random walk of the dot.
  76. #While the statcheck returns true, generate a random x and y displacement that is either -1 or 1
  77. #The if statements below check for corners and for boundaries, and it reflects them.
  78. #for example, if the new dot is in the upper left corner, reflect x if x=-1 and reflect y if y=-1, and so on
  79. #once we're done with checking for boundaries and corners, make the new dot move according to x and y
  80.  
  81. def rw():
  82. while (not(stat_check())):
  83. x = 2*(nprnd.random() > 0.5) - 1
  84. y = 2*(nprnd.random() > 0.5) - 1
  85. if (newDot == [0,0]):
  86. if (x == -1):
  87. x = -x
  88. if (y == -1):
  89. y = -y
  90. elif (newDot == [side_length-1, 0]):
  91. if (x == 1):
  92. x = -x
  93. if (y == -1):
  94. y = -y
  95. elif (newDot == [side_length-1, side_length-1]):
  96. if (x == 1):
  97. x = -x
  98. if (y == 1):
  99. y = -y
  100. elif (newDot == [0, side_length-1]):
  101. if (x == -1):
  102. x = -x
  103. if (y == 1):
  104. y = -y
  105. elif (int(matrix[newDot[0],newDot[1]] / (side_length - 1)) == 0):
  106. if (y == -1):
  107. y = -y
  108. #the above is a problem. false bounds.
  109. elif (int(matrix[newDot[0],newDot[1]] / (side_length - 1)) == 1):
  110. if (x == 1):
  111. x = -x
  112. elif (int(matrix[newDot[0],newDot[1]] / (side_length - 1)) == 2):
  113. if (y == 1):
  114. y = -y
  115. elif (int(matrix[newDot[0],newDot[1]] / (side_length - 1)) == 3):
  116. if (x == -1):
  117. x = -x
  118. movement = [x,y]
  119. newDot[0] = newDot[0] + movement[0]
  120. newDot[1] = newDot[1] + movement[1]
  121.  
  122.  
  123.  
  124. #Keep spawning dots and ding random walk while the number of stationary dots is lower than the required number of Stat dots
  125.  
  126. while (statCounter < reqStat):
  127. newDot = spawn_dot()
  128. rw()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement