def test_plot_grid(self): matplotlib_bands_density(self.grid, pyplot.gca(), 100, energy_range=(-3, 3), show_fermi=False) matplotlib_bands_density(self.grid, pyplot.gca(), 100, energy_range=(-3, 3), method='gaussian', show_fermi=False) l = list(i for i in pyplot.gca().get_children() if isinstance(i, Line2D)) assert len(l) == 2 yy = [] for ll in l: x, y = ll.get_data() yy.append(y) testing.assert_equal(x, numpy.linspace(-3, 3, 100)) assert numpy.all(y < 10) and numpy.all(y >= 0) assert numpy.any(y > 0.1) assert (numpy.abs(yy[0] - yy[1])**2).sum()**.5 / 100 < 1e-2
def test_plot_fill_weights(self): w1 = 0.3 * numpy.ones(self.cell.values.shape) w2 = 0.7 * numpy.ones(self.cell.values.shape) rpc1 = matplotlib_bands_density(self.cell, pyplot.gca(), 100, show_fermi=False, weights=w1, use_fill=True) rpc2 = matplotlib_bands_density(self.cell, pyplot.gca(), 100, show_fermi=False, weights=w2, on_top_of=w1, use_fill=True) for rpc in (rpc1, rpc2): for p in rpc.get_paths(): (xmin, ymin), (xmax, ymax) = p.get_extents().get_points() assert xmin >= -3.31 assert ymin >= 0 assert xmax <= 3.31 assert ymax < 10
def test_unknown_orientation(self): with self.assertRaises(ValueError): matplotlib_bands_density(self.cell, pyplot.gca(), 100, energy_range=(-3, 3), orientation="unknown")
def test_unknown_units_portrait(self): matplotlib_bands_density(self.grid, pyplot.gca(), 100, units=2 * Ry, orientation='portrait') assert pyplot.gca().get_yaxis().get_label().get_text() == 'Energy' assert pyplot.gca().get_xaxis().get_label().get_text() == 'Density'
def test_custom_units_portrait(self): matplotlib_bands_density(self.cell, pyplot.gca(), 100, units=2 * Ry, units_name="Hartree", orientation='portrait') assert pyplot.gca().get_xaxis().get_label().get_text().endswith( "(electrons per unit cell per Hartree)") assert pyplot.gca().get_yaxis().get_label().get_text().endswith( "(Hartree)")
def test_plot_weights(self): w1 = 0.3*numpy.ones(self.cell.values.shape) w2 = 0.7*numpy.ones(self.cell.values.shape) rl1 = matplotlib_bands_density(self.cell, pyplot.gca(), 100, show_fermi = False, weights = w1)[0] rl2 = matplotlib_bands_density(self.cell, pyplot.gca(), 100, show_fermi = False, weights = w2, on_top_of = w1)[0] x1,y1 = rl1.get_data() x2,y2 = rl2.get_data() testing.assert_allclose(x1, numpy.linspace(-3,3,100)) testing.assert_allclose(x2, numpy.linspace(-3,3,100)) testing.assert_allclose(y1, 0.3*y2)
def test_plot_fill_weights(self): w1 = 0.3*numpy.ones(self.cell.values.shape) w2 = 0.7*numpy.ones(self.cell.values.shape) rpc1 = matplotlib_bands_density(self.cell, pyplot.gca(), 100, show_fermi = False, weights = w1, use_fill = True) rpc2 = matplotlib_bands_density(self.cell, pyplot.gca(), 100, show_fermi = False, weights = w2, on_top_of = w1, use_fill = True) for rpc in (rpc1,rpc2): for p in rpc.get_paths(): (xmin, ymin), (xmax, ymax) = p.get_extents().get_points() assert xmin>=-3.01 assert ymin>=0 assert xmax<=3.01 assert ymax< 10
def test_plot_fill_portrait(self): rpc = matplotlib_bands_density(self.cell, pyplot.gca(), 100, show_fermi = False, use_fill = True, orientation = "portrait") for p in rpc.get_paths(): (xmin, ymin), (xmax, ymax) = p.get_extents().get_points() assert ymin>=-3.01 assert xmin>=0 assert ymax<=3.01 assert xmax< 10
def test_plot_grid(self): matplotlib_bands_density(self.grid, pyplot.gca(), 100, energy_range = (-3,3), show_fermi = False) matplotlib_bands_density(self.grid, pyplot.gca(), 100, energy_range = (-3,3), force_gaussian = True, show_fermi = False) l = list(i for i in pyplot.gca().get_children() if isinstance(i, Line2D)) assert len(l) == 2 yy = [] for ll in l: x,y = ll.get_data() yy.append(y) testing.assert_equal(x, numpy.linspace(-3,3,100)) assert numpy.all(y<10) and numpy.all(y>=0) assert numpy.any(y>0.1) assert (numpy.abs(yy[0]-yy[1])**2).sum()**.5/100 < 1e-2
def test_portrait(self): rl = matplotlib_bands_density(self.cell, pyplot.gca(), 100, energy_range = (-3, 3), orientation = "portrait")[0] l = list(i for i in pyplot.gca().get_children() if isinstance(i, Line2D)) assert len(l) == 2 x,y = rl.get_data() testing.assert_equal(y, numpy.linspace(-3,3,100)) assert numpy.all(x<10) and numpy.all(x>0) assert numpy.any(x>0.1)
def test_gaussian(self): rl1 = matplotlib_bands_density(self.cell, pyplot.gca(), 100, method="gaussian", gaussian_spread=0.1, units="eV")[0] a = -0.5 / (0.1 * numericalunits.eV)**2 b = 1 / (2 * math.pi)**0.5 / (0.1 * numericalunits.eV) rl2 = matplotlib_bands_density( self.cell, pyplot.gca(), 100, method=lambda x: b * numpy.exp(a * x**2))[0] x1, y1 = rl1.get_data() x2, y2 = rl2.get_data() testing.assert_equal(x1, x2) testing.assert_equal(y1, y2)
def test_plot_weights(self): w1 = 0.3 * numpy.ones(self.cell.values.shape) w2 = 0.7 * numpy.ones(self.cell.values.shape) rl1 = matplotlib_bands_density(self.cell, pyplot.gca(), 100, show_fermi=False, weights=w1)[0] rl2 = matplotlib_bands_density(self.cell, pyplot.gca(), 100, show_fermi=False, weights=w2, on_top_of=w1)[0] x1, y1 = rl1.get_data() x2, y2 = rl2.get_data() testing.assert_allclose(x1, numpy.linspace(-3.3, 3.3, 100)) testing.assert_allclose(x2, numpy.linspace(-3.3, 3.3, 100)) testing.assert_allclose(y1, 0.3 * y2)
def test_plot_fill_portrait(self): rpc = matplotlib_bands_density(self.cell, pyplot.gca(), 100, show_fermi=False, use_fill=True, orientation="portrait") for p in rpc.get_paths(): (xmin, ymin), (xmax, ymax) = p.get_extents().get_points() assert ymin >= -3.31 assert xmin >= 0 assert ymax <= 3.31 assert xmax < 10
def test_portrait(self): rl = matplotlib_bands_density(self.cell, pyplot.gca(), 100, energy_range=(-3, 3), orientation="portrait")[0] l = list(i for i in pyplot.gca().get_children() if isinstance(i, Line2D)) assert len(l) == 2 x, y = rl.get_data() testing.assert_equal(y, numpy.linspace(-3, 3, 100)) assert numpy.all(x < 10) and numpy.all(x > 0) assert numpy.any(x > 0.1)
def test_plot_fill(self): rpc = matplotlib_bands_density(self.cell, pyplot.gca(), 100, show_fermi = False, use_fill = True) axes = pyplot.gcf().axes assert len(axes) == 1 axes = axes[0] pc = list(i for i in axes.get_children() if isinstance(i, PolyCollection)) assert len(pc) == 1 pc = pc[0] assert pc == rpc for p in rpc.get_paths(): (xmin, ymin), (xmax, ymax) = p.get_extents().get_points() assert xmin>=-3.01 assert ymin>=0 assert xmax<=3.01 assert ymax< 10
def test_plot(self): rl = matplotlib_bands_density(self.cell, pyplot.gca(), 100, show_fermi = False) assert len(rl) == 1 rl = rl[0] axes = pyplot.gcf().axes assert len(axes) == 1 axes = axes[0] testing.assert_allclose(axes.get_xlim(), (-3,3)) l = list(i for i in axes.get_children() if isinstance(i, Line2D)) assert len(l) == 1 l = l[0] assert l == rl x,y = l.get_data() testing.assert_allclose(x, numpy.linspace(-3,3,100)) assert numpy.all(y<10) and numpy.all(y>0) assert numpy.any(y>0.1)
def test_plot_fill(self): rpc = matplotlib_bands_density(self.cell, pyplot.gca(), 100, show_fermi=False, use_fill=True) axes = pyplot.gcf().axes assert len(axes) == 1 axes = axes[0] pc = list(i for i in axes.get_children() if isinstance(i, PolyCollection)) assert len(pc) == 1 pc = pc[0] assert pc == rpc for p in rpc.get_paths(): (xmin, ymin), (xmax, ymax) = p.get_extents().get_points() assert xmin >= -3.31 assert ymin >= 0 assert xmax <= 3.31 assert ymax < 10
def test_plot(self): rl = matplotlib_bands_density(self.cell, pyplot.gca(), 100, show_fermi=False) assert len(rl) == 1 rl = rl[0] axes = pyplot.gcf().axes assert len(axes) == 1 axes = axes[0] testing.assert_allclose(axes.get_xlim(), (-3.3, 3.3)) l = list(i for i in axes.get_children() if isinstance(i, Line2D)) assert len(l) == 1 l = l[0] assert l == rl x, y = l.get_data() testing.assert_allclose(x, numpy.linspace(-3.3, 3.3, 100)) assert numpy.all(y < 10) and numpy.all(y > 0) assert numpy.any(y > 0.1)
from dfttools.simple import parse from dfttools import presentation from matplotlib import pyplot with open("plot.py.data",'r') as f: # Retrieve the last band structure from the file bands = parse(f, "band-structure")[-1] # Convert to a grid grid = bands.as_grid() # Plot both presentation.matplotlib_bands_density(bands, pyplot.gca(), 200, energy_range = (-2, 2), label = "bands") presentation.matplotlib_bands_density(grid, pyplot.gca(), 200, energy_range = (-2, 2), label = "grid") pyplot.legend() pyplot.show()
from dfttools.simple import parse from dfttools import presentation from matplotlib import pyplot with open("plot.py.data",'r') as f: # Read bands data bands = parse(f, "band-structure")[0] # Prepare axes ax_left = pyplot.subplot2grid((1,3), (0, 0), colspan=2) ax_right = pyplot.subplot2grid((1,3), (0, 2)) # Plot bands presentation.matplotlib_bands(bands,ax_left) presentation.matplotlib_bands_density(bands, ax_right, 100, orientation = 'portrait') ax_right.set_ylabel('') pyplot.show()
# Set the band values bands.values[..., 0] = -e bands.values[..., 1] = e # Assign some weights weights = bands.values.copy() weights -= weights.min() weights /= weights.max() # Prepare axes ax_left = pyplot.subplot2grid((1, 3), (0, 0), colspan=2) ax_right = pyplot.subplot2grid((1, 3), (0, 2)) # Plot bands p = presentation.matplotlib_bands(bands, ax_left, weights=weights) presentation.matplotlib_bands_density(bands, ax_right, 100, orientation='portrait') presentation.matplotlib_bands_density(bands, ax_right, 100, orientation='portrait', weights=weights, use_fill=True, color="#AAAAFF") ax_right.set_ylabel('') pyplot.colorbar(p) pyplot.show()
from numericalunits import eV import numpy # A reciprocal basis basis = Basis((1, 1, 1, 0, 0, -0.5), kind='triclinic', meta={"Fermi": 0}) # Grid shape shape = (50, 50, 1) # A dummy grid with correct grid coordinates and empty grid values grid = Grid( basis, tuple(numpy.linspace(0, 1, x, endpoint=False) + .5 / x for x in shape), numpy.zeros(shape + (2, ), dtype=numpy.float64), ) # Calculate graphene band k = grid.cartesian() * numpy.pi / 3.**.5 * 2 e = (1 + 4 * numpy.cos(k[..., 1])**2 + 4 * numpy.cos(k[..., 1]) * numpy.cos(k[..., 0] * 3.**.5))**.5 * eV # Set the band values grid.values[..., 0] = -e grid.values[..., 1] = e presentation.matplotlib_bands_density(grid, pyplot.gca(), 200, energy_range=(-1, 1)) pyplot.show()
from dfttools.simple import parse from dfttools import presentation from matplotlib import pyplot with open("plot.py.data", 'r') as f: # Retrieve the last band structure from the file bands = parse(f, "band-structure") # Convert to a grid grid = bands.as_grid() # Plot both presentation.matplotlib_bands_density(bands, pyplot.gca(), 200, energy_range=(-2, 2), label="bands") presentation.matplotlib_bands_density(grid, pyplot.gca(), 200, energy_range=(-2, 2), label="grid") pyplot.legend() pyplot.show()
def test_units(self): matplotlib_bands_density(self.cell, pyplot.gca(), 100, units = "eV") assert pyplot.gca().get_xaxis().get_label().get_text().endswith("eV") assert pyplot.gca().get_yaxis().get_label().get_text().endswith("eV")
def test_custom_units(self): matplotlib_bands_density(self.cell, pyplot.gca(), 100, units = 2*Ry, units_name = "Hartree") assert pyplot.gca().get_xaxis().get_label().get_text().endswith("Hartree") assert pyplot.gca().get_yaxis().get_label().get_text().endswith("Hartree")
def test_unknown_units_portrait(self): matplotlib_bands_density(self.grid, pyplot.gca(), 100, units = 2*Ry, orientation = 'portrait') assert pyplot.gca().get_yaxis().get_label().get_text() == 'Energy' assert pyplot.gca().get_xaxis().get_label().get_text() == 'Density'
def test_unknown_orientation(self): with self.assertRaises(ValueError): matplotlib_bands_density(self.cell, pyplot.gca(), 100, energy_range = (-3, 3), orientation = "unknown")
from dfttools.simple import parse from dfttools import presentation from matplotlib import pyplot with open("plot.py.data",'r') as f: # Read bands data bands = parse(f, "band-structure") # Prepare axes ax_left = pyplot.subplot2grid((1,3), (0, 0), colspan=2) ax_right = pyplot.subplot2grid((1,3), (0, 2)) # Plot bands presentation.matplotlib_bands(bands,ax_left) presentation.matplotlib_bands_density(bands, ax_right, 100, orientation = 'portrait') ax_right.set_ylabel('') pyplot.show()
basis, kp, numpy.zeros((100,2), dtype = numpy.float64), ) # Calculate graphene band k = bands.cartesian()*numpy.pi/3.**.5*2 e = (1+4*numpy.cos(k[...,1])**2 + 4*numpy.cos(k[...,1])*numpy.cos(k[...,0]*3.**.5))**.5*eV # Set the band values bands.values[...,0] = -e bands.values[...,1] = e # Assign some weights weights = bands.values.copy() weights -= weights.min() weights /= weights.max() # Prepare axes ax_left = pyplot.subplot2grid((1,3), (0, 0), colspan=2) ax_right = pyplot.subplot2grid((1,3), (0, 2)) # Plot bands p = presentation.matplotlib_bands(bands,ax_left,weights = weights) presentation.matplotlib_bands_density(bands, ax_right, 100, orientation = 'portrait') presentation.matplotlib_bands_density(bands, ax_right, 100, orientation = 'portrait', weights = weights, use_fill = True, color = "#AAAAFF") ax_right.set_ylabel('') pyplot.colorbar(p) pyplot.show()
def test_units(self): matplotlib_bands_density(self.cell, pyplot.gca(), 100, units="eV") assert pyplot.gca().get_xaxis().get_label().get_text().endswith("(eV)") assert pyplot.gca().get_yaxis().get_label().get_text().endswith( "(states per unit cell per eV)")
from dfttools.types import Basis, Grid from dfttools import presentation from matplotlib import pyplot from numericalunits import eV import numpy # A reciprocal basis basis = Basis((1,1,1,0,0,-0.5), kind = 'triclinic', meta = {"Fermi": 0}) # Grid shape shape = (50,50,1) # A dummy grid with correct grid coordinates and empty grid values grid = Grid( basis, tuple(numpy.linspace(0,1,x, endpoint = False)+.5/x for x in shape), numpy.zeros(shape+(2,), dtype = numpy.float64), ) # Calculate graphene band k = grid.cartesian()*numpy.pi/3.**.5*2 e = (1+4*numpy.cos(k[...,1])**2 + 4*numpy.cos(k[...,1])*numpy.cos(k[...,0]*3.**.5))**.5*eV # Set the band values grid.values[...,0] = -e grid.values[...,1] = e presentation.matplotlib_bands_density(grid, pyplot.gca(), 200, energy_range = (-1, 1)) pyplot.show()