Guest User

Untitled

a guest
Jul 16th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. import numpy as np
  4. import matplotlib.pyplot as plt
  5.  
  6. # ========= functions
  7.  
  8. def blob(p,w,Xs,Ys):
  9. # helps generate nice test-function
  10. dXs = Xs-p[0]
  11. dYs = Ys-p[1]
  12. return 1/( 1 + (dXs**2+dYs**2)/(w*w) )
  13.  
  14. def matchPBC(f,Xs,Ys):
  15. '''
  16. match periodic boundary condions iteratevily (one axis at a tim)
  17. '''
  18. f=f.copy()
  19. ### X-boundary
  20. Dxs = f[:,0] - f[:,-1]; # difference for each pixel at boundary
  21. f += Dxs[:,None]*(Xs-0.5) # linear interpolation for each x-line[!
  22. ### Y-boundary
  23. Dys = f[0,:] - f[-1,:];
  24. f += Dys[None,:]*(Ys-0.5)
  25. return f
  26.  
  27. # ========= main
  28.  
  29. ### perpare support arryas
  30. xs = np.linspace(0.0,1.0,100,endpoint=False)
  31. ys = np.linspace(0.0,1.0,100,endpoint=False)
  32. Xs,Ys = np.meshgrid(xs,ys)
  33.  
  34. #### test function
  35. #f = np.sin(Xs*6) + np.sin(Xs*11) + np.cos(Xs*7-Ys*9) + np.sin(Xs*13+Ys*14) #+ np.sin(Xs*33+Ys*24)
  36. #f = np.cos(Xs*3.5) + np.sin(Ys*8)
  37. #f = (Xs*5 + Ys*16)/21.0
  38. f = blob([0.0,0.9],0.2,Xs,Ys) + blob([0.5,0.0],0.2,Xs,Ys)
  39.  
  40. #### do it !!!
  41. fpbc = matchPBC(f,Xs,Ys)
  42.  
  43. #### plot results !!!
  44. plt.subplot(1,2,1); plt.imshow( np.tile(f ,(2,2)) )
  45. plt.subplot(1,2,2); plt.imshow( np.tile(fpbc,(2,2)) )
  46. plt.show()
Add Comment
Please, Sign In to add comment