atomict 1.2.17__py3-none-any.whl → 1.2.20__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- atomict/__version__.py +1 -1
- atomict/cli/main.py +30 -29
- atomict/io/{msgpack.py → atoms.py} +89 -238
- atomict/io/formats/__init__.py +15 -0
- atomict/io/formats/atraj.py +99 -0
- atomict/io/formats/tess.py +319 -0
- atomict/io/formats/traj.py +62 -0
- atomict/simulation/fhi_aims.py +5 -4
- atomict/simulation/mlrelax.py +11 -10
- atomict/simulation/models.py +2 -1
- atomict/simulation/phonon.py +0 -3
- atomict/simulation/utils.py +5 -4
- {atomict-1.2.17.dist-info → atomict-1.2.20.dist-info}/METADATA +3 -1
- {atomict-1.2.17.dist-info → atomict-1.2.20.dist-info}/RECORD +18 -15
- atomict/io/trajectory.py +0 -437
- {atomict-1.2.17.dist-info → atomict-1.2.20.dist-info}/WHEEL +0 -0
- {atomict-1.2.17.dist-info → atomict-1.2.20.dist-info}/entry_points.txt +0 -0
- {atomict-1.2.17.dist-info → atomict-1.2.20.dist-info}/licenses/LICENSE +0 -0
- {atomict-1.2.17.dist-info → atomict-1.2.20.dist-info}/top_level.txt +0 -0
atomict/__version__.py
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
__version__ = "1.2.
|
|
1
|
+
__version__ = "1.2.20"
|
atomict/cli/main.py
CHANGED
|
@@ -61,8 +61,7 @@ def cli(ctx, verbose: bool):
|
|
|
61
61
|
@cli.command(default_command=True)
|
|
62
62
|
@click.argument("input_file", required=True)
|
|
63
63
|
@click.argument("output_file", required=True)
|
|
64
|
-
|
|
65
|
-
def convert(input_file, output_file, strict_map_keys):
|
|
64
|
+
def convert(input_file, output_file):
|
|
66
65
|
"""Convert between atomic structure file formats using ASE
|
|
67
66
|
|
|
68
67
|
Supports all formats that ASE can read/write, with special handling for .atm and .atraj files.
|
|
@@ -71,14 +70,13 @@ def convert(input_file, output_file, strict_map_keys):
|
|
|
71
70
|
tess input.xyz output.atm
|
|
72
71
|
tess input.traj output.atraj
|
|
73
72
|
|
|
74
|
-
Options:
|
|
75
|
-
--strict-map-keys Enable strict map keys in msgpack files (default: False)
|
|
76
73
|
"""
|
|
77
74
|
try:
|
|
78
75
|
import os.path
|
|
79
76
|
from ase.io import read, write
|
|
80
77
|
from ase.io.formats import UnknownFileTypeError
|
|
81
|
-
from atomict.io.
|
|
78
|
+
from atomict.io.formats.atraj import read_atraj, write_atraj
|
|
79
|
+
from atomict.io.formats.tess import read_tess, write_tess
|
|
82
80
|
except ImportError:
|
|
83
81
|
console.print("[red]Error: ASE (Atomic Simulation Environment) is required for file conversion.[/red]")
|
|
84
82
|
console.print("[yellow]Install it with: pip install ase[/yellow]")
|
|
@@ -103,30 +101,34 @@ def convert(input_file, output_file, strict_map_keys):
|
|
|
103
101
|
console.print(f"[red]Error: Input file '{input_file}' not found.[/red]")
|
|
104
102
|
return
|
|
105
103
|
|
|
106
|
-
|
|
107
|
-
traj_msgpack_formats = ["atraj"]
|
|
104
|
+
traj_msgpack_formats = ["atraj", "tess"]
|
|
108
105
|
|
|
109
|
-
|
|
110
|
-
|
|
106
|
+
def _is_supported(ext: str) -> bool:
|
|
107
|
+
return (
|
|
108
|
+
ext in RW_FORMATS
|
|
109
|
+
or ext in traj_msgpack_formats
|
|
110
|
+
)
|
|
111
|
+
|
|
112
|
+
def _print_supported_error(ext: str, action: str) -> None:
|
|
113
|
+
console.print(f"[red]Error: Format '{ext}' is not supported for {action}.[/red]")
|
|
111
114
|
console.print("[yellow]Supported read/write formats include:[/yellow]")
|
|
112
115
|
for i in range(0, len(RW_FORMATS), 5):
|
|
113
116
|
console.print("[yellow] " + ", ".join(RW_FORMATS[i:i+5]) + "[/yellow]")
|
|
114
|
-
console.print("[yellow]Special formats: atm (msgpack), atraj (msgpack trajectory)[/yellow]")
|
|
117
|
+
console.print("[yellow]Special formats: atm (msgpack), atraj (msgpack trajectory), tess (msgpack trajectory)[/yellow]")
|
|
118
|
+
|
|
119
|
+
if not _is_supported(input_ext):
|
|
120
|
+
_print_supported_error(input_ext, "reading")
|
|
115
121
|
return
|
|
116
|
-
|
|
117
|
-
if
|
|
118
|
-
|
|
119
|
-
console.print("[yellow]Supported read/write formats include:[/yellow]")
|
|
120
|
-
for i in range(0, len(RW_FORMATS), 5):
|
|
121
|
-
console.print("[yellow] " + ", ".join(RW_FORMATS[i:i+5]) + "[/yellow]")
|
|
122
|
-
console.print("[yellow]Special formats: atm (msgpack), atraj (msgpack trajectory)[/yellow]")
|
|
122
|
+
|
|
123
|
+
if not _is_supported(output_ext):
|
|
124
|
+
_print_supported_error(output_ext, "writing")
|
|
123
125
|
return
|
|
124
126
|
|
|
125
127
|
try:
|
|
126
|
-
if input_ext
|
|
127
|
-
atoms =
|
|
128
|
-
elif input_ext
|
|
129
|
-
atoms, _ =
|
|
128
|
+
if input_ext == "atraj":
|
|
129
|
+
atoms, _ = read_atraj(input_file)
|
|
130
|
+
elif input_ext == "tess":
|
|
131
|
+
atoms, _ = read_tess(input_file)
|
|
130
132
|
else:
|
|
131
133
|
atoms = read(input_file, index=":")
|
|
132
134
|
except UnknownFileTypeError:
|
|
@@ -140,15 +142,14 @@ def convert(input_file, output_file, strict_map_keys):
|
|
|
140
142
|
return
|
|
141
143
|
|
|
142
144
|
try:
|
|
143
|
-
if output_ext
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
save_msgpack_trajectory(atoms, output_file)
|
|
148
|
-
console.print(f"[green]Successfully converted {input_file} to {output_file} (MSGPACK trajectory format)[/green]")
|
|
145
|
+
if output_ext == "atraj":
|
|
146
|
+
write_atraj(atoms, output_file)
|
|
147
|
+
elif output_ext == "tess":
|
|
148
|
+
write_tess(atoms, output_file)
|
|
149
149
|
else:
|
|
150
150
|
write(output_file, atoms)
|
|
151
|
-
|
|
151
|
+
|
|
152
|
+
console.print(f"[green]Successfully converted {input_file} to {output_file}[/green]")
|
|
152
153
|
|
|
153
154
|
except UnknownFileTypeError:
|
|
154
155
|
console.print(f"[red]Error: Unknown file type for output file '{output_file}'[/red]")
|
|
@@ -199,7 +200,7 @@ if type -q tess
|
|
|
199
200
|
end
|
|
200
201
|
"""
|
|
201
202
|
click.echo(f"# Shell completion for {shell}")
|
|
202
|
-
click.echo(completion_script.strip())
|
|
203
|
+
click.echo((completion_script or "").strip())
|
|
203
204
|
click.echo(
|
|
204
205
|
"# Don't forget to source your rc file! `source ~/.bashrc` or `source ~/.zshrc` ..."
|
|
205
206
|
)
|
|
@@ -1,7 +1,11 @@
|
|
|
1
|
-
from typing import
|
|
1
|
+
from typing import List
|
|
2
|
+
import numpy as np
|
|
3
|
+
from ase import Atoms
|
|
4
|
+
from ase.constraints import dict2constraint
|
|
5
|
+
from ase.calculators.singlepoint import SinglePointCalculator
|
|
2
6
|
|
|
3
7
|
|
|
4
|
-
def atoms_to_dict(atoms_list, selective=False):
|
|
8
|
+
def atoms_to_dict(atoms_list: List[Atoms], selective: bool = False):
|
|
5
9
|
"""Extract all properties from ASE Atoms objects into a standardized dictionary.
|
|
6
10
|
|
|
7
11
|
Parameters:
|
|
@@ -16,7 +20,6 @@ def atoms_to_dict(atoms_list, selective=False):
|
|
|
16
20
|
Dict
|
|
17
21
|
Dictionary with all extracted properties
|
|
18
22
|
"""
|
|
19
|
-
import numpy as np
|
|
20
23
|
|
|
21
24
|
# Create data structure with common properties
|
|
22
25
|
data = {
|
|
@@ -205,91 +208,113 @@ def dict_to_atoms(data):
|
|
|
205
208
|
List[Atoms]
|
|
206
209
|
List of ASE Atoms objects
|
|
207
210
|
"""
|
|
208
|
-
try:
|
|
209
|
-
import numpy as np
|
|
210
|
-
from ase import Atoms
|
|
211
|
-
from ase.constraints import dict2constraint
|
|
212
|
-
from ase.calculators.singlepoint import SinglePointCalculator
|
|
213
|
-
except ImportError:
|
|
214
|
-
raise ImportError("You need to install with `pip install atomict[utils]` to use msgpack I/O")
|
|
215
211
|
|
|
216
212
|
n_frames = data['n_frames']
|
|
217
|
-
atoms_list = []
|
|
218
|
-
|
|
219
|
-
#
|
|
213
|
+
atoms_list = [None] * n_frames # type: ignore[assignment]
|
|
214
|
+
|
|
215
|
+
# Cache lookups outside loop for speed
|
|
220
216
|
unique_symbols = data['unique_symbols']
|
|
217
|
+
unique_symbols_array = np.asarray(unique_symbols, dtype=object)
|
|
221
218
|
symbols_map = data['symbols']
|
|
222
|
-
|
|
223
|
-
|
|
219
|
+
positions = data['positions']
|
|
220
|
+
cells = data['cell']
|
|
221
|
+
pbc = data['pbc']
|
|
222
|
+
n_atoms_seq = data['n_atoms']
|
|
223
|
+
|
|
224
|
+
numbers_data = data.get('numbers')
|
|
225
|
+
has_numbers = numbers_data is not None
|
|
226
|
+
|
|
227
|
+
tags = data.get('tags')
|
|
228
|
+
masses = data.get('masses')
|
|
229
|
+
momenta = data.get('momenta')
|
|
230
|
+
initial_charges = data.get('initial_charges')
|
|
231
|
+
initial_magmoms = data.get('initial_magmoms')
|
|
232
|
+
top_mask = data.get('top_mask')
|
|
233
|
+
constraints = data.get('constraints')
|
|
234
|
+
ase_objtype = data.get('ase_objtype')
|
|
235
|
+
forces = data.get('forces')
|
|
236
|
+
stress = data.get('stress')
|
|
237
|
+
atom_infos = data.get('atom_infos')
|
|
238
|
+
custom_arrays = data.get('custom_arrays')
|
|
239
|
+
calc_results = data.get('calc_results')
|
|
240
|
+
|
|
224
241
|
for i in range(n_frames):
|
|
225
|
-
|
|
226
|
-
if isinstance(
|
|
227
|
-
|
|
242
|
+
symbols_entry = symbols_map[i]
|
|
243
|
+
if isinstance(symbols_entry, np.ndarray):
|
|
244
|
+
symbols_idx = symbols_entry
|
|
228
245
|
else:
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
246
|
+
start_idx = i * n_atoms_seq[i]
|
|
247
|
+
symbols_idx = symbols_map[start_idx:start_idx + n_atoms_seq[i]]
|
|
248
|
+
|
|
249
|
+
if has_numbers:
|
|
250
|
+
atoms = Atoms(
|
|
251
|
+
numbers=numbers_data[i],
|
|
252
|
+
positions=positions[i],
|
|
253
|
+
cell=cells[i],
|
|
254
|
+
pbc=pbc[i],
|
|
255
|
+
)
|
|
256
|
+
else:
|
|
257
|
+
# Avoid numpy conversion and tolist() for small arrays
|
|
258
|
+
if isinstance(symbols_idx, np.ndarray):
|
|
259
|
+
frame_symbols = [unique_symbols[idx] for idx in symbols_idx]
|
|
260
|
+
else:
|
|
261
|
+
frame_symbols = [unique_symbols[idx] for idx in symbols_idx]
|
|
262
|
+
atoms = Atoms(
|
|
263
|
+
symbols=frame_symbols,
|
|
264
|
+
positions=positions[i],
|
|
265
|
+
cell=cells[i],
|
|
266
|
+
pbc=pbc[i],
|
|
267
|
+
)
|
|
240
268
|
|
|
241
269
|
# Set optional properties if they exist
|
|
242
|
-
if
|
|
243
|
-
atoms.set_tags(
|
|
244
|
-
|
|
245
|
-
if
|
|
246
|
-
atoms.set_masses(
|
|
247
|
-
|
|
248
|
-
if
|
|
249
|
-
atoms.set_momenta(
|
|
250
|
-
|
|
251
|
-
if
|
|
252
|
-
atoms.set_initial_charges(
|
|
253
|
-
|
|
254
|
-
if
|
|
255
|
-
atoms.set_initial_magnetic_moments(
|
|
256
|
-
|
|
257
|
-
if 'top_mask' in data and i < len(data['top_mask']) and data['top_mask'][i] is not None:
|
|
258
|
-
atoms.top_mask = np.array(data['top_mask'][i], dtype=bool)
|
|
270
|
+
if tags is not None:
|
|
271
|
+
atoms.set_tags(tags[i])
|
|
272
|
+
|
|
273
|
+
if masses is not None:
|
|
274
|
+
atoms.set_masses(masses[i])
|
|
275
|
+
|
|
276
|
+
if momenta is not None:
|
|
277
|
+
atoms.set_momenta(momenta[i])
|
|
278
|
+
|
|
279
|
+
if initial_charges is not None:
|
|
280
|
+
atoms.set_initial_charges(initial_charges[i])
|
|
281
|
+
|
|
282
|
+
if initial_magmoms is not None:
|
|
283
|
+
atoms.set_initial_magnetic_moments(initial_magmoms[i])
|
|
259
284
|
|
|
260
|
-
if
|
|
261
|
-
atoms.
|
|
285
|
+
if top_mask is not None and i < len(top_mask) and top_mask[i] is not None:
|
|
286
|
+
atoms.top_mask = np.asarray(top_mask[i], dtype=bool)
|
|
262
287
|
|
|
263
|
-
if
|
|
264
|
-
for c in
|
|
288
|
+
if constraints is not None and i < len(constraints):
|
|
289
|
+
for c in constraints[i]:
|
|
265
290
|
atoms.constraints.append(dict2constraint(c))
|
|
266
291
|
|
|
267
|
-
if
|
|
268
|
-
atoms.ase_objtype =
|
|
292
|
+
if ase_objtype is not None and i < len(ase_objtype) and ase_objtype[i] is not None:
|
|
293
|
+
atoms.ase_objtype = ase_objtype[i]
|
|
269
294
|
|
|
270
|
-
if
|
|
271
|
-
atoms.arrays['forces'] =
|
|
295
|
+
if forces is not None and i < len(forces):
|
|
296
|
+
atoms.arrays['forces'] = forces[i]
|
|
272
297
|
|
|
273
|
-
if
|
|
274
|
-
atoms.stress = np.array(
|
|
298
|
+
if stress is not None and i < len(stress):
|
|
299
|
+
atoms.stress = np.array(stress[i], dtype=np.float64).copy()
|
|
275
300
|
|
|
276
301
|
# Restore atom info
|
|
277
|
-
if
|
|
278
|
-
atoms.info.update(
|
|
302
|
+
if atom_infos is not None and i < len(atom_infos):
|
|
303
|
+
atoms.info.update(atom_infos[i])
|
|
279
304
|
|
|
280
305
|
# Restore custom arrays
|
|
281
|
-
if
|
|
282
|
-
for key, values in
|
|
306
|
+
if custom_arrays is not None:
|
|
307
|
+
for key, values in custom_arrays.items():
|
|
283
308
|
if i < len(values) and values[i] is not None:
|
|
284
309
|
atoms.arrays[key] = values[i]
|
|
285
310
|
|
|
286
311
|
# Restore calculator if present
|
|
287
312
|
calc_created = False
|
|
288
313
|
calc_data = {}
|
|
289
|
-
|
|
314
|
+
|
|
290
315
|
# First try from calc_results (new format)
|
|
291
|
-
if
|
|
292
|
-
calc_data =
|
|
316
|
+
if calc_results is not None and i < len(calc_results):
|
|
317
|
+
calc_data = calc_results[i]
|
|
293
318
|
|
|
294
319
|
if calc_data and len(calc_data) > 1: # Only create calculator if there's data beyond just the name
|
|
295
320
|
# Initialize a SinglePointCalculator
|
|
@@ -320,180 +345,6 @@ def dict_to_atoms(data):
|
|
|
320
345
|
calc.results[key] = value
|
|
321
346
|
atoms.calc = calc
|
|
322
347
|
|
|
323
|
-
atoms_list
|
|
348
|
+
atoms_list[i] = atoms
|
|
324
349
|
|
|
325
350
|
return atoms_list
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
def load_msgpack(filename: str, strict_map_key: bool = True) -> Union['ase.Atoms', List['ase.Atoms']]:
|
|
329
|
-
"""Load atoms from a msgpack file with high efficiency and speed.
|
|
330
|
-
|
|
331
|
-
Parameters:
|
|
332
|
-
-----------
|
|
333
|
-
filename : str
|
|
334
|
-
The input filename
|
|
335
|
-
strict_map_key : bool, default=False
|
|
336
|
-
If True, only allow string keys in msgpack dictionaries
|
|
337
|
-
If False, allow integer and other keys in msgpack dictionaries
|
|
338
|
-
"""
|
|
339
|
-
|
|
340
|
-
try:
|
|
341
|
-
import msgpack
|
|
342
|
-
import msgpack_numpy as m
|
|
343
|
-
except ImportError:
|
|
344
|
-
raise ImportError("You need to install with `pip install atomict[utils]` to use msgpack I/O")
|
|
345
|
-
|
|
346
|
-
# Enable numpy array deserialization
|
|
347
|
-
m.patch()
|
|
348
|
-
|
|
349
|
-
# Load data
|
|
350
|
-
with open(filename, 'rb') as f:
|
|
351
|
-
data = msgpack.unpack(f, raw=False, strict_map_key=strict_map_key)
|
|
352
|
-
|
|
353
|
-
# Convert to atoms objects
|
|
354
|
-
atoms_list = dict_to_atoms(data)
|
|
355
|
-
|
|
356
|
-
# Return single atom or list based on input
|
|
357
|
-
return atoms_list[0] if data['n_frames'] == 1 else atoms_list
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
def save_msgpack(atoms: Union['ase.Atoms', List['ase.Atoms']], filename: str):
|
|
361
|
-
"""Save atoms to a msgpack file with high efficiency and speed."""
|
|
362
|
-
|
|
363
|
-
try:
|
|
364
|
-
import msgpack
|
|
365
|
-
import msgpack_numpy as m
|
|
366
|
-
from ase import Atoms
|
|
367
|
-
except ImportError:
|
|
368
|
-
raise ImportError("You need to install with `pip install atomict[utils]` to use msgpack I/O")
|
|
369
|
-
|
|
370
|
-
# Enable numpy array serialization
|
|
371
|
-
m.patch()
|
|
372
|
-
|
|
373
|
-
# Single atoms case - convert to list
|
|
374
|
-
if isinstance(atoms, Atoms):
|
|
375
|
-
atoms_list = [atoms]
|
|
376
|
-
else:
|
|
377
|
-
atoms_list = atoms
|
|
378
|
-
|
|
379
|
-
# Extract properties to dictionary - use selective mode for single atoms
|
|
380
|
-
# to avoid storing default properties
|
|
381
|
-
selective = len(atoms_list) == 1
|
|
382
|
-
data = atoms_to_dict(atoms_list, selective=selective)
|
|
383
|
-
|
|
384
|
-
# Pack and save
|
|
385
|
-
with open(filename, 'wb') as f:
|
|
386
|
-
msgpack.pack(data, f, use_bin_type=True)
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
def save_msgpack_trajectory(atoms: Union['ase.Atoms', List['ase.Atoms']], filename: str, metadata: Dict = None):
|
|
390
|
-
"""Save atoms to a msgpack trajectory file with metadata.
|
|
391
|
-
|
|
392
|
-
Parameters:
|
|
393
|
-
-----------
|
|
394
|
-
atoms : Atoms or list of Atoms
|
|
395
|
-
The atoms to save
|
|
396
|
-
filename : str
|
|
397
|
-
The output filename
|
|
398
|
-
metadata : dict, optional
|
|
399
|
-
Additional metadata to store with the trajectory
|
|
400
|
-
"""
|
|
401
|
-
try:
|
|
402
|
-
import msgpack
|
|
403
|
-
import msgpack_numpy as m
|
|
404
|
-
from ase import Atoms
|
|
405
|
-
except ImportError:
|
|
406
|
-
raise ImportError("You need to install with `pip install atomict[utils]` to use msgpack I/O")
|
|
407
|
-
|
|
408
|
-
# Enable numpy array serialization
|
|
409
|
-
m.patch()
|
|
410
|
-
|
|
411
|
-
# Single atoms case - convert to list
|
|
412
|
-
if isinstance(atoms, Atoms):
|
|
413
|
-
atoms_list = [atoms]
|
|
414
|
-
else:
|
|
415
|
-
atoms_list = atoms
|
|
416
|
-
|
|
417
|
-
# Create container for the trajectory data
|
|
418
|
-
traj_data = {
|
|
419
|
-
'format_version': 1, # Version for future compatibility
|
|
420
|
-
'metadata': metadata or {},
|
|
421
|
-
}
|
|
422
|
-
|
|
423
|
-
# Extract properties to dictionary - no selective mode for trajectories
|
|
424
|
-
atoms_data = atoms_to_dict(atoms_list, selective=False)
|
|
425
|
-
|
|
426
|
-
# Add atoms data to the trajectory container
|
|
427
|
-
traj_data['atoms_data'] = atoms_data
|
|
428
|
-
|
|
429
|
-
# Pack and save
|
|
430
|
-
with open(filename, 'wb') as f:
|
|
431
|
-
msgpack.pack(traj_data, f, use_bin_type=True)
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
def load_msgpack_trajectory(filename: str, strict_map_key: bool = True) -> Tuple[List['ase.Atoms'], Dict]:
|
|
435
|
-
"""Load atoms from a msgpack trajectory file with metadata.
|
|
436
|
-
|
|
437
|
-
Parameters:
|
|
438
|
-
-----------
|
|
439
|
-
filename : str
|
|
440
|
-
The input filename
|
|
441
|
-
strict_map_key : bool, default=False
|
|
442
|
-
If True, only allow string keys in msgpack dictionaries
|
|
443
|
-
If False, allow integer and other keys in msgpack dictionaries
|
|
444
|
-
|
|
445
|
-
Returns:
|
|
446
|
-
--------
|
|
447
|
-
atoms_list : list of Atoms
|
|
448
|
-
The loaded atoms
|
|
449
|
-
metadata : dict
|
|
450
|
-
The metadata stored with the trajectory
|
|
451
|
-
"""
|
|
452
|
-
try:
|
|
453
|
-
import msgpack
|
|
454
|
-
import msgpack_numpy as m
|
|
455
|
-
except ImportError:
|
|
456
|
-
raise ImportError("You need to install with `pip install atomict[utils]` to use msgpack I/O")
|
|
457
|
-
|
|
458
|
-
# Enable numpy array deserialization
|
|
459
|
-
m.patch()
|
|
460
|
-
|
|
461
|
-
# Load data
|
|
462
|
-
with open(filename, 'rb') as f:
|
|
463
|
-
traj_data = msgpack.unpack(f, raw=False, strict_map_key=strict_map_key)
|
|
464
|
-
|
|
465
|
-
# Check if this is a new-style trajectory with format_version
|
|
466
|
-
if isinstance(traj_data, dict) and 'format_version' in traj_data:
|
|
467
|
-
metadata = traj_data.get('metadata', {})
|
|
468
|
-
atoms_data = traj_data.get('atoms_data', {})
|
|
469
|
-
else:
|
|
470
|
-
# Legacy format - just raw atoms data
|
|
471
|
-
metadata = {}
|
|
472
|
-
atoms_data = traj_data
|
|
473
|
-
|
|
474
|
-
# Ensure that calculated properties are transferred to the calculator in dict_to_atoms
|
|
475
|
-
if 'calc_results' not in atoms_data and hasattr(atoms_data, 'get') and atoms_data.get('forces') is not None:
|
|
476
|
-
# If we have forces in the data but no calc_results, create calc_results entries
|
|
477
|
-
calc_data_list = []
|
|
478
|
-
n_frames = atoms_data.get('n_frames', 0)
|
|
479
|
-
|
|
480
|
-
for i in range(n_frames):
|
|
481
|
-
calc_data = {'name': 'SinglePointCalculator'}
|
|
482
|
-
if 'forces' in atoms_data and i < len(atoms_data['forces']):
|
|
483
|
-
calc_data['forces'] = atoms_data['forces'][i]
|
|
484
|
-
if 'stress' in atoms_data and i < len(atoms_data['stress']):
|
|
485
|
-
calc_data['stress'] = atoms_data['stress'][i]
|
|
486
|
-
if 'energy' in atoms_data and i < len(atoms_data['energy']):
|
|
487
|
-
calc_data['energy'] = atoms_data['energy'][i]
|
|
488
|
-
calc_data_list.append(calc_data)
|
|
489
|
-
|
|
490
|
-
atoms_data['calc_results'] = calc_data_list
|
|
491
|
-
|
|
492
|
-
# Convert to atoms objects
|
|
493
|
-
atoms_list = dict_to_atoms(atoms_data)
|
|
494
|
-
|
|
495
|
-
# Make sure atoms_list is always a list
|
|
496
|
-
if not isinstance(atoms_list, list):
|
|
497
|
-
atoms_list = [atoms_list]
|
|
498
|
-
|
|
499
|
-
return atoms_list, metadata
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
from atomict.io.formats.atraj import (
|
|
2
|
+
read_atraj,
|
|
3
|
+
write_atraj,
|
|
4
|
+
)
|
|
5
|
+
from atomict.io.formats.tess import (
|
|
6
|
+
read_tess,
|
|
7
|
+
write_tess,
|
|
8
|
+
)
|
|
9
|
+
from atomict.io.formats.traj import (
|
|
10
|
+
read_traj,
|
|
11
|
+
write_traj,
|
|
12
|
+
)
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
__all__ = [read_atraj, write_atraj, read_tess, write_tess, read_traj, write_traj]
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
from typing import Union, List, Dict, TYPE_CHECKING
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
if TYPE_CHECKING:
|
|
5
|
+
import ase
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def write_atraj(atoms: Union['ase.Atoms', List['ase.Atoms']], filename: str, metadata: Dict = None):
|
|
9
|
+
"""Save atoms to a mpv1 file."""
|
|
10
|
+
|
|
11
|
+
from atomict.io.atoms import atoms_to_dict
|
|
12
|
+
|
|
13
|
+
try:
|
|
14
|
+
import msgpack
|
|
15
|
+
import msgpack_numpy as m
|
|
16
|
+
from ase import Atoms
|
|
17
|
+
except ImportError:
|
|
18
|
+
raise ImportError("You need to install with `pip install atomict[utils]` to use the newer formats")
|
|
19
|
+
|
|
20
|
+
# Enable numpy array serialization
|
|
21
|
+
m.patch()
|
|
22
|
+
|
|
23
|
+
# Single atoms case - convert to list
|
|
24
|
+
if isinstance(atoms, Atoms):
|
|
25
|
+
atoms_list = [atoms]
|
|
26
|
+
else:
|
|
27
|
+
atoms_list = atoms
|
|
28
|
+
|
|
29
|
+
# Create container for the trajectory data
|
|
30
|
+
traj_data = {
|
|
31
|
+
'format_version': 1, # Version for future compatibility
|
|
32
|
+
'metadata': metadata or {},
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
# Extract properties to dictionary - no selective mode for trajectories
|
|
36
|
+
atoms_data = atoms_to_dict(atoms_list, selective=False)
|
|
37
|
+
|
|
38
|
+
# Add atoms data to the trajectory container
|
|
39
|
+
traj_data['atoms_data'] = atoms_data
|
|
40
|
+
|
|
41
|
+
# Pack and save
|
|
42
|
+
with open(filename, 'wb') as f:
|
|
43
|
+
msgpack.pack(traj_data, f, use_bin_type=True)
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def read_atraj(filename: str) -> Union['ase.Atoms', List['ase.Atoms']]:
|
|
47
|
+
|
|
48
|
+
"""Load atoms from a mpv1 file."""
|
|
49
|
+
from atomict.io.atoms import dict_to_atoms
|
|
50
|
+
|
|
51
|
+
try:
|
|
52
|
+
import msgpack
|
|
53
|
+
import msgpack_numpy as m
|
|
54
|
+
except ImportError:
|
|
55
|
+
raise ImportError("You need to install with `pip install atomict[utils]` to use the newer formats")
|
|
56
|
+
|
|
57
|
+
# Enable numpy array deserialization
|
|
58
|
+
m.patch()
|
|
59
|
+
|
|
60
|
+
# Load data
|
|
61
|
+
with open(filename, 'rb') as f:
|
|
62
|
+
traj_data = msgpack.unpack(f, raw=False, strict_map_key=True)
|
|
63
|
+
|
|
64
|
+
# Check if this is a new-style trajectory with format_version
|
|
65
|
+
if isinstance(traj_data, dict) and 'format_version' in traj_data:
|
|
66
|
+
metadata = traj_data.get('metadata', {})
|
|
67
|
+
atoms_data = traj_data.get('atoms_data', {})
|
|
68
|
+
else:
|
|
69
|
+
# Legacy format - just raw atoms data
|
|
70
|
+
metadata = {}
|
|
71
|
+
atoms_data = traj_data
|
|
72
|
+
|
|
73
|
+
# Ensure that calculated properties are transferred to the calculator in dict_to_atoms
|
|
74
|
+
if 'calc_results' not in atoms_data and hasattr(atoms_data, 'get') and atoms_data.get('forces') is not None:
|
|
75
|
+
# If we have forces in the data but no calc_results, create calc_results entries
|
|
76
|
+
calc_data_list = []
|
|
77
|
+
n_frames = atoms_data.get('n_frames', 0)
|
|
78
|
+
|
|
79
|
+
for i in range(n_frames):
|
|
80
|
+
calc_data = {'name': 'SinglePointCalculator'}
|
|
81
|
+
if 'forces' in atoms_data and i < len(atoms_data['forces']):
|
|
82
|
+
calc_data['forces'] = atoms_data['forces'][i]
|
|
83
|
+
if 'stress' in atoms_data and i < len(atoms_data['stress']):
|
|
84
|
+
calc_data['stress'] = atoms_data['stress'][i]
|
|
85
|
+
if 'energy' in atoms_data and i < len(atoms_data['energy']):
|
|
86
|
+
calc_data['energy'] = atoms_data['energy'][i]
|
|
87
|
+
calc_data_list.append(calc_data)
|
|
88
|
+
|
|
89
|
+
atoms_data['calc_results'] = calc_data_list
|
|
90
|
+
|
|
91
|
+
# Convert to atoms objects
|
|
92
|
+
atoms_list = dict_to_atoms(atoms_data)
|
|
93
|
+
|
|
94
|
+
# Make sure atoms_list is always a list
|
|
95
|
+
if not isinstance(atoms_list, list):
|
|
96
|
+
atoms_list = [atoms_list]
|
|
97
|
+
|
|
98
|
+
return atoms_list, metadata
|
|
99
|
+
|