Coverage for ase/gui/quickinfo.py: 72.15%

79 statements  

« prev     ^ index     » next       coverage.py v7.16.2, created at 2026-11-27 15:21 +0000

1"Module for displaying information about the system." 

2 

3import warnings 

4 

5import numpy as np 

6 

7from ase.gui.i18n import _ 

8 

9ucellformat = """\ 

10 {:8.3f} {:8.3f} {:8.3f} 

11 {:8.3f} {:8.3f} {:8.3f} 

12 {:8.3f} {:8.3f} {:8.3f} 

13""" 

14 

15 

16def info(gui): 

17 images = gui.images 

18 nimg = len(images) 

19 atoms = gui.atoms 

20 

21 tokens = [] 

22 

23 def add(token=''): 

24 tokens.append(token) 

25 

26 if len(atoms) < 1: 

27 add(_('This frame has no atoms.')) 

28 else: 

29 img = gui.frame 

30 

31 if nimg == 1: 

32 add(_('Single image loaded.')) 

33 else: 

34 add(_('Image {} loaded (0–{}).').format(img, nimg - 1)) 

35 add() 

36 add(_('Number of atoms: {}').format(len(atoms))) 

37 

38 # We need to write ų further down, so we have no choice but to 

39 # use proper subscripts in the chemical formula: 

40 formula = atoms.get_chemical_formula() 

41 subscripts = dict(zip('0123456789', '₀₁₂₃₄₅₆₇₈₉')) 

42 pretty_formula = ''.join(subscripts.get(c, c) for c in formula) 

43 add(pretty_formula) 

44 

45 add() 

46 add(_('Unit cell [Å]:')) 

47 add(ucellformat.format(*atoms.cell.ravel())) 

48 periodic = [ 

49 [_('no'), _('yes')][int(periodic)] for periodic in atoms.pbc 

50 ] 

51 # TRANSLATORS: This has the form Periodic: no, no, yes 

52 add(_('Periodic: {}, {}, {}').format(*periodic)) 

53 add() 

54 

55 cellpar = atoms.cell.cellpar() 

56 add() 

57 add(_('Lengths [Å]: {:.3f}, {:.3f}, {:.3f}').format(*cellpar[:3])) 

58 add(_('Angles: {:.1f}°, {:.1f}°, {:.1f}°').format(*cellpar[3:])) 

59 

60 if atoms.cell.rank == 3: 

61 add(_('Volume: {:.3f} ų').format(atoms.cell.volume)) 

62 

63 add() 

64 

65 if nimg > 1: 

66 if all((atoms.cell == img.cell).all() for img in images): 

67 add(_('Unit cell is fixed.')) 

68 else: 

69 add(_('Unit cell varies.')) 

70 

71 if atoms.pbc[:2].all() and atoms.cell.rank >= 1: 

72 try: 

73 lat = atoms.cell.get_bravais_lattice() 

74 except RuntimeError: 

75 add(_('Could not recognize the lattice type')) 

76 except Exception: 

77 add(_('Unexpected error determining lattice type')) 

78 else: 

79 add(_('Reduced Bravais lattice:\n{}').format(lat)) 

80 

81 # Print electronic structure information if we have a calculator 

82 if atoms.calc: 

83 calc = atoms.calc 

84 

85 def getresult(name, get_quantity): 

86 # ase/io/trajectory.py line 170 does this by using 

87 # the get_property(prop, atoms, allow_calculation=False) 

88 # so that is an alternative option. 

89 try: 

90 if calc.calculation_required(atoms, [name]): 

91 quantity = None 

92 else: 

93 quantity = get_quantity() 

94 except Exception as err: 

95 quantity = None 

96 errmsg = ( 

97 'An error occurred while retrieving {} ' 

98 'from the calculator: {}'.format(name, err) 

99 ) 

100 warnings.warn(errmsg) 

101 return quantity 

102 

103 # SinglePointCalculators are named after the code which 

104 # produced the result, so this will typically list the 

105 # name of a code even if they are just cached results. 

106 add() 

107 from ase.calculators.singlepoint import SinglePointCalculator 

108 

109 if isinstance(calc, SinglePointCalculator): 

110 add(_('Calculator: {} (cached)').format(calc.name)) 

111 else: 

112 add(_('Calculator: {} (attached)').format(calc.name)) 

113 

114 energy = getresult('energy', atoms.get_potential_energy) 

115 forces = getresult('forces', atoms.get_forces) 

116 magmom = getresult('magmom', atoms.get_magnetic_moment) 

117 

118 if energy is not None: 

119 energy_str = _('Energy: {:.3f} eV').format(energy) 

120 add(energy_str) 

121 

122 if forces is not None: 

123 maxf = np.linalg.norm(forces, axis=1).max() 

124 forces_str = _('Max force: {:.3f} eV/Å').format(maxf) 

125 add(forces_str) 

126 

127 if magmom is not None: 

128 mag_str = _('Magmom: {:.3f} µ').format(magmom) 

129 add(mag_str) 

130 

131 return '\n'.join(tokens)