expansion_plot.py 3.9 KB

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