Advertisement
julietbravo

Untitled

May 25th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.65 KB | None | 0 0
  1. import numpy as np
  2. import matplotlib.pyplot as pl
  3.  
  4. pl.close('all')
  5.  
  6. scalar = np.random.random(49).reshape((7,7))   # j,i
  7. u      = np.random.random(49).reshape((7,7))   # j,i
  8. v      = np.random.random(49).reshape((7,7))   # j,i
  9.  
  10. i0 = 3  # central grid point
  11. j0 = 3  # central grid point
  12.  
  13. dxy = 2.
  14.  
  15. n = 2       # Averaging domain (+/- n grid points)
  16. s = 2*n+1   # Size of averaging domain
  17.  
  18. # Start/end indices of averaging domain
  19. istart = i0-n
  20. iend   = i0+n+1
  21.  
  22. jstart = j0-n
  23. jend   = j0+n+1
  24.  
  25. # --------------
  26. # Manual calculation
  27. # --------------
  28. tend_1 = np.zeros_like(scalar)
  29.  
  30. for i in range(istart, iend):
  31.     for j in range(jstart, jend):
  32.         tend_1[j,i] = -u[j,i] * (scalar[j,i+1] - scalar[j,i-1]) / (2*dxy) \
  33.                       -v[j,i] * (scalar[j+1,i] - scalar[j-1,i]) / (2*dxy)
  34.  
  35. # --------------
  36. # Manual numpy slices
  37. # --------------
  38. ic  = np.s_[istart:iend]
  39. jc  = np.s_[jstart:jend]
  40.  
  41. ip2 = np.s_[istart+2:iend+2]    # [j,i+2]
  42. ip1 = np.s_[istart+1:iend+1]    # [j,i+1]
  43. im1 = np.s_[istart-1:iend-1]    # [j,i-1]
  44. im2 = np.s_[istart-2:iend-2]    # [j,i-2]
  45.  
  46. jp2 = np.s_[jstart+2:jend+2]    # [j+2,i]
  47. jp1 = np.s_[jstart+1:jend+1]    # [j+1,i]
  48. jm1 = np.s_[jstart-1:jend-1]    # [j-1,i]
  49. jm2 = np.s_[jstart-2:jend-2]    # [j-2,i]
  50.  
  51. tend_2 = np.zeros_like(scalar)
  52. tend_2[jc,ic] = -u[jc,ic] * (scalar[jc,ip1] - scalar[jc,im1]) / (2*dxy) \
  53.                 -v[jc,ic] * (scalar[jp1,ic] - scalar[jm1,ic]) / (2*dxy)
  54.  
  55. # --------------
  56. # Slice class
  57. # --------------
  58. class Slice:
  59.     def __init__(self, istart, iend, jstart, jend):
  60.         self.istart = istart
  61.         self.iend   = iend
  62.         self.jstart = jstart
  63.         self.jend   = jend
  64.  
  65.     def __getitem__(self, pos):
  66.         return np.s_[self.jstart+pos[0]:self.jend+pos[0],\
  67.                      self.istart+pos[1]:self.iend+pos[1]]
  68.  
  69.     def __call__(self, j, i):
  70.         return np.s_[self.jstart+j:self.jend+j,\
  71.                      self.istart+i:self.iend+i]
  72.  
  73. offs = Slice(istart, iend, jstart, jend)
  74.  
  75. tend_3 = np.zeros_like(scalar)
  76. tend_3[ offs(0,0) ] = -u[ offs(0,0) ] * (scalar[ offs(0,+1) ] - scalar[ offs(0,-1) ]) / (2*dxy) \
  77.                       -v[ offs(0,0) ] * (scalar[ offs(+1,0) ] - scalar[ offs(-1,0) ]) / (2*dxy)
  78. # OR:
  79. tend_3[ offs(0,0) ] = -u[ offs[0,0] ] * (scalar[ offs[0,+1] ] - scalar[ offs[0,-1] ]) / (2*dxy) \
  80.                       -v[ offs[0,0] ] * (scalar[ offs[+1,0] ] - scalar[ offs[-1,0] ]) / (2*dxy)
  81.  
  82.  
  83. print(np.all(tend_2 == tend_1), np.all(tend_3 == tend_1))
  84.  
  85.  
  86.  
  87.  
  88. pl.figure()
  89.  
  90. pl.subplot(131)
  91. pl.imshow(tend_1)
  92. pl.colorbar()
  93.  
  94. pl.subplot(132)
  95. pl.imshow(tend_2)
  96. pl.colorbar()
  97.  
  98. pl.subplot(133)
  99. pl.imshow(tend_3)
  100. pl.colorbar()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement