1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
|
"""Trajectory"""
import contextlib
import io
import warnings
from typing import Tuple
import numpy as np
from ase import __version__
from ase.atoms import Atoms
from ase.calculators.calculator import PropertyNotImplementedError
from ase.calculators.singlepoint import SinglePointCalculator, all_properties
from ase.io.formats import is_compressed
from ase.io.jsonio import decode, encode
from ase.io.pickletrajectory import PickleTrajectory
from ase.parallel import world
from ase.utils import tokenize_version
__all__ = ['Trajectory', 'PickleTrajectory']
def Trajectory(filename, mode='r', atoms=None, properties=None, master=None,
comm=world):
"""A Trajectory can be created in read, write or append mode.
Parameters:
filename: str
The name of the file. Traditionally ends in .traj.
mode: str
The mode. 'r' is read mode, the file should already exist, and
no atoms argument should be specified.
'w' is write mode. The atoms argument specifies the Atoms
object to be written to the file, if not given it must instead
be given as an argument to the write() method.
'a' is append mode. It acts as write mode, except that
data is appended to a preexisting file.
atoms: Atoms object
The Atoms object to be written in write or append mode.
properties: list of str
If specified, these calculator properties are saved in the
trajectory. If not specified, all supported quantities are
saved. Possible values: energy, forces, stress, dipole,
charges, magmom and magmoms.
master: bool
Controls which process does the actual writing. The
default is that process number 0 does this. If this
argument is given, processes where it is True will write.
comm: Communicator object
Communicator to handle parallel file reading and writing.
The atoms, properties and master arguments are ignored in read mode.
"""
if mode == 'r':
return TrajectoryReader(filename)
return TrajectoryWriter(filename, mode, atoms, properties, master=master,
comm=comm)
class TrajectoryWriter:
"""Writes Atoms objects to a .traj file."""
def __init__(self, filename, mode='w', atoms=None, properties=None,
master=None, comm=world):
"""A Trajectory writer, in write or append mode.
Parameters:
filename: str
The name of the file. Traditionally ends in .traj.
mode: str
The mode. 'r' is read mode, the file should already exist, and
no atoms argument should be specified.
'w' is write mode. The atoms argument specifies the Atoms
object to be written to the file, if not given it must instead
be given as an argument to the write() method.
'a' is append mode. It acts as write mode, except that
data is appended to a preexisting file.
atoms: Atoms object
The Atoms object to be written in write or append mode.
properties: list of str
If specified, these calculator properties are saved in the
trajectory. If not specified, all supported quantities are
saved. Possible values: energy, forces, stress, dipole,
charges, magmom and magmoms.
master: bool
Controls which process does the actual writing. The
default is that process number 0 does this. If this
argument is given, processes where it is True will write.
comm: MPI communicator
MPI communicator for this trajectory writer, by default world.
Passing a different communicator facilitates writing of
different trajectories on different MPI ranks.
"""
if master is None:
master = comm.rank == 0
self.filename = filename
self.mode = mode
self.atoms = atoms
self.properties = properties
self.master = master
self.comm = comm
self.description = {}
self.header_data = None
self.multiple_headers = False
self._open(filename, mode)
def __enter__(self):
return self
def __exit__(self, exc_type, exc_value, tb):
self.close()
def set_description(self, description):
self.description.update(description)
def _open(self, filename, mode):
import ase.io.ulm as ulm
if mode not in 'aw':
raise ValueError('mode must be "w" or "a".')
if self.master:
self.backend = ulm.open(filename, mode, tag='ASE-Trajectory')
if len(self.backend) > 0 and mode == 'a':
with Trajectory(filename) as traj:
atoms = traj[0]
self.header_data = get_header_data(atoms)
else:
self.backend = ulm.DummyWriter()
def write(self, atoms=None, **kwargs):
"""Write the atoms to the file.
If the atoms argument is not given, the atoms object specified
when creating the trajectory object is used.
Use keyword arguments to add extra properties::
writer.write(atoms, energy=117, dipole=[0, 0, 1.0])
"""
if atoms is None:
atoms = self.atoms
for image in atoms.iterimages():
self._write_atoms(image, **kwargs)
def _write_atoms(self, atoms, **kwargs):
b = self.backend
if self.header_data is None:
b.write(version=1, ase_version=__version__)
if self.description:
b.write(description=self.description)
# Atomic numbers and periodic boundary conditions are written
# in the header in the beginning.
#
# If an image later on has other numbers/pbc, we write a new
# header. All subsequent images will then have their own header
# whether or not their numbers/pbc change.
self.header_data = get_header_data(atoms)
write_header = True
else:
if not self.multiple_headers:
header_data = get_header_data(atoms)
self.multiple_headers = not headers_equal(self.header_data,
header_data)
write_header = self.multiple_headers
write_atoms(b, atoms, write_header=write_header)
calc = atoms.calc
if calc is None and len(kwargs) > 0:
calc = SinglePointCalculator(atoms)
if calc is not None:
if not hasattr(calc, 'get_property'):
calc = OldCalculatorWrapper(calc)
c = b.child('calculator')
c.write(name=calc.name)
if hasattr(calc, 'todict'):
c.write(parameters=calc.todict())
for prop in all_properties:
if prop in kwargs:
x = kwargs[prop]
else:
if self.properties is not None:
if prop in self.properties:
x = calc.get_property(prop, atoms)
else:
x = None
else:
try:
x = calc.get_property(prop, atoms,
allow_calculation=False)
except (PropertyNotImplementedError, KeyError):
# KeyError is needed for Jacapo.
# XXX We can perhaps remove this.
x = None
if x is not None:
if prop in ['stress', 'dipole']:
x = x.tolist()
c.write(prop, x)
info = {}
for key, value in atoms.info.items():
try:
encode(value)
except TypeError:
warnings.warn(f'Skipping "{key}" info.')
else:
info[key] = value
if info:
b.write(info=info)
b.sync()
def close(self):
"""Close the trajectory file."""
self.backend.close()
def __len__(self):
return self.comm.sum_scalar(len(self.backend))
class TrajectoryReader:
"""Reads Atoms objects from a .traj file."""
def __init__(self, filename):
"""A Trajectory in read mode.
The filename traditionally ends in .traj.
"""
self.filename = filename
self.numbers = None
self.pbc = None
self.masses = None
self._open(filename)
def __enter__(self):
return self
def __exit__(self, exc_type, exc_value, tb):
self.close()
def _open(self, filename):
import ase.io.ulm as ulm
self.backend = ulm.open(filename, 'r')
self._read_header()
def _read_header(self):
b = self.backend
if b.get_tag() != 'ASE-Trajectory':
raise OSError('This is not a trajectory file!')
if len(b) > 0:
self.pbc = b.pbc
self.numbers = b.numbers
self.masses = b.get('masses')
self.constraints = b.get('constraints', '[]')
self.description = b.get('description')
self.version = b.version
self.ase_version = b.get('ase_version')
def close(self):
"""Close the trajectory file."""
self.backend.close()
def __getitem__(self, i=-1):
if isinstance(i, slice):
return SlicedTrajectory(self, i)
b = self.backend[i]
if 'numbers' in b:
# numbers and other header info was written alongside the image:
atoms = read_atoms(b, traj=self)
else:
# header info was not written because they are the same:
atoms = read_atoms(b,
header=[self.pbc, self.numbers, self.masses,
self.constraints],
traj=self)
if 'calculator' in b:
results = {}
implemented_properties = []
c = b.calculator
for prop in all_properties:
if prop in c:
results[prop] = c.get(prop)
implemented_properties.append(prop)
calc = SinglePointCalculator(atoms, **results)
calc.name = b.calculator.name
calc.implemented_properties = implemented_properties
if 'parameters' in c:
calc.parameters.update(c.parameters)
atoms.calc = calc
return atoms
def __len__(self):
return len(self.backend)
def __iter__(self):
for i in range(len(self)):
yield self[i]
class SlicedTrajectory:
"""Wrapper to return a slice from a trajectory without loading
from disk. Initialize with a trajectory (in read mode) and the
desired slice object."""
def __init__(self, trajectory, sliced):
self.trajectory = trajectory
self.map = range(len(self.trajectory))[sliced]
def __getitem__(self, i):
if isinstance(i, slice):
# Map directly to the original traj, not recursively.
traj = SlicedTrajectory(self.trajectory, slice(0, None))
traj.map = self.map[i]
return traj
return self.trajectory[self.map[i]]
def __len__(self):
return len(self.map)
def get_header_data(atoms):
return {'pbc': atoms.pbc.copy(),
'numbers': atoms.get_atomic_numbers(),
'masses': atoms.get_masses() if atoms.has('masses') else None,
'constraints': list(atoms.constraints)}
def headers_equal(headers1, headers2):
assert len(headers1) == len(headers2)
eq = True
for key in headers1:
eq &= np.array_equal(headers1[key], headers2[key])
return eq
class VersionTooOldError(Exception):
pass
def read_atoms(backend,
header: Tuple = None,
traj: TrajectoryReader = None,
_try_except: bool = True) -> Atoms:
from ase.constraints import dict2constraint
if _try_except:
try:
return read_atoms(backend, header, traj, False)
except Exception as ex:
if (traj is not None and tokenize_version(__version__) <
tokenize_version(traj.ase_version)):
msg = ('You are trying to read a trajectory file written '
f'by ASE-{traj.ase_version} from ASE-{__version__}. '
'It might help to update your ASE')
raise VersionTooOldError(msg) from ex
else:
raise
b = backend
if header:
pbc, numbers, masses, constraints = header
else:
pbc = b.pbc
numbers = b.numbers
masses = b.get('masses')
constraints = b.get('constraints', '[]')
atoms = Atoms(positions=b.positions,
numbers=numbers,
cell=b.cell,
masses=masses,
pbc=pbc,
info=b.get('info'),
constraint=[dict2constraint(d)
for d in decode(constraints)],
momenta=b.get('momenta'),
magmoms=b.get('magmoms'),
charges=b.get('charges'),
tags=b.get('tags'))
return atoms
def write_atoms(backend, atoms, write_header=True):
b = backend
if write_header:
b.write(pbc=atoms.pbc.tolist(),
numbers=atoms.numbers)
if atoms.constraints:
if all(hasattr(c, 'todict') for c in atoms.constraints):
b.write(constraints=encode(atoms.constraints))
if atoms.has('masses'):
b.write(masses=atoms.get_masses())
b.write(positions=atoms.get_positions(),
cell=atoms.get_cell().tolist())
if atoms.has('tags'):
b.write(tags=atoms.get_tags())
if atoms.has('momenta'):
b.write(momenta=atoms.get_momenta())
if atoms.has('initial_magmoms'):
b.write(magmoms=atoms.get_initial_magnetic_moments())
if atoms.has('initial_charges'):
b.write(charges=atoms.get_initial_charges())
def read_traj(fd, index):
trj = TrajectoryReader(fd)
for i in range(*index.indices(len(trj))):
yield trj[i]
@contextlib.contextmanager
def defer_compression(fd):
"""Defer the file compression until all the configurations are read."""
# We do this because the trajectory and compressed-file
# internals do not play well together.
# Be advised not to defer compression of very long trajectories
# as they use a lot of memory.
if is_compressed(fd):
with io.BytesIO() as bytes_io:
try:
# write the uncompressed data to the buffer
yield bytes_io
finally:
# write the buffered data to the compressed file
bytes_io.seek(0)
fd.write(bytes_io.read())
else:
yield fd
def write_traj(fd, images):
"""Write image(s) to trajectory."""
if isinstance(images, Atoms):
images = [images]
with defer_compression(fd) as fd_uncompressed:
trj = TrajectoryWriter(fd_uncompressed)
for atoms in images:
trj.write(atoms)
class OldCalculatorWrapper:
def __init__(self, calc):
self.calc = calc
try:
self.name = calc.name
except AttributeError:
self.name = calc.__class__.__name__.lower()
def get_property(self, prop, atoms, allow_calculation=True):
try:
if (not allow_calculation and
self.calc.calculation_required(atoms, [prop])):
return None
except AttributeError:
pass
method = 'get_' + {'energy': 'potential_energy',
'magmom': 'magnetic_moment',
'magmoms': 'magnetic_moments',
'dipole': 'dipole_moment'}.get(prop, prop)
try:
result = getattr(self.calc, method)(atoms)
except AttributeError:
raise PropertyNotImplementedError
return result
def convert(name):
import os
t = TrajectoryWriter(name + '.new')
for atoms in PickleTrajectory(name, _warn=False):
t.write(atoms)
t.close()
os.rename(name, name + '.old')
os.rename(name + '.new', name)
def main():
import optparse
parser = optparse.OptionParser(usage='python -m ase.io.trajectory '
'a1.traj [a2.traj ...]',
description='Convert old trajectory '
'file(s) to new format. '
'The old file is kept as a1.traj.old.')
_opts, args = parser.parse_args()
for name in args:
convert(name)
if __name__ == '__main__':
main()
|