2.14. Logging

Tkwant provides a logger to record events and internal states during a calculation. Enabling the logger, which by default prints only warning messages to standard output (stdout), can help to find errors in the user code and also facilitate debugging tkwant. Logging is enabled by putting the following line of code at the beginning of a Python script right after importing the tkwant package:

import logging
tkwant.logging.level = logging.INFO  # possible levels (increasing verbosity): WARNING, INFO, DEBUG

A typical log message, here a warning, looks like this:

WARNING:tkwant.manybody:1452:rank=0: no occupied states found, the chemical potential is probably wrong.

The verbosity of the logger can increased to print also status and debug information, see Level. Logging can be also completely turned off as desribed in Handlers, or narrowed to a specific part as desribed in Filters. A complete Example is shown below.

Tkwant’s logger is based on logging module of the Python standard library. We refer to the Logging HOWTO tutorial for more information about Python’s logging module.

2.14.1. Level

Logging has several severity levels to control which level of log messages are recorded. By default, tkwant is logging only warning messages (corresponding to logging.WARNING). To change the log level, one has set tkwant.logging.level to a level, as defined in the Python logging module. As an example, to log also progress information, the additional line of code must be added:

import tkwant
import logging
tkwant.logging.level = logging.INFO  # possible levels (increasing verbosity): WARNING, INFO, DEBUG

Again, the logging level must be set after importing the tkwant module and before executing any tkwant code. The most verbose output is generated by setting the level to logging.DEBUG.

2.14.2. Handlers

The handler specify the format of the logging output. Tkwant has implemented two predefined stream handlers: tkwant.logging.debug_handler and tkwant.logging.simple_handler.

The debug_handler aka tkwant.logging.debug_handler is the default handler. With this handler, a typical log output looks like:

INFO:tkwant.manybody:229:rank=0: distribution function: zero-temperature fermi-dirac

The format is:

level: module-name: line-number: MPI-rank: log message

Note that the MPI rank corresponds to the rank of tkwant’s global MPI communicator that might be different to the rank of sub-communicators.

The simple_handler is less verbose and prints only the log message. It can be set with:

import tkwant
tkwant.logging.handler = tkwant.logging.simple_handler

With this handler, a typical log output looks like:

distribution function: zero-temperature fermi-dirac

The format is:

log message

Alternative handlers whose API matches the logging module of the Python standard library can be set in the same way.

Turning off logging completely is possible by setting the handler to the NullHandler:

import logging
tkwant.logging.level = logging.NullHandler()  # suppress logging

The logging handler must be set after importing the tkwant module and before executing any tkwant code.

2.14.3. Filters

The logging output can be filtered to reduce the output to specific parts. To log only logging events triggered by a certain module, as e.g. tkwant.leads, on can set

import logging
tkwant.logging.filter = logging.Filter('tkwant.leads')

Alternatively, one can also define a filter function. The following code logs only the messages from MPI with rank zero (which is Tkwant’s default behavior):

def rank_filter(record):
    return True if record.rank == 0 else False

tkwant.logging.filter = rank_filter

To print the logging output from all MPI ranks one has to set:

tkwant.logging.filter = None

If one likes to log only messages containing the word interval one can use

def message_filter(record):
    return True if 'interval' in record.getMessage() else False

tkwant.logging.filter = message_filter

Again, the filter must be set after importing the tkwant module and before executing any tkwant code. The documentation of the Python standard library logging module provides more information to write custom filters.

2.14.4. Example

As an example, we show the toy example from Getting started: a simple example with a one-dimensional chain with enabled logging and the generated output. Note that much logging output is generated by the call to tkwant.manybody.State() and reveals the preprocessing steps of the automatic high-level approach.

import numpy as np
import matplotlib.pyplot as plt

import kwant
import tkwant

#-------------------- enable logging --------------------------------
import logging
tkwant.logging.level = logging.INFO
#--------------------------------------------------------------------


def v(time, tau=8):
    """Time dependent perturbation V(t)"""
    if time < tau:
        return time / tau
    return 1


