def read(self, filename, format = 'pwinput'): """Load structure from a file, any original data become lost. 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: struct = Structure() parser = struct.read(filename, format = format) new_structure = QEStructure(qeInput = self._qeInput) new_structure._setStructureFromMatterStructure(struct, \ massList = [], psList = [], ibrav = 0) new_structure.lattice._qeInput.update( forceUpdate = True ) self.__Init(new_structure) return parser
def read(self, filename, format='pwinput'): """Load structure from a file, any original data become lost. 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: struct = Structure() parser = struct.read(filename, format=format) new_structure = QEStructure(qeInput=self._qeInput) new_structure._setStructureFromMatterStructure(struct, \ massList = [], psList = [], ibrav = 0) new_structure.lattice._qeInput.update(forceUpdate=True) self.__Init(new_structure) return parser
def testGetChemicalFormula(self): at1 = Atom('C', [0.333333333333333, 0.666666666666667, 0]) at2 = Atom('C', [0.666666666666667, 0.333333333333333, 0]) #at3 = Atom('H', [0, 0, 0]) stru = Structure( [ at1, at2], lattice=Lattice(3.8, 3.8, 5.6, 90, 90, 120) ) print 'formula: ', stru.getChemicalFormula() return
def setUp(self): # at1 = Atom('C', [0.333333333333333, 0.666666666666667, 0]) # at2 = Atom('C', [0.666666666666667, 0.333333333333333, 0]) at1 = Atom('C', [0, 0, 0]) at2 = Atom('C', [1, 1, 1]) self.stru = Structure([at1, at2], lattice=Lattice(1, 1, 1, 90, 90, 120)) self.places = 12
def _setReducedStructureFromMatterStructure(self, structure, ibrav, massList = [], psList = []): """ structure - matter.Structure object ibrav - Lattice index psList - list of strings with potential names matterStructure object will be modified with reduced atomic positions """ import copy matterLattice = copy.deepcopy(structure.lattice) # there is a big problem with what Nikolay is doing because he is initializing QELattice with abcalbega which # leaves room for errors in how the basis vectors are chosen compared to the old lattice a = matterLattice.a b = matterLattice.b c = matterLattice.c cAB = cosd(matterLattice.gamma) cBC = cosd(matterLattice.alpha) cAC = cosd(matterLattice.beta) qeLattice = QELattice(ibrav = ibrav, a = a, b = b, c = c, cBC = cBC, \ cAC = cAC, cAB = cAB) #what he should do is initialize the vectors of QELattice with the vectors of matterLattice # qeLattice._qeInput = self._qeInput self.lattice = qeLattice # make a deep copy: reducedStructure = Structure(atoms = structure) reducedStructure.placeInLattice(Lattice(base=qeLattice.matter().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 a0.symbol==a1.symbol 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 a.symbol not in atomNames: atomNames.append(a.symbol) 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: self.addNewAtom(atype = atom.symbol, xyz = atom.xyz, \ mass = atomicSpecies[atom.symbol][0], \ potential = atomicSpecies[atom.symbol][1],\ lattice = self.lattice, optConstraint = [])
def testGetChemicalFormula(self): at1 = Atom('C', [0.333333333333333, 0.666666666666667, 0]) at2 = Atom('C', [0.666666666666667, 0.333333333333333, 0]) #at3 = Atom('H', [0, 0, 0]) stru = Structure([at1, at2], lattice=Lattice(3.8, 3.8, 5.6, 90, 90, 120)) print 'formula: ', stru.getChemicalFormula() return
def testChemicalFormulaPositionsSymbols(self): at1 = Atom('C', [0.333333333333333, 0.666666666666667, 0]) at2 = Atom('C', [0.666666666666667, 0.333333333333333, 0]) #at3 = Atom('H', [0, 0, 0]) stru = Structure( [ at1, at2], lattice=Lattice(3.8, 3.8, 5.6, 90, 90, 120) ) assert stru.getChemicalFormula()=='C_2' #self.assertListAlmostEqual(stru.xyz, [[0.33333333333333298, 0.66666666666666696, 0.0], [0.66666666666666696, 0.33333333333333298, 0.0]]) #self.assertListAlmostEqual(stru.xyz_cartn,[[1.0969655114602876, 1.9000000000000017, 0.0], [2.1939310229205784, -2.0020877317117325e-15, 0.0]]) #self.assertListAlmostEqual(stru.symbols, ['C', 'C']) #print "here's the lattice", stru.lattice.base return
def test2a(self): 'Structure: methood "symConsistent"' lattice = Lattice(a=2,b=2,c=2,alpha=90,beta=90,gamma=90) atoms = [ Atom('Ni', (0,0,0)), Atom('Ni', (0,0.5,0.5)), Atom('Ni', (0.5,0,0.5)), Atom('Ni', (0.5,0.5,0)), ] struct = Structure(lattice=lattice, atoms=atoms, sgid=225) verdict, pos, op = struct.symConsistent() self.assert_(verdict) return
def test2a(self): 'Structure: methood "symConsistent"' lattice = Lattice(a=2, b=2, c=2, alpha=90, beta=90, gamma=90) atoms = [ Atom('Ni', (0, 0, 0)), Atom('Ni', (0, 0.5, 0.5)), Atom('Ni', (0.5, 0, 0.5)), Atom('Ni', (0.5, 0.5, 0)), ] struct = Structure(lattice=lattice, atoms=atoms, sgid=225) verdict, pos, op = struct.symConsistent() self.assert_(verdict) return
def test3(self): 'Structure: "primitive_unitcell"' lattice = Lattice(a=2,b=2,c=2,alpha=90,beta=90,gamma=90) atoms = [ Atom('Fe', (0,0,0)), Atom('Pd', (0,0.5,0.5)), Atom('Pd', (0.5,0,0.5)), Atom('Pd', (0.5,0.5,0)), ] struct = Structure(lattice=lattice, atoms=atoms, sgid=221) verdict, pos, op = struct.symConsistent() print verdict, pos, op self.assert_(verdict) self.assertEqual(len(struct.primitive_unitcell.atoms), 4) return
def test3(self): 'Structure: "primitive_unitcell"' lattice = Lattice(a=2, b=2, c=2, alpha=90, beta=90, gamma=90) atoms = [ Atom('Fe', (0, 0, 0)), Atom('Pd', (0, 0.5, 0.5)), Atom('Pd', (0.5, 0, 0.5)), Atom('Pd', (0.5, 0.5, 0)), ] struct = Structure(lattice=lattice, atoms=atoms, sgid=221) verdict, pos, op = struct.symConsistent() print verdict, pos, op self.assert_(verdict) self.assertEqual(len(struct.primitive_unitcell.atoms), 4) return
def setUp(self): # at1 = Atom('C', [0.333333333333333, 0.666666666666667, 0]) # at2 = Atom('C', [0.666666666666667, 0.333333333333333, 0]) at1 = Atom('C', [0, 0, 0]) at2 = Atom('C', [1, 1, 1]) self.stru = Structure( [ at1, at2], lattice=Lattice(1, 1, 1, 90, 90, 120) ) self.places = 12
def __init__(self, atoms=[], lattice=None, filename=None, qeInput=None): """ atoms -- list of QEAtom atom instances or a QEStructure object lattice -- QELattice object filename -- filename QE input file qeInput -- pointer to a PWInput parsing object. If not None, its PWInput.structure and PWInput.structure.lattice will be reset to the current instance of the structure """ Structure.__init__(self) self.formatString = '%# .8f %# .8f %# .8f' self._atomicPositionsType = 'crystal' self._qeInput = qeInput self.lattice = QELattice() self.lattice._qeInput = self._qeInput if lattice != None: if self.lattice._qeInput != None: self._qeInput = self.lattice._qeInput else: self.lattice._qeInput = self._qeInput self.lattice = QELattice(lattice=lattice) # CP and PW inputs are compatible from pwinput import PWInput from cpinput import CPInput if isinstance(atoms, PWInput) or isinstance(atoms, CPInput): qeInput = atoms elif isinstance(atoms, QEStructure): stru = atoms self.__dict__.update(stru.__dict__) # deep copy of the lattice will deep copy PWInput as well self.lattice = QELattice(lattice=stru.lattice) self._qeInput = self.lattice._qeInput self[:] = stru else: if self.lattice == None: raise "Lattice must be provided" self[:] = atoms if filename != None: qeInput = PWInput(filename=filename) qeInput.parse() self.parseInput(qeInput) if qeInput != None: self.lattice._qeInput = qeInput self._qeInput = qeInput if self.lattice._qeInput != None: self.lattice._qeInput.structure = self self.lattice._qeInput.structure.lattice = self.lattice
def __init__(self, atoms = [], lattice = None, filename = None, qeInput = None): """ atoms -- list of QEAtom atom instances or a QEStructure object lattice -- QELattice object filename -- filename QE input file qeInput -- pointer to a PWInput parsing object. If not None, its PWInput.structure and PWInput.structure.lattice will be reset to the current instance of the structure """ Structure.__init__(self) self.formatString = '%# .8f %# .8f %# .8f' self._atomicPositionsType = 'crystal' self._qeInput = qeInput self.lattice = QELattice() self.lattice._qeInput = self._qeInput if lattice != None: if self.lattice._qeInput != None: self._qeInput = self.lattice._qeInput else: self.lattice._qeInput = self._qeInput self.lattice = QELattice( lattice = lattice ) # CP and PW inputs are compatible from pwinput import PWInput from cpinput import CPInput if isinstance( atoms, PWInput) or isinstance( atoms, CPInput): qeInput = atoms elif isinstance( atoms, QEStructure): stru = atoms self.__dict__.update( stru.__dict__ ) # deep copy of the lattice will deep copy PWInput as well self.lattice = QELattice(lattice = stru.lattice) self._qeInput = self.lattice._qeInput self[:] = stru else: if self.lattice == None: raise "Lattice must be provided" self[:] = atoms if filename != None: qeInput = PWInput(filename = filename) qeInput.parse() self.parseInput(qeInput) if qeInput != None: self.lattice._qeInput = qeInput self._qeInput = qeInput if self.lattice._qeInput != None: self.lattice._qeInput.structure = self self.lattice._qeInput.structure.lattice = self.lattice
def setUp(self): xyzfile = os.path.join(testdata_dir, 'scf3.xyz') self.stru = Structure() self.stru.read(xyzfile) #print self.stru5 file = os.path.join(testdata_dir, 'Pb.cif') self.stru2 = Structure() self.stru2.read(file) #print self.stru2 # xyzfile = os.path.join(testdata_dir, 'KC24PosAndCharges.xyz') # self.stru2 = Structure() # self.stru2.read(xyzfile) self.places = 12
def setUp(self): xyzfile = os.path.join(testdata_dir, 'scf3.xyz') self.stru = Structure() self.stru.read(xyzfile) #print self.stru5 file = os.path.join(testdata_dir, 'Pb.cif') self.stru2 = Structure() self.stru2.read(file) #print self.stru2 # xyzfile = os.path.join(testdata_dir, 'KC24PosAndCharges.xyz') # self.stru2 = Structure() # self.stru2.read(xyzfile) self.places = 12
def setUp(self): # at1 = Atom('C', [0.333333333333333, 0.666666666666667, 0]) # at2 = Atom('C', [0.666666666666667, 0.333333333333333, 0]) at1 = Atom('C', [0, 0, 0]) at2 = Atom('C', [1, 1, 1]) self.stru = Structure([at1, at2], lattice=Lattice(1, 1, 1, 90, 90, 120)) ciffile = os.path.join(testdata_dir, 'PbTe.cif') self.stru2 = Structure() self.stru2.read(ciffile) ciffile = os.path.join(testdata_dir, 'graphite.cif') self.stru3 = Structure() self.stru3.read(ciffile) at1 = Atom('Al', [0.0, 0.0, 0.0]) at2 = Atom('Al', [0.0, 0.5, 0.5]) at3 = Atom('Al', [0.5, 0.0, 0.5]) at4 = Atom('Al', [0.5, 0.5, 0.0]) self.stru4 = Structure([at1, at2, at3, at4], lattice=Lattice(4.05, 4.05, 4.05, 90, 90, 90), sgid=225) self.places = 12
def load(fullfilename): filedir, fileprefix, fileext = path_split(fullfilename) material=Material(fileprefix) try: from matter.Structure import Structure st = Structure() st.read(fullfilename) for i,atom in enumerate(st): Z = atom.Z xyz_cartn = atom.xyz_cartn sym = atom.symbol material.add_atom(Atom(Z, xyz_cartn, sym, sym+str(i))) material.bonds_from_distance() lvs = st.lattice.base cell = Cell(lvs[0],lvs[1],lvs[2]) material.set_cell(cell) return material except: print 'Vimm needs matter package from pypi to open cif files' return
def test1(self): 'Structure: property "primitive_unitcell"' lattice = Lattice(a=2, b=2, c=2, alpha=90, beta=90, gamma=90) atoms = [ Atom('Ni', (0, 0, 0)), Atom('Ni', (0, 0.5, 0.5)), Atom('Ni', (0.5, 0, 0.5)), Atom('Ni', (0.5, 0.5, 0)), ] struct = Structure(lattice=lattice, atoms=atoms, sgid=225) print struct.primitive_unitcell return
def setUp(self): # at1 = Atom('C', [0.333333333333333, 0.666666666666667, 0]) # at2 = Atom('C', [0.666666666666667, 0.333333333333333, 0]) at1 = Atom('C', [0, 0, 0]) at2 = Atom('C', [1, 1, 1]) self.stru = Structure( [ at1, at2], lattice=Lattice(1, 1, 1, 90, 90, 120) ) ciffile = os.path.join(testdata_dir, 'PbTe.cif') self.stru2 = Structure() self.stru2.read(ciffile) ciffile = os.path.join(testdata_dir, 'graphite.cif') self.stru3 = Structure() self.stru3.read(ciffile) at1 = Atom('Al', [0.0, 0.0, 0.0]) at2 = Atom('Al', [0.0, 0.5, 0.5]) at3 = Atom('Al', [0.5, 0.0, 0.5]) at4 = Atom('Al', [0.5, 0.5, 0.0]) self.stru4 = Structure( [ at1, at2, at3, at4], lattice=Lattice(4.05, 4.05, 4.05, 90, 90, 90), sgid = 225 ) self.places = 12
def test1(self): 'Structure: property "primitive_unitcell"' lattice = Lattice(a=2, b=2, c=2, alpha=90, beta=90, gamma=90) atoms = [ Atom('Ni', (0, 0, 0)), Atom('Ni', (0, 0.5, 0.5)), Atom('Ni', (0.5, 0, 0.5)), Atom('Ni', (0.5, 0.5, 0)), ] struct = Structure(lattice=lattice, atoms=atoms, sgid=225) pcell = struct.primitive_unitcell self.assert_(pcell) self.assertArrayEqual(pcell.base, [[-1., 0., 1.], [0., 1., 1.], [-1., 1., 0.]]) return
class TestStructure(unittest.TestCase): """test methods of Structure class""" def setUp(self): xyzfile = os.path.join(testdata_dir, 'scf3.xyz') self.stru = Structure() self.stru.read(xyzfile) #print self.stru5 file = os.path.join(testdata_dir, 'Pb.cif') self.stru2 = Structure() self.stru2.read(file) #print self.stru2 # xyzfile = os.path.join(testdata_dir, 'KC24PosAndCharges.xyz') # self.stru2 = Structure() # self.stru2.read(xyzfile) self.places = 12 def testRS(self): """reciprocal space tests""" print self.stru2.lattice print self.stru2.lattice.recbase print self.stru2.lattice.recbase2pi def assertListAlmostEqual(self, l1, l2, places=None): """wrapper for list comparison""" if places is None: places = self.places self.assertEqual(len(l1), len(l2)) for i in range(len(l1)): self.assertAlmostEqual(l1[i], l2[i], places) def test_charges(self): charges = [0.0, 0.0, 0.0, 0.0] self.stru.charges = charges self.assertAlmostEqual(self.stru[0].charge, charges[0]) #print self.stru2.charges def test_writeStr(self): """check Structure.writeStr()""" print self.stru.writeStr('xyz') return
class TestStructure(unittest.TestCase): """test methods of Structure class""" def setUp(self): xyzfile = os.path.join(testdata_dir, 'scf3.xyz') self.stru = Structure() self.stru.read(xyzfile) #print self.stru5 file = os.path.join(testdata_dir, 'Pb.cif') self.stru2 = Structure() self.stru2.read(file) #print self.stru2 # xyzfile = os.path.join(testdata_dir, 'KC24PosAndCharges.xyz') # self.stru2 = Structure() # self.stru2.read(xyzfile) self.places = 12 def testRS(self): """reciprocal space tests""" print self.stru2.lattice print self.stru2.lattice.recbase print self.stru2.lattice.recbase2pi def assertListAlmostEqual(self, l1, l2, places=None): """wrapper for list comparison""" if places is None: places = self.places self.assertEqual(len(l1), len(l2)) for i in range(len(l1)): self.assertAlmostEqual(l1[i], l2[i], places) def test_charges(self): charges = [0.0, 0.0, 0.0, 0.0] self.stru.charges = charges self.assertAlmostEqual(self.stru[0].charge, charges[0]) #print self.stru2.charges def test_writeStr(self): """check Structure.writeStr()""" print self.stru.writeStr('xyz') return
class TestStructure(unittest.TestCase): """test methods of Structure class""" def setUp(self): xyzfile = os.path.join(testdata_dir, 'scf3.xyz') self.stru = Structure() self.stru.read(xyzfile) #print self.stru5 file = os.path.join(testdata_dir, 'Pb.cif') self.stru2 = Structure() self.stru2.read(file) #print self.stru2 # xyzfile = os.path.join(testdata_dir, 'KC24PosAndCharges.xyz') # self.stru2 = Structure() # self.stru2.read(xyzfile) self.places = 12 def testRS(self): """reciprocal space tests""" # print self.stru2.lattice recbase = self.stru2.lattice.recbase expected = \ [[ 0.20242669, 0., 0. ], [ 0., 0.20242669, 0. ], [ 0., 0., 0.20242669]] from numpy import array expected = array(expected) self.assertArrayEqual(recbase, expected) from math import pi self.assertArrayEqual( self.stru2.lattice.recbase2pi, expected * 2 * pi, ) return def test_charges(self): charges = [0.0, 0.0, 0.0, 0.0] self.stru.charges = charges self.assertAlmostEqual(self.stru[0].charge, charges[0]) #print self.stru2.charges def test_writeStr(self): """check Structure.writeStr()""" s = self.stru.writeStr('xyz') expected = '''4 F_3 Sc_1 Sc 0 0 0 F 0 8.04246 8.17697 F 8.04246 0 8.17697 F 8.04246 8.04246 0 ''' self.assertEqual(s, expected) return def assertListAlmostEqual(self, l1, l2, places=None): """wrapper for list comparison""" if places is None: places = self.places self.assertEqual(len(l1), len(l2)) for i in range(len(l1)): self.assertAlmostEqual(l1[i], l2[i], places) def assertArrayEqual(self, v1, v2): from numpy.testing import assert_array_almost_equal assert_array_almost_equal(v1, v2) return
class TestStructure(unittest.TestCase): """test methods of Structure class""" def setUp(self): xyzfile = os.path.join(testdata_dir, 'scf3.xyz') self.stru = Structure() self.stru.read(xyzfile) #print self.stru5 file = os.path.join(testdata_dir, 'Pb.cif') self.stru2 = Structure() self.stru2.read(file) #print self.stru2 # xyzfile = os.path.join(testdata_dir, 'KC24PosAndCharges.xyz') # self.stru2 = Structure() # self.stru2.read(xyzfile) self.places = 12 def testRS(self): """reciprocal space tests""" # print self.stru2.lattice recbase = self.stru2.lattice.recbase expected = \ [[ 0.20242669, 0., 0. ], [ 0., 0.20242669, 0. ], [ 0., 0., 0.20242669]] from numpy import array expected = array(expected) self.assertArrayEqual(recbase, expected) from math import pi self.assertArrayEqual( self.stru2.lattice.recbase2pi, expected * 2 * pi, ) return def test_charges(self): charges = [0.0, 0.0, 0.0, 0.0] self.stru.charges = charges self.assertAlmostEqual(self.stru[0].charge, charges[0]) #print self.stru2.charges def test_writeStr(self): """check Structure.writeStr()""" s = self.stru.writeStr('xyz') expected = '''4 F_3 Sc_1 Sc 0 0 0 F 0 8.04246 8.17697 F 8.04246 0 8.17697 F 8.04246 8.04246 0 ''' self.assertEqual(s, expected) return def assertListAlmostEqual(self, l1, l2, places=None): """wrapper for list comparison""" if places is None: places = self.places self.assertEqual(len(l1), len(l2)) for i in range(len(l1)): self.assertAlmostEqual(l1[i], l2[i], places) def assertArrayEqual(self, v1, v2): from numpy.testing import assert_array_almost_equal assert_array_almost_equal(v1, v2) return
def matter(self): stru = Structure(lattice = self.lattice.matter()) for atom in self: stru.addNewAtom(atype = atom.symbol, xyz = atom.xyz, \ lattice = self.lattice.matter() ) return stru
class TestStructure(unittest.TestCase): """test methods of Structure class""" def setUp(self): # at1 = Atom('C', [0.333333333333333, 0.666666666666667, 0]) # at2 = Atom('C', [0.666666666666667, 0.333333333333333, 0]) at1 = Atom('C', [0, 0, 0]) at2 = Atom('C', [1, 1, 1]) self.stru = Structure( [ at1, at2], lattice=Lattice(1, 1, 1, 90, 90, 120) ) self.places = 12 def assertListAlmostEqual(self, l1, l2, places=None): """wrapper for list comparison""" if places is None: places = self.places self.assertEqual(len(l1), len(l2)) for i in range(len(l1)): self.assertAlmostEqual(l1[i], l2[i], places) def testGetChemicalFormula(self): at1 = Atom('C', [0.333333333333333, 0.666666666666667, 0]) at2 = Atom('C', [0.666666666666667, 0.333333333333333, 0]) #at3 = Atom('H', [0, 0, 0]) stru = Structure( [ at1, at2], lattice=Lattice(3.8, 3.8, 5.6, 90, 90, 120) ) print 'formula: ', stru.getChemicalFormula() return # FIXME move into TestAtom def test_cartesian(self): """check conversion to Cartesian coordinates""" from math import sqrt stru = self.stru s_rc0 = stru[0].xyz_cartn f_rc0 = 3*[0.0] s_rc1 = stru[1].xyz_cartn f_rc1 = [sqrt(0.75), 0.5, 1.] self.assertListAlmostEqual(s_rc0, f_rc0) self.assertListAlmostEqual(s_rc1, f_rc1) # def test___init__(self): # """check Structure.__init__() # """ # return # # def test___str__(self): # """check Structure.__str__() # """ # return # # def test_addNewAtom(self): # """check Structure.addNewAtom() # """ # return # # def test_getLastAtom(self): # """check Structure.getLastAtom() # """ # return # def test_getAtom(self): # """check Structure.getAtom() # """ # a0 = self.stru[0] # a1 = self.stru[1] # # check execeptions for invalid arguments # self.assertRaises(ValueError, self.stru.getAtom, 300) # self.assertRaises(ValueError, self.stru.getAtom, -44) # self.assertRaises(ValueError, self.stru.getAtom, "Na") # # check returned values # self.failUnless(a0 is self.stru.getAtom(0)) # self.failUnless(a1 is self.stru.getAtom(1)) # self.failUnless(a0 is self.stru.getAtom("C1")) # self.failUnless(a1 is self.stru.getAtom("C2")) # # check if labels get properly updated # cdsefile = os.path.join(testdata_dir, 'CdSe_bulk.stru') # cdse = Structure(filename=cdsefile) # self.stru[1:1] = cdse # self.failUnless(a0 is self.stru.getAtom("C1")) # self.failUnless(a1 is self.stru.getAtom("C2")) # self.failUnless(self.stru[1] is self.stru.getAtom("Cd1")) # return def test_getLabels(self): """check Structure.getLabels() """ self.assertEqual(["C1", "C2"], self.stru.getLabels()) pbtefile = os.path.join(testdata_dir, 'PbTe.cif') self.stru.read(pbtefile, format='cif') labels = self.stru.getLabels() self.assertEqual("Pb2+1", labels[0]) self.assertEqual("Pb2+4", labels[3]) self.assertEqual("Te1", labels[4]) self.assertEqual("Te4", labels[-1]) return def test_distance(self): """check Structure.distance() """ from math import sqrt self.assertRaises(ValueError, self.stru.distance, 333, "C1") self.assertRaises(ValueError, self.stru.distance, "C", "C1") self.assertAlmostEqual(sqrt(2.0), self.stru.distance(0, 1), self.places) self.assertAlmostEqual(sqrt(2.0), self.stru.distance("C1", "C2"), self.places) self.assertEqual(0, self.stru.distance(0, "C1")) return # def test_angle(self): # """check Structure.angle() # """ # return def test_placeInLattice(self): """check Structure.placeInLattice() -- conversion of coordinates """ stru = self.stru new_lattice = Lattice(.5, .5, .5, 90, 90, 60) stru.placeInLattice(new_lattice) a0 = stru[0] self.assertListAlmostEqual(a0.xyz, 3*[0.0]) a1 = stru[1] self.assertListAlmostEqual(a1.xyz, [2.0, 0.0, 2.0]) def test_forces(self): forces = [[0.0, 0.61, 0.7], [1.8, 0.9, 1.1]] self.stru.forces = forces self.assertListAlmostEqual(self.stru[0].force, forces[0]) def test_spaceGroupQuery(self): ciffile = os.path.join(testdata_dir, 'PbTe.cif') self.stru.read(ciffile) #print self.stru.spaceGroup.number #print self.stru.spaceGroup.short_name # op = self.stru.spa # temporarily disabledceGroup.symop_list[1] # print op sg = self.stru.sg assert sg.number is 225 print sg.num_sym_equiv print sg.num_primitive_sym_equiv print sg.short_name print sg.alt_name print sg.point_group_name print sg.crystal_system print sg.pdb_name for symop in sg.symop_list: print symop
class TestStructure(unittest.TestCase): """test methods of Structure class""" def setUp(self): # at1 = Atom('C', [0.333333333333333, 0.666666666666667, 0]) # at2 = Atom('C', [0.666666666666667, 0.333333333333333, 0]) at1 = Atom('C', [0, 0, 0]) at2 = Atom('C', [1, 1, 1]) self.stru = Structure( [ at1, at2], lattice=Lattice(1, 1, 1, 90, 90, 120) ) ciffile = os.path.join(testdata_dir, 'PbTe.cif') self.stru2 = Structure() self.stru2.read(ciffile) ciffile = os.path.join(testdata_dir, 'graphite.cif') self.stru3 = Structure() self.stru3.read(ciffile) at1 = Atom('Al', [0.0, 0.0, 0.0]) at2 = Atom('Al', [0.0, 0.5, 0.5]) at3 = Atom('Al', [0.5, 0.0, 0.5]) at4 = Atom('Al', [0.5, 0.5, 0.0]) self.stru4 = Structure( [ at1, at2, at3, at4], lattice=Lattice(4.05, 4.05, 4.05, 90, 90, 90), sgid = 225 ) self.places = 12 def assertListAlmostEqual(self, l1, l2, places=None): """wrapper for list comparison""" if places is None: places = self.places self.assertEqual(len(l1), len(l2)) for i in range(len(l1)): self.assertAlmostEqual(l1[i], l2[i], places) def testChemicalFormulaPositionsSymbols(self): at1 = Atom('C', [0.333333333333333, 0.666666666666667, 0]) at2 = Atom('C', [0.666666666666667, 0.333333333333333, 0]) #at3 = Atom('H', [0, 0, 0]) stru = Structure( [ at1, at2], lattice=Lattice(3.8, 3.8, 5.6, 90, 90, 120) ) assert stru.getChemicalFormula()=='C_2' #self.assertListAlmostEqual(stru.xyz, [[0.33333333333333298, 0.66666666666666696, 0.0], [0.66666666666666696, 0.33333333333333298, 0.0]]) #self.assertListAlmostEqual(stru.xyz_cartn,[[1.0969655114602876, 1.9000000000000017, 0.0], [2.1939310229205784, -2.0020877317117325e-15, 0.0]]) #self.assertListAlmostEqual(stru.symbols, ['C', 'C']) #print "here's the lattice", stru.lattice.base return def test_writeStr(self): """check Structure.writeStr()""" s = self.stru3.writeStr('xyz') expected = '4\nC_4\nC 0 0 0\nC 0 0 3.348\nC 0.708986 1.228 0\nC 1.41797 -2.67473e-16 3.348\n' self.assertEqual(s, expected) return # FIXME move into TestAtom def test_cartesian(self): """check conversion to Cartesian coordinates""" from math import sqrt stru = self.stru s_rc0 = stru[0].xyz_cartn f_rc0 = 3*[0.0] s_rc1 = stru[1].xyz_cartn f_rc1 = [sqrt(0.75), 0.5, 1.] self.assertListAlmostEqual(s_rc0, f_rc0) self.assertListAlmostEqual(s_rc1, f_rc1) # def test___init__(self): # """check Structure.__init__() # """ # return # # def test___str__(self): # """check Structure.__str__() # """ # return # # def test_addNewAtom(self): # """check Structure.addNewAtom() # """ # return # # def test_getLastAtom(self): # """check Structure.getLastAtom() # """ # return # def test_getAtom(self): # """check Structure.getAtom() # """ # a0 = self.stru[0] # a1 = self.stru[1] # # check execeptions for invalid arguments # self.assertRaises(ValueError, self.stru.getAtom, 300) # self.assertRaises(ValueError, self.stru.getAtom, -44) # self.assertRaises(ValueError, self.stru.getAtom, "Na") # # check returned values # self.failUnless(a0 is self.stru.getAtom(0)) # self.failUnless(a1 is self.stru.getAtom(1)) # self.failUnless(a0 is self.stru.getAtom("C1")) # self.failUnless(a1 is self.stru.getAtom("C2")) # # check if labels get properly updated # cdsefile = os.path.join(testdata_dir, 'CdSe_bulk.stru') # cdse = Structure(filename=cdsefile) # self.stru[1:1] = cdse # self.failUnless(a0 is self.stru.getAtom("C1")) # self.failUnless(a1 is self.stru.getAtom("C2")) # self.failUnless(self.stru[1] is self.stru.getAtom("Cd1")) # return def test_getLabels(self): """check Structure.getLabels() """ self.assertEqual(["C1", "C2"], self.stru.getLabels()) pbtefile = os.path.join(testdata_dir, 'PbTe.cif') self.stru.read(pbtefile, format='cif') labels = self.stru.getLabels() #TODO need to fix these tests #self.assertEqual("Pb2+1", labels[0]) #self.assertEqual("Pb2+4", labels[3]) self.assertEqual("Te1", labels[4]) self.assertEqual("Te4", labels[-1]) return def test_distance(self): """check Structure.distance() """ from math import sqrt self.assertRaises(ValueError, self.stru.distance, 333, "C1") self.assertRaises(ValueError, self.stru.distance, "C", "C1") self.assertAlmostEqual(sqrt(2.0), self.stru.distance(0, 1), self.places) self.assertAlmostEqual(sqrt(2.0), self.stru.distance("C1", "C2"), self.places) self.assertEqual(0, self.stru.distance(0, "C1")) return # def test_angle(self): # """check Structure.angle() # """ # return def test_placeInLattice(self): """check Structure.placeInLattice() -- conversion of coordinates """ stru = self.stru new_lattice = Lattice(.5, .5, .5, 90, 90, 60) stru.placeInLattice(new_lattice) a0 = stru[0] self.assertListAlmostEqual(a0.xyz, 3*[0.0]) a1 = stru[1] self.assertListAlmostEqual(a1.xyz, [2.0, 0.0, 2.0]) def test_forces(self): forces = [[0.0, 0.61, 0.7], [1.8, 0.9, 1.1]] self.stru.forces = forces self.assertListAlmostEqual(self.stru[0].force, forces[0]) def test_spaceGroupQuery(self): sg = self.stru2.sg assert sg.number is 225 assert sg.num_sym_equiv is 192 assert sg.num_primitive_sym_equiv is 48 assert sg.short_name=='Fm-3m' assert sg.alt_name=='F M 3 M' assert sg.point_group_name=='PGm3barm' assert sg.crystal_system=='CUBIC' assert sg.pdb_name=='F 4/m -3 2/m' assert_array_almost_equal(sg.symop_list[1].R, numpy.identity(3)) assert_array_almost_equal(sg.symop_list[1].t, numpy.array([ 0. , 0.5, 0.5])) # for symmop in sg.symop_list: # print symmop def test_symConsistent(self): result = self.stru2.symConsistent() assert result[0] is True self.stru3.sg = 225 result,badAtomPos,badSymOp = self.stru3.symConsistent() assert result is False #print badAtomPos,badSymOp def test_PrimCellFind(self): "" #print 'PbTe 225' #print self.stru2.primitive_unitcell #print 'graphite' #print self.stru3.primitive_unitcell #print def test_bravais_crystalsystem_centering(self): assert self.stru2.centering is 'F' assert self.stru2.centering_description=='face centered' assert self.stru2.crystal_system is 'CUBIC' assert self.stru2.bravais_type=='face centered cubic' def test_species(self): ""
def _setReducedStructureFromMatterStructure(self, structure, ibrav, massList=[], psList=[]): """ structure - matter.Structure object ibrav - Lattice index psList - list of strings with potential names matterStructure object will be modified with reduced atomic positions """ import copy matterLattice = copy.deepcopy(structure.lattice) # there is a big problem with what Nikolay is doing because he is initializing QELattice with abcalbega which # leaves room for errors in how the basis vectors are chosen compared to the old lattice a = matterLattice.a b = matterLattice.b c = matterLattice.c cAB = cosd(matterLattice.gamma) cBC = cosd(matterLattice.alpha) cAC = cosd(matterLattice.beta) qeLattice = QELattice(ibrav = ibrav, a = a, b = b, c = c, cBC = cBC, \ cAC = cAC, cAB = cAB) #what he should do is initialize the vectors of QELattice with the vectors of matterLattice # qeLattice._qeInput = self._qeInput self.lattice = qeLattice # make a deep copy: reducedStructure = Structure(atoms=structure) reducedStructure.placeInLattice(Lattice(base=qeLattice.matter().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 a0.symbol == a1.symbol 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 a.symbol not in atomNames: atomNames.append(a.symbol) 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: self.addNewAtom(atype = atom.symbol, xyz = atom.xyz, \ mass = atomicSpecies[atom.symbol][0], \ potential = atomicSpecies[atom.symbol][1],\ lattice = self.lattice, optConstraint = [])
def matter(self): stru = Structure(lattice=self.lattice.matter()) for atom in self: stru.addNewAtom(atype = atom.symbol, xyz = atom.xyz, \ lattice = self.lattice.matter() ) return stru
def c(*args): from matter.Structure import Structure return Structure(lattice=args[0], atoms=args[1])
class TestStructure(unittest.TestCase): """test methods of Structure class""" def setUp(self): # at1 = Atom('C', [0.333333333333333, 0.666666666666667, 0]) # at2 = Atom('C', [0.666666666666667, 0.333333333333333, 0]) at1 = Atom('C', [0, 0, 0]) at2 = Atom('C', [1, 1, 1]) self.stru = Structure([at1, at2], lattice=Lattice(1, 1, 1, 90, 90, 120)) self.places = 12 def assertListAlmostEqual(self, l1, l2, places=None): """wrapper for list comparison""" if places is None: places = self.places self.assertEqual(len(l1), len(l2)) for i in range(len(l1)): self.assertAlmostEqual(l1[i], l2[i], places) def testGetChemicalFormula(self): at1 = Atom('C', [0.333333333333333, 0.666666666666667, 0]) at2 = Atom('C', [0.666666666666667, 0.333333333333333, 0]) #at3 = Atom('H', [0, 0, 0]) stru = Structure([at1, at2], lattice=Lattice(3.8, 3.8, 5.6, 90, 90, 120)) print 'formula: ', stru.getChemicalFormula() return # FIXME move into TestAtom def test_cartesian(self): """check conversion to Cartesian coordinates""" from math import sqrt stru = self.stru s_rc0 = stru[0].xyz_cartn f_rc0 = 3 * [0.0] s_rc1 = stru[1].xyz_cartn f_rc1 = [sqrt(0.75), 0.5, 1.] self.assertListAlmostEqual(s_rc0, f_rc0) self.assertListAlmostEqual(s_rc1, f_rc1) # def test___init__(self): # """check Structure.__init__() # """ # return # # def test___str__(self): # """check Structure.__str__() # """ # return # # def test_addNewAtom(self): # """check Structure.addNewAtom() # """ # return # # def test_getLastAtom(self): # """check Structure.getLastAtom() # """ # return # def test_getAtom(self): # """check Structure.getAtom() # """ # a0 = self.stru[0] # a1 = self.stru[1] # # check execeptions for invalid arguments # self.assertRaises(ValueError, self.stru.getAtom, 300) # self.assertRaises(ValueError, self.stru.getAtom, -44) # self.assertRaises(ValueError, self.stru.getAtom, "Na") # # check returned values # self.failUnless(a0 is self.stru.getAtom(0)) # self.failUnless(a1 is self.stru.getAtom(1)) # self.failUnless(a0 is self.stru.getAtom("C1")) # self.failUnless(a1 is self.stru.getAtom("C2")) # # check if labels get properly updated # cdsefile = os.path.join(testdata_dir, 'CdSe_bulk.stru') # cdse = Structure(filename=cdsefile) # self.stru[1:1] = cdse # self.failUnless(a0 is self.stru.getAtom("C1")) # self.failUnless(a1 is self.stru.getAtom("C2")) # self.failUnless(self.stru[1] is self.stru.getAtom("Cd1")) # return def test_getLabels(self): """check Structure.getLabels() """ self.assertEqual(["C1", "C2"], self.stru.getLabels()) pbtefile = os.path.join(testdata_dir, 'PbTe.cif') self.stru.read(pbtefile, format='cif') labels = self.stru.getLabels() self.assertEqual("Pb2+1", labels[0]) self.assertEqual("Pb2+4", labels[3]) self.assertEqual("Te1", labels[4]) self.assertEqual("Te4", labels[-1]) return def test_distance(self): """check Structure.distance() """ from math import sqrt self.assertRaises(ValueError, self.stru.distance, 333, "C1") self.assertRaises(ValueError, self.stru.distance, "C", "C1") self.assertAlmostEqual(sqrt(2.0), self.stru.distance(0, 1), self.places) self.assertAlmostEqual(sqrt(2.0), self.stru.distance("C1", "C2"), self.places) self.assertEqual(0, self.stru.distance(0, "C1")) return # def test_angle(self): # """check Structure.angle() # """ # return def test_placeInLattice(self): """check Structure.placeInLattice() -- conversion of coordinates """ stru = self.stru new_lattice = Lattice(.5, .5, .5, 90, 90, 60) stru.placeInLattice(new_lattice) a0 = stru[0] self.assertListAlmostEqual(a0.xyz, 3 * [0.0]) a1 = stru[1] self.assertListAlmostEqual(a1.xyz, [2.0, 0.0, 2.0]) def test_forces(self): forces = [[0.0, 0.61, 0.7], [1.8, 0.9, 1.1]] self.stru.forces = forces self.assertListAlmostEqual(self.stru[0].force, forces[0]) def test_spaceGroupQuery(self): ciffile = os.path.join(testdata_dir, 'PbTe.cif') self.stru.read(ciffile) #print self.stru.spaceGroup.number #print self.stru.spaceGroup.short_name # op = self.stru.spa # temporarily disabledceGroup.symop_list[1] # print op sg = self.stru.sg assert sg.number is 225 print sg.num_sym_equiv print sg.num_primitive_sym_equiv print sg.short_name print sg.alt_name print sg.point_group_name print sg.crystal_system print sg.pdb_name for symop in sg.symop_list: print symop