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_lammps_dump(path)[source]

Get the last snaptshot from a LAMMPS dump file.

Parameters:

path (Path) – Path to the LAMMPS dump file.

Returns:

The last snapshot from the LAMMPS dump file.

Return type:

defaultdict

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_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_scaling_law(centers, counts)[source]

Fit a scaling law to the given histogram data.

Parameters:
  • centers (npt.NDArray[np.float64]) – The centers of the bins.

  • counts (npt.NDArray[np.float64]) – The values of the histogram.

Returns:

A tuple containing: the prefactor of the scaling law, the exponent of the scaling law, and the scaling law function.

Return type:

tuple

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.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.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.math.scaling_law(x, a, s)[source]

Evaluate the scaling law function a / x**s.

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

  • a (float) – Prefactor.

  • s (float) – Exponent.

Returns:

Evaluated scaling law.

Return type:

npt.NDArray[np.float64]

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.