|
| 1 | +Batch Structure Optimization |
| 2 | +================ |
| 3 | + |
| 4 | +This is a simple example of how to use MatterSim to efficiently relax a list of structures. |
| 5 | + |
| 6 | + |
| 7 | +Import the necessary modules |
| 8 | +---------------------------- |
| 9 | + |
| 10 | +First we import the necessary modules. |
| 11 | + |
| 12 | +.. code-block:: python |
| 13 | + :linenos: |
| 14 | +
|
| 15 | + from ase.build import bulk |
| 16 | + from mattersim.applications.batch_relax import BatchRelaxer |
| 17 | + from mattersim.forcefield.potential import Potential |
| 18 | +
|
| 19 | +Set up the MatterSim batch relaxer |
| 20 | +---------------------------------- |
| 21 | + |
| 22 | +.. code-block:: python |
| 23 | + :linenos: |
| 24 | +
|
| 25 | + # initialize the default MatterSim Potential |
| 26 | + potential = Potential.from_checkpoint() |
| 27 | +
|
| 28 | + # initialize the batch relaxer with a EXPCELLFILTER for cell relaxation and a FIRE optimizer |
| 29 | + relaxer = BatchRelaxer(potential, fmax=0.01, filter="EXPCELLFILTER", optimizer="FIRE") |
| 30 | +
|
| 31 | +
|
| 32 | +Relax the structures |
| 33 | +-------------------- |
| 34 | + |
| 35 | +.. code-block:: python |
| 36 | + :linenos: |
| 37 | +
|
| 38 | + # Here, we generate a list of ASE Atoms objects we want to relax |
| 39 | + atoms = [bulk("C"), bulk("Mg"), bulk("Si"), bulk("Ni")] |
| 40 | +
|
| 41 | + # And then perturb them a bit so that relaxation is not trivial |
| 42 | + for atom in atoms: |
| 43 | + atom.rattle(stdev=0.1) |
| 44 | +
|
| 45 | + # Run the relaxation |
| 46 | + relaxation_trajectories = relaxer.relax(atoms) |
| 47 | +
|
| 48 | +
|
| 49 | +Inspect the relaxed structures |
| 50 | +------------------------------ |
| 51 | + |
| 52 | +.. code-block:: python |
| 53 | + :linenos: |
| 54 | + |
| 55 | + # Extract the relaxed structures and corresponding energies |
| 56 | + relaxed_structures = [traj[-1] for traj in relaxation_trajectories.values()] |
| 57 | + relaxed_energies = [structure.info['total_energy'] for structure in relaxed_structures] |
| 58 | +
|
| 59 | + # Do the same with the initial structures and energies |
| 60 | + initial_structures = [traj[0] for traj in relaxation_trajectories.values()] |
| 61 | + initial_energies = [structure.info['total_energy'] for structure in initial_structures] |
| 62 | +
|
| 63 | + # verify by inspection that total energy has decreased in all instances |
| 64 | + for initial_energy, relaxed_energy in zip(initial_energies, relaxed_energies): |
| 65 | + print(f"Initial energy: {initial_energy} eV, relaxed energy: {relaxed_energy} eV") |
0 commit comments