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

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"))

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

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

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.1.0
File Size Uploaded Provenance Actions
hklforge-0.1.0.tar.gz 703.4 kB With provenance Details for hklforge-0.1.0.tar.gz

Built distribution (wheel)

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

Total release size: 770.0 kB

Release files / hklforge-0.1.0.tar.gz

Download URL hklforge-0.1.0.tar.gz
Size 703.4 kB
Tags Source
SHA-256 checksum
How to use checksums
24b34b287d8ff04f0d9939ff25874685088ff9c3531966a419a55f44bf7fc49f
BLAKE2b-256 checksum
How to use checksums
b04026735c75466d1e35dcf7f01117436f2daa8d8a1fb9f5be1dff64565d8279
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 9, 2026.

Transparency log

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

Download URL hklforge-0.1.0-py3-none-any.whl
Size 66.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2621bc2eac842de51efe0cff1a8ead2074f9acbaca1865880a12343bb475a72b
BLAKE2b-256 checksum
How to use checksums
97189e2ddbe23a2a271fda0080bf1fc2419b4fce150d8a681a98121f61695259
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 9, 2026.

Transparency log

Security for hklforge 0.1.0

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.1.0.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 9, 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