Advertisement
Guest User

Untitled

a guest
Apr 27th, 2015
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.96 KB | None | 0 0
  1. def Poisson_Solver_Neumann(u, v, Nx, Ny, dt, dx, dy, T, xp, yp):
  2. """Solves the 2D Poisson equation implicitly on a staggered grid
  3. using Neumann Boundary Conditions
  4.  
  5. Params:
  6. ------
  7. u, v 2D array of float, x and y velocities
  8. Nx, Ny float, Number of segments in x and y
  9. dt float, time step size
  10. T float, current time
  11. X, Y 2D array of float, meshgrid
  12.  
  13. Returns:
  14. -------
  15. ANeum 2D array of float, A matrix with Neumann conditions
  16. f_RHSn 1D array of float, f(x,y) for Neumann conditions
  17. """
  18. #Building A
  19. ANeum = np.zeros((Nx*Ny,Nx*Ny),dtype=float)
  20.  
  21. a = Nx * Ny
  22. b = Nx * Ny
  23. c_int = -4 * (Nx * Ny)
  24. c_edge = -3 * (Nx * Ny)
  25. c_corner = -2 * (Nx * Ny)
  26. d = Nx * Ny
  27. e = Nx * Ny
  28.  
  29. #Set corner points
  30. ANeum[0,0] = c_corner
  31. ANeum[-1,-1] = c_corner
  32. ANeum[Nx*Ny-Ny,Nx*Ny-Ny] = c_corner
  33. ANeum[Ny-1,Ny-1] = c_corner
  34.  
  35. #Set edges in first block
  36. for j in range(1,Ny-1):
  37. ANeum[j,j] = c_edge
  38. j +=j
  39.  
  40. #Set edges in last block
  41. for j in range((Nx*Ny)-Ny,(Nx*Ny)-2):
  42. ANeum[j+1,j+1] = c_edge
  43. j +=j
  44.  
  45. #Set edges along main diagonal except for first block
  46. for j in range(Nx+1,Ny*Nx):
  47. if j % Nx ==0:
  48. ANeum[j-1,j-1] = c_edge
  49. j +=j
  50.  
  51. #Set edges on main diagonal except for last block
  52. for j in range(Nx,(Ny*Nx)-Nx):
  53. if j % Nx ==0:
  54. ANeum[j,j] = c_edge
  55. j +=j
  56.  
  57. #Second diagonal above and below diagonal
  58. for j in range(Ny,Ny*Nx):
  59. ANeum[j,j-Ny] = a
  60. ANeum[j-Nx,j] = e
  61. j +=j
  62.  
  63. #first diagonal below main diagonal
  64. for j in range(1,Ny*Nx):
  65. if j % Ny ==0:
  66. ANeum[j,j-1] = 0
  67. else:
  68. ANeum[j,j-1] = b
  69. j +=j
  70.  
  71. #first diagonal above main diagonal
  72. for j in range(0,Ny*Nx):
  73. if j % Nx ==0:
  74. ANeum[j-1,j] = 0
  75. else:
  76. ANeum[j-1,j] = d
  77. j +=j
  78.  
  79. #Main Diagonal
  80. for j in range(0,Ny*Nx):
  81. if ANeum[j,j] ==0:
  82. ANeum[j,j] = c_int
  83.  
  84. #Setting random point to Dirichlet
  85. ANeum[0,:] = 0
  86. ANeum[0,0] = 1
  87.  
  88. return ANeum
  89.  
  90. for t in range(0,nt):
  91. un = np.empty_like(u)
  92. vn = np.empty_like(v)
  93. #copy info from last loop, needed for current loop into variable_n
  94. un = u.copy()
  95. vn = v.copy()
  96. #Pn = p.copy()
  97. T = T_0 + t*dt
  98.  
  99. ####STEP 1
  100. ### t -----> t + (1/3)*dt
  101. un, vn = BCs(un, vn)
  102. G1 = Build_G1(un, vn, dx, dy, nu)
  103. G2 = Build_G2(un, vn, dx, dy, nu)
  104. u1 = un + (dt*G1) #put 1/3 back in
  105. v1 = vn + (dt*G2)
  106. u1, v1 = BCs(u1, v1)
  107. u_div = (u1[1:-1,2:-1] - u1[1:-1,1:-2])/dx
  108. v_div = (v1[2:-1,1:-1] - v1[1:-2,1:-1])/dy
  109. #Solve for Pressure Field
  110. b1 = np.zeros((Ny+2,Nx+2), dtype=float)
  111. b1[1:-1,1:-1] = (u1[1:-1,2:-1] - u1[1:-1,1:-2])/dx +
  112. (v1[2:-1,1:-1] - v1[1:-2,1:-1])/dy
  113. b1 = np.reshape(b1[1:-1,1:-1], Ny*Nx)
  114. A1 = Poisson_Solver_Neumann(u1, v1, Nx, Ny, dt, dx, dy, T, xp, yp)
  115. b1 = b1[:]*(1.0/dt)
  116. temp1 = np.linalg.solve(A1,b1)
  117.  
  118. p_star = np.reshape(temp1, (Ny,Nx))
  119.  
  120.  
  121. F1_Pstar, F2_Pstar = Pressure_Terms(p_star, dx, dy)
  122.  
  123. #calc predictor velocities
  124. u_star = u1.copy()
  125. v_star = v1.copy()
  126.  
  127. u_star[1:-1,2:-2] = u1[1:-1,2:-2] - (dt*F1_Pstar)
  128. v_star[2:-2,1:-1] = v1[2:-2,1:-1] - (dt*F2_Pstar)
  129.  
  130. u = u_star.copy()
  131. v = v_star.copy()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement