Exemple #1
0
 def __init__(self,
              ibrav=1,
              a=1.,
              b=1.,
              c=1.,
              cBC=0.,
              cAC=0.,
              cAB=0.,
              qeConf=None,
              base=None):
     #        Lattice.__init__(self)
     self.formatString = '%# .8f %# .8f %# .8f'
     self.qeConf = qeConf
     self._type = 'celldm'
     self._primitiveLattice = Lattice()
     self._standardLattice = Lattice()
     self._base = None
     self._a0 = None
     if self.qeConf != None:
         self.setLatticeFromPWInput(self.qeConf)
     else:
         if ibrav > 0 and base != None:
             self.setLatticeFromQEVectors(ibrav, base)
         else:
             self.setLattice(ibrav, a, b, c, cBC, cAC, cAB, base)
Exemple #2
0
def doc2diffpy(doc):
    """ Convert doc into diffpy Structure object. """
    from numpy import asarray
    from diffpy.Structure.atom import Atom
    from diffpy.Structure.lattice import Lattice
    from diffpy.Structure.structure import Structure

    lattice = Lattice(a=doc['lattice_abc'][0][0],
                      b=doc['lattice_abc'][0][1],
                      c=doc['lattice_abc'][0][2],
                      alpha=doc['lattice_abc'][1][0],
                      beta=doc['lattice_abc'][1][1],
                      gamma=doc['lattice_abc'][1][2])
    atoms = []
    for ind, atom in enumerate(doc['atom_types']):
        # encode atype as utf-8 or you will waste hours of your life
        atoms.append(
            Atom(atype=atom.encode('utf-8'),
                 xyz=asarray(doc['positions_frac'][ind])))
    title = None
    for sources in doc['source']:
        if sources.endswith('.res') or sources.endswith('.castep'):
            title = sources.split('/')[-1].split('.')[0].encode('utf-8')

    return Structure(atoms, lattice, title=title)
Exemple #3
0
    def __init__(self, ibrav = 1,a = 1. ,b = 1.,c = 1.,
            cBC = 0.,cAC = 0. ,cAB = 0., base = None, lattice = None ):
        self.formatString = '%# .8f %# .8f %# .8f'
        self._qeInput = None
        #self._qeInput = None
        self._type = 'celldm'
        
        # diffpyStructure container class, used for lattice operations
        self.__primitiveLattice = Lattice()
        
        # Lattice vectors in bohr or angstrom:
        self._base = None

        # initialize the lattice if there is enough information 
        if ibrav > 0 and base != None:
            self.setLatticeFromQEVectors(ibrav, base)
        else:
            self.setLattice(ibrav ,a ,b , c, cBC ,cAC ,cAB, base)
        
        # copy constructor:
        if isinstance(ibrav, QELattice) or lattice != None:
            if lattice.ibrav > 0:
                self.setLattice( ibrav = lattice.ibrav, a = lattice.a, \
                                         b = lattice.b, c = lattice.c,
                                         cBC = lattice.cBC, cAC = lattice.cAC,\
                                         cAB = lattice.cAB)
            else:
                self.setLattice( ibrav = lattice.ibrav, a = lattice.a, \
                                                          base = lattice.base )            
            # copy input:
            from pwinput import PWInput        
            self._qeInput = PWInput()
            self._qeInput.readString( lattice._qeInput.toString() )
Exemple #4
0
    def __init__(self, ibrav = 1,a = 1. ,b = 1.,c = 1.,
                 cBC = 0.,cAC = 0. ,cAB = 0., fname = None, base = None ):
#        Lattice.__init__(self)
        self.filename = fname
        self._type = 'celldm'
        self.qeConf = None   # should be none if nothing to parse
        self._primitiveLattice = Lattice()
        self._standardLattice = Lattice()
        self._base = None
        self._a0 = None
        if self.filename != None:
            self.setLatticeFromPWSCF(self.filename)
        else:
            if ibrav > 0 and base != None:
                self.setLatticeFromQEVectors(ibrav, base)
            else:
                self.setLattice(ibrav ,a ,b , c, cBC ,cAC ,cAB, base)
