Exemplo n.º 1
0
 def convert_numeric(self):
     for name,attr in self._attributes.iteritems():
         self[name] = string2val(attr)
     #end for
     if 'text' in self:
         self.value = string2val(self.text)
         del self.text
     #end if
     texts = []
     for name,elem in self._elements.iteritems():
         if isinstance(elem,XMLelement):
             if 'text' in elem and len(elem._attributes)==0 and len(elem._elements)==0:
                 self[name] = string2val(elem.text)
                 texts.append(name)
             else:
                 elem.convert_numeric()
             #end if
         #end if
     #end if
     for name in texts:
         self._elements[name] = self[name]
Exemplo n.º 2
0
def test_string2val():
    import numpy as np
    from testing import value_eq
    from superstring import string2val

    v = string2val('False')
    assert(isinstance(v,bool))
    assert(v==False)

    v = string2val('True')
    assert(isinstance(v,bool))
    assert(v==True)

    v = string2val('43')
    assert(isinstance(v,int))
    assert(v==43)

    v = string2val('3.14')
    assert(isinstance(v,float))
    assert(np.abs(v-3.14)<1e-6)

    v = string2val('1 2 3 4',delim=' ')
    assert(isinstance(v,np.ndarray))
    assert(value_eq(v,np.array([1,2,3,4],dtype=int)))

    v = string2val('1, 2, 3, 4',delim=',')
    assert(isinstance(v,np.ndarray))
    assert(value_eq(v,np.array([1,2,3,4],dtype=int)))

    v = string2val('1. 2 3 4',delim=' ')
    assert(isinstance(v,np.ndarray))
    assert(value_eq(v,np.array([1,2,3,4],dtype=float)))

    v = string2val('1., 2, 3, 4',delim=',')
    assert(isinstance(v,np.ndarray))
    assert(value_eq(v,np.array([1,2,3,4],dtype=float)))
Exemplo n.º 3
0
    def readfile(self, filepath):
        text = open(filepath, 'r').read()
        if '<UPF' not in text:
            upf_format = 'old'
            lines = text.split('\n')
            xml = '<upf>\n'
            for l in lines:
                if l.find('/>') != -1:
                    ln = l.replace('/>', '>').replace('<', '</')
                else:
                    ln = l
                #end if
                xml += ln + '\n'
            #end for
            xml += '</upf>\n'
            tmppath = filepath + '_tmp'
            open(tmppath, 'w').write(xml)
            x = readxml(tmppath, contract_names=True, strip_prefix='pp_')
            os.system('rm ' + tmppath)
            x.convert_numeric()
            x.condense()
            x.remove_hidden()
            pp = x.upf
        else:
            upf_format = 'new'
            #x = readxml(filepath,contract_names=True,strip_prefix='pp_')
            #x.convert_numeric()
            #x.condense()
            #x.remove_hidden()
            #pp = x.upf
            pp = obj()
            pp_contents = open(filepath, 'r').read()
        #end if
        if upf_format == 'old':
            lines = pp.header.split('\n')
            h = obj()
            i = 0
            h.version = string2val(lines[i].split()[0])
            i += 1
            h.element = string2val(lines[i].split()[0])
            i += 1
            h.type = string2val(lines[i].split()[0])
            i += 1
            ncc = string2val(lines[i].split()[0])
            i += 1
            h.nonlinear_cc = dict(T=True, F=False)[ncc]
            h.functional = lines[i].split()[0:4]
            i += 1
            h.Z = string2val(lines[i].split()[0])
            i += 1
            h.total_energy = string2val(lines[i].split()[0])
            i += 1
            h.wfc_cutoff, h.rho_cutoff = array(lines[i].split()[0:2],
                                               dtype=float)
            i += 1
            h.lmax = string2val(lines[i].split()[0])
            i += 1
            h.npts = string2val(lines[i].split()[0])
            i += 1
            h.nwfc = string2val(lines[i].split()[0])
            i += 1
            pp.header = h
            if 'beta' in pp. nonlocal:
                beta = pp. nonlocal .beta
                if isinstance(beta, str):
                    beta = [beta]
                #end if
                b = obj()
                for i in range(len(beta)):
                    sections = beta[i].split('\n', 2)
                    p = string2val(sections[0].split()[0])
                    v = string2val(sections[2])
                    b[p] = v
                #end for
                pp. nonlocal .beta = b
            #end if
            dij = pp. nonlocal .dij
            d = obj()
            lines = dij.split('\n')[1:]
            for l in lines:
                t = l.split()
                d[int(t[0]), int(t[1])] = string2val(t[2])
            #end for
            pp. nonlocal .dij = d
            pswfc = pp.pswfc
            tokens = pswfc.split()
            nwfc = pp.header.nwfc
            npts = pp.header.npts
            wf = []
            for n in range(nwfc):
                i = n * (4 + npts)
                label = tokens[i]
                l = int(tokens[i + 1])
                ws = []
                for v in tokens[i + 4:i + 4 + npts]:
                    ws.append(float(v))
                #end for
                orb = obj()
                orb.label = label
                orb.l = l
                orb.wfc = array(ws)
                wf.append(orb)
            #end for
            pp.pswfc = wf

            #fill in standard fields
            self.pp = pp
            self.r = pp.mesh.r[1:]
            self.local = convert(pp.local, 'Ry', self.energy_units)[1:]
            nl = obj()
            vnl = zeros(self.local.shape)
            if 'beta' in pp. nonlocal:
                beta = pp. nonlocal .beta
                for t, d in pp. nonlocal .dij.iteritems():
                    bi = beta[t[0]]
                    bj = beta[t[1]]
                    if not isinstance(bi, str) and not isinstance(bj, str):
                        bb = d * bi * bj
                    else:  # the file is being misread, fix later
                        bb = 0 * pp.mesh.r
                    #end if
                    naftcut = len(pp.mesh.r) - len(bb)
                    if naftcut > 0:
                        bb = append(bb, zeros((naftcut, )))
                    #end if
                    vnl += bb[1:] / self.r**2
                #end for
            #end if
            vnl = convert(vnl, 'Ry', self.energy_units)
            nl[0] = vnl
            self. nonlocal = nl
            h = pp.header
            p = obj()
            p[0] = self.local
            p[1] = self.local + self. nonlocal [0]
            self.potentials = p
            self.element = h.element
            self.type = h.type
            self.Z = h.Z
