hklforge 0.2.1__tar.gz → 0.2.2__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.
Files changed (96) hide show
  1. {hklforge-0.2.1 → hklforge-0.2.2}/CLAUDE.md +17 -0
  2. {hklforge-0.2.1 → hklforge-0.2.2}/PKG-INFO +2 -1
  3. {hklforge-0.2.1 → hklforge-0.2.2}/README.md +1 -0
  4. {hklforge-0.2.1 → hklforge-0.2.2}/docs/README.md +2 -2
  5. {hklforge-0.2.1 → hklforge-0.2.2}/docs/modes.md +69 -2
  6. {hklforge-0.2.1 → hklforge-0.2.2}/docs/verification.md +21 -0
  7. {hklforge-0.2.1 → hklforge-0.2.2}/src/hklforge/draw.py +1 -1
  8. {hklforge-0.2.1 → hklforge-0.2.2}/src/hklforge/rotation.py +25 -0
  9. {hklforge-0.2.1 → hklforge-0.2.2}/src/hklforge/solve.py +303 -35
  10. {hklforge-0.2.1 → hklforge-0.2.2}/src/hklforge.egg-info/PKG-INFO +2 -1
  11. {hklforge-0.2.1 → hklforge-0.2.2}/src/hklforge.egg-info/SOURCES.txt +1 -0
  12. {hklforge-0.2.1 → hklforge-0.2.2}/src/hklforge.egg-info/scm_file_list.json +1 -0
  13. hklforge-0.2.2/src/hklforge.egg-info/scm_version.json +8 -0
  14. hklforge-0.2.2/tests/test_energy.py +239 -0
  15. hklforge-0.2.1/src/hklforge.egg-info/scm_version.json +0 -8
  16. {hklforge-0.2.1 → hklforge-0.2.2}/.githooks/pre-push +0 -0
  17. {hklforge-0.2.1 → hklforge-0.2.2}/.github/workflows/ci.yml +0 -0
  18. {hklforge-0.2.1 → hklforge-0.2.2}/.github/workflows/publish.yml +0 -0
  19. {hklforge-0.2.1 → hklforge-0.2.2}/.gitignore +0 -0
  20. {hklforge-0.2.1 → hklforge-0.2.2}/.vscode/settings.json +0 -0
  21. {hklforge-0.2.1 → hklforge-0.2.2}/LICENSE +0 -0
  22. {hklforge-0.2.1 → hklforge-0.2.2}/dev/build_notebooks.py +0 -0
  23. {hklforge-0.2.1 → hklforge-0.2.2}/dev/cdp.py +0 -0
  24. {hklforge-0.2.1 → hklforge-0.2.2}/dev/check_naz_scan.py +0 -0
  25. {hklforge-0.2.1 → hklforge-0.2.2}/dev/check_viewer.py +0 -0
  26. {hklforge-0.2.1 → hklforge-0.2.2}/dev/conda-forge/recipe.yaml +0 -0
  27. {hklforge-0.2.1 → hklforge-0.2.2}/dev/conda-forge/run_test.py +0 -0
  28. {hklforge-0.2.1 → hklforge-0.2.2}/dev/diffcalc_stats.py +0 -0
  29. {hklforge-0.2.1 → hklforge-0.2.2}/dev/make_spacegroup_table.py +0 -0
  30. {hklforge-0.2.1 → hklforge-0.2.2}/dev/reference/bernina_two_reflections.py +0 -0
  31. {hklforge-0.2.1 → hklforge-0.2.2}/dev/reference/check_two_reflections_vs_diffcalc.py +0 -0
  32. {hklforge-0.2.1 → hklforge-0.2.2}/dev/reference/compare_ad_hoc_psic.py +0 -0
  33. {hklforge-0.2.1 → hklforge-0.2.2}/dev/reference/compare_libhkl_builtins.py +0 -0
  34. {hklforge-0.2.1 → hklforge-0.2.2}/dev/reference/compare_libhkl_e6c.py +0 -0
  35. {hklforge-0.2.1 → hklforge-0.2.2}/dev/reference/compare_libhkl_planner.py +0 -0
  36. {hklforge-0.2.1 → hklforge-0.2.2}/docs/crystallography.md +0 -0
  37. {hklforge-0.2.1 → hklforge-0.2.2}/docs/figures/azimuths.png +0 -0
  38. {hklforge-0.2.1 → hklforge-0.2.2}/docs/figures/omega.png +0 -0
  39. {hklforge-0.2.1 → hklforge-0.2.2}/docs/figures/psi.png +0 -0
  40. {hklforge-0.2.1 → hklforge-0.2.2}/docs/figures/psic_circles.png +0 -0
  41. {hklforge-0.2.1 → hklforge-0.2.2}/docs/figures/reference_angles.png +0 -0
  42. {hklforge-0.2.1 → hklforge-0.2.2}/docs/figures/scattering.png +0 -0
  43. {hklforge-0.2.1 → hklforge-0.2.2}/docs/figures/surface_angles.png +0 -0
  44. {hklforge-0.2.1 → hklforge-0.2.2}/docs/figures/tau.png +0 -0
  45. {hklforge-0.2.1 → hklforge-0.2.2}/docs/make_figures.py +0 -0
  46. {hklforge-0.2.1 → hklforge-0.2.2}/docs/releasing.md +0 -0
  47. {hklforge-0.2.1 → hklforge-0.2.2}/docs/viewer.md +0 -0
  48. {hklforge-0.2.1 → hklforge-0.2.2}/docs/virtual_angles.md +0 -0
  49. {hklforge-0.2.1 → hklforge-0.2.2}/examples/01_quickstart.ipynb +0 -0
  50. {hklforge-0.2.1 → hklforge-0.2.2}/examples/02_virtual_angles.ipynb +0 -0
  51. {hklforge-0.2.1 → hklforge-0.2.2}/examples/03_diffcalc_comparison.ipynb +0 -0
  52. {hklforge-0.2.1 → hklforge-0.2.2}/examples/04_double_diffraction.ipynb +0 -0
  53. {hklforge-0.2.1 → hklforge-0.2.2}/examples/05_custom_geometries.ipynb +0 -0
  54. {hklforge-0.2.1 → hklforge-0.2.2}/examples/06_crystallography_planning.ipynb +0 -0
  55. {hklforge-0.2.1 → hklforge-0.2.2}/pyproject.toml +0 -0
  56. {hklforge-0.2.1 → hklforge-0.2.2}/setup.cfg +0 -0
  57. {hklforge-0.2.1 → hklforge-0.2.2}/src/hklforge/__init__.py +0 -0
  58. {hklforge-0.2.1 → hklforge-0.2.2}/src/hklforge/crystal/__init__.py +0 -0
  59. {hklforge-0.2.1 → hklforge-0.2.2}/src/hklforge/crystal/bz.py +0 -0
  60. {hklforge-0.2.1 → hklforge-0.2.2}/src/hklforge/crystal/cell.py +0 -0
  61. {hklforge-0.2.1 → hklforge-0.2.2}/src/hklforge/crystal/cif.py +0 -0
  62. {hklforge-0.2.1 → hklforge-0.2.2}/src/hklforge/crystal/reflections.py +0 -0
  63. {hklforge-0.2.1 → hklforge-0.2.2}/src/hklforge/crystal/spacegroups.json +0 -0
  64. {hklforge-0.2.1 → hklforge-0.2.2}/src/hklforge/crystal/spacegroups.py +0 -0
  65. {hklforge-0.2.1 → hklforge-0.2.2}/src/hklforge/crystal/structure.py +0 -0
  66. {hklforge-0.2.1 → hklforge-0.2.2}/src/hklforge/geometries/__init__.py +0 -0
  67. {hklforge-0.2.1 → hklforge-0.2.2}/src/hklforge/geometries/e4ch.yml +0 -0
  68. {hklforge-0.2.1 → hklforge-0.2.2}/src/hklforge/geometries/fourcv.yml +0 -0
  69. {hklforge-0.2.1 → hklforge-0.2.2}/src/hklforge/geometries/k4cv.yml +0 -0
  70. {hklforge-0.2.1 → hklforge-0.2.2}/src/hklforge/geometries/k6c.yml +0 -0
  71. {hklforge-0.2.1 → hklforge-0.2.2}/src/hklforge/geometries/psic.yml +0 -0
  72. {hklforge-0.2.1 → hklforge-0.2.2}/src/hklforge/geometry.py +0 -0
  73. {hklforge-0.2.1 → hklforge-0.2.2}/src/hklforge/lattice.py +0 -0
  74. {hklforge-0.2.1 → hklforge-0.2.2}/src/hklforge/orient.py +0 -0
  75. {hklforge-0.2.1 → hklforge-0.2.2}/src/hklforge/view/__init__.py +0 -0
  76. {hklforge-0.2.1 → hklforge-0.2.2}/src/hklforge/view/explorer.py +0 -0
  77. {hklforge-0.2.1 → hklforge-0.2.2}/src/hklforge/view/viewer.html +0 -0
  78. {hklforge-0.2.1 → hklforge-0.2.2}/src/hklforge/virtual.py +0 -0
  79. {hklforge-0.2.1 → hklforge-0.2.2}/src/hklforge.egg-info/dependency_links.txt +0 -0
  80. {hklforge-0.2.1 → hklforge-0.2.2}/src/hklforge.egg-info/requires.txt +0 -0
  81. {hklforge-0.2.1 → hklforge-0.2.2}/src/hklforge.egg-info/top_level.txt +0 -0
  82. {hklforge-0.2.1 → hklforge-0.2.2}/tests/data/Bi2Se3.cif +0 -0
  83. {hklforge-0.2.1 → hklforge-0.2.2}/tests/data/GaAs.cif +0 -0
  84. {hklforge-0.2.1 → hklforge-0.2.2}/tests/data/TiO2_rutile.cif +0 -0
  85. {hklforge-0.2.1 → hklforge-0.2.2}/tests/test_builtins.py +0 -0
  86. {hklforge-0.2.1 → hklforge-0.2.2}/tests/test_bz.py +0 -0
  87. {hklforge-0.2.1 → hklforge-0.2.2}/tests/test_crystal.py +0 -0
  88. {hklforge-0.2.1 → hklforge-0.2.2}/tests/test_double.py +0 -0
  89. {hklforge-0.2.1 → hklforge-0.2.2}/tests/test_forward.py +0 -0
  90. {hklforge-0.2.1 → hklforge-0.2.2}/tests/test_frame.py +0 -0
  91. {hklforge-0.2.1 → hklforge-0.2.2}/tests/test_orient.py +0 -0
  92. {hklforge-0.2.1 → hklforge-0.2.2}/tests/test_rotation.py +0 -0
  93. {hklforge-0.2.1 → hklforge-0.2.2}/tests/test_solve_diffcalc.py +0 -0
  94. {hklforge-0.2.1 → hklforge-0.2.2}/tests/test_solve_generic.py +0 -0
  95. {hklforge-0.2.1 → hklforge-0.2.2}/tests/test_view.py +0 -0
  96. {hklforge-0.2.1 → hklforge-0.2.2}/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.1
3
+ Version: 0.2.2
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, double diffraction and the detector distance,
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
@@ -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.k)
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: