This tutorial is concerned with structural optimization. Except for high-symmetry structures - like the cubic crystal we are using in this tutorial the determination of the correct structure for the investigated system is a crucial part in the workflow of the computation. The Quantum-Espresso package is able to do a full structural optimization of the crystal and the QE-util provides an interface to this mode of operation. To continue skip over the initial setup to the second cell below.

In [1]:

```
# Import the basic libraries
# ASE system
import ase
from ase import Atom, Atoms
from ase import io
from ase.lattice.spacegroup import crystal
from ase.units import GPa, Bohr, Rydberg
# Spacegroup/symmetry library
from pyspglib import spglib
# iPython utility function
from IPython.core.display import Image
# Import the remote execution tools from the qe-util package
from qeutil import RemoteQE
# Access info
import host
qe=RemoteQE(label='SiC-structure', # A name for the project
kpts=[3,3,3], # k-space grid
xc='pz', # Exchange functional type in the name of the pseudopotentials
pp_type='vbc', # Variant of the pseudopotential
pp_format='UPF', # Format of the pseudopotential files
ecutwfc=70, # Energy cut-off
pseudo_dir='../pspot',
use_symmetry=True,
procs=4) # Use 8 cores for the calculation
print qe.directory
```

To start any calculation we need to create a crystal. We will again use a cubic, zinc-blende (F-43m) SiC crystal again with a lattice constant determined in our first tutorial. But this time we will distort it randomly to have a low-symmetry structure with many potential degrees of freedom and configuration displaced from the equilibrium position. Our task is to find the equilibrium structure - which hepefully will be a high-symmetry cubic crystal with a F-43m space group symmetry.

In [2]:

```
a=4.344
cryst = crystal(['Si', 'C'],
[(0, 0, 0), (0.25, 0.25, 0.25)],
spacegroup=216,
cellpar=[a, a, a, 90, 90, 90])
# Assign the calculator to our system
cryst.set_calculator(qe)
# Verify the symmetry
print "Symmetry group:", spglib.get_spacegroup(cryst)
```

In [3]:

```
print "Stress tensor (Voigt notation, GPa):\n", cryst.get_stress()/GPa
```

Let us distort randomly the positions of atoms in the unit cell as well as the unit cell itself. We will use standard deviation of 0.05 (5% of the unit cel size). This should generate a completely non-symmetric crystal (group P1).

In [4]:

```
cryst.rattle(stdev=0.05)
cryst.set_cell(diag(1+0.01*randn(3))*cryst.get_cell(), scale_atoms=True)
```

In [5]:

```
# Verify that indeed we have a low symmetry structure
print "Symmetry group:", spglib.get_spacegroup(cryst)
```

In [6]:

```
# Display the structure
ase.io.write('crystal.png', cryst, format='png', show_unit_cell=2, rotation='115y,15x', scale=30)
Image(filename='crystal.png')
```

Out[6]:

Let us check the effect the distorsion has on the crystal by calculating the stress tensor and forces acting on atoms in the distorted structure. Note that both forces as well as stresses are far from zero.

In [7]:

```
print "Stress tensor (Voigt notation, GPa):\n", cryst.get_stress()/GPa
# Print also the forces (eV/A)
print "\nForces on atoms (eV/A)"
print "======================"
print cryst.get_forces()
```

Our goal is to find the the structure as close as possible to minimum of energy (i.e. with as small as possible stresses on the unit cell and forces on atoms). The Quantum Espresso has a special mode of working where it minimizes forces and stresses.
The QE-util package provides full access to this mode by the way of `calc`

parameter of the calculator object. There are two variants of the optimization procedure:

`'relax'`

changes just the atomic positions keeping the cell fixed`'vc-relax'`

which changes atomic positions as well as unit cell shape and size

We will first try to optimize the internal degrees of freedom (atomic positions) and in the second step optimize both atomic positions and the unit cell parameters. This is actually a recommended procedure in the case of complicated structures. Running just `vc-relax`

optimization may not converge at all in such cases or, even worse, give you a *false minimum*.

**Note:** The optimization runs take much longer then the simple energy calculations. Be patient.

In [8]:

