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
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
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
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): # 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): ""
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