Utilities

class rhodent.utils.DistributedArray

Array that contains data on the root rank of the calculation communicator. Acts as an empty numpy array on other ranks.

Is implemented as numpy.ndarray on the root rank and rhodent.utils.ArrayIsOnRootRank on other ranks.

class rhodent.utils.ArrayIsOnRootRank[source]
rhodent.utils.add_fake_kpts(ksd)[source]

This function is necessary to read some fields without having a calculator attached.

rhodent.utils.broaden_ia2ou(M_ia, energy_i, energy_a, energy_o, energy_u, sigma)[source]

Broaden matrix onto energy grids.

\[M(\varepsilon_o, \varepsilon_u) = \sum_{ia} M_{ia} \frac{1}{\sqrt{2 \pi \sigma^2}} \exp\left(-\frac{ (\varepsilon_i - \varepsilon_o)^2 }{ 2 \sigma^2 }\right) \exp\left(-\frac{ (\varepsilon_a - \varepsilon_u)^2 }{ 2 \sigma^2 }\right)\]
Return type:

ndarray[Any, dtype[float64]]

Returns:

\(M(\varepsilon_o, \varepsilon_u)\)

rhodent.utils.broaden_n2e(M_n, energy_n, energy_e, sigma)[source]

Broaden matrix onto energy grids

\[M(\varepsilon_e) = \sum_n M_n \frac{1}{\sqrt{2 \pi \sigma^2}} \exp\left(-\frac{ \left(\varepsilon_n - \varepsilon_e\right)^2 }{ 2 \sigma^2 }\right),\]
Return type:

ndarray[Any, dtype[float64]]

Returns:

\(M(\varepsilon_0)\)

rhodent.utils.broaden_xn2e(M_xn, energy_n, energy_e, sigma)[source]

Broaden matrix onto energy grids

\[M(\varepsilon_e) = \sum_n M_n \frac{1}{\sqrt{2 \pi \sigma^2}} \exp\left(-\frac{ \left(\varepsilon_n - \varepsilon_e\right)^2 }{ 2 \sigma^2 }\right),\]
Return type:

ndarray[Any, dtype[float64]]

Returns:

\(M(\varepsilon_0)\)

rhodent.utils.concatenate_indices(indices_list)[source]

Concatenate indices

Given an array A and a list of incides indices_list such that A can be indexed

>>> for indices in indices_list:
>>>     A[indices]

this function shall concatenate the indices into indices_concat so that the array can be indexed in one go. This function will also give a new list of indices new_indices_list that can be used to index the A[indices_concat]. The following snippet shall be equivalent to the previous snipped

>>> B = A[indices_concat]
>>> for indices in new_indices_list:
>>>     B[indices]

Note that the indices need not be ordered, nor contigous, but the returned indices_concat will be a list of slices, and thus contiguous.

Example

>>> A = np.random.rand(100)
>>> value = 0
>>> new_value = 0
>>>
>>> indices_list = [slice(10, 12), slice(12, 19)]
>>> for indices in indices_list:
>>>     value += np.sum(A[indices])
>>>
>>> indices_concat, new_indices_list = concatenate_indices(indices_list)
>>> new_value = np.sum(A[indices_concat])
>>>
>>> assert abs(value - new_value) < 1e-10
>>>
>>> B = A[indices_concat]
>>> assert B.shape == (9, )
>>> new_value = 0
>>> for indices in new_indices_list:
>>>     new_value += np.sum(B[indices])
>>>
>>> assert abs(value - new_value) < 1e-10
Return type:

tuple[TypeVar(IND, slice, tuple[slice, ...]), list[TypeVar(IND, slice, tuple[slice, ...])]]

Returns:

(indices_concat, new_indices_list)

rhodent.utils.create_pulse(frequency, fwhm=5.0, t0=10.0, print=None)[source]

Create Gaussian laser pulse.

Return type:

GaussianPulse

frequency

Pulse frequncy in eV

fwhm

Full width at half maximum in time domain in fs

t0

Maximum of pulse envelope in fs

print

Printing function to control verbosity

rhodent.utils.detect_repeatrange(n, stride, verbose=True)[source]

If an array of length n is not divisible by the stride stride then some work will have to be repeated

Return type:

slice | None

rhodent.utils.gauss_ij(energy_i, energy_j, sigma)[source]

