2.10. Some miscellaneous functions

This module contains some misceallenous functions used in the program.

qha.tools.find_nearest(array, value)[source]

Given an array , and given a value , returns an index j such that value is between array[j] and array[j+1]. The array must be monotonic increasing. j=-1 or j=len(array) is returned to indicate that value is out of range below and above respectively. If array is unsorted, consider first using an \(O(n \log n)\) sort and then use this function. Referenced from Stack Overflow.

Parameters
  • array (array(float64, 1d, A)) – An array of monotonic increasing real numbers.

  • value (float64) – The value which user wants to find between two of the consecutive elements in array.

Return type

int

Returns

The index mentioned above.

>>> find_nearest([1, 3, 4, 6, 9, 11], 3.5)
1
>>> find_nearest([1, 3, 4, 6, 9, 11], 0)
0
>>> find_nearest([1, 3, 4, 6, 9, 11], 12)
4
qha.tools.lagrange3(xs, ys)[source]

A second-order Lagrange polynomial function. Given 3 points for interpolation: \((x_0, y_0), \ldots, (x_2, y_2)\), evaluate the Lagrange polynomial on \(x\), referenced from Wolfram MathWorld also.

>>> xs = [0, 1, 3]
>>> ys = [2, 4, 5]
>>> f = lagrange3(xs, ys)
>>> f(2.5)
5.125
Parameters
  • xs (array(float64, 1d, A)) – A vector of the x-coordinates’ of the 3 points.

  • ys (array(float64, 1d, A)) – A vector of the y-coordinates’ of the 3 points.

Return type

Callable[[float], float]

Returns

A function that can evaluate the value of an x-coordinate within the range of \([\text{min}(xs), \text{max}(xs)]\), where \(xs\) denotes the argument xs.

qha.tools.lagrange4(xs, ys)[source]

A third-order Lagrange polynomial function. Given 4 points for interpolation: \((x_0, y_0), \ldots, (x_3, y_3)\), evaluate the Lagrange polynomial on \(x\), referenced from Wolfram MathWorld.

Parameters
  • xs (array(float64, 1d, A)) – A vector of the x-coordinates’ of the 4 points.

  • ys (array(float64, 1d, A)) – A vector of the y-coordinates’ of the 4 points.

Return type

Callable[[float], float]

Returns

A function that can evaluate the value of an x-coordinate within the range of \([\text{min}(xs), \text{max}(xs)]\), where \(xs\) denotes the argument xs.

qha.tools.is_monotonic_decreasing(array)[source]

Check whether the array is monotonic decreasing or not. For example, in QHA calculation, the volumes should be listed as a decreasing array, while the pressures should be monotonic increasing. This function can be used to check whether the volumes are in the right order.

>>> is_monotonic_decreasing([1, 2, 4, 5, 9])
False
>>> is_monotonic_decreasing([2, -5, -10, -20])
True
Parameters

array (array(float64, 1d, A)) – The array to be evaluated.

Return type

bool

Returns

True if the argument array is monotonic decreasing, otherwise False.

qha.tools.is_monotonic_increasing(array)[source]

Check whether the array is monotonic increasing or not. For example, in QHA calculation, the volumes should be listed as decreasing array, and the pressures should be monotonic increasing. This function can be used to check whether the pressures are in the right order.

>>> is_monotonic_increasing([1, 2, 4, 5, 9])
True
>>> is_monotonic_increasing([2, -5, -10, -20])
False
Parameters

array (array(float64, 1d, A)) – The array to be evaluated.

Return type

bool

Returns

True if the argument array is monotonic increasing, otherwise False.

qha.tools.arange(start, num, step)[source]

Similar to numpy.arange, generate an one-dimensional array but with start, num, step specified.

Parameters
  • start (float64) – The starting point of the array, which is included in this array.

  • num (int) – The total number of elements in this array.

  • step (float64) – A number specifying the difference between any of the two elements.

Return type

array(float64, 1d, A)

Returns

An arithmetic progression.

qha.tools.calibrate_energy_on_reference(volumes_before_calibration, energies_before_calibration, order=3)[source]

In multi-configuration system calculation, the volume set of each calculation may vary a little, This function would make the volume set of the first configuration (normally, the most populated configuration) as a reference volume set, then calibrate the energies of all configurations to this reference volume set.

Parameters
  • volumes_before_calibration (array(float64, 2d, A)) – Original volume sets of all configurations

  • energies_before_calibration (array(float64, 2d, A)) – Free energies of all configurations on the original volume sets.

  • order (Optional[int]) – The order of Birch–Murnaghan EOS fitting.

Returns

Free energies of each configuration on the referenced volumes (usually the volumes of the first configuration).