A Python project for representing and visualizing crystal structures using the notation
M = (X, A, L) → atomic coordinates, atom types, and lattice.
crystal_code helps students and researchers understand how crystals are built from three fundamental components:
- X: Fractional coordinates of atoms within the unit cell
- A: Atom types (chemical species)
- L: 3×3 lattice vectors defining the crystal shape
Together, these components define any crystalline material.
from crystal_code.crystal import Crystal
# Define atomic positions (fractional coordinates)
X = [[0, 0, 0], [0.5, 0.5, 0.5]]
# Define atom types
A = ["Na", "Cl"]
# Define cubic lattice vectors (in Å)
L = [
[5.64, 0.0, 0.0],
[0.0, 5.64, 0.0],
[0.0, 0.0, 5.64]
]
# Create the crystal structure object
M = Crystal(X, A, L)
# Save the structure to a CIF file
M.save_cif("NaCl.cif")This code generates a simple NaCl (rock-salt) crystal structure that can be viewed using ASE or VESTA.
git clone https://github.com/Andrew-Aniagboso/crystal_code.git
cd crystal_code
pip install -r requirements.txtcase1: Creating and saving a crystal structure to a CIF file
Import the Crystal class and create crystal structures by providing atomic coordinates, atom types, and
lattice vectors. Use the save_cif method to export the structure to a CIF file.
Refer to the example above for guidance.
case2: Running the example notebook Run the example code to generate a CIF file for the NaCl crystal structure.Once installed, open the example notebook:
how to run:
jupyter notebook notebooks/demo.ipynbOr run directly in PyCharm using the integrated Jupyter support.
case1: Visualizing CIF files You can visualize the generated CIF files using tools like ASE or VESTA. Simply open the CIF file in your preferred visualization software to explore the crystal structure.
case2: Using ASE for visualization You can also use ASE to visualize the crystal structure directly in Python:
ase gui notebooks/NaCl.cifYou can rotate, zoom, and inspect the 3D atomic arrangement interactively.
Contributions are welcome! Please fork the repository and submit a pull request with your changes.
This project is released under the MIT License.
