3. Batch Structure Optimization#

This is a simple example of how to use MatterSim to efficiently relax a list of structures.

3.1. Import the necessary modules#

First we import the necessary modules.

1from ase.build import bulk
2from mattersim.applications.batch_relax import BatchRelaxer
3from mattersim.forcefield.potential import Potential

3.2. Set up the MatterSim batch relaxer#

1# initialize the default MatterSim Potential
2potential = Potential.from_checkpoint()
3
4# initialize the batch relaxer with a EXPCELLFILTER for cell relaxation and a FIRE optimizer
5relaxer = BatchRelaxer(potential, fmax=0.01, filter="EXPCELLFILTER", optimizer="FIRE")

3.3. Relax the structures#

1# Here, we generate a list of ASE Atoms objects we want to relax
2atoms = [bulk("C"), bulk("Mg"), bulk("Si"), bulk("Ni")]
3
4# And then perturb them a bit so that relaxation is not trivial
5for atom in atoms:
6    atom.rattle(stdev=0.1)
7
8# Run the relaxation
9relaxation_trajectories = relaxer.relax(atoms)

3.4. Inspect the relaxed structures#

 1# Extract the relaxed structures and corresponding energies
 2relaxed_structures = [traj[-1] for traj in relaxation_trajectories.values()]
 3relaxed_energies = [structure.info['total_energy'] for structure in relaxed_structures]
 4
 5# Do the same with the initial structures and energies
 6initial_structures = [traj[0] for traj in relaxation_trajectories.values()]
 7initial_energies = [structure.info['total_energy'] for structure in initial_structures]
 8
 9# verify by inspection that total energy has decreased in all instances
10for initial_energy, relaxed_energy in zip(initial_energies, relaxed_energies):
11    print(f"Initial energy: {initial_energy} eV, relaxed energy: {relaxed_energy} eV")