def test_del_state(self): "Atom: delete state" Fe57 = Atom( 'Fe', mass=57 ) Fe57.velocity = (0,0,1) del Fe57.velocity self._assertRaises( AttributeError, 'Fe57.velocity', locals()) return
def test_del_state(self): "Atom: delete state" Fe57 = Atom('Fe', mass=57) Fe57.velocity = (0, 0, 1) del Fe57.velocity self._assertRaises(AttributeError, 'Fe57.velocity', locals()) return
def test_dsaworm(self): 'Structure: dsaw orm' Fe57 = Atom('Fe', mass=57) Al = Atom('Al') atoms = [Fe57, Al] lattice = Lattice(a=1, b=2, c=3, alpha=89, beta=91, gamma=92) struct = Structure(atoms=atoms, lattice=lattice) orm = self.orm structrecord = orm(struct) orm.save(struct) struct_loaded = orm.load(Structure, structrecord.id) props = ['sg', 'description'] for prop in props: self.assertEqual(getattr(struct_loaded, prop), getattr(struct, prop)) continue props = ['a', 'b', 'c', 'alpha', 'beta', 'gamma'] for prop in props: self.assertEqual(getattr(struct_loaded.lattice, prop), getattr(struct.lattice, prop)) continue self.assertEqual(len(struct_loaded), 2) for atom in struct_loaded: self.assertEqual(atom.__class__, Atom) return
def supercell(S, mno): """Perform supercell expansion for a structure. New lattice parameters are multiplied and fractional coordinates divided by corresponding multiplier. New atoms are grouped with their source in the original cell. S -- an instance of Structure from matter. mno -- sequence of 3 integers for cell multipliers along the a, b and c axes. Return a new expanded structure instance. Raise TypeError when S is not Structure instance. Raise ValueError for invalid mno argument. """ # check arguments if len(mno) != 3: emsg = "Argument mno must contain 3 numbers." raise ValueError, emsg elif min(mno) < 1: emsg = "Multipliers must be greater or equal 1" raise ValueError, emsg if not isinstance(S, Structure): emsg = "The first argument must be a Structure instance." raise TypeError, emsg # convert mno to a tuple of integers so it can be used as range limit. mno = (int(mno[0]), int(mno[1]), int(mno[2])) # create return instance newS = Structure(S) if mno == (1, 1, 1): return newS # back to business ijklist = [(i,j,k) for i in range(mno[0]) for j in range(mno[1]) for k in range(mno[2])] # numpy.floor returns float array mnofloats = numpy.array(mno, dtype=float) # build a list of new atoms newAtoms = [] for a in S: for ijk in ijklist: adup = Atom(a) adup.xyz = (a.xyz + ijk)/mnofloats newAtoms.append(adup) # newS can own references in newAtoms, no need to make copies newS.__setslice__(0, len(newS), newAtoms, copy=False) # take care of lattice parameters newS.lattice.setLatPar( a=mno[0]*S.lattice.a, b=mno[1]*S.lattice.b, c=mno[2]*S.lattice.c ) return newS
def test_setable_state(self): "Atom: setable state" Fe57 = Atom('Fe', mass=57) v = (0, 0, 1) print '- Set velocity to %s ... ' % (v, ), Fe57.velocity = v print ' velocity = %s ' % (Fe57.velocity, ) print "- velocity's documentation: %s " % Atom.velocity.doc return
def test_writeStr_xyz(self): """check string representation of normal xyz file""" stru = self.stru stru.description = "test of writeStr" stru.lattice = Lattice(1.0, 2.0, 3.0, 90.0, 90.0, 90.0) stru[:] = [Atom('H', [1., 1., 1.]), Atom('Cl', [3., 2., 1.])] s1 = stru.writeStr(self.format) s1 = re.sub('[ \t]+', ' ', s1) s0 = "2\n%s\nH 1 2 3\nCl 3 4 3\n" % stru.description self.assertEqual(s1, s0)
def test_write_xyz(self): """check writing of normal xyz file""" stru = self.stru stru.description = "test of writeStr" stru.lattice = Lattice(1.0, 2.0, 3.0, 90.0, 90.0, 90.0) stru[:] = [Atom('H', [1., 1., 1.]), Atom('Cl', [3., 2., 1.])] stru.write(self.tmpname, self.format) f_s = open(self.tmpname).read() f_s = re.sub('[ \t]+', ' ', f_s) s_s = "2\n%s\nH 1 2 3\nCl 3 4 3\n" % stru.description self.assertEqual(f_s, s_s)
def test_writeStr_xyz_Supercell(self): 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) from matter.expansion import supercell al_333 = supercell(self.stru4, (3, 3, 3)) s1 = al_333.writeStr(self.format)
def test_setable_state(self): "Atom: setable state" Fe57 = Atom( 'Fe', mass=57 ) v = (0,0,1) # print '- Set velocity to %s ... ' % (v,), Fe57.velocity = v # print ' velocity = %s ' % (Fe57.velocity,) self.assertEqual(Fe57.velocity, v) # print "- velocity's documentation: %s " % Atom.velocity.doc self.assert_(Atom.velocity.doc) return
def test1(self): 'makeXYZfileContent' from matter import Lattice, Atom, Structure Al = Atom('Al', (0, 0, 0)) Fe = Atom('Fe', (0.5, 0.5, 0.5)) atoms = [Al, Fe] lattice = Lattice(1, 1, 1, 90, 90, 90) s = Structure(atoms, lattice) content = struct_utils.makeXYZfileContent(s, latticeAsDescription=True) self.assertEqual(len(content), 4) self.assertEqual(int(content[0]), 2) self.assertEqual(len(content[1].split(' ')), 9) return
def test_xyz(self): "Atom: xyz cartesian attribute" from matter import Lattice C = Atom( 'C', [0.1, 0.2, 0.3], lattice = Lattice(2,2,2,90,90,90)) self.assertEqual(list(C.xyz), [0.1, 0.2, 0.3]) self.assertEqual(list(C.xyz_cartn), [0.2, 0.4, 0.6]) return
def test_xyz(self): "Atom: xyz cartesian attribute" from matter import Lattice C = Atom('C', [0.1, 0.2, 0.3], lattice=Lattice(2, 2, 2, 90, 90, 90)) print "fractional pos=%s" % C.xyz print "cartesian pos=%s" % C.xyz_cartn return
def makeUnitcell(): from matter import Atom, Structure, Lattice atoms = [Atom('Ni')] # positions = [(0,0,0)] cellvectors = [ (3.57,0,0), (0,3.57,0), (0,0,3.57) ] lattice = Lattice(base=cellvectors) return Structure(lattice=lattice, atoms=atoms)
def test_writeStr_rawxyz(self): """check writing of normal xyz file""" stru = self.stru stru.description = "test of writeStr" stru.lattice = Lattice(1.0, 2.0, 3.0, 90.0, 90.0, 90.0) # plain version stru[:] = [Atom('H', [1., 1., 1.])] s1 = stru.writeStr(self.format) s1 = re.sub('[ \t]+', ' ', s1) s0 = "H 1 2 3\n"
def makeMatter(): from matter import Structure, Atom, Lattice atoms = [Atom('Fe', xyz=(0, 0, 0)), Atom('Al', xyz=(0.5, 0.5, 0.5))] lattice = Lattice(base=((1., 0, 0), (0, 1., 0), (0, 0, 1.))) return Structure(atoms, lattice)
def test_str(self): "Atom: __str__" Fe57 = Atom( 'Fe', mass=57 ) Fe57.velocity = (0,0,1) # print Fe57 return
def test_load_matter(self): from matter import Structure, Atom, Lattice 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]) a = 2. * 5.663 / 1.889725989 # set a in angstrom 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(structure = struct, ibrav = 2, massList = massList, psList = psList) self.input.structure.load(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 """ #print str(self.input.structure) self.assertEqual(str(self.input.structure), answer1) 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]) a = 2. * 5.663 / 1.889725989 # set a in angstrom struct2 = 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)) self.input.structure.load(structure=struct2, 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 """ #print str(self.input.structure) self.assertEqual(str(self.input.structure), answer2)
# the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 # USA # read and view pickled structure object from matter import Structure, Lattice, Atom at1 = Atom('C', [0.333333333333333, 0.666666666666667, 0]) at2 = Atom('C', [0.666666666666667, 0.333333333333333, 0]) a = 2.4612 c = 6.7079 graphite = Structure([at1, at2], lattice=Lattice(a, a, c, 90, 90, 120)) print graphite import pickle output = open('graphite.pkl', 'wb') pickle.dump(graphite, output) output.close() pkl_file = open('graphite.pkl', 'rb') g2 = pickle.load(pkl_file) print g2
def test_str(self): "Atom: __str__" Fe57 = Atom('Fe', mass=57) Fe57.velocity = (0, 0, 1) print Fe57 return
def test_natural_element(self): "Atom: natural element ctor" Fe = Atom('Fe') print Fe return
def test_del_ctorarg(self): "Atom: delete ctor arg" Fe57 = Atom('Fe', mass=57) self._assertRaises(AttributeError, "del Fe57.Z", locals()) return
def testMatter(): pwInput = PWInput(config=testSCFString) struct = matter.Structure() struct.read('data/graphite.cif', format='cif') #struct.read('data/PbTe.cif', format='cif') #struct.read('data/Ni.stru', format='pdffit') #struct.read('data/CdSe-wurtzite.stru', format='pdffit') print struct print struct.lattice.base #struct = Structure(filename='data/Ni.stru') #struct = Structure(filename='data/graphite.cif') #struct = Structure(filename='data/PbTe.cif') #struct = Structure(filename='data/CdSe-wurtzite.stru') #struct = Structure(pbte) # does not work well in matter: #s = pbte.writeStr(format='cif') 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]) struct2 = Structure([ at1, at2, at3, at4, at5, at6, at7, at8, at9, at10, at11, at12, at13, at14, at15, at16 ], lattice=Lattice(2, 2, 2, 90, 90, 90)) #print struct massList = [50., 55.] psList = ['ps1', 'ps2'] #massList = [1, 2, 3, 4, 5, 6,1, 2, 3, 4, 5, 6] #psList = ['ps1', 'ps2', 'ps2', 'ps3', 'ps4','ps1', 'ps2', 'ps2', 'ps3', 'ps4'] # pwInput.structure.load(ibrav = 0, structure = struct, \ # massList = massList, psList = psList) #pwInput.structure.load(structure = struct ) #print pwInput.structure.atomLabels() pwInput.structure.load(structure = struct, ibrav = 2, \ massList = massList, psList = psList) # pwInput.structure.setStructureFromDiffpyStructure(struct, \ # massList = massList,\ # psList = psList) # pwInput.structure.setReducedStructureFromDiffpyStructure(struct, ibrav = 2, \ # massList = massList,\ # psList = psList) # this will update string s: #s = '' #s = pwInput.structure.toString(string = s) #pwInput.removeNamelist('system') #pwInput.removeCard('atomic_species') #pwInput.removeCard('atomic_positions') #pwInput.removeCard('cell_parameters') #pwInput.structure.parseInput() s = pwInput.structure.toString() #pwInput.structure.atomicPositionsType = 'alat' #s = pwInput.structure.toString() #pwInput.structure.save('scf.in') print s
def test_isotope_ctor(self): "Atom: isotope ctor" Fe57 = Atom('Fe', mass=57) print "- Z=%s" % Fe57.Z print "- mass=%s" % Fe57.mass return
def test_undefined_state(self): "Atom: undefined state" Fe57 = Atom('Fe', mass=57) self._assertRaises(AttributeError, "Fe57.velocity", locals()) return
def test_isotope_ctor(self): "Atom: isotope ctor" Fe57 = Atom( 'Fe', mass=57 ) self.assertEqual(Fe57.Z, 26) self.assertEqual(Fe57.mass, 57) return