Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

❓ [QUESTION]how can I use the ase calculator for testing ? #401

Open
StellarOdys2ey opened this issue Jan 10, 2024 · 1 comment
Open
Labels
question Further information is requested

Comments

@StellarOdys2ey
Copy link

I have deployed the result of a training session into a pth model and hope to test it by ASE calculator for some testing data of xyz format, but I don't know how to use the nequip_calculator in the folder named nequip/train,where can I find some tutorials?

@StellarOdys2ey StellarOdys2ey added the question Further information is requested label Jan 10, 2024
@hezhengda
Copy link

Hey, @StellarOdys2ey ,

Don't know whether it will still help you or not. Here's my code on how to evaluate the model from NequIP.

#!/usr/bin/env python3

"""
This script evaluates a NequIP model by comparing its predictions with DFT calculations.
It reads an XYZ file containing atomic structures, uses a deployed NequIP model to predict
energies and forces, and then calculates the Root Mean Square Error (RMSE) between the 
predicted and actual forces. Additionally, it computes the correlation coefficient between 
the predicted and actual energies.

Usage:
    python evaluate.py --test_file <path_to_xyz_file> --model_path <path_to_model>

Arguments:
    --test_file: Path to the XYZ file containing atomic structures for testing.
    --model_path: Path to the deployed NequIP model.
"""

# To-DO: 
# 1. How to use multiple GPUs for faster evaluation? The number of GPUs can be set as an argument.

from tqdm import tqdm
import numpy as np
from ase.io import read
from nequip.ase import NequIPCalculator
import math
import argparse

def calculate_rmse(list1, list2):
    # Ensure both lists have the same structure
    assert len(list1) == len(list2) and all(len(sublist1) == len(sublist2) for sublist1, sublist2 in zip(list1, list2)), "Lists must have the same structure."
    
    # Initialize sum of squared differences
    total_squared_diff = 0
    total_elements = 0
    
    # Loop through both lists
    for sublist1, sublist2 in zip(list1, list2):
        for val1, val2 in zip(sublist1, sublist2):
            total_squared_diff += (val1 - val2) ** 2
            total_elements += 1
            
    # Calculate RMSE
    rmse = math.sqrt(total_squared_diff / total_elements)
    return rmse

if __name__=='__main__':

    parser = argparse.ArgumentParser(description='Evaluate NequIP model')
    parser.add_argument('--test_file', type=str, required=True, help='Path to the xyz file for testing')
    parser.add_argument('--model_path', type=str, required=True, help='Path to the deployed model')
    args = parser.parse_args()

    lst_atoms = read(args.test_file, index=':')
    
    nequip_calc = NequIPCalculator.from_deployed_model(model_path=args.model_path)
    
    lst_e_dft = []
    lst_e_nequip = []
    
    lst_f_dft = []
    lst_f_nequip = []
    
    for atoms in tqdm(lst_atoms):
        lst_e_dft.append(atoms.get_potential_energy())
        lst_f_dft.append(atoms.get_forces().tolist()[0])
    
        # reset the calculation
        atoms.calc = nequip_calc
        lst_e_nequip.append(atoms.get_potential_energy())
        lst_f_nequip.append(atoms.get_forces().tolist()[0])
        
    np_e_dft = np.array(lst_e_dft)
    np_e_nequip = np.array(lst_e_nequip)
    
    print(np.corrcoef(np_e_dft, np_e_nequip))
    print(f'The RMSE between forces is: {calculate_rmse(lst_f_dft, lst_f_nequip)}eV/atom')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants