hklforge 0.2.1__tar.gz → 0.2.3__tar.gz
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.
- {hklforge-0.2.1 → hklforge-0.2.3}/CLAUDE.md +17 -0
- {hklforge-0.2.1 → hklforge-0.2.3}/PKG-INFO +2 -1
- {hklforge-0.2.1 → hklforge-0.2.3}/README.md +1 -0
- {hklforge-0.2.1 → hklforge-0.2.3}/docs/README.md +2 -2
- {hklforge-0.2.1 → hklforge-0.2.3}/docs/modes.md +69 -2
- {hklforge-0.2.1 → hklforge-0.2.3}/docs/verification.md +21 -0
- {hklforge-0.2.1 → hklforge-0.2.3}/src/hklforge/crystal/spacegroups.py +25 -0
- {hklforge-0.2.1 → hklforge-0.2.3}/src/hklforge/crystal/structure.py +30 -1
- {hklforge-0.2.1 → hklforge-0.2.3}/src/hklforge/draw.py +1 -1
- {hklforge-0.2.1 → hklforge-0.2.3}/src/hklforge/rotation.py +25 -0
- {hklforge-0.2.1 → hklforge-0.2.3}/src/hklforge/solve.py +303 -35
- {hklforge-0.2.1 → hklforge-0.2.3}/src/hklforge/view/viewer.html +3 -1
- {hklforge-0.2.1 → hklforge-0.2.3}/src/hklforge.egg-info/PKG-INFO +2 -1
- {hklforge-0.2.1 → hklforge-0.2.3}/src/hklforge.egg-info/SOURCES.txt +1 -0
- {hklforge-0.2.1 → hklforge-0.2.3}/src/hklforge.egg-info/scm_file_list.json +1 -0
- hklforge-0.2.3/src/hklforge.egg-info/scm_version.json +8 -0
- {hklforge-0.2.1 → hklforge-0.2.3}/tests/test_crystal.py +43 -0
- hklforge-0.2.3/tests/test_energy.py +239 -0
- hklforge-0.2.1/src/hklforge.egg-info/scm_version.json +0 -8
- {hklforge-0.2.1 → hklforge-0.2.3}/.githooks/pre-push +0 -0
- {hklforge-0.2.1 → hklforge-0.2.3}/.github/workflows/ci.yml +0 -0
- {hklforge-0.2.1 → hklforge-0.2.3}/.github/workflows/publish.yml +0 -0
- {hklforge-0.2.1 → hklforge-0.2.3}/.gitignore +0 -0
- {hklforge-0.2.1 → hklforge-0.2.3}/.vscode/settings.json +0 -0
- {hklforge-0.2.1 → hklforge-0.2.3}/LICENSE +0 -0
- {hklforge-0.2.1 → hklforge-0.2.3}/dev/build_notebooks.py +0 -0
- {hklforge-0.2.1 → hklforge-0.2.3}/dev/cdp.py +0 -0
- {hklforge-0.2.1 → hklforge-0.2.3}/dev/check_naz_scan.py +0 -0
- {hklforge-0.2.1 → hklforge-0.2.3}/dev/check_viewer.py +0 -0
- {hklforge-0.2.1 → hklforge-0.2.3}/dev/conda-forge/recipe.yaml +0 -0
- {hklforge-0.2.1 → hklforge-0.2.3}/dev/conda-forge/run_test.py +0 -0
- {hklforge-0.2.1 → hklforge-0.2.3}/dev/diffcalc_stats.py +0 -0
- {hklforge-0.2.1 → hklforge-0.2.3}/dev/make_spacegroup_table.py +0 -0
- {hklforge-0.2.1 → hklforge-0.2.3}/dev/reference/bernina_two_reflections.py +0 -0
- {hklforge-0.2.1 → hklforge-0.2.3}/dev/reference/check_two_reflections_vs_diffcalc.py +0 -0
- {hklforge-0.2.1 → hklforge-0.2.3}/dev/reference/compare_ad_hoc_psic.py +0 -0
- {hklforge-0.2.1 → hklforge-0.2.3}/dev/reference/compare_libhkl_builtins.py +0 -0
- {hklforge-0.2.1 → hklforge-0.2.3}/dev/reference/compare_libhkl_e6c.py +0 -0
- {hklforge-0.2.1 → hklforge-0.2.3}/dev/reference/compare_libhkl_planner.py +0 -0
- {hklforge-0.2.1 → hklforge-0.2.3}/docs/crystallography.md +0 -0
- {hklforge-0.2.1 → hklforge-0.2.3}/docs/figures/azimuths.png +0 -0
- {hklforge-0.2.1 → hklforge-0.2.3}/docs/figures/omega.png +0 -0
- {hklforge-0.2.1 → hklforge-0.2.3}/docs/figures/psi.png +0 -0
- {hklforge-0.2.1 → hklforge-0.2.3}/docs/figures/psic_circles.png +0 -0
- {hklforge-0.2.1 → hklforge-0.2.3}/docs/figures/reference_angles.png +0 -0
- {hklforge-0.2.1 → hklforge-0.2.3}/docs/figures/scattering.png +0 -0
- {hklforge-0.2.1 → hklforge-0.2.3}/docs/figures/surface_angles.png +0 -0
- {hklforge-0.2.1 → hklforge-0.2.3}/docs/figures/tau.png +0 -0
- {hklforge-0.2.1 → hklforge-0.2.3}/docs/make_figures.py +0 -0
- {hklforge-0.2.1 → hklforge-0.2.3}/docs/releasing.md +0 -0
- {hklforge-0.2.1 → hklforge-0.2.3}/docs/viewer.md +0 -0
- {hklforge-0.2.1 → hklforge-0.2.3}/docs/virtual_angles.md +0 -0
- {hklforge-0.2.1 → hklforge-0.2.3}/examples/01_quickstart.ipynb +0 -0
- {hklforge-0.2.1 → hklforge-0.2.3}/examples/02_virtual_angles.ipynb +0 -0
- {hklforge-0.2.1 → hklforge-0.2.3}/examples/03_diffcalc_comparison.ipynb +0 -0
- {hklforge-0.2.1 → hklforge-0.2.3}/examples/04_double_diffraction.ipynb +0 -0
- {hklforge-0.2.1 → hklforge-0.2.3}/examples/05_custom_geometries.ipynb +0 -0
- {hklforge-0.2.1 → hklforge-0.2.3}/examples/06_crystallography_planning.ipynb +0 -0
- {hklforge-0.2.1 → hklforge-0.2.3}/pyproject.toml +0 -0
- {hklforge-0.2.1 → hklforge-0.2.3}/setup.cfg +0 -0
- {hklforge-0.2.1 → hklforge-0.2.3}/src/hklforge/__init__.py +0 -0
- {hklforge-0.2.1 → hklforge-0.2.3}/src/hklforge/crystal/__init__.py +0 -0
- {hklforge-0.2.1 → hklforge-0.2.3}/src/hklforge/crystal/bz.py +0 -0
- {hklforge-0.2.1 → hklforge-0.2.3}/src/hklforge/crystal/cell.py +0 -0
- {hklforge-0.2.1 → hklforge-0.2.3}/src/hklforge/crystal/cif.py +0 -0
- {hklforge-0.2.1 → hklforge-0.2.3}/src/hklforge/crystal/reflections.py +0 -0
- {hklforge-0.2.1 → hklforge-0.2.3}/src/hklforge/crystal/spacegroups.json +0 -0
- {hklforge-0.2.1 → hklforge-0.2.3}/src/hklforge/geometries/__init__.py +0 -0
- {hklforge-0.2.1 → hklforge-0.2.3}/src/hklforge/geometries/e4ch.yml +0 -0
- {hklforge-0.2.1 → hklforge-0.2.3}/src/hklforge/geometries/fourcv.yml +0 -0
- {hklforge-0.2.1 → hklforge-0.2.3}/src/hklforge/geometries/k4cv.yml +0 -0
- {hklforge-0.2.1 → hklforge-0.2.3}/src/hklforge/geometries/k6c.yml +0 -0
- {hklforge-0.2.1 → hklforge-0.2.3}/src/hklforge/geometries/psic.yml +0 -0
- {hklforge-0.2.1 → hklforge-0.2.3}/src/hklforge/geometry.py +0 -0
- {hklforge-0.2.1 → hklforge-0.2.3}/src/hklforge/lattice.py +0 -0
- {hklforge-0.2.1 → hklforge-0.2.3}/src/hklforge/orient.py +0 -0
- {hklforge-0.2.1 → hklforge-0.2.3}/src/hklforge/view/__init__.py +0 -0
- {hklforge-0.2.1 → hklforge-0.2.3}/src/hklforge/view/explorer.py +0 -0
- {hklforge-0.2.1 → hklforge-0.2.3}/src/hklforge/virtual.py +0 -0
- {hklforge-0.2.1 → hklforge-0.2.3}/src/hklforge.egg-info/dependency_links.txt +0 -0
- {hklforge-0.2.1 → hklforge-0.2.3}/src/hklforge.egg-info/requires.txt +0 -0
- {hklforge-0.2.1 → hklforge-0.2.3}/src/hklforge.egg-info/top_level.txt +0 -0
- {hklforge-0.2.1 → hklforge-0.2.3}/tests/data/Bi2Se3.cif +0 -0
- {hklforge-0.2.1 → hklforge-0.2.3}/tests/data/GaAs.cif +0 -0
- {hklforge-0.2.1 → hklforge-0.2.3}/tests/data/TiO2_rutile.cif +0 -0
- {hklforge-0.2.1 → hklforge-0.2.3}/tests/test_builtins.py +0 -0
- {hklforge-0.2.1 → hklforge-0.2.3}/tests/test_bz.py +0 -0
- {hklforge-0.2.1 → hklforge-0.2.3}/tests/test_double.py +0 -0
- {hklforge-0.2.1 → hklforge-0.2.3}/tests/test_forward.py +0 -0
- {hklforge-0.2.1 → hklforge-0.2.3}/tests/test_frame.py +0 -0
- {hklforge-0.2.1 → hklforge-0.2.3}/tests/test_orient.py +0 -0
- {hklforge-0.2.1 → hklforge-0.2.3}/tests/test_rotation.py +0 -0
- {hklforge-0.2.1 → hklforge-0.2.3}/tests/test_solve_diffcalc.py +0 -0
- {hklforge-0.2.1 → hklforge-0.2.3}/tests/test_solve_generic.py +0 -0
- {hklforge-0.2.1 → hklforge-0.2.3}/tests/test_view.py +0 -0
- {hklforge-0.2.1 → hklforge-0.2.3}/tests/test_virtual.py +0 -0
|
@@ -203,6 +203,23 @@ then executed with nbconvert (edit the generator, not the notebooks).
|
|
|
203
203
|
escape/eco must keep working with each hklforge release meanwhile: changes
|
|
204
204
|
here stay additive.
|
|
205
205
|
|
|
206
|
+
## Free energy (added 2026-11-19)
|
|
207
|
+
|
|
208
|
+
`mode["energy"] = (lo, hi)` (eV, None = open) makes the energy one more
|
|
209
|
+
unknown, for modes over-determined by one at a fixed energy (owner's
|
|
210
|
+
request: "loosen the energy constraint to find a solution"). Positions get an
|
|
211
|
+
`"energy"` key; `Diffractometer.k_of(angles)` gives their |k| and `hkl`,
|
|
212
|
+
`spots`, `draw` use it. Fixed-energy modes are unchanged (no key, same
|
|
213
|
+
cases). Still exact: without k, Bragg gives 1/k = -2 x.q/|q|^2 and every
|
|
214
|
+
x-group constraint is linear in x (`_Context.x_planes`), so two are allowed.
|
|
215
|
+
Cases `e_q/e_q_x/e_q_naz/e_axis` (detector held: k' gives 2theta, so k),
|
|
216
|
+
`e_beam` (nD=2, nS = x constraints + naz; 0..3 sample circles), `e_q1`
|
|
217
|
+
(nD=1, nS=1: mirror of b in Q-hat on the detector cone, degree-2 trig
|
|
218
|
+
polynomial, `rotation.solve_trig_poly`). Not supported: qaz, between, nD=1
|
|
219
|
+
with constraints. Verified in `tests/test_energy.py` (docs/verification.md
|
|
220
|
+
"Free energy"): 880 random-geometry trials truth recovered; solutions = the
|
|
221
|
+
fixed-energy planner's at their energy; e_q1 = brute-force eta scan (47/47).
|
|
222
|
+
|
|
206
223
|
## Next
|
|
207
224
|
|
|
208
225
|
Kappa <-> Euler conversion modes (a kappa *geometry* already works, see
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: hklforge
|
|
3
|
-
Version: 0.2.
|
|
3
|
+
Version: 0.2.3
|
|
4
4
|
Summary: Diffractometer angle solvers generated exactly from a declared geometry, with crystallography for planning
|
|
5
5
|
Author: Henrik Lemke
|
|
6
6
|
License-Expression: BSD-3-Clause
|
|
@@ -89,6 +89,7 @@ d.solve((1, 1, 1), {"mu": 0, "nu": 0, "h2": (2, 2, 0)}) # double diffraction
|
|
|
89
89
|
d.solve((1, 1, 1), {"eta": 0, "chi": 0, "h2": (2, 2, 0)}, detector="between")
|
|
90
90
|
d.solve((1, 1, 2), {"mu": 0, "nu": 0, "psi": 30}) # needs reference= / reference_hkl=
|
|
91
91
|
d.solve((1, 1, 2), {"nu": 0, "bisect": True, "omega": 0}) # bisecting
|
|
92
|
+
d.solve((1, 1, 3), {"mu": 0, "chi": 30, "phi": 20, "delta": 30, "energy": (5000, 20000)}) # energy free
|
|
92
93
|
print(d.plan({"chi": 10, "phi": 20, "betain": 5}).describe())
|
|
93
94
|
angles = d.solve((1, 1, 2), {"mu": 0, "nu": 0, "betain": 2})[0]
|
|
94
95
|
d.pseudo(angles) # all virtual angles of a position
|
|
@@ -47,6 +47,7 @@ d.solve((1, 1, 1), {"mu": 0, "nu": 0, "h2": (2, 2, 0)}) # double diffraction
|
|
|
47
47
|
d.solve((1, 1, 1), {"eta": 0, "chi": 0, "h2": (2, 2, 0)}, detector="between")
|
|
48
48
|
d.solve((1, 1, 2), {"mu": 0, "nu": 0, "psi": 30}) # needs reference= / reference_hkl=
|
|
49
49
|
d.solve((1, 1, 2), {"nu": 0, "bisect": True, "omega": 0}) # bisecting
|
|
50
|
+
d.solve((1, 1, 3), {"mu": 0, "chi": 30, "phi": 20, "delta": 30, "energy": (5000, 20000)}) # energy free
|
|
50
51
|
print(d.plan({"chi": 10, "phi": 20, "betain": 5}).describe())
|
|
51
52
|
angles = d.solve((1, 1, 2), {"mu": 0, "nu": 0, "betain": 2})[0]
|
|
52
53
|
d.pseudo(angles) # all virtual angles of a position
|
|
@@ -4,8 +4,8 @@
|
|
|
4
4
|
beta, betain, betaout, tau, psi, omega, bisect), with its definition, a 3D
|
|
5
5
|
figure and what holding it fixes.
|
|
6
6
|
- [Modes](modes.md): how to write a mode, how many things to hold, the closed-form
|
|
7
|
-
chain used for each case,
|
|
8
|
-
other geometries.
|
|
7
|
+
chain used for each case, the energy as a free unknown within limits,
|
|
8
|
+
double diffraction and the detector distance, other geometries.
|
|
9
9
|
- [Crystallography and orientation](crystallography.md): space groups
|
|
10
10
|
(absences with the reason, equivalents), cells, CIF and structure factors,
|
|
11
11
|
reflection lists and groups, U from reflections, `detector_towards`; what
|
|
@@ -18,6 +18,7 @@ at, plus virtual-angle constraints ([definitions](virtual_angles.md)):
|
|
|
18
18
|
| `bin_eq_bout`, `a_eq_b`, `bisect` | `True` |
|
|
19
19
|
| `h2` | (h, k, l) of a second reflection to hold in Bragg condition |
|
|
20
20
|
| `vomega` | `omega`, for a geometry that has a circle named omega |
|
|
21
|
+
| `energy` | (lo, hi) in eV: the energy is free within these limits ([below](#the-energy-as-one-more-unknown)) |
|
|
21
22
|
|
|
22
23
|
`solve` returns **every** solution: free circles wrapped to (−180, 180], held
|
|
23
24
|
circles as given. Each solution is re-checked through the forward model
|
|
@@ -40,7 +41,9 @@ Count the free circles: nS on the sample, nD on the detector.
|
|
|
40
41
|
the bisect circle: one free outer circle per axis constraint, and exactly two
|
|
41
42
|
free circles from the bisect circle inwards.
|
|
42
43
|
- nD = 0 is rejected (|Q| would be fixed), and so is anything that leaves a
|
|
43
|
-
continuum or over-determines; the `ModeError` says which and why.
|
|
44
|
+
continuum or over-determines; the `ModeError` says which and why. An
|
|
45
|
+
over-determined mode can often be solved by letting the energy vary
|
|
46
|
+
(next section).
|
|
44
47
|
|
|
45
48
|
The counts in the table below are for a near-cubic crystal (a = 4 Å, 9 keV,
|
|
46
49
|
(1,1,2)); a given hkl can have fewer (e.g. `{"qaz": 90, "chi": 0, "phi": 0}`
|
|
@@ -77,6 +80,69 @@ Where the solutions form a continuum (e.g. psic chi = 0 makes eta and phi the
|
|
|
77
80
|
same axis; h2 = h1) `Degenerate` is raised with the mode in the message;
|
|
78
81
|
contradictory equations on parallel vectors just give no solution.
|
|
79
82
|
|
|
83
|
+
## The energy as one more unknown
|
|
84
|
+
|
|
85
|
+
A mode that is over-determined by one at a fixed energy (all detector circles
|
|
86
|
+
held, or one held sample circle too many) can be solved by freeing the
|
|
87
|
+
photon energy within limits:
|
|
88
|
+
|
|
89
|
+
```python
|
|
90
|
+
# mu, chi, phi and delta held: eta and nu alone cannot reach (1,1,3) at a fixed energy
|
|
91
|
+
d.solve((1, 1, 3), {"mu": 0, "chi": 30, "phi": 20, "delta": 30, "energy": (5000, 20000)})
|
|
92
|
+
# [{'mu': 0.0, 'eta': 77.848, 'chi': 30.0, 'phi': 20.0, 'nu': 94.625, 'delta': 30.0, 'energy': 5176.208},
|
|
93
|
+
# {'mu': 0.0, 'eta': 37.948, 'chi': 30.0, 'phi': 20.0, 'nu': 27.765, 'delta': 30.0, 'energy': 11075.263}]
|
|
94
|
+
d.solve((1, 1, 1), {"mu": 0, "eta": 10, "chi": 30, "phi": 45, "energy": (None, None)}) # crystal fixed
|
|
95
|
+
d.solve((2, 2, 0), {"nu": 0, "delta": 40, "mu": 0, "chi": 10, "energy": (5000, 20000)}) # detector fixed
|
|
96
|
+
d.solve((0, 0, 4), {"mu": 0, "chi": 30, "phi": 10, "h2": (2, 2, 0), "energy": (5000, 20000)})
|
|
97
|
+
d.solve((1, 1, 3), {"mu": 0, "phi": 0, "betain": 2, "h2": (1, -1, 3), "energy": (5000, 20000)})
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
`mode["energy"] = (lo, hi)` in eV (either may be `None`) makes the energy a
|
|
101
|
+
free unknown; each returned position then has its `"energy"` (eV), within the
|
|
102
|
+
limits, and `d.hkl`, `d.spots` and `draw` use it. The diffractometer's own
|
|
103
|
+
energy is not used for such a mode. Freeing the energy takes one degree of
|
|
104
|
+
freedom, so the mode needs **one more** held circle or constraint than at a
|
|
105
|
+
fixed energy; a mode that is already complete is refused with that message.
|
|
106
|
+
Positions with the energy free are found exactly, as before:
|
|
107
|
+
|
|
108
|
+
- **Detector held** (nD = 0): k' is fixed, so 2θ, and the energy follows from
|
|
109
|
+
|q| = 2k sin θ (one value). Then the sample as in `q`, `q_x`, `q_naz` or
|
|
110
|
+
`axis` (cases `e_q`, `e_q_x`, `e_q_naz`, `e_axis`).
|
|
111
|
+
- **Two free detector circles** (`e_beam`, sample first): without k, Bragg
|
|
112
|
+
only says where the energy is, 1/k = −2 x·q / |q|², and every x-group
|
|
113
|
+
constraint becomes **linear** in x = Rᵀb: betaout x·(v − 2(q·v)q/|q|²) =
|
|
114
|
+
sin βout, in = out x·(v − (q·v)q/|q|²) = 0, h2 x·(q₂ − |q₂|²/|q|² q) = 0,
|
|
115
|
+
psi x·(q̂ × p) = 0 on the side x·p > 0. So up to **two** x-group constraints
|
|
116
|
+
can be held. Free sample circles = number of x-group constraints + naz:
|
|
117
|
+
0 (crystal fixed: R known), 1 (one projection), 2 with two x-group
|
|
118
|
+
constraints (`cone_points` + `sp2`), 2 with one and naz (degree-8 pair),
|
|
119
|
+
3 with two and naz (`cone_points`, naz fixes the turn about b, `sp3`). The
|
|
120
|
+
energy is then k = −|q|² / (2 x·q) (only x·q < 0 is physical), and the
|
|
121
|
+
detector turns onto k' with `sp2`.
|
|
122
|
+
- **One free detector circle, one free sample circle** (`e_q1`): k' is the
|
|
123
|
+
mirror image of the beam in Q̂, k' = b − 2(b·Q̂)Q̂, and must lie on the cone
|
|
124
|
+
the single detector circle sweeps: a degree-2 trigonometric polynomial in
|
|
125
|
+
the sample circle (≤ 4 roots, `solve_trig_poly`). The detector by `sp1`.
|
|
126
|
+
|
|
127
|
+
Not supported with a free energy: `qaz`, `detector="between"`, one free
|
|
128
|
+
detector circle with any constraint, and bisect/omega unless all detector
|
|
129
|
+
circles are held. A continuum (e.g. psic mu = nu = 0 makes eta and delta
|
|
130
|
+
coaxial: with Q perpendicular to that axis every eta has its own energy)
|
|
131
|
+
raises `Degenerate`.
|
|
132
|
+
|
|
133
|
+
| what (psic) | mode | case | |
|
|
134
|
+
|---|---|---|---|
|
|
135
|
+
| one sample, one detector circle (eta, nu) | `{"mu": 0, "chi": 30, "phi": 20, "delta": 30, "energy": (lo, hi)}` | e_q1 | 2 (≤ 4) |
|
|
136
|
+
| crystal fixed, detector follows (Laue-like) | `{"mu": 0, "eta": 10, "chi": 30, "phi": 45, "energy": (lo, hi)}` | e_beam | 2 |
|
|
137
|
+
| detector fixed | `{"nu": 0, "delta": 40, "mu": 0, "chi": 10, "energy": (lo, hi)}` | e_q | 2 |
|
|
138
|
+
| two reflections with one free sample circle | `{"mu": 0, "chi": 30, "phi": 10, "h2": (2, 2, 0), "energy": (lo, hi)}` | e_beam | 2 (≤ 4) |
|
|
139
|
+
| fixed incidence and two reflections | `{"mu": 0, "phi": 0, "betain": 2, "h2": (1, -1, 3), "energy": (lo, hi)}` | e_beam | 4 (≤ 8) |
|
|
140
|
+
|
|
141
|
+
Counts for Si (a = 5.431 Å), (1,1,3) / (1,1,1) / (2,2,0) / (0,0,4) / (1,1,3) as in
|
|
142
|
+
the code above, 5-20 keV. In psic, eta and delta at mu = nu = 0 (and mu and
|
|
143
|
+
nu always) turn about one axis, so with only those two free a general Q is
|
|
144
|
+
out of reach at any energy: pick a pair on different axes, as above.
|
|
145
|
+
|
|
80
146
|
## psic examples, with diffcalc's names
|
|
81
147
|
|
|
82
148
|
| what | mode | case | typical count |
|
|
@@ -92,7 +158,8 @@ contradictory equations on parallel vectors just give no solution.
|
|
|
92
158
|
| two reflections, detector between | `{"eta": 0, "chi": 0, "h2": (2, 2, 0)}`, `detector="between"` | between | 8 |
|
|
93
159
|
|
|
94
160
|
Not supported (and not in diffcalc either): naz together with bisect / omega;
|
|
95
|
-
more than one x-group constraint (it would over-determine x
|
|
161
|
+
more than one x-group constraint at a fixed energy (it would over-determine x;
|
|
162
|
+
with the energy free, two are fine).
|
|
96
163
|
|
|
97
164
|
## Double diffraction and the detector distance
|
|
98
165
|
|
|
@@ -90,6 +90,27 @@ naz with two sample circles (degree-8 polynomial, no diffcalc code):
|
|
|
90
90
|
polishes every sign-change cell with scipy and compares: 33 cases, 48
|
|
91
91
|
sample settings found by both, none by only one.
|
|
92
92
|
|
|
93
|
+
## Free energy
|
|
94
|
+
|
|
95
|
+
`tests/test_energy.py`, as above on random geometries but with a random true
|
|
96
|
+
energy (4-20 keV) and the energy free in a random window around it: 40
|
|
97
|
+
trials for each of the 22 free-energy variants (detector held with every
|
|
98
|
+
sample case and omega; one detector + one sample circle; sample first with
|
|
99
|
+
0, 1 or 2 x-group constraints, with and without naz). 880 trials, the truth
|
|
100
|
+
found in every one, 2474 solutions, each re-checked through the forward
|
|
101
|
+
model at its own energy (hkl to 1e-8, constraints to 1e-6 deg). Two checks
|
|
102
|
+
outside the new code paths:
|
|
103
|
+
|
|
104
|
+
- every sample-first and one-detector-circle solution is also returned by the
|
|
105
|
+
fixed-energy planner at that energy, with one held sample circle released
|
|
106
|
+
(more than 200 positions);
|
|
107
|
+
- the degree-2 case (`e_q1`, psic with mu, chi, phi, nu held) against a scan of
|
|
108
|
+
eta on a 0.001° grid through the forward model, 1-60 keV: 48 combinations
|
|
109
|
+
of mode and hkl, the same number of solutions as sign changes in all 47
|
|
110
|
+
finite ones (26 solutions); the one continuum (eta and delta coaxial, Q
|
|
111
|
+
perpendicular to them) is reported as `Degenerate`, and the scan confirms
|
|
112
|
+
that the equation holds for every eta there.
|
|
113
|
+
|
|
93
114
|
## Double diffraction
|
|
94
115
|
|
|
95
116
|
`tests/test_double.py`, against the Bernina reference
|
|
@@ -66,6 +66,31 @@ def parse_triplet(text: str):
|
|
|
66
66
|
return R, t
|
|
67
67
|
|
|
68
68
|
|
|
69
|
+
def format_triplet(R, t) -> str:
|
|
70
|
+
"""(R, t) -> 'x,y,z' triplet, e.g. '-y+1/4,x+1/4,z+1/4' (``parse_triplet``'s inverse)."""
|
|
71
|
+
parts = []
|
|
72
|
+
for i in range(3):
|
|
73
|
+
terms = ""
|
|
74
|
+
for j, var in enumerate("xyz"):
|
|
75
|
+
c = int(round(R[i][j]))
|
|
76
|
+
if c:
|
|
77
|
+
terms += ("-" if c < 0 else "+" if terms else "") + (f"{abs(c)}*" if abs(c) != 1 else "") + var
|
|
78
|
+
f = Fraction(float(t[i])).limit_denominator(48)
|
|
79
|
+
if f:
|
|
80
|
+
terms += ("-" if f < 0 else "+" if terms else "") + str(abs(f))
|
|
81
|
+
parts.append(terms or "0")
|
|
82
|
+
return ",".join(parts)
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def same_ops(a, b) -> bool:
|
|
86
|
+
"""Whether two lists of (R, t) are the same set of operations (t modulo 1)."""
|
|
87
|
+
def key(op):
|
|
88
|
+
R, t = op
|
|
89
|
+
return (tuple(int(round(x)) for x in np.ravel(R)),
|
|
90
|
+
tuple(int(round((float(x) % 1.0) * 48)) % 48 for x in t))
|
|
91
|
+
return len(a) == len(b) and {key(o) for o in a} == {key(o) for o in b}
|
|
92
|
+
|
|
93
|
+
|
|
69
94
|
def _reduced(v) -> tuple:
|
|
70
95
|
v = [int(round(x)) for x in v]
|
|
71
96
|
g = 0
|
|
@@ -24,7 +24,7 @@ import numpy as np
|
|
|
24
24
|
|
|
25
25
|
from . import cif
|
|
26
26
|
from .cell import AVOGADRO, b_matrix, cell_tuple, volume
|
|
27
|
-
from .spacegroups import SpaceGroup, parse_triplet
|
|
27
|
+
from .spacegroups import SpaceGroup, format_triplet, parse_triplet, same_ops
|
|
28
28
|
|
|
29
29
|
try:
|
|
30
30
|
import xraydb
|
|
@@ -59,6 +59,35 @@ class Structure:
|
|
|
59
59
|
raise ValueError("a structure needs a space group or its symmetry operations")
|
|
60
60
|
self.ops = self.space_group.all_ops()
|
|
61
61
|
|
|
62
|
+
# --- as plain data (JSON) ---
|
|
63
|
+
|
|
64
|
+
def to_dict(self) -> dict:
|
|
65
|
+
"""Everything needed to rebuild it (``from_dict``), as JSON-ready data:
|
|
66
|
+
name, cell, space group name, the sites as
|
|
67
|
+
``[label, element, scatterer, x, y, z, occupancy, b_iso]`` and, only
|
|
68
|
+
when they are not the space group's own (a CIF's operations in another
|
|
69
|
+
setting, or no recognised space group), the symmetry operations as
|
|
70
|
+
'x,y,z' triplets."""
|
|
71
|
+
out = {"name": self.name, "cell": {k: float(v) for k, v in self.cell.items()},
|
|
72
|
+
"space_group": None if self.space_group is None else self.space_group.name,
|
|
73
|
+
"sites": [[s.label, s.element, s.scatterer, *(round(float(x), 6) for x in s.xyz),
|
|
74
|
+
round(float(s.occupancy), 6), round(float(s.b_iso), 6)] for s in self.sites]}
|
|
75
|
+
if self.space_group is None or not same_ops(self.ops, self.space_group.all_ops()):
|
|
76
|
+
out["ops"] = [format_triplet(R, t) for R, t in self.ops]
|
|
77
|
+
if self.notes:
|
|
78
|
+
out["notes"] = list(self.notes)
|
|
79
|
+
return out
|
|
80
|
+
|
|
81
|
+
@classmethod
|
|
82
|
+
def from_dict(cls, d: dict) -> "Structure":
|
|
83
|
+
"""The inverse of ``to_dict``."""
|
|
84
|
+
sg = SpaceGroup(d["space_group"]) if d.get("space_group") else None
|
|
85
|
+
ops = [parse_triplet(t) for t in d["ops"]] if d.get("ops") else None
|
|
86
|
+
sites = [Site(str(lab), str(el), str(scat), np.array([float(x), float(y), float(z)]),
|
|
87
|
+
float(occ), float(b)) for lab, el, scat, x, y, z, occ, b in d["sites"]]
|
|
88
|
+
return cls(str(d.get("name", "")), {k: float(v) for k, v in d["cell"].items()}, sg, sites, ops,
|
|
89
|
+
list(d.get("notes", [])))
|
|
90
|
+
|
|
62
91
|
# --- the unit cell ---
|
|
63
92
|
|
|
64
93
|
def atoms(self):
|
|
@@ -154,7 +154,7 @@ def draw(d, angles: dict, show=("ttheta",), ax=None, title=None, lab_axes=True,
|
|
|
154
154
|
sc.arrow(kout, COLORS["kout"], "k' (detector)" if hkls else "k'")
|
|
155
155
|
sc.arrow(Qh, COLORS["Q"], "Q")
|
|
156
156
|
for i, h in enumerate(hkls):
|
|
157
|
-
ki = unit(b + R @ d.ub @ np.asarray(h, float) / d.
|
|
157
|
+
ki = unit(b + R @ d.ub @ np.asarray(h, float) / d.k_of(angles))
|
|
158
158
|
sc.arrow(ki, "#e377c2", f"k'{i + 1} {tuple(int(x) if float(x).is_integer() else x for x in h)}",
|
|
159
159
|
lw=1.5, ls="--")
|
|
160
160
|
sc.plane(b, Qh, COLORS["Q"], size=1.0, alpha=0.06) # scattering plane
|
|
@@ -72,6 +72,31 @@ def solve_projection(w, axis, v, c: float) -> list[float]:
|
|
|
72
72
|
return solve_trig_linear(a, b, c - g, scale=np.linalg.norm(w) * np.linalg.norm(v))
|
|
73
73
|
|
|
74
74
|
|
|
75
|
+
def solve_trig_poly(f, degree: int, scale: float = 1.0) -> list[float]:
|
|
76
|
+
"""All t in (-pi, pi] with f(t) = 0, for f a real trigonometric polynomial
|
|
77
|
+
of at most ``degree`` (a sum of cos(j t), sin(j t), j <= degree).
|
|
78
|
+
|
|
79
|
+
The coefficients come exactly from 4 * degree samples (FFT); with
|
|
80
|
+
z = exp(i t), z^degree f is a polynomial of degree 2 * degree whose
|
|
81
|
+
unit-circle roots are all the t.
|
|
82
|
+
"""
|
|
83
|
+
n = 4 * degree
|
|
84
|
+
ts = 2 * np.pi * np.arange(n) / n
|
|
85
|
+
F = np.fft.fft([f(t) for t in ts]) / n
|
|
86
|
+
ck = {j: F[j % n] for j in range(-degree, degree + 1)}
|
|
87
|
+
big = max(abs(v) for v in ck.values())
|
|
88
|
+
if big < 1e-14 * scale:
|
|
89
|
+
raise Degenerate("equation holds for every angle")
|
|
90
|
+
poly = np.array([ck[j] for j in range(degree, -degree - 1, -1)])
|
|
91
|
+
poly = np.trim_zeros(np.where(abs(poly) < 1e-13 * big, 0, poly), "f")
|
|
92
|
+
roots = np.roots(poly) if poly.size > 1 else []
|
|
93
|
+
out = []
|
|
94
|
+
for t in (float(np.angle(z)) for z in roots if abs(abs(z) - 1) < 1e-4):
|
|
95
|
+
if not any(abs(_wrap(t - o)) < 1e-7 for o in out): # a double root once
|
|
96
|
+
out.append(t)
|
|
97
|
+
return out
|
|
98
|
+
|
|
99
|
+
|
|
75
100
|
# --- sub-problem 1: rotate p onto q about one axis --------------------------
|
|
76
101
|
|
|
77
102
|
def sp1(axis, p, q) -> float:
|