MD simulation

Dear hiphive users and developers,

I want to perform molecular dynamics calculations according to the tutorial, but I faced the following error and I did not succeed in solving it by changing the parameters, and no matter how many times I change the parameters, I still encounter this error. Please guide me how to fix this error.

ValueError: Displacement 4.22391 larger than maximum allowed displacement 3.00000

here is the complete error:

ValueError                                Traceback (most recent call last)
Cell In[133], line 12
     10 # run MD
     11 MaxwellBoltzmannDistribution(atoms, temperature * units.kB)
---> 12

File ~\AppData\Roaming\Python\Python310\site-packages\ase\md\, in, steps)
    135 """ Call and adjust max_steps """
    136 self.max_steps = steps + self.nsteps
--> 137 return

File ~\AppData\Roaming\Python\Python310\site-packages\ase\optimize\, in
    149 def run(self):
    150     """Run dynamics algorithm.
    152     This method will return when the forces on all individual
    153     atoms are less than *fmax* or when the number of steps exceeds
    154     *steps*."""
--> 156     for converged in Dynamics.irun(self):
    157         pass
    158     return converged

File ~\AppData\Roaming\Python\Python310\site-packages\ase\optimize\, in Dynamics.irun(self)
    131 # run the algorithm until converged or max_steps reached
    132 while not self.converged() and self.nsteps < self.max_steps:
    134     # compute the next step
--> 135     self.step()
    136     self.nsteps += 1
    138     # let the user inspect the step and change things before logging
    139     # and predicting the next step

File ~\AppData\Roaming\Python\Python310\site-packages\ase\md\, in Langevin.step(self, forces)
    168 # recalc velocities after RATTLE constraints are applied
    169 self.v = (self.atoms.get_positions() - x -
    170           self.c5 * self.eta) / self.dt
--> 171 forces = atoms.get_forces(md=True)
    173 # Update the velocities
    174 self.v += (self.c1 * forces / self.masses - self.c2 * self.v +
    175            self.c3 * self.xi - self.c4 * self.eta)

File ~\AppData\Roaming\Python\Python310\site-packages\ase\, in Atoms.get_forces(self, apply_constraint, md)
    786 if self._calc is None:
    787     raise RuntimeError('Atoms object has no calculator.')
--> 788 forces = self._calc.get_forces(self)
    790 if apply_constraint:
    791     # We need a special md flag here because for MD we want
    792     # to skip real constraints but include special "constraints"
    793     # Like Hookean.
    794     for constraint in self.constraints:

File ~\AppData\Roaming\Python\Python310\site-packages\ase\calculators\, in GetPropertiesMixin.get_forces(self, atoms)
     22 def get_forces(self, atoms=None):
---> 23     return self.get_property('forces', atoms)

File ~\AppData\Roaming\Python\Python310\site-packages\ase\calculators\, in Calculator.get_property(self, name, atoms, allow_calculation)
    735     if not allow_calculation:
    736         return None
--> 737     self.calculate(atoms, [name], system_changes)
    739 if name not in self.results:
    740     # For some reason the calculator was not able to do what we want,
    741     # and that is OK.
    742     raise PropertyNotImplementedError('{} not present in this '
    743                                       'calculation'.format(name))

File ~\AppData\Roaming\Python\Python310\site-packages\hiphive\calculators\, in ForceConstantCalculator.calculate(self, atoms, properties, system_changes)
     81 Calculator.calculate(self, atoms, properties, system_changes)
     82 self._check_atoms()
---> 83 self._compute_displacements()
     85 if 'forces' in properties or 'energy' in properties:
     86     E, forces = self.compute_energy_and_forces()

File ~\AppData\Roaming\Python\Python310\site-packages\hiphive\calculators\, in ForceConstantCalculator._compute_displacements(self)
    106 if max_disp > self.max_allowed_disp:
    107     msg = 'Displacement {:.5f} larger than maximum allowed displacement {:.5f}'
--> 108     raise ValueError(msg.format(max_disp, self.max_allowed_disp))

ValueError: Displacement 4.22391 larger than maximum allowed displacement 3.00000


The error tells you that one of the atoms moved a long way from it’s lattice point during the simulation. Aka the simulation exploded. This is typically indicative of the expansion being unstable, a result of poor sampling, fitting or simply that the system is prone to e.g. diffusion.

Did you follow the tutorial exactly or did you change the system, parameters etc?

Yes I did follow the tutorial exactly but I replaced the “prim” and “fcp” objects with my own and as you said surly this problem caused by my fcp. I have calculated this fcp by DFT for rattled_structures created by hiphive. Shall I restarted my calculation with new structures or is there another way to solve this problem. For example can I change the allowed displacement ?

You can either:

  1. Retrain you potential with structures close to the configuration where the system became unstable
  2. Increase the regularization during the fitting procedure

People have also tried stabilize FCPs using pair and/or site potentials with varying degree of success.

You can change the max allowed displacement with a kw in the calculator if you think 3Å displacements are physically reasonable displacements in your system.

To solve my problem, I have generated a new set of displacements using the tutorials in superposition of normal modes of advanced topic, but I faced a new error which I mentioned it below:

/usr/local/lib/python3.10/dist-packages/hiphive/ in prepare_structures(structures, atoms_ideal, calc, check_permutation)
    178     list of prepared structures with forces and displacements as arrays
    179     """
--> 180     return [prepare_structure(s, atoms_ideal, calc, check_permutation) for s in structures]

/usr/local/lib/python3.10/dist-packages/hiphive/ in <listcomp>(.0)
    178     list of prepared structures with forces and displacements as arrays
    179     """
--> 180     return [prepare_structure(s, atoms_ideal, calc, check_permutation) for s in structures]

/usr/local/lib/python3.10/dist-packages/hiphive/ in prepare_structure(atoms, atoms_ideal, calc, check_permutation)
    123     atoms_new = atoms.copy()
    124     atoms_new = atoms_new[perm]
--> 125     atoms_new.arrays['forces'] = forces[perm]
    126     disps = get_displacements(atoms_new, atoms_ideal)
    127     atoms_new.arrays['displacements'] = disps

IndexError: index 20 is out of bounds for axis 0 with size 20

The code and its colab link is attached bellow:

from import write
from hiphive import ClusterSpace, StructureContainer, ForceConstantPotential
from trainstation import Optimizer
from hiphive.utilities import prepare_structures
from hiphive.structure_generation import (generate_rattled_structures,

# parameters
alat = 3.52            # lattice parameter
size = 2               # supercell size
n_structures = 15     # number of configurations to generate

# rattle parameters
T = 800
rattle_std = 0.12
min_distance = 0.67 * alat

# reference structure
from import read
from ase.calculators.espresso import Espresso
prim = read('struct.cif')

inp_data={'prefix':"myprefix",'electron_maxstep':200,'outdir':"./",        'pseudo_dir':"./",'tstress':True,'tprnfor':True,'calculation':'scf', 
 'ecutrho':240,'verbosity':'high','ecutwfc':30, 'diagonalization': 'david', 'occupations':'smearing','smearing':'mp', 'mixing_mode':'plain', 'mixing_beta':0.7,'degauss':0.001, 'nspin':1}


prim.calc=Espresso(pseudopotentials=pseudodict, input_data=inp_data, kpts=(8,8,8))


supercell = prim.repeat(size)
reference_positions = supercell.get_positions()
write('', supercell)

# standard rattle
print('standard rattle')
structures_rattle = generate_rattled_structures(
    supercell, n_structures, rattle_std)
write('structures_rattle.extxyz', structures_rattle)

# Monte Carlo rattle
print('Monte Carlo rattle')
structures_mc_rattle = generate_mc_rattled_structures(
    supercell, n_structures, 0.25*rattle_std, min_distance, n_iter=20)
write('structures_mc_rattle.extxyz', structures_mc_rattle)

# Phonon rattle
# initial model
cs = ClusterSpace(supercell, [5.0])
sc = StructureContainer(cs)
rattled_structures = generate_rattled_structures(supercell, 1, 0.05)
rattled_structures = prepare_structures(rattled_structures, supercell, calc)

Could you please help me to solve it?

You set calc=test, but test is an ase-atoms object not a calculator?
(also in your code you never call generate_phonon_rattled_structures)

This is a problem I have had since the beginning. The calculator that is used in tutorial is EMT which is not compatible with my compounds. I need to do my calculation via DFT codes and import the results in database or introduce the output file. In a situation like now, I get confused and I don’t know what to put in place of EMT()??? I have introduced the output file of QE as " test=read(“espresso.pwo”)" but I do not know how to solve this error and what to to replace with calc=EMT()???
about calling “generate_phonon_rattled_structures”, you are rigth, I did not send you the rest of code and this is mentioned there (as said in tutorial)

Please guide me to solve the error.

I dont know exactly how the quantum Espresso calculator works, but if you want to use the calculator you define as Espresso(pseudopotentials=pseudodict, ...) for the supercell calculations, then can you not simply use this as the input calculator for the prepare_structures?

If you have already done the DFT calculations for the rattled-structures (outside of this python script), then if you read the structure supercell = read('...') does it not have a calculator (SinglePoint) already attached with the DFT forces? In this case you dont need to pass any calculator to prepare_structures.

You can read more about ase calculators, read/write functions, ase Atoms object on their homepage Atomic Simulation Environment — ASE documentation

Thank you for your guidance, I imported the structure via command supercell = read('...') and I could generated the structurs_phonon_rattle_T800.extxyz. But in output is written “Imaginary modes present”! How can I remove these modes? is there any way to generate structures without imaginary modes?

The warning " “Imaginary modes present”" indicates that the harmonic force constants (not the structures) have imaginary modes. If you want to resolve this I suggest you start by looking into your harmonic force constants.

I know that the imaginary modes are related to the force constants in the harmonic approximation. These imaginary modes indicate that this structure is dynamically unstable. This result about my compounds is consistent with the experimental results. ‌Because my compounds are only stable at high temperatures. The way of displacements that are created in the structure is somehow related to the temperature. In the commands I sent earlier, phonon displacements were created at 800 K. That’s why I expected that there would be no more imaginary modes. Is this expectation wrong? Do I need to change another parameters?

the phonon-rattle function takes the harmonic force constants provided (which has imaginary modes) and generate supercells with displacements according to some temperature in the harmonic approximation. It does not modify the underlying harmonic force constants and thus increasing temperature will not get rid of the warning.