parameters.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. from __future__ import annotations
  2. from dataclasses import dataclass
  3. import numpy as np
  4. import units_and_constants as uc
  5. Array = np.ndarray
  6. @dataclass(kw_only=True)
  7. class ModelParams:
  8. R: float | Array
  9. kappa: float | Array = None
  10. kappaR: float | Array = None
  11. c0: float | Array = None
  12. epsilon: float = 80
  13. temperature: float = 293
  14. def __post_init__(self):
  15. self.kappa, self.kappaR, self.c0 = screening_calculator(self.R, self.temperature, self.epsilon,
  16. self.kappa, self.kappaR, self.c0)
  17. def unravel(self) -> list[ModelParams]:
  18. params_list = []
  19. all_r = np.array([self.R]) if not isinstance(self.R, Array) else self.R
  20. all_kappa = np.array([self.kappa]) if not isinstance(self.kappa, Array) else self.kappa
  21. for r in all_r:
  22. for kappa in all_kappa:
  23. params_list.append(ModelParams(R=r, kappa=kappa))
  24. return params_list
  25. def bjerrum(temp: float, epsilon: float) -> float:
  26. return uc.CONSTANTS.e0 ** 2 / (4 * np.pi * epsilon * uc.CONSTANTS.epsilon0 * uc.CONSTANTS.Boltzmann * temp)
  27. def kappa_from_concentration(c0: float, temp: float, epsilon: float) -> float:
  28. return np.sqrt(8 * np.pi * bjerrum(temp, epsilon) * c0)
  29. def concentration_from_kappa(kappa: float, temp: float, epsilon: float):
  30. # TODO: what is with the units of c0 calculated here?
  31. return kappa ** 2 / (8 * np.pi * bjerrum(temp, epsilon))
  32. def screening_calculator(radius: float, temp: float, epsilon: float,
  33. kappa: float = None, kappaR: float = None, c0: float = None) -> (float, float, float):
  34. if kappa is not None:
  35. return kappa, kappa * radius, concentration_from_kappa(kappa, temp, epsilon)
  36. elif kappaR is not None:
  37. return kappaR / radius, kappaR, concentration_from_kappa(kappaR / radius, temp, epsilon)
  38. elif c0 is not None:
  39. kappa = kappa_from_concentration(c0, temp, epsilon)
  40. return kappa, kappa * radius, c0
  41. raise ValueError('One of the arguments kappa, kappaR or c0 should be different from None.')