Advertisement
Guest User

Untitled

a guest
Nov 18th, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.88 KB | None | 0 0
  1. # Grid settings. Replace them with your values.
  2. # Grid size.
  3. cols = 6
  4. rows = 6
  5. # X and Y steps.
  6. x_step = 0.2
  7. y_step = 0.2
  8.  
  9. # Replace it with your Phi() function.
  10. def phi(x, y):
  11.     t1 = 1 - x * x
  12.     t2 = 1 - y * y
  13.     return t1 * t1 + t2 * t2
  14.  
  15. def X(i):
  16.     return i * x_step
  17.  
  18. def Y(i):
  19.     return i * y_step
  20.  
  21. # Replace it with your F() function.
  22. def F(x, y):
  23.     return 4 * (2 - 3 * x * x - 3 * y * y)
  24.  
  25. def cell(m, row, col):
  26.     return m[row * cols + col]
  27.  
  28. def set_cell(m, row, col, value):
  29.     m[row * cols + col] = value
  30.  
  31. # Scalar product of matrices a and b in internal points.
  32. def scalar(a, b):
  33.     ret = 0
  34.     for i in range(1, rows - 1):
  35.         for j in range(1, cols - 1):
  36.             ret += x_step * y_step * cell(a, i, j) * cell(b, i, j)
  37.     return ret
  38.  
  39. # Negative result of Laplas(Matrix m). Creates a new matrix.
  40. def laplas_5_matrix(m):
  41.     ret = m.copy()
  42.     for i in range(1, rows - 1):
  43.         for j in range(1, cols - 1):
  44.             a11 = cell(m, i, j)
  45.             a21 = cell(m, i + 1, j)
  46.             a01 = cell(m, i - 1, j)
  47.             a10 = cell(m, i, j - 1)
  48.             a12 = cell(m, i, j + 1)
  49.             tmp1 = 1.0/(x_step * x_step) * (2*a11 - a01 - a21)
  50.             tmp2 = 1.0/(y_step * y_step) * (2*a11 - a10 - a12)
  51.             set_cell(ret, i, j, -(tmp1 + tmp2))
  52.     return ret
  53.  
  54. # Init P and R.
  55. P = [0] * (cols * rows)
  56. R = P.copy()
  57. for i in range(0, cols):
  58.     set_cell(P, 0, i, phi(X(i), Y(0)))
  59.     set_cell(P, rows - 1, i, phi(X(i), Y(rows - 1)))
  60.  
  61. for i in range(0, rows):
  62.     set_cell(P, i, 0, phi(X(0), Y(i)))
  63.     set_cell(P, i, cols - 1, phi(X(cols - 1), Y(i)))
  64.  
  65. # Calculate R_i using formula R_i = -laplas(P_i) - F.
  66. def calculate_next_R():
  67.     matrix_buffer = laplas_5_matrix(P)
  68.     for i in range(1, rows - 1):
  69.         for j in range(1, cols - 1):
  70.             set_cell(R, i, j, cell(matrix_buffer, i, j) - F(X(j), Y(i)))
  71.     for i in range(0, cols):
  72.         set_cell(R, 0, i, 0)
  73.         set_cell(R, rows - 1, i, 0)
  74.     for i in range(0, rows):
  75.         set_cell(R, i, 0, 0)
  76.         set_cell(R, i, cols - 1, 0)
  77.  
  78. # Print matrix in a readable format.
  79. def print_matrix(m):
  80.     for i in range(rows - 1, -1, -1):
  81.         buf = []
  82.         for j in range(cols):
  83.             buf.append(cell(m, i, j))
  84.         print(buf)
  85.  
  86. # Calculate P_i+1 using formula P_i+1 = P_i - tau * r_or_g_i.
  87. def calculate_next_P(tau, r_or_g):
  88.     for i in range(rows):
  89.         for j in range(cols):
  90.             old = cell(P, i, j)
  91.             set_cell(P, i, j, old - tau * cell(r_or_g, i, j))
  92.  
  93. # Calculate G_i using formula G_i = R_i - alpha * G_i-1.
  94. def calculate_next_G(alpha):
  95.     for i in range(rows):
  96.         for j in range(cols):
  97.             old = cell(G, i, j)
  98.             set_cell(G, i, j, cell(R, i, j) - alpha * old)
  99.  
  100. # Make step 0.
  101. calculate_next_R()
  102.  
  103. # Make step 1.
  104. G = R.copy()
  105. tmp1 = scalar(R, R)
  106. matrix_buffer = laplas_5_matrix(R)
  107. tmp2 = scalar(matrix_buffer, R)
  108. tau = tmp1 / tmp2
  109. calculate_next_P(tau, R)
  110.  
  111. # Make step 2.
  112. calculate_next_R()
  113. matrix_buffer = laplas_5_matrix(R)
  114. tmp1 = scalar(matrix_buffer, G)
  115. matrix_buffer = laplas_5_matrix(G)
  116. tmp2 = scalar(matrix_buffer, G)
  117. alpha = tmp1 / tmp2
  118. # ...
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement