Skip to content

Instantly share code, notes, and snippets.

@janash
Last active August 4, 2023 20:21
Show Gist options
  • Save janash/69c6d5e48956a2f7fc0eedc46dab1ced to your computer and use it in GitHub Desktop.
Save janash/69c6d5e48956a2f7fc0eedc46dab1ced to your computer and use it in GitHub Desktop.
cubic lattice
def generate_cubic_lattice(num_atoms, density):
"""
Generate points on a cubic lattice using a desired final density.
Parameters
----------
num_atoms: int
The number of atoms to place on the lattice.
density: float
The desired system density.
Returns
-------
coords: list
A nested list of generated coordinates.
"""
# Calculate box length based on number of atoms and density.
volume = num_atoms / density
box_length = math.pow(volume, (1./3.))
# Calculate the upper bound of cube size.
# Our approach will be to place atoms until
# we place all needed. For this, we need
# to determine the maximum number of atoms on each
# side.
max_side = math.ceil(math.pow(num_atoms, (1./3.)))
# Determine spacing based on number of atoms
# and box length.
spacing = box_length / max_side # units length / atom
coordinates = []
count = 0
for i in range(max_side):
for j in range(max_side):
for k in range(max_side):
coordinates.append([i*spacing, j*spacing, k*spacing])
count += 1
if count == num_atoms:
return coordinates, box_length
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment