Example 3: Using the dictionary interface of the archive¶
The archive is like a dictionary, persistent on disk. [for Python afficionados, it is similar to a shelve, but in a portable format]. Therefore, one can iterate on its elements.
Let us imagine that you have stored 5 Green functions in an hdf5 file.
For example, we can create such a file as [file]
:
from triqs.gf import GfReFreq
from triqs.gf.descriptors import SemiCircular
from h5 import HDFArchive
import numpy
R = HDFArchive('myfile.h5', 'w')
for D in range(1,10,2) :
g = GfReFreq(indices = [0], window = (-2.00,2.00), name = "D=%s"%D)
g << SemiCircular(half_bandwidth = 0.1*D)
R[g.name]= g
Imagine that we want to plot those functions [file]
:
from triqs.gf import GfReFreq
from h5 import HDFArchive
from math import pi
R = HDFArchive('myfile.h5', 'r')
from triqs.plot.mpl_interface import oplot, plt
for name, g in list(R.items()) : # iterate on the elements of R, like a dict ...
oplot( (- 1/pi * g).imag, "-o", name = name)
plt.xlim(-1,1)
plt.ylim(0,7)
This produces the following plot (scaled semi-circular density of states).
Various pythonic constructs can be used in combinaison with HDFArchive, e.g. in order to plot only a few curves from a list
keylist = ['D=1', 'D=3']
for g in ( R[x] for x in keylist): # use an iterator
oplot( (- 1/pi * g).imag, "-o", name = 'g' )
or if we want to add the names
for n,g in ( (x,R[x]) for x in keylist): # use an iterator
oplot( (- 1/pi * g).imag, "-o", name = n )
The advantage of using an iterator is that the object is only retrieved from disk when needed.