Computes the matrix

\[M_{ij} = \frac{1}{\sqrt{2 \pi \sigma^2}} \exp\left(-\frac{ \left(\varepsilon_i - \varepsilon_j\right)^2 }{ 2 \sigma^2 }\right),\]

which is useful for Gaussian broadening.

Parameters:
  • energy_i (Union[_SupportsArray[dtype[Any]], _NestedSequence[_SupportsArray[dtype[Any]]], bool, int, float, complex, str, bytes, _NestedSequence[Union[bool, int, float, complex, str, bytes]]]) – Energies \(\varepsilon_i\)

  • energy_j (Union[_SupportsArray[dtype[Any]], _NestedSequence[_SupportsArray[dtype[Any]]], bool, int, float, complex, str, bytes, _NestedSequence[Union[bool, int, float, complex, str, bytes]]]) – Energies \(\varepsilon_j\)

  • sigma (float) – Gaussian broadening width \(\sigma\)

Return type:

ndarray[Any, dtype[float64]]

Returns:

Matrix \(M_{ij}\)

rhodent.utils.gauss_ij_with_filter(energy_i, energy_j, sigma, fltthresh=None)[source]

Computes the matrix

\[M_{ij} = \frac{1}{\sqrt{2 \pi \sigma^2}} \exp\left(-\frac{ \left(\varepsilon_i - \varepsilon_j\right)^2 }{ 2 \sigma^2 }\right)\]

Useful for Gaussian broadening. Optionally only computes the exponent above a certain threshold, and returns the filter

Parameters:
  • energy_i (Union[_SupportsArray[dtype[Any]], _NestedSequence[_SupportsArray[dtype[Any]]], bool, int, float, complex, str, bytes, _NestedSequence[Union[bool, int, float, complex, str, bytes]]]) – Energies \(\varepsilon_i\)

  • energy_j (Union[_SupportsArray[dtype[Any]], _NestedSequence[_SupportsArray[dtype[Any]]], bool, int, float, complex, str, bytes, _NestedSequence[Union[bool, int, float, complex, str, bytes]]]) – Energies \(\varepsilon_j\)

  • sigma (float) – Gaussian broadening width \(\sigma\)

  • fltthresh (Optional[float]) – Filtering threshold

Return type:

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

Returns:

Matrix \(M_{ij}\), filter

rhodent.utils.get_array_filter(values, filter_values)[source]

Get array filter that can be used to filter out data.

Parameters:
  • values (ndarray[Any, dtype[float64]]) – Array of values, e.g. linspace of times or frequencies

  • filter_values (list[float] | ndarray[Any, dtype[float64]] | None) – List of values that one wishes to extract. The closes values from values will be selected as filter

Return type:

slice | ndarray[Any, dtype[bool]]

Returns:

Object that can be used to index values and arrays with the same shape as values

rhodent.utils.redistribute_LCAO_into_block_cyclic_form(C_nM, nn, band_comm)[source]

Redistribute LCAO coefficients into a block cyclic form for PLBAS/ScaLAPACK.

Parameters:
  • C_nM – LCAO coefficients distributed over bands

  • nn – Global number of bands

  • band_comm – Band communicator

Returns:

Matrix object in block cyclic form

rhodent.utils.safe_fill(a, b)[source]

Perform the operation a[:] = b, checking if the dimensions match.

If the dimensions of b are larger than the dimensions of a, raise an error.

If the dimensions of b are smaller than the dimensions of a, write to the first elements of a.

rhodent.utils.safe_fill_larger(a, b)[source]

Perform the operation a[:] = b, checking if the dimensions match

If the dimensions of b are smaller than the dimensions of a, raise an error

If the dimensions of b are larger than the dimensions of a, write the first elements of b to a.

rhodent.utils.two_communicators(*comm_sizes)[source]

Create two MPI communicators.

Must satisfy comm_sizes[0] * comm_sizes[1] = world.size.

The second communicator has the ranks in sequence.

Return type:

tuple[gpaw.mpi.Communicator, gpaw.mpi.Communicator]

Example

>>> world.size == 8
>>> two_communicators(2, 4)

This gives:

[0, 4]
[1, 5]
[2, 6]
[3, 7]

and:

[0, 1, 2, 3]
[4, 5, 6, 7]