patch_size.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import numpy as np
  2. from scipy.optimize import bisect
  3. import expansion
  4. from matplotlib import pyplot as plt
  5. import parameters
  6. import potentials
  7. Expansion = expansion.Expansion
  8. Array = np.ndarray
  9. ModelParams = parameters.ModelParams
  10. @expansion.map_over_expansion
  11. def charge_patch_size(ex: Expansion, phi: float = 0, theta0: Array | float = 0, theta1: Array | float = np.pi / 2):
  12. return bisect(lambda theta: ex.charge_value(theta, phi), theta0, theta1)
  13. def potential_patch_size(ex: Expansion, params: ModelParams,
  14. phi: float = 0, theta0: Array | float = 0, theta1: Array | float = np.pi / 2,
  15. match_expansion_axis_to_params: int = None):
  16. # this is more complicate to map over leading axes of the expansion as potential also depends on model parameters,
  17. # with some, such as kappaR, also being the parameters of the expansion in the first place. When mapping,
  18. # we must therefore provide the expansion axis that should match the collection of parameters in params.
  19. meap = match_expansion_axis_to_params # just a shorter variable name
  20. @expansion.map_over_expansion
  21. def potential_zero(exp: Expansion, prms: ModelParams):
  22. return bisect(lambda theta: potentials.charged_shell_potential(theta, phi, 1, exp, prms), theta0, theta1)
  23. params_list = params.unravel()
  24. if meap is not None:
  25. expansion_list = [Expansion(ex.l_array, np.take(ex.coefs, i, axis=meap)) for i in range(ex.shape[meap])]
  26. else:
  27. expansion_list = [ex for _ in params_list]
  28. if not len(expansion_list) == len(params_list):
  29. raise ValueError(f'Axis of expansion that is supposed to match params does not have the same length, got '
  30. f'len(params.unravel()) = {len(params_list)} and '
  31. f'expansion.shape[{meap}] = {len(expansion_list)}')
  32. results = []
  33. for exp, prms in zip(expansion_list, params_list):
  34. results.append(potential_zero(exp, prms))
  35. if meap is not None:
  36. return np.array(results).swapaxes(0, meap) # return the params-matched axis to where it belongs
  37. return np.array(results)
  38. if __name__ == '__main__':
  39. a_bar = np.linspace(0.2, 0.8, 100)
  40. kappaR = np.array([0.26, 1, 3, 10, 26])
  41. params = ModelParams(R=150, kappaR=kappaR)
  42. ex = expansion.MappedExpansionQuad(a_bar=a_bar[:, None], sigma_m=0.001, l_max=20, kappaR=kappaR[None, :])
  43. # patch_size = charge_patch_size(ex)
  44. patch_size_pot = potential_patch_size(ex, params, match_expansion_axis_to_params=1)
  45. plt.plot(a_bar, patch_size_pot * 180 / np.pi, label=kappaR)
  46. plt.legend()
  47. plt.show()