---
title: Crystal structure and diffraction lab
heading: Building lattices, computing structure factors and powder patterns, and fitting a lattice constant, in Python
eyebrow: B12 · Lesson 5 · Crystal structure lab
xp: 50
---

::: hook
Lessons 1 to 4 described crystals by their cells, planes and diffraction peaks, and worked a few cases by hand. Real crystallography is computation: generate the atoms, sum their scattered waves to see which reflections survive, list where the peaks fall, and fit a lattice constant to measured positions. In this lab you'll build those tools for the common cubic structures, then use them on silicon and on a silicon–germanium layer, the way a fab's X-ray tool checks a wafer.
:::

::: play
- The curves are the scattering angles 2θ of silicon's first four allowed reflections, (111), (220), (311) and (400), against the X-ray wavelength.
- Bragg's law, $\lambda = 2d\sin\theta$, pushes every peak to larger angles as the wavelength grows; the marker sits at copper Kα, 0.15406 nm.
- The lattice-constant slider shows how a strained or alloyed layer's peaks shift. The readouts give the peaks at copper Kα.
:::

```sim plot
view: graph
x: l = 0.05..0.3 nm
y: t = 0..180 °
slider: a = 0.53..0.58 step 0.0005 = 0.5431 nm
plot: 2*asin(l*sqrt(3)/(2*a))*180/pi | label: (111) | samples: 300
plot: 2*asin(l*sqrt(8)/(2*a))*180/pi | color: ch2 | label: (220) | samples: 300
plot: 2*asin(l*sqrt(11)/(2*a))*180/pi | color: ch3 | label: (311) | samples: 300
plot: 2*asin(l*sqrt(16)/(2*a))*180/pi | color: ch4 | label: (400) | samples: 300
marker: l = 0.15406 | label: copper Kα
readout: (111) at Cu Kα, ° = 2*asin(0.15406*sqrt(3)/(2*a))*180/pi | digits: 4
readout: (400) at Cu Kα, ° = 2*asin(0.15406*sqrt(16)/(2*a))*180/pi | digits: 4
```

::: predict
## Silicon has the diamond structure: an fcc lattice with two atoms per lattice point. Copper is plain fcc. Which reflection appears for copper but is missing for silicon?

- [ ] (111)
- [x] (200)
- [ ] (220)
- [ ] None: both show the same peaks

::: reveal
(200). In fcc, reflections appear when h, k and l are all odd or all even, so copper shows (111), (200), (220) and more. Silicon's second atom, shifted by a quarter of the body diagonal, scatters (200) exactly out of phase with the first, so the two cancel. Step 2 computes this from the atoms' positions.
:::
:::

::: nameit
## Lattices and bases

A crystal is a lattice plus a basis. In fractional coordinates of the cubic cell: simple cubic has one atom at (0, 0, 0); bcc adds (½, ½, ½); fcc adds the three face centers; diamond repeats the fcc set shifted by (¼, ¼, ¼), for 8 atoms per cell. Nearest-neighbor distances come from searching all atoms in the cell and its neighbors for the shortest separation.

## The structure factor

Each atom scatters with a phase set by its position. For reflection (hkl), the cell's waves add to

$$F_{hkl} = \sum_jf_je^{2\pi i(hx_j + ky_j + lz_j)},$$

and the peak's intensity goes as $|F|^2$. With identical atoms ($f_j$ = 1), fcc gives F = 4 when h, k, l are unmixed and 0 otherwise; diamond further cancels the even reflections with h + k + l = 4n + 2, such as (200) and (222).

## A powder pattern

Every allowed reflection with $s = h^2 + k^2 + l^2$ has spacing $d = \frac{a}{\sqrt s}$ and appears at $2\theta = 2\arcsin\frac{\lambda\sqrt s}{2a}$, until $\frac{\lambda\sqrt s}{2a}$ exceeds 1.

## Fitting a lattice constant

Squaring Bragg's law gives $\sin^2\theta = \frac{\lambda^2}{4a^2}s$, linear in s. A least-squares fit of $\sin^2\theta$ against $\frac{\lambda^2s}{4}$ through the origin gives a slope $c = \frac{\sum xy}{\sum x^2}$ and $a = \frac{1}{\sqrt c}$, using every measured peak at once.
:::

::: example
## Where do silicon's first five allowed reflections fall with copper Kα (λ = 0.15406 nm, a = 0.5431 nm)?

