1 """Contains the functions used to print the trajectories and read input
2 configurations with xyz formatting.
4 Copyright (C) 2013, Joshua More and Michele Ceriotti
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http.//www.gnu.org/licenses/>.
21 print_xyz_path: Prints all the bead configurations.
22 print_xyz: Prints the centroid configurations.
23 read_xyz: Reads the cell parameters and atom configurations from a xyz file.
26 __all__
= ['print_xyz_path', 'print_xyz', 'read_xyz', 'iter_xyz']
30 import ipi
.utils
.mathtools
as mt
31 from ipi
.utils
.depend
import depstrip
32 from ipi
.engine
.atoms
import Atoms
33 from ipi
.utils
.units
import *
35 def print_xyz_path(beads
, cell
, filedesc
= sys
.stdout
):
36 """Prints all the bead configurations, into a xyz formatted file.
38 Prints all the replicas for each time step separately, rather than all at
42 beads: A beads object giving the bead positions.
43 cell: A cell object giving the system box.
44 filedesc: An open writable file object. Defaults to standard output.
47 a
, b
, c
, alpha
, beta
, gamma
= mt
.h2abc_deg(cell
.h
)
51 for j
in range(nbeads
):
52 filedesc
.write("%d\n# bead: %d CELL(abcABC): %10.5f %10.5f %10.5f %10.5f %10.5f %10.5f \n" % (natoms
, j
, a
, b
, c
, alpha
, beta
, gamma
))
53 for i
in range(natoms
):
54 qs
= depstrip(beads
.q
)
55 lab
= depstrip(beads
.names
)
56 filedesc
.write("%8s %12.5e %12.5e %12.5e\n" % (lab
[i
], qs
[j
][3*i
], qs
[j
][3*i
+1], qs
[j
][3*i
+2]))
58 def print_xyz(atoms
, cell
, filedesc
= sys
.stdout
, title
=""):
59 """Prints the centroid configurations, into a xyz formatted file.
62 atoms: An atoms object giving the centroid positions.
63 cell: A cell object giving the system box.
64 filedesc: An open writable file object. Defaults to standard output.
65 title: This gives a string to be appended to the comment line.
68 a
, b
, c
, alpha
, beta
, gamma
= mt
.h2abc_deg(cell
.h
)
71 filedesc
.write("%d\n# CELL(abcABC): %10.5f %10.5f %10.5f %10.5f %10.5f %10.5f %s\n" % ( natoms
, a
, b
, c
, alpha
, beta
, gamma
, title
))
72 # direct access to avoid unnecessary slow-down
73 qs
= depstrip(atoms
.q
)
74 lab
= depstrip(atoms
.names
)
75 for i
in range(natoms
):
76 filedesc
.write("%8s %12.5e %12.5e %12.5e\n" % (lab
[i
], qs
[3*i
], qs
[3*i
+1], qs
[3*i
+2]))
78 def read_xyz(filedesc
):
79 """Takes a xyz-style file and creates an Atoms object.
82 filedesc: An open readable file object from a xyz formatted file.
85 An Atoms object with the appropriate atom labels, masses and positions.
88 natoms
= filedesc
.readline()
90 raise EOFError("The file descriptor hit EOF.")
92 comment
= filedesc
.readline()
99 body
= filedesc
.readline()
100 if body
.strip() == "":
105 masses
.append(Elements
.mass(name
))
114 if natoms
!= len(names
):
115 raise ValueError("The number of atom records does not match the header of the xyz file.")
117 atoms
= Atoms(natoms
)
118 # for i in range(natoms):
121 # nat.name = names[i]
122 # nat.m = Elements.mass(names[i])
123 atoms
.q
= np
.asarray(qatoms
)
124 atoms
.names
= np
.asarray(names
, dtype
='|S4')
125 atoms
.m
= np
.asarray(masses
)
129 def iter_xyz(filedesc
):
130 """Takes a xyz-style file and yields one Atoms object after another.
133 filedesc: An open readable file object from a xyz formatted file.
136 Generator over the xyz trajectory, that yields
137 Atoms objects with the appropriate atom labels, masses and positions.
142 atoms
= read_xyz(filedesc
)