PhaseSmith
PhaseSmith is an early-stage powder-diffraction computation library with a
Rust numerical core and a typed Python/NumPy API. The current implementation
includes symmetric TCH, CW U/V/W/X/Y broadening, FCJ asymmetry, wavelength
components, extensible sample physics, multi-phase CW X-ray/neutron and neutron
TOF calculation, plus a first-class scripted Le Bail workflow. Analytical
derivatives are computed during fused peak accumulation. The crystallography
foundation includes native general-cell mathematics, P1 complex structure
factors, exact symmetry, bounded reflection generation, and prepared X-ray and
neutron scattering factors with analytical derivatives. General-symmetry
structural intensities and monochromatic structural patterns now have a fused
native values/JVP/VJP path, direct fixed CIF anisotropic displacement, and a
scriptable RietveldPhase API.
CIF-backed Le Bail can refine setting-aware lattice parameters with analytical
derivatives and guarded, stable-ID reflection-domain regeneration.
The first full CIF-backed Rietveld workflow now refines CW profile/background,
phase scale, lattice, symmetry-allowed coordinates, occupancy, and isotropic
displacement through matrix-free Rust JVP/VJP products with safe checkpoints
and structured logs.
Model-independent preprocessing now includes a native Smooth Bruckner
background implementation compatible with pinned xypattern/Dioptas behavior,
plus optional Chebyshev compression and plain NumPy subtraction results.
Install the Python interface from PyPI or the native Rust facade from crates.io:
python -m pip install phasesmith
cargo add phasesmith
Use the documentation index to follow the shortest path from powder data and a CIF to background subtraction, Le Bail extraction, Rietveld refinement, reports, and persistence. The architecture and non-negotiable numerical rules are in PROJECT_BRIEF.md. The tag-driven GitHub/PyPI/crates.io process is documented in docs/releasing.md.
Development
Requires Rust 1.85 or newer, Python 3.11 or newer, uv, and maturin.
uv venv
uv pip install -e '.[dev]'
maturin develop --uv
cargo test --workspace --all-features
uv run pytest
Editable environments retain the absolute checkout location. After moving or
renaming the repository, recreate .venv, repeat the three setup commands
above, and confirm uv run python -c "import phasesmith; print(phasesmith.__file__)"
points into the current checkout before running the gate.
Native GUI applications
GUI applications are separate consumers of the published Rust library. A
future Tauri or other native application can depend on phasesmith, own its
presentation state and background jobs, and call the native workflows directly
without bundling Python.
Run the Rust benchmarks with cargo bench -p phasesmith-core. For a comparable
optimized Python-to-Rust measurement, build the release extension and require
release mode explicitly:
maturin develop --release --uv
uv run python benchmarks/profile.py --require-release
uv run python benchmarks/lebail.py --require-release
uv run python benchmarks/scattering.py --require-release
uv run python benchmarks/structural_pattern.py --require-release
uv run python benchmarks/lattice_refinement.py --require-release
uv run python benchmarks/background.py --require-release
uv run python benchmarks/real_data.py --require-release
The joint multi-histogram PbSO4 workload is also executable entirely in Rust, without building or launching Python:
cargo run --release -p phasesmith-workflows --example joint_pbso4 -- \
validation/data/gsasii-pbso4-cw
Checksum-pinned validation also has a Python-free CLI. Successful runs write the same stable JSON report used by the scripting adapter:
cargo run -p phasesmith-validation --bin phasesmith-validation -- datasets
cargo run --release -p phasesmith-validation --bin phasesmith-validation -- \
run iucr-qarr-1g validation/data/iucr-qarr-1g
Ordinary phasesmith.validation calls delegate to these Rust workflows.
Python callbacks, custom execution policies, and explicit reference tests keep
the independent scripting path; the native CLI does not launch Python.
The real-data benchmark verifies pinned QARR 1g, APS sucrose, and official
PbSO4 X-ray/neutron tutorial inputs, records cold and warmed complete-workflow
timings, and hashes the timing-free scientific report. By default it compares
one- and two-thread QARR execution and runs both PbSO4 probes. Use --dataset,
--threads, and --json-output to select cases and retain a machine-readable
result. This is the PhaseSmith-only performance harness; the paired QARR and
PbSO4 GSAS-II commands below are cross-implementation scientific and
performance gates. The corresponding slow regression tests are opt-in:
uv run pytest -m real_data
The pinned external-oracle environment can compare the same support-limited CW profile-and-derivative workload against GSAS-II. A second benchmark starts from the crystal structure and compares structure factors, integrated intensities, and the composed structural CW pattern. Numerical agreement is checked before timings are reported:
uv run python benchmarks/compare_gsasii.py --require-release \
--gsas-python /path/to/gsas/python \
--gsas-root /path/to/pinned/GSAS-II \
--binary-dir /path/to/compatible/GSASII-bin/platform-directory
uv run python benchmarks/compare_gsasii_structural.py --require-release \
--gsas-python /path/to/gsas/python \
--gsas-root /path/to/pinned/GSAS-II \
--binary-dir /path/to/compatible/GSASII-bin/platform-directory
uv run python benchmarks/compare_gsasii_qarr.py --require-release \
--gsas-python /path/to/gsas/python \
--gsas-root /path/to/pinned/GSAS-II \
--binary-dir /path/to/compatible/GSASII-bin/platform-directory \
--data-directory validation/data/iucr-qarr-1g \
--phasesmith-threads 2
uv run python benchmarks/compare_gsasii_pbso4.py --require-release \
--gsas-python /path/to/gsas/python \
--gsas-root /path/to/pinned/GSAS-II \
--binary-dir /path/to/compatible/GSASII-bin/platform-directory \
--data-directory validation/data/gsasii-pbso4-cw
The exact scope and interpretation are documented in docs/gsasii-performance.md. This is a kernel-level comparison. The QARR and PbSO4 drivers separately compare complete native workflows and gate their reported scientific results before reporting speed. The established Python PbSO4 report includes the supplied reference cell alongside the per-probe staged fits and GSAS-II's joint refinement. The Rust example separately exercises PhaseSmith's first-class summed joint objective.
Background estimation is an explicit preprocessing step:
=
=
Set chebyshev_order=None to use the raw Bruckner envelope. Neither xypattern
nor Dioptas is required at runtime.
=
=
# Dense materialization is explicit and intended for small compatibility uses.
=
# (peak, parameter, sample)
Use profile_tch or accumulate_tch when Gaussian and Lorentzian component
FWHMs are the direct inputs. Explicit tch_shape_from_gaussian_sigma and
profile_tch_from_gaussian_sigma helpers are provided when Gaussian width is a
standard deviation; width conventions are never inferred.
An entire constant-wavelength reflection list, including all local and shared instrument derivatives, is also one array-oriented call:
=
=
# intensity, position
# U, V, W, X, Y
Atomic scattering models are also script-first and can be prepared once for a structure. X-ray species select exact neutral/ionic table states, while neutron species retain natural/isotope identity:
=
=
# (reflection, site)
# analytical df/ds, same shape
A typed structure can be evaluated without assembling scattering arrays or looping over atoms/reflections in Python. Correction geometry is explicit; the default neutral model returns raw multiplicity-weighted structural intensity:
= .
=
Use calculate_structure_factors when the bounded dense analytical structural
Jacobian is also required.
Axial divergence is a separate typed model and composes with CW broadening without expanding reflections in Python:
=
=
# U, V, W, X, Y, sample_over_radius, detector_over_radius
Discrete radiation components are optional. The ordinary CW calls above are monochromatic; a K-alpha doublet is an explicit model:
=
=
Sample physics is explicit and provider-based. Built-in and third-party models return the same vectorized contribution schema, and Python is never called from the native peak/sample loop:
=
=
=
The high-level script interface assigns durable IDs and calculates all phases through one flattened native call:
=
=
=
Neutron CW is an explicit monochromatic probe configuration and cannot receive an X-ray K-alpha doublet:
=
=
TOF reflections use d-spacing as their durable local coordinate. Values and all local/shared derivatives are accumulated in one native call:
=
= # bin centers, microseconds
=
# intensity, d_spacing
# 15 instrument rows
A complete Le Bail extraction uses the same typed pattern, instrument, and phase models and requires no project file or hand-written optimizer callback:
=
=
For a CIF-backed single phase, the convenience constructor creates the generated reflection domain and the symmetry-allowed bounded lattice parameter set directly from the observed grid:
=
=
A monochromatic structural refinement is likewise constructed directly from a CIF. Parameter families are explicit and no GSAS-II installation is involved:
=
=
Multiphase calculations and Rietveld refinement can use a bounded worker pool.
The bounded default is two threads. Embedding applications that already
schedule independent work can select one thread; choose another fixed budget
for predictable GUI/script behavior, or None to use the available logical
CPUs:
=
=
Independent phases execute concurrently, while their values and analytical derivatives are combined in the original phase order.
For long-running scripts and application integration, the small project facade
keeps restart state, cooperative stop control, persistence, and plain reports
together while leaving RietveldInput fully accessible:
=
=
=
=
=
# advice and rationale; no refinement has run yet
=
=
# Another thread or a callback can stop safely; the accepted state is resumable.
The intelligent path is optional workflow orchestration. project.refine()
continues to call the general solver once with exactly the caller-selected
parameters, and project.refine_recipe(recipe) runs an explicit user-defined
sequence.
Refinable backgrounds share one analytical interface. Built-ins include power and Chebyshev series, fixed-knot linear interpolation, broad normalized Gaussian amorphous components, and ordered composites. Smooth Bruckner remains an explicit preprocessing operation and is never inserted into refinement automatically.
GSAS-II is used only as the optional pinned validation oracle described in
oracle/README.md.
License
PhaseSmith is licensed under the MIT License. GSAS-II is separately licensed, is used only as an optional external validation oracle, and is not redistributed here.