root / ase / io / netcdf.py @ 14
Historique | Voir | Annoter | Télécharger (2,77 ko)
| 1 |
"""Read and write ASE2's netCDF trajectory files."""
|
|---|---|
| 2 |
|
| 3 |
from ase.io.pupynere import NetCDFFile |
| 4 |
from ase.atoms import Atoms |
| 5 |
from ase.calculators.singlepoint import SinglePointCalculator |
| 6 |
|
| 7 |
|
| 8 |
def read_netcdf(filename, index=-1): |
| 9 |
nc = NetCDFFile(filename) |
| 10 |
dims = nc.dimensions |
| 11 |
vars = nc.variables |
| 12 |
|
| 13 |
positions = vars['CartesianPositions'] |
| 14 |
numbers = vars['AtomicNumbers'][:] |
| 15 |
pbc = vars['BoundaryConditions'][:] |
| 16 |
cell = vars['UnitCell'] |
| 17 |
tags = vars['Tags'][:] |
| 18 |
if not tags.any(): |
| 19 |
tags = None
|
| 20 |
magmoms = vars['MagneticMoments'][:] |
| 21 |
if not magmoms.any(): |
| 22 |
magmoms = None
|
| 23 |
|
| 24 |
nimages = positions.shape[0]
|
| 25 |
|
| 26 |
attach_calculator = False
|
| 27 |
if 'PotentialEnergy' in vars: |
| 28 |
energy = vars['PotentialEnergy'] |
| 29 |
attach_calculator = True
|
| 30 |
else:
|
| 31 |
energy = nimages * [None]
|
| 32 |
|
| 33 |
if 'CartesianForces' in vars: |
| 34 |
forces = vars['CartesianForces'] |
| 35 |
attach_calculator = True
|
| 36 |
else:
|
| 37 |
forces = nimages * [None]
|
| 38 |
|
| 39 |
if 'Stress' in vars: |
| 40 |
stress = vars['Stress'] |
| 41 |
attach_calculator = True
|
| 42 |
else:
|
| 43 |
stress = nimages * [None]
|
| 44 |
|
| 45 |
if isinstance(index, int): |
| 46 |
indices = [index] |
| 47 |
else:
|
| 48 |
indices = range(nimages)[index]
|
| 49 |
|
| 50 |
images = [] |
| 51 |
for i in indices: |
| 52 |
atoms = Atoms(positions=positions[i], |
| 53 |
numbers=numbers, |
| 54 |
cell=cell[i], |
| 55 |
pbc=pbc, |
| 56 |
tags=tags, magmoms=magmoms) |
| 57 |
|
| 58 |
if attach_calculator:
|
| 59 |
calc = SinglePointCalculator(energy[i], forces[i], stress[i], |
| 60 |
None, atoms) ### Fixme magmoms |
| 61 |
atoms.set_calculator(calc) |
| 62 |
|
| 63 |
images.append(atoms) |
| 64 |
|
| 65 |
if isinstance(index, int): |
| 66 |
return images[0] |
| 67 |
else:
|
| 68 |
return images
|
| 69 |
|
| 70 |
|
| 71 |
class LOA: |
| 72 |
def __init__(self, images): |
| 73 |
self.set_atoms(images[0]) |
| 74 |
|
| 75 |
def __len__(self): |
| 76 |
return len(self.atoms) |
| 77 |
|
| 78 |
def set_atoms(self, atoms): |
| 79 |
self.atoms = atoms
|
| 80 |
|
| 81 |
def GetPotentialEnergy(self): |
| 82 |
return self.atoms.get_potential_energy() |
| 83 |
|
| 84 |
def GetCartesianForces(self): |
| 85 |
return self.atoms.get_forces() |
| 86 |
|
| 87 |
def GetUnitCell(self): |
| 88 |
return self.atoms.get_cell() |
| 89 |
|
| 90 |
def GetAtomicNumbers(self): |
| 91 |
return self.atoms.get_atomic_numbers() |
| 92 |
|
| 93 |
def GetCartesianPositions(self): |
| 94 |
return self.atoms.get_positions() |
| 95 |
|
| 96 |
def GetBoundaryConditions(self): |
| 97 |
return self.atoms.get_pbc() |
| 98 |
|
| 99 |
|
| 100 |
def write_netcdf(filename, images): |
| 101 |
from ASE.Trajectories.NetCDFTrajectory import NetCDFTrajectory |
| 102 |
|
| 103 |
if not isinstance(images, (list, tuple)): |
| 104 |
images = [images] |
| 105 |
|
| 106 |
loa = LOA(images) |
| 107 |
traj = NetCDFTrajectory(filename, loa) |
| 108 |
for atoms in images: |
| 109 |
loa.set_atoms(atoms) |
| 110 |
traj.Update() |
| 111 |
traj.Close() |
| 112 |
|