core.components.calculate#

Copyright (c) Meta Platforms, Inc. and affiliates.

This source code is licensed under the MIT license found in the LICENSE file in the root directory of this source tree.

Submodules#

Classes#

CalculateRunner

Runner to run calculations/predictions using an ASE-like calculator and save results to file.

ElasticityRunner

Calculate elastic tensor for a set of structures.

RelaxationRunner

Relax a sequence of several structures/molecules.

Package Contents#

class core.components.calculate.CalculateRunner(calculator: ase.calculators.Calculator, input_data: collections.abc.Sequence)#

Bases: fairchem.core.components.runner.Runner

Runner to run calculations/predictions using an ASE-like calculator and save results to file.

Note

When running with the fairchemv2 cli, the job_config and attribute is set at runtime to those given in the config file.

See the Runner interface class for implementation details.

job_config#

a managed attribute that gives access to the job config

Type:

DictConfig

result_glob_pattern#

glob pattern of results written to file

Type:

str

result_glob_pattern: ClassVar[str] = '*'#
_calculator#
_input_data#
property calculator: ase.calculators.Calculator#

Get the calculator instance.

Returns:

The ASE-like calculator used for calculations

Return type:

Calculator

property input_data: collections.abc.Sequence#

Get the input data.

Returns:

The input data to be processed

Return type:

Sequence

abstract calculate(job_num: int = 0, num_jobs: int = 1) R#

Run any calculation using an ASE like Calculator.

Parameters:
  • job_num (int, optional) – Current job number in array job. Defaults to 0.

  • num_jobs (int, optional) – Total number of jobs in array. Defaults to 1.

Returns:

Results of the calculation

Return type:

R

abstract write_results(results: R, results_dir: str, job_num: int = 0, num_jobs: int = 1) None#

Write results to file in results_dir.

Parameters:
  • results (R) – Results from the calculation

  • results_dir (str) – Directory to write results to

  • job_num (int, optional) – Current job number in array job. Defaults to 0.

  • num_jobs (int, optional) – Total number of jobs in array. Defaults to 1.

abstract save_state(checkpoint_location: str, is_preemption: bool = False) bool#

Save the current state of the calculation to a checkpoint.

Parameters:
  • checkpoint_location (str) – Location to save the checkpoint

  • is_preemption (bool, optional) – Whether this save is due to preemption. Defaults to False.

Returns:

True if state was successfully saved, False otherwise

Return type:

bool

abstract load_state(checkpoint_location: str | None) None#

Load a previously saved state from a checkpoint.

Parameters:

checkpoint_location (str | None) – Location of the checkpoint to load, or None if no checkpoint

run()#

Run the actual calculation and save results.

Creates the results directory if it doesn’t exist, runs the calculation, and writes the results to the specified directory.

Note

Re-implementing this method in derived classes is discouraged.

class core.components.calculate.ElasticityRunner(calculator: ase.calculators.calculator.Calculator, input_data: fairchem.core.datasets.AseDBDataset)#

Bases: fairchem.core.components.calculate.CalculateRunner

Calculate elastic tensor for a set of structures.

result_glob_pattern: ClassVar[str] = 'elasticity_*-*.json.gz'#
calculate(job_num: int = 0, num_jobs: int = 1) list[dict[str, Any]]#

Calculate elastic properties for a batch of structures.

Parameters:
  • job_num (int, optional) – Current job number in array job. Defaults to 0.

  • num_jobs (int, optional) – Total number of jobs in array. Defaults to 1.

Returns:

List of dictionaries containing elastic properties for each structure

write_results(results: list[dict[str, Any]], results_dir: str, job_num: int = 0, num_jobs: int = 1) None#

Write calculation results to a compressed JSON file.

Parameters:
  • results – List of dictionaries containing elastic properties

  • results_dir – Directory path where results will be saved

  • job_num – Index of the current job

  • num_jobs – Total number of jobs

save_state(checkpoint_location: str, is_preemption: bool = False) bool#

Save the current state of the calculation to a checkpoint.

Parameters:
  • checkpoint_location (str) – Location to save the checkpoint

  • is_preemption (bool, optional) – Whether this save is due to preemption. Defaults to False.

Returns:

True if state was successfully saved, False otherwise

Return type:

bool

load_state(checkpoint_location: str | None) None#

Load a previously saved state from a checkpoint.

Parameters:

checkpoint_location (str | None) – Location of the checkpoint to load, or None if no checkpoint

class core.components.calculate.RelaxationRunner(calculator: ase.calculators.calculator.Calculator, input_data: fairchem.core.datasets.AseDBDataset, calculate_properties: collections.abc.Sequence[str], save_relaxed_atoms: bool = True, normalize_properties_by: dict[str, str] | None = None, save_target_properties: collections.abc.Sequence[str] | None = None, **relax_kwargs)#

Bases: fairchem.core.components.calculate.CalculateRunner

Relax a sequence of several structures/molecules.

This class handles the relaxation of atomic structures using a specified calculator, processes the input data in chunks, and saves the results.

result_glob_pattern: ClassVar[str] = 'relaxation_*-*.json.gz'#
_calculate_properties#
_save_relaxed_atoms#
_normalize_properties_by#
_save_target_properties#
_relax_kwargs#
calculate(job_num: int = 0, num_jobs: int = 1) list[dict[str, Any]]#

Perform relaxation calculations on a subset of structures.

Splits the input data into chunks and processes the chunk corresponding to job_num.

Parameters:
  • job_num (int, optional) – Current job number in array job. Defaults to 0.

  • num_jobs (int, optional) – Total number of jobs in array. Defaults to 1.

Returns:

list[dict[str, Any]] - List of dictionaries containing calculation results

write_results(results: list[dict[str, Any]], results_dir: str, job_num: int = 0, num_jobs: int = 1) None#

Write calculation results to a compressed JSON file.

Parameters:
  • results – List of dictionaries containing elastic properties

  • results_dir – Directory path where results will be saved

  • job_num – Index of the current job

  • num_jobs – Total number of jobs

save_state(checkpoint_location: str, is_preemption: bool = False) bool#

Save the current state of the calculation to a checkpoint.

Parameters:
  • checkpoint_location (str) – Location to save the checkpoint

  • is_preemption (bool, optional) – Whether this save is due to preemption. Defaults to False.

Returns:

True if state was successfully saved, False otherwise

Return type:

bool

load_state(checkpoint_location: str | None) None#

Load a previously saved state from a checkpoint.

Parameters:

checkpoint_location (str | None) – Location of the checkpoint to load, or None if no checkpoint