Coverage for icet/core/structure.py: 100%

22 statements  

« prev     ^ index     » next       coverage.py v7.5.0, created at 2024-12-26 04:12 +0000

1""" 

2This module provides the Structure class. 

3""" 

4from typing import List, Sequence 

5from ase import Atom, Atoms 

6 

7from _icet import _Structure 

8from .lattice_site import LatticeSite 

9 

10 

11class Structure(_Structure): 

12 """ 

13 This class stores the cell metric, positions, chemical symbols, 

14 and periodic boundary conditions that describe a structure. It 

15 also holds information pertaining to the components that are 

16 allowed on each site and provides functionality for computing 

17 distances between sites. 

18 

19 Note 

20 ---- 

21 As a user you will usually not interact directly with objects of this type. 

22 

23 Parameters 

24 ---------- 

25 positions 

26 List of positions in Cartesian coordinates. 

27 atomic_numbers 

28 Chemical symbol of each case. 

29 cell 

30 Cell metric. 

31 pbc 

32 Periodic boundary conditions. 

33 """ 

34 

35 def __init__(self, 

36 positions: List[List[float]], 

37 atomic_numbers: List[int], 

38 cell: List[List[float]], 

39 pbc: List[bool]) -> None: 

40 _Structure.__init__(self, 

41 positions=positions, 

42 atomic_numbers=atomic_numbers, 

43 cell=cell, 

44 pbc=pbc) 

45 

46 @classmethod 

47 def from_atoms(self, conf: Atoms): 

48 """ 

49 Returns the input configuration as an icet Structure object. 

50 

51 Parameters 

52 ---------- 

53 conf 

54 Input configuration. 

55 

56 Returns 

57 ------- 

58 Structure 

59 """ 

60 return self(conf.positions, 

61 conf.get_atomic_numbers(), 

62 conf.cell, 

63 conf.pbc.tolist()) 

64 

65 def find_lattice_sites_by_positions(self, 

66 positions: List[Sequence], 

67 fractional_position_tolerance: float) -> List[LatticeSite]: 

68 """ 

69 Returns the lattice sites that match the positions. 

70 

71 Parameters 

72 ---------- 

73 positions 

74 List of positions in Cartesian coordinates. 

75 fractional_position_tolerance 

76 Tolerance for positions in fractional coordinates. 

77 """ 

78 lattice_sites = [] 

79 for position in positions: 

80 lattice_sites.append(self.find_lattice_site_by_position( 

81 position=position, 

82 fractional_position_tolerance=fractional_position_tolerance)) 

83 return lattice_sites 

84 

85 

86def _structure_to_atoms(obj) -> Atoms: 

87 """ 

88 Returns the structure as an :class:`Atoms <ase.Atoms>` object. 

89 """ 

90 conf = Atoms(pbc=obj.pbc) 

91 conf.set_cell(obj.cell) 

92 for atomic_number, position in zip(obj.atomic_numbers, obj.positions): 

93 conf.append(Atom(atomic_number, position)) 

94 return conf 

95 

96 

97# We want to be able to create ASE Atoms objects also if we 

98# have a _Structure object (returned from the C++ side) and not 

99# only if we have a Structure object, so we bind this function 

100# to _Structure instead of Structure 

101_Structure.to_atoms = _structure_to_atoms