@@ -18,41 +18,101 @@ class ASEStructureFormat(Format):
18
18
automatic detection fails.
19
19
"""
20
20
21
- def from_labeled_system (self , data , ** kwargs ):
22
- return data
23
-
24
- def from_multi_systems (self , file_name , begin = None , end = None , step = None , ase_fmt = None , ** kwargs ):
21
+ def from_system (self , atoms : "ase.Atoms" , ** kwargs ) -> dict :
22
+ """Convert ase.Atoms to a System.
23
+
24
+ Parameters
25
+ ----------
26
+ atoms : ase.Atoms
27
+ an ASE Atoms, containing a structure
28
+
29
+ Returns
30
+ -------
31
+ dict
32
+ data dict
33
+ """
34
+ symbols = atoms .get_chemical_symbols ()
35
+ atom_names = list (set (symbols ))
36
+ atom_numbs = [symbols .count (symbol ) for symbol in atom_names ]
37
+ atom_types = np .array ([atom_names .index (symbol ) for symbol in symbols ]).astype (int )
38
+ cells = atoms .cell [:]
39
+ coords = atoms .get_positions ()
40
+ info_dict = {
41
+ 'atom_names' : atom_names ,
42
+ 'atom_numbs' : atom_numbs ,
43
+ 'atom_types' : atom_types ,
44
+ 'cells' : np .array ([cells ]).astype ('float32' ),
45
+ 'coords' : np .array ([coords ]).astype ('float32' ),
46
+ 'orig' : [0 ,0 ,0 ],
47
+ }
48
+ return info_dict
49
+
50
+ def from_labeled_system (self , atoms : "ase.Atoms" , ** kwargs ) -> dict :
51
+ """Convert ase.Atoms to a LabeledSystem. Energies and forces
52
+ are calculated by the calculator.
53
+
54
+ Parameters
55
+ ----------
56
+ atoms : ase.Atoms
57
+ an ASE Atoms, containing a structure
58
+
59
+ Returns
60
+ -------
61
+ dict
62
+ data dict
63
+
64
+ Raises
65
+ ------
66
+ RuntimeError
67
+ ASE will raise RuntimeError if the atoms does not
68
+ have a calculator
69
+ """
70
+ info_dict = self .from_system (atoms )
71
+ try :
72
+ energies = atoms .get_potential_energy (force_consistent = True )
73
+ except PropertyNotImplementedError :
74
+ energies = atoms .get_potential_energy ()
75
+ forces = atoms .get_forces ()
76
+ info_dict = {
77
+ ** info_dict ,
78
+ 'energies' : np .array ([energies ]).astype ('float32' ),
79
+ 'forces' : np .array ([forces ]).astype ('float32' ),
80
+ }
81
+ try :
82
+ stress = atoms .get_stress (False )
83
+ except PropertyNotImplementedError :
84
+ pass
85
+ else :
86
+ virials = np .array ([- atoms .get_volume () * stress ]).astype ('float32' )
87
+ info_dict ['virials' ] = virials
88
+ return info_dict
89
+
90
+ def from_multi_systems (self , file_name : str , begin : int = None , end : int = None , step : int = None , ase_fmt : str = None , ** kwargs ) -> "ase.Atoms" :
91
+ """Convert a ASE supported file to ASE Atoms.
92
+
93
+ It will finally be converted to MultiSystems.
94
+
95
+ Parameters
96
+ ----------
97
+ file_name : str
98
+ path to file
99
+ begin : int, optional
100
+ begin frame index
101
+ end : int, optional
102
+ end frame index
103
+ step : int, optional
104
+ frame index step
105
+ ase_fmt : str, optional
106
+ ASE format. See the ASE documentation about supported formats
107
+
108
+ Yields
109
+ ------
110
+ ase.Atoms
111
+ ASE atoms in the file
112
+ """
25
113
frames = ase .io .read (file_name , format = ase_fmt , index = slice (begin , end , step ))
26
114
for atoms in frames :
27
- symbols = atoms .get_chemical_symbols ()
28
- atom_names = list (set (symbols ))
29
- atom_numbs = [symbols .count (symbol ) for symbol in atom_names ]
30
- atom_types = np .array ([atom_names .index (symbol ) for symbol in symbols ]).astype (int )
31
-
32
- cells = atoms .cell [:]
33
- coords = atoms .get_positions ()
34
- try :
35
- energies = atoms .get_potential_energy (force_consistent = True )
36
- except PropertyNotImplementedError :
37
- energies = atoms .get_potential_energy ()
38
- forces = atoms .get_forces ()
39
- info_dict = {
40
- 'atom_names' : atom_names ,
41
- 'atom_numbs' : atom_numbs ,
42
- 'atom_types' : atom_types ,
43
- 'cells' : np .array ([cells ]).astype ('float32' ),
44
- 'coords' : np .array ([coords ]).astype ('float32' ),
45
- 'energies' : np .array ([energies ]).astype ('float32' ),
46
- 'forces' : np .array ([forces ]).astype ('float32' ),
47
- 'orig' : [0 ,0 ,0 ],
48
- }
49
- try :
50
- stress = atoms .get_stress (False )
51
- virials = np .array ([- atoms .get_volume () * stress ]).astype ('float32' )
52
- info_dict ['virials' ] = virials
53
- except PropertyNotImplementedError :
54
- pass
55
- yield info_dict
115
+ yield atoms
56
116
57
117
def to_system (self , data , ** kwargs ):
58
118
'''
0 commit comments