Example #1
0
    def _parse(self, silent=False):

        poscarfile = self._file
        # Read comment line
        commentline = poscarfile.readline()

        # Read three unit cell lines
        self.scale_factor = float(poscarfile.readline())  # lattice constant
        vec1line = self.strip_comments(poscarfile.readline())
        vec2line = self.strip_comments(poscarfile.readline())
        vec3line = self.strip_comments(poscarfile.readline())
        self.basis = np.zeros((3, 3))
        self.basis[0] = map(float, vec1line.split())
        self.basis[1] = map(float, vec2line.split())
        self.basis[2] = map(float, vec3line.split())
        self.basis *= self.scale_factor

        # Read the atom counts line
        # and check for optional line containing atom types (VASP >= 5.1)
        self.atomtypes = []
        sixthline = self.strip_comments(poscarfile.readline())
        try:
            dummy = int(sixthline.split()[0])
            atomcountline = sixthline
        except:
            self.atomtypes = np.array(
                [get_atomic_number_from_symbol(i) for i in sixthline.split()])
            atomcountline = self.strip_comments(poscarfile.readline())
        self.atomcounts = map(int, atomcountline.split())
        self.natoms = sum(self.atomcounts)
        if len(self.atomtypes) == 0:
            if not silent:
                print "Note: Atom types are not specified in '%s'" % self.filename
            # assign negastive numbers, so we can still differentiate between the different types
            # (this will probably cause som nasty bugs at some time...)
            self.atomtypes = np.arange(-1, -len(self.atomcounts) - 1, -1)

        # Check for optional line specifying selective dynamics
        self.selective_dynamics = False
        line = self.strip_comments(poscarfile.readline())
        if line[0].upper() == 'S':
            self.selective_dynamics = True
            line = self.strip_comments(poscarfile.readline())

        # Read coordinate type line (direct or cartesian)
        self.direct_coords = False
        if line[0].upper() == 'D':
            self.direct_coords = True

        # Atomic positions
        self.positions = np.zeros((self.natoms, 3))
        for j in range(self.natoms):
            line = self.strip_comments(poscarfile.readline())
            self.positions[j] = [float(x) for x in (line.split()[0:3])]
            if len(self.positions[j]) != 3:
                raise IOError(
                    'POSCAR file %s ended before all coordinates were read in'
                    % self.filename)

        poscarfile.close()
Example #2
0
    def _parse(self, silent=False):

        poscarfile = self._file
        # Read comment line
        commentline = poscarfile.readline()

        # Read three unit cell lines
        self.scale_factor = float(poscarfile.readline()) # lattice constant
        vec1line = self.strip_comments(poscarfile.readline())
        vec2line = self.strip_comments(poscarfile.readline())
        vec3line = self.strip_comments(poscarfile.readline())
        self.basis = np.zeros((3,3))
        self.basis[0] = map(float,vec1line.split())
        self.basis[1] = map(float,vec2line.split())
        self.basis[2] = map(float,vec3line.split())
        self.basis *= self.scale_factor
        
        # Read the atom counts line
        # and check for optional line containing atom types (VASP >= 5.1)
        self.atomtypes = []
        sixthline = self.strip_comments(poscarfile.readline())
        try:
            dummy = int(sixthline.split()[0])
            atomcountline = sixthline
        except:
            self.atomtypes = np.array([get_atomic_number_from_symbol(i) for i in sixthline.split()])
            atomcountline = self.strip_comments(poscarfile.readline())
        self.atomcounts = map(int, atomcountline.split())
        self.natoms = sum(self.atomcounts)
        if len(self.atomtypes) == 0:
            if not silent:
                print "Note: Atom types are not specified in '%s'" % self.filename
            # assign negastive numbers, so we can still differentiate between the different types
            # (this will probably cause som nasty bugs at some time...)
            self.atomtypes = np.arange(-1,-len(self.atomcounts)-1,-1) 

        # Check for optional line specifying selective dynamics
        self.selective_dynamics = False
        line = self.strip_comments(poscarfile.readline())
        if line[0].upper() == 'S':
            self.selective_dynamics = True
            line = self.strip_comments(poscarfile.readline())
        
        # Read coordinate type line (direct or cartesian)
        self.direct_coords = False
        if line[0].upper() == 'D':
            self.direct_coords = True

        # Atomic positions
        self.positions = np.zeros((self.natoms,3))
        for j in range(self.natoms):
            line = self.strip_comments(poscarfile.readline())
            self.positions[j] = [float(x) for x in (line.split()[0:3])]
            if len(self.positions[j]) != 3:
                raise IOError('POSCAR file %s ended before all coordinates were read in' % self.filename)
        
        poscarfile.close()