::: solution
1. **The allowed reflections.** Diamond's allowed reflections, in order of s, are (111), (220), (311), (400) and (331), with s = 3, 8, 11, 16 and 19.
2. **Bragg's law.** Each angle comes from
   $$2\theta = 2\arcsin\frac{\lambda\sqrt s}{2a}$$
3. **The five angles.**
   $$\begin{aligned} 2\theta_{111} &= 28.44° \\ 2\theta_{220} &= 47.30° \\ 2\theta_{311} &= 56.12° \\ 2\theta_{400} &= 69.13° \\ 2\theta_{331} &= 76.38° \end{aligned}$$
4. **The fingerprint.** Copper's fcc pattern would also show (200) and (222); their absence is the fingerprint of the diamond structure.

**Answer:** the first five reflections fall at 2θ = 28.44°, 47.30°, 56.12°, 69.13° and 76.38°.
:::
:::

::: example
## Why does silicon's (200) reflection vanish? Compute $F_{200}$ for the diamond basis.

::: solution
1. **The fcc atoms.** Each of the four fcc atoms contributes $e^{2\pi i \cdot 2x}$, and 2x is 0 or 1 for all of them, so the fcc part is 4.
2. **The shifted atoms.** The four shifted atoms each pick up an extra phase
   $$\begin{aligned} e^{2\pi i(2 \times \frac14)} &= e^{i\pi} \\ &= -1 \end{aligned}$$
3. **Add the two parts.**
   $$\begin{aligned} F_{200} &= 4 + 4 \times (-1) \\ &= 0 \end{aligned}$$
4. **The (111) reflection.** Here the shift's phase is $e^{2\pi i \cdot \frac34} = -i$, giving
   $$\begin{aligned} |F| &= |4 - 4i| \\ &= 4\sqrt2 \\ &= 5.66 \end{aligned}$$
5. **The (220) reflection.** Here it's $e^{2\pi i} = 1$, giving 8.
6. **Weak forbidden peaks.** Weak forbidden peaks do sometimes appear, from the bonding charge between atoms, which isn't spherical.

**Answer:** the shifted atoms' phase of −1 cancels the fcc part exactly, so $F_{200} = 0$ and the reflection vanishes.
:::
:::

::: example
## A relaxed $\text{Si}_{0.8}\text{Ge}_{0.2}$ layer follows Vegard's law between silicon (0.5431 nm) and germanium (0.5658 nm). Where's its (400) peak, and how far from silicon's?

::: solution
1. **Lattice constant.**
   $$\begin{aligned} a &= 0.5431 + 0.2 \times 0.0227 \\ &= 0.54764\ \text{nm} \end{aligned}$$
2. **Peak position.**
   $$\begin{aligned} 2\theta_{400} &= 2\arcsin\frac{0.15406 \times 4}{2 \times 0.54764} \\ &= 68.48° \end{aligned}$$
3. **Compare with silicon.** The silicon substrate's peak is at 69.13°, so the layer's is 0.65° lower, easily resolved.
4. **What a diffractometer reads.** A high-resolution diffractometer reads the layer's composition (and, from the peaks' widths and asymmetric reflections, its strain) from shifts like this.

**Answer:** the (400) peak falls at 2θ = 68.48°, 0.65° below silicon's 69.13°.
:::
:::

::: check
kind: python

## Step 1. Write `basis(kind)` returning the fractional positions for `'sc'`, `'bcc'`, `'fcc'` or `'diamond'`, and `nearest(kind, a)` returning the nearest-neighbor distance, searching the cell and its 26 neighboring copies.

```python starter
def basis(kind):
    return [(0.0, 0.0, 0.0)]

def nearest(kind, a):
    return a

print(len(basis('diamond')), nearest('diamond', 0.5431))
```

```python solution
import itertools, math

def basis(kind):
    sc = [(0.0, 0.0, 0.0)]
    if kind == 'sc':
        return sc
    if kind == 'bcc':
        return sc + [(0.5, 0.5, 0.5)]
    fcc = sc + [(0.0, 0.5, 0.5), (0.5, 0.0, 0.5), (0.5, 0.5, 0.0)]
    if kind == 'fcc':
        return fcc
    return fcc + [(x + 0.25, y + 0.25, z + 0.25) for x, y, z in fcc]

def nearest(kind, a):
    b = basis(kind)
    best = math.inf
    for p in b:
        for q in b:
            for n in itertools.product((-1, 0, 1), repeat=3):
                d = math.dist(p, (q[0] + n[0], q[1] + n[1], q[2] + n[2]))
                if d > 1e-9:
                    best = min(best, d)
    return best * a

print(len(basis('diamond')), nearest('diamond', 0.5431))
```