Exemplo n.º 4
0
    def readfile(self,filepath):
        text = open(filepath,'r').read()
        if '<UPF' not in text:
            upf_format = 'old'
            lines = text.split('\n')
            xml = '<upf>\n'
            for l in lines:
                if l.find('/>')!=-1:
                    ln = l.replace('/>','>').replace('<','</')
                else:
                    ln = l
                #end if
                xml+=ln+'\n'
            #end for
            xml += '</upf>\n'
            tmppath = filepath+'_tmp'
            open(tmppath,'w').write(xml)
            x = readxml(tmppath,contract_names=True,strip_prefix='pp_')
            os.system('rm '+tmppath)
            x.convert_numeric()
            x.condense()
            x.remove_hidden()
            pp = x.upf
        else:
            upf_format = 'new'
            #x = readxml(filepath,contract_names=True,strip_prefix='pp_')
            #x.convert_numeric()
            #x.condense()
            #x.remove_hidden()
            #pp = x.upf
            pp = obj()
        #end if
        if upf_format=='old':
            lines = pp.header.split('\n')
            h = obj()
            i=0
            h.version = string2val(lines[i].split()[0]); i+=1
            h.element = string2val(lines[i].split()[0]); i+=1
            h.type = string2val(lines[i].split()[0]); i+=1
            ncc = string2val(lines[i].split()[0]); i+=1
            h.nonlinear_cc = dict(T=True,F=False)[ncc]
            h.functional = lines[i].split()[0:4]; i+=1
            h.Z = string2val(lines[i].split()[0]); i+=1
            h.total_energy = string2val(lines[i].split()[0]); i+=1
            h.wfc_cutoff,h.rho_cutoff = array(lines[i].split()[0:2],dtype=float); i+=1
            h.lmax = string2val(lines[i].split()[0]); i+=1
            h.npts = string2val(lines[i].split()[0]); i+=1
            h.nwfc = string2val(lines[i].split()[0]); i+=1
            pp.header = h
            if 'beta' in pp.nonlocal:
                beta = pp.nonlocal.beta
                if isinstance(beta,str):
                    beta = [beta]
                #end if
                b = obj()
                for i in range(len(beta)):
                    sections = beta[i].split('\n',2)
                    p = string2val(sections[0].split()[0])
                    v = string2val(sections[2])
                    b[p]=v
                #end for
                pp.nonlocal.beta = b
            #end if
            dij = pp.nonlocal.dij
            d = obj()
            lines = dij.split('\n')[1:]
            for l in lines:
                t = l.split()
                d[int(t[0]),int(t[1])] = string2val(t[2])
            #end for
            pp.nonlocal.dij = d
            pswfc = pp.pswfc
            tokens = pswfc.split()
            nwfc= pp.header.nwfc
            npts= pp.header.npts
            wf = []
            for n in range(nwfc):
                i = n*(4+npts)
                label = tokens[i]
                l = int(tokens[i+1])
                ws = []
                for v in tokens[i+4:i+4+npts]:
                    ws.append(float(v))
                #end for
                orb = obj()
                orb.label = label
                orb.l = l
                orb.wfc = array(ws)
                wf.append(orb)
            #end for
            pp.pswfc = wf

            #fill in standard fields
            self.pp = pp
            self.r = pp.mesh.r[1:]
            self.local = convert(pp.local,'Ry',self.energy_units)[1:]
            nl = obj()
            vnl = zeros(self.local.shape)
            if 'beta' in pp.nonlocal:
                beta = pp.nonlocal.beta
                for t,d in pp.nonlocal.dij.iteritems():
                    bi = beta[t[0]]
                    bj = beta[t[1]]
                    if not isinstance(bi,str) and not isinstance(bj,str):
                        bb = d*bi*bj
                    else: # the file is being misread, fix later
                        bb  = 0*pp.mesh.r
                    #end if
                    naftcut = len(pp.mesh.r)-len(bb)
                    if naftcut>0:
                        bb=append(bb,zeros((naftcut,)))
                    #end if
                    vnl += bb[1:]/self.r**2
                #end for
            #end if
            vnl = convert(vnl,'Ry',self.energy_units)
            nl[0] = vnl
            self.nonlocal = nl
            h = pp.header
            p = obj()
            p[0] = self.local
            p[1] = self.local+self.nonlocal[0]
            self.potentials = p
            self.element = h.element
            self.type = h.type
            self.Z  = h.Z