Exemple #5
0
 def _matter_diffpy(self, structure):
     """
     converts matter Structure object to diffpy.Structure
     returns diffpy.Structure object
     """
     l = structure.lattice
     lat = Lattice( a=l.a, b=l.b, c=l.c, alpha=l.alpha, \
                     beta=l.beta, gamma=l.gamma)
     stru = Structure(lattice=lat)
     for a in structure:
         stru.addNewAtom(atype = a.symbol, xyz = a.xyz, name = a.symbol, \
                    anisotropy=a.anisotropy, U=a.U, Uisoequiv=a.Uisoequiv, \
                    lattice=lat)
     return stru
    def __init__(self,
                 atoms=[],
                 lattice=None,
                 title=None,
                 filename=None,
                 format=None):
        """define group of atoms in a specified lattice.

        atoms    -- list of Atom instances to be included in this Structure.
                    When atoms argument is an existing Structure instance,
                    the new Structure is its copy.
        lattice  -- instance of Lattice defining coordinate systems, property.
        title    -- string description of the structure
        filename -- optional, name of a file to load the structure from.
                    Overrides atoms argument when specified.
        format   -- optional structure format of the loaded filename.  By default
                    all structure formats are tried one by one.  Ignored when
                    filename has not been specified.

        Structure(stru)     create a copy of Structure instance stru.

        Because Structure is inherited from a list it can use list expansions,
        for example:
            oxygen_atoms = [ for a in stru if a.element == "O" ]
            oxygen_stru = Structure(oxygen_atoms, lattice=stru.lattice)
        """
        # if filename is specified load it and return
        if filename is not None:
            if any((atoms, lattice, title)):
                emsg = "Cannot use filename and atoms arguments together."
                raise ValueError(emsg)
            readkwargs = (format is not None) and {'format': format} or {}
            self.read(filename, **readkwargs)
            return
        # copy initialization, must be first to allow lattice, title override
        if isinstance(atoms, Structure):
            Structure.__copy__(atoms, self)
        # assign arguments:
        if title is not None:
            self.title = title
        if lattice is not None:
            self.lattice = lattice
        elif self.lattice is None:
            self.lattice = Lattice()
        # insert atoms unless already done by __copy__
        if not len(self) and len(atoms):
            self.extend(atoms)
        return
Exemple #7
0
 def test_xyz_cartn(self):
     """check Atom.xyz_cartn property
     """
     hexagonal = Lattice(1, 1, 1, 90, 90, 120)
     a0 = Atom('C', [0, 0, 0], lattice=hexagonal)
     a1 = Atom('C', [1, 1, 1], lattice=hexagonal)
     self.assertTrue(all(a0.xyz_cartn == 0))
     rc1 = numpy.array([0.75**0.5, 0.5, 1])
     self.assertTrue(numpy.allclose(rc1, a1.xyz_cartn))
     a1.xyz_cartn[2] = 0
     self.assertTrue(numpy.allclose([1, 1, 0], a1.xyz))
     a1.xyz_cartn[:2] = 0
     self.assertTrue(all(a1.xyz == 0))
     a3 = Atom('C', [1, 2, 3])
     self.assertTrue(numpy.array_equal(a3.xyz, a3.xyz_cartn))
     a3.xyz_cartn = 1.3
     self.assertTrue(all(1.3 == a3.xyz_cartn))
     self.assertTrue(all(1.3 == a3.xyz))
     return
    def __copy__(self, target=None):
        '''Create a deep copy of this instance.

        target   -- optional target instance for copying, useful for
                    copying a derived class.  Defaults to new instance
                    of the same type as self.

        Return a duplicate instance of this object.
        '''
        if target is None:
            target = Structure()
        elif target is self:
            return target
        # copy attributes as appropriate:
        target.title = self.title
        target.lattice = Lattice(self.lattice)
        target.pdffit = copy.deepcopy(self.pdffit)
        # copy all atoms to the target
        target[:] = self
        return target
