Example 3: Phonon Calculation¶
This example demonstrates phonon calculations and thermal property analysis.
Source Code¶
See examples/03_phonon_calculation.py
What You'll Learn¶
- Calculating phonon frequencies
- Checking dynamical stability
- Computing thermal properties
- Plotting phonon DOS
Code Walkthrough¶
Setup¶
from mace_inference import MACEInference
from ase.io import read
calc = MACEInference(model="medium", device="auto")
atoms = read("structures/si_diamond.cif")
Optimize Unit Cell¶
Phonon calculations require well-relaxed structures:
Calculate Phonons¶
result = calc.phonon(
atoms,
supercell_matrix=[2, 2, 2], # Supercell for force constants
displacement=0.01, # Displacement in Å
temperature_range=(0, 500, 10) # For thermal properties
)
Check Stability¶
frequencies = result['frequencies']
imaginary = frequencies[frequencies < -0.1] # Threshold for numerical noise
if len(imaginary) > 0:
print("⚠️ Structure is dynamically unstable!")
print(f"Imaginary modes: {imaginary}")
else:
print("✓ Structure is dynamically stable")
Thermal Properties¶
# Thermal properties available if temperature_range was specified
if 'thermal' in result:
thermal = result['thermal']
print(f"Thermal properties calculated for {len(thermal['temperatures'])} temperatures")
Plot DOS¶
import matplotlib.pyplot as plt
dos = result['dos']
plt.figure(figsize=(8, 5))
plt.fill_between(dos['frequencies'], dos['total_dos'], alpha=0.5)
plt.plot(dos['frequencies'], dos['total_dos'])
plt.xlabel("Frequency (THz)")
plt.ylabel("DOS (states/THz)")
plt.title("Phonon Density of States")
plt.axvline(0, color='k', linestyle='--', lw=0.5)
plt.savefig("phonon_dos.png", dpi=150)
Expected Output¶
============================================================
Example 3: Phonon Calculation
============================================================
1. Loading structure: Si8
2. Optimizing structure...
Converged in 1 steps
3. Calculating phonons...
Supercell: (2, 2, 2) = 64 atoms
Displacements: 6
✓ Force constants calculated
4. Analyzing results...
Gamma-point frequencies (THz):
0.00, 0.00, 0.00 (acoustic)
12.45, 12.45, 12.45
15.23, 15.23, 15.23
...
✓ No imaginary frequencies - structure is stable
5. Thermal properties at 300 K:
Free energy: 0.2341 eV
Entropy: 0.4521 meV/K
Heat capacity: 0.3892 meV/K
✅ Example 3 completed successfully!
Key Points¶
- Supercell Size: Larger supercells give more accurate results but are slower
- Optimization: Tight force tolerance (< 0.001 eV/Å) is important
- Imaginary Modes: Small negatives near zero are numerical noise; large negatives indicate instability
- Units: Frequencies in THz, thermal properties per atom