DimSpin 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.
@@ -0,0 +1,140 @@
1
+ """Expand one spin centre into the magnetically inequivalent orientations of a crystal.
2
+
3
+ Given a representative spin centre and a space group number, the space group
4
+ maps the centre onto its orbit. Each site in the orbit rotates the molecular
5
+ frame by
6
+
7
+ R_cart = L @ R @ L^-1,
8
+
9
+ with R the integer fractional-coordinate rotation of that site and L the
10
+ standard lattice matrix. The resulting ``R_cart`` is the molecular
11
+ orientation used when assembling tensors.
12
+
13
+ Output: ``(orientations, multiplicities)``. The total spectrum is the sum of
14
+ ``multiplicity[i] * spectrum(orientations[i])``.
15
+
16
+ Two sites with identical (g, D, A) tensors in the crystal frame give the same
17
+ spectrum for every crystal orientation and are merged into one magnetic
18
+ equivalence class, weighted by its multiplicity. For a special position the
19
+ operations are first grouped by the position they produce, so the stabilizer
20
+ is not counted more than once.
21
+
22
+ Notes: only second-order tensors g, D and A are transformed. High-order ZFS
23
+ coefficients and the third-order T/R/F tensors are not rotated here. The
24
+ molecular frame of the reference site is assumed to coincide with the crystal
25
+ frame; any fixed tilt belongs to the tensor orientation. Fractional
26
+ coordinates must use the same standard setting as the operations: unique axis
27
+ b for monoclinic groups and hexagonal axes for rhombohedral groups.
28
+ """
29
+
30
+ from __future__ import annotations
31
+
32
+ import warnings
33
+
34
+ import numpy as np
35
+
36
+ from .Space_Group import get_space_group
37
+ from .Space_Group_L import get_L
38
+
39
+
40
+ def _round_key(arr, tol):
41
+ """
42
+ Inputs: array; rounding tolerance.
43
+
44
+ Returns: a hashable tuple of the rounded values.
45
+ """
46
+ n = max(0, int(round(-np.log10(tol))))
47
+ return tuple(np.round(np.asarray(arr, dtype=float), n).ravel().tolist())
48
+
49
+
50
+ def _group_by(items, keyfunc):
51
+ """
52
+ Inputs: items; key function.
53
+
54
+ Returns: groups of items, preserving first-appearance order of the keys.
55
+ """
56
+ groups = {}
57
+ order = []
58
+ for it in items:
59
+ k = keyfunc(it)
60
+ if k not in groups:
61
+ groups[k] = []
62
+ order.append(k)
63
+ groups[k].append(it)
64
+ return [groups[k] for k in order]
65
+
66
+
67
+ def build_space_group_orientations(ref_spin, group_number, frac_pos=None, tol=1e-8):
68
+ """
69
+ Inputs: representative spin centre; space group number (1-230); optional
70
+ fractional coordinates of the site; grouping tolerance.
71
+
72
+ Returns: (orientations, multiplicities), with each orientation a 3x3
73
+ orthogonal matrix giving the molecular frame relative to the crystal
74
+ frame.
75
+
76
+ Notes: the spin centre's tensors are normalized in place by its
77
+ ``identity_tensor`` method. Without ``frac_pos`` every symmetry operation
78
+ is treated as a distinct site (general position); with ``frac_pos`` the
79
+ operations are grouped by the position they produce, which handles
80
+ special positions correctly. A mirror, inversion or glide operation gives
81
+ det(R_cart) = -1; second-order tensors transform as R @ T @ R.T for either
82
+ sign.
83
+ """
84
+ number = int(group_number)
85
+ sg = get_space_group(number)
86
+ L = get_L(number)
87
+
88
+ ref_spin.identity_tensor()
89
+
90
+ g = np.asarray(ref_spin.g, dtype=float)
91
+ D = np.asarray(ref_spin.D, dtype=float)
92
+ A = np.asarray(ref_spin.A, dtype=float)
93
+
94
+ x0 = None if frac_pos is None else np.asarray(frac_pos, dtype=float)
95
+
96
+ entries = []
97
+ for op in sg.operations:
98
+ R = np.asarray(op.rotation, dtype=float)
99
+ t = np.array([float(v) for v in op.translation])
100
+ R_cart = L @ R @ np.linalg.inv(L)
101
+ pos = (R @ x0 + t) % 1.0 if x0 is not None else None
102
+ entries.append({
103
+ "pos": pos,
104
+ "R_cart": R_cart,
105
+ "g": R_cart @ g @ R_cart.T,
106
+ "D": R_cart @ D @ R_cart.T,
107
+ "A": R_cart @ A @ R_cart.T,
108
+ })
109
+
110
+ if x0 is not None:
111
+ def _site_key(e):
112
+ pos = np.where(e["pos"] >= 1.0 - tol, 0.0, e["pos"])
113
+ return _round_key(pos, tol)
114
+
115
+ site_groups = _group_by(entries, _site_key)
116
+ for grp in site_groups:
117
+ g0 = grp[0]["g"]
118
+ for e in grp[1:]:
119
+ if not np.allclose(g0, e["g"], atol=tol):
120
+ warnings.warn(
121
+ "The tensors are inconsistent with the site symmetry: "
122
+ "operations that map the site onto itself give different "
123
+ "tensors. Check that the representative tensors satisfy "
124
+ "the local symmetry of this Wyckoff position."
125
+ )
126
+ break
127
+ entries = [grp[0] for grp in site_groups]
128
+
129
+ def _tensor_key(e):
130
+ return (_round_key(e["g"], tol), _round_key(e["D"], tol), _round_key(e["A"], tol))
131
+
132
+ classes = _group_by(entries, _tensor_key)
133
+
134
+ orientations = [cls[0]["R_cart"] for cls in classes]
135
+ multiplicities = [len(cls) for cls in classes]
136
+
137
+ return orientations, multiplicities
138
+
139
+
140
+ __all__ = ["build_space_group_orientations"]
@@ -0,0 +1,135 @@
1
+ """Standard lattice matrices for the crystal systems.
2
+
3
+ Space-group operations are given as integer matrices R in fractional
4
+ coordinates, while g, D and A tensors live in Cartesian coordinates. The two
5
+ are related by
6
+
7
+ R_cart = L @ R @ L^-1,
8
+
9
+ with L the lattice matrix whose columns are the vectors a, b, c. Absolute
10
+ lengths cancel in this expression, so standard cell parameters are enough.
11
+
12
+ Conventions: a lies along Cartesian x, b lies in the xy plane, and c follows
13
+ from the angles alpha = b.c, beta = a.c, gamma = a.b in degrees. Only the
14
+ trigonal and hexagonal systems need a non-trivial L (gamma = 120 degrees);
15
+ the other systems use an orthonormal L. Rhombohedral groups are expressed in
16
+ hexagonal axes, which is the standard setting used by Space_Group.
17
+
18
+ Public entry points: :func:`get_L` and :func:`get_lattice_info`.
19
+ """
20
+
21
+ from __future__ import annotations
22
+
23
+ import numpy as np
24
+
25
+
26
+ def crystal_system_for(number: int) -> str:
27
+ """
28
+ Inputs: space group number.
29
+
30
+ Returns: the crystal system name.
31
+ """
32
+ number = int(number)
33
+ if number <= 2:
34
+ return "triclinic"
35
+ if number <= 15:
36
+ return "monoclinic"
37
+ if number <= 74:
38
+ return "orthorhombic"
39
+ if number <= 142:
40
+ return "tetragonal"
41
+ if number <= 167:
42
+ return "trigonal"
43
+ if number <= 194:
44
+ return "hexagonal"
45
+ if number <= 230:
46
+ return "cubic"
47
+ raise ValueError(f"invalid space group number: {number}")
48
+
49
+
50
+ def lattice_matrix(a, b, c, alpha, beta, gamma):
51
+ """
52
+ Inputs: cell lengths a, b, c; cell angles alpha, beta, gamma in degrees.
53
+
54
+ Returns: the 3x3 lattice matrix whose columns are a, b, c.
55
+
56
+ Notes: a is along x and b lies in the xy plane.
57
+ """
58
+ alpha = np.radians(alpha)
59
+ beta = np.radians(beta)
60
+ gamma = np.radians(gamma)
61
+ ca, cb, cg = np.cos(alpha), np.cos(beta), np.cos(gamma)
62
+ sg = np.sin(gamma)
63
+
64
+ a_vec = np.array([a, 0.0, 0.0])
65
+ b_vec = np.array([b * cg, b * sg, 0.0])
66
+ cy = (ca - cb * cg) / sg
67
+ cz = np.sqrt(max(0.0, 1.0 - cb ** 2 - cy ** 2))
68
+ c_vec = np.array([c * cb, c * cy, c * cz])
69
+
70
+ return np.array([a_vec, b_vec, c_vec]).T
71
+
72
+
73
+ STANDARD_PARAMETERS = {
74
+ "triclinic": dict(a=1.0, b=1.0, c=1.0, alpha=90.0, beta=90.0, gamma=90.0),
75
+ "monoclinic": dict(a=1.0, b=1.0, c=1.0, alpha=90.0, beta=90.0, gamma=90.0),
76
+ "orthorhombic": dict(a=1.0, b=1.0, c=1.0, alpha=90.0, beta=90.0, gamma=90.0),
77
+ "tetragonal": dict(a=1.0, b=1.0, c=1.0, alpha=90.0, beta=90.0, gamma=90.0),
78
+ "trigonal": dict(a=1.0, b=1.0, c=1.0, alpha=90.0, beta=90.0, gamma=120.0),
79
+ "hexagonal": dict(a=1.0, b=1.0, c=1.0, alpha=90.0, beta=90.0, gamma=120.0),
80
+ "cubic": dict(a=1.0, b=1.0, c=1.0, alpha=90.0, beta=90.0, gamma=90.0),
81
+ }
82
+
83
+
84
+ def standard_lattice(crystal_system: str) -> np.ndarray:
85
+ """
86
+ Inputs: crystal system name.
87
+
88
+ Returns: the 3x3 lattice matrix in the standard setting.
89
+ """
90
+ key = str(crystal_system).lower()
91
+ if key not in STANDARD_PARAMETERS:
92
+ raise ValueError(f"unknown crystal system: {crystal_system}")
93
+ p = STANDARD_PARAMETERS[key]
94
+ return lattice_matrix(p["a"], p["b"], p["c"], p["alpha"], p["beta"], p["gamma"])
95
+
96
+
97
+ ALL_STANDARD_LATTICES = {
98
+ system: standard_lattice(system) for system in STANDARD_PARAMETERS
99
+ }
100
+
101
+
102
+ def get_L(number):
103
+ """
104
+ Inputs: space group number.
105
+
106
+ Returns: the standard lattice matrix L for that group.
107
+ """
108
+ return standard_lattice(crystal_system_for(number))
109
+
110
+
111
+ def get_lattice_info(number):
112
+ """
113
+ Inputs: space group number.
114
+
115
+ Returns: a dict with the number, crystal system, lattice matrix and cell
116
+ parameters.
117
+ """
118
+ number = int(number)
119
+ system = crystal_system_for(number)
120
+ return {
121
+ "number": number,
122
+ "crystal_system": system,
123
+ "lattice": standard_lattice(system),
124
+ "parameters": dict(STANDARD_PARAMETERS[system]),
125
+ }
126
+
127
+
128
+ __all__ = [
129
+ "crystal_system_for",
130
+ "lattice_matrix",
131
+ "standard_lattice",
132
+ "ALL_STANDARD_LATTICES",
133
+ "get_L",
134
+ "get_lattice_info",
135
+ ]
@@ -0,0 +1,68 @@
1
+ """Transitions and broadened spectra for one crystal orientation."""
2
+
3
+ import numpy as np
4
+
5
+ from .Truncate_Observable import truncate_observable
6
+
7
+
8
+ def spec_transitions(spin, spec):
9
+ """
10
+ Inputs: Spin and Spec objects.
11
+
12
+ Returns: (positions, intensities) of all transitions for a single
13
+ orientation, without line broadening.
14
+
15
+ Notes: intensities carry the modulation sign, so one broadening step on
16
+ the combined arrays gives the difference spectrum (positive minus
17
+ negative modulation). Powder mode accumulates these transitions over all
18
+ orientations and broadens once. ``num_allowed = 2 S (2 I + 1)`` is the
19
+ number of allowed transitions (2 S spin steps times 2 I + 1 nuclear
20
+ states), not the number of level pairs; truncate_observable always keeps
21
+ these and adds the forbidden transitions above the relative threshold.
22
+ """
23
+ spin.update_Btensor()
24
+ spin.update_H_initial()
25
+
26
+ num_allowed = int(2 * spin.S * (2 * spin.I + 1))
27
+ positions = []
28
+ intensities = []
29
+
30
+ def _collect(transitions, sign):
31
+ positions.extend(float(t[0]) for t in transitions)
32
+ intensities.extend(sign * float(t[3]) for t in transitions)
33
+
34
+ if spec.method == 'High_Field_Method':
35
+ from .High_Field_Method import high_field_method
36
+ _collect(truncate_observable(high_field_method(spin, spec, 'positive'), num_allowed), +1.0)
37
+ _collect(truncate_observable(high_field_method(spin, spec, 'negative'), num_allowed), -1.0)
38
+ elif spec.method == 'Adaptive_Grid_Method':
39
+ from .Adaptive_Grid_Method import adaptive_grid_method
40
+ _collect(truncate_observable(adaptive_grid_method(spin, spec, 'positive'), num_allowed), +1.0)
41
+ _collect(truncate_observable(adaptive_grid_method(spin, spec, 'negative'), num_allowed), -1.0)
42
+
43
+ return (np.asarray(positions, dtype=float),
44
+ np.asarray(intensities, dtype=float))
45
+
46
+
47
+ def broaden_transitions(spec, positions, intensities):
48
+ """
49
+ Inputs: Spec object with the field grid and linewidths; transition
50
+ positions and intensities.
51
+
52
+ Returns: the broadened spectrum on ``spec.fieldrange``.
53
+ """
54
+ if positions.size == 0:
55
+ return np.zeros(spec.specpoints)
56
+ from .Line_Broadening import broaden
57
+ return broaden(spec.fieldrange, positions, intensities,
58
+ spec.lw_lorentzian, spec.lw_gaussian)
59
+
60
+
61
+ def spec_oneside(spin, spec):
62
+ """
63
+ Inputs: Spin and Spec objects.
64
+
65
+ Returns: the broadened spectrum for one crystal orientation.
66
+ """
67
+ positions, intensities = spec_transitions(spin, spec)
68
+ return broaden_transitions(spec, positions, intensities)
@@ -0,0 +1,89 @@
1
+ """Electron spin and nuclear-spin operator matrices."""
2
+
3
+ from functools import lru_cache
4
+
5
+ import numpy as np
6
+
7
+
8
+ @lru_cache(maxsize=None)
9
+ def Sz_get(S, I):
10
+ """Return the electron-spin z operator."""
11
+ levels = int(2 * S + 1)
12
+ sz = np.zeros((levels, levels))
13
+ for ii in range(levels):
14
+ sz[ii, ii] = S - ii
15
+ return np.kron(sz, np.identity(int(2 * I + 1)))
16
+
17
+
18
+ def Sp(S):
19
+ """Return the electron-spin raising operator."""
20
+ levels = int(2 * S + 1)
21
+ sp = np.zeros((levels, levels))
22
+ for ii in range(levels - 1):
23
+ jj = ii + 1
24
+ sp[ii, jj] = np.sqrt(S * (S + 1) - (S - ii) * (S - jj))
25
+ return sp
26
+
27
+
28
+ def Sm(S):
29
+ """Return the electron-spin lowering operator."""
30
+ levels = int(2 * S + 1)
31
+ sm = np.zeros((levels, levels))
32
+ for jj in range(levels - 1):
33
+ ii = jj + 1
34
+ sm[ii, jj] = np.sqrt(S * (S + 1) - (S - ii) * (S - jj))
35
+ return sm
36
+
37
+
38
+ @lru_cache(maxsize=None)
39
+ def Sx_get(S, I):
40
+ """Return the electron-spin x operator."""
41
+ return np.kron((Sp(S) + Sm(S)) / 2, np.identity(int(2 * I + 1)))
42
+
43
+
44
+ @lru_cache(maxsize=None)
45
+ def Sy_get(S, I):
46
+ """Return the electron-spin y operator."""
47
+ return np.kron((Sp(S) - Sm(S)) / (2j), np.identity(int(2 * I + 1)))
48
+
49
+
50
+ @lru_cache(maxsize=None)
51
+ def Iz_get(S, I):
52
+ """Return the nuclear-spin z operator."""
53
+ levels = int(2 * I + 1)
54
+ iz = np.zeros((levels, levels))
55
+ for ii in range(levels):
56
+ iz[ii, ii] = I - ii
57
+ return np.kron(np.identity(int(2 * S + 1)), iz)
58
+
59
+
60
+ def Ip(I):
61
+ """Return the nuclear-spin raising operator."""
62
+ levels = int(2 * I + 1)
63
+ ip = np.zeros((levels, levels))
64
+ for ii in range(levels - 1):
65
+ jj = ii + 1
66
+ ip[ii, jj] = np.sqrt(I * (I + 1) - (I - ii) * (I - jj))
67
+ return ip
68
+
69
+
70
+ def Im(I):
71
+ """Return the nuclear-spin lowering operator."""
72
+ levels = int(2 * I + 1)
73
+ im = np.zeros((levels, levels))
74
+ for jj in range(levels - 1):
75
+ ii = jj + 1
76
+ im[ii, jj] = np.sqrt(I * (I + 1) - (I - ii) * (I - jj))
77
+ return im
78
+
79
+
80
+ @lru_cache(maxsize=None)
81
+ def Ix_get(S, I):
82
+ """Return the nuclear-spin x operator."""
83
+ return np.kron(np.identity(int(2 * S + 1)), (Ip(I) + Im(I)) / 2)
84
+
85
+
86
+ @lru_cache(maxsize=None)
87
+ def Iy_get(S, I):
88
+ """Return the nuclear-spin y operator."""
89
+ return np.kron(np.identity(int(2 * S + 1)), (Ip(I) - Im(I)) / (2j))
@@ -0,0 +1,161 @@
1
+ """Extended Stevens operators for high-order zero-field splitting.
2
+
3
+ The Hamiltonian is written as H = sum_{k,q} B_k^q * O_k^q(S), with ranks
4
+ k = 2, 4, 6 and orders q = -k ... k. The operators are real Hermitian
5
+ matrices built from Sx, Sy, Sz, and the coefficients B_k^q carry the energy
6
+ unit (MHz here).
7
+
8
+ q > 0 gives cosine-type operators and q < 0 sine-type operators, following the
9
+ Rudowicz/EasySpin convention. Sign conventions for q < 0 differ between
10
+ sources, so coefficients taken from the literature should be checked.
11
+
12
+ Rank k = 2 is equivalent to Team_ZFS.H_ZFS with B_2^0 = D/3 and B_2^2 = E.
13
+ O_k^q vanishes identically when k > 2S.
14
+ """
15
+
16
+ import numpy as np
17
+
18
+ from .Spin_Operators import Sx_get, Sy_get, Sz_get
19
+
20
+
21
+ def _sym(A, B):
22
+ """
23
+ Inputs: two square matrices A and B.
24
+
25
+ Returns: the anticommutator A @ B + B @ A.
26
+ """
27
+ return A @ B + B @ A
28
+
29
+
30
+ def stev_op(k, q, S, I=0, R=None):
31
+ """
32
+ Inputs: rank k in {2, 4, 6}; order q with -k <= q <= k; electron and
33
+ nuclear spins S and I; optional 3x3 rotation R taking laboratory operators
34
+ to the tensor frame (S_tensor = R @ S_lab).
35
+
36
+ Returns: the extended Stevens operator O_k^q as a (2S+1)(2I+1) Hermitian
37
+ matrix, identity on the nuclear space.
38
+
39
+ Notes: q > 0 gives cosine-type and q < 0 sine-type operators, and the sign
40
+ convention for q < 0 differs between sources. O_k^q is zero for k > 2S.
41
+ """
42
+ n = int(round((2 * S + 1) * (2 * I + 1)))
43
+ if k > 2 * S:
44
+ return np.zeros((n, n))
45
+
46
+ Sx = Sx_get(S, I)
47
+ Sy = Sy_get(S, I)
48
+ Sz = Sz_get(S, I)
49
+
50
+ if R is not None:
51
+ R = np.asarray(R, dtype=float)
52
+ Sx, Sy, Sz = (
53
+ R[0, 0] * Sx + R[0, 1] * Sy + R[0, 2] * Sz,
54
+ R[1, 0] * Sx + R[1, 1] * Sy + R[1, 2] * Sz,
55
+ R[2, 0] * Sx + R[2, 1] * Sy + R[2, 2] * Sz,
56
+ )
57
+
58
+ Sp_mat = Sx + 1.0j * Sy
59
+ Sm_mat = Sx - 1.0j * Sy
60
+ Id = np.eye(n)
61
+ s = S
62
+
63
+ if k == 2:
64
+ if q == 0:
65
+ return 3.0 * (Sz @ Sz) - s * (s + 1.0) * Id
66
+ if q == 1:
67
+ return 0.5 * _sym(Sz, Sx)
68
+ if q == -1:
69
+ return 0.5 * _sym(Sz, Sy)
70
+ if q == 2:
71
+ return Sx @ Sx - Sy @ Sy
72
+ if q == -2:
73
+ return _sym(Sx, Sy)
74
+
75
+ if k == 4:
76
+ Sz2 = Sz @ Sz
77
+ Sz3 = Sz2 @ Sz
78
+ Sz4 = Sz2 @ Sz2
79
+ if q == 0:
80
+ return (35.0 * Sz4
81
+ - (30.0 * s * (s + 1.0) - 25.0) * Sz2
82
+ + (3.0 * s ** 2 * (s + 1.0) ** 2 - 6.0 * s * (s + 1.0)) * Id)
83
+ A1 = 7.0 * Sz3 - 3.0 * s * (s + 1.0) * Sz - Sz
84
+ if q == 1:
85
+ return 0.5 * _sym(A1, Sx)
86
+ if q == -1:
87
+ return 0.5 * _sym(A1, Sy)
88
+ B2 = 7.0 * Sz2 - s * (s + 1.0) * Id - 5.0 * Id
89
+ if q == 2:
90
+ return 0.5 * _sym(B2, Sx @ Sx - Sy @ Sy)
91
+ if q == -2:
92
+ return 0.5 * _sym(B2, _sym(Sx, Sy))
93
+ P3 = Sp_mat @ Sp_mat @ Sp_mat
94
+ M3 = Sm_mat @ Sm_mat @ Sm_mat
95
+ if q == 3:
96
+ return 0.25 * _sym(Sz, P3 + M3)
97
+ if q == -3:
98
+ return 0.25 * _sym(Sz, (P3 - M3) / 1.0j)
99
+ P4 = Sp_mat @ P3
100
+ M4 = Sm_mat @ M3
101
+ if q == 4:
102
+ return 0.5 * (P4 + M4)
103
+ if q == -4:
104
+ return 0.5 * (P4 - M4) / 1.0j
105
+
106
+ if k == 6:
107
+ Sz2 = Sz @ Sz
108
+ Sz3 = Sz2 @ Sz
109
+ Sz4 = Sz2 @ Sz2
110
+ Sz5 = Sz4 @ Sz
111
+ Sz6 = Sz3 @ Sz3
112
+ if q == 0:
113
+ return (231.0 * Sz6
114
+ - (315.0 * s * (s + 1.0) - 735.0) * Sz4
115
+ + (105.0 * s ** 2 * (s + 1.0) ** 2
116
+ - 525.0 * s * (s + 1.0) + 294.0) * Sz2
117
+ - (5.0 * s ** 3 * (s + 1.0) ** 3
118
+ - 40.0 * s ** 2 * (s + 1.0) ** 2
119
+ + 60.0 * s * (s + 1.0)) * Id)
120
+ A1 = (33.0 * Sz5
121
+ - (30.0 * s * (s + 1.0) - 15.0) * Sz3
122
+ - (5.0 * s * (s + 1.0) - 10.0) * Sz)
123
+ if q == 1:
124
+ return 0.5 * _sym(A1, Sx)
125
+ if q == -1:
126
+ return 0.5 * _sym(A1, Sy)
127
+ B2 = (33.0 * Sz4
128
+ - (18.0 * s * (s + 1.0) - 123.0) * Sz2
129
+ + (s ** 2 * (s + 1.0) ** 2 - 10.0 * s * (s + 1.0) + 102.0) * Id)
130
+ if q == 2:
131
+ return 0.5 * _sym(B2, Sx @ Sx - Sy @ Sy)
132
+ if q == -2:
133
+ return 0.5 * _sym(B2, _sym(Sx, Sy))
134
+ C3 = 11.0 * Sz3 - (3.0 * s * (s + 1.0) + 59.0) * Sz
135
+ P3 = Sp_mat @ Sp_mat @ Sp_mat
136
+ M3 = Sm_mat @ Sm_mat @ Sm_mat
137
+ if q == 3:
138
+ return 0.25 * _sym(C3, P3 + M3)
139
+ if q == -3:
140
+ return 0.25 * _sym(C3, (P3 - M3) / 1.0j)
141
+ D4 = 11.0 * Sz2 - s * (s + 1.0) * Id - 38.0 * Id
142
+ P4 = Sp_mat @ P3
143
+ M4 = Sm_mat @ M3
144
+ if q == 4:
145
+ return 0.25 * _sym(D4, P4 + M4)
146
+ if q == -4:
147
+ return 0.25 * _sym(D4, (P4 - M4) / 1.0j)
148
+ P5 = Sp_mat @ P4
149
+ M5 = Sm_mat @ M4
150
+ if q == 5:
151
+ return 0.25 * _sym(Sz, P5 + M5)
152
+ if q == -5:
153
+ return 0.25 * _sym(Sz, (P5 - M5) / 1.0j)
154
+ P6 = Sp_mat @ P5
155
+ M6 = Sm_mat @ M5
156
+ if q == 6:
157
+ return 0.5 * (P6 + M6)
158
+ if q == -6:
159
+ return 0.5 * (P6 - M6) / 1.0j
160
+
161
+ raise ValueError(f"unsupported (k, q) = ({k}, {q}); only k in {{2, 4, 6}} is available")
@@ -0,0 +1,23 @@
1
+ """Electron-nuclear hyperfine term built from an A tensor."""
2
+
3
+ import numpy as np
4
+
5
+ from .Spin_Operators import Ix_get, Iy_get, Iz_get, Sx_get, Sy_get, Sz_get
6
+
7
+
8
+ def H_Hyperfine(A, S, I):
9
+ """
10
+ Inputs: 3x3 A tensor (MHz), electron spin S, nuclear spin I.
11
+
12
+ Returns: the hyperfine Hamiltonian.
13
+ """
14
+ S_ops = (Sx_get(S, I), Sy_get(S, I), Sz_get(S, I))
15
+ I_ops = (Ix_get(S, I), Iy_get(S, I), Iz_get(S, I))
16
+ A = np.asarray(A, dtype=float)
17
+
18
+ H = np.zeros_like(S_ops[0])
19
+ for i in range(3):
20
+ for j in range(3):
21
+ if A[i, j] != 0.0:
22
+ H = H + A[i, j] * (S_ops[i] @ I_ops[j])
23
+ return H
dimspin/Team_ZFS.py ADDED
@@ -0,0 +1,24 @@
1
+ """Zero-field splitting term built from a D tensor."""
2
+
3
+ import numpy as np
4
+
5
+ from .Spin_Operators import Sx_get, Sy_get, Sz_get
6
+
7
+
8
+ def H_ZFS(D, S, I):
9
+ """
10
+ Inputs: 3x3 D tensor (MHz), electron spin S, nuclear spin I.
11
+
12
+ Returns: the zero-field splitting Hamiltonian.
13
+
14
+ Notes: the tensor is assumed symmetric; a nonsymmetric D tensor gives a non-Hermitian matrix.
15
+ """
16
+ S_ops = (Sx_get(S, I), Sy_get(S, I), Sz_get(S, I))
17
+ D = np.asarray(D, dtype=float)
18
+
19
+ H = np.zeros_like(S_ops[0])
20
+ for i in range(3):
21
+ for j in range(3):
22
+ if D[i, j] != 0.0:
23
+ H = H + D[i, j] * (S_ops[i] @ S_ops[j])
24
+ return H
@@ -0,0 +1,48 @@
1
+ """Assembly of the high-order zero-field splitting Hamiltonian.
2
+
3
+ H = sum_{k,q} B_k^q * O_k^q(S), with the Stevens operators from
4
+ Steven_Operator.stev_op.
5
+ """
6
+
7
+ import numpy as np
8
+
9
+ from .Steven_Operator import stev_op
10
+
11
+
12
+ def B_arrays_to_dict(B2=None, B4=None, B6=None):
13
+ """
14
+ Inputs: optional B2, B4, B6 arrays with 2k+1 entries each, ordered by
15
+ q = -k ... k.
16
+
17
+ Returns: a {(k, q): B_k^q} dict holding only the nonzero coefficients; an
18
+ array of the wrong length raises ValueError.
19
+ """
20
+ B = {}
21
+ for k, Bk in [(2, B2), (4, B4), (6, B6)]:
22
+ if Bk is None:
23
+ continue
24
+ Bk = np.asarray(Bk, dtype=float).reshape(-1)
25
+ if Bk.size != 2 * k + 1:
26
+ raise ValueError(f"B{k} must contain exactly {2 * k + 1} elements (q = -{k} ... {k}).")
27
+ for i, value in enumerate(Bk):
28
+ q = i - k
29
+ if value != 0:
30
+ B[(k, q)] = float(value)
31
+ return B
32
+
33
+
34
+ def H_ZFS_HighOrder(B, S, I, R=None):
35
+ """
36
+ Inputs: dict {(k, q): B_k^q} in MHz, electron and nuclear spins S and I,
37
+ optional 3x3 rotation R taking laboratory operators to the tensor frame.
38
+
39
+ Returns: the high-order zero-field splitting Hamiltonian.
40
+ """
41
+ nS = int(round(2 * S + 1))
42
+ nI = int(round(2 * I + 1))
43
+ H = np.zeros((nS * nI, nS * nI), dtype=complex)
44
+ for (k, q), Bkq in B.items():
45
+ if Bkq == 0:
46
+ continue
47
+ H = H + Bkq * stev_op(k, q, S, I, R)
48
+ return H