Example #3
0
    def ionic_steps(self):

        atoms = []
        for rc in self.doc.xpath("/modeling/atominfo/array[@name='atoms']/set/rc"):
            atoms.append(get_atomic_number_from_symbol(rc[0].text.strip()))
        #atoms = [rc[0].text.strip() for rc in atoms]
        atoms = np.array(atoms, dtype=int)

        results = self.doc.xpath( "/modeling/calculation")
        if results:
            return [IonicStep(r, atoms) for r in results]
        else:
            raise LookupError('Value not found')
Example #4
0
    def ionic_steps(self):

        atoms = []
        for rc in self.doc.xpath(
                "/modeling/atominfo/array[@name='atoms']/set/rc"):
            atoms.append(get_atomic_number_from_symbol(rc[0].text.strip()))
        #atoms = [rc[0].text.strip() for rc in atoms]
        atoms = np.array(atoms, dtype=int)

        results = self.doc.xpath("/modeling/calculation")
        if results:
            return [IonicStep(r, atoms) for r in results]
        else:
            raise LookupError('Value not found')
Example #5
0
    def get_named_structure(self, name):
        """ Returns a named structure as a oppvasp.Structure object. """

        struc = self.doc.xpath("/modeling/structure[@name='%s']" % (name))[0]

        basis = struc.xpath("crystal/varray[@name='basis']/v")
        basis = [[float(x) for x in p.text.split()] for p in basis]

        rec_basis = struc.xpath("crystal/varray[@name='rec_basis']/v")
        rec_basis = [[float(x) for x in p.text.split()] for p in rec_basis]

        vol = struc.xpath("crystal/i[@name='volume']")[0]
        vol = float(vol.text)

        pos = struc.xpath("varray[@name='positions']/v")
        pos = [[float(x) for x in p.text.split()] for p in pos]

        vel = struc.xpath("varray[@name='velocities']/v")
        if vel:
            vel = [[float(x) for x in p.text.split()] for p in vel]
        else:
            # Found no velocities
            vel = None

        forces = struc.xpath("varray[@name='forces']/v")
        if forces:
            forces = [[float(x) for x in p.text.split()] for p in forces]
        else:
            # Found no forces
            forces = None

        atoms = []
        for rc in self.doc.xpath(
                "/modeling/atominfo/array[@name='atoms']/set/rc"):
            atoms.append(get_atomic_number_from_symbol(rc[0].text.strip()))
        #atoms = [rc[0].text.strip() for rc in atoms]
        atoms = np.array(atoms, dtype=int)

        return Structure(cell=basis,
                         atomtypes=atoms,
                         positions=pos,
                         velocities=vel,
                         forces=forces,
                         coords='direct')
Example #6
0
    def ionic_steps(self, step = 0):

        try:
            parser
        except:
            parser = etree.XMLParser()
            context = etree.iterparse(self.filename, tag='atominfo')
            for event, elem in context:
                atominfo = []
                for rc in elem.xpath("array[@name='atoms']/set/rc"):
                    atominfo.append(get_atomic_number_from_symbol(rc[0].text.strip()))
                break  # avoid scanning the whole file!

            context = etree.iterparse(self.filename, tag='calculation')
        
        for event, elem in context:
            step = IonicStep(elem, atominfo)
            elem.clear()
            while elem.getprevious() is not None:
                del elem.getparent()[0]
            yield step
Example #7
0
    def ionic_steps(self, step=0):

        try:
            parser
        except:
            parser = etree.XMLParser()
            context = etree.iterparse(self.filename, tag='atominfo')
            for event, elem in context:
                atominfo = []
                for rc in elem.xpath("array[@name='atoms']/set/rc"):
                    atominfo.append(
                        get_atomic_number_from_symbol(rc[0].text.strip()))
                break  # avoid scanning the whole file!

            context = etree.iterparse(self.filename, tag='calculation')

        for event, elem in context:
            step = IonicStep(elem, atominfo)
            elem.clear()
            while elem.getprevious() is not None:
                del elem.getparent()[0]
            yield step
