inclination.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. """Plot a survey of the inclination angle"""
  2. import matplotlib.pyplot as plt
  3. import numpy as np
  4. from analysis import utils
  5. def plotEdgeView(data, ax, axisOn):
  6. """Plot the encounter as viewed from the edge of the galaxy
  7. Parameters:
  8. ax: the matplotlib axis where the data should be plotted
  9. data: a data dictionary in the format saved by the simulation
  10. axisOn (bool): whether to include the y axis in this subplot.
  11. It can be omitted when it is shared among subplots.
  12. """
  13. r_vec = data['r_vec']
  14. theta, phi = data['CONFIG']['galaxy1']['orientation']
  15. # Obtain the edge view, rotate 90º around y axis
  16. spin = np.array([np.sin(theta)*np.cos(phi),
  17. np.sin(theta)*np.sin(phi),
  18. np.cos(theta)])
  19. phi = np.pi/2
  20. M = np.array([[np.cos(-phi), 0,np.sin(-phi)],
  21. [0, 1, 0],
  22. [-np.sin(-phi), 0, np.cos(-phi)]])
  23. r_vec = np.einsum('ai,ij->aj', r_vec, M)
  24. spin = np.einsum('i,ij->j', spin, M)
  25. data['tracks'] = np.einsum('abi,ij->abj', data['tracks'], M)
  26. data['tracks'] -= r_vec[0,:] #Relative to center of galaxy
  27. r_vec[:,:] -= r_vec[0,:]
  28. # Flip y to be consistent with Toomre and Toomre
  29. r_vec[:,1] = -r_vec[:,1]
  30. data['tracks'][:,:,1] *= -1
  31. # Plotting
  32. utils.plotCenterMasses(ax, {'r_vec': r_vec, 'type':data['type']})
  33. ax.plot(data['tracks'][1,:,0], data['tracks'][1,:,1],
  34. c='black', alpha=1.0, linewidth=1)
  35. ax.scatter(r_vec[:,0], r_vec[:,1], s=0.01, marker='.',
  36. c='#c40f4c', alpha=.6)
  37. utils.setSize(ax, x=(-1.5, 1.5), y=(-1.5,3.5), mode='square')
  38. utils.stylizePlot([ax])
  39. utils.setAxes(ax, x=r'x / $r_{min}$', y=r'y / $r_{min}$',
  40. ycoords=(-0.2, .8), mode=('bottomleft' if axisOn else 'bottom'))
  41. def plotFrontalView(data, ax, axisOn):
  42. """Plot the encounter as viewed normal to the galactic plane
  43. Parameters:
  44. ax: the matplotlib axis where the data should be plotted
  45. data: a data dictionary in the format saved by the simulation
  46. axisOn (bool): whether to include the y axis in this subplot.
  47. It can be omitted when it is shared among subplots.
  48. """
  49. r_vec = data['r_vec']
  50. theta, phi = data['CONFIG']['galaxy1']['orientation']
  51. # Rotate the position vectors into the appropiate view
  52. M2 = np.array([[np.cos(-phi), np.sin(-phi), 0],
  53. [-np.sin(-phi), np.cos(-phi), 0],
  54. [0, 0, 1]])
  55. M1 = np.array([[1, 0, 0],
  56. [0, np.cos(-theta), np.sin(-theta)],
  57. [0, -np.sin(-theta), np.cos(-theta)]])
  58. r_vec = np.einsum('ai,ij->aj', np.einsum('ai,ij->aj', r_vec, M2), M1)
  59. data['tracks'] = np.einsum('abi,ij->abj', data['tracks'], M2)
  60. data['tracks'] = np.einsum('abi,ij->abj', data['tracks'], M1)
  61. data['tracks'] -= data['tracks'][0]
  62. r_vec[:,:] -= r_vec[0,:]
  63. #Plotting
  64. utils.plotCenterMasses(ax, {'r_vec': r_vec, 'type':data['type']})
  65. utils.plotTracks(ax, data['tracks'])
  66. ax.scatter(r_vec[:,0], r_vec[:,1], c='#c40f4c', s=0.01, marker='.')
  67. utils.setSize(ax, x=(-3, 2), y=(-1.5, 1.5), mode='square')
  68. utils.stylizePlot([ax])
  69. utils.setAxes(ax, x=r'x / $r_{min}$', y=r'y / $r_{min}$',
  70. ycoords=(-0.2, .8), mode=('bottomleft' if axisOn else 'bottom'))
  71. #########################################################
  72. # Select the inclinations
  73. inclinations = [15, 45, 60, 90, 135]
  74. f, axs = plt.subplots(2, len(inclinations), figsize=(11, 6),
  75. sharey=False, sharex=False, gridspec_kw = {'height_ratios':[5/3, 1]})
  76. t = 5000 # time of plots
  77. # Plot both views for each encounter
  78. for i in range(len(inclinations)):
  79. data = utils.loadData('i{}'.format(inclinations[i]), t)
  80. plotEdgeView(data, ax=axs[0,i], axisOn=True if i==0 else False)
  81. data = utils.loadData('i{}'.format(inclinations[i]), t)
  82. plotFrontalView(data, ax=axs[1,i], axisOn=True if i==0 else False)
  83. f.subplots_adjust(hspace=0, wspace=0)
  84. plt.tight_layout()
  85. plt.show()