npl.calculators package

Submodules

npl.calculators.base_calculator module

npl.calculators.energy_calculator module

class npl.calculators.energy_calculator.BayesianRRCalculator(feature_key)[source]

Bases: EnergyCalculator

BayesianRRCalculator is a class for performing Bayesian Ridge Regression (BRR) on nanoparticle datasets. Attributes: ———– ridge : BayesianRidge

The Bayesian Ridge Regression model. The key used to store energy values in the nanoparticles.

feature_keystr

The key used to extract feature vectors from the nanoparticles.

Methods:

__init__(self, feature_key):

Initializes the BayesianRRCalculator with a given feature key.

fit(self, training_set, energy_key, validation_set=None):

Fits the Bayesian Ridge Regression model using the provided training set.

validate(self, validation_set, energy_key):

Validates the Bayesian Ridge Regression model using the provided validation set.

get_coefficients(self):

Returns the coefficients of the Bayesian Ridge Regression model.

set_coefficients(self, new_coefficients):

Sets the coefficients of the Bayesian Ridge Regression model.

set_feature_key(self, feature_key):

Sets the feature key used to extract feature vectors from the nanoparticles.

compute_energy(self, particle):

Computes the energy of a given nanoparticle using the Bayesian Ridge Regression model.

Examples:

>>> from npl.calculators.energy_calculator import BayesianRRCalculator
>>> calculator = BayesianRRCalculator(feature_key='some_feature_key')
>>> training_set = [...]  # List of Nanoparticles for training
>>> validation_set = [...]  # List of Nanoparticles for validation
>>> calculator.fit(training_set, energy_key='some_energy_key', validation_set=validation_set)
>>> coefficients = calculator.get_coefficients()
>>> calculator.set_coefficients(new_coefficients)
>>> calculator.set_feature_key('new_feature_key')
>>> energy = calculator.compute_energy(some_nanoparticle)
compute_energy(particle)[source]

Compute the energy using BRR.

Assumes that a feature vector with key=self.feature_key is present in the particle.

Parameters:

particle : Nanoparticle

fit(training_set, energy_key, validation_set=None)[source]

Fit the Bayesian Ridge Regression (BRR) model.

Parameters:

training_setlist of Nanoparticles

The dataset used for training the model.

energy_keystr

The key used to extract energy values from the nanoparticles.

validation_setfloat or list of Nanoparticles, optional

If a float is provided, it represents the fraction of the training set to be used as the validation set. If a list is provided, it is used as the validation set directly. Default is None.

Returns:

None

get_coefficients()[source]
set_coefficients(new_coefficients)[source]
set_feature_key(feature_key)[source]
validate(validation_set, energy_key)[source]

Validate the Bayesian Ridge Regression (BRR) model.

Parameters:

validation_setlist of Nanoparticles

The dataset used for validating the model.

energy_keystr

The key used to extract energy values from the nanoparticles.

Returns:

None

class npl.calculators.energy_calculator.DipoleMomentCalculator[source]

Bases: object

compute_dipole_moment(particle, charges=[1, -1])[source]
get_dipole_moments()[source]
get_environments()[source]
get_total_dipole_moment()[source]
class npl.calculators.energy_calculator.EMTCalculator(fmax=0.01, steps=50, relax_atoms=False)[source]

Bases: EnergyCalculator

EMTCalculator is a class for calculating the energy of a nanoparticle using the Effective Medium Theory (EMT) method.

Attributes:

fmax (float): The maximum force tolerance for the BFGS optimizer. Default is 0.01. steps (int): The maximum number of steps for the BFGS optimizer. Default is 50. energy_key (str): The key used to store the calculated energy in the particle object. Default is ‘EMT’. relax_atoms (bool): Flag indicating whether to relax the atoms during energy calculation. Default is False.

Methods:
compute_energy(particle):

Compute the energy of the given nanoparticle using EMT.

Initialize the EMTCalculator with the given parameters.

fmax (float): The maximum force tolerance for the BFGS optimizer. Default is 0.01. steps (int): The maximum number of steps for the BFGS optimizer. Default is 50. relax_atoms (bool): Flag indicating whether to relax the atoms during energy calculation. Default is False.

Compute the energy using EMT.

BFGS is used for relaxation. By default, the atoms are NOT relaxed, i.e., the

particle (Nanoparticle): The nanoparticle object for which the energy is to be calculated.

Example:
>>> from npl.calculators.energy_calculator import EMTCalculator
>>> from npl.nanoparticle import Nanoparticle
>>> particle = Nanoparticle()
>>> particle.truncated_octahedron(7,2, {'Au' : 0.5, 'Ag' : 0.5})
>>> calculator = EMTCalculator(fmax=0.02, steps=100, relax_atoms=True)
>>> calculator.compute_energy(particle)
>>> energy = particle.get_energy('EMT')
>>> print(f"Computed energy: {energy}")
compute_energy(particle)[source]

Compute the energy using EMT.

BFGS is used for relaxation. By default, the atoms are NOT relaxed, i.e. the geometry remains unchanged unless this is explicitly stated.

Parameters:

particle : Nanoparticle relax_atoms : bool

class npl.calculators.energy_calculator.EnergyCalculator[source]

Bases: object

Base class for an energy calculator.

Valid implementations have to implement the compute_energy(particle) function. Energies are saved in the particle object with the key of the respective calculator.

compute_energy(particle)[source]
get_energy_key()[source]
static load(name_file)[source]
save(name_file: str)[source]
set_energy_key(energy_key)[source]
class npl.calculators.energy_calculator.GPRCalculator(feature_key, kernel=None, alpha=0.01, normalize_y=True)[source]

Bases: EnergyCalculator

Energy calculator using global feature vectors and Gaussian Process Regression.

compute_energy(particle)[source]

Compute the energy using GPR.

Assumes that a feature vector with key=self.feature_key is present in the particle.

Parameters:

particle : Nanoparticle

fit(training_set, energy_key)[source]

Fit the GPR model.

The feature vectors with key = self.feature_key will be used for feature vectors. The energy with the specified energy_key will be the target function.

Parameters:

training_set : list of Nanoparticles energy_key : str

class npl.calculators.energy_calculator.LateralInteractionCalculator[source]

Bases: object

bind_grid(particle)[source]
compute_energy(particle)[source]
construct_interatomic_potential_matrix(particle)[source]
class npl.calculators.energy_calculator.MixingEnergyCalculator(base_calculator=None, mixing_parameters=None, recompute_energies=False)[source]

Bases: EnergyCalculator

Compute the mixing energy using an arbitrary energy model.

For the original energy model it is assumed that all previous steps in the energy pipeline, e.g. calculation of local environment, feature vectors etc. has been carried out.

compute_energy(particle)[source]

Compute the mixing energy of the particle using the base energy model.

If energies have been computed using the same energy model as the base calculator they are reused if self.recompute_energies == False

Parameters:

particle : Nanoparticle

compute_mixing_parameters(particle, symbols)[source]

Compute the energies for the pure particles of the given symbols as reference points.

Parameters:

particle : Nanoparticle symbols : list of str

npl.calculators.energy_calculator.compute_coefficients_for_linear_topological_model(global_topological_coefficients, symbols, n_atoms)[source]
npl.calculators.energy_calculator.compute_coefficients_for_shape_optimization(global_topological_coefficients, symbols)[source]

npl.calculators.top_calculator module

class npl.calculators.top_calculator.TOPCalculator(feature_key: str, stoichiometry: str = None, model_paths: list | str = None, feature_classifier: <module 'npl.descriptors' from '/home/docs/checkouts/readthedocs.org/user_builds/nplib/checkouts/latest/npl/descriptors/__init__.py'> = None, **kwargs)[source]

Bases: Calculator

A class representing a calculator for performing relaxation calculations using the ASE library.

Parameters:

calculator (Calculator): The calculator object used for performing the calculations. fmax (float): The maximum force tolerance for the relaxation.

calculate(atoms)[source]

Do the calculation.

properties: list of str

List of what needs to be calculated. Can be any combination of ‘energy’, ‘forces’, ‘stress’, ‘dipole’, ‘charges’, ‘magmom’ and ‘magmoms’.

system_changes: list of str

List of what has changed since last calculation. Can be any combination of these six: ‘positions’, ‘numbers’, ‘cell’, ‘pbc’, ‘initial_charges’ and ‘initial_magmoms’.

