calculators

Examples of using calculators:

###############################################################################
# Detect GPU card
###############################################################################
import torch
if torch.cuda.is_available():
    device='cuda'
    print("CUDA is available.")
    print(f"Number of GPUs: {torch.cuda.device_count()}")
    print(f"GPU Name: {torch.cuda.get_device_name(0)}")
else:
    device='cpu'
    print("CUDA is NOT available.")

import dgl
u, v = torch.tensor([0, 0, 0, 1], device=device), torch.tensor([1, 2, 3, 3],
                                                               device=device)
g = dgl.graph((u, v), device=device)
print(f'DGL graph device: {g.device}')

###############################################################################
# AGAT calculators with torch
###############################################################################
from agat.app.calculators import AgatCalculator, AgatCalculatorAseGraphTorch
from agat.app.calculators import AgatCalculatorAseGraphTorchNumpy
from agat.app.calculators import AgatEnsembleCalculator, OnTheFlyCalculator
import os
from ase.io import read, write

# --- 1 --- AgatCalculator
model_save_dir = os.path.join('potential_models', 'agat_model_1')
graph_build_scheme_dir = os.path.join('potential_models')
atoms = read(os.path.join('potential_models', 'POSCAR'))
calculator=AgatCalculator(model_save_dir, graph_build_scheme_dir,
                          device=device)
calculator.calculate(atoms)
print(calculator.results)

# --- 2 --- AgatCalculatorAseGraphTorch
model_save_dir = os.path.join('potential_models', 'agat_model_1')
graph_build_scheme_dir = os.path.join('potential_models')
atoms = read(os.path.join('potential_models', 'POSCAR'))
calculator=AgatCalculatorAseGraphTorch(model_save_dir, graph_build_scheme_dir,
                                       device=device)
calculator.calculate(atoms)
print(calculator.results)

# --- 3 --- AgatCalculatorAseGraphTorchNumpy
model_save_dir = os.path.join('potential_models', 'agat_model_1')
graph_build_scheme_dir = os.path.join('potential_models')
atoms = read(os.path.join('potential_models', 'POSCAR'))
calculator=AgatCalculatorAseGraphTorchNumpy(model_save_dir,
                                            graph_build_scheme_dir,
                                            device=device)
calculator.calculate(atoms)
print(calculator.results)

# --- 4 --- AgatEnsembleCalculator
model_ensemble_dir = os.path.join('potential_models')
graph_build_scheme_dir = os.path.join('potential_models')
atoms = read(os.path.join('potential_models', 'POSCAR'))
calculator=AgatEnsembleCalculator(model_ensemble_dir, graph_build_scheme_dir,
                                  device=device)
calculator.calculate(atoms)
print(calculator.results)

# --- 5 --- OnTheFlyCalculator
# model_ensemble_dir = os.path.join('potential_models')
# graph_build_scheme_dir = os.path.join('potential_models')
# atoms = read(os.path.join('potential_models', 'POSCAR'))
# calculator=OnTheFlyCalculator(model_ensemble_dir, graph_build_scheme_dir,
#                                   device=device)
# calculator.calculate(atoms)
# print(calculator.results)
class AgatCalculator(Calculator)

Deploy AGAT model on ase.calculators for geometry optimization and molecular dynamics simulations.

Note

Go to https://wiki.fysik.dtu.dk/ase/development/calculators.html#adding-new-calculators for more information about ase.calculators

implemented_properties
['energy', 'forces', 'stress']
default_parameters
{}
ignored_changes
set()
__init__(self, model_save_dir, graph_build_scheme_dir, graph_build_scheme, device = 'cuda', \**kwargs)
Parameters:
  • model_save_dir (str) – Directory storing the well-trained model.

  • graph_build_scheme_dir (str :param graph_build_scheme: Direcotry storing the graph_build_scheme.json file or parse the input dict. Note that this argument has higher priority than graph_build_scheme_dir.) – Direcotry storing the graph_build_scheme.json file.

  • device (str, optional) – model device, defaults to ‘cuda’

  • **kwargs (dict) – other input arguments

Returns:

Calculated properties.

Return type:

dict

load_graph_build_scheme(self, path)

Load graph building scheme.

Note

This file is normally saved to the disk when you build your dataset, under the same directory containing all_graphs.bin.

Parameters:

path (str) – Directory for storing graph_build_scheme.json file.

Returns:

A dict denotes how to build the graph.

Return type:

dict

calculate(self, atoms=None, properties=None, system_changes=['positions', 'numbers', 'cell', 'pbc'])
Parameters:
  • atoms (ase.atoms, optional) – ase.atoms object, defaults to None

  • properties (none, optional) – calculated properties, defaults to None

  • system_changes (TYPE, optional) – DESCRIPTION, defaults to [‘positions’, ‘numbers’, ‘cell’, ‘pbc’]

Returns:

calculated results

Return type:

dict

class AgatCalculatorAseGraphTorch(Calculator)

