Coverage for ase/cli/dimensionality.py: 100.00%
37 statements
« prev ^ index » next coverage.py v7.16.2, created at 2026-11-27 15:21 +0000
« 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
6class CLICommand:
7 """Analyze dimensionality of structure.
9 This tool analyzes the dimensionality of the bonded clusters in a
10 structure using the scoring parameter described in:
12 "Definition of a scoring parameter to identify low-dimensional materials
13 components", P.M. Larsen, M. Pandey, M. Strange, and K. W. Jacobsen
14 Phys. Rev. Materials 3 034003, 2019,
15 https://doi.org/10.1103/PhysRevMaterials.3.034003
16 https://arxiv.org/abs/1808.02114
18 A score in the range [0-1] is assigned to each possible dimensionality
19 classification. The scores sum to 1. A bonded cluster can be a molecular
20 (0D), chain (1D), layer (2D), or bulk (3D) cluster. Mixed dimensionalities,
21 such as 0D+3D are possible. Input files may use any format supported by
22 ASE.
24 Example usage:
26 * ase dimensionality --display-all structure.cif
27 * ase dimensionality structure1.cif structure2.cif
29 For each structure the following data is printed:
31 * type - the dimensionalities present
32 * score - the score of the classification
33 * a - the start of the k-interval (see paper)
34 * b - the end of the k-interval (see paper)
35 * component counts - the number of clusters with each dimensionality type
37 If the `--display-all` option is used, all dimensionality classifications
38 are displayed.
40 """
42 @staticmethod
43 def add_arguments(parser):
44 add = parser.add_argument
45 add('filenames', nargs='+', help='input file(s) to analyze')
46 add(
47 '--display-all',
48 dest='full',
49 action='store_true',
50 help='display all dimensionality classifications',
51 )
52 add(
53 '--no-merge',
54 dest='no_merge',
55 action='store_true',
56 help='do not merge k-intervals with same dimensionality',
57 )
59 @staticmethod
60 def run(args, parser):
61 import os
62 import warnings
64 from ase.geometry.dimensionality import analyze_dimensionality
65 from ase.io import iread
67 files = [os.path.split(path)[1] for path in args.filenames]
68 lmax = max(len(f) for f in files) + 2
70 print(
71 'file'.ljust(lmax)
72 + 'type score a b component counts'
73 )
74 print('=' * lmax + '===============================================')
76 merge = not args.no_merge
78 # reading CIF files can produce a ton of distracting warnings
79 with warnings.catch_warnings():
80 warnings.filterwarnings('ignore')
81 for path, f in zip(args.filenames, files):
82 for atoms in iread(path):
83 result = analyze_dimensionality(atoms, merge=merge)
84 if not args.full:
85 result = result[:1]
87 for i, entry in enumerate(result):
88 dimtype = entry.dimtype.rjust(4)
89 score = f'{entry.score:.3f}'.ljust(5)
90 a = f'{entry.a:.3f}'.ljust(5)
91 b = f'{entry.b:.3f}'.ljust(5)
92 if i == 0:
93 name = f.ljust(lmax)
94 else:
95 name = ' ' * lmax
97 line = ('{}{}' + ' {}' * 4).format(
98 name, dimtype, score, a, b, entry.h
99 )
100 print(line)
102 if args.full:
103 print()