Exemple #9
0
    def _setReducedStructureFromDiffpyStructure(self,
                                                structure,
                                                ibrav,
                                                massList=[],
                                                psList=[]):
        """
        structure - diffpy.Structure object
        ibrav - Lattice index
        psList - list of strings with potential names
        diffpyStructure object will be modified with reduced atomic positions
        """
        import copy

        diffpyLattice = copy.deepcopy(structure.lattice)

        a = diffpyLattice.a
        b = diffpyLattice.b
        c = diffpyLattice.c
        cAB = cosd(diffpyLattice.gamma)
        cBC = cosd(diffpyLattice.alpha)
        cAC = cosd(diffpyLattice.beta)

        qeLattice = QELattice(ibrav = ibrav, a = a, b = b, c = c,  cBC =  cBC, \
                              cAC = cAC, cAB = cAB)

        qeLattice._qeInput = self._qeInput
        self.lattice = qeLattice
        # make a deep copy:
        reducedStructure = Structure(atoms=structure)

        reducedStructure.placeInLattice(Lattice(base=qeLattice.diffpy().base))

        # collect atoms that are at equivalent position to some previous atom
        duplicates = set([
            a1 for i0, a0 in enumerate(reducedStructure)
            for a1 in reducedStructure[i0 + 1:]
            if self._element(a0) == self._element(a1)
            and equalPositions(a0.xyz, a1.xyz, eps=1e-4)
        ])

        # Filter out duplicate atoms.  Use slice assignment so that
        # reducedStructure is not replaced with a list.
        reducedStructure[:] = [
            a for a in reducedStructure if not a in duplicates
        ]

        atomNames = []
        for a in reducedStructure:
            if self._element(a) not in atomNames:
                atomNames.append(self._element(a))

        atomicSpecies = {}
        for i, elem in enumerate(atomNames):
            if len(massList) - 1 < i:
                mass = 0
            else:
                mass = massList[i]
            if len(psList) - 1 < i:
                ps = ''
            else:
                ps = psList[i]
            atomicSpecies[elem] = (mass, ps)

        self[:] = []

        # convert to bohr units
        self.lattice.setLattice(ibrav, self.lattice.a*1.889725989, \
                                 self.lattice.b*1.889725989,
                                 self.lattice.c*1.889725989)

        for atom in reducedStructure:
            elem = self._element(atom)
            self.addNewAtom(atype = elem, xyz = atom.xyz, \
                            mass = atomicSpecies[elem][0], \
                            potential = atomicSpecies[elem][1],\
                            lattice = self.lattice, optConstraint = [])
    def test_load_matter(self):
        try:
            from matter import Structure, Atom, Lattice
        except ImportError:
            return

        at1 = Atom('V', [0., 0., 0.])
        at2 = Atom('V', [0.5, 0., 0.])
        at3 = Atom('V', [0., 0.5, 0.])
        at4 = Atom('V', [0., 0., 0.5])
        at5 = Atom('V', [0.5, 0.5, 0.])
        at6 = Atom('V', [0., 0.5, 0.5])
        at7 = Atom('V', [0.5, 0., 0.5])
        at8 = Atom('V', [0.5, 0.5, 0.5])

        at9 = Atom('V', [0.25, 0.25, 0.25])
        at10 = Atom('Fe', [0.75, 0.25, 0.25])
        at11 = Atom('V', [0.75, 0.75, 0.25])
        at12 = Atom('Fe', [0.25, 0.75, 0.25])

        at13 = Atom('Fe', [0.25, 0.25, 0.75])
        at14 = Atom('V', [0.75, 0.25, 0.75])
        at15 = Atom('Fe', [0.75, 0.75, 0.75])
        at16 = Atom('V', [0.25, 0.75, 0.75])
        # set a in angstrom
        a = 2. * 5.663 / 1.889725989
        struct = Structure( [ at1, at2, at3, at4, at5, at6, at7, at8, at9, \
                             at10, at11, at12, at13, at14, at15, at16], \
                             lattice = Lattice(a, a, a, 90, 90, 90) )
        #print struct
        massList = [50.9415, 55.847]
        psList = ['V.pbe-n-van.UPF', 'Fe.pbe-nd-rrkjus.UPF']

        self.input.structure.load(source = 'matter', structure = struct, \
                                ibrav = 2, massList = massList, psList = psList)

        answer1 = """"Face Centered Cubic" cell:
-5.66300000  0.00000000  5.66300000
 0.00000000  5.66300000  5.66300000
-5.66300000  5.66300000  0.00000000

Atomic positions in units of lattice parametr "a":
V       0.00000000  0.00000000  0.00000000  
V       0.50000000  0.00000000  0.00000000  
V       0.25000000  0.25000000  0.25000000  
Fe      0.75000000  0.25000000  0.25000000  

V   50.9415 V.pbe-n-van.UPF
Fe  55.8470 Fe.pbe-nd-rrkjus.UPF
"""

        self.assertEqual(str(self.input.structure), answer1)

        self.input.structure.load(source = 'matter', structure = struct, \
                                massList = massList, psList = psList)

        answer2 = """"generic" cell:
 11.32600000  0.00000000  0.00000000
 0.00000000  11.32600000  0.00000000
 0.00000000  0.00000000  11.32600000

Atomic positions in units of lattice parametr "a":
V       0.00000000  0.00000000  0.00000000  
V       2.99673076  0.00000000  0.00000000  
V       0.00000000  2.99673076  0.00000000  
V       0.00000000  0.00000000  2.99673076  
V       2.99673076  2.99673076  0.00000000  
V       0.00000000  2.99673076  2.99673076  
V       2.99673076  0.00000000  2.99673076  
V       2.99673076  2.99673076  2.99673076  
V       1.49836538  1.49836538  1.49836538  
Fe      4.49509614  1.49836538  1.49836538  
V       4.49509614  4.49509614  1.49836538  
Fe      1.49836538  4.49509614  1.49836538  
Fe      1.49836538  1.49836538  4.49509614  
V       4.49509614  1.49836538  4.49509614  
Fe      4.49509614  4.49509614  4.49509614  
V       1.49836538  4.49509614  4.49509614  

V   50.9415 V.pbe-n-van.UPF
Fe  55.8470 Fe.pbe-nd-rrkjus.UPF
"""
        self.assertEqual(str(self.input.structure), answer2)
Exemple #11
0
#!/usr/bin/env python
import math
import numpy

# To change this template, choose Tools | Templates
# and open the template in the editor.
from diffpy.Structure.structure import Structure
from diffpy.Structure.lattice import Lattice

myAtoms = ['Mg', 'B', 'B']
myLattice = Lattice()
theta = -30. * math.pi / 180.
rotMatrix = [[math.cos(theta), -math.sin(theta), 0],
             [math.sin(theta), math.cos(theta), 0], [0, 0, 1]]
#myLattice.setLatPar(1., 1, 1.,gamma=120.0)#,baserot = rotMatrix)
print myLattice.base
a = 5.2
c = 6.3
base = [[1, 0, 0], [-1. / 2., math.sqrt(3.) / 2., 0.], [0, 0, c / a]]
myLattice.setLatBase(base)
print myLattice.base
print "baserot:"
print myLattice.baserot

myLattice.setLatPar(5.2, 5.2, 6.3, gamma=120.0, baserot=myLattice.baserot)
print myLattice.base

#myLattice.setLatBase(numpy.array(base)*a)
#print myLattice
#print myLattice.abcABG()
Exemple #12
0
    def setReducedStructureFromDiffpyStructure(self, structure, ibrav, massList = [], psList = []):
        """
        structure - diffpy.Structure object
        ibrav - Lattice index
        psList - list of strings with pseudopotential names
        diffpyStructure object will be modified with reduced atomic positions
        """

        #self.atomicSpecies = OrderedDict()
        #self.optConstraints = []
        #self.atomicPositionsType = 'crystal'

        diffpyLattice = structure.lattice

        a = diffpyLattice.a
        b = diffpyLattice.b
        c = diffpyLattice.c
        cAB = cosd(diffpyLattice.gamma)
        cBC = cosd(diffpyLattice.alpha)
        cAC = cosd(diffpyLattice.beta)

        qeLattice = QELattice(ibrav = ibrav, a = a, b = b, c = c,  cBC =  cBC, \
                              cAC = cAC, cAB = cAB)

        self.lattice = qeLattice
        # make a deep copy (does not wok now)
        #reducedStructure = Structure(diffpyStructure)
        reducedStructure = structure

        reducedStructure.placeInLattice(Lattice(base=qeLattice.diffpy().base))

        # collect atoms that are at equivalent position to some previous atom
        duplicates = set([a1
            for i0, a0 in enumerate(reducedStructure) for a1 in reducedStructure[i0+1:]
                if   self._element(a0) == self._element(a1) and equalPositions(a0.xyz, a1.xyz, eps=1e-4)])

        
        # Filter out duplicate atoms.  Use slice assignment so that
        # reducedStructure is not replaced with a list.
        reducedStructure[:] = [a for a in reducedStructure if not a in duplicates]

        self.structure = reducedStructure

        atomNames = []
        for a in reducedStructure:
            if self._element(a) not in atomNames:
                atomNames.append(self._element(a))
        
        #print atomNames
        #print len(massList)
        for i, elem in enumerate(atomNames):
            if len(massList) - 1 < i:
                mass = 0
            else:
                mass = massList[i]
            if len(psList) - 1 < i:
                ps = ''
            else:
                ps = psList[i]      
            #print mass, ps
            # atomDict[a] = 
        # for i, atom in enumerate(reducedStructure):
            # elem = self._element(atom)
            # if len(massList) - 1 < i:
                # mass = 0
            # else:
                # mass = massList[i]
            # if len(psList) - 1 < i:
                # ps = ''
            # else:
                # ps = psList[i]            
            self.atomicSpecies[elem] =  AtomicSpecies(elem, mass, ps)
        
        for atom in reducedStructure:
            self.optConstraints.append([])

        # convert to bohr units
        self.lattice.setLattice(ibrav, self.lattice.a*1.889725989, \
                                 self.lattice.b*1.889725989,
                                 self.lattice.c*1.889725989)

        self.nat = len(reducedStructure)
        self.ntyp = len(self.atomicSpecies)