예제 #1
0
파일: gen.py 프로젝트: carlos-salgado/simpy
    def tofile(self, fobj, relcoords=False):
        """Writes a GEN file.

        Args:
            fobj: File name or file object where geometry should be written.
            relcoords: If true, geometry will be outputted in relative
                coordinates (as multiples of the lattice vectors)
        """
        fp = openfile(fobj, "w")
        line = [ "{0:d}".format(self.geometry.natom), ]
        geo = self.geometry
        if geo.periodic:
            if relcoords:
                line.append("F")
                coords = geo.relcoords
            else:
                line.append("S")
                coords = geo.coords
        else:
            line.append("C")
            coords = geo.coords
        fp.write(" ".join(line) + "\n")
        fp.write(" ".join(geo.specienames) + "\n")
        for ii in range(geo.natom):
            fp.write("{0:6d} {1:3d} {2:18.10E} {3:18.10E} {4:18.10E}\n".format(
                ii + 1, geo.indexes[ii] + 1, *coords[ii]))
        if geo.periodic:
            fp.write("{0:18.10E} {1:18.10E} {2:18.10E}\n".format(*geo.origin))
            for vec in geo.latvecs:
                fp.write("{0:18.10E} {1:18.10E} {2:18.10E}\n".format(*vec))
        fp.close()
예제 #2
0
    def fromfile(cls, fobj):
        """Creates an XYZ instance from a file.

        Args:
            fobj: filename or file like object containing geometry in
                XYZ-format.

        """
        fp = openfile(fobj, "r")
        lines = fp.readlines()
        fp.close()
        words = lines[0].split()
        natom = int(words[0])
        comment = lines[1].strip()
        specienames = []
        speciedict = {}
        indexes = np.empty((natom, ), dtype=int)
        coords = np.empty((natom, 3), dtype=float)
        for ii, line in enumerate(lines[2:2 + natom]):
            words = line.split()
            species = words[0]
            index = speciedict.get(species, -1)
            if index == -1:
                specienames.append(species)
                speciedict[species] = len(specienames) - 1
                indexes[ii] = len(specienames) - 1
            else:
                indexes[ii] = index
            coords[ii] = np.array(words[1:4], dtype=float)
        geometry = Geometry(specienames, indexes, coords)
        return cls(geometry, comment)
예제 #3
0
    def fromfile(cls, fobj):
        """Returns a band.out representation created from a file object.

        Args:
            fobj: File like object or string with file name.
        """
        fp = openfile(fobj)
        txt = fp.read()
        fp.close()
        ispins = set()
        kweights = []
        eigvalarrays = []
        match = PAT_BLOCK.search(txt)
        while match:
            ispins.add(int(match.group("ispin")))
            kweight = match.group("kweight")
            if kweight:
                kweights.append(float(kweight))
            else:
                kweights.append(1.0)
            tmp = np.array(match.group("vals").split(), dtype=float)
            eigvalarrays.append(tmp.reshape((-1, 2)))
            match = PAT_BLOCK.search(txt, match.end())
        nspin = len(ispins)
        nkpt = len(eigvalarrays) / nspin
        eigvalspin = np.array(eigvalarrays).reshape((nspin, nkpt, -1, 2))
        kweights = np.array(kweights).reshape((nspin, nkpt))
        return cls(kweights, eigvalspin)
예제 #4
0
    def fromfile(cls, fobj):
        """Returns a band.out representation created from a file object.

        Args:
            fobj: File like object or string with file name.
        """
        fp = openfile(fobj)
        txt = fp.read()
        fp.close()
        ispins = set()
        kweights = []
        eigvalarrays = []
        match = PAT_BLOCK.search(txt)
        while match:
            ispins.add(int(match.group("ispin")))
            kweight = match.group("kweight")
            if kweight:
                kweights.append(float(kweight))
            else:
                kweights.append(1.0)
            tmp = np.array(match.group("vals").split(), dtype=float)
            eigvalarrays.append(tmp.reshape((-1, 2)))
            match = PAT_BLOCK.search(txt, match.end())
        nspin = len(ispins)
        nkpt = len(eigvalarrays) / nspin
        eigvalspin = np.array(eigvalarrays).reshape((nspin, nkpt, -1, 2))
        kweights = np.array(kweights).reshape((nspin, nkpt))
        return cls(kweights, eigvalspin)
예제 #5
0
파일: gen.py 프로젝트: tarbaig/simpy
    def tofile(self, fobj, relcoords=False):
        """Writes a GEN file.

        Args:
            fobj: File name or file object where geometry should be written.
            relcoords: If true, geometry will be outputted in relative
                coordinates (as multiples of the lattice vectors)
        """
        fp = openfile(fobj, "w")
        line = [
            "{0:d}".format(self.geometry.natom),
        ]
        geo = self.geometry
        if geo.periodic:
            if relcoords:
                line.append("F")
                coords = geo.relcoords
            else:
                line.append("S")
                coords = geo.coords
        else:
            line.append("C")
            coords = geo.coords
        fp.write(" ".join(line) + "\n")
        fp.write(" ".join(geo.specienames) + "\n")
        for ii in range(geo.natom):
            fp.write("{0:6d} {1:3d} {2:18.10E} {3:18.10E} {4:18.10E}\n".format(
                ii + 1, geo.indexes[ii] + 1, *coords[ii]))
        if geo.periodic:
            fp.write("{0:18.10E} {1:18.10E} {2:18.10E}\n".format(*geo.origin))
            for vec in geo.latvecs:
                fp.write("{0:18.10E} {1:18.10E} {2:18.10E}\n".format(*vec))
        fp.close()
예제 #6
0
파일: xyz.py 프로젝트: carlos-salgado/simpy
    def fromfile(cls, fobj):
        """Creates an XYZ instance from a file.

        Args:
            fobj: filename or file like object containing geometry in
                XYZ-format.

        """
        fp = openfile(fobj, "r")
        lines = fp.readlines()
        fp.close()
        words = lines[0].split()
        natom = int(words[0])
        comment = lines[1].strip()
        specienames = []
        speciedict = {}
        indexes = np.empty((natom, ), dtype=int)
        coords = np.empty((natom, 3), dtype=float)
        for ii, line in enumerate(lines[2:2+natom]):
            words = line.split()
            species = words[0]
            index = speciedict.get(species, -1)
            if index == -1:
                specienames.append(species)
                speciedict[species] = len(specienames) - 1
                indexes[ii] = len(specienames) - 1
            else:
                indexes[ii] = index
            coords[ii] = np.array(words[1:4], dtype=float)
        geometry = Geometry(specienames, indexes, coords)
        return cls(geometry, comment)
예제 #7
0
    def fromfile(cls, fobj):
        '''Reads crystallographic information from a CIF file.

        Args:
            fobj: filename or file like object containing geometry in
                CIF-format.

        '''
        fp = openfile(fobj, "r")
        lines = fp.readlines()
        fp.close()
        celllengths = np.empty(3, dtype=float)
        cellangles = np.empty(3, dtype=float)
        for jj in range(3):
            celllengths[jj] = lines[jj + 1].split()[1]
            cellangles[jj] = lines[jj + 4].split()[1]
        natom = len(lines) - 13
        specienames = []
        speciedict = {}
        indexes = np.empty((natom, ), dtype=int)
        coords = np.empty((natom, 3), dtype=float)
        for ii, line in enumerate(lines[13:13+natom]):
            words = line.split()
            species = words[0]
            index = speciedict.get(species, -1)
            if index == -1:
                specienames.append(species)
                speciedict[species] = len(specienames) - 1
                indexes[ii] = len(specienames) - 1
            else:
                indexes[ii] = index
            coords[ii] = np.array(words[1:4], dtype=float)
        latvecs = get_latvecs_fromcif(celllengths, cellangles)
        geometry = Geometry(specienames, indexes, coords, latvecs=latvecs, relcoords=True)
        return cls(geometry)
예제 #8
0
    def tofile(self, fobj):
        """Writes an XYZ file.

        Args:
            fobj: File name or file object where geometry should be written.
        """
        fp = openfile(fobj, "w")
        geo = self.geometry
        fp.write("{0:d}\n".format(geo.natom))
        fp.write(self.comment + "\n")
        for ii in range(geo.natom):
            fp.write("{0:3s} {1:18.10E} {2:18.10E} {3:18.10E}\n".format(
                geo.specienames[geo.indexes[ii]], *geo.coords[ii]))
        fp.close()
예제 #9
0
파일: xyz.py 프로젝트: carlos-salgado/simpy
    def tofile(self, fobj):
        """Writes an XYZ file.

        Args:
            fobj: File name or file object where geometry should be written.
        """
        fp = openfile(fobj, "w")
        geo = self.geometry
        fp.write("{0:d}\n".format(geo.natom))
        fp.write(self.comment + "\n")
        for ii in range(geo.natom):
            fp.write("{0:3s} {1:18.10E} {2:18.10E} {3:18.10E}\n".format(
                geo.specienames[geo.indexes[ii]], *geo.coords[ii]))
        fp.close()
예제 #10
0
파일: cif.py 프로젝트: itamblyn/scripts
 def tofile(self, fobj):
     geo = self.geometry
     fp = openfile(fobj, "w")
     fp.write("data_global\n")
     for name, value in zip(["a", "b", "c"], self.celllengths):
         fp.write("_cell_length_{0:s} {1:.10f}\n".format(name, value))
     # cell angles are needed in degrees
     for name, value in zip(["alpha", "beta", "gamma"],
                            self.cellangles * 180.0 / np.pi):
         fp.write("_cell_angle_{0:s} {1:.10f}\n".format(name, value))
     fp.write("_symmetry_space_group_name_H-M 'P 1'\n")
     fp.write("loop_\n_atom_site_label\n_atom_site_fract_x\n"
              "_atom_site_fract_y\n_atom_site_fract_z\n")
     for ii in range(geo.natom):
         fp.write("{0:3s} {1:.10f} {2:.10f} {3:.10f}\n".format(
             geo.specienames[geo.indexes[ii]], *geo.relcoords[ii]))
     fp.close()
예제 #11
0
 def tofile(self, fobj):
     geo = self.geometry
     fp = openfile(fobj, "w")
     fp.write("data_global\n")
     for name, value in zip(["a", "b", "c"], self.celllengths):
         fp.write("_cell_length_{0:s} {1:.10f}\n".format(name, value))
     # cell angles are needed in degrees
     for name, value in zip(["alpha", "beta", "gamma"],
                            self.cellangles * 180.0 / np.pi):
         fp.write("_cell_angle_{0:s} {1:.10f}\n".format(name, value))
     fp.write("_symmetry_space_group_name_H-M 'P 1'\n")
     fp.write("loop_\n_atom_site_label\n_atom_site_fract_x\n"
              "_atom_site_fract_y\n_atom_site_fract_z\n")
     for ii in range(geo.natom):
         fp.write("{0:3s} {1:.10f} {2:.10f} {3:.10f}\n".format(
             geo.specienames[geo.indexes[ii]], *geo.relcoords[ii]))
     fp.close()
예제 #12
0
    def fromfile(cls, fobj):
        """Returns a band.out representation created from a file object.

        Args:
            fobj: File like object or string with file name.
        """
        fp = openfile(fobj)
        txt = fp.read()
        fp.close()
        ispins = set()
        kweights = []
        eigvalarrays = []
        match = _PAT_BLOCK.search(txt)
        while match:
            ispin = match.group("ispin")
            if ispin:
                ispins.add(int(ispin))
            else:
                ispins.add(1)
            kweight = match.group("kweight")
            if kweight:
                kweights.append(float(kweight))
            else:
                kweights.append(1.0)
            vals = match.group("vals")
            tmp = np.array(vals.split(), dtype=float)

            # Only keep last two data columns
            # (optional first column, if present, contains sequential numbering)
            nrows = vals.strip().count('\n') + 1
            ncols = len(tmp) // nrows
            tmp.shape = (nrows, ncols)
            tmp = tmp[:, ncols - 2:ncols]

            eigvalarrays.append(tmp)
            match = _PAT_BLOCK.search(txt, match.end())

        nspin = len(ispins)
        nkpt = len(eigvalarrays) // nspin
        eigvalspin = np.array(eigvalarrays)
        eigvalspin.shape = (nspin, nkpt, -1, 2)
        kweights = np.array(kweights)
        kweights.shape = (nspin, nkpt)
        return cls(kweights, eigvalspin)
예제 #13
0
파일: gen.py 프로젝트: tarbaig/simpy
    def fromfile(cls, fobj):
        """Creates a Gen instance from a file.

        Args:
            fobj: filename or file like object containing geometry in
                GEN-format.
        """
        fp = openfile(fobj, "r")
        lines = fp.readlines()
        fp.close()
        words = lines[0].split()
        natom = int(words[0])
        flag = words[1].lower()
        if flag == "s":
            periodic = True
            relative = False
        elif flag == "f":
            periodic = True
            relative = True
        else:
            periodic = False
            relative = False
        specienames = lines[1].split()
        indexes = np.empty((natom, ), dtype=int)
        coords = np.empty((natom, 3), dtype=float)
        for ii, line in enumerate(lines[2:2 + natom]):
            words = line.split()
            indexes[ii] = int(words[1]) - 1
            coords[ii] = np.array(words[2:5], dtype=float)
        if periodic:
            origin = np.array(lines[natom + 2].split(), dtype=float)
            latvecs = np.empty((3, 3), dtype=float)
            for jj in range(3):
                latvecs[jj] = np.array(lines[natom + 3 + jj].split(),
                                       dtype=float)
        else:
            origin = None
            latvecs = None
        geometry = Geometry(specienames, indexes, coords, latvecs, origin,
                            relative)
        return cls(geometry)
예제 #14
0
파일: gen.py 프로젝트: carlos-salgado/simpy
    def fromfile(cls, fobj):
        """Creates a Gen instance from a file.

        Args:
            fobj: filename or file like object containing geometry in
                GEN-format.
        """
        fp = openfile(fobj, "r")
        lines = fp.readlines()
        fp.close()
        words = lines[0].split()
        natom = int(words[0])
        flag = words[1].lower()
        if flag == "s":
            periodic = True
            relative = False
        elif flag == "f":
            periodic = True
            relative = True
        else:
            periodic = False
            relative = False
        specienames = lines[1].split()
        indexes = np.empty((natom, ), dtype=int)
        coords = np.empty((natom, 3), dtype=float)
        for ii, line in enumerate(lines[2:2+natom]):
            words = line.split()
            indexes[ii] = int(words[1]) - 1
            coords[ii] = np.array(words[2:5], dtype=float)
        if periodic:
            origin = np.array(lines[natom+2].split(), dtype=float)
            latvecs = np.empty((3, 3), dtype=float)
            for jj in range(3):
                latvecs[jj] = np.array(lines[natom+3+jj].split(), dtype=float)
        else:
            origin = None
            latvecs = None
        geometry = Geometry(specienames, indexes, coords, latvecs, origin,
                            relative)
        return cls(geometry)