deepmodeling / abacus-develop

An electronic structure package based on either plane wave basis or numerical atomic orbitals.
http://abacus.ustc.edu.cn
GNU Lesser General Public License v3.0
151 stars 122 forks source link

ERROR when using ASE-ABACUS with different ABACUS version #4471

Open Luc1anoo opened 2 weeks ago

Luc1anoo commented 2 weeks ago

Describe the bug

I'm performing the computation of structure relaxation of Ethanol by ASE-ABACUS and ABACUS. I use the following script model.py and relax.py to create model of Ethanol and do the relaxation computation. The computation can be done successfully with ABACUS version of 3.6.0, but will throw the following error when running the relax.py after changing the ABACUS version to 3.6.3. It seems something wrong with reading the structure of Ethanol.

Traceback (most recent call last):
  File "/2/relax/relax.py", line 58, in <module>
    dyn = QuasiNewton(stru, trajectory='Ethanol.traj')
  File "/opt/mamba/lib/python3.10/site-packages/ase/optimize/bfgslinesearch.py", line 84, in __init__
    Optimizer.__init__(self, atoms, restart, logfile, trajectory,
  File "/opt/mamba/lib/python3.10/site-packages/ase/optimize/optimize.py", line 236, in __init__
    self.set_force_consistent()
  File "/opt/mamba/lib/python3.10/site-packages/ase/optimize/optimize.py", line 336, in set_force_consistent
    self.atoms.get_potential_energy(force_consistent=True)
  File "/opt/mamba/lib/python3.10/site-packages/ase/atoms.py", line 727, in get_potential_energy
    energy = self._calc.get_potential_energy(
  File "/opt/mamba/lib/python3.10/site-packages/ase/calculators/abc.py", line 24, in get_potential_energy
    return self.get_property(name, atoms)
  File "/opt/mamba/lib/python3.10/site-packages/ase/calculators/calculator.py", line 502, in get_property
    self.calculate(atoms, [name], system_changes)
  File "/opt/mamba/lib/python3.10/site-packages/ase/calculators/genericfileio.py", line 142, in calculate
    self.results = self.template.read_results(self.directory)
  File "/opt/mamba/lib/python3.10/site-packages/ase/calculators/abacus.py", line 139, in read_results
    return read_abacus_results(open(path / f'running_{self.cal_name}.log', 'r'),
  File "/opt/mamba/lib/python3.10/site-packages/ase/utils/__init__.py", line 487, in iofunc
    obj = func(fd, *args, **kwargs)
  File "/opt/mamba/lib/python3.10/site-packages/ase/io/abacus.py", line 1784, in read_abacus_results
    chunks = _get_abacus_chunks(fd, index, non_convergence_ok)
  File "/opt/mamba/lib/python3.10/site-packages/ase/utils/__init__.py", line 487, in iofunc
    obj = func(fd, *args, **kwargs)
  File "/opt/mamba/lib/python3.10/site-packages/ase/io/abacus.py", line 1759, in _get_abacus_chunks
    final_chunk = AbacusOutCalcChunk(calc_contents, header_chunk, -1)
  File "/opt/mamba/lib/python3.10/site-packages/ase/io/abacus.py", line 1139, in __init__
    self._header = header.header_summary
  File "/opt/mamba/lib/python3.10/site-packages/ase/utils/__init__.py", line 588, in getter
    cache[name] = meth(self)
  File "/opt/mamba/lib/python3.10/site-packages/ase/io/abacus.py", line 1106, in header_summary
    "initial_atoms": self.initial_atoms,
  File "/opt/mamba/lib/python3.10/site-packages/ase/utils/__init__.py", line 588, in getter
    cache[name] = meth(self)
  File "/opt/mamba/lib/python3.10/site-packages/ase/io/abacus.py", line 993, in initial_atoms
    labels, positions, mag, vel = self.initial_site
  File "/opt/mamba/lib/python3.10/site-packages/ase/utils/__init__.py", line 588, in getter
    cache[name] = meth(self)
  File "/opt/mamba/lib/python3.10/site-packages/ase/io/abacus.py", line 987, in initial_site
    return parse_block(self._parse_site()[0])
  File "/opt/mamba/lib/python3.10/site-packages/ase/io/abacus.py", line 975, in parse_block
    labels, pos, mag, vel = str_to_sites(site)
  File "/opt/mamba/lib/python3.10/site-packages/ase/io/abacus.py", line 960, in str_to_sites
    labels = val[:, 0]
IndexError: too many indices for array: array is 1-dimensional, but 2 were indexed

Expected behavior

No response

To Reproduce

model.py:

from ase.io import read, write
from pathlib import Path
from ase.collections import g2
import numpy as np

# get Ethanol model
atoms = g2['CH3CH2OH']
# translation
translation_vector = np.array([5,5,5])
atoms.positions += translation_vector
# export as cif
write('ethanol.cif', atoms)
# cif to STRU
cs_dir = './'
cs_vasp = Path(cs_dir, 'ethanol.cif')
cs_atoms = read(cs_vasp, format='cif')
cs_stru = Path(cs_dir, 'STRU')
pp = {
        'H':'H_ONCV_PBE-1.0.upf',
        'C':'C_ONCV_PBE-1.0.upf',
        'O':'O_ONCV_PBE-1.0.upf',
      }
basis = {
        'H': 'H_gga_8au_100Ry_2s1p.orb',
        'C': 'C_gga_8au_100Ry_2s2p1d.orb',
        'O': 'O_gga_7au_100Ry_2s2p1d.orb',
        }
write(cs_stru, cs_atoms, format='abacus', pp=pp, basis=basis)

relax.py:

import os
from ase.optimize import QuasiNewton
from ase.io import read, write
from ase.io import Trajectory
from ase.visualize import view
from ase.calculators.abacus import Abacus, AbacusProfile
from pathlib import Path

# env setting
os.environ['ABACUS_PP_PATH'] = '/abacus-develop/tests/PP_ORB'
os.environ['ABACUS_ORBITAL_PATH'] = '/abacus-develop/tests/PP_ORB'
print(os.environ.get('ABACUS_PP_PATH'))
print(os.environ.get('ABACUS_ORBITAL_PATH'))
stru = read('STRU', format='abacus')
pseudo_dir = os.environ['ABACUS_PP_PATH']
basis_dir = os.environ['ABACUS_ORBITAL_PATH']
out_dir = "OUT"
properties = ["energy", "forces", "stress"]
pp = {
        'H':'H_ONCV_PBE-1.0.upf',
        'C':'C_ONCV_PBE-1.0.upf',
        'O':'O_ONCV_PBE-1.0.upf',
      }
basis = {
        'H': 'H_gga_8au_100Ry_2s1p.orb',
        'C': 'C_gga_8au_100Ry_2s2p1d.orb',
        'O': 'O_gga_7au_100Ry_2s2p1d.orb',
        }
kpts = [1, 1, 1]
# INPUT setting
parameters = {
    'suffix': 'Ethanol',
    'calculation': 'scf',
    'basis_type': 'lcao',
    'ks_solver': 'genelpa',
    'nspin' : 1,
    'xc': 'pbe',
    'ecutwfc': 50,
    'scf_thr': 1e-7,
    'kpts': kpts,
    'pp': pp,
    'basis': basis,
    'pseudo_dir': pseudo_dir,
    'basis_dir': basis_dir,
    'smearing_method': 'gauss',
    'smearing_sigma': 0.0037,
    'cal_force': 1,
    'cal_stress': 1,
    'out_stru': 1,
    'out_chg': 0,
}

# attch calculator to the model
profile = AbacusProfile(argv=['mpirun','-n','32','abacus'])
stru.calc = Abacus(profile=profile, directory=out_dir, **parameters)

# relax
dyn = QuasiNewton(stru, trajectory='Ethanol.traj')
dyn.run(fmax=0.05)
print('Energy:', stru.get_potential_energy())

# read the relaxed stru in the output of ABACUS and then write it in extxyz format and STRU format
cs_dir = './OUT/OUT.Ethanol/'
cs_stru_output = Path(cs_dir, 'running_scf.log')
cs_atoms= read(cs_stru_output, format='abacus-out')
cs_vasp = Path(cs_dir, 'relax_traj.extxyz')
write(cs_vasp, cs_atoms, format='extxyz')
cs_stru = Path(cs_dir, 'STRU_relax')
write(cs_stru, cs_atoms, format='abacus', pp=pp, basis=basis)

Environment

version: ASE-ABACUS: ase-3.23.0b1 ABACUS: 3.6.0, 3.6.3

Additional Context

No response

Task list for Issue attackers (only for developers)

WHUweiqingzhou commented 1 week ago

@1041176461 could you have a look?

QuantumMisaka commented 1 week ago

@Luc1anoo Have you tried the latest ASE-ABACUS interface ?

1041176461 commented 1 week ago

We update the ase-abacus version to ase-3.23.1b1, please check the newest commit. I check read function for reading 'running_cell-relax.log' output by ABACUS v3.6.5 , it runs successfully.

Luc1anoo commented 5 days ago

We update the ase-abacus version to ase-3.23.1b1, please check the newest commit. I check read function for reading 'running_cell-relax.log' output by ABACUS v3.6.5 , it runs successfully.

I updated the ase-abacus version. This time the read function worked well. However, the calculator seemed to fail with the following error:

(base) root@bohrium-17819-1150041:/personal/TEST/2/relax# python3 relax.py 
/abacus-develop/tests/PP_ORB
/abacus-develop/tests/PP_ORB
Traceback (most recent call last):
  File "/personal/TEST/2/relax/relax.py", line 59, in <module>
    dyn.run(fmax=0.05)
  File "/opt/mamba/lib/python3.10/site-packages/ase/optimize/optimize.py", line 430, in run
    return Dynamics.run(self, steps=steps)
  File "/opt/mamba/lib/python3.10/site-packages/ase/optimize/optimize.py", line 275, in run
    for converged in Dynamics.irun(self, steps=steps):
  File "/opt/mamba/lib/python3.10/site-packages/ase/optimize/optimize.py", line 225, in irun
    self.optimizable.get_forces()
  File "/opt/mamba/lib/python3.10/site-packages/ase/optimize/optimize.py", line 34, in get_forces
    return self.atoms.get_forces()
  File "/opt/mamba/lib/python3.10/site-packages/ase/atoms.py", line 818, in get_forces
    forces = self._calc.get_forces(self)
  File "/opt/mamba/lib/python3.10/site-packages/ase/calculators/abc.py", line 30, in get_forces
    return self.get_property('forces', atoms)
  File "/opt/mamba/lib/python3.10/site-packages/ase/calculators/calculator.py", line 538, in get_property
    self.calculate(atoms, [name], system_changes)
  File "/opt/mamba/lib/python3.10/site-packages/ase/calculators/genericfileio.py", line 321, in calculate
    self.write_inputfiles(atoms, properties)
  File "/opt/mamba/lib/python3.10/site-packages/ase/calculators/genericfileio.py", line 312, in write_inputfiles
    self.template.write_input(
TypeError: AbacusTemplate.write_input() got an unexpected keyword argument 'profile'

The snippet of my script relax.py is as follows with other parts remaining unchanged:

abacus = '/usr/local/bin/abacus'
profile = AbacusProfile('mpirun -n 2 abacus')
stru.calc = Abacus(profile=profile, directory=out_dir, **parameters)
QuantumMisaka commented 4 days ago

@1041176461 I've repeated the same error above, the problem seems to be in write_input function. Also, I have no idea about the change of usage of AbacusProfile until I saw this issue. May you give a look and give us the right way to use ase-abacus interface after bug fixed ?

1041176461 commented 4 days ago

I fixed it in the commit https://gitlab.com/1041176461/ase-abacus/-/commit/80e300311f90e939f606068995d6ae61d13f645b, please test it again @QuantumMisaka @Luc1anoo

QuantumMisaka commented 4 days ago

@1041176461 There are still other problems:

  1. a redundant comma is left
    Traceback (most recent call last):
    File "/lustre/home/2201110432/WORK/FeCHO-SCAN/Fe5C2-CO-react/clean/new-orb/PBE/vib/vib_analysis.py", line 10, in <module>
    from ase.calculators.abacus import Abacus, AbacusProfile
    File "/lustre/home/2201110432/apps/miniconda3/envs/ase/lib/python3.10/site-packages/ase/calculators/abacus.py", line 16
    from ase.io.abacus import write_abacus, copy_basis, copy_offsite_basis, copy_pp, write_input, write_kpt,
                                                                                                            ^
    SyntaxError: trailing comma not allowed without surrounding parentheses
  2. After I fixed it myself, the ase-abacus interface can work well with specifying the right pseudo_dir , orbital_dir , pp and basis.

So the comma need to be fixed.

1041176461 commented 4 days ago

We fixed the bug @QuantumMisaka metioned above and error for basis and pp environment varible settings. I modify your script @Luc1anoo and add some comments, please test it again based on this one.

import os
from pathlib import Path

import numpy as np
from ase.calculators.abacus import Abacus, AbacusProfile
from ase.collections import g2
from ase.io import Trajectory, read, write
from ase.optimize import QuasiNewton
from ase.visualize import view

# get Ethanol model
atoms = g2['CH3CH2OH']
# translation
translation_vector = np.array([5,5,5])
atoms.positions += translation_vector
atoms.center(vacuum=5)

# 不需要来回转换文件,直接使用atoms即可
out_dir = "OUT"
properties = ["energy", "forces", "stress"]

# pseudo_dir和basis_dir可自动从ABACUS_PP_PATH和ABACUS_ORBITAL_PATH获取
# 已支持默认pp和basis,详情见issue:https://gitlab.com/1041176461/ase-abacus/-/issues/30
kpts = [1, 1, 1]
# INPUT setting
parameters = {
    'suffix': 'Ethanol',
    'calculation': 'scf',
    'basis_type': 'lcao',
    'ks_solver': 'genelpa',
    'nspin' : 1,
    'xc': 'pbe',
    'ecutwfc': 50,
    'scf_thr': 1e-7,
    'kpts': kpts,
    'smearing_method': 'gauss',
    'smearing_sigma': 0.0037,
    'cal_force': 1,
    'cal_stress': 1,
    'out_stru': 1,
    'out_chg': 0,
}

# attch calculator to the model
# 当前版本使用command=字符串
profile = AbacusProfile(command='mpirun -n 1 abacus')
atoms.calc = Abacus(profile=profile, directory=out_dir, **parameters)

#relax
dyn = QuasiNewton(atoms, trajectory='Ethanol.traj')
dyn.run(fmax=0.05)
print('Energy:', atoms.get_potential_energy())
QuantumMisaka commented 3 days ago

The ATST-Tools https://github.com/QuantumMisaka/ATST-Tools have been updated to 1.4.3 version, which march the newest version of ase-abacus interface

QuantumMisaka commented 3 days ago

@1041176461 Can one use the default PP and ORB while specifying pseudo_dir and orbital_dir at the same time ? In my test this way seems to fail. like:

lib_dir = "/lustre/home/2201110432/example/abacus"
pseudo_dir = f"{lib_dir}/PP"
basis_dir = f"{lib_dir}/ORB"
# default pp and basis is supported by ase-abacus interface
# pp = {
#       'C':'C_ONCV_PBE-1.0.upf',
#       'H':'H_ONCV_PBE-1.0.upf',
#       'Pt':'Pt_ONCV_PBE-1.0.upf',
#       }
# basis = {
#          'C': 'C_gga_7au_100Ry_2s2p1d.orb',
#          'H': 'H_gga_6au_100Ry_2s1p.orb',
#          'Pt': 'Pt_gga_7au_100Ry_4s2p2d1f.orb'
#          ,}
kpts = [2, 1, 2]
parameters = {
    'calculation': 'scf',
    'nspin': 2,
    'xc': 'pbe',
    'ecutwfc': 100,
    'dft_functional': 'pbe',
    'ks_solver': 'genelpa',
    'symmetry': 0,
    'vdw_method': 'd3_bj',
    'smearing_method': 'gaussian',
    'smearing_sigma': 0.001,
    'basis_type': 'lcao',
    'mixing_type': 'broyden',
    'scf_thr': 1e-6,
    'scf_nmax': 100,
    'kpts': kpts,
    'pseudo_dir': pseudo_dir,
    'basis_dir': basis_dir,
    'cal_force': 1,
    'cal_stress': 1,
    'init_wfc': 'atomic',
    'init_chg': 'atomic',
    'out_stru': 1,
    'out_chg': 0,
    'out_mul': 0,
    'out_wfc_lcao': 0,
    'out_bandgap': 0,
    'efield_flag': 1,
    'dip_cor_flag': 1,
    'efield_dir': 1,
}
    # 'pp': pp,
    # 'basis': basis,

And by this way ase-abacus will not copy PP and ORB to running directory

1041176461 commented 3 days ago

@QuantumMisaka In this case, pseudo_dir and basis_dir will be output into INPUT file. Here are the corresponding codes image

If you have any good suggestions, please submit a new MR to the Gitlab repo.

Luc1anoo commented 3 days ago

We update the ase-abacus version to ase-3.23.1b1, please check the newest commit. I check read function for reading 'running_cell-relax.log' output by ABACUS v3.6.5 , it runs successfully.

By the way, the initial error caused by read function still exists with the ABAUCS version of 3.6.3 (3.6.5 is well).

1041176461 commented 3 days ago

If v3.6.0 and v3.6.5 run successfully, I think there is a problem with the formatting of ABACUS v3.6.3