hklforge
Diffractometer angle calculations for any geometry, with solvers that are generated exactly from the geometry rather than hand-written for one instrument (diffcalc) or found by numerical search (libhkl, ad_hoc_diffractometer).
Status: the geometry model, the exact sub-problems, the planner (geometry + mode -> all solutions) and all of diffcalc's virtual angles (psi, naz, qaz, alpha/beta, betain/betaout, omega/bisect, ...) exist and are tested against diffcalc (all 194 modes), libhkl (all 13 E6C modes), xrayutilities and the Bernina two-reflection solver. For planning, there is crystallography (space-group absences with the reason, equivalents, structure factors from a CIF, reflection lists), tested against gemmi, and orientation helpers (U from reflections, pointing the detector along a beam).
Documentation: docs/ - virtual angles
with 3D figures, modes, crystallography and orientation,
verification, 3D view, geometry explorer, stored geometries.
Example notebooks: examples/. Design notes and roadmap: CLAUDE.md.
Install
pip install hklforge # numpy + pyyaml only
pip install "hklforge[crystal]" # + pandas, xraydb: reflection tables, structure factors
pip install "hklforge[draw]" # + matplotlib: 3D drawings
pip install "hklforge[app]" # + ipywidgets: the geometry explorer in a notebook
Use
from hklforge import Geometry
psic = Geometry.builtin("psic") # You (1999) six-circle, as diffcalc
custom = Geometry.from_xrayutilities( # xrayutilities' QConversion syntax
sample=["x+", "z-", "y+", "z-"], detector=["x+", "z-"], r_i=(0, 1, 0))
psic.q_phi({"mu": 0, "eta": 10, "chi": 0, "phi": 0, "nu": 0, "delta": 20}, k=4.5)
from hklforge import Diffractometer
from hklforge.lattice import b_matrix
ub = b_matrix(5.431, 5.431, 5.431) # phi frame, q = UB h (U = 1)
d = Diffractometer(psic, ub, energy=9000, surface_hkl=(0, 0, 1), reference_hkl=(0, 0, 1))
d.solve((1, 1, 2), {"eta": 0, "chi": 0, "phi": 0}) # every branch, as dicts of degrees
d.solve((1, 1, 2), {"chi": 10, "phi": 20, "betain": 5})
d.solve((1, 1, 1), {"mu": 0, "nu": 0, "h2": (2, 2, 0)}) # double diffraction, detector on h1
d.solve((1, 1, 1), {"eta": 0, "chi": 0, "h2": (2, 2, 0)}, detector="between")
d.solve((1, 1, 2), {"mu": 0, "nu": 0, "psi": 30}) # needs reference= / reference_hkl=
d.solve((1, 1, 2), {"nu": 0, "bisect": True, "omega": 0}) # bisecting
d.solve((1, 1, 3), {"mu": 0, "chi": 30, "phi": 20, "delta": 30, "energy": (5000, 20000)}) # energy free
print(d.plan({"chi": 10, "phi": 20, "betain": 5}).describe())
angles = d.solve((1, 1, 2), {"mu": 0, "nu": 0, "betain": 2})[0]
d.pseudo(angles) # all virtual angles of a position
from hklforge.draw import draw # 3D drawing (matplotlib)
draw(d, angles, show=("ttheta", "psi"))
Geometry.builtins() # psic, fourcv, e4ch, k4cv, k6c (libhkl's names and frames)
from hklforge.view import page, message # interactive three.js view, any geometry
open("k6c.html", "w").write(page(Geometry.builtin("k6c")))
from hklforge.view.explorer import GeometryExplorer # how a geometry is built, step by step
GeometryExplorer().widget
d.scan((1, 1, 2), {"mu": 0, "nu": 0, "phi": 0}, "phi", range(-180, 181, 5)) # solutions vs a held value
For planning (numpy only at import; pandas for tables, xraydb for |F|):
from hklforge.crystal.spacegroups import SpaceGroup
from hklforge.crystal.structure import read_cif
from hklforge.crystal import reflections
SpaceGroup("P63/mmc").why_forbidden((0, 0, 1)) # 'forbidden ... by the 6₃ screw axis along [0 0 1] and a c-glide plane ...'
st = read_cif("Bi2Se3.cif")
st.structure_factor((0, 0, 3), energy=9000) # |F| with f0, f' + i f'' and Debye-Waller
reflections.reflection_list(st.cell, 9000, structure=st, d_min=1.5) # allowed, why not, mult, |F|, I rel
reflections.rotation_groups(hkls, ub, axis) # reflections a turn about an axis maps onto each other
from hklforge.orient import u_from_two # U from two (hkl, direction) pairs, Busing & Levy
psic.detector_towards(k_out_lab, near=angles) # detector circles pointing along a beam
from hklforge.crystal.bz import special_points # Γ, X, L, ... and the standard paths, in hkl
special_points(st.cell, "R-3m")
Tests (pytest) compare against diffcalc-core and xrayutilities, the crystallography against gemmi
(every space-group setting, structure factors).
License
BSD 3-Clause, see LICENSE. The software is provided "as is", without warranty of any kind. Check computed angles against your instrument's limits before moving motors.
Metadata
Release files for hklforge 0.3.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| hklforge-0.3.1.tar.gz | 953.8 kB | With provenance | Details for hklforge-0.3.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| hklforge-0.3.1-py3-none-any.whl | Python 3 | none | any | Details for hklforge-0.3.1-py3-none-any.whl |
Total release size: 1.2 MB
Release files / hklforge-0.3.1.tar.gz
| Download URL | hklforge-0.3.1.tar.gz |
|---|---|
| Size | 953.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
311f577e25b5046fac8ffeb04f2dc6bba1f1ea6cea2a143d116e08b12d6e6a36
|
|
BLAKE2b-256 checksum How to use checksums |
152acbeeca004fb6e07490e8a5b92eab2e448e34fe9053493703a5795750f2bf
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 21, 2026.
Transparency logRelease files / hklforge-0.3.1-py3-none-any.whl
| Download URL | hklforge-0.3.1-py3-none-any.whl |
|---|---|
| Size | 295.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
6afcb9a700bca1340a8d911e7cc16c7c5104d9957113b9b32a9e7a805331c614
|
|
BLAKE2b-256 checksum How to use checksums |
09cffa6433e67cebb15ae6c415c34a733256bd8c68d758ad2b4c86827043224e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 21, 2026.
Transparency logSecurity for hklforge 0.3.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for hklforge-0.3.1.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 21, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.