Crystallography & XRD Refinement Demo

A live, interactive lesson in the two things that matter most in powder diffraction: how a crystal structure turns into an XRD pattern, and how that pattern is refined back into structural parameters. Nothing here is pre-rendered — the lattice, the diffraction peaks and the refinement statistics are recomputed in your browser as you move the sliders, so you can watch every parameter act on the result.

Open the demo in a new tab

The frame above is the real thing, not a recording. Switch between the two modes with the tabs, drag the 3D lattice to rotate it, and hover a diffraction peak to read off the Miller index that produced it.

At a glance

FeatureWhat you can do with it
Crystal structuresSimple cubic, body-centred cubic and face-centred cubic, rendered live in 3D
Instrument parametersLattice parameter a (2.0–8.0 Å), wavelength λ (0.1–2.5 Å), peak width (FWHM)
Live XRD patternPeaks computed from Bragg’s law, with multiplicities, the Lorentz–polarization factor and the structure factor
Miller indicesHover any peak to read off the (hkl) that produced it
RefinementImport a simulated pattern, choose a structural model, refine manually or automatically
OptimisersHill climbing, simulated annealing and gradient descent — three genuinely different search strategies
StatisticsRwp, Rp and χ² update live while the refinement runs
InterfaceChinese and English, switchable at any time

Mode 1 — Structure and XRD simulation

The left panel holds the parameters; the centre holds the answer. Pick a structure and the 3D lattice redraws immediately. Change a and the peaks slide; change λ and everything moves to higher or lower angles.

  • 3D crystal structure — simple cubic (SC), body-centred cubic (BCC) and face-centred cubic (FCC) structures rendered live, and you can rotate them with the mouse.
  • Interactive parameters — lattice parameter a (2.0–8.0 Å), X-ray wavelength λ (0.1–2.5 Å) and peak width (FWHM).
  • Live XRD pattern — diffraction peaks computed from Bragg’s law and the extinction rules, with the intensity built up from multiplicity, the Lorentz–polarization factor and the structure factor.
  • Miller index hints — hover a peak to read off its (hkl).
  • Bilingual interface — Chinese / English.

What to take away

  • How the crystal structure maps onto the position of the diffraction peaks.
  • Bragg’s law, nλ = 2d sin θ, and why d depends on the cell parameter.
  • The extinction rules that remove peaks for different space groups.
  • How the cell parameter and the wavelength shift the whole pattern.

Mode 2 — Principles of XRD refinement

This mode takes the pattern you just produced, corrupts it the way a real measurement would be corrupted, and asks how you would get the answer back.

  • Data import — noisy “experimental” data generated by the simulation mode: a slowly varying background plus Poisson counting statistics.
  • Model selection — fit with a different crystal structure model than the true one and watch the fit degrade.
  • Manual refinement — slide the cell parameter and watch the residual shrink.
  • Automatic refinement — three genuine optimisation algorithms: hill climbing, simulated annealing and gradient descent.
  • Live statistics — Rwp (weighted profile residual), Rp (profile residual) and χ² (goodness of fit).
  • Standard output — a Rietveld-style plot with observed, calculated and difference curves.

What to take away

  • What a refinement actually minimises, and how it is evaluated.
  • The meaning of the Rietveld statistics, and why Rwp is always the larger of the two.
  • How the three optimisation algorithms behave differently — hill climbing stalls in local minima, annealing escapes them, gradient descent converges fast but needs a good starting point.
  • How the choice of model affects the fit, and why the wrong structure never quite fits.

The physics

Bragg law:  nλ = 2d sinθ
  n  diffraction order (integer)
  λ  X-ray wavelength (Å)
  d  spacing of the reflecting planes
  θ  Bragg angle

Plane spacing:  d = a / √(h² + k² + l²)

The extinction rules follow directly from the structure factor:

  • SC — every (hkl) diffracts.
  • BCC — h + k + l even.
  • FCC — h, k, l all odd or all even.

Peak intensities account for multiplicity, the Lorentz–polarization factor and the structure factor. Noise is added as a slowly varying background plus Poisson counting statistics, so the refinement is exercised on realistic data rather than on a clean synthetic curve.

Using the demo

  1. In mode 1, change the structure and the instrument parameters and follow the 3D lattice and the pattern together.
  2. Compare SC, BCC and FCC for the same a and λ, and note which reflections disappear.
  3. Switch to mode 2, press Import simulated data, pick a model, then refine manually or automatically and watch Rwp, Rp and χ² respond.
  4. Run all three algorithms on the same data and compare how many evaluations each one needs.
  5. Hover the symbols in the formulas to read their definitions.

Technical notes

Vue.js 2.6, Three.js and Plotly.js are loaded from a CDN; all physical calculations and the optimisation algorithms run in the browser. The 3D view keeps its aspect ratio in step with the frame, so the demo stays legible whether it is opened full size or embedded in this page.

Teaching use

Materials science courses, XRD training, undergraduate crystallography, and self-study of refinement methods. A natural sequence is to work through mode 1 first, then use mode 2 to compare the three algorithms and build intuition about how they behave.