Deploy AGAT model on ase.calculators for geometry optimization and molecular dynamics simulations.

Hint

This object builds dgl graphs with modified ase codes that leverage GPU resources: AseGraphTorch, and much faster than original ase method on CPU. See https://github.com/jzhang-github/AGAT/blob/main/agat/data/build_graph.py#L383

Note

Go to https://wiki.fysik.dtu.dk/ase/development/calculators.html#adding-new-calculators for more information about ase.calculators

implemented_properties
['energy', 'forces', 'stress']
default_parameters
{}
ignored_changes
set()
__init__(self, model_save_dir, graph_build_scheme_dir, graph_build_scheme, device = 'cuda', \**kwargs)
Parameters:
  • model_save_dir (str) – Directory storing the well-trained model.

  • graph_build_scheme_dir (str :param graph_build_scheme: Direcotry storing the graph_build_scheme.json file or parse the input dict. Note that this argument has higher priority than graph_build_scheme_dir.) – Direcotry storing the graph_build_scheme.json file.

  • device (str, optional) – model device, defaults to ‘cuda’

  • **kwargs (dict) – other input arguments

Returns:

Calculated properties.

Return type:

dict

Example:

model_save_dir = 'agat_model'
graph_build_scheme_dir = 'dataset'
atoms = read('CONTCAR')
calculator=AgatCalculator(model_save_dir,
                          graph_build_scheme_dir)
atoms = Atoms(atoms, calculator=calculator)
dyn = BFGS(atoms, trajectory='test.traj')
dyn.run(fmax=0.005)

traj = read('test.traj', index=':')
write("XDATCAR.gat", traj)
load_graph_build_scheme(self, path)

Load graph building scheme.

Note

This file is normally saved to the disk when you build your dataset, under the same directory containing all_graphs.bin.

Parameters:

path (str) – Directory for storing graph_build_scheme.json file.

Returns:

A dict denotes how to build the graph.

Return type:

dict

calculate(self, atoms=None, properties=None, system_changes=['positions', 'numbers', 'cell', 'pbc'])
Parameters:
  • atoms (ase.atoms, optional) – ase.atoms object, defaults to None

  • properties (none, optional) – calculated properties, defaults to None

  • system_changes (TYPE, optional) – DESCRIPTION, defaults to [‘positions’, ‘numbers’, ‘cell’, ‘pbc’]

Returns:

calculated results

Return type:

dict

Note

The outputs are torch.Tensor s.

class AgatCalculatorAseGraphTorchNumpy(Calculator)

Deploy AGAT model on ase.calculators for geometry optimization and molecular dynamics simulations.

Hint

This object builds dgl graphs with modified ase codes that leverage GPU resources: AseGraphTorch, and much faster than original ase method on CPU. See https://github.com/jzhang-github/AGAT/blob/main/agat/data/build_graph.py#L383

Note

Go to https://wiki.fysik.dtu.dk/ase/development/calculators.html#adding-new-calculators for more information about ase.calculators

implemented_properties
['energy', 'forces', 'stress']
default_parameters
{}
ignored_changes
set()
__init__(self, model_save_dir, graph_build_scheme_dir, graph_build_scheme, device='cuda', **kwargs)
Parameters:
  • model_save_dir (str) – Directory storing the well-trained model.

  • graph_build_scheme_dir (str :param graph_build_scheme: Direcotry storing the graph_build_scheme.json file or parse the input dict. Note that this argument has higher priority than graph_build_scheme_dir.) – Direcotry storing the graph_build_scheme.json file.

  • device (str, optional) – model device, defaults to ‘cuda’

  • **kwargs (dict) – other input arguments

Returns:

Calculated properties.

Return type:

dict

Example:

model_save_dir = 'agat_model'
graph_build_scheme_dir = 'dataset'
atoms = read('CONTCAR')
calculator=AgatCalculator(model_save_dir,
                          graph_build_scheme_dir)
atoms = Atoms(atoms, calculator=calculator)
dyn = BFGS(atoms, trajectory='test.traj')
dyn.run(fmax=0.005)

traj = read('test.traj', index=':')
write("XDATCAR.gat", traj)
load_graph_build_scheme(self, path)

Load graph building scheme.

Note

This file is normally saved to the disk when you build your dataset, under the same directory containing all_graphs.bin.

Parameters:

path (str) – Directory for storing graph_build_scheme.json file.

Returns:

A dict denotes how to build the graph.

Return type:

dict

calculate(self, atoms=None, properties=None, system_changes=['positions', 'numbers', 'cell', 'pbc'])
Parameters:
  • atoms (ase.atoms, optional) – ase.atoms object, defaults to None

  • properties (none, optional) – calculated properties, defaults to None

  • system_changes (TYPE, optional) – DESCRIPTION, defaults to [‘positions’, ‘numbers’, ‘cell’, ‘pbc’]

Returns:

calculated results

Return type:

dict

