Skip to content

Repository files navigation

LEGO - Lattice Elemental Geometry Operations

Crystal structure generator for molecular dynamics simulations. Creates arbitrarily rotated and cut crystal structures in IMD and LAMMPS data formats.

Originally by Erik Bitzek (2000-2011). Rewritten 2026.

Building

Requires a C compiler, zlib, and optionally OpenMP.

Linux

make

GCC with OpenMP is typically available out of the box.

macOS

The default Apple Clang does not support OpenMP. LEGO builds without it (single-threaded counting pass), which is fine for smaller systems:

make

For OpenMP support (recommended for large systems), install GCC via Homebrew:

brew install gcc
make CC=gcc-15 OMP_FLAG="-fopenmp"

Replace gcc-15 with the version Homebrew installed (check with ls /opt/homebrew/bin/gcc-*).

Custom Compiler

make CC=icc CFLAGS="-O3 -xHost"

The build produces a lego binary in the project root. The structures/ directory must be accessible at runtime (see Structure Search Path).

Quick Start

  1. Create a parameter file Cu_fcc.param:
structure fcc
lattice_const 3.615
mass 63.546
new_x 1 -1 0
new_z 1 1 1
box_x 50
box_y 50
box_z 50
box_mode periodic
output_format imd
outfile Cu_fcc_111.imd
  1. Run LEGO:
./lego Cu_fcc.param

This generates a (111)-oriented FCC copper crystal with periodic box dimensions of approximately 50 Angstrom in each direction.

Parameter Reference

Parameters are specified one per line in the form keyword value. Lines starting with # are comments.

Required Parameters

ParameterDescription
structureCrystal structure name (see Built-in Structures) or poscar for custom
outfileOutput filename
box_xTarget box size in x (Angstrom)
box_yTarget box size in y (Angstrom)
box_zTarget box size in z (Angstrom)

Crystal Definition

ParameterDefaultDescription
structure(required)Built-in name or poscar
structure_filePath to POSCAR file (required when structure poscar)
lattice_constfrom POSCAROverride lattice constant a (Angstrom)
lattice_const_bfrom POSCAROverride lattice constant b
lattice_const_cfrom POSCAROverride lattice constant c
massfrom POSCAROverride mass for type 0 (AMU)
mass1from POSCAROverride mass for type 1 (AMU)

Rotation

Three methods are available (highest priority first):

ParameterDescription
rotmat1_xExplicit rotation matrix row 1 (3 values)
rotmat1_yExplicit rotation matrix row 2 (3 values)
rotmat1_zExplicit rotation matrix row 3 (3 values)
new_xCrystallographic direction for x-axis (3 Miller indices)
new_yCrystallographic direction for y-axis (3 Miller indices)
new_zCrystallographic direction for z-axis (3 Miller indices)
axis1Rotation axis for first rotation (3 values)
angle1Rotation angle for first rotation (degrees)
axis2Rotation axis for second rotation (3 values)
angle2Rotation angle for second rotation (degrees)

When using new_x/new_z, specify any two of the three directions. The third is computed automatically via cross product.

Box and Output

ParameterDefaultDescription
box_modeperiodicperiodic (round to repeat distance) or exact
output_formatimdimd or lammps
compressnoGzip LAMMPS output (yes/no)
epsilon1.0e-6Tolerance for boundary checks
startnr1Starting atom ID number
atomtype(none)Override all atom types to this value

Cut Planes

Two independent cut planes are available. Each requires a normal vector, a distance or point, and a direction:

ParameterDescription
cut_norm1Normal vector of cut plane 1 (3 values)
plane_distance1Signed distance from origin to plane 1
plane_point1Point on plane 1 (alternative to distance)
cutdir1Cut direction: +1 remove far side, -1 remove near side, 0 disable
cut_norm2Normal vector of cut plane 2
plane_distance2Signed distance from origin to plane 2
plane_point2Point on plane 2
cutdir2Cut direction for plane 2

Miscellaneous

ParameterDescription
shiftvecShift all atoms by this vector (3 values, pre-rotation Cartesian)

Built-in Structures

NamePOSCAR FileDescription
scsc.poscarSimple cubic
fccfcc.poscarFace-centered cubic
bccbcc.poscarBody-centered cubic
hex / hcphex.poscarHexagonal close-packed
L12L12_Ni3Al.poscarL1_2 ordered (e.g. Ni3Al)
B2B2_NiAl.poscarB2 ordered (e.g. NiAl)
D011D011_Fe3C.poscarD0_11 ordered (e.g. Fe3C)

Custom structures can be used by setting structure poscar and providing a structure_file path to any valid VASP POSCAR file.

Structure Search Path

LEGO searches for built-in POSCAR files in this order:

  1. $LEGO_STRUCTURES environment variable
  2. structures/ relative to the current working directory
  3. structures/ relative to the executable location

Output Formats

IMD

The default format. Produces plain text files with an orthogonal box header:

#F A 1 1 1 3 0 0
#C number type mass x y z
#X box_x 0 0
#Y 0 box_y 0
#Z 0 0 box_z
##
#E
id type mass x y z
...

Masses are converted to IMD internal units (eV ps^2 / Angstrom^2).

LAMMPS

Produces LAMMPS data files in atomic style:

LEGO generated structure: ...
N atoms
M atom types
0.0 box_x xlo xhi
0.0 box_y ylo yhi
0.0 box_z zlo zhi
Masses
1 mass1
...
Atoms # atomic
id type x y z
...

LAMMPS types are 1-based. Masses are in AMU. Gzip compression is available via the compress parameter.

Python Wrapper

A Python wrapper is provided in python/lego.py:

fromlegoimportLego# Initialize (finds the binary automatically)lego=Lego()
# Run with a parameter fileresult=lego.run("params.param")
print(result.total_atoms, result.box)
# Run with keyword arguments (generates a temp parameter file)result=lego.run(
structure="fcc",
lattice_const=3.615,
mass=63.546,
box_x=100, box_y=100, box_z=100,
new_x="1 1 0", new_z="0 0 1",
output_format="lammps",
outfile="Cu_fcc.lmp"
)
# Convert CIF to POSCAR (requires pymatgen)Lego.cif_to_poscar("structure.cif", "structure.poscar")
# Convert any format to POSCAR (requires ASE)Lego.ase_to_poscar("structure.xyz", "structure.poscar")

The LegoResult object provides:

  • total_atoms -- number of atoms generated
  • box -- tuple of (box_x, box_y, box_z)
  • repeat_dist -- tuple of minimum repeat distances
  • success -- whether the run succeeded
  • stdout / stderr -- raw output

Examples

See the examples/ directory for ready-to-run parameter files:

  • W_bcc_100.param -- BCC tungsten, default (100) orientation
  • W_bcc_110.param -- BCC tungsten, (110) orientation
  • Cu_fcc_111.param -- FCC copper, (111) orientation
  • Cu_fcc_lammps.param -- FCC copper, LAMMPS output
  • Ni3Al_L12.param -- L1_2 Ni3Al ordered alloy
  • NiAl_B2.param -- B2 NiAl ordered alloy
  • custom_poscar.param -- Using a custom POSCAR file

License

GPLv3. See LICENSE.

Citation

If you use LEGO in your research, please cite it. See CITATION.cff for metadata.

About

Crystal structure generator for molecular dynamics simulations

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages