Coverage for /builds/kinetik161/ase/ase/cli/reciprocal.py: 76.09%
46 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-12-10 11:04 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2023-12-10 11:04 +0000
1# Note:
2# Try to avoid module level import statements here to reduce
3# import time during CLI execution
4from ase.cli.main import CLIError
7def plot_reciprocal_cell(path, output=None):
8 import matplotlib.pyplot as plt
10 path.plot()
12 if output:
13 plt.savefig(output)
14 else:
15 plt.show()
18def read_object(filename):
19 from ase.io import read
20 from ase.io.formats import UnknownFileTypeError
21 from ase.io.jsonio import read_json
23 try:
24 return read(filename)
25 except UnknownFileTypeError:
26 # Probably a bandpath/bandstructure:
27 return read_json(filename)
30def obj2bandpath(obj):
31 from ase import Atoms
32 from ase.dft.kpoints import BandPath
34 if isinstance(obj, BandPath):
35 print('Object is a band path')
36 print(obj)
37 return obj
39 if isinstance(getattr(obj, 'path', None), BandPath):
40 print(f'Object contains a bandpath: {obj}')
41 path = obj.path
42 print(path)
43 return path
45 if isinstance(obj, Atoms):
46 print(f'Atoms object: {obj}')
47 print('Determining standard form of Bravais lattice:')
48 lat = obj.cell.get_bravais_lattice(pbc=obj.pbc)
49 print(lat.description())
50 print('Showing default bandpath')
51 return lat.bandpath(density=0)
53 raise CLIError(f'Strange object: {obj}')
56class CLICommand:
57 """Show the reciprocal space.
59 Read unit cell from a file and show a plot of the 1. Brillouin zone. If
60 the file contains information about k-points, then those can be plotted
61 too.
63 Examples:
65 $ ase build -x fcc Al al.traj
66 $ ase reciprocal al.traj
67 """
69 @staticmethod
70 def add_arguments(parser):
71 add = parser.add_argument
72 add('name', metavar='input-file',
73 help='Input file containing unit cell.')
74 add('output', nargs='?', help='Write plot to file (.png, .svg, ...).')
76 @staticmethod
77 def run(args, parser):
78 obj = read_object(args.name)
79 path = obj2bandpath(obj)
80 plot_reciprocal_cell(path, output=args.output)