```python tests
assert [len(basis(k)) for k in ('sc', 'bcc', 'fcc', 'diamond')] == [1, 2, 4, 8], "Cells hold 1, 2, 4 and 8 atoms."
assert abs(nearest('diamond', 0.5431) - 0.23517) < 1e-4, f"Silicon's bond is a√3/4 = 0.2352 nm; got {nearest('diamond', 0.5431)}"
assert abs(nearest('fcc', 0.3615) - 0.25562) < 1e-4, "Copper's nearest neighbors are a/√2 apart."
assert abs(nearest('bcc', 0.2866) - 0.24820) < 1e-4, "Iron's are a√3/2 apart."
```

::: right
Right. Searching neighboring cells matters: some nearest neighbors sit across the cell boundary.
:::
:::

::: check
kind: python

## Step 2. Write `structure_factor(kind, h, k, l)` returning $|F_{hkl}|$ for identical atoms on your basis.

```python starter
def structure_factor(kind, h, k, l):
    return 1.0

print(structure_factor('diamond', 2, 0, 0))
```

```python solution
import cmath, math

def basis(kind):
    sc = [(0.0, 0.0, 0.0)]
    if kind == 'sc':
        return sc
    if kind == 'bcc':
        return sc + [(0.5, 0.5, 0.5)]
    fcc = sc + [(0.0, 0.5, 0.5), (0.5, 0.0, 0.5), (0.5, 0.5, 0.0)]
    if kind == 'fcc':
        return fcc
    return fcc + [(x + 0.25, y + 0.25, z + 0.25) for x, y, z in fcc]

def structure_factor(kind, h, k, l):
    return abs(sum(cmath.exp(2j * math.pi * (h * x + k * y + l * z)) for x, y, z in basis(kind)))

print(structure_factor('diamond', 2, 0, 0))
```

```python tests
import math
assert structure_factor('diamond', 2, 0, 0) < 1e-9 and structure_factor('diamond', 2, 2, 2) < 1e-9, "(200) and (222) vanish in diamond."
assert abs(structure_factor('diamond', 1, 1, 1) - 4 * math.sqrt(2)) < 1e-9, "(111) has |F| = 4√2."
assert abs(structure_factor('diamond', 2, 2, 0) - 8) < 1e-9, "(220) has |F| = 8."
assert abs(structure_factor('fcc', 2, 0, 0) - 4) < 1e-9 and structure_factor('fcc', 1, 0, 0) < 1e-9, "fcc allows (200) but not (100)."
assert structure_factor('bcc', 1, 0, 0) < 1e-9 and abs(structure_factor('bcc', 1, 1, 0) - 2) < 1e-9, "bcc needs h + k + l even."
```

::: right
Right. The selection rules all come from one sum over the basis.
:::
:::

::: check
kind: python

## Step 3. Write `powder_peaks(kind, a, lam, smax)` returning a sorted list of `(two_theta_degrees, s)` for every distinct $s = h^2 + k^2 + l^2 \le$ `smax` that has an allowed reflection and a real angle.

```python starter
def powder_peaks(kind, a, lam, smax):
    return []

print(powder_peaks('diamond', 0.5431, 0.15406, 20)[:3])
```

```python solution
import cmath, itertools, math

def basis(kind):
    sc = [(0.0, 0.0, 0.0)]
    if kind == 'sc':
        return sc
    if kind == 'bcc':
        return sc + [(0.5, 0.5, 0.5)]
    fcc = sc + [(0.0, 0.5, 0.5), (0.5, 0.0, 0.5), (0.5, 0.5, 0.0)]
    if kind == 'fcc':
        return fcc
    return fcc + [(x + 0.25, y + 0.25, z + 0.25) for x, y, z in fcc]

def F(kind, h, k, l):
    return abs(sum(cmath.exp(2j * math.pi * (h * x + k * y + l * z)) for x, y, z in basis(kind)))

def powder_peaks(kind, a, lam, smax):
    n = int(math.isqrt(smax)) + 1
    found = set()
    for h, k, l in itertools.product(range(n + 1), repeat=3):
        s = h * h + k * k + l * l
        if 0 < s <= smax and s not in found and F(kind, h, k, l) > 1e-6:
            x = lam * math.sqrt(s) / (2 * a)
            if x < 1:
                found.add(s)
    return sorted((2 * math.degrees(math.asin(lam * math.sqrt(s) / (2 * a))), s) for s in found)

print(powder_peaks('diamond', 0.5431, 0.15406, 20)[:3])
```

