def test___init__(self): """check Atom.__init__() """ a = Atom() self.assertEqual('', a.element) self.assertTrue((a.xyz == 0).all()) self.assertEqual('', a.label) self.assertEqual(1.0, a.occupancy) self.assertFalse(a.anisotropy) self.assertTrue((a.U == 0).all()) self.assertTrue(a.lattice is None) # check initialization with arguments a1 = Atom('C', xyz=(1, 2, 3), Uisoequiv=0.005) self.assertEqual('C', a1.element) self.assertTrue(numpy.array_equal([1, 2, 3], a1.xyz)) self.assertFalse(a1.anisotropy) self.assertEqual(0.005, a1.Uisoequiv) # initialize with anisotropic displacement parameters uani = numpy.identity(3, dtype=float) * numpy.array([1, 2, 3]) * .01 a2 = Atom('C', U=uani) self.assertTrue(numpy.array_equal(uani, a2.U)) self.assertTrue(a2.anisotropy) a3 = Atom('C', Uisoequiv=0.02, anisotropy=True) self.assertTrue(a3.anisotropy) self.assertEqual(a3.U[2, 2], 0.02) self.assertRaises(ValueError, Atom, 'C', Uisoequiv=0.02, U=uani) return
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 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 addNewAtom(self, *args, **kwargs): """Add new Atom instance to the end of this Structure. All arguments are forwarded to Atom constructor. No return value. """ kwargs['lattice'] = self.lattice a = Atom(*args, **kwargs) self.append(a, copy=False) return
def append(self, a, copy=True): """Append atom to a structure and update its lattice attribute. a -- instance of Atom copy -- flag for appending a copy of a. When False, append a and update a.lattice. No return value. """ adup = copy and Atom(a) or a adup.lattice = self.lattice list.append(self, adup) return
def __setitem__(self, idx, a, copy=True): """Set idx-th atom to a. idx -- index of atom in this Structure a -- instance of Atom copy -- flag for setting to a copy of a. When False, set to a and update a.lattice. No return value. """ adup = copy and Atom(a) or a adup.lattice = self.lattice list.__setitem__(self, idx, adup) return
def insert(self, idx, a, copy=True): """Insert atom a before position idx in this Structure. idx -- position in atom list a -- instance of Atom copy -- flag for inserting a copy of a. When False, append a and update a.lattice. No return value. """ adup = copy and Atom(a) or a adup.lattice = self.lattice list.insert(self, idx, adup) return
def extend(self, atoms, copy=True): """Extend Structure by appending copies from a list of atoms. atoms -- list of Atom instances copy -- flag for extending with copies of Atom instances. When False extend with atoms and update their lattice attributes. No return value. """ if copy: adups = [Atom(a) for a in atoms] else: adups = atoms for a in adups: a.lattice = self.lattice list.extend(self, adups) return
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 __setslice__(self, lo, hi, atoms, copy=True): """Set Structure slice from lo to hi-1 to the sequence of atoms. lo -- low index for the slice hi -- high index of the slice atoms -- sequence of Atom instances copy -- flag for using copies of Atom instances. When False, set to existing instances and update their lattice attributes. No return value. """ if copy: ownatoms = set(list.__getslice__(self, lo, hi)) adups = [(a in ownatoms and a or Atom(a)) for a in atoms] else: adups = atoms for a in adups: a.lattice = self.lattice list.__setslice__(self, lo, hi, adups) return
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)