```
# Switch to the atomic position relaxation mode
qe.set(calc='relax')
# Switch off the use of symmetries.
qe.set(use_symmetry=False)
# Force recalculation by clearing the results from the previous calculation.
qe.reset()
```

In [9]:

```
# Run the calculation and get the stresses and forces at the end.
# The structure in cryst is *not* modified
print "Stress:\n", cryst.get_stress()/GPa
print "\nForces:\n", cryst.get_forces()
```

After running the first cycle, we note that the stresses have been reduced mostly to the diagonal part of the tensor (first three numbers in the Voigt notation) and forces have been diminished to the level of $10^{5}$ eV/A. We can improve this result by lowering the convergence condition to the $10^{-8}$ eV/A (note the translation of units to the units required by Quantum Espresso). We change the condition and re-run the calculation on the same structure.

In [10]:

```
qe.set(forc_conv_thr=1e-8*Rydberg/Bohr)
qe.reset()
```

In [11]:

```
print "Stress:\n", cryst.get_stress()/GPa
print "\nForces:\n", cryst.get_forces()
```

We can see that the forces dropped by another two orders of magnitude and the stress got concentrated even more on the diagonal elements. Thus, let us update the positions in the `cryst`

object with a our new positions. The found positions are present in the `results`

dictionary of the calculator object. We can check the symmetry of the resulting crystal - but it will probably be different from the desired F-43m. Which should be expected - the unit cell is still the same deformed unit cell we have generated at the start.

In [12]:

```
# Update the positions using calculated values
cryst.set_scaled_positions(qe.results['atomic_positions'])
# Check the symmetry. Probably not the F-43m !
print "Symmetry group:", spglib.get_spacegroup(cryst,symprec=1e-4)
```

We have approched optimal atomic positions in the unit cell. It is time to search for the optimal shape and size of the unit cell itself. We need to switch to the `vc-relax`

mode and run the optimization again. Then update the structure again - this time both unit cell and atomic positions and check the symmetry.

In [16]:

```
qe.set(calc='vc-relax')
qe.reset()
```

In [17]:

```
print "Stress:\n", cryst.get_stress()/GPa
print "\nForces:\n", cryst.get_forces()
```

In [18]:

```
# Update the crystal
cryst.set_cell(qe.results['cell'])
cryst.set_scaled_positions(qe.results['atomic_positions'])
# Check the symmetry
print "Symmetry group:", spglib.get_spacegroup(cryst,symprec=1e-4)
```

After a single run the forces/stresses as well as the symmetry will not be converged well enough. You will need to repeat the cycle of updating the crystal and optimizing it again (two cells above) one or two more times - until you reach the appropriate level of forces/stresses and symmetry.

We can conclude the run by "snapping" the structure to the high-symmetry values by rounding the positions and sizes to appropriate number of decimal places.

Finally we check the symmetry of the structure and verify the stresses and forces by running a single point (`scf`

- Self Consistent Field) calculation on the final structure - which should result in stress components below 0.01 GPa and forces below $10^{-6}$ eV/A as well as recovery of the expected F-43m symmetry group of the crystal.

In [19]:

```
# Round the sizes and positions to get to the high-symmetry structure
cryst.set_cell(np.round(qe.results['cell'],4))
cryst.set_scaled_positions(np.round(qe.results['atomic_positions'],3))
# See the structure
print "Unit cell:\n", cryst.get_cell()
print "\nAtomic positions:\n", cryst.get_scaled_positions()
```

In [20]:

```
# Vierify the symmetry
print "Symmetry group:", spglib.get_spacegroup(cryst)
```

In [21]:

```
# Switch to the single point energy calculation mode
qe.set(calc='scf')
qe.reset()
```

In [22]:

```
# Verify the final stresses and forces
print "Stress tensor (Voigt notation, GPa):\n", cryst.get_stress()/GPa
# Print also the forces (eV/A)
print "\nForces on atoms (eV/A)"
print "======================"
print cryst.get_forces()
```

In [23]:

```
# Display the structure
ase.io.write('crystal.png', cryst, format='png', show_unit_cell=2, rotation='115y,15x', scale=30)
Image(filename='crystal.png')
```

Out[23]: