Below is an updated version of the Python code. I've extended the previous program to include 3D plotting of the reciprocal lattice points using matplotlib. Here's what I've added/changed:
Key Additions:
- Generating Reciprocal Lattice Points: A new function
generate_reciprocal_points creates a finite set of reciprocal lattice points by combining integer multiples (h, k, l) of the reciprocal basis vectors (b1, b2, b3). I loop over h, k, l from -n to n (default n=2 for a small, manageable plot; you can adjust this).
- Plotting: A new function
plot_reciprocal_lattice uses Matplotlib's 3D scatter plot to visualize these points in reciprocal space (axes labeled as kx, ky, kz).
- Integration: The plotting happens automatically after calculating the reciprocal vectors, but only if the input is valid (atoms = 1, 2, or 4).
- Dependencies: Added
matplotlib import. Install it if needed: pip install matplotlib.
The code remains modular with small functions and loops for clarity.
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
def get_real_lattice_vectors(a, b, c, atoms):
"""
Returns the real lattice vectors based on the number of atoms.
"""
if atoms == 1:
# Simple Cubic (SC) - allowing a, b, c for generality (orthorhombic)
a1 = np.array([a, 0, 0])
a2 = np.array([0, b, 0])
a3 = np.array([0, 0, c])
elif atoms == 2:
# Body-Centered Cubic (BCC) - assumes cubic, uses 'a' (ignores b, c for simplicity)
a1 = (a / 2) * np.array([-1, 1, 1])
a2 = (a / 2) * np.array([1, -1, 1])
a3 = (a / 2) * np.array([1, 1, -1])
elif atoms == 4:
# Face-Centered Cubic (FCC) - assumes cubic, uses 'a' (ignores b, c for simplicity)
a1 = (a / 2) * np.array([0, 1, 1])
a2 = (a / 2) * np.array([1, 0, 1])
a3 = (a / 2) * np.array([1, 1, 0])
else:
return None, None, None
return a1, a2, a3
def calculate_reciprocal_lattice(a1, a2, a3):
"""
Calculate reciprocal lattice vectors b1, b2, b3 from real lattice vectors a1, a2, a3.
Formula:
b1 = 2*pi * (a2 x a3) / (a1 . (a2 x a3))
b2 = 2*pi * (a3 x a1) / (a1 . (a2 x a3))
b3 = 2*pi * (a1 x a2) / (a1 . (a2 x a3))
"""
volume = np.dot(a1, np.cross(a2, a3))
if volume == 0:
raise ValueError("The volume of the unit cell is zero, invalid lattice vectors.")
b1 = 2 * np.pi * np.cross(a2, a3) / volume
b2 = 2 * np.pi * np.cross(a3, a1) / volume
b3 = 2 * np.pi * np.cross(a1, a2) / volume
return b1, b2, b3
def generate_reciprocal_points(b1, b2, b3, n=2):
"""
Generate a finite set of reciprocal lattice points: G = h*b1 + k*b2 + l*b3
for h, k, l in [-n, n].
"""
points = []
for h in range(-n, n + 1):
for k in range(-n, n + 1):
for l in range(-n, n + 1):
G = h * b1 + k * b2 + l * b3
points.append(G)
return np.array(points)
def print_vectors(vectors, name):
"""
Nicely print the lattice vectors.
"""
for i, vec in enumerate(vectors, start=1):
print(f"{name}_{i} = [{vec[0]:.4f}, {vec[1]:.4f}, {vec[2]:.4f}]")
def plot_reciprocal_lattice(points):
"""
Plot the reciprocal lattice points in 3D.
"""
fig = plt.figure(figsize=(8, 6))
ax = fig.add_subplot(111, projection='3d')
ax.scatter(points[:, 0], points[:, 1], points[:, 2], color='blue', s=50)
ax.set_xlabel('kx (1/Å)')
ax.set_ylabel('ky (1/Å)')
ax.set_zlabel('kz (1/Å)')
ax.set_title('Reciprocal Lattice Points')
plt.show()
def main():
print("Reciprocal Lattice Calculator and Plotter for SC, BCC, FCC")
a = float(input("Enter lattice parameter a: "))
b = float(input("Enter lattice parameter b (ignored for BCC/FCC): "))
c = float(input("Enter lattice parameter c (ignored for BCC/FCC): "))
atoms = int(input("Enter number of atoms in the unit cell (1 for SC, 2 for BCC, 4 for FCC): "))
n = int(input("Enter range for h,k,l (e.g., 2 for -2 to 2, larger for more points): "))
a1, a2, a3 = get_real_lattice_vectors(a, b, c, atoms)
if a1 is None:
print("This program is insufficient to calculate this lattice.")
return
print("\nReal lattice vectors:")
print_vectors([a1, a2, a3], "a")
b1, b2, b3 = calculate_reciprocal_lattice(a1, a2, a3)
print("\nReciprocal lattice vectors:")
print_vectors([b1, b2, b3], "b")
points = generate_reciprocal_points(b1, b2, b3, n)
print(f"\nGenerated {len(points)} reciprocal lattice points.")
plot_reciprocal_lattice(points)
if __name__ == "__main__":
main()
How It Works:
- Input: Same as before, plus a new input for
n (the range for h, k, l). Larger n gives more points but can clutter the plot.
- Reciprocal Points Generation: Uses nested loops to compute G vectors. For n=2, it generates (2*2+1)^3 = 125 points.
- Plotting: A 3D scatter plot appears after calculations. Points are blue dots. Axes are labeled in reciprocal space units (assuming lattice parameters in Å, so reciprocal in 1/Å).
- Notes:
- For BCC and FCC, the code assumes cubic symmetry and uses only
a (ignores b and c for those cases, as per standard definitions). For SC, it uses a, b, c allowing orthorhombic generality.
- If the plot looks empty or points overlap (e.g., due to symmetry), try adjusting
n or zooming in the plot window.
- The reciprocal lattice is infinite, but we plot a finite subset for visualization.
Example Usage:
Reciprocal Lattice Calculator and Plotter for SC, BCC, FCC
Enter lattice parameter a: 1
Enter lattice parameter b (ignored for BCC/FCC): 1
Enter lattice parameter c (ignored for BCC/FCC): 1
Enter number of atoms in the unit cell (1 for SC, 2 for BCC, 4 for FCC): 2
Enter range for h,k,l (e.g., 2 for -2 to 2, larger for more points): 2
Real lattice vectors:
a_1 = [-0.5000, 0.5000, 0.5000]
a_2 = [0.5000, -0.5000, 0.5000]
a_3 = [0.5000, 0.5000, -0.5000]
Reciprocal lattice vectors:
b_1 = [0.0000, 6.2832, 6.2832]
b_2 = [6.2832, 0.0000, 6.2832]
b_3 = [6.2832, 6.2832, 0.0000]
Generated 125 reciprocal lattice points.
- A 3D plot window will pop up showing the points (you can rotate it interactively).
If you need customizations (e.g., colors, more points, highlighting the first Brillouin zone, or saving the plot to a file), let me know!