expansion_plot.py 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. from charged_shells import expansion, parameters
  2. import numpy as np
  3. import matplotlib.pyplot as plt
  4. import plotly.graph_objects as go
  5. from config import *
  6. import quadrupole_model_mappings
  7. Expansion = expansion.Expansion
  8. def plot_theta_profile(ex: Expansion, phi: float = 0, num: int = 100, theta_start: float = 0, theta_end: float = np.pi):
  9. theta_vals = np.linspace(theta_start, theta_end, num)
  10. charge = ex.charge_value(theta_vals, phi)
  11. plt.plot(theta_vals, charge.T)
  12. plt.show()
  13. def plot_theta_profile_multiple(ex_list: list[Expansion], label_list, phi: float = 0, num: int = 100,
  14. theta_start: float = 0, theta_end: float = np.pi):
  15. theta_vals = np.linspace(theta_start, theta_end, num)
  16. fig, ax = plt.subplots()
  17. for ex, label in zip(ex_list, label_list):
  18. ax.plot(theta_vals, ex.charge_value(theta_vals, phi).T, label=label)
  19. ax.tick_params(which='both', direction='in', top=True, right=True, labelsize=12)
  20. ax.set_xlabel(r'$\theta$', fontsize=13)
  21. ax.set_ylabel(r'$\sigma$', fontsize=13)
  22. plt.legend(fontsize=12)
  23. plt.tight_layout()
  24. plt.savefig(FIGURES_PATH.joinpath("charge_shape_comparison.png"), dpi=600)
  25. plt.show()
  26. def plot_charge_3d(ex: Expansion, num_theta=100, num_phi=100, save_as: Path = None):
  27. theta = np.linspace(0, np.pi, num_theta)
  28. phi = np.linspace(0, 2 * np.pi, num_phi)
  29. theta, phi = np.meshgrid(theta, phi)
  30. r = ex.charge_value(theta.flatten(), phi.flatten()).reshape(theta.shape)
  31. # Convert spherical coordinates to Cartesian coordinates
  32. x = np.sin(theta) * np.cos(phi)
  33. y = np.sin(theta) * np.sin(phi)
  34. z = np.cos(theta)
  35. # Create a heatmap on the sphere
  36. fig = go.Figure(data=go.Surface(x=x, y=y, z=z, surfacecolor=r,
  37. colorscale='RdBu', reversescale=True))
  38. fig.update_layout(scene=dict(aspectmode='data'))
  39. fig.update_layout(scene=dict(xaxis_title='', yaxis_title='', zaxis_title=''))
  40. # Remove axes planes, background, ticks, and labels
  41. fig.update_layout(scene=dict(xaxis=dict(showbackground=False, gridcolor='white', showticklabels=False, ticks=''),
  42. yaxis=dict(showbackground=False, gridcolor='white', showticklabels=False, ticks=''),
  43. zaxis=dict(showbackground=False, gridcolor='white', showticklabels=False, ticks='')))
  44. # Adjust the width and height for higher resolution
  45. fig.update_layout(width=1200, height=1200)
  46. # Save as PNG with higher resolution
  47. if save_as is not None:
  48. fig.write_image(save_as, scale=3) # Adjust the scale as needed
  49. fig.show()
  50. def main():
  51. params = parameters.ModelParams(kappaR=3, R=150)
  52. # ex = expansion.MappedExpansionQuad(0.328, params.kappaR, 0.001, 30)
  53. # ex = expansion.Expansion(np.arange(3), np.array([1, -1, 0, 1, 2, 0, 3, 0, 2]))
  54. # ex = expansion.GaussianCharges(omega_k=np.array([[0, 0], [np.pi, 0]]), lambda_k=2.676, sigma1=0.00044, l_max=30)
  55. # ex = expansion.SphericalCap(np.array([[0, 0], [np.pi, 0]]), 0.894, 0.00132, 50)
  56. # ex = quadrupole_model_mappings.ic_to_gauss(0.001, 0.328, params, l_max=30)
  57. ex = quadrupole_model_mappings.ic_to_cap(0.001, 0.328, params, l_max=50)
  58. # print(np.real(ex.coefs))
  59. # plot_theta_profile(ex, num=1000, theta_end=2 * np.pi, phi=0)
  60. plot_charge_3d(ex, save_as=FIGURES_PATH.joinpath("model_3D_cap.png"))
  61. # new_coeffs = expanison.expansion_rotation(Quaternion(np.arange(20).reshape(5, 4)).normalized, ex.coeffs, ex.l_array)
  62. # print(new_coeffs.shape)
  63. #
  64. # newnew_coeffs = expansion.expansion_rotation(Quaternion(np.arange(16).reshape(4, 4)).normalized, new_coeffs, ex.l_array)
  65. # print(newnew_coeffs.shape)
  66. if __name__ == '__main__':
  67. main()