/usr/share/pyshared/ase/io/netcdf.py is in python-ase 3.6.0.2515-1.1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | """Read and write ASE2's netCDF trajectory files."""
from ase.io.pupynere import NetCDFFile
from ase.atoms import Atoms
from ase.calculators.singlepoint import SinglePointCalculator
def read_netcdf(filename, index=-1):
nc = NetCDFFile(filename)
dims = nc.dimensions
vars = nc.variables
positions = vars['CartesianPositions']
numbers = vars['AtomicNumbers'][:]
pbc = vars['BoundaryConditions'][:]
cell = vars['UnitCell']
tags = vars['Tags'][:]
if not tags.any():
tags = None
magmoms = vars['MagneticMoments'][:]
if not magmoms.any():
magmoms = None
nimages = positions.shape[0]
attach_calculator = False
if 'PotentialEnergy' in vars:
energy = vars['PotentialEnergy']
attach_calculator = True
else:
energy = nimages * [None]
if 'CartesianForces' in vars:
forces = vars['CartesianForces']
attach_calculator = True
else:
forces = nimages * [None]
if 'Stress' in vars:
stress = vars['Stress']
attach_calculator = True
else:
stress = nimages * [None]
if isinstance(index, int):
indices = [index]
else:
indices = range(nimages)[index]
images = []
for i in indices:
atoms = Atoms(positions=positions[i],
numbers=numbers,
cell=cell[i],
pbc=pbc,
tags=tags, magmoms=magmoms)
if attach_calculator:
calc = SinglePointCalculator(energy[i], forces[i], stress[i],
None, atoms) ### Fixme magmoms
atoms.set_calculator(calc)
images.append(atoms)
if isinstance(index, int):
return images[0]
else:
return images
class LOA:
def __init__(self, images):
self.set_atoms(images[0])
def __len__(self):
return len(self.atoms)
def set_atoms(self, atoms):
self.atoms = atoms
def GetPotentialEnergy(self):
return self.atoms.get_potential_energy()
def GetCartesianForces(self):
return self.atoms.get_forces()
def GetUnitCell(self):
return self.atoms.get_cell()
def GetAtomicNumbers(self):
return self.atoms.get_atomic_numbers()
def GetCartesianPositions(self):
return self.atoms.get_positions()
def GetBoundaryConditions(self):
return self.atoms.get_pbc()
def write_netcdf(filename, images):
from ASE.Trajectories.NetCDFTrajectory import NetCDFTrajectory
if not isinstance(images, (list, tuple)):
images = [images]
loa = LOA(images)
traj = NetCDFTrajectory(filename, loa)
for atoms in images:
loa.set_atoms(atoms)
traj.Update()
traj.Close()
|