File: reciprocal.py

package info (click to toggle)
python-ase 3.22.1-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 14,344 kB
  • sloc: python: 126,379; xml: 946; makefile: 111; javascript: 47
file content (75 lines) | stat: -rw-r--r-- 1,955 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
from ase import Atoms
from ase.io import read
from ase.io.jsonio import read_json
from ase.dft.kpoints import BandPath
from ase.cli.main import CLIError
from ase.io.formats import UnknownFileTypeError


def plot_reciprocal_cell(path, output=None):
    import matplotlib.pyplot as plt

    path.plot()

    if output:
        plt.savefig(output)
    else:
        plt.show()


def read_object(filename):
    try:
        return read(filename)
    except UnknownFileTypeError:
        # Probably a bandpath/bandstructure:
        return read_json(filename)


def obj2bandpath(obj):
    if isinstance(obj, BandPath):
        print('Object is a band path')
        print(obj)
        return obj

    if isinstance(getattr(obj, 'path', None), BandPath):
        print(f'Object contains a bandpath: {obj}')
        path = obj.path
        print(path)
        return path

    if isinstance(obj, Atoms):
        print(f'Atoms object: {obj}')
        print('Determining standard form of Bravais lattice:')
        lat = obj.cell.get_bravais_lattice(pbc=obj.pbc)
        print(lat.description())
        print('Showing default bandpath')
        return lat.bandpath(density=0)

    raise CLIError(f'Strange object: {obj}')


class CLICommand:
    """Show the reciprocal space.

    Read unit cell from a file and show a plot of the 1. Brillouin zone.  If
    the file contains information about k-points, then those can be plotted
    too.

    Examples:

        $ ase build -x fcc Al al.traj
        $ ase reciprocal al.traj
    """

    @staticmethod
    def add_arguments(parser):
        add = parser.add_argument
        add('name', metavar='input-file',
            help='Input file containing unit cell.')
        add('output', nargs='?', help='Write plot to file (.png, .svg, ...).')

    @staticmethod
    def run(args, parser):
        obj = read_object(args.name)
        path = obj2bandpath(obj)
        plot_reciprocal_cell(path, output=args.output)