Metadata-Version: 2.4
Name: necklace-enum
Version: 0.3.4
Summary: Derivative superstructures generated as Lyndon words on the quotient group of a superlattice
Author-email: "Richard H. Taylor" <r_taylor@outlook.com>
License-Expression: MIT
Project-URL: Homepage, https://r-h-taylor.github.io/necklace-enum/
Project-URL: Repository, https://github.com/r-h-taylor/necklace-enum
Project-URL: Archive, https://doi.org/10.5281/zenodo.24330095
Keywords: derivative structures,enumeration,alloys,cluster expansion,Lyndon words,necklaces,Hart-Forcade
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Science/Research
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Scientific/Engineering :: Physics
Classifier: Topic :: Scientific/Engineering :: Chemistry
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: numpy>=1.24
Requires-Dist: numba>=0.59
Provides-Extra: ase
Requires-Dist: ase>=3.22; extra == "ase"
Provides-Extra: pymatgen
Requires-Dist: pymatgen; extra == "pymatgen"
Provides-Extra: test
Requires-Dist: pytest; extra == "test"
Requires-Dist: ase>=3.22; extra == "test"
Requires-Dist: spglib; extra == "test"
Dynamic: license-file

# necklace-enum

[![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.24330095.svg)](https://doi.org/10.5281/zenodo.24330095)

Enumerates derivative superstructures — the symmetrically distinct ways of decorating a parent lattice with several kinds of atoms — by generating them directly as canonical words, without storing labelings or comparing against the structures already found.

Project page: https://r-h-taylor.github.io/necklace-enum/ · Archived at Zenodo: https://doi.org/10.5281/zenodo.24330095

This is the reference implementation for

> R. H. Taylor et al., *Algorithm for low-memory derivative structure enumeration using toroidal necklaces* (2026).

It keeps the superlattice and Smith-normal-form construction of Hart & Forcade
([Phys. Rev. B 77, 224115 (2008)](https://doi.org/10.1103/PhysRevB.77.224115)) and replaces their duplicate-removal
step. Reading the sites column by column along the largest cyclic factor of the quotient group turns each labeling
into a word. Translations act on the word as rotations combined with a single letterwise permutation, so the
canonical structures are Lyndon words, which are generated directly with an extended necklace recursion.

## Install

```bash
pip install necklace-enum            # numpy + numba
pip install "necklace-enum[ase]"     # to export ASE Atoms
```

From a clone of this repository (needed for the tests and the benchmark scripts in `scripts/`):

```bash
pip install ".[test]"
pytest
```

## Use

```python
import necklace_enum as te

P = te.fcc(["Cu", "Au"])
te.count_structures(P, range(1, 13))                 # {n: count} without listing them

for s in te.enumerate_structures(P, 8, composition={"Cu": 6, "Au": 2}):
    atoms = s.to_ase()                               # or s.to_pymatgen(), or s.hnf / s.labels

hcp = te.hcp(["Li", "Vac", "Co"], allowed=[{"Li", "Vac"}, {"Co", "Vac"}])   # site restrictions
custom = te.Parent(lattice, basis, species=["A", "B", "C"])                 # any parent
```

Command line:

```bash
necklace-enum count fcc Cu,Au 1-12
necklace-enum count fcc 2 2-23 --label-exchange          # Hart & Forcade, Table V
necklace-enum list hcp Mg,Cd 4 --composition 5,3 --format extxyz -o out.xyz
```

**Fast listing as arrays.** `enumerate_labelings` takes the same arguments as `enumerate_structures` but hands the
structures to Python as rows of integer arrays, in batches, instead of one `Structure` object each. It is several
times faster and is the form to use when the structures go straight into NumPy code or a file:

```python
for H, labels in te.enumerate_labelings(P, 12):     # labels: (rows, sites) array, one structure per row
    ...                                              # te.Structure(P, H, tuple(row)) rebuilds an object
```

**Degeneracies (weights for statistical mechanics).** Every structure carries its degeneracy, at fixed
composition as well as over all compositions: `s.degeneracy` is the number of labelings of its superlattice that are
equivalent to it (enumlib's `labdegn`), `s.superlattice_degeneracy` the number of equivalent superlattices of the same
size (`Hdegn`), and `s.total_degeneracy` their product (`Totdegn`), the weight g in a partition function
sum g exp(-E/kT). Summed over all structures of one size, the total degeneracies count every labeling of every
superlattice exactly once. While testing a word for canonicity it compares
the word with its images under the symmetry operations, and the comparisons that end equal are its stabilizer, so
degeneracy = group order / stabilizer order at no extra pass. They are computed only when requested:
`enumerate_structures` fills them in by default, `enumerate_labelings` with `degeneracy=True`, and skipping them
(`degeneracy=False`) makes object listing about a quarter faster.

```python
for s in te.enumerate_structures(P, 8, composition=(6, 2)):
    print(s.labels, s.degeneracy, s.superlattice_degeneracy, s.total_degeneracy)

for H, labels, g in te.enumerate_labelings(P, 12, composition=(9, 3), degeneracy=True):
    weights = g * te.superlattice_degeneracy(P, H)  # one weight per row of labels

stats = {}
te.count_structures(P, 12, stats=stats)              # stats["weight"]: sum of total degeneracies
```

```bash
necklace-enum list fcc Cu,Au 8 --composition 6,2 --degeneracy      # columns: g on cell, equivalent cells, total
```

`to_ase()` stores the three numbers in `atoms.info`, so they are kept in extended XYZ files.

**Counting without listing.** `count_structures_burnside` gives the same counts as `count_structures`, with the
same arguments, by Burnside's lemma: it sums fixed points over the symmetry operations of each superlattice instead
of generating structures. Its cost grows with the number of superlattices, not structures, so it reaches far larger
cells (binary fcc at n = 32 in about a second, n = 96 in under half a minute):

```python
te.count_structures_burnside(te.fcc(2), range(1, 61), label_exchange=True)
```

```bash
necklace-enum count fcc 2 1-60 --label-exchange --burnside
python scripts/oeis_counts.py --nmax 100 -o b_fcc_binary.txt     # OEIS b-file
```

**Conventions.**
- By default, species are distinct: A₃B and AB₃ are different structures. With `label_exchange=True`, structures
  that differ only by a permutation of species are identified, as in Hart & Forcade's tables.
- Only structures that contain every species are listed unless `include_incomplete=True`.
- Structures that are periodic in a smaller cell are excluded; they appear at their own, smaller size.

## enumlib compatibility

necklace-enum reads enumlib's input file and writes its `struct_enum.out` format, so it can take the place of
`enum.x` in existing workflows:

```bash
necklace-enum enumlib struct_enum.in                    # writes struct_enum.out, as enum.x does
necklace-enum list fcc Cu,Au 1-12 --format enum -o struct_enum.out
```

```python
te.run_enumlib_input("struct_enum.in", "struct_enum.out")
te.write_struct_enum("struct_enum.out", P, range(1, 13))
header, rows = te.read_struct_enum("struct_enum.out")    # also reads enumlib's own files
```

enumlib's `makeStr.py` and `makestr.x` (which pymatgen calls) build POSCAR files from this output unchanged.
The output follows enumlib's conventions:
- A "partial" listing without concentration restrictions identifies structures related by swapping species, as in
  Hart & Forcade's tables. "Full" listings and concentration-restricted runs list them separately.
- The degeneracy columns are exact, in every mode: `Totdegn` summed over a cell size equals the number of
  (superlattice, labeling) pairs the structures stand for, including at fixed concentration.

`scripts/compare_with_enumlib.py` runs both programs on the same input and compares the results.

## Verification

```bash
python check_necklace_enum.py           # stand-alone check (under a minute)
python check_necklace_enum.py --full    # more cases and larger cells (a few minutes)
pytest                   # brute force, published counts, export checks (about 15 s)
pytest --slow            # also binary fcc up to n = 23
```

The tests compare the generated structures, one by one, with an independent brute-force code for fcc, bcc,
simple-cubic and hcp parents (including site restrictions and fixed compositions). They also reproduce every count
tabulated by Hart & Forcade, compare the Burnside counter with the generator over parents, species counts,
compositions and site restrictions, and check that each exported crystal is primitive in its own cell and that no two
exported crystals are symmetry-equivalent. The enumlib-format tests rebuild every structure from the written file with enumlib's indexing rule and check the degeneracy columns against a direct count. `tests/test_degeneracy.py` checks the generator's degeneracies against an independent pass that applies every operation of each superlattice's group, over Bravais and multilattice parents, label exchange, fixed compositions and site restrictions, and checks that the total degeneracies count every labeling of every superlattice, by direct enumeration. `tests/test_hart_forcade.py` compares the structure lists, superlattice by superlattice and in canonical form, with `tests/hart_forcade.py`, our own compiled reimplementation of the table-based 2008 algorithm (not enumlib), in 118 cases: fcc, bcc, simple-cubic, hexagonal and tetragonal parents, two to four species, with and without label exchange, at all and at fixed compositions.

## Performance and limits

- The compiled generator labels binary fcc structures at tens of nanoseconds per structure. `count_structures`
  counts all 4,592,213,143 binary fcc structures with 32 sites per cell (up to label exchange) in about 194 s on
  one core of an Apple M2.
- Memory does not grow with the number of structures. The generator's state is polynomial in the cell size, and
  listing streams structures from the compiled kernel in batches of 2^16 words. Counting stays within 10 MB of the
  85 MB interpreter baseline up to 9.2 billion structures; listing stays below 0.22 GB
  (logs in `scripts/*_M2.txt`).
- `enumerate_structures` builds a Python object per structure, so listing that way is far slower than counting.
  `enumerate_labelings` hands over arrays instead and is several times faster; use it, counts or fixed compositions
  for very large runs.
- The per-superlattice setup is plain Python and takes a few tenths of a second per size. For dilute fixed-composition
  runs it is larger than the labeling time.
- Letters hold `d * d1 * d2` sites. For multilattices with many sites and species the letter tables can grow large,
  in which case a `MemoryError` names the offending Smith normal form.
- The first call compiles the kernels with Numba (a few seconds); later calls use the on-disk cache.


## License and citation

MIT. If you use this code, please cite:

1. R. H. Taylor et al., *Algorithm for low-memory derivative structure enumeration using toroidal necklaces* (2026).
   Software: necklace-enum, Zenodo, [doi:10.5281/zenodo.24330095](https://doi.org/10.5281/zenodo.24330095).
2. G. L. W. Hart and R. W. Forcade, *Algorithm for generating derivative structures*,
   Phys. Rev. B **77**, 224115 (2008), [doi:10.1103/PhysRevB.77.224115](https://doi.org/10.1103/PhysRevB.77.224115).

```bibtex
@article{Taylor2026necklace,
  author = {Taylor, Richard H. and Curtarolo, Stefano},
  title  = {Algorithm for low-memory derivative structure enumeration using toroidal necklaces},
  year   = {2026},
  note   = {Software: necklace-enum, doi:10.5281/zenodo.24330095}
}

@article{HartForcade2008,
  author  = {Hart, Gus L. W. and Forcade, Rodney W.},
  title   = {Algorithm for generating derivative structures},
  journal = {Phys. Rev. B},
  volume  = {77},
  pages   = {224115},
  year    = {2008},
  doi     = {10.1103/PhysRevB.77.224115}
}
```
