def read(self, filename, format='pwinput'): """Load structure from a file, any original data become lost. l filename -- file to be loaded format -- structure formats 'pwinput' ( pw.x input, default), 'pwoutput' (pw.x output), 'bratoms', 'cif', 'discus', 'pdb', 'pdffit', 'rawxyz', 'xcfg', 'xyz' Return instance of data Parser used to process file. This can be inspected for information related to particular format. """ from qecalc.qetask.qeparser.qestructureparser import parser_index if self._qeInput == None: from qecalc.qetask.qeparser.pwinput import PWInput self._qeInput = PWInput() self._qeInput.structure = self #self._qeInput.parse() if format in parser_index: module = __import__("qestructureparser.P_" + format, globals(), \ locals(), ['P_' + format], -1) parser = module.getParser(self._qeInput) new_structure = parser.parse(filename) else: diffpyStruct = Structure() parser = diffpyStruct.read(filename, format=format) new_structure = QEStructure(qeInput=self._qeInput) new_structure._setStructureFromDiffpyStructure(diffpyStruct, \ massList = [], psList = [], ibrav = 0) new_structure.lattice._qeInput.update(forceUpdate=True) self.__Init(new_structure) return parser
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)
def __init__(self, qeConf): """the structure is initialized from PWSCF config file 'lattice' and 'structure' are automatically updated""" self.filename = qeConf.filename self.atomicSpecies = OrderedDict() self.formatString = '%# .8f %# .8f %# .8f' # optConstraints three 1/0 for each coordinate of each atom self.optConstraints = [] self.qeConf = qeConf self.lattice = QELattice() self.structure = Structure(lattice=self.lattice.diffpy()) self.nat = None self.ntyp = None
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 setStructureFromQEInput(self): """ Loads structure from PWSCF config file""" self.atomicSpecies = OrderedDict() self.lattice.setLatticeFromPWInput(self.qeConf) #self.lattice = QELattice(qeConf = self.qeConf) self.structure = Structure(lattice = self.lattice.diffpy()) self.nat = self.ntyp = None self.filename = self.qeConf.filename self.optConstraints = [] if 'system' in self.qeConf.namelists: self.nat = int(self.qeConf.namelist('system').param('nat')) self.ntyp = int(self.qeConf.namelist('system').param('ntyp')) if 'atomic_positions' in self.qeConf.cards: atomicLines = self.qeConf.card('atomic_positions').lines() self.atomicPositionsType = self.qeConf.card('atomic_positions').arg() if self.atomicPositionsType == 'bohr' or self.atomicPositionsType == 'angstrom': raise NotImplementedError if self.atomicPositionsType == None: self.atomicPositionsType = 'alat' for line in atomicLines: if '!' not in line: words = line.split() coords = [float(w) for w in words[1:4]] constraint = [] if len(words) > 4: constraint = [int(c) for c in words[4:7]] self.optConstraints.append(numpy.array(constraint, dtype = int)) atomSymbol = words[0] if self.atomicPositionsType == 'alat': coords = self.lattice.diffpy().fractional(numpy.array(coords[0:3])*self.lattice.a0) if self.atomicPositionsType == 'crystal': coords = numpy.array(coords[0:3]) self.structure.addNewAtom(atomSymbol, xyz = numpy.array(coords[0:3])) # parse mass ATOMIC_SPECIES section: if 'atomic_species' in self.qeConf.cards: atomicSpeciesLines = self.qeConf.card('atomic_species').lines() for line in atomicSpeciesLines: if '!' not in line: if line.strip() != '': atomicSpeciesWords = line.split() element = atomicSpeciesWords[0] mass = 0 ps = '' if len(atomicSpeciesWords) > 1 : mass = float(atomicSpeciesWords[1]) if len(atomicSpeciesWords) > 2: ps = atomicSpeciesWords[2] self.atomicSpecies[element] = AtomicSpecies(element, mass, ps)
def convert_atoms_to_stru(atoms): """ Convert between ASE and Diffpy structural objects Parameters ----------- atoms: ase.Atoms object Returns ------- diffpy.Structure object: """ diffpy_atoms = [] symbols = atoms.get_chemical_symbols() q = atoms.get_positions() tags = atoms.get_tags() for symbol, xyz, tag, in zip(symbols, q, tags): d_atom = dAtom(symbol, xyz=xyz, label=tag, occupancy=1) diffpy_atoms.append(d_atom) stru = Structure(diffpy_atoms) return stru
def diffpy(self): stru = Structure(lattice=self.lattice.diffpy()) for atom in self: stru.addNewAtom(atype = atom.element, xyz = atom.xyz, \ lattice = self.lattice.diffpy() ) return stru
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 setStructureFromPWOutput(self, pwscfOutputFile): """ Loads structure from PWSCF output file. If there was geometry optimization (relax or vc-relax), the structure will be reinitialized from the last step of the optimization """ file = open(pwscfOutputFile) pwscfOut = file.readlines() pseudoList = [] atomList = [] massList = [] self.atomicSpecies = OrderedDict() self.atomicPositionsType = 'alat' # parse beginning: for i, line in enumerate(pwscfOut): if 'lattice parameter (a_0)' in line: a_0 = float(line.split()[4]) if 'bravais-lattice index' in line: ibrav = int(line.split('=')[1]) if 'number of atoms/cell' in line: self.nat = int(line.split('=')[1]) if 'number of atomic types' in line: self.ntyp = int(line.split('=')[1]) if 'PseudoPot.' in line: pseudoList.append(line.split('read from file')[1].strip()) if 'atomic species valence mass pseudopotential' in line: for j in range(self.ntyp): atomList.append(pwscfOut[i + j + 1].split()[0]) massList.append(float(pwscfOut[i + j + 1].split()[2])) if 'crystal axes: (cart. coord. in units of a_0)' in line: latticeVectors = [ [float(f) * a_0 for f in pwscfOut[i + 1].split()[3:6]], [float(f) * a_0 for f in pwscfOut[i + 2].split()[3:6]], [float(f) * a_0 for f in pwscfOut[i + 3].split()[3:6]] ] self.lattice.setLatticeFromQEVectors(ibrav, latticeVectors) if 'site n. atom positions (a_0 units)' in line: self.structure = Structure(lattice=self.lattice.diffpy()) for n in range(self.nat): words = pwscfOut[i + n + 1].split() atomSymbol = words[1] coords = [float(w) for w in words[6:9]] constraint = [] self.optConstraints.append( numpy.array(constraint, dtype=int)) #print numpy.array(coords[0:3])*a_0 coords = self.lattice.diffpy().fractional( numpy.array(coords[0:3]) * a_0) self.structure.addNewAtom(atomSymbol, xyz=numpy.array(coords[0:3])) for a, m, p in zip(atomList, massList, pseudoList): self.atomicSpecies[a] = AtomicSpecies(a, m, p) #print 'Input structure from output file: ', self.toString() #Parse end: # Find all geometry optimization steps posList = [ i for i, line in enumerate(pwscfOut) if '! total energy' in line ] lastSection = pwscfOut[posList[-1]:] for i, line in enumerate(lastSection): if 'CELL_PARAMETERS (alat)' in line: latticeVectors = [ [float(f) * a_0 for f in lastSection[i + 1].split()], [float(f) * a_0 for f in lastSection[i + 2].split()], [float(f) * a_0 for f in lastSection[i + 3].split()] ] self.lattice.setLatticeFromQEVectors(ibrav, latticeVectors) #self.lattice = QELattice(ibrav = ibrav, base = latticeVectors) print self.lattice.diffpy().base if 'ATOMIC_POSITIONS (alat)' in line: self.structure = Structure(lattice=self.lattice.diffpy()) for n in range(self.nat): words = lastSection[i + n + 1].split() atomSymbol = words[0] coords = [float(w) for w in words[1:4]] constraint = [] if len(words) > 4: constraint = [int(c) for c in words[4:7]] self.optConstraints.append( numpy.array(constraint, dtype=int)) coords = self.lattice.diffpy().fractional( numpy.array(coords[0:3]) * a_0) self.structure.addNewAtom(atomSymbol, xyz=numpy.array(coords[0:3]))
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)
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() myStruct = Structure(myAtoms, myLattice) # fractional coordinates: myStruct[0].xyz[:] = [ 0, 0, 0, ] myStruct[1].xyz[:] = [ 1, 2, 3, ] print myStruct