def test(): ax = plt.gca() for i, lat in enumerate(all_variants()): if lat.ndim == 2: break xid = '{:02d}.{}'.format(i, lat.variant) path = lat.bandpath(density=10) path.write('path.{}.json'.format(xid)) atoms = Atoms(cell=lat.tocell(), pbc=True) atoms.calc = FreeElectrons(nvalence=0, kpts=path.kpts) bs = calculate_band_structure(atoms, path) bs.write('bs.{}.json'.format(xid)) bs.plot(ax=ax, emin=0, emax=20, filename='fig.{}.png'.format(xid)) ax.clear()
def test_bravais_lattices(): import numpy as np from ase.lattice import (bravais_lattices, all_variants, get_lattice_from_canonical_cell) for name in bravais_lattices: latcls = bravais_lattices[name] assert latcls.name == name assert latcls.longname is not None for par in latcls.parameters: assert par in ['a', 'b', 'c', 'alpha', 'beta', 'gamma'] for lat in all_variants(): print(lat.variant) for par in lat.parameters: print(par, getattr(lat, par)) print('cell', lat.tocell()) cell = lat.tocell() if lat.name in ['TRI']: # Automatic check not implemented for these cell types, but we # can still recognize the canonical form: lat1 = get_lattice_from_canonical_cell(cell) else: lat1 = cell.get_bravais_lattice() assert lat1.name == lat.name, (lat1.name, lat.name) assert lat1.variant == lat.variant assert np.abs(cell - lat1.tocell()).max() < 1e-13 print('cellpar', lat.cellpar()) print('special path', lat.special_path) arr = lat.get_special_points_array() assert arr.shape == (len(lat.special_point_names), 3) dct = lat.get_special_points() assert len(dct) == len(lat.special_point_names) print(lat)
import pytest from ase.calculators.test import FreeElectrons from ase.lattice import all_variants from ase.spectrum.band_structure import calculate_band_structure from ase import Atoms @pytest.mark.parametrize("i, lat", [pytest.param(i, lat, id=lat.variant) for i, lat in enumerate(all_variants()) if lat.ndim == 3]) def test_lattice_bandstructure(testdir, i, lat, figure): xid = '{:02d}.{}'.format(i, lat.variant) path = lat.bandpath(density=10) path.write('path.{}.json'.format(xid)) atoms = Atoms(cell=lat.tocell(), pbc=True) atoms.calc = FreeElectrons(nvalence=0, kpts=path.kpts) bs = calculate_band_structure(atoms, path) bs.write('bs.{}.json'.format(xid)) ax = figure.gca() bs.plot(ax=ax, emin=0, emax=20, filename='fig.{}.png'.format(xid))
import pytest from ase.calculators.test import FreeElectrons from ase.lattice import all_variants from ase.dft.band_structure import calculate_band_structure from ase import Atoms @pytest.mark.parametrize("i, lat", [ pytest.param(i, lat, id=lat.variant) for i, lat in enumerate(all_variants()) if lat.ndim == 3 ]) def test_lattice_bandstructure(i, lat, figure): xid = '{:02d}.{}'.format(i, lat.variant) path = lat.bandpath(density=10) path.write('path.{}.json'.format(xid)) atoms = Atoms(cell=lat.tocell(), pbc=True) atoms.calc = FreeElectrons(nvalence=0, kpts=path.kpts) bs = calculate_band_structure(atoms, path) bs.write('bs.{}.json'.format(xid)) ax = figure.gca() bs.plot(ax=ax, emin=0, emax=20, filename='fig.{}.png'.format(xid))
Brillouin zone data ------------------- .. list-table:: :widths: 10 15 45 """ entry = """\ * - {name} ({longname}) - {bandpath} - .. image:: {fname} :width: 40 % """ with open('bztable.rst', 'w') as fd: print(header, file=fd) for i, lat in enumerate(all_variants(include_blunt_angles=False)): id = '{:02d}.{}'.format(i, lat.variant) imagefname = '{}.svg'.format(id) txt = entry.format(name=lat.variant, longname=lat.longname, bandpath=lat.bandpath().path, fname=imagefname) print(txt, file=fd) ax = lat.plot_bz() fig = ax.get_figure() fig.savefig(imagefname, bbox_inches='tight') fig.clear()
import pytest from ase import Atoms from ase.lattice import all_variants from ase.build.supercells import make_supercell from ase.calculators.emt import EMT def emt_energy_per_atom(atoms): atoms = atoms.copy() atoms.calc = EMT() return atoms.get_potential_energy() / len(atoms) @pytest.mark.parametrize('lat', [var for var in all_variants() if var.ndim == 3]) def test_conventional_map(lat): if not hasattr(lat, 'conventional_cellmap'): pytest.skip() conv_lat = lat.conventional() prim_atoms = Atoms('Au', cell=lat.tocell(), pbc=1) conv_atoms = make_supercell(prim_atoms, lat.conventional_cellmap) e1 = emt_energy_per_atom(prim_atoms) e2 = emt_energy_per_atom(conv_atoms) assert e1 == pytest.approx(e2) assert conv_lat.cellpar() == pytest.approx(conv_atoms.cell.cellpar()) # Rule out also that cells could differ by a rotation: assert conv_lat.tocell()[:] == pytest.approx(conv_atoms.cell[:])
"""Bravais lattice type check. 1) For each Bravais variant, check that we recognize the standard cell correctly. 2) For those Bravais lattices that we can recognize in non-standard form, Niggli-reduce them and recognize them as well.""" import numpy as np from ase.lattice import (get_lattice_from_canonical_cell, all_variants, identify_lattice) for lat in all_variants(): if lat.ndim == 2: break cell = lat.tocell() def check(lat1): print('check', repr(lat), '-->', repr(lat1)) err = np.abs(cell.cellpar() - lat1.cellpar()).max() assert err < 1e-5, err check(get_lattice_from_canonical_cell(cell)) if lat.name == 'TRI': # The TRI lattices generally permute (the ones we produce as # all_variants() are reduced to a form with smaller # orthogonality defect) which might be desirable but would # trigger an error in this test. continue
"""Bravais lattice type check. 1) For each Bravais variant, check that we recognize the standard cell correctly. 2) For those Bravais lattices that we can recognize in non-standard form, Niggli-reduce them and recognize them as well.""" import pytest import numpy as np from ase.lattice import (get_lattice_from_canonical_cell, all_variants, identify_lattice) variants = [lat for lat in all_variants() if lat.ndim == 3] @pytest.mark.parametrize('lat', variants) def test_lattice(lat): cell = lat.tocell() def check(lat1): print('check', repr(lat), '-->', repr(lat1)) err = np.abs(cell.cellpar() - lat1.cellpar()).max() assert err < 1e-5, err check(get_lattice_from_canonical_cell(cell)) if lat.name == 'TRI': # The TRI lattices generally permute (the ones we produce as
import pytest from ase.calculators.calculator import kpts2kpts from ase.lattice import all_variants from ase import Atoms # This function tests whether giving a bandpath # and an atoms object with a completed cell to # kpts2kpts actually produces a band path with # the same special points in the end. If this # isn't fulfilled then it means that something # has gone wrong (most likely that the bravais # lattice wasn't correctly identified). @pytest.mark.parametrize('lat', all_variants()) def test_kpts2kpts(lat): print() print(lat) bandpath = lat.bandpath() a = Atoms() a.cell = lat.tocell().complete() a.pbc[:lat.ndim] = True path = {'path': bandpath.path} bandpath2 = kpts2kpts(path, atoms=a) print('cell', a.cell) print('Original', bandpath) print('path', path) print('Produced by kpts2kpts', bandpath2) sp = set(bandpath.special_points) sp2 = set(bandpath2.special_points) msg = ('Input and output bandpath from kpts2kpts dont agree!\n' 'Input: {}\n Output: {}'.format(bandpath, bandpath2))