Note

The outputs are numpy.array s.

class AgatEnsembleCalculator(Calculator)

Deploy AGAT model on ase.calculators for geometry optimization and molecular dynamics simulations.

Hint

This object is used to calculate atomic energy, forces, and cell stresses with multiples models.

Note

Go to https://wiki.fysik.dtu.dk/ase/development/calculators.html#adding-new-calculators for more information about ase.calculators

implemented_properties
['energy', 'forces', 'stress']
default_parameters
{}
ignored_changes
set()
__init__(model_ensemble_dir, graph_build_scheme_dir=None, graph_build_scheme=None, start_step=0, device='cuda', io=None, **kwargs)
Parameters:
  • model_ensemble_dir (str) – Directory storing the well-trained models.

  • graph_build_scheme_dir (str :param graph_build_scheme: Direcotry storing the graph_build_scheme.json file or parse the input dict. Note that this argument has higher priority than graph_build_scheme_dir.) – Direcotry storing the graph_build_scheme.json file.

  • device (str, optional :param io: Unknown. May be useful for logging in the future.) – model device, defaults to ‘cuda’

  • **kwargs (dict) – other input arguments

Returns:

Calculated properties.

Return type:

dict

Note

graph_build_scheme has higher priority than graph_build_scheme_dir.

Example:

model_save_dir = 'agat_model'
graph_build_scheme_dir = 'dataset'
atoms = read('CONTCAR')
calculator=AgatCalculator(model_save_dir,
                          graph_build_scheme_dir)
atoms = Atoms(atoms, calculator=calculator)
dyn = BFGS(atoms, trajectory='test.traj')
dyn.run(fmax=0.005)

traj = read('test.traj', index=':')
write("XDATCAR.gat", traj)
load_graph_build_scheme(self, path)

Load graph building scheme.

Note

This file is normally saved to the disk when you build your dataset, under the same directory containing all_graphs.bin.

Parameters:

path (str) – Directory for storing graph_build_scheme.json file.

Returns:

A dict denotes how to build the graph.

Return type:

dict

calculate(self, atoms=None, properties=None, system_changes=['positions', 'numbers', 'cell', 'pbc'])
Parameters:
  • atoms (ase.atoms, optional) – ase.atoms object, defaults to None

  • properties (none, optional) – calculated properties, defaults to None

  • system_changes (TYPE, optional) – DESCRIPTION, defaults to [‘positions’, ‘numbers’, ‘cell’, ‘pbc’]

Returns:

calculated results

Return type:

dict

class OnTheFlyCalculator(Calculator)

Deploy AGAT model on ase.calculators for geometry optimization and molecular dynamics simulations.

For the on-the-fly training of a agat.model.PotentialModel.

Note

Go to https://wiki.fysik.dtu.dk/ase/development/calculators.html#adding-new-calculators for more information about ase.calculators

implemented_properties
['energy', 'forces', 'stress']
default_parameters
{}
ignored_changes
set()
__init__(self, model_save_dir, graph_build_scheme, use_vasp=False, start_step=0, vasp_work_dir='.', vasp_inputs_dir='.', gamma_only=False, vasp_potential_generator='getpotential.sh', vasp_script='vasp_run.sh', device = 'cuda', energy_threshold = 0.5, force_threshold = 0.5, stress_threshold = 0.5, io=None, \**kwargs)
Parameters:
  • model_save_dir – Directory storing the well-trained model.

  • device (str, optional :param io: Unknown.) – model device, defaults to ‘cuda’

  • **kwargs (dict) – other input arguments

Returns:

Calculated properties.

Return type:

dict

Note

graph_build_scheme has higher priority than graph_build_scheme_dir.

Example:

model_save_dir = 'agat_model'
graph_build_scheme_dir = 'dataset'
atoms = read('CONTCAR')
calculator=AgatCalculator(model_save_dir,
                          graph_build_scheme_dir)
atoms = Atoms(atoms, calculator=calculator)
dyn = BFGS(atoms, trajectory='test.traj')
dyn.run(fmax=0.005)

traj = read('test.traj', index=':')
write("XDATCAR.gat", traj)
load_graph_build_scheme(self, path)

Load graph building scheme.

Note

This file is normally saved to the disk when you build your dataset, under the same directory containing all_graphs.bin.

Parameters:

path (str) – Directory for storing graph_build_scheme.json file.

Returns:

A dict denotes how to build the graph.

Return type:

dict

calculate(self, atoms=None, properties=None, system_changes=['positions', 'numbers', 'cell', 'pbc'])
Parameters:
  • atoms (ase.atoms, optional) – ase.atoms object, defaults to None

  • properties (none, optional) – calculated properties, defaults to None

  • system_changes (TYPE, optional) – DESCRIPTION, defaults to [‘positions’, ‘numbers’, ‘cell’, ‘pbc’]

Returns:

calculated results

Return type:

dict

Note

The outputs are numpy.array s.