Subclasses need to implement this, but can ignore properties and system_changes if they want. Calculated properties should be inserted into results dictionary like shown in this dummy example:

self.results = {'energy': 0.0,
                'forces': np.zeros((len(atoms), 3)),
                'stress': np.zeros(6),
                'dipole': np.zeros(3),
                'charges': np.zeros(len(atoms)),
                'magmom': 0.0,
                'magmoms': np.zeros(len(atoms))}

The subclass implementation should first call this implementation to set the atoms attribute and create any missing directories.

compute_energy(particle)[source]
get_coefficients()[source]
get_data_by_stoichiometry(stoichiometry)[source]

Retrieve data for a given stoichiometry.

Parameters:

stoichiometry (str): The stoichiometry to look up (e.g., “Pt70Au70”). data (dict): The JSON data.

Returns:

dict: The data for the specified stoichiometry, or None if not found.

get_energy_key()[source]
get_feature_classifier()[source]
get_feature_key()[source]
load_coefficients(stoichiometry)[source]
load_model(model_path)[source]
set_coefficients(coefficients)[source]

Module contents

class npl.calculators.BayesianRRCalculator(feature_key)[source]

Bases: EnergyCalculator

BayesianRRCalculator is a class for performing Bayesian Ridge Regression (BRR) on nanoparticle datasets. Attributes: ———– ridge : BayesianRidge

The Bayesian Ridge Regression model. The key used to store energy values in the nanoparticles.

feature_keystr

The key used to extract feature vectors from the nanoparticles.

Methods:

__init__(self, feature_key):

Initializes the BayesianRRCalculator with a given feature key.

fit(self, training_set, energy_key, validation_set=None):

Fits the Bayesian Ridge Regression model using the provided training set.

validate(self, validation_set, energy_key):

Validates the Bayesian Ridge Regression model using the provided validation set.

get_coefficients(self):

Returns the coefficients of the Bayesian Ridge Regression model.

set_coefficients(self, new_coefficients):

Sets the coefficients of the Bayesian Ridge Regression model.

set_feature_key(self, feature_key):

Sets the feature key used to extract feature vectors from the nanoparticles.

compute_energy(self, particle):

Computes the energy of a given nanoparticle using the Bayesian Ridge Regression model.

Examples:

>>> from npl.calculators.energy_calculator import BayesianRRCalculator
>>> calculator = BayesianRRCalculator(feature_key='some_feature_key')
>>> training_set = [...]  # List of Nanoparticles for training
>>> validation_set = [...]  # List of Nanoparticles for validation
>>> calculator.fit(training_set, energy_key='some_energy_key', validation_set=validation_set)
>>> coefficients = calculator.get_coefficients()
>>> calculator.set_coefficients(new_coefficients)
>>> calculator.set_feature_key('new_feature_key')
>>> energy = calculator.compute_energy(some_nanoparticle)
compute_energy(particle)[source]

Compute the energy using BRR.

Assumes that a feature vector with key=self.feature_key is present in the particle.

Parameters:

particle : Nanoparticle

fit(training_set, energy_key, validation_set=None)[source]

Fit the Bayesian Ridge Regression (BRR) model.

Parameters:

training_setlist of Nanoparticles

The dataset used for training the model.

energy_keystr

The key used to extract energy values from the nanoparticles.

validation_setfloat or list of Nanoparticles, optional

If a float is provided, it represents the fraction of the training set to be used as the validation set. If a list is provided, it is used as the validation set directly. Default is None.

Returns:

None

get_coefficients()[source]
set_coefficients(new_coefficients)[source]
set_feature_key(feature_key)[source]
validate(validation_set, energy_key)[source]

Validate the Bayesian Ridge Regression (BRR) model.

Parameters:

validation_setlist of Nanoparticles

The dataset used for validating the model.

energy_keystr

The key used to extract energy values from the nanoparticles.

Returns:

None

class npl.calculators.EMTCalculator(fmax=0.01, steps=50, relax_atoms=False)[source]

Bases: EnergyCalculator

EMTCalculator is a class for calculating the energy of a nanoparticle using the Effective Medium Theory (EMT) method.

Attributes:

