# Permutation matrix¶

The purpose of this example is to demonstrate the extraction of a permutation matrix.

## Import modules¶

To extract permutation matrices, only a single icet function is required, namely permutation_matrix_from_atoms(). Additionally, the ASE function bulk() will be used to generate a structure.

import numpy as np
from ase.build import bulk
from icet.core.permutation_matrix import permutation_matrix_from_atoms



## Generate prototype unit cell¶

The next step is to build a prototype ASE Atoms object, here an aluminum fcc unit cell.

atoms = bulk('Al', 'fcc', a=2.0)



## Obtain permutation matrix¶

Next, the permutation_matrix_from_atoms() function is called with the ASE Atoms object that was defined above and a neighbor cutoff of 2.0 Å as arguments. The resulting output is a list that contains three elements, namely a PermutationMatrix, a primitive Structure and a NeighborList.

neighbor_cutoff = 2.0
permutation_matrix, prim_structure, neighbor_list = \
permutation_matrix_from_atoms(atoms, neighbor_cutoff)



Since the verbosity was set to $$\geq 3$$, the following extra information should be printed by permutation_matrix_from_atoms():

size of atoms_prim 1
number of positions: 19


## Extract permuted positions¶

The permuted as well as the indexed and unique positions can be extracted from the PermutationMatrix object, generated earlier, using the methods get_permuted_positions() and get_indexed_positions(), respectively.

perm_pos = permutation_matrix.get_permuted_positions()
ind_pos, unique_pos = permutation_matrix.get_indexed_positions()



## Source code¶

The complete source code is available in examples/get_permutation_matrix.py
'''
This example generate a permutation matrix for a structure
'''

# Import modules
import numpy as np
from ase.build import bulk
from icet.core.permutation_matrix import permutation_matrix_from_atoms

# Create a prototype Al structure
atoms = bulk('Al', 'fcc', a=2.0)

# Generate a permutation matrix for all neighbors inside the cutoff
neighbor_cutoff = 2.0
permutation_matrix, prim_structure, neighbor_list = \
permutation_matrix_from_atoms(atoms, neighbor_cutoff)

# Extract the permuted, indexed and unique positions.
perm_pos = permutation_matrix.get_permuted_positions()
ind_pos, unique_pos = permutation_matrix.get_indexed_positions()

# Print the permuted, indexed and unique positions.
print('Permutated fractional coordinates')
for pp in perm_pos:
unique_rows = np.vstack({tuple(row) for row in pp})
for el in unique_rows:
print(el, end=' ')
print('')
print('Permutated indices and positions')
for i, pos in enumerate(ind_pos):
print(i, len(set(pos)), pos)
print('Unique permuted indices and positions')
for index, dist in enumerate(unique_pos):
print(index, dist)