Restarting calculations from previously saved resultsΒΆ
The physical system in this example is similar to Evolution of a scattering state under a voltage pulse in a quantum dot. For time arguments on the right of the dashed vertical line, the result is calculated with the restarted wave function.
tkwant features highlighted
restarting calculations from previously saved results
Saving
import pickle
import functools as ft
import numpy as np
import kwant
import tkwant
def circle(pos):
(x, y) = pos
radius = 10
return x ** 2 + y ** 2 < radius ** 2
def make_system(a=1, gamma=1.0):
"""Make a tight binding system on a single square lattice"""
# `a` is the lattice constant and `gamma` the hopping integral
# both set by default to 1 for simplicity.
lat = kwant.lattice.square(a, norbs=1)
syst = kwant.Builder()
# Define the quantum dot
syst[lat.shape(circle, (0, 0))] = 4 * gamma
syst[lat.neighbors()] = -gamma
lead = kwant.Builder(kwant.TranslationalSymmetry((-1, 0)))
radius = 10
lead[(lat(0, j) for j in range(-radius // 2 + 1, radius // 2))] = 4 * gamma
lead[lat.neighbors()] = -gamma
syst.attach_lead(lead)
syst.attach_lead(lead.reversed())
return syst
# time-dependent voltage on top of the leads
def faraday_flux(time):
return 0.05 * (time - 10 * np.sin(0.1 * time))
syst = make_system()
# add a time-dependent voltage to lead 0 -- this is implemented
# by adding sites to the system at the interface with the lead and
# multiplying the hoppings to these sites by exp(-1j * faraday_flux(time))
extra_sites = tkwant.leads.add_voltage(syst, 0, faraday_flux)
lead_syst_hoppings = [(s, site) for site in extra_sites
for s in syst.neighbors(site)
if s not in extra_sites]
syst = syst.finalized()
# create an observable for calculating the current flowing from the left lead
current_operator = kwant.operator.Current(syst, where=lead_syst_hoppings, sum=True)
tmax = 150
times = np.linspace(0, tmax, 100)
# create a time-dependent wavefunction that starts in a scattering state
# originating from the left lead
wavefunction_type = ft.partial(tkwant.onebody.WaveFunction.from_kwant,
kernel_type=tkwant.onebody.kernels.Scipy)
mode = 0
psi = tkwant.onebody.ScatteringStates(syst, energy=1., lead=0, tmax=tmax,
wavefunction_type=wavefunction_type)[mode]
# evolve forward in time, calculating the current
current = []
for time in times:
psi.evolve(time)
current.append(psi.evaluate(current_operator))
# now we want to "save" our progress. we also save the timesteps with the
# calculated currents and the operator.
pickle.dump((psi, times, current, current_operator), open('state.npy', "wb"))
Restarting
import pickle
import numpy as np
from matplotlib import pyplot as plt
# pickle only saves the name reference to "faraday_flux",
# this function must be explicitly available.
def faraday_flux(time):
return 0.05 * (time - 10 * np.sin(0.1 * time))
# load the previously saved state and for convenience also the same operator.
psi, times, current, current_operator = pickle.load(open('state.npy', 'rb'))
tmax = times[-1]
times2 = np.linspace(tmax, 2 * tmax, 100)
for time in times2:
psi.evolve(time)
current.append(psi.evaluate(current_operator))
plt.plot(np.append(times, times2), current)
plt.plot([tmax] * 2, [min(current), max(current)], 'k--')
plt.xlabel(r'time $t$')
plt.ylabel(r'current $I(t)$')
plt.show()
WARNING:tkwant.onebody.onebody:435:rank=0: time > tmax; time= 151.5151515151515, tmax= 150
WARNING:tkwant.onebody.onebody:435:rank=0: time > tmax; time= 153.03030303030303, tmax= 150
WARNING:tkwant.onebody.onebody:435:rank=0: time > tmax; time= 154.54545454545453, tmax= 150
WARNING:tkwant.onebody.onebody:435:rank=0: time > tmax; time= 156.06060606060606, tmax= 150
WARNING:tkwant.onebody.onebody:435:rank=0: time > tmax; time= 157.57575757575756, tmax= 150
WARNING:tkwant.onebody.onebody:435:rank=0: time > tmax; time= 159.0909090909091, tmax= 150
WARNING:tkwant.onebody.onebody:435:rank=0: time > tmax; time= 160.6060606060606, tmax= 150
WARNING:tkwant.onebody.onebody:435:rank=0: time > tmax; time= 162.12121212121212, tmax= 150
WARNING:tkwant.onebody.onebody:435:rank=0: time > tmax; time= 163.63636363636363, tmax= 150
WARNING:tkwant.onebody.onebody:435:rank=0: time > tmax; time= 165.15151515151516, tmax= 150
WARNING:tkwant.onebody.onebody:435:rank=0: time > tmax; time= 166.66666666666666, tmax= 150
WARNING:tkwant.onebody.onebody:435:rank=0: time > tmax; time= 168.1818181818182, tmax= 150
WARNING:tkwant.onebody.onebody:435:rank=0: time > tmax; time= 169.6969696969697, tmax= 150
WARNING:tkwant.onebody.onebody:435:rank=0: time > tmax; time= 171.21212121212122, tmax= 150
WARNING:tkwant.onebody.onebody:435:rank=0: time > tmax; time= 172.72727272727272, tmax= 150
WARNING:tkwant.onebody.onebody:435:rank=0: time > tmax; time= 174.24242424242425, tmax= 150
WARNING:tkwant.onebody.onebody:435:rank=0: time > tmax; time= 175.75757575757575, tmax= 150
WARNING:tkwant.onebody.onebody:435:rank=0: time > tmax; time= 177.27272727272728, tmax= 150
WARNING:tkwant.onebody.onebody:435:rank=0: time > tmax; time= 178.78787878787878, tmax= 150
WARNING:tkwant.onebody.onebody:435:rank=0: time > tmax; time= 180.3030303030303, tmax= 150
WARNING:tkwant.onebody.onebody:435:rank=0: time > tmax; time= 181.8181818181818, tmax= 150
WARNING:tkwant.onebody.onebody:435:rank=0: time > tmax; time= 183.33333333333334, tmax= 150
WARNING:tkwant.onebody.onebody:435:rank=0: time > tmax; time= 184.84848484848484, tmax= 150
WARNING:tkwant.onebody.onebody:435:rank=0: time > tmax; time= 186.36363636363637, tmax= 150
WARNING:tkwant.onebody.onebody:435:rank=0: time > tmax; time= 187.87878787878788, tmax= 150
WARNING:tkwant.onebody.onebody:435:rank=0: time > tmax; time= 189.39393939393938, tmax= 150
WARNING:tkwant.onebody.onebody:435:rank=0: time > tmax; time= 190.9090909090909, tmax= 150
WARNING:tkwant.onebody.onebody:435:rank=0: time > tmax; time= 192.42424242424244, tmax= 150
WARNING:tkwant.onebody.onebody:435:rank=0: time > tmax; time= 193.93939393939394, tmax= 150
WARNING:tkwant.onebody.onebody:435:rank=0: time > tmax; time= 195.45454545454544, tmax= 150
WARNING:tkwant.onebody.onebody:435:rank=0: time > tmax; time= 196.96969696969697, tmax= 150
WARNING:tkwant.onebody.onebody:435:rank=0: time > tmax; time= 198.4848484848485, tmax= 150
WARNING:tkwant.onebody.onebody:435:rank=0: time > tmax; time= 200.0, tmax= 150
WARNING:tkwant.onebody.onebody:435:rank=0: time > tmax; time= 201.5151515151515, tmax= 150
WARNING:tkwant.onebody.onebody:435:rank=0: time > tmax; time= 203.03030303030303, tmax= 150
WARNING:tkwant.onebody.onebody:435:rank=0: time > tmax; time= 204.54545454545456, tmax= 150
WARNING:tkwant.onebody.onebody:435:rank=0: time > tmax; time= 206.06060606060606, tmax= 150
WARNING:tkwant.onebody.onebody:435:rank=0: time > tmax; time= 207.57575757575756, tmax= 150
WARNING:tkwant.onebody.onebody:435:rank=0: time > tmax; time= 209.0909090909091, tmax= 150
WARNING:tkwant.onebody.onebody:435:rank=0: time > tmax; time= 210.60606060606062, tmax= 150
WARNING:tkwant.onebody.onebody:435:rank=0: time > tmax; time= 212.12121212121212, tmax= 150
WARNING:tkwant.onebody.onebody:435:rank=0: time > tmax; time= 213.63636363636363, tmax= 150
WARNING:tkwant.onebody.onebody:435:rank=0: time > tmax; time= 215.15151515151516, tmax= 150
WARNING:tkwant.onebody.onebody:435:rank=0: time > tmax; time= 216.66666666666669, tmax= 150
WARNING:tkwant.onebody.onebody:435:rank=0: time > tmax; time= 218.1818181818182, tmax= 150
WARNING:tkwant.onebody.onebody:435:rank=0: time > tmax; time= 219.6969696969697, tmax= 150
WARNING:tkwant.onebody.onebody:435:rank=0: time > tmax; time= 221.21212121212122, tmax= 150
WARNING:tkwant.onebody.onebody:435:rank=0: time > tmax; time= 222.72727272727272, tmax= 150
WARNING:tkwant.onebody.onebody:435:rank=0: time > tmax; time= 224.24242424242425, tmax= 150
WARNING:tkwant.onebody.onebody:435:rank=0: time > tmax; time= 225.75757575757575, tmax= 150
WARNING:tkwant.onebody.onebody:435:rank=0: time > tmax; time= 227.27272727272725, tmax= 150
WARNING:tkwant.onebody.onebody:435:rank=0: time > tmax; time= 228.78787878787878, tmax= 150
WARNING:tkwant.onebody.onebody:435:rank=0: time > tmax; time= 230.3030303030303, tmax= 150
WARNING:tkwant.onebody.onebody:435:rank=0: time > tmax; time= 231.8181818181818, tmax= 150
WARNING:tkwant.onebody.onebody:435:rank=0: time > tmax; time= 233.33333333333331, tmax= 150
WARNING:tkwant.onebody.onebody:435:rank=0: time > tmax; time= 234.84848484848484, tmax= 150
WARNING:tkwant.onebody.onebody:435:rank=0: time > tmax; time= 236.36363636363637, tmax= 150
WARNING:tkwant.onebody.onebody:435:rank=0: time > tmax; time= 237.87878787878788, tmax= 150
WARNING:tkwant.onebody.onebody:435:rank=0: time > tmax; time= 239.39393939393938, tmax= 150
WARNING:tkwant.onebody.onebody:435:rank=0: time > tmax; time= 240.9090909090909, tmax= 150
WARNING:tkwant.onebody.onebody:435:rank=0: time > tmax; time= 242.42424242424244, tmax= 150
WARNING:tkwant.onebody.onebody:435:rank=0: time > tmax; time= 243.93939393939394, tmax= 150
WARNING:tkwant.onebody.onebody:435:rank=0: time > tmax; time= 245.45454545454544, tmax= 150
WARNING:tkwant.onebody.onebody:435:rank=0: time > tmax; time= 246.96969696969697, tmax= 150
WARNING:tkwant.onebody.onebody:435:rank=0: time > tmax; time= 248.4848484848485, tmax= 150
WARNING:tkwant.onebody.onebody:435:rank=0: time > tmax; time= 250.0, tmax= 150
WARNING:tkwant.onebody.onebody:435:rank=0: time > tmax; time= 251.5151515151515, tmax= 150
WARNING:tkwant.onebody.onebody:435:rank=0: time > tmax; time= 253.03030303030303, tmax= 150
WARNING:tkwant.onebody.onebody:435:rank=0: time > tmax; time= 254.54545454545456, tmax= 150
WARNING:tkwant.onebody.onebody:435:rank=0: time > tmax; time= 256.06060606060606, tmax= 150
WARNING:tkwant.onebody.onebody:435:rank=0: time > tmax; time= 257.57575757575756, tmax= 150
WARNING:tkwant.onebody.onebody:435:rank=0: time > tmax; time= 259.0909090909091, tmax= 150
WARNING:tkwant.onebody.onebody:435:rank=0: time > tmax; time= 260.6060606060606, tmax= 150
WARNING:tkwant.onebody.onebody:435:rank=0: time > tmax; time= 262.1212121212121, tmax= 150
WARNING:tkwant.onebody.onebody:435:rank=0: time > tmax; time= 263.6363636363636, tmax= 150
WARNING:tkwant.onebody.onebody:435:rank=0: time > tmax; time= 265.1515151515151, tmax= 150
WARNING:tkwant.onebody.onebody:435:rank=0: time > tmax; time= 266.6666666666667, tmax= 150
WARNING:tkwant.onebody.onebody:435:rank=0: time > tmax; time= 268.1818181818182, tmax= 150
WARNING:tkwant.onebody.onebody:435:rank=0: time > tmax; time= 269.6969696969697, tmax= 150
WARNING:tkwant.onebody.onebody:435:rank=0: time > tmax; time= 271.21212121212125, tmax= 150
WARNING:tkwant.onebody.onebody:435:rank=0: time > tmax; time= 272.72727272727275, tmax= 150
WARNING:tkwant.onebody.onebody:435:rank=0: time > tmax; time= 274.24242424242425, tmax= 150
WARNING:tkwant.onebody.onebody:435:rank=0: time > tmax; time= 275.75757575757575, tmax= 150
WARNING:tkwant.onebody.onebody:435:rank=0: time > tmax; time= 277.27272727272725, tmax= 150
WARNING:tkwant.onebody.onebody:435:rank=0: time > tmax; time= 278.78787878787875, tmax= 150
WARNING:tkwant.onebody.onebody:435:rank=0: time > tmax; time= 280.3030303030303, tmax= 150
WARNING:tkwant.onebody.onebody:435:rank=0: time > tmax; time= 281.8181818181818, tmax= 150
WARNING:tkwant.onebody.onebody:435:rank=0: time > tmax; time= 283.33333333333337, tmax= 150
WARNING:tkwant.onebody.onebody:435:rank=0: time > tmax; time= 284.8484848484849, tmax= 150
WARNING:tkwant.onebody.onebody:435:rank=0: time > tmax; time= 286.3636363636364, tmax= 150
WARNING:tkwant.onebody.onebody:435:rank=0: time > tmax; time= 287.8787878787879, tmax= 150
WARNING:tkwant.onebody.onebody:435:rank=0: time > tmax; time= 289.3939393939394, tmax= 150
WARNING:tkwant.onebody.onebody:435:rank=0: time > tmax; time= 290.9090909090909, tmax= 150
WARNING:tkwant.onebody.onebody:435:rank=0: time > tmax; time= 292.42424242424244, tmax= 150
WARNING:tkwant.onebody.onebody:435:rank=0: time > tmax; time= 293.93939393939394, tmax= 150
WARNING:tkwant.onebody.onebody:435:rank=0: time > tmax; time= 295.45454545454544, tmax= 150
WARNING:tkwant.onebody.onebody:435:rank=0: time > tmax; time= 296.969696969697, tmax= 150
WARNING:tkwant.onebody.onebody:435:rank=0: time > tmax; time= 298.4848484848485, tmax= 150
WARNING:tkwant.onebody.onebody:435:rank=0: time > tmax; time= 300.0, tmax= 150