fmax (float): The maximum force tolerance for the BFGS optimizer. Default is 0.01. steps (int): The maximum number of steps for the BFGS optimizer. Default is 50. energy_key (str): The key used to store the calculated energy in the particle object. Default is ‘EMT’. relax_atoms (bool): Flag indicating whether to relax the atoms during energy calculation. Default is False.

Methods:
compute_energy(particle):

Compute the energy of the given nanoparticle using EMT.

Initialize the EMTCalculator with the given parameters.

fmax (float): The maximum force tolerance for the BFGS optimizer. Default is 0.01. steps (int): The maximum number of steps for the BFGS optimizer. Default is 50. relax_atoms (bool): Flag indicating whether to relax the atoms during energy calculation. Default is False.

Compute the energy using EMT.

BFGS is used for relaxation. By default, the atoms are NOT relaxed, i.e., the

particle (Nanoparticle): The nanoparticle object for which the energy is to be calculated.

Example:
>>> from npl.calculators.energy_calculator import EMTCalculator
>>> from npl.nanoparticle import Nanoparticle
>>> particle = Nanoparticle()
>>> particle.truncated_octahedron(7,2, {'Au' : 0.5, 'Ag' : 0.5})
>>> calculator = EMTCalculator(fmax=0.02, steps=100, relax_atoms=True)
>>> calculator.compute_energy(particle)
>>> energy = particle.get_energy('EMT')
>>> print(f"Computed energy: {energy}")
compute_energy(particle)[source]

Compute the energy using EMT.

BFGS is used for relaxation. By default, the atoms are NOT relaxed, i.e. the geometry remains unchanged unless this is explicitly stated.

Parameters:

particle : Nanoparticle relax_atoms : bool

class npl.calculators.EnergyCalculator[source]

Bases: object

Base class for an energy calculator.

Valid implementations have to implement the compute_energy(particle) function. Energies are saved in the particle object with the key of the respective calculator.

compute_energy(particle)[source]
get_energy_key()[source]
static load(name_file)[source]
save(name_file: str)[source]
set_energy_key(energy_key)[source]
class npl.calculators.TOPCalculator(feature_key: str, stoichiometry: str = None, model_paths: list | str = None, feature_classifier: <module 'npl.descriptors' from '/home/docs/checkouts/readthedocs.org/user_builds/nplib/checkouts/latest/npl/descriptors/__init__.py'> = None, **kwargs)[source]

Bases: Calculator

A class representing a calculator for performing relaxation calculations using the ASE library.

Parameters:

calculator (Calculator): The calculator object used for performing the calculations. fmax (float): The maximum force tolerance for the relaxation.

calculate(atoms)[source]

Do the calculation.

properties: list of str

List of what needs to be calculated. Can be any combination of ‘energy’, ‘forces’, ‘stress’, ‘dipole’, ‘charges’, ‘magmom’ and ‘magmoms’.

system_changes: list of str

List of what has changed since last calculation. Can be any combination of these six: ‘positions’, ‘numbers’, ‘cell’, ‘pbc’, ‘initial_charges’ and ‘initial_magmoms’.

Subclasses need to implement this, but can ignore properties and system_changes if they want. Calculated properties should be inserted into results dictionary like shown in this dummy example:

self.results = {'energy': 0.0,
                'forces': np.zeros((len(atoms), 3)),
                'stress': np.zeros(6),
                'dipole': np.zeros(3),
                'charges': np.zeros(len(atoms)),
                'magmom': 0.0,
                'magmoms': np.zeros(len(atoms))}

The subclass implementation should first call this implementation to set the atoms attribute and create any missing directories.

compute_energy(particle)[source]
get_coefficients()[source]
get_data_by_stoichiometry(stoichiometry)[source]

Retrieve data for a given stoichiometry.

Parameters:

stoichiometry (str): The stoichiometry to look up (e.g., “Pt70Au70”). data (dict): The JSON data.

Returns:

dict: The data for the specified stoichiometry, or None if not found.

get_energy_key()[source]
get_feature_classifier()[source]
get_feature_key()[source]
load_coefficients(stoichiometry)[source]
load_model(model_path)[source]
set_coefficients(coefficients)[source]
npl.calculators.compute_coefficients_for_linear_topological_model(global_topological_coefficients, symbols, n_atoms)[source]