Example #8
0
    def set_atomtypes(self, atoms):
        """
        If the array is set using a list of atomic symbols,
        it will be converted into an array of atomic numbers, so 
        the following two lines of code are equivalent:

        >>> set_atomtypes([14,16,16])
        >>> set_atomtypes(['Si','O','O'])
        """
        if type(atoms).__name__ == 'ndarray':
            self._atomtypes = np.array(atoms, dtype = 'int')
        elif type(atoms).__name__ == 'list':
            self._atomtypes = np.zeros(len(atoms), dtype = 'int')
            for i, atom in enumerate(atoms):
                if type(atom).__name__ == 'str':
                    self._atomtypes[i] = get_atomic_number_from_symbol(atom)
                else:
                    self._atomtypes[i] = atom
        elif type(atoms).__name__ == 'NoneType':
            self._atomtypes = None
        else:
            print "Error: atoms must be a list or array"
Example #9
0
    def set_atomtypes(self, atoms):
        """
        If the array is set using a list of atomic symbols,
        it will be converted into an array of atomic numbers, so 
        the following two lines of code are equivalent:

        >>> set_atomtypes([14,16,16])
        >>> set_atomtypes(['Si','O','O'])
        """
        if type(atoms).__name__ == 'ndarray':
            self._atomtypes = np.array(atoms, dtype='int')
        elif type(atoms).__name__ == 'list':
            self._atomtypes = np.zeros(len(atoms), dtype='int')
            for i, atom in enumerate(atoms):
                if type(atom).__name__ == 'str':
                    self._atomtypes[i] = get_atomic_number_from_symbol(atom)
                else:
                    self._atomtypes[i] = atom
        elif type(atoms).__name__ == 'NoneType':
            self._atomtypes = None
        else:
            print "Error: atoms must be a list or array"
Example #10
0
    def get_named_structure(self, name):
        """ Returns a named structure as a oppvasp.Structure object. """

        struc = self.doc.xpath("/modeling/structure[@name='%s']" % (name))[0]

        basis = struc.xpath("crystal/varray[@name='basis']/v")
        basis = [[float(x) for x in p.text.split()] for p in basis]
        
        rec_basis = struc.xpath("crystal/varray[@name='rec_basis']/v")
        rec_basis = [[float(x) for x in p.text.split()] for p in rec_basis]

        vol = struc.xpath("crystal/i[@name='volume']")[0]
        vol = float(vol.text)

        pos = struc.xpath("varray[@name='positions']/v")
        pos = [[float(x) for x in p.text.split()] for p in pos]

        vel = struc.xpath("varray[@name='velocities']/v")
        if vel:
            vel = [[float(x) for x in p.text.split()] for p in vel]
        else:
            # Found no velocities
            vel = None

        forces = struc.xpath("varray[@name='forces']/v")
        if forces:
            forces = [[float(x) for x in p.text.split()] for p in forces]
        else:
            # Found no forces
            forces = None

        atoms = []
        for rc in self.doc.xpath("/modeling/atominfo/array[@name='atoms']/set/rc"):
            atoms.append(get_atomic_number_from_symbol(rc[0].text.strip()))
        #atoms = [rc[0].text.strip() for rc in atoms]
        atoms = np.array(atoms, dtype=int)

        return Structure( cell = basis, atomtypes = atoms, positions = pos, velocities = vel, forces = forces, coords = 'direct' )
Example #11
0
 def _get_atoms(self, elem):
     atoms = []
     for rc in elem.xpath("array[@name='atoms']/set/rc"):
         atoms.append(get_atomic_number_from_symbol(rc[0].text))
     return np.array(atoms, dtype=int)
Example #12
0
 def _get_atoms(self, elem):
     atoms = []
     for rc in elem.xpath("array[@name='atoms']/set/rc"):
         atoms.append(get_atomic_number_from_symbol(rc[0].text))
     return np.array(atoms, dtype=int)