```python tests
p = powder_peaks('diamond', 0.5431, 0.15406, 20)
assert [s for _, s in p] == [3, 8, 11, 16, 19], f"Silicon's first peaks have s = 3, 8, 11, 16, 19; got {[s for _, s in p]}"
assert abs(p[0][0] - 28.442) < 0.01 and abs(p[1][0] - 47.302) < 0.01 and abs(p[2][0] - 56.122) < 0.01, "Peaks at 28.44°, 47.30° and 56.12°."
assert [s for _, s in powder_peaks('fcc', 0.3615, 0.15406, 12)] == [3, 4, 8, 11, 12], "Copper shows (111), (200), (220), (311), (222)."
```

::: right
Right. The list of allowed s values alone identifies the structure, before any intensities.
:::
:::

::: check
kind: python

## Step 4. Write `fit_lattice(peaks, lam)` that takes measured `(two_theta_degrees, s)` pairs and returns the lattice constant from a least-squares fit of $\sin^2\theta$ against $\frac{\lambda^2s}{4}$ through the origin.

```python starter
def fit_lattice(peaks, lam):
    return 0.5

data = [(28.45, 3), (47.29, 8), (56.13, 11), (69.12, 16)]
print(fit_lattice(data, 0.15406))
```

```python solution
import math

def fit_lattice(peaks, lam):
    xs = [lam * lam * s / 4 for _, s in peaks]
    ys = [math.sin(math.radians(t / 2)) ** 2 for t, _ in peaks]
    c = sum(x * y for x, y in zip(xs, ys)) / sum(x * x for x in xs)
    return 1 / math.sqrt(c)

data = [(28.45, 3), (47.29, 8), (56.13, 11), (69.12, 16)]
print(fit_lattice(data, 0.15406))
```

```python tests
import math, random
data = [(28.45, 3), (47.29, 8), (56.13, 11), (69.12, 16)]
assert abs(fit_lattice(data, 0.15406) - 0.5431) < 2e-4, f"These peaks give silicon's 0.5431 nm; got {fit_lattice(data, 0.15406)}"
rng = random.Random(3)
exact = [(2 * math.degrees(math.asin(0.15406 * math.sqrt(s) / (2 * 0.5658))), s) for s in (3, 8, 11, 16, 19, 24)]
noisy = [(t + rng.gauss(0, 0.01), s) for t, s in exact]
assert abs(fit_lattice(noisy, 0.15406) - 0.5658) < 2e-4, "Noisy germanium peaks should still give 0.5658 nm."
```

::: right
Right. High-angle peaks pin a down best, since sin θ changes fastest there for a given shift in a.
:::
:::

::: check
kind: python

## Step 5. Write `sige_peak(x, lam)` returning the (400) scattering angle in degrees for relaxed $\text{Si}_{1-x}\text{Ge}_x$ by Vegard's law (silicon 0.5431, germanium 0.5658 nm), and `ge_fraction(two_theta, lam)` inverting it.

```python starter
def sige_peak(x, lam):
    return 69.13

def ge_fraction(two_theta, lam):
    return 0.0

print(sige_peak(0.2, 0.15406))
```

```python solution
import math

def sige_peak(x, lam):
    a = 0.5431 + x * (0.5658 - 0.5431)
    return 2 * math.degrees(math.asin(lam * 4 / (2 * a)))

def ge_fraction(two_theta, lam):
    a = lam * 4 / (2 * math.sin(math.radians(two_theta / 2)))
    return (a - 0.5431) / (0.5658 - 0.5431)

print(sige_peak(0.2, 0.15406))
```

```python tests
assert abs(sige_peak(0.0, 0.15406) - 69.1296) < 1e-3, "Silicon's (400) is at 69.13°."
assert abs(sige_peak(0.2, 0.15406) - 68.4761) < 1e-3, f"20% germanium moves it to 68.48°; got {sige_peak(0.2, 0.15406)}"
assert abs(ge_fraction(sige_peak(0.37, 0.15406), 0.15406) - 0.37) < 1e-9, "ge_fraction must invert sige_peak."
```