def create_system(length):

    def onsite_potential(site, time):
        """Time dependent onsite potential (static part + V(t))"""
        return 1 + v(time)

    # system building
    lat = kwant.lattice.square(a=1, norbs=1)
    syst = kwant.Builder()

    # central scattering region
    syst[(lat(x, 0) for x in range(length))] = 1
    syst[lat.neighbors()] = -1
    # time dependent onsite-potential at the leftmost site
    syst[lat(0, 0)] = onsite_potential

    # add leads
    sym = kwant.TranslationalSymmetry((-1, 0))
    lead_left = kwant.Builder(sym)
    lead_left[lat(0, 0)] = 1
    lead_left[lat.neighbors()] = -1
    syst.attach_lead(lead_left)
    syst.attach_lead(lead_left.reversed())

    return syst


# parameters
tmax = 20
length = 5

# create system
syst = create_system(length).finalized()

times = np.linspace(0, tmax)

# define an observable
density_operator = kwant.operator.Density(syst)

# do the actual tkwant simulation
state = tkwant.manybody.State(syst, tmax=tmax)

densities = []
for time in times:
    state.evolve(time)
    state.refine_intervals(rtol=1e-3, atol=1e-3)
    density = state.evaluate(density_operator)
    densities.append(density)

# plot the result
plt.plot(times, densities)
plt.xlabel(r'time $t$')
plt.ylabel(r'charge density $n$')
plt.show()
initial number of intervals=2
refinement step=0, time=0, max errsum=1.3478432769227361e-08, min errbnd=1e-05, total number of intervals=2
refinement step=0, time=0.0, max errsum=1.3478432769227361e-08, min errbnd=0.001, total number of intervals=2
refinement step=0, time=0.40816326530612246, max errsum=5.06404329010697e-07, min errbnd=0.001, total number of intervals=2
refinement step=0, time=0.8163265306122449, max errsum=2.492646141492808e-07, min errbnd=0.001, total number of intervals=2
refinement step=0, time=1.2244897959183674, max errsum=7.714781659017302e-08, min errbnd=0.001, total number of intervals=2
refinement step=0, time=1.6326530612244898, max errsum=1.4988736865497792e-06, min errbnd=0.001, total number of intervals=2
refinement step=0, time=2.0408163265306123, max errsum=2.875718949270364e-06, min errbnd=0.001, total number of intervals=2
refinement step=0, time=2.4489795918367347, max errsum=1.7334552821687804e-06, min errbnd=0.001, total number of intervals=2
refinement step=0, time=2.857142857142857, max errsum=4.096792960656631e-06, min errbnd=0.001, total number of intervals=2
refinement step=0, time=3.2653061224489797, max errsum=9.96808849615559e-07, min errbnd=0.001, total number of intervals=2
refinement step=0, time=3.673469387755102, max errsum=1.8623942443179076e-06, min errbnd=0.001, total number of intervals=2
refinement step=0, time=4.081632653061225, max errsum=2.794470790641709e-06, min errbnd=0.001, total number of intervals=2
refinement step=0, time=4.4897959183673475, max errsum=4.553854500032003e-06, min errbnd=0.001, total number of intervals=2
refinement step=0, time=4.8979591836734695, max errsum=6.73552137236205e-06, min errbnd=0.001, total number of intervals=2
refinement step=0, time=5.3061224489795915, max errsum=5.0606371151559315e-06, min errbnd=0.001, total number of intervals=2
refinement step=0, time=5.714285714285714, max errsum=3.5423027650691697e-06, min errbnd=0.001, total number of intervals=2
refinement step=0, time=6.122448979591837, max errsum=4.382990408969733e-06, min errbnd=0.001, total number of intervals=2
refinement step=0, time=6.530612244897959, max errsum=8.664017541402482e-06, min errbnd=0.001, total number of intervals=2
refinement step=0, time=6.938775510204081, max errsum=1.1595097210503468e-05, min errbnd=0.001, total number of intervals=2
refinement step=0, time=7.346938775510204, max errsum=3.0957317080011195e-05, min errbnd=0.001, total number of intervals=2
refinement step=0, time=7.755102040816327, max errsum=0.0001593807266848606, min errbnd=0.001, total number of intervals=2
refinement step=0, time=8.16326530612245, max errsum=0.0002607733919743455, min errbnd=0.001, total number of intervals=2
refinement step=0, time=8.571428571428571, max errsum=0.00019462993951196295, min errbnd=0.001, total number of intervals=2
refinement step=0, time=8.979591836734695, max errsum=0.00025266870645725603, min errbnd=0.001, total number of intervals=2
refinement step=0, time=9.387755102040817, max errsum=0.0003163505714283302, min errbnd=0.001, total number of intervals=2
refinement step=0, time=9.795918367346939, max errsum=0.00027102622405759903, min errbnd=0.001, total number of intervals=2
refinement step=0, time=10.204081632653061, max errsum=0.00035224083674269117, min errbnd=0.001, total number of intervals=2
refinement step=0, time=10.612244897959183, max errsum=0.00019985353351114677, min errbnd=0.001, total number of intervals=2
refinement step=0, time=11.020408163265307, max errsum=8.24345858383615e-05, min errbnd=0.001, total number of intervals=2
refinement step=0, time=11.428571428571429, max errsum=8.07524817428488e-05, min errbnd=0.001, total number of intervals=2
refinement step=0, time=11.83673469387755, max errsum=8.570955848241071e-05, min errbnd=0.001, total number of intervals=2
refinement step=0, time=12.244897959183675, max errsum=6.897618481795182e-05, min errbnd=0.001, total number of intervals=2
refinement step=0, time=12.653061224489797, max errsum=0.0002918442593491386, min errbnd=0.001, total number of intervals=2
refinement step=0, time=13.061224489795919, max errsum=0.00015030111880069667, min errbnd=0.001, total number of intervals=2
refinement step=0, time=13.46938775510204, max errsum=0.00020683223579337292, min errbnd=0.001, total number of intervals=2
refinement step=0, time=13.877551020408163, max errsum=0.00027240591931653894, min errbnd=0.001, total number of intervals=2
refinement step=0, time=14.285714285714286, max errsum=0.00013562898508359867, min errbnd=0.001, total number of intervals=2
refinement step=0, time=14.693877551020408, max errsum=0.0001989688871479762, min errbnd=0.001, total number of intervals=2
refinement step=0, time=15.10204081632653, max errsum=0.00021152772021388396, min errbnd=0.001, total number of intervals=2
refinement step=0, time=15.510204081632654, max errsum=0.00020090045667800168, min errbnd=0.001, total number of intervals=2
refinement step=0, time=15.918367346938776, max errsum=0.00027404194717768973, min errbnd=0.001, total number of intervals=2
refinement step=0, time=16.3265306122449, max errsum=0.0002583108212877655, min errbnd=0.001, total number of intervals=2
refinement step=0, time=16.73469387755102, max errsum=0.00013953216430413626, min errbnd=0.001, total number of intervals=2
refinement step=0, time=17.142857142857142, max errsum=9.678279402282706e-05, min errbnd=0.001, total number of intervals=2
refinement step=0, time=17.551020408163264, max errsum=0.00012275400285846188, min errbnd=0.001, total number of intervals=2
refinement step=0, time=17.95918367346939, max errsum=0.0002645532873850069, min errbnd=0.001, total number of intervals=2
refinement step=0, time=18.367346938775512, max errsum=0.000265285793377869, min errbnd=0.001, total number of intervals=2
refinement step=0, time=18.775510204081634, max errsum=0.0006349828586432764, min errbnd=0.001, total number of intervals=2
refinement step=0, time=19.183673469387756, max errsum=0.0018508248295065468, min errbnd=0.001, total number of intervals=2
refinement step=1, time=19.183673469387756, max errsum=0.0010240244073703808, min errbnd=0.001, total number of intervals=3
refinement step=2, time=19.183673469387756, max errsum=0.0022075791522734615, min errbnd=0.001, total number of intervals=4
refinement step=3, time=19.183673469387756, max errsum=0.0010908541043670211, min errbnd=0.001, total number of intervals=5
refinement step=4, time=19.183673469387756, max errsum=0.001021851785418235, min errbnd=0.001, total number of intervals=6
refinement step=5, time=19.183673469387756, max errsum=0.0009135026772643705, min errbnd=0.001, total number of intervals=7
refinement step=0, time=19.591836734693878, max errsum=0.0007401408264771478, min errbnd=0.001, total number of intervals=7
refinement step=0, time=20.0, max errsum=0.0007771073196399074, min errbnd=0.001, total number of intervals=7
../_images/logging_12_57.png