BFGS optimization with LAMMPS using multiprocessors

I have been trying to use the BFGS optimizer in ASE with LAMMPS as a calculator to optimize a structure. The BFGS optimizer works fine when using a single processor (“ASE_LAMMPSRUN_COMMAND” =“mpirun -np 1 lmp_mpi”) but fails to do so (i.e., producing weird forces and energies in the optimization steps which never stop) when using multiple processor (i.e., “ASE_LAMMPSRUN_COMMAND” =“mpirun -np 2 lmp_mpi” . However, LAMMPS can calculate just the energy well using multiple processors when BFGS optimizer is not called. The LAMMPS executable are downloaded from the conda-forge channel. How can I successfully use multiple processors when performing BFGS optimization. I have provided below a snippet of the code that can reproduce what I said above.

import numpy as np
import ase
import os
from ase import Atom, Atoms
from ase import build
from import bulk
from ase.calculators.lammpsrun import LAMMPS
from ase.optimize import BFGS

os.environ["ASE_LAMMPSRUN_COMMAND"]="mpirun -np 2 lmp_mpi"
parameters = {'pair_style': 'eam/alloy',
              'pair_coeff': ['* * NiAlH_jea.eam.alloy H Ni']}

files = ['NiAlH_jea.eam.alloy']

Ni = bulk('Ni', cubic=True)

#Applying small perturbation to H away from the diagonal
H = Atom('H', position=Ni.cell.diagonal()/2 + 0.001)
NiH = Ni + H
multiplier = np.identity(3) * 2, multiplier)

lammps = LAMMPS(parameters=parameters, files=files)

#relax the supercell
NiH_sc.calc = lammps

print("Energy ", NiH_sc.get_potential_energy())

I’m not a lammps expert. The lammps handle which the calculator uses internally is a process, which could be lammps, but in this case it would be MPI. Communication with the subprocess is done over stdin/stdout. What the lammps process receives then depends on how MPI forwards stdin and stdout to the ranks.


  • Maybe lammps has documentation on how stdin interacts with MPI.
  • Maybe MPI has options to ensure that input is received by all processes, or by only one process, and one of those is the right choice
  • Maybe I don’t know what I’m talking about :slight_smile: