Utilities Module¶
Utility functions for device management, I/O, and D3 correction.
Device Utilities¶
Device management utilities for MACE inference
get_device ¶
Get the appropriate device for computation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
device
|
DeviceType
|
Device specification ("auto", "cpu", or "cuda") |
'auto'
|
Returns:
| Type | Description |
|---|---|
str
|
Device string ("cpu" or "cuda") |
Raises:
| Type | Description |
|---|---|
ValueError
|
If CUDA is requested but not available |
Examples:
>>> device = get_device("auto") # Auto-detect
>>> device = get_device("cuda") # Force CUDA
>>> device = get_device("cpu") # Force CPU
Source code in src/mace_inference/utils/device.py
validate_device ¶
Validate that the specified device is available.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
device
|
str
|
Device string to validate |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If device is invalid or unavailable |
Source code in src/mace_inference/utils/device.py
get_device_info ¶
Get information about available compute devices.
Returns:
| Type | Description |
|---|---|
dict
|
Dictionary with device information |
Source code in src/mace_inference/utils/device.py
get_device¶
Determine the best available device for computation.
Parameters:
device(str): Device specification"auto": Automatically select best available"cpu": Force CPU"cuda": Use CUDA GPU"cuda:0","cuda:1": Specific GPU"mps": Apple Silicon GPU
Returns: str - The device string to use
Example:
from mace_inference.utils import get_device
device = get_device("auto")
print(f"Using device: {device}") # e.g., "cuda" or "cpu"
I/O Utilities¶
I/O utilities for structure handling
load_structure ¶
Load atomic structure from file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filepath
|
Union[str, Path]
|
Path to structure file (CIF, POSCAR, XYZ, etc.) |
required |
index
|
int
|
Frame index for trajectory files (-1 for last frame) |
-1
|
Returns:
| Type | Description |
|---|---|
Atoms
|
ASE Atoms object |
Examples:
>>> atoms = load_structure("structure.cif")
>>> atoms = load_structure("trajectory.traj", index=0) # First frame
Source code in src/mace_inference/utils/io.py
save_structure ¶
Save atomic structure to file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
atoms
|
Atoms
|
ASE Atoms object |
required |
filepath
|
Union[str, Path]
|
Output file path |
required |
format
|
str
|
File format (auto-detected from extension if None) |
None
|
**kwargs
|
Additional arguments passed to ase.io.write |
{}
|
Examples:
Source code in src/mace_inference/utils/io.py
parse_structure_input ¶
Parse various structure input formats.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
structure
|
Union[str, Path, Atoms, List[Atoms]]
|
Structure file path, Atoms object, or list of Atoms |
required |
Returns:
| Type | Description |
|---|---|
Union[Atoms, List[Atoms]]
|
Atoms object or list of Atoms objects |
Examples:
>>> atoms = parse_structure_input("structure.cif")
>>> atoms = parse_structure_input(existing_atoms)
Source code in src/mace_inference/utils/io.py
create_supercell ¶
Create a supercell from the input structure.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
atoms
|
Atoms
|
Input ASE Atoms object |
required |
supercell_matrix
|
Union[List[int], ndarray, int]
|
Supercell size (e.g., [2, 2, 2] or 2 for isotropic) |
required |
Returns:
| Type | Description |
|---|---|
Atoms
|
Supercell Atoms object |
Examples:
>>> supercell = create_supercell(atoms, [2, 2, 2])
>>> supercell = create_supercell(atoms, 2) # Same as [2, 2, 2]
Source code in src/mace_inference/utils/io.py
atoms_to_dict ¶
Convert Atoms object to dictionary (for JSON serialization).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
atoms
|
Atoms
|
ASE Atoms object |
required |
Returns:
| Type | Description |
|---|---|
dict
|
Dictionary representation |
Source code in src/mace_inference/utils/io.py
dict_to_atoms ¶
Convert dictionary to Atoms object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
dict
|
Dictionary with structure data |
required |
Returns:
| Type | Description |
|---|---|
Atoms
|
ASE Atoms object |
Source code in src/mace_inference/utils/io.py
load_structure¶
Load atomic structure from file.
Parameters:
path(str): Path to structure file (CIF, POSCAR, XYZ, etc.)
Returns: Atoms - ASE Atoms object
save_structure¶
Save atomic structure to file.
Parameters:
atoms(Atoms): Structure to savepath(str): Output file pathformat(str): File format (auto-detected if None)
D3 Correction¶
D3 dispersion correction utilities
create_d3_calculator ¶
Create a DFT-D3 dispersion correction calculator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
device
|
str
|
Compute device ("cpu" or "cuda") |
'cpu'
|
damping
|
str
|
Damping function ("zero", "bj", "zerom", "bjm") |
'bj'
|
xc
|
str
|
Exchange-correlation functional (e.g., "pbe", "b3lyp") |
'pbe'
|
Returns:
| Type | Description |
|---|---|
|
TorchDFTD3Calculator instance |
Raises:
| Type | Description |
|---|---|
ImportError
|
If torch-dftd is not installed |
Examples:
Source code in src/mace_inference/utils/d3_correction.py
create_combined_calculator ¶
create_combined_calculator(mace_calculator, enable_d3: bool = False, d3_device: Optional[str] = None, d3_damping: str = 'bj', d3_xc: str = 'pbe')
Create a combined MACE + D3 calculator using ASE's SumCalculator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mace_calculator
|
MACE calculator instance |
required | |
enable_d3
|
bool
|
Whether to enable D3 correction |
False
|
d3_device
|
Optional[str]
|
Device for D3 calculator (defaults to MACE device) |
None
|
d3_damping
|
str
|
D3 damping function |
'bj'
|
d3_xc
|
str
|
D3 exchange-correlation functional |
'pbe'
|
Returns:
| Type | Description |
|---|---|
|
Combined calculator (or just MACE calculator if D3 disabled) |
Examples:
>>> from mace.calculators import mace_mp
>>> mace_calc = mace_mp(model="medium", device="cuda")
>>> combined = create_combined_calculator(mace_calc, enable_d3=True)
Source code in src/mace_inference/utils/d3_correction.py
check_d3_available ¶
create_d3_calculator¶
def create_d3_calculator(
base_calculator: Calculator,
xc: str = "pbe",
damping: str = "bj"
) -> Calculator
Create a combined MACE+D3 calculator.
Parameters:
base_calculator(Calculator): The MACE calculatorxc(str): Exchange-correlation functionaldamping(str): Damping function ("bj"or"zero")
Returns: Calculator - Sum calculator with MACE + D3
Example:
from mace_inference.utils.d3_correction import create_d3_calculator
from mace.calculators import MACECalculator
mace_calc = MACECalculator(model_path="medium", device="cpu")
combined_calc = create_d3_calculator(mace_calc, xc="pbe", damping="bj")
D3_AVAILABLE¶
Boolean indicating if torch-dftd is installed.