# Cluster counts¶

The purpose of this example is to demonstrate how to carry out a cluster count analysis.

## Import modules¶

First, one needs to import the classes ClusterCounts and Structure, which are used for storing information regarding cluster counts and structures, respectively, in addition to the functions create_orbit_list and get_primitive_structure. In particular, the latter will be used to generate a primitive structure, for which an orbit list will be created, using the former. Also, the ASE function ase.build.bulk() is needed to generate the prototype structure.

from ase.build import bulk
from icet import Structure
from icet.tools import get_primitive_structure
from icet.core.cluster_counts import ClusterCounts
from icet.core.orbit_list import create_orbit_list


## Generate a prototype Ti supercell¶

The next step is to build a prototype supercell, in the form of a $$2\times2\times1$$ BCC cell that is randomly populated with titanium and tungsten atoms. Moreover, the cutoff for pairs is set to 4 Å.

atoms = bulk('Ti', 'bcc', a=3.43).repeat([2, 2, 1])
atoms.pbc = [True, True, False]
atoms.set_chemical_symbols(['Ti', 'W', 'W', 'Ti'])


## Create a primitive orbit list¶

As will be seen later, the orbit list corresponding to the primitive structure of the ASE Atoms object defined earlier is required for calculating the cluster counts. More precisely, this is achieved by first calling the get_primitive_structure followed by the Structure.from_atoms method. Thereafter, the primitive structure thus obtained is provided as input to create_orbit_list, together with the list of cutoff distances for the clusters.

# pair clusters within the cutoff distance
cutoffs = [4.0]
prim_atoms = get_primitive_structure(atoms)
prim_structure = Structure.from_atoms(prim_atoms)
prim_orbitlist = create_orbit_list(prim_structure, cutoffs)



## Count the clusters¶

To count the number of clusters a ClusterSpace object is initiated first. Thereafter, a Structure is generated from the supercell that was generated above with help of the Structure.from_atoms method. To calculate the number of clusters, this structure together with the primitive orbit list that was created in the previous section are given as input arguments to the ClusterCounts.count_clusters method.

cluster_counts = ClusterCounts()
structure = Structure.from_atoms(atoms)
cluster_counts.count_clusters(structure, prim_orbitlist)



## Source code¶

The complete source code is available in examples/get_cluster_count.py
"""
This example demonstrates how to count the number of clusters for a structure.
"""

# Start import
from ase.build import bulk
from icet import Structure
from icet.tools import get_primitive_structure
from icet.core.cluster_counts import ClusterCounts
from icet.core.orbit_list import create_orbit_list
# End import

# Create a titanium, single-layered, sheet and randomly populate some of the
# sites with W atoms.
# Start setup
atoms = bulk('Ti', 'bcc', a=3.43).repeat([2, 2, 1])
atoms.pbc = [True, True, False]
atoms.set_chemical_symbols(['Ti', 'W', 'W', 'Ti'])
# End setup

# Determine the orbit list for the corresponding primitive structure for all
# pair clusters within the cutoff distance
cutoffs = [4.0]
prim_atoms = get_primitive_structure(atoms)
prim_structure = Structure.from_atoms(prim_atoms)
prim_orbitlist = create_orbit_list(prim_structure, cutoffs)

# Use the primitive orbit list to count the number of clusters.
cluster_counts = ClusterCounts()
structure = Structure.from_atoms(atoms)
cluster_counts.count_clusters(structure, prim_orbitlist)

# Print all of the clusters that were found.
print('Number of atoms {0}'.format(len(atoms)))
print('Found {} clusters'.format(len(cluster_counts)))
cluster_counts.print()