energy_gap.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. import matplotlib.pyplot as plt
  2. import numpy as np
  3. from charged_shells import expansion, interactions, mapping
  4. from charged_shells.parameters import ModelParams
  5. from functools import partial
  6. from pathlib import Path
  7. from matplotlib import cm
  8. from mpl_toolkits.axes_grid1 import make_axes_locatable
  9. from matplotlib.colors import TwoSlopeNorm
  10. from matplotlib.ticker import FuncFormatter
  11. import json
  12. Expansion = expansion.Expansion
  13. def energy_gap(ex1: Expansion, params: ModelParams, dist=2., match_expansion_axis_to_params=None):
  14. ex2 = ex1.clone()
  15. ex3 = ex1.clone()
  16. ex2.rotate_euler(alpha=0, beta=np.pi/2, gamma=0) # to get EP config between ex1 and ex2
  17. energy_fn = mapping.parameter_map_two_expansions(partial(interactions.charged_shell_energy, dist=dist),
  18. match_expansion_axis_to_params)
  19. energy_ep = energy_fn(ex1, ex2, params)
  20. energy_pp = energy_fn(ex1, ex3, params)
  21. return (energy_pp - energy_ep) / energy_pp
  22. def abar_kappaR_dependence(save_as=None):
  23. kappaR = np.linspace(0.01, 25, 25)
  24. a_bar = np.array([0.2, 0.4, 0.6, 0.8])
  25. params = ModelParams(R=150, kappaR=kappaR)
  26. ex = expansion.MappedExpansionQuad(a_bar[:, None], kappaR[None, :], 0.001)
  27. # ex = expansion.MappedExpansionQuad(a_bar, kappaR, 0.001)
  28. gap = energy_gap(ex, params, match_expansion_axis_to_params=1)
  29. fig, ax = plt.subplots(figsize=plt.figaspect(0.5))
  30. for g, lbl in zip(gap, [rf'$\bar a={a}$' for a in a_bar]):
  31. ax.plot(kappaR, g, label=lbl)
  32. ax.legend(fontsize=17)
  33. ax.tick_params(which='both', direction='in', top=True, right=True, labelsize=12)
  34. ax.set_xlabel(r'$\kappa R$', fontsize=15)
  35. ax.set_ylabel(r'$\frac{V_{pp}-V_{ep}}{V_{pp}}$', fontsize=20)
  36. plt.tight_layout()
  37. if save_as is not None:
  38. plt.savefig(save_as, dpi=600)
  39. plt.show()
  40. def abar_kappaR_dependence2(save_as=None):
  41. kappaR = np.array([1, 3, 10, 30])
  42. a_bar = np.linspace(0.2, 0.8, 30)
  43. params = ModelParams(R=150, kappaR=kappaR)
  44. ex = expansion.MappedExpansionQuad(a_bar[:, None], kappaR[None, :], 0.001)
  45. # ex = expansion.MappedExpansionQuad(a_bar, kappaR, 0.001)
  46. gap = energy_gap(ex, params, match_expansion_axis_to_params=1)
  47. fig, ax = plt.subplots(figsize=plt.figaspect(0.5))
  48. for g, lbl in zip(gap.T, [rf'$\kappa R={kR}$' for kR in kappaR]):
  49. ax.plot(a_bar, g, label=lbl)
  50. ax.legend(fontsize=17)
  51. ax.tick_params(which='both', direction='in', top=True, right=True, labelsize=12)
  52. ax.set_xlabel(r'$\bar a$', fontsize=15)
  53. ax.set_ylabel(r'$\frac{V_{pp}-V_{ep}}{V_{pp}}$', fontsize=20)
  54. plt.tight_layout()
  55. if save_as is not None:
  56. plt.savefig(save_as, dpi=600)
  57. plt.show()
  58. def charge_kappaR_dependence(a_bar, min_charge, max_charge, save_as=None, cmap=cm.jet):
  59. kappaR = np.linspace(0.01, 10, 50)
  60. sigma_tilde = 0.001
  61. params = ModelParams(R=150, kappaR=kappaR)
  62. charge = np.linspace(min_charge, max_charge, 100)
  63. ex = expansion.MappedExpansionQuad(a_bar, kappaR, sigma_tilde, sigma0=charge)
  64. # ex = expansion.MappedExpansionQuad(a_bar, kappaR, 0.001)
  65. gap = energy_gap(ex, params, match_expansion_axis_to_params=0)
  66. colors = cmap(np.linspace(0, 1, len(charge)))
  67. sm = cm.ScalarMappable(cmap=cmap, norm=plt.Normalize(vmin=np.min(charge) / sigma_tilde,
  68. vmax=np.max(charge) / sigma_tilde))
  69. sm.set_array([])
  70. fig, ax = plt.subplots(figsize=plt.figaspect(0.5))
  71. for g, c in zip(gap.T, colors):
  72. ax.plot(kappaR, g, c=c)
  73. # ax.legend(fontsize=17)
  74. ax.tick_params(which='both', direction='in', top=True, right=True, labelsize=15)
  75. ax.set_xlabel(r'$\kappa R$', fontsize=20)
  76. ax.set_ylabel(r'$(V_{pp}-V_{ep})/V_{pp}$', fontsize=20)
  77. divider = make_axes_locatable(ax)
  78. cax = divider.append_axes('right', size='5%', pad=0.05)
  79. cax.tick_params(labelsize=15)
  80. cbar = fig.colorbar(sm, cax=cax, orientation='vertical')
  81. cbar.set_label(r'$\eta$', rotation=0, labelpad=15, fontsize=20)
  82. # plt.tight_layout()
  83. plt.subplots_adjust(left=0.1, right=0.9, top=0.95, bottom=0.12)
  84. if save_as is not None:
  85. plt.savefig(save_as, dpi=600)
  86. plt.show()
  87. def charge_kappaR_dependence_heatmap(a_bar, min_charge, max_charge, save_as=None, cmap=cm.jet):
  88. kappaR = np.linspace(0.01, 10, 50)
  89. params = ModelParams(R=150, kappaR=kappaR)
  90. charge = np.linspace(min_charge, max_charge, 100)
  91. ex = expansion.MappedExpansionQuad(a_bar, kappaR, 0.001, sigma0=charge)
  92. # ex = expansion.MappedExpansionQuad(a_bar, kappaR, 0.001)
  93. gap = energy_gap(ex, params, match_expansion_axis_to_params=0)
  94. norm = TwoSlopeNorm(vmin=np.min(gap), vcenter=0, vmax=np.max(gap))
  95. sm = cm.ScalarMappable(cmap=cmap, norm=norm)
  96. sm.set_array([])
  97. def y_formatter(x, pos):
  98. return f"{charge[int(x)-1]:.2f}"
  99. def x_formatter(x, pos):
  100. return f"{kappaR[int(x)-1]:.2f}"
  101. fig, ax = plt.subplots(figsize=plt.figaspect(1))
  102. ax.imshow(gap.T, cmap=cmap, origin='lower',
  103. # extent=[kappaR.min(), kappaR.max(), charge.min(), charge.max()]
  104. )
  105. # ax.legend(fontsize=17)
  106. ax.tick_params(which='both', direction='in', top=True, right=True, labelsize=12)
  107. ax.set_xlabel(r'$\kappa R$', fontsize=15)
  108. ax.set_ylabel(r'$\tilde \sigma_0$', fontsize=15)
  109. plt.gca().xaxis.set_major_formatter(FuncFormatter(x_formatter))
  110. plt.gca().yaxis.set_major_formatter(FuncFormatter(y_formatter))
  111. # ax.set_xticks(kappaR)
  112. # ax.set_yticks(charge)
  113. divider = make_axes_locatable(ax)
  114. cax = divider.append_axes('right', size='5%', pad=0.05)
  115. cbar = fig.colorbar(sm, cax=cax, orientation='vertical')
  116. cbar.set_label(r'$\frac{V_{pp}-V_{ep}}{V_{pp}}$', rotation=90, labelpad=20, fontsize=12)
  117. plt.tight_layout()
  118. if save_as is not None:
  119. plt.savefig(save_as, dpi=600)
  120. plt.show()
  121. def IC_gap_plot(config_data: dict, save_as=None):
  122. em_data_path = (Path(config_data["emanuele_data"]).joinpath("FIG_10"))
  123. em_data = np.load(em_data_path.joinpath("relative_gap.npz"))
  124. for k in list(em_data.keys()):
  125. data = em_data[k]
  126. print(k, data.shape)
  127. for i in range(3):
  128. print(np.unique(data[:, i]))
  129. print('\n')
  130. def IC_gap_kappaR(config_data: dict, save_as=None):
  131. em_data_path = (Path(config_data["emanuele_data"]).joinpath("FIG_10"))
  132. em_data = np.load(em_data_path.joinpath("relative_gap.npz"))
  133. data = em_data['fixA']
  134. print(data)
  135. sort = np.argsort(data[:, 2])
  136. xdata = data[:, 2][sort]
  137. ydata = data[:, 3][sort]
  138. plt.plot(xdata, ydata)
  139. plt.xlabel('kappaR')
  140. plt.ylabel('gap')
  141. plt.show()
  142. def IC_gap_abar(config_data: dict, save_as=None):
  143. em_data_path = (Path(config_data["emanuele_data"]).joinpath("FIG_10"))
  144. em_data = np.load(em_data_path.joinpath("relative_gap.npz"))
  145. data = em_data['fixM']
  146. print(data)
  147. sort = np.argsort(data[:, 1])
  148. xdata = data[:, 1][sort]
  149. ydata = data[:, 3][sort]
  150. plt.plot(xdata, ydata)
  151. plt.xlabel('abar')
  152. plt.ylabel('gap')
  153. plt.show()
  154. def IC_gap_charge_at_abar(a_bar, config_data: dict, save_as=None, cmap=cm.coolwarm, which_change='changezp',
  155. eta_min: float = None, eta_max: float = None):
  156. em_data_path = (Path(config_data["emanuele_data"]).joinpath("FIG_10"))
  157. em_data = np.load(em_data_path.joinpath("relative_gap_ZC.npz"))
  158. data = em_data[which_change]
  159. sigma_tilde = 0.001
  160. relevant_indices = data[:, 1] == a_bar
  161. if not np.any(relevant_indices):
  162. raise ValueError(f'No results for given a_bar = {a_bar}. Possible values: {np.unique(data[:, 1])}')
  163. data = data[relevant_indices]
  164. charge, inverse, counts = np.unique(data[:, 0], return_counts=True, return_inverse=True)
  165. # print(f'All charge: {charge}')
  166. eta = charge / sigma_tilde
  167. if eta_min is None:
  168. eta_min = np.min(eta)
  169. if eta_max is None:
  170. eta_max = np.max(eta)
  171. def map_eta_to_unit(x):
  172. return (x - eta_min) / (eta_max - eta_min)
  173. # print(eta[0], eta[1])
  174. # print(map_eta_to_unit(eta[0]), map_eta_to_unit(eta[-1]))
  175. colors_linspace = np.linspace(map_eta_to_unit(eta[0]), map_eta_to_unit(eta[-1]), len(charge))
  176. colors_linspace[colors_linspace > 1] = 1
  177. colors_linspace[colors_linspace < 0] = 0
  178. colors = cmap(colors_linspace)
  179. sm = cm.ScalarMappable(cmap=cmap, norm=plt.Normalize(vmin=eta_min, vmax=eta_max))
  180. sm.set_array([])
  181. fig, ax = plt.subplots(figsize=plt.figaspect(0.5))
  182. for i, c in enumerate(colors):
  183. idx, = np.nonzero(inverse == i)
  184. kR = data[idx, 2]
  185. gap = data[idx, 3]
  186. sort = np.argsort(kR)
  187. ax.plot(kR[sort], gap[sort], c=c)
  188. ax.tick_params(which='both', direction='in', top=True, right=True, labelsize=15)
  189. ax.set_xlabel(r'$\kappa R$', fontsize=20)
  190. ax.set_ylabel(r'$(V_{pp}-V_{ep})/V_{pp}$', fontsize=20)
  191. ax.set_xlim(-0.25, 10.25)
  192. ax.set_ylim(-4, 5)
  193. divider = make_axes_locatable(ax)
  194. cax = divider.append_axes('right', size='5%', pad=0.05)
  195. cax.tick_params(labelsize=15)
  196. cbar = fig.colorbar(sm, cax=cax, orientation='vertical')
  197. cbar.set_label(r'$\eta$', rotation=0, labelpad=15, fontsize=20)
  198. # plt.tight_layout()
  199. plt.subplots_adjust(left=0.1, right=0.9, top=0.95, bottom=0.12)
  200. if save_as is not None:
  201. plt.savefig(save_as, dpi=600)
  202. plt.show()
  203. def test_gap(a_bar, kappaR, charge):
  204. params = ModelParams(R=150, kappaR=kappaR)
  205. ex = expansion.MappedExpansionQuad(a_bar, kappaR, 0.001, sigma0=charge)
  206. gap = energy_gap(ex, params, match_expansion_axis_to_params=None)
  207. print(gap)
  208. def main():
  209. with open(Path("/home/andraz/ChargedShells/charged-shells/config.json")) as config_file:
  210. config_data = json.load(config_file)
  211. # test_gap(0.3, 10, charge=-0.003)
  212. # abar_kappaR_dependence(Path("/home/andraz/ChargedShells/Figures/full_amplitude_kappaR_dep.png"))
  213. # abar_kappaR_dependence2(Path("/home/andraz/ChargedShells/Figures/full_amplitude_abar_dep.png"))
  214. # charge_kappaR_dependence(a_bar=0.8, min_charge=-0.002, max_charge=0.002,
  215. # save_as=Path("/home/andraz/ChargedShells/Figures/full_amplitude_charge_abar08.png"),
  216. # cmap=cm.coolwarm)
  217. # charge_kappaR_dependence_heatmap(a_bar=0.5, min_charge=-0.003, max_charge=0.003,
  218. # save_as=Path("/home/andraz/ChargedShells/Figures/full_amplitude_heatmap_abar05.png"),
  219. # cmap=cm.bwr)
  220. # IC_gap_plot(config_data)
  221. # IC_gap_kappaR(config_data)
  222. # IC_gap_abar(config_data)
  223. IC_gap_charge_at_abar(0.3, config_data, which_change='changezc', eta_min=-2, eta_max=2,
  224. save_as=Path("/home/andraz/ChargedShells/Figures/Emanuele_data/IC_full_amplitude_charge_abar03.png")
  225. )
  226. if __name__ == '__main__':
  227. main()