Coverage for ase/cli/reciprocal.py: 76.09%

46 statements  

« prev     ^ index     » next       coverage.py v7.16.2, created at 2026-11-27 15:21 +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 

5 

6 

7def plot_reciprocal_cell(path, output=None): 

8 import matplotlib.pyplot as plt 

9 

10 path.plot() 

11 

12 if output: 

13 plt.savefig(output) 

14 else: 

15 plt.show() 

16 

17 

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 

22 

23 try: 

24 return read(filename) 

25 except UnknownFileTypeError: 

26 # Probably a bandpath/bandstructure: 

27 return read_json(filename) 

28 

29 

30def obj2bandpath(obj): 

31 from ase import Atoms 

32 from ase.dft.kpoints import BandPath 

33 

34 if isinstance(obj, BandPath): 

35 print('Object is a band path') 

36 print(obj) 

37 return obj 

38 

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 

44 

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) 

52 

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

54 

55 

56class CLICommand: 

57 """Show the reciprocal space. 

58 

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. 

62 

63 Examples 

64 -------- 

65 

66 $ ase build -x fcc Al al.traj 

67 $ ase reciprocal al.traj 

68 """ 

69 

70 @staticmethod 

71 def add_arguments(parser): 

72 add = parser.add_argument 

73 add( 

74 'name', 

75 metavar='input-file', 

76 help='Input file containing unit cell.', 

77 ) 

78 add('output', nargs='?', help='Write plot to file (.png, .svg, ...).') 

79 

80 @staticmethod 

81 def run(args, parser): 

82 obj = read_object(args.name) 

83 path = obj2bandpath(obj) 

84 plot_reciprocal_cell(path, output=args.output)