arrayff 0.0.2__py3-none-any.whl → 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
arrayff/__init__.py CHANGED
@@ -7,6 +7,8 @@ never import a concrete array library.
7
7
 
8
8
  from __future__ import annotations
9
9
 
10
+ from importlib.metadata import PackageNotFoundError, version
11
+
10
12
  from arrayff import utils
11
13
  from arrayff.electrostatics import QEq
12
14
  from arrayff.potentials import (
@@ -16,7 +18,10 @@ from arrayff.potentials import (
16
18
  get_potential,
17
19
  )
18
20
 
19
- __version__ = "0.0.1"
21
+ try:
22
+ __version__ = version("arrayff")
23
+ except PackageNotFoundError: # pragma: no cover - source tree without install
24
+ __version__ = "0.0.0+unknown"
20
25
 
21
26
  __all__ = [
22
27
  "CompositePotential",
@@ -0,0 +1,78 @@
1
+ """Ewald summation parameters for periodic charge models.
2
+
3
+ Only the splitting parameter, the real-space cutoff and the *integer*
4
+ reciprocal lattice indices are chosen here (in numpy, from the current cell).
5
+ The real-space pairs come from a neighbour list; kernels rebuild the actual
6
+ vectors from the (possibly traced) cell, so position and strain derivatives
7
+ stay exact.
8
+ """
9
+
10
+ from __future__ import annotations
11
+
12
+ import dataclasses as dc
13
+ from math import log, pi, sqrt
14
+
15
+ import numpy as np
16
+
17
+ __all__ = ["EwaldSetup", "ewald_setup", "reciprocal_indices"]
18
+
19
+
20
+ @dc.dataclass(frozen=True)
21
+ class EwaldSetup:
22
+ """Splitting parameter, cutoffs and reciprocal indices of an Ewald sum.
23
+
24
+ Attributes
25
+ ----------
26
+ alpha:
27
+ Ewald splitting parameter in 1/Angstrom.
28
+ cutoff:
29
+ Real-space cutoff in Angstrom (the neighbour-list radius).
30
+ kcut:
31
+ Reciprocal-space cutoff in 1/Angstrom.
32
+ kvectors:
33
+ ``(K, 3)`` integer reciprocal lattice indices, origin excluded.
34
+ """
35
+
36
+ alpha: float
37
+ cutoff: float
38
+ kcut: float
39
+ kvectors: np.ndarray
40
+
41
+
42
+ def reciprocal_indices(cell: np.ndarray, kcut: float) -> np.ndarray:
43
+ """Integer ``m`` with ``0 < |2 pi m @ inv(cell).T| <= kcut``."""
44
+ cell = np.asarray(cell, dtype=float)
45
+ extent = np.ceil(kcut * np.linalg.norm(cell, axis=1) / (2.0 * pi))
46
+ axes = [np.arange(-int(n), int(n) + 1) for n in extent]
47
+ grid = np.stack(np.meshgrid(*axes, indexing="ij"), axis=-1).reshape(-1, 3)
48
+ k = 2.0 * pi * grid @ np.linalg.inv(cell).T
49
+ norm = np.linalg.norm(k, axis=1)
50
+ return grid[(norm > 0.0) & (norm <= kcut)]
51
+
52
+
53
+ def ewald_setup(
54
+ cell: np.ndarray,
55
+ *,
56
+ accuracy: float = 1e-6,
57
+ real_cutoff: float = 10.0,
58
+ extra_cutoff: float = 0.0,
59
+ ) -> EwaldSetup:
60
+ """Choose ``alpha`` and the k-space indices for a target accuracy.
61
+
62
+ ``erfc(alpha * r_c)`` and ``exp(-k_c^2 / 4 alpha^2)`` are both about
63
+ ``accuracy``. ``extra_cutoff`` widens the real-space radius for
64
+ additional short-range terms (e.g. the shielding correction).
65
+ """
66
+ if not 0.0 < accuracy < 1.0:
67
+ raise ValueError("accuracy must be in (0, 1)")
68
+ if real_cutoff <= 0.0:
69
+ raise ValueError("real_cutoff must be positive")
70
+ s = sqrt(-log(accuracy))
71
+ alpha = s / real_cutoff
72
+ kcut = 2.0 * alpha * s
73
+ return EwaldSetup(
74
+ alpha=alpha,
75
+ cutoff=max(real_cutoff, extra_cutoff),
76
+ kcut=kcut,
77
+ kvectors=reciprocal_indices(cell, kcut),
78
+ )