Coverage for /builds/kinetik161/ase/ase/cli/dimensionality.py: 100.00%

37 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 

4 

5 

6class CLICommand: 

7 """Analyze the dimensionality of the bonded clusters in a structure, using 

8 the scoring parameter described in: 

9 

10 "Definition of a scoring parameter to identify low-dimensional materials 

11 components", P.M. Larsen, M. Pandey, M. Strange, and K. W. Jacobsen 

12 Phys. Rev. Materials 3 034003, 2019, 

13 https://doi.org/10.1103/PhysRevMaterials.3.034003 

14 https://arxiv.org/abs/1808.02114 

15 

16 A score in the range [0-1] is assigned to each possible dimensionality 

17 classification. The scores sum to 1. A bonded cluster can be a molecular 

18 (0D), chain (1D), layer (2D), or bulk (3D) cluster. Mixed dimensionalities, 

19 such as 0D+3D are possible. Input files may use any format supported by 

20 ASE. 

21 

22 Example usage: 

23 

24 * ase dimensionality --display-all structure.cif 

25 * ase dimensionality structure1.cif structure2.cif 

26 

27 For each structure the following data is printed: 

28 

29 * type - the dimensionalities present 

30 * score - the score of the classification 

31 * a - the start of the k-interval (see paper) 

32 * b - the end of the k-interval (see paper) 

33 * component counts - the number of clusters with each dimensionality type 

34 

35 If the `--display-all` option is used, all dimensionality classifications 

36 are displayed. 

37 """ 

38 

39 @staticmethod 

40 def add_arguments(parser): 

41 add = parser.add_argument 

42 add('filenames', nargs='+', help='input file(s) to analyze') 

43 add('--display-all', dest='full', action='store_true', 

44 help='display all dimensionality classifications') 

45 add('--no-merge', dest='no_merge', action='store_true', 

46 help='do not merge k-intervals with same dimensionality') 

47 

48 @staticmethod 

49 def run(args, parser): 

50 import os 

51 import warnings 

52 

53 from ase.geometry.dimensionality import analyze_dimensionality 

54 from ase.io import iread 

55 

56 files = [os.path.split(path)[1] for path in args.filenames] 

57 lmax = max([len(f) for f in files]) + 2 

58 

59 print('file'.ljust(lmax) + 

60 'type score a b component counts') 

61 print('=' * lmax + '===============================================') 

62 

63 merge = not args.no_merge 

64 

65 # reading CIF files can produce a ton of distracting warnings 

66 with warnings.catch_warnings(): 

67 warnings.filterwarnings('ignore') 

68 for path, f in zip(args.filenames, files): 

69 for atoms in iread(path): 

70 result = analyze_dimensionality(atoms, merge=merge) 

71 if not args.full: 

72 result = result[:1] 

73 

74 for i, entry in enumerate(result): 

75 dimtype = entry.dimtype.rjust(4) 

76 score = f'{entry.score:.3f}'.ljust(5) 

77 a = f'{entry.a:.3f}'.ljust(5) 

78 b = f'{entry.b:.3f}'.ljust(5) 

79 if i == 0: 

80 name = f.ljust(lmax) 

81 else: 

82 name = ' ' * lmax 

83 

84 line = ('{}{}' + ' {}' * 4).format(name, dimtype, 

85 score, a, b, 

86 entry.h) 

87 print(line) 

88 

89 if args.full: 

90 print()