irradiapy.utils package

Submodules

irradiapy.utils.io module

Utility functions for I/O operations.

irradiapy.utils.io.apply_boundary_conditions_to_lammps(path_in, path_out, x, y, z, overwrite=False)[source]

Apply periodic boundary conditions to a LAMMPS dump file.

Parameters:
  • path_in (Path) – Path to the input LAMMPS file (bzip2 compressed or not).

  • path_out (Path) – Path to the output LAMMPS file (bzip2 compressed or not).

  • x (bool) – Whether to apply periodic boundary conditions in the x direction.

  • y (bool) – Whether to apply periodic boundary conditions in the y direction.

  • z (bool) – Whether to apply periodic boundary conditions in the z direction.

  • overwrite (bool, optional (default=False)) – Whether to overwrite the output file if it exists.

Return type:

None

irradiapy.utils.io.compress_file_bz2(input_path, output_path, compresslevel=9)[source]

Compress a file using bzip2.

Parameters:
  • input_path (str) – Path to the input file to be compressed.

  • output_path (str) – Path where the compressed file will be saved.

  • compresslevel (int, optional (default=9)) – Compression level for bzip2.

Return type:

None

irradiapy.utils.io.decompress_file_bz2(input_path, output_path)[source]

Decompress a bzip2-compressed file.

Parameters:
  • input_path (str) – Path to the input .bz2 file.

  • output_path (str) – Path to the output decompressed file.

Return type:

None

irradiapy.utils.io.get_last_reader(reader)[source]

Get the last snapshot from a LAMMPS dump file using a reader.

Parameters:

reader (LAMMPSReader, BZIP2LAMMPSReader, LAMMPSLogReader) – An instance of a LAMMPS reader.

Returns:

The last snapshot from the LAMMPS file.

Return type:

any

irradiapy.utils.io.merge_lammps_snapshots(path_in, path_out, overwrite=False)[source]

Merge multiple snapshots in a LAMMPS file into a single snapshot.

Parameters:
  • path_in (Path) – Path to the input LAMMPS file (bzip2 compressed or not).

  • path_out (Path) – Path to the output LAMMPS file (bzip2 compressed or not).

  • overwrite (bool, optional (default=False)) – Whether to overwrite the output file if it exists.

Returns:

A dictionary containing the merged snapshot data.

Return type:

defaultdict

irradiapy.utils.math module

This module contains math utilities for the irradiapy package.

irradiapy.utils.math.apply_boundary_conditions(data_atoms, x, y, z)[source]

Apply boundary conditions to atoms.

Parameters:
  • data_atoms (defaultdict[str, Any]) – Atoms data containing atom positions and boundaries.

  • x (bool) – Whether to apply periodic boundary conditions in the x direction.

  • y (bool) – Whether to apply periodic boundary conditions in the y direction.

  • z (bool) – Whether to apply periodic boundary conditions in the z direction.

Returns:

Updated atoms data with applied boundary conditions.

Return type:

defaultdict[str, Any]

irradiapy.utils.math.fit_gaussian(xs, ys, p0=None, asymmetry=1.0)[source]

Fit data to a Gaussian function.

Parameters:
  • xs (npt.NDArray[np.float64]) – X values where the function is evaluated.

  • ys (npt.NDArray[np.float64]) – Y values at the given xs.

  • p0 (npt.NDArray[np.float64], optional (default=None)) – Initial guess of fit parameters. If None, a guess is generated.

  • asymmetry (float, optional (default=1.0)) – Bound for the asymmetry fit parameter. Fit will be done in (-asymmetry, asymmetry).

Return type:

tuple[ndarray[tuple[Any, ...], dtype[float64]], ndarray[tuple[Any, ...], dtype[float64]], Callable[[ndarray[tuple[Any, ...], dtype[float64]]], ndarray[tuple[Any, ...], dtype[float64]]]]

Returns:

  • popt (npt.NDArray[np.float64]) – Optimal values for the parameters.

  • pcov (npt.NDArray[np.float64]) – Covariance of popt.

  • fit_function (Callable[[npt.NDArray[np.float64]], npt.NDArray[np.float64]]) – Function that evaluates the fitted Gaussian.

irradiapy.utils.math.fit_linear(xs, ys, yerrs=None)[source]

Fit a linear function to the given data: y = a * x + b.

Parameters:
  • xs (npt.NDArray[np.float64]) – X values where the function is evaluated.

  • ys (npt.NDArray[np.float64]) – Y values at the given xs.

  • yerrs (npt.NDArray[np.float64], optional) – Y errors.

Returns:

Tuple containing: - (a, b): Fitted parameters of the linear function. - (error_a, error_b): Errors of the fitted parameters. - fit_function: Function that evaluates the fitted linear function.

Return type:

tuple[float, float, Callable[[npt.NDArray[np.float64]], npt.NDArray[np.float64]]]

irradiapy.utils.math.fit_lorentzian(xs, ys, p0=None, asymmetry=1.0)[source]

Fit data to a Lorentzian function.

Parameters:
  • xs (npt.NDArray[np.float64]) – X values where the function is evaluated.

  • ys (npt.NDArray[np.float64]) – Y values at the given xs.

  • p0 (npt.NDArray[np.float64], optional (default=None)) – Initial guess of fit parameters. If None, a guess is generated.

  • asymmetry (float, optional (default=1.0)) – Bound for the asymmetry fit parameter. Fit will be done in (-asymmetry, asymmetry).

Return type:

tuple[ndarray[tuple[Any, ...], dtype[float64]], ndarray[tuple[Any, ...], dtype[float64]], Callable[[ndarray[tuple[Any, ...], dtype[float64]]], ndarray[tuple[Any, ...], dtype[float64]]]]

Returns:

  • popt (npt.NDArray[np.float64]) – Optimal values for the parameters.

  • pcov (npt.NDArray[np.float64]) – Covariance of popt.

  • fit_function (Callable[[npt.NDArray[np.float64]], npt.NDArray[np.float64]]) – Function that evaluates the fitted Lorentzian.

irradiapy.utils.math.fit_power_law(xs, ys, yerrs=None)[source]

Fit a power law to the given histogram data: y = a * x**k.

Note

The fit is done in log-log space, so the input data should be positive.

Parameters:
  • xs (npt.NDArray[np.float64]) – X values where the function is evaluated.

  • ys (npt.NDArray[np.float64]) – Y values at the given xs.

  • yerrs (npt.NDArray[np.float64], optional) – Y errors.

Returns:

Tuple containing: - (a, k): Fitted parameters of the power law. - (error_a, error_k): Errors of the fitted parameters. - fit_function: Function that evaluates the fitted power law.

Return type:

tuple[float, float, Callable[[npt.NDArray[np.float64]], npt.NDArray[np.float64]]]

irradiapy.utils.math.gaussian(xs, x_peak, linewidth, amplitude, asymmetry)[source]

Evaluate a Gaussian function.

Parameters:
  • xs (npt.NDArray[np.float64]) – Where to evaluate the function.

  • x_peak (float) – Position with maximum value.

  • linewidth (float) – Linewidth.

  • amplitude (float) – Maximum amplitude.

  • asymmetry (float) – Asymmetry.

Returns:

Evaluated Gaussian function.

Return type:

float | npt.NDArray[np.float64]

References

See https://doi.org/10.1016/j.nimb.2021.05.014

irradiapy.utils.math.linear(x, a, b)[source]

Evaluate a linear function: y = a * x + b.

Parameters:
  • x (npt.NDArray[np.float64]) – Input values.

  • a (float) – Slope of the line.

  • b (float) – Intercept of the line.

Returns:

Evaluated linear function.

Return type:

npt.NDArray[np.float64]

irradiapy.utils.math.lorentzian(xs, x_peak, linewidth, amplitude, asymmetry)[source]

Evaluate a Lorentzian function.

Parameters:
  • xs (npt.NDArray[np.float64]) – Where to evaluate the function.

  • x_peak (float) – Position with maximum value.

  • linewidth (float) – Linewidth.

  • amplitude (float) – Maximum amplitude.

  • asymmetry (float) – Asymmetry.

Returns:

Evaluated Lorentzian function.

Return type:

float | npt.NDArray[np.float64]

References

See https://doi.org/10.1016/j.nimb.2021.05.014

irradiapy.utils.math.power_law(x, a, k)[source]

Evaluate a power law: y = a * x**k

Parameters:
  • x (npt.NDArray[np.float64]) – Input values.

  • a (float) – Prefactor.

  • k (float) – Exponent.

Returns:

Evaluated power law.

Return type:

npt.NDArray[np.float64]

irradiapy.utils.math.recombine_in_radius(data_defects, radius)[source]

Recombine defects (interstitials and vacancies) within a given radius.

Takes into account periodic boundary conditions.

Parameters:
  • data_defects (defaultdict[str, Any]) – Defects data containing defect positions and boundaries.

  • radius (float) – Radius within which to recombine defects.

Returns:

Updated defects data with recombined defects.

Return type:

defaultdict[str, Any]

irradiapy.utils.math.repeated_prime_factors(n)[source]

Return the prime factors of n (with repetition).

Parameters:

n (int) – The number to factorize.

Returns:

List of prime factors of n, including repetitions.

Return type:

list[int]

irradiapy.utils.mpi module

Utilities for MPI-related functionalities.

class irradiapy.utils.mpi.MPIExceptionHandlerMixin[source]

Bases: object

Provides a common MPI exception handler method when mpi_safe_method cannot be used.

This is useful for __init__ and __post_init__ methods where decorators cannot be applied because self.comm and self.rank are not yet defined before the function is called. You can use this method after the comm and rank attributes are initialized in a try/except block.

class irradiapy.utils.mpi.MPITagAllocator[source]

Bases: object

A class to allocate unique tags for processes.

classmethod get_tag()[source]

Get a unique tag for the current process.

Warning

This method should not be called in a statement that is only executed by one rank, such as in if rank == 0:. It is designed to be called by all ranks to ensure that all ranks receive the same tag.

irradiapy.utils.mpi.ap_rm_file(original, target, comm)[source]

Append content from original to target and delete original.

Return type:

None

irradiapy.utils.mpi.broadcast_variables(root, comm, *variables)[source]

Broadcasts variables.

Parameters:
  • root (int) – The rank of the process that will broadcast the variables.

  • comm (MPI.Comm) – The MPI communicator.

  • variables (tuple) – The variables to broadcast.

Returns:

The broadcasted variables.

Return type:

list

irradiapy.utils.mpi.cp_file(original, target, comm)[source]

Copy a file from original to target, overwriting target if it exists.

Return type:

None

irradiapy.utils.mpi.mpi_safe_method(method)[source]

Decorator that wraps an MPI-using method so any exception prints a traceback with the current rank and then calls MPI.Abort.

The method should be a member of a class that has comm and rank attributes.

irradiapy.utils.mpi.mpi_subdomains_decomposition(n)[source]

Factor n into three integers nx, ny, nz for MPI decomposition into subdomains.

nx, ny and nz are such that: - nx * ny * nz == n - the maximum of (nx, ny, nz) divided by the minimum is as small as possible.

Parameters:

n (int) – The number of processes to decompose into subdomains.

Returns:

A tuple containing the number of subdomains in the x, y, and z directions.

Return type:

tuple[int, int, int]

irradiapy.utils.mpi.mv_file(original, target, comm)[source]

Move a file from original to target, overwriting target if it exists.

Return type:

None

irradiapy.utils.mpi.rm_file(path_file, comm)[source]

Remove a file.

Return type:

None

irradiapy.utils.sqlite module

sqlite3 utilities.

irradiapy.utils.sqlite.delete_all_arrays(path_db)[source]

Drop the arrays table from the database.

Parameters:

path_db (Path) – Path to the SQLite database file.

Return type:

None

irradiapy.utils.sqlite.delete_array(path_db, name)[source]

Delete an array from the arrays table.

Parameters:
  • path_db (Path) – Path to the SQLite database file.

  • name (str) – Name of the array to delete.

Return type:

None

irradiapy.utils.sqlite.insert_array(path_db, name, **kargs)[source]

Insert or update an array in the arrays table.

Parameters:
  • path_db (Path) – Path to the SQLite database file.

  • name (str) – Name of the array to insert or update.

  • kargs (dict) – Keyword arguments representing the array data.

Return type:

None

irradiapy.utils.sqlite.read_array(path_db, name)[source]

Read an array from the arrays table.

Parameters:
  • path_db (Path) – Path to the SQLite database file.

  • name (str) – Name of the array to read.

Returns:

Dictionary containing the array data.

Return type:

dict[str, npt.NDArray]

Module contents

Utilities subpackage.