Advertisement
Guest User

Untitled

a guest
Oct 20th, 2022
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.46 KB | None | 0 0
  1. import pylion as pl
  2. from pathlib import Path
  3. import matplotlib.pyplot as plt
  4. from mpl_toolkits.mplot3d import Axes3D
  5. import matplotlib.animation as animation
  6. import numpy as np
  7. from matplotlib.animation import FuncAnimation
  8. # use filename for simulation name
  9. name = Path(__file__).stem
  10.  
  11. s = pl.Simulation(name)
  12.  
  13. ions = {'mass': 40, 'charge': -1}
  14. s.append(pl.createioncloud(ions, 1e-3, 1))
  15.  
  16. trap = {'radius': 3.5e-3, 'length': 2.75e-3, 'kappa': 0.244,'frequency': 4e6, 'voltage': 1000, 'endcapvoltage': 70}
  17. s.append(pl.linearpaultrap(trap))
  18.  
  19. s.append(pl.langevinbath(1, 1e-5))
  20.  
  21. s.append(pl.dump('positions.txt', variables=['x', 'y', 'z'], steps=10))
  22. vavg = pl.timeaverage(20, variables=['vx', 'vy', 'vz'])
  23. s.append(pl.dump('secv.txt', vavg, steps=200))
  24.  
  25. s.append(pl.evolve(1e4))
  26. s.execute()
  27.  
  28. file1 = open('positions.txt', 'r')
  29. lines = file1.readlines()
  30. coords = []
  31. zdata = np.zeros(1000)
  32. xdata = np.zeros(1000)
  33. ydata = np.zeros(1000)
  34. for i in range(1000):
  35. coords.append(lines[10*i+9])
  36. coord = coords[i].rstrip().split()
  37. xdata[i] = 1e6*float(coord[1])
  38. ydata[i] = 1e6*float(coord[2])
  39. zdata[i] = 1e6*float(coord[3])
  40. #print(str(zdata[i])+'\n')
  41. xav=np.average(xdata)
  42. xdev=np.std(xdata)
  43. yav=np.average(ydata)
  44. ydev=np.std(ydata)
  45. zav=np.average(zdata)
  46. zdev=np.std(zdata)
  47.  
  48. def func(num, dataSet, lines, redDots):
  49. # NOTE: there is no .set_data() for 3 dim data...
  50. line.set_data(dataSet[0:2, :num])
  51. line.set_3d_properties(dataSet[2, :num])
  52. redDots.set_data(dataSet[0:2, :num])
  53. redDots.set_3d_properties(dataSet[2, :num])
  54. return line
  55.  
  56.  
  57. # THE DATA POINTS
  58. t = np.arange(0,1000,1) # This would be the z-axis ('t' means time here)
  59. x = xdata[t]
  60. y = ydata[t]
  61. z = zdata[t]
  62. dataSet = np.array([x, y, z])
  63. numDataPoints = len(t)
  64.  
  65. # GET SOME MATPLOTLIB OBJECTS
  66. fig = plt.figure()
  67. ax = Axes3D(fig)
  68. redDots = plt.plot(dataSet[0], dataSet[1], dataSet[2], lw=1, c='r', marker='o')[0] # For scatter plot
  69. # NOTE: Can't pass empty arrays into 3d version of plot()
  70. line = plt.plot(dataSet[0], dataSet[1], dataSet[2], lw=1, c='g')[0] # For line plot
  71.  
  72. # AXES PROPERTIES]
  73. # ax.set_xlim3d([limit0, limit1])
  74. ax.set_xlabel('X(t)')
  75. ax.set_ylabel('Y(t)')
  76. ax.set_zlabel('Z(t)')
  77. ax.set_title('Trajectory of ion for Pauli trap')
  78.  
  79. # Creating the Animation object
  80. line_ani = animation.FuncAnimation(fig, func, frames=numDataPoints, fargs=(dataSet,lines,redDots), interval=200, blit=False)
  81. line_ani.save(r'Animation.mp4')
  82.  
  83.  
  84. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement