Exemplo n.º 1
0
def read_json(filedesc, **kwargs):
    """Reads a JSON-style file with i-pi style comments and creates an Atoms and Cell object.

    Args:
        filedesc: An open readable file object from a json formatted file with i-PI header comments.

    Returns:
        An Atoms object with the appropriate atom labels, masses and positions.
        A Cell object.
    """

    try:
        line = json.loads(filedesc.readline())
    except ValueError:
        raise EOFError("The file descriptor hit EOF.")
    atoms = Atoms(line[0])
    atoms.q = np.asarray(line[8])
    atoms.names = np.asarray(line[9], dtype='|S4')
    atoms.m = np.asarray(map(Elements.mass, atoms.names))

    a = float(line[1])
    b = float(line[2])
    c = float(line[3])
    alpha = float(line[4]) * np.pi / 180
    beta = float(line[5]) * np.pi / 180
    gamma = float(line[6]) * np.pi / 180
    h = mt.abc2h(a, b, c, alpha, beta, gamma)
    cell = Cell(h)

    return {"atoms": atoms, "cell": cell}
Exemplo n.º 2
0
def read_pdb(filedesc):
    """Takes a pdb-style file and creates an Atoms and Cell object.

   Args:
      filedesc: An open readable file object from a pdb formatted file.

   Returns:
      An Atoms object with the appropriate atom labels, masses and positions,
      and a Cell object with the appropriate cell dimensions and an estimate
      of a reasonable cell mass.
   """

    header = filedesc.readline()
    if "TITLE" in header:
        header = filedesc.readline()  # skip the comment field
    if header == "":
        raise EOFError("End of file or empty header in PDB file")

    a = float(header[6:15])
    b = float(header[15:24])
    c = float(header[24:33])
    alpha = float(header[33:40])
    beta = float(header[40:47])
    gamma = float(header[47:54])
    alpha *= np.pi / 180.0
    beta *= np.pi / 180.0
    gamma *= np.pi / 180.0
    h = mt.abc2h(a, b, c, alpha, beta, gamma)
    cell = Cell(h)

    natoms = 0
    body = filedesc.readline()
    qatoms = []
    names = []
    masses = []
    while (body.strip() != "" and body.strip() != "END"):
        natoms += 1
        name = body[12:16].strip()
        names.append(name)
        masses.append(Elements.mass(name))
        x = float(body[31:39])
        y = float(body[39:47])
        z = float(body[47:55])
        qatoms.append(x)
        qatoms.append(y)
        qatoms.append(z)

        body = filedesc.readline()

    atoms = Atoms(natoms)
    atoms.q = np.asarray(qatoms)
    atoms.names = np.asarray(names, dtype='|S4')
    atoms.m = np.asarray(masses)

    return atoms, cell
Exemplo n.º 3
0
def read_pdb(filedesc):
   """Takes a pdb-style file and creates an Atoms and Cell object.

   Args:
      filedesc: An open readable file object from a pdb formatted file.

   Returns:
      An Atoms object with the appropriate atom labels, masses and positions,
      and a Cell object with the appropriate cell dimensions and an estimate
      of a reasonable cell mass.
   """

   header = filedesc.readline()
   if "TITLE" in header: header = filedesc.readline()   # skip the comment field
   if header == "":
      raise EOFError("End of file or empty header in PDB file")

   a = float(header[6:15])
   b = float(header[15:24])
   c = float(header[24:33])
   alpha = float(header[33:40])
   beta = float(header[40:47])
   gamma = float(header[47:54])
   alpha *= np.pi/180.0
   beta *= np.pi/180.0
   gamma *= np.pi/180.0
   h = mt.abc2h(a, b, c, alpha, beta, gamma)
   cell = Cell(h)

   natoms = 0
   body = filedesc.readline()
   qatoms = []
   names = []
   masses = []
   while (body.strip() != "" and body.strip() != "END"):
      natoms += 1
      name = body[12:16].strip()
      names.append(name)
      masses.append(Elements.mass(name))
      x = float(body[31:39])
      y = float(body[39:47])
      z = float(body[47:55])
      qatoms.append(x)
      qatoms.append(y)
      qatoms.append(z)

      body = filedesc.readline()

   atoms = Atoms(natoms)
   atoms.q = np.asarray(qatoms)
   atoms.names = np.asarray(names,dtype='|S4')
   atoms.m = np.asarray(masses)

   return atoms, cell
Exemplo n.º 4
0
def read_xyz(filedesc):
    """Takes a xyz-style file and creates an Atoms object.

   Args:
      filedesc: An open readable file object from a xyz formatted file.

   Returns:
      An Atoms object with the appropriate atom labels, masses and positions.
   """

    natoms = filedesc.readline()
    if natoms == "":
        raise EOFError("The file descriptor hit EOF.")
    natoms = int(natoms)
    comment = filedesc.readline()

    qatoms = []
    names = []
    masses = []
    iat = 0
    while (iat < natoms):
        body = filedesc.readline()
        if body.strip() == "":
            break
        body = body.split()
        name = body[0]
        names.append(name)
        masses.append(Elements.mass(name))
        x = float(body[1])
        y = float(body[2])
        z = float(body[3])
        qatoms.append(x)
        qatoms.append(y)
        qatoms.append(z)
        iat += 1

    if natoms != len(names):
        raise ValueError(
            "The number of atom records does not match the header of the xyz file."
        )

    atoms = Atoms(natoms)
    #   for i in range(natoms):
    #      nat = atoms[i]
    #      nat.q = qatoms[i]
    #      nat.name = names[i]
    #      nat.m = Elements.mass(names[i])
    atoms.q = np.asarray(qatoms)
    atoms.names = np.asarray(names, dtype='|S4')
    atoms.m = np.asarray(masses)

    return atoms
Exemplo n.º 5
0
def read_xyz(filedesc):
    """Takes a xyz-style file and creates an Atoms object.

   Args:
      filedesc: An open readable file object from a xyz formatted file.

   Returns:
      An Atoms object with the appropriate atom labels, masses and positions.
   """

    natoms = filedesc.readline()
    if natoms == "":
        raise EOFError("The file descriptor hit EOF.")
    natoms = int(natoms)
    comment = filedesc.readline()

    qatoms = []
    names = []
    masses = []
    iat = 0
    while iat < natoms:
        body = filedesc.readline()
        if body.strip() == "":
            break
        body = body.split()
        name = body[0]
        names.append(name)
        masses.append(Elements.mass(name))
        x = float(body[1])
        y = float(body[2])
        z = float(body[3])
        qatoms.append(x)
        qatoms.append(y)
        qatoms.append(z)
        iat += 1

    if natoms != len(names):
        raise ValueError("The number of atom records does not match the header of the xyz file.")

    atoms = Atoms(natoms)
    #   for i in range(natoms):
    #      nat = atoms[i]
    #      nat.q = qatoms[i]
    #      nat.name = names[i]
    #      nat.m = Elements.mass(names[i])
    atoms.q = np.asarray(qatoms)
    atoms.names = np.asarray(names, dtype="|S4")
    atoms.m = np.asarray(masses)

    return atoms