Skip to main content

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
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.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for hklforge 0.2.1
File Size Uploaded Provenance Actions
hklforge-0.2.1.tar.gz 923.3 kB With provenance Details for hklforge-0.2.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for hklforge 0.2.1
File Interpreter ABI Platform Actions
hklforge-0.2.1-py3-none-any.whl Python 3 none any Details for hklforge-0.2.1-py3-none-any.whl

Total release size: 1.2 MB

Release files / hklforge-0.2.1.tar.gz

Download URL hklforge-0.2.1.tar.gz
Size 923.3 kB
Tags Source
SHA-256 checksum
How to use checksums
5f1b734f4733fc5339cf69f52ceb2046d29ebe88728c394a8872662db4350614
BLAKE2b-256 checksum
How to use checksums
9f2cc8d3528731dc3e38ac709fee45d82613eaf22691500f038da2f9d3678c80
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 17, 2026.

Transparency log

Release files / hklforge-0.2.1-py3-none-any.whl

Download URL hklforge-0.2.1-py3-none-any.whl
Size 274.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
70cd1ab8fc4aa2a8d50f23e59374420058f39a1dbafdad51fc3e00b3ab10331b
BLAKE2b-256 checksum
How to use checksums
de3e1407e6550facbd9bd53f0a0b9ac0d68a79b586a9f1d2828e2f56e52337d6
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 17, 2026.

Transparency log

Security for hklforge 0.2.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.

Select from 2 files

Attestation for hklforge-0.2.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 17, 2026.

Transparency log

A 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.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page