::: right
Right. A 0.65° shift for 20% germanium is easy to measure, so X-ray diffraction is the standard check on SiGe layers.
:::
:::

::: check
kind: parts
given: N = 4 | 8; M = 28.09 | 63.55 | 72.63; a = 0.3615 | 0.5431 | 0.5658

## A cubic crystal has {N} atoms per cell of average molar mass {M} g/mol and lattice constant {a} nm.

::: part
answer: N/(a*1e-7)^3/1e22
tolerance: 0.5%

### How many atoms per cm³, in units of 10²²?

::: misses
- [any] $\frac{N}{a^3}$.
:::
:::

::: part
answer: N*M/(6.02214076e23*(a*1e-7)^3)
unit: g/cm³
tolerance: 0.5%

### What's its density?

::: misses
- [any] $\rho = \frac{NM}{N_Aa^3}$.
:::
:::
:::

::: check
given: s = 3 | 8 | 11 | 16; a = 0.5431 | 0.5658; l = 0.15406 | 0.07093
answer: 2*asin(l*sqrt(s)/(2*a))*180/pi
unit: °
tolerance: 0.1%

## A cubic crystal with a = {a} nm is measured with {l} nm X-rays. Where does the reflection with $h^2 + k^2 + l^2$ = {s} fall?

::: right
Right. $2\theta = 2\arcsin\frac{\lambda\sqrt s}{2a}$.
:::

::: misses
- [asin(l*sqrt(s)/(2*a))*180/pi] That's θ; the detector angle is 2θ.
- [any] $d = \frac{a}{\sqrt s}$, then Bragg.
:::
:::

::: check
given: t = 28.44 | 47.3 | 69.13; s = 3 | 8 | 16
answer: 0.15406*sqrt(s)/(2*sin(t/2*pi/180))
unit: nm
tolerance: 0.1%

## A copper Kα (0.15406 nm) peak at 2θ = {t}° is indexed with $h^2 + k^2 + l^2$ = {s}. What lattice constant does it imply?

::: right
Right. $a = \frac{\lambda\sqrt s}{2\sin\theta}$.
:::

::: misses
- [0.15406*sqrt(s)/(2*sin(t*pi/180))] Use θ, half of 2θ.
- [any] Invert Bragg's law.
:::
:::

::: check
given: x = 0.1 | 0.25 | 0.4
answer: 2*asin(0.15406*4/(2*(0.5431 + x*0.0227)))*180/pi
unit: °
tolerance: 0.05%

## Where does the (400) peak of relaxed $\text{Si}_{1-x}\text{Ge}_x$ with x = {x} fall, with copper Kα?

::: right
Right. Vegard's law for a, then Bragg's law with $d = a/4$.
:::

::: misses
- [2*asin(0.15406*2/(2*(0.5431 + x*0.0227)))*180/pi] For (400), $d = \frac{a}{4}$.
- [any] $a = 0.5431 + 0.0227x$.
:::
:::

::: explain
## Why do some reflections vanish completely even though planes with those indices exist in the crystal?

::: model
Every set of lattice planes reflects, but within each cell there may be several atoms, and the waves they scatter arrive with different phases. For some reflections the atoms' phases cancel exactly. In diamond, the second fcc sublattice is shifted by a quarter of the body diagonal, which for (200) adds a phase of π to every one of its atoms, so its four atoms cancel the other four. The planes are there, but halfway between each pair sits an equally dense plane of the other sublattice that scatters exactly out of phase. The pattern of missing reflections is what tells crystallographers which basis a lattice carries.
:::
:::

::: practice

::: check
given: s = 3 | 11; a = 0.5431 | 0.5658
answer: 2*asin(0.15406*sqrt(s)/(2*a))*180/pi
unit: °
tolerance: 0.1%

## 2θ with Cu Kα for s = {s}, a = {a} nm?

::: misses
- [any] $2\arcsin\frac{\lambda\sqrt s}{2a}$.
:::
:::

::: check
given: t = 33 | 56.12; s = 4 | 11
answer: 0.15406*sqrt(s)/(2*sin(t/2*pi/180))
unit: nm
tolerance: 0.1%

## Lattice constant from a Cu Kα peak at {t}° with s = {s}?

::: misses
- [any] $\frac{\lambda\sqrt s}{2\sin\theta}$.
:::
:::
:::
