Commit a6547b2d authored by Pablo Piskunow's avatar Pablo Piskunow
Browse files

Rename method `average` to `integrate`

  `integrate` is a more accurate name for a function that integrates
   the spectral density. Docstrings and tutorial updated.
parent d010de54
Pipeline #3858 passed with stages
in 30 minutes and 11 seconds
......@@ -56,22 +56,22 @@
+ )
def dos_averaging_example(fsyst):
def dos_integrating_example(fsyst):
spectrum = kwant.kpm.SpectralDensity(fsyst)
- print('identity resolution:', spectrum.average())
- print('identity resolution:', spectrum.integrate())
+ with open('kpm_normalization.txt', 'w') as f:
+ with redirect_stdout(f):
+ print('identity resolution:', spectrum.average())
+ print('identity resolution:', spectrum.integrate())
# Fermi energy 0.1 and temperature 0.2
fermi = lambda E: 1 / (np.exp((E - 0.1) / 0.2) + 1)
n_states = len(fsyst.sites) # 1 degree of freedom per site
- print('number of filled states:', n_states * spectrum.average(fermi))
- print('number of filled states:', n_states * spectrum.integrate(fermi))
+ with open('kpm_total_states.txt', 'w') as f:
+ with redirect_stdout(f):
+ print('number of filled states:', n_states * spectrum.average(fermi))
+ print('number of filled states:', n_states * spectrum.integrate(fermi))
def increasing_accuracy_example(fsyst):
......
......@@ -85,20 +85,20 @@ def simple_dos_example():
])
def dos_averaging_example(fsyst):
def dos_integrating_example(fsyst):
spectrum = kwant.kpm.SpectralDensity(fsyst)
#HIDDEN_BEGIN_av1
print('identity resolution:', spectrum.average())
#HIDDEN_END_av1
#HIDDEN_BEGIN_int1
print('identity resolution:', spectrum.integrate())
#HIDDEN_END_int1
#HIDDEN_BEGIN_av2
#HIDDEN_BEGIN_int2
# Fermi energy 0.1 and temperature 0.2
fermi = lambda E: 1 / (np.exp((E - 0.1) / 0.2) + 1)
n_states = len(fsyst.sites) # 1 degree of freedom per site
print('number of filled states:', n_states * spectrum.average(fermi))
#HIDDEN_END_av2
print('number of filled states:', n_states * spectrum.integrate(fermi))
#HIDDEN_END_int2
def increasing_accuracy_example(fsyst):
......@@ -206,7 +206,7 @@ def main():
fsyst = make_syst().finalized()
dos_averaging_example(fsyst)
dos_integrating_example(fsyst)
increasing_accuracy_example(fsyst)
operator_example(fsyst)
ldos_example(fsyst)
......
......@@ -113,26 +113,26 @@ evaluate the density of states.
.. image:: ../images/kpm_dos.*
In addition to being called like functions, `~kwant.kpm.SpectralDensity`
objects also have a method `~kwant.kpm.SpectralDensity.average` which can be
objects also have a method `~kwant.kpm.SpectralDensity.integrate` which can be
used to integrate the density of states against some distribution function over
the whole spectrum. If no distribution function is specified, then the uniform
distribution is used:
.. literalinclude:: kernel_polynomial_method.py
:start-after: #HIDDEN_BEGIN_av1
:end-before: #HIDDEN_END_av1
:start-after: #HIDDEN_BEGIN_int1
:end-before: #HIDDEN_END_int1
.. literalinclude:: ../images/kpm_normalization.txt
We see that the integral of the density of states is normalized to 1. If
we wish to calculate, say, the average number of states populated in
we wish to calculate, say, the number of states populated in
equilibrium, then we should integrate with respect to a Fermi-Dirac
distribution and multiply by the total number of available states in
the system:
.. literalinclude:: kernel_polynomial_method.py
:start-after: #HIDDEN_BEGIN_av2
:end-before: #HIDDEN_END_av2
:start-after: #HIDDEN_BEGIN_int2
:end-before: #HIDDEN_END_int2
.. literalinclude:: ../images/kpm_total_states.txt
......
......@@ -255,7 +255,7 @@ class SpectralDensity:
return np.transpose(np.polynomial.chebyshev.chebval(
rescaled_energy, coef_cheb) / g_e).real
def average(self, distribution_function=None):
def integrate(self, distribution_function=None):
"""Returns the total spectral density.
Returns the integral over the whole spectrum with an optional
......
......@@ -290,7 +290,7 @@ def test_kwant_op_current():
assert_allclose(spectrum_syst.densities, spectrum.densities)
def test_kwant_op_average():
def test_kwant_op_integrate():
"""Check that the kwant.operator.Density gives the same result as the
identity operator.
"""
......@@ -305,8 +305,8 @@ def test_kwant_op_average():
assert spectrum_syst.densities.shape[1] == ham.shape[0]
# same algorithms are used so these arrays are equal up to TOL
assert_allclose(np.sum(spectrum_syst.average(distribution_function=ones)),
spectrum.average())
assert_allclose(np.sum(spectrum_syst.integrate(distribution_function=ones)),
spectrum.integrate())
# ## test for methods to work as expected
......@@ -495,16 +495,16 @@ def test_call():
# different algorithms are used so these arrays are equal up to TOL_SP
assert_allclose_sp(densities_array, spectrum.densities)
# ### check average
# ### check integrate
def test_average():
def test_integrate():
ham = kwant.rmt.gaussian(dim)
spectrum = make_spectrum(ham, p)
ones = lambda x: np.ones_like(x)
assert np.abs(spectrum.average() - simps(spectrum.densities,
assert np.abs(spectrum.integrate() - simps(spectrum.densities,
x=spectrum.energies)) < TOL_SP
assert np.abs(spectrum.average() - spectrum.average(
assert np.abs(spectrum.integrate() - spectrum.integrate(
distribution_function=ones)) < TOL
# ### check increase_energy_resolution
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment