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

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 $ ase build -x fcc Al al.traj 

66 $ ase reciprocal al.traj 

67 """ 

68 

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, ...).') 

75 

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)