momentGW.util

Utility functions.

Module Contents

class momentGW.util.Timer

Timer class.

lap()

Return the time since the last call to lap.

Returns:

lap – Lap time.

Return type:

float

total()

Return the total time since initialization.

Returns:

total – Total time.

Return type:

float

static format_time(seconds, precision=2)

Return a formatted time.

Parameters:
  • seconds (float) – Time in seconds.

  • precision (int, optional) – Number of time units to display. Default is 2.

Returns:

formatted – Formatted time.

Return type:

str

class momentGW.util.DIIS(dev=None, filename=None, incore=getattr(__config__, 'lib_diis_DIIS_incore', False))

Bases: pyscf.lib.diis.DIIS

Direct inversion of the iterative subspace (DIIS).

Notes

For some reason, the default pyscf DIIS object can result in fully linearly dependent error vectors in high-moment self-consistent calculations. This class is a drop-in replacement with a fallback in this case.

See also

pyscf.lib.diis.DIIS

PySCF DIIS object which this class extends.

update_with_complex_unravel(x, xerr=None)

Execute DIIS where the error vectors are unravelled to concatenate the real and imaginary parts.

Parameters:
  • x (numpy.ndarray) – Array to update with DIIS.

  • xerr (numpy.ndarray, optional) – Error metric for the array. Default is None.

Returns:

x – Updated array.

Return type:

numpy.ndarray

extrapolate(nd=None)

Extrapolate the DIIS vectors.

Parameters:

nd (int, optional) – Number of vectors to extrapolate. Default is None, which extrapolates all vectors.

Returns:

xnew – Extrapolated vector.

Return type:

numpy.ndarray

Notes

This function improves the robustness of the DIIS procedure in the event of linear dependencies.

See also

pyscf.lib.diis.DIIS.extrapolate

PySCF DIIS extrapolation which this function refactors.

update(x, xerr=None)

Extrapolate vector

  • If xerr the error vector is given, this function will push the target

vector and error vector in the DIIS subspace, and use the error vector to extrapolate the vector and return the extrapolated vector. * If xerr is None, this function will take the difference between the current given vector and the last given vector as the error vector to extrapolate the vector.

restore(filename, inplace=True)

Read diis contents from a diis file and replace the attributes of current diis object if needed, then construct the vector.

class momentGW.util.SilentSCF(mf)

Context manager to shut PySCF’s SCF classes up.

Parameters:

mf (pyscf.scf.SCF) – SCF object to silence.

momentGW.util.list_union(*args)

Find the union of a list of lists, with the elements sorted by their first occurrence.

Parameters:

args (list of list) – Lists to find the union of.

Returns:

out – Union of the lists.

Return type:

list

momentGW.util.dict_union(*args)

Find the union of a list of dictionaries, preserving the order of the first occurrence of each key.

Parameters:

args (list of dict) – Dictionaries to find the union of.

Returns:

out – Union of the dictionaries.

Return type:

dict

momentGW.util.build_1h1p_energies(mo_energy, mo_occ)

Construct an array of 1h1p energies where elements are

\[\begin{split}\\Delta_{ij} = \\epsilon_i - \\epsilon_j\end{split}\]
Parameters:
  • mo_energy (numpy.ndarray or tuple of numpy.ndarray) – Molecular orbital energies. If a tuple, the first element is used for occupied orbitals and the second element is used for virtual orbitals.

  • mo_occ (numpy.ndarray or tuple of numpy.ndarray) – Molecular orbital occupancies. If a tuple, the first element is used for occupied orbitals and the second element is used for virtual orbitals.

Returns:

d – 1h1p energies.

Return type:

numpy.ndarray

momentGW.util.einsum(*operands, **kwargs)

Evaluate an Einstein summation convention on the operands.

Using the Einstein summation convention, many common multi-dimensional, linear algebraic array operations can be represented in a simple fashion. In implicit mode einsum computes these values.

In explicit mode, einsum provides further flexibility to compute other array operations that might not be considered classical Einstein summation operations, by disabling, or forcing summation over specified subscript labels.

See the numpy.einsum documentation for clarification.

Parameters:
  • operands (list) – Any valid input to numpy.einsum.

  • out (numpy.ndarray, optional) – If provided, the calculation is done into this array.

  • contract (callable, optional) – The function to use for contraction. Default value is _contract.

  • optimize (bool, optional) – If True, use the numpy.einsum_path to optimize the contraction. Default value is True.

Returns:

output – The calculation based on the Einstein summation convention.

Return type:

numpy.ndarray

See also

numpy.einsum

NumPy’s einsum function.