materialsvirtuallab / matgl

Graph deep learning library for materials
BSD 3-Clause "New" or "Revised" License
232 stars 57 forks source link

[Bug]: AttributeError: 'M3GNet' object has no attribute 'calc_stresses' #246

Closed dream123321 closed 3 months ago

dream123321 commented 3 months ago

Email (Optional)

2760344463@qq.com

Version

Version: 1.0.0

Which OS(es) are you using?

What happened?

I retrained a model by adding streess_weight=1 to the reference code. (lit_module = PotentialLightningModule(model=model,stress_weight=1)) However, by loading the self-trained model, the following errors occurred when performing structural optimization: AttributeError: 'M3GNet' object has no attribute 'calc_stresses' But it is normal to use pre-trained model "M3GNet-MP-2021.2.8-PES"

Code snippet

from pymatgen.core import Lattice, Structure
import matgl
from matgl.ext.ase import PESCalculator, MolecularDynamics, Relaxer
import warnings
from pymatgen.io.vasp.inputs import Poscar
warnings.filterwarnings('ignore')

def mlp():
    model_path = "/home/jinghuang/M3GNet/pretrained_models/trained_model"
    model = matgl.load_model(model_path)

    relaxer = Relaxer(potential=model)
    struct = Structure.from_file("POSCAR")
    # out = r"D:\HuaweiMoveData\Users\27603\Desktop\POSCAR"
    # struct.to(out)
    relax_results = relaxer.relax(struct, fmax=0.01)

    # extract results
    final_structure = relax_results["final_structure"]
    final_energy = relax_results["trajectory"].energies[-1]
    # print out the final relaxed structure and energy
    print('_____________________________________________________')
    print(struct)
    print(final_structure)
    print(f"The final energy is {float(final_energy):.3f} eV.")
    poscar = Poscar(final_structure)
    with open('log.tote', mode='w') as f:
        f.write(str(float(final_energy)))

    poscar.write_file("CONTCAR")

mlp()

Log output

Traceback (most recent call last):
  File "/home/jinghuang/BN_test/mlp_C4_EA_model_2/work/000000/ase_in.py", line 32, in <module>
    mlp()
  File "/home/jinghuang/BN_test/mlp_C4_EA_model_2/work/000000/ase_in.py", line 12, in mlp
    relaxer = Relaxer(potential=model)
  File "/home/jinghuang/anaconda3/envs/pytorch/lib/python3.10/site-packages/matgl/ext/ase.py", line 235, in __init__
    self.calculator = PESCalculator(
  File "/home/jinghuang/anaconda3/envs/pytorch/lib/python3.10/site-packages/matgl/ext/ase.py", line 146, in __init__
    self.compute_stress = potential.calc_stresses
  File "/home/jinghuang/anaconda3/envs/pytorch/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1688, in __getattr__
    raise AttributeError(f"'{type(self).__name__}' object has no attribute '{name}'")
AttributeError: 'M3GNet' object has no attribute 'calc_stresses'

Code of Conduct

kenko911 commented 3 months ago

Hi @dream123321, I think the error comes from the fact that you saved the M3GNet model class but not Potential class. Please just do lit_model.model.save() after finishing the potential training