CederGroupHub / chgnet

Pretrained universal neural network potential for charge-informed atomistic modeling https://chgnet.lbl.gov
https://doi.org/10.1038/s42256-023-00716-3
Other
215 stars 55 forks source link

[Bug]: magmom is None #113

Closed emilannevelink closed 5 months ago

emilannevelink commented 5 months ago

Email (Optional)

emilannevelink@hotmail.com

Version

v0.3.3

Which OS(es) are you using?

What happened?

I am attempting to assign the charge state based on the md example.

when I use struct_with_chg = solve_charge_by_mag(structure)

I get the following error

Traceback (most recent call last):
  File "/Users/Emil/MLIP-GEN-tests/chgnet/test/print_md.py", line 18, in <module>
    struct_with_chg = solve_charge_by_mag(structure)
  File "/Users/Emil/anaconda3/envs/chgnet2/lib/python3.9/site-packages/chgnet/utils/vasp_utils.py", line 196, in solve_charge_by_mag
    if mag[idx] >= min_mag and mag[idx] < max_mag:
TypeError: 'NoneType' object is not subscriptable

I believe this is because there is no initial magmom specified and so magmom is not a site property.

In vasp_utils.py, lines 187-189 in solve_charge_by_mag could be changed to

mag = structure.site_properties.get(
        "final_magmom", structure.site_properties.get("magmom")
    )

to solve this issue.

I'm curious if this introduces other issues that I'm unaware of.

Code snippet

from chgnet.model.model import CHGNet
from chgnet.model.dynamics import MolecularDynamics
from pymatgen.core import Structure
import warnings
warnings.filterwarnings("ignore", module="pymatgen")
warnings.filterwarnings("ignore", module="ase")

structure = Structure.from_file("data/mp-18767-LiMnO2.cif")
chgnet = CHGNet.load()

md = MolecularDynamics(
    atoms=structure,
    model=chgnet,
    ensemble="nvt",
    temperature=1000,  # in K
    timestep=2,  # in femto-seconds
    trajectory="md_out.traj",
    logfile="md_out.log",
    loginterval=100,
)
md.run(50) 

from ase.io.trajectory import Trajectory
from pymatgen.io.ase import AseAtomsAdaptor
from chgnet.utils import solve_charge_by_mag

traj = Trajectory("md_out.traj")
mag = traj[-1].get_magnetic_moments()
print(mag)

# get the non-charge-decorated structure
structure = AseAtomsAdaptor.get_structure(traj[-1])
print(structure)

# get the charge-decorated structure
struct_with_chg = solve_charge_by_mag(structure)
print(struct_with_chg)

Log output

Traceback (most recent call last):
  File "/Users/Emil/MLIP-GEN-tests/chgnet/test/print_md.py", line 18, in <module>
    struct_with_chg = solve_charge_by_mag(structure)
  File "/Users/Emil/anaconda3/envs/chgnet2/lib/python3.9/site-packages/chgnet/utils/vasp_utils.py", line 196, in solve_charge_by_mag
    if mag[idx] >= min_mag and mag[idx] < max_mag:
TypeError: 'NoneType' object is not subscriptable

Code of Conduct

janosh commented 5 months ago

thanks for reporting and suggesting a fix! 👍