|
9 | 9 | try:
|
10 | 10 | import ase.io
|
11 | 11 | from ase.calculators.calculator import PropertyNotImplementedError
|
| 12 | + from ase.io import Trajectory |
12 | 13 |
|
13 | 14 | if TYPE_CHECKING:
|
14 | 15 | from ase.optimize.optimize import Optimizer
|
@@ -43,7 +44,7 @@ def from_system(self, atoms: "ase.Atoms", **kwargs) -> dict:
|
43 | 44 | data dict
|
44 | 45 | """
|
45 | 46 | symbols = atoms.get_chemical_symbols()
|
46 |
| - atom_names = list(set(symbols)) |
| 47 | + atom_names = list(dict.fromkeys(symbols)) |
47 | 48 | atom_numbs = [symbols.count(symbol) for symbol in atom_names]
|
48 | 49 | atom_types = np.array([atom_names.index(symbol) for symbol in symbols]).astype(
|
49 | 50 | int
|
@@ -187,6 +188,115 @@ def to_labeled_system(self, data, *args, **kwargs):
|
187 | 188 | return structures
|
188 | 189 |
|
189 | 190 |
|
| 191 | +@Format.register("ase/traj") |
| 192 | +class ASETrajFormat(Format): |
| 193 | + """Format for the ASE's trajectory format <https://wiki.fysik.dtu.dk/ase/ase/io/trajectory.html#module-ase.io.trajectory>`_ (ase).' |
| 194 | + a `traj' contains a sequence of frames, each of which is an `Atoms' object. |
| 195 | + """ |
| 196 | + |
| 197 | + def from_system( |
| 198 | + self, |
| 199 | + file_name: str, |
| 200 | + begin: Optional[int] = 0, |
| 201 | + end: Optional[int] = None, |
| 202 | + step: Optional[int] = 1, |
| 203 | + **kwargs, |
| 204 | + ) -> dict: |
| 205 | + """Read ASE's trajectory file to `System` of multiple frames. |
| 206 | +
|
| 207 | + Parameters |
| 208 | + ---------- |
| 209 | + file_name : str |
| 210 | + ASE's trajectory file |
| 211 | + begin : int, optional |
| 212 | + begin frame index |
| 213 | + end : int, optional |
| 214 | + end frame index |
| 215 | + step : int, optional |
| 216 | + frame index step |
| 217 | + **kwargs : dict |
| 218 | + other parameters |
| 219 | +
|
| 220 | + Returns |
| 221 | + ------- |
| 222 | + dict_frames: dict |
| 223 | + a dictionary containing data of multiple frames |
| 224 | + """ |
| 225 | + traj = Trajectory(file_name) |
| 226 | + sub_traj = traj[begin:end:step] |
| 227 | + dict_frames = ASEStructureFormat().from_system(sub_traj[0]) |
| 228 | + for atoms in sub_traj[1:]: |
| 229 | + tmp = ASEStructureFormat().from_system(atoms) |
| 230 | + dict_frames["cells"] = np.append(dict_frames["cells"], tmp["cells"][0]) |
| 231 | + dict_frames["coords"] = np.append(dict_frames["coords"], tmp["coords"][0]) |
| 232 | + |
| 233 | + ## Correct the shape of numpy arrays |
| 234 | + dict_frames["cells"] = dict_frames["cells"].reshape(-1, 3, 3) |
| 235 | + dict_frames["coords"] = dict_frames["coords"].reshape(len(sub_traj), -1, 3) |
| 236 | + |
| 237 | + return dict_frames |
| 238 | + |
| 239 | + def from_labeled_system( |
| 240 | + self, |
| 241 | + file_name: str, |
| 242 | + begin: Optional[int] = 0, |
| 243 | + end: Optional[int] = None, |
| 244 | + step: Optional[int] = 1, |
| 245 | + **kwargs, |
| 246 | + ) -> dict: |
| 247 | + """Read ASE's trajectory file to `System` of multiple frames. |
| 248 | +
|
| 249 | + Parameters |
| 250 | + ---------- |
| 251 | + file_name : str |
| 252 | + ASE's trajectory file |
| 253 | + begin : int, optional |
| 254 | + begin frame index |
| 255 | + end : int, optional |
| 256 | + end frame index |
| 257 | + step : int, optional |
| 258 | + frame index step |
| 259 | + **kwargs : dict |
| 260 | + other parameters |
| 261 | +
|
| 262 | + Returns |
| 263 | + ------- |
| 264 | + dict_frames: dict |
| 265 | + a dictionary containing data of multiple frames |
| 266 | + """ |
| 267 | + traj = Trajectory(file_name) |
| 268 | + sub_traj = traj[begin:end:step] |
| 269 | + |
| 270 | + ## check if the first frame has a calculator |
| 271 | + if sub_traj[0].calc is None: |
| 272 | + raise ValueError( |
| 273 | + "The input trajectory does not contain energies and forces, may not be a labeled system." |
| 274 | + ) |
| 275 | + |
| 276 | + dict_frames = ASEStructureFormat().from_labeled_system(sub_traj[0]) |
| 277 | + for atoms in sub_traj[1:]: |
| 278 | + tmp = ASEStructureFormat().from_labeled_system(atoms) |
| 279 | + dict_frames["cells"] = np.append(dict_frames["cells"], tmp["cells"][0]) |
| 280 | + dict_frames["coords"] = np.append(dict_frames["coords"], tmp["coords"][0]) |
| 281 | + dict_frames["energies"] = np.append( |
| 282 | + dict_frames["energies"], tmp["energies"][0] |
| 283 | + ) |
| 284 | + dict_frames["forces"] = np.append(dict_frames["forces"], tmp["forces"][0]) |
| 285 | + if "virials" in tmp.keys() and "virials" in dict_frames.keys(): |
| 286 | + dict_frames["virials"] = np.append( |
| 287 | + dict_frames["virials"], tmp["virials"][0] |
| 288 | + ) |
| 289 | + |
| 290 | + ## Correct the shape of numpy arrays |
| 291 | + dict_frames["cells"] = dict_frames["cells"].reshape(-1, 3, 3) |
| 292 | + dict_frames["coords"] = dict_frames["coords"].reshape(len(sub_traj), -1, 3) |
| 293 | + dict_frames["forces"] = dict_frames["forces"].reshape(len(sub_traj), -1, 3) |
| 294 | + if "virials" in dict_frames.keys(): |
| 295 | + dict_frames["virials"] = dict_frames["virials"].reshape(-1, 3, 3) |
| 296 | + |
| 297 | + return dict_frames |
| 298 | + |
| 299 | + |
190 | 300 | @Driver.register("ase")
|
191 | 301 | class ASEDriver(Driver):
|
192 | 302 | """ASE Driver.
|
|
0 commit comments