예제 #1
0
class TestMuon(unittest.TestCase):
 
    def setUp(self):
        self._sample = Sample()
       
        self._sample._reset(cell=True,sym=True,magdefs=True,muon=True)
        
        co_lattice.seek(0)
        atoms, sym = read_cif(co_lattice,0) # selectd index 0
    
        if atoms:
            self._sample._reset(muon=True,sym=True)
            self._sample.cell = atoms
            self._sample.sym = sym
        else:
            raise RuntimeError
    
    def test_dftgrid(self):
        # get a 64x64x64 grid independently on the atoms position
        r = build_uniform_grid(self._sample,4,-1.)
        # check that positions are inequivalent
        self.assertEqual(len(self._sample.sym.equivalent_sites(r)[0]),64)

        # same as above but missing the origin already occupied by Co!
        r = build_uniform_grid(self._sample,4,0.000000001)
        # check that positions are inequivalent
        self.assertEqual(len(self._sample.sym.equivalent_sites(r)[0]),60)

        # empty set becouse all too close
        r = build_uniform_grid(self._sample,4,2.0)
        self.assertEqual(len(r),0)
예제 #2
0
파일: test_muesr.py 프로젝트: bonfus/muesr
    def test_symbolic_fourier_components(self):
        m = Sample()
        load_xsf(m, os.path.join(self._stdir,'crys.xsf'))
        
        
        
        rand_k = np.random.rand(3)
        rand_real_fc = np.random.rand(3).reshape((1,3))+0.j # make array complex
        
        mm = MM(1)
        mm.k=np.array(rand_k)
        mm.fc_set(rand_real_fc)
        m.mm = mm
        
        
        m.add_muon([0.1,0.1,0.1])

        r = locfield(m, 's',[10,10,10],40)[0]

        if have_sympy:
            smm = SMM(1,"x,y,z")
            smm.k=np.array(rand_k)
            smm.set_symFC("[[x,y,z]]")
            smm.set_params(rand_real_fc[0].real.tolist())
    
            m.mm = smm
    
            r2 = locfield(m, 's',[10,10,10],40)[0]
    
            self.assertAlmostEqual( r.D[0], r2.D[0], places=10, msg=None, delta=None)
            self.assertAlmostEqual( r.D[1], r2.D[1], places=10, msg=None, delta=None)
            self.assertAlmostEqual( r.D[2], r2.D[2], places=7, msg=None, delta=None)           
예제 #3
0
파일: test_muesr.py 프로젝트: stur86/muesr
    def test_compare_ass_and_rass(self):
        m = Sample()
        load_xsf(m, os.path.join(self._stdir, 'crys2.xsf'))
        mm = MM(9)
        mm.k = np.array(np.random.rand(3))
        mm.fc_set(
            (np.random.rand(27) + 1.j * np.random.rand(27)).reshape(9, 3))
        m.mm = mm
        m.add_muon(np.random.rand(3))
        m.add_muon(np.random.rand(3))
        r1 = locfield(m, 's', [10, 10, 10], 20)
        r2 = locfield(m, 'r', [10, 10, 10], 20, axis=[1, 1, 1], nangles=1)

        r1[0].ACont = 1.0
        r2[0].ACont = 1.0
        r1[1].ACont = 1.0
        r2[1].ACont = 1.0

        min_oom = min(self.oom(r1[0].T), self.oom(r2[0].T[0]))
        np.testing.assert_array_almost_equal(r1[0].T,
                                             r2[0].T[0],
                                             decimal=6 - min_oom)

        min_oom = min(self.oom(r1[1].T), self.oom(r2[1].T[0]))
        np.testing.assert_array_almost_equal(r1[1].T,
                                             r2[1].T[0],
                                             decimal=6 - min_oom)
예제 #4
0
파일: test_muesr.py 프로젝트: stur86/muesr
 def test_double_moment(self):
     m = Sample()
     load_xsf(m, os.path.join(self._stdir, 'crys.xsf'))
     mm = MM(1)
     mm.k = np.array([0., 0., 0.])
     mm.fc_set(np.array([[0. + 0j, 0., 1. + 0.j]]))
     m.mm = mm
     m.add_muon([0.1, 0.1, 0.1])
     r = locfield(m, 's', [1, 1, 1], 4)[0]
     m.mm.fc_set(np.array([[0. + 0j, 0. + 0.j, 2. + 0.j]]))
     r2 = locfield(m, 's', [1, 1, 1], 4)[0]
     # r should be: [0 tesla, 11.4226 milliteslas, 11.4226 milliteslas]
     #
     #    (magnetic_constant/4pi)⋅((1/(1angstrom⋅sqrt(2.5^2+2.5^2+2.5^2)))^3)⋅(3⋅(1 bohr_magneton)⋅cos(atan(sqrt(2)))⋅([1,1,1]/sqrt(3))−1bohr_magneton⋅[1,0,0])
     #
     #
     self.assertAlmostEqual(r.D[0],
                            0.5 * r2.D[0],
                            places=10,
                            msg=None,
                            delta=None)
     self.assertAlmostEqual(r.D[1],
                            0.5 * r2.D[1],
                            places=10,
                            msg=None,
                            delta=None)
     self.assertAlmostEqual(r.D[2],
                            0.5 * r2.D[2],
                            places=7,
                            msg=None,
                            delta=None)
예제 #5
0
class TestMuon(unittest.TestCase):
    def setUp(self):
        self._sample = Sample()

        self._sample._reset(cell=True, sym=True, magdefs=True, muon=True)

        co_lattice.seek(0)
        atoms, sym = read_cif(co_lattice, 0)  # selectd index 0

        if atoms:
            self._sample._reset(muon=True, sym=True)
            self._sample.cell = atoms
            self._sample.sym = sym
        else:
            raise RuntimeError

    def test_dftgrid(self):
        # get a 64x64x64 grid independently on the atoms position
        r = build_uniform_grid(self._sample, 4, -1.)
        # check that positions are inequivalent
        self.assertEqual(len(self._sample.sym.equivalent_sites(r)[0]), 64)

        # same as above but missing the origin already occupied by Co!
        r = build_uniform_grid(self._sample, 4, 0.000000001)
        # check that positions are inequivalent
        self.assertEqual(len(self._sample.sym.equivalent_sites(r)[0]), 60)

        # empty set becouse all too close
        r = build_uniform_grid(self._sample, 4, 2.0)
        self.assertEqual(len(r), 0)
예제 #6
0
파일: test_muesr.py 프로젝트: stur86/muesr
    def test_mcif(self):
        m = Sample()
        load_mcif(m, os.path.join(self._stdir, 'Cd2Os2O7.mcif'))
        m.add_muon([float(x) for x in '0.125 0.125 0.125'.split()])

        # distance is l(Os1-H) =  2.20122(0) Å
        r1 = locfield(m, 's', [1, 1, 1], 2.3, 4, 2.3)
        r1[0].ACont = 1.

        # total field is 0
        np.testing.assert_array_almost_equal(r1[0].T, np.zeros(3), decimal=7)

        # distance to nnn is l(Os1-H) =  5.53962(0) Å
        r1 = locfield(m, 's', [3, 3, 3], 5.54, 4, 2.3)
        r1[0].ACont = 1.

        # total field is 0
        np.testing.assert_array_almost_equal(r1[0].T, np.zeros(3), decimal=7)

        # now use a funny cell
        r1 = locfield(m, 's', [
            np.random.randint(3, 10),
            np.random.randint(3, 10),
            np.random.randint(3, 10)
        ], 5.54, 4, 2.3)
        r1[0].ACont = 1.

        # total field is 0
        np.testing.assert_array_almost_equal(r1[0].T, np.zeros(3), decimal=7)
예제 #7
0
파일: test_muesr.py 프로젝트: bonfus/muesr
 def test_mcif(self):
     m = Sample()
     load_mcif(m, os.path.join(self._stdir,'Cd2Os2O7.mcif'))
     m.add_muon([ float(x) for x in '0.125 0.125 0.125'.split() ])
     
     # distance is l(Os1-H) =  2.20122(0) Å
     r1 = locfield(m, 's',[1,1,1],2.3,4,2.3)
     r1[0].ACont = 1.
     
     # total field is 0
     np.testing.assert_array_almost_equal(r1[0].T,np.zeros(3),decimal=7)
     
     
     # distance to nnn is l(Os1-H) =  5.53962(0) Å
     r1 = locfield(m, 's',[3,3,3],5.54,4,2.3)
     r1[0].ACont = 1.
     
     # total field is 0
     np.testing.assert_array_almost_equal(r1[0].T,np.zeros(3),decimal=7)
     
     # now use a funny cell
     r1 = locfield(m, 's',[np.random.randint(3,10),np.random.randint(3,10),np.random.randint(3,10)],5.54,4,2.3)
     r1[0].ACont = 1.
     
     # total field is 0
     np.testing.assert_array_almost_equal(r1[0].T,np.zeros(3),decimal=7)
예제 #8
0
파일: test_muesr.py 프로젝트: bonfus/muesr
    def test_compare_rass_and_incass(self):
        m = Sample()
        load_xsf(m, os.path.join(self._stdir,'crys.xsf'))
        mm = MM(1)
        # define three orthogonal vectors
        rp1 = np.random.rand(3)   # used for k
        r2 = np.random.rand(3)
        rp2 = np.cross(rp1,r2)
        rp3 = np.cross(rp1,rp2)
        
        # normalize a and b vectors
        rp2 /= np.linalg.norm(rp2)
        rp3 /= np.linalg.norm(rp3)
        
        #chose a random value for stag moment
        stm = np.random.ranf()*10.
        rp2 *= stm
        rp3 *= stm
        
        mm.k=np.array(rp1)
        mm.fc_set((rp2+1.j*rp3).reshape(1,3))
        m.mm = mm
        m.add_muon(np.random.rand(3))
        m.add_muon(np.random.rand(3))
        r1 = locfield(m, 's',[50,50,50],50)
        r2 = locfield(m, 'r',[50,50,50],50,nangles=1200,axis=rp1)
        r3 = locfield(m, 'i',[50,50,50],50,nangles=1200)

        r1[0].ACont = 1.0
        r2[0].ACont = 1.0
        r3[0].ACont = 1.0
        r1[1].ACont = 1.0
        r2[1].ACont = 1.0
        r3[1].ACont = 1.0
        
        min_oom = min(self.oom(r1[0].T), self.oom(r2[0].T[0]))
        np.testing.assert_array_almost_equal(r1[0].T,r2[0].T[0],decimal=7-min_oom)
        min_oom = min(self.oom(r1[1].T), self.oom(r2[1].T[0]))
        np.testing.assert_array_almost_equal(r1[1].T,r2[1].T[0],decimal=7-min_oom)
        
        r2_norms = np.apply_along_axis(np.linalg.norm, 1, r2[0].D)
        r3_norms = np.apply_along_axis(np.linalg.norm, 1, r3[0].D)
        
        min_oom=min(self.oom(np.max(r2_norms)), self.oom(np.max(r3_norms)))
        np.testing.assert_array_almost_equal(np.max(r2_norms),np.max(r3_norms),decimal=4-min_oom)
        
        min_oom=min(self.oom(np.min(r2_norms)), self.oom(np.min(r3_norms)))
        np.testing.assert_array_almost_equal(np.min(r2_norms),np.min(r3_norms),decimal=4-min_oom)

        r2_norms = np.apply_along_axis(np.linalg.norm, 1, r2[1].T)
        r3_norms = np.apply_along_axis(np.linalg.norm, 1, r3[1].T)
        
        min_oom=min(self.oom(np.max(r2_norms)), self.oom(np.max(r3_norms)))
        np.testing.assert_array_almost_equal(np.max(r2_norms),np.max(r2_norms),decimal=5-min_oom)
        
        min_oom=min(self.oom(np.min(r2_norms)), self.oom(np.min(r3_norms)))
        np.testing.assert_array_almost_equal(np.min(r3_norms),np.min(r3_norms),decimal=5-min_oom)
예제 #9
0
파일: test_muesr.py 프로젝트: stur86/muesr
 def test_one_dipole_outside(self):
     m = Sample()
     load_xsf(m, os.path.join(self._stdir, 'crys.xsf'))
     mm = MM(1)
     mm.k = np.array([0., 0., 0.])
     mm.fc_set(np.array([[1. + 0j, 0. + 1.j, 0. + 1.j]]))
     m.mm = mm
     m.add_muon([0.5, 0.5, 0.5])
     # the diagonal is 8.6603
     r = locfield(m, 's', [1, 1, 1], 8.66 / 2.)[0]
     self.assertEqual(np.sum(np.abs(r.D)), 0.)
예제 #10
0
파일: test_muesr.py 프로젝트: bonfus/muesr
 def test_mcif2(self):
     m = Sample()
     load_mcif(m, os.path.join(self._stdir,'ScMnO3.mcif'))
     m.add_muon( [float(x) for x in '0.66666666 0.33333333 0.25'.split()])
     
     # No contact
     r1 = locfield(m, 's',[np.random.randint(10,14),np.random.randint(10,14),np.random.randint(10,14)],22,4,0.3)
     r1[0].ACont = 1.
     
     # total field is 0
     np.testing.assert_array_almost_equal(r1[0].T,np.zeros(3),decimal=7)
예제 #11
0
파일: test_muesr.py 프로젝트: bonfus/muesr
 def test_one_dipole_outside(self):
     m = Sample()
     load_xsf(m, os.path.join(self._stdir,'crys.xsf'))
     mm = MM(1)
     mm.k=np.array([0.,0.,0.])
     mm.fc_set(np.array([[1.+0j,0.+1.j,0.+1.j]]))
     m.mm = mm
     m.add_muon([0.5,0.5,0.5])
     # the diagonal is 8.6603
     r = locfield(m, 's',[1,1,1],8.66/2.)[0]
     self.assertEqual( np.sum(np.abs(r.D)), 0.)
예제 #12
0
파일: test_muesr.py 프로젝트: bonfus/muesr
    def test_rotation(self):
        m = Sample()
        load_xsf(m, os.path.join(self._stdir,'crys3.xsf'))
        mm = MM(1)
        mm.k=np.array([0.,0.,0.])
        mm.fc_set(np.array([[0.+0j,1.+0.j,0.+0.j]]))
        m.mm = mm
        m.add_muon([0.0,0.001,0.0])
        r = locfield(m, 'r',[1,1,1],1.2,nnn=0,nangles=300,axis=[1,0,0])[0]

        # r for angle = 0 should be: [0 T, 1.8548 T, 0 T]
        #
        #    (magnetic_constant/4pi)⋅((1/(1angstrom⋅sqrt(1^2)))^3)⋅(3⋅(1 bohr_magneton)⋅cos(0)⋅([0,1,0]/sqrt(1))−1bohr_magneton⋅[0,1,0])
        #
        #
        # r for angle = 90 should be: [0 T, 0 T, −0.927401 T]
        # 
        #    (magnetic_constant/4pi)⋅((1/(1angstrom⋅sqrt(1^2)))^3)⋅(3⋅(1 bohr_magneton)⋅cos(pi/2)⋅([0,1,0]/sqrt(1))−1bohr_magneton⋅[0,0,1])
        #
        # Opposite moments produce opposite fields. 150 is half of 300
        np.testing.assert_array_almost_equal(r.D[0],-r.D[150],decimal=7)
        np.testing.assert_array_almost_equal(r.D[0],np.array([0., 1.8548018,0.]),decimal=7)
        np.testing.assert_array_almost_equal(r.D[75],np.array([0., 0,-0.9274009]),decimal=6)
        
        rnorms = np.apply_along_axis(np.linalg.norm,1,r.T-r.L)
        self.assertAlmostEqual( np.min(rnorms), 0.9274009 ,places=6, msg=None, delta=None)
        self.assertAlmostEqual( np.max(rnorms), 1.8548018 ,places=6, msg=None, delta=None)

        r = locfield(m, 'r',[5,1,1],1.5,nnn=0,nangles=300,axis=[1,0,0])[0]
        np.testing.assert_array_almost_equal(r.D[0],-r.D[150],decimal=7)
        np.testing.assert_array_almost_equal(r.D[0],np.array([0., 2.18268753,0.]),decimal=7)
        np.testing.assert_array_almost_equal(r.D[75],np.array([0., 0,-1.58317237]),decimal=6)
        
        rnorms = np.apply_along_axis(np.linalg.norm,1,r.T-r.L)
        self.assertAlmostEqual( np.min(rnorms), 1.58317237 ,places=6, msg=None, delta=None)
        self.assertAlmostEqual( np.max(rnorms), 2.18268753 ,places=6, msg=None, delta=None)
        
        # Now test incomm
        mm = MM(1)
        mm.k=np.array([0.,0.,0.])
        mm.fc_set(np.array([[0.+0j,1.+0.j,0.+1.j]]))
        m.mm = mm
        i = locfield(m, 'i',[1,1,1],1.2,nnn=0,nangles=300)[0]
        
        inorms = np.apply_along_axis(np.linalg.norm,1,i.T-i.L)
        
        self.assertAlmostEqual( np.min(inorms), 0.9274009 ,places=6, msg=None, delta=None)
        self.assertAlmostEqual( np.max(inorms), 1.8548018 ,places=6, msg=None, delta=None)
        
        i = locfield(m, 'i',[5,1,1],1.5,nnn=0,nangles=300)[0]
        
        rnorms = np.apply_along_axis(np.linalg.norm,1,i.T-i.L)
        self.assertAlmostEqual( np.min(rnorms), 1.58317237 ,places=6, msg=None, delta=None)
        self.assertAlmostEqual( np.max(rnorms), 2.18268753 ,places=6, msg=None, delta=None)        
예제 #13
0
    def setUp(self):
        self._sample = Sample()

        self._sample._reset(cell=True, sym=True, magdefs=True, muon=True)

        co_lattice.seek(0)
        atoms, sym = read_cif(co_lattice, 0)  # selectd index 0

        if atoms:
            self._sample._reset(muon=True, sym=True)
            self._sample.cell = atoms
            self._sample.sym = sym
        else:
            raise RuntimeError
예제 #14
0
파일: test_muesr.py 프로젝트: stur86/muesr
    def test_mcif2(self):
        m = Sample()
        load_mcif(m, os.path.join(self._stdir, 'ScMnO3.mcif'))
        m.add_muon([float(x) for x in '0.66666666 0.33333333 0.25'.split()])

        # No contact
        r1 = locfield(m, 's', [
            np.random.randint(10, 14),
            np.random.randint(10, 14),
            np.random.randint(10, 14)
        ], 22, 4, 0.3)
        r1[0].ACont = 1.

        # total field is 0
        np.testing.assert_array_almost_equal(r1[0].T, np.zeros(3), decimal=7)
예제 #15
0
class TestMuon(unittest.TestCase):
 
    def setUp(self):
        
        self._sample = Sample()
        
        self._sample._reset(cell=True,sym=True,magdefs=True,muon=True)
        
        co_lattice.seek(0)
        atoms, sym = read_cif(co_lattice,0) # selectd index 0
        
        if atoms:
            self._sample._reset(muon=True,sym=True)
            self._sample.cell = atoms
            self._sample.sym = sym
        else:
            raise RuntimeError
    
    def test_symsearch(self):
        
        if have_spg:
            # damn slow test!
            origsym = deepcopy(self._sample.sym)
            symsearch(self._sample)
            
            new_rotations, new_translations = self._sample.sym.get_op()
            self.assertEqual(len(new_rotations),len(origsym.get_op()[0]))
            
            for r, t in origsym.get_symop():
                ni = np.argwhere(np.all(new_rotations==r, axis=(1,2)))

                if len(ni) >= 1:
                    found = False
                    for i in ni:
                        # for some reason there is a rounding error so
                        # mod is done on 0.9999999... instead of 1
                        if np.allclose(np.mod(t,1-1e-13),np.mod(new_translations[i[0]],1-1e-13)):
                            found = True

                    if found == False:
                        raise RuntimeError
                        
                else:
                    raise RuntimeError
            
            
        else:
            self.assertFalse(symsearch(self._sample))
예제 #16
0
파일: test_muesr.py 프로젝트: stur86/muesr
    def test_one_dipole_inside(self):
        m = Sample()
        load_xsf(m, os.path.join(self._stdir, 'crys.xsf'))
        mm = MM(1)
        mm.k = np.array([0., 0., 0.])
        mm.fc_set(np.array([[1. + 0j, 0. + 0.j, 0. + 0.j]]))
        m.mm = mm
        m.add_muon([0.5, 0.5, 0.5])
        r = locfield(m, 's', [1, 1, 1], 8.67 / 2.)[0]

        # BDip should be: [0 tesla, 11.4226 milliteslas, 11.4226 milliteslas]
        #
        #    (magnetic_constant/4pi)⋅((1/(1angstrom⋅sqrt(2.5^2+2.5^2+2.5^2)))^3)⋅(3⋅(1 bohr_magneton)⋅cos(atan(sqrt(2)))⋅([1,1,1]/sqrt(3))−1bohr_magneton⋅[1,0,0])
        #
        # BLor should be: [ 0.01138414 T, 0 , 0 ]
        #
        #    0.3333333333⋅magnetic_constant (1 bohr_magneton)/((4/3)pi(angstrom 8.67/2.)^3)
        #
        # BCont should be (Assuming ACont = 1 AA^-3 )
        #
        #	 (2/3)⋅magnetic_constant (1 bohr_magneton)⋅(1angstrom^−3) = 7.769376 T
        #

        self.assertAlmostEqual(r.D[0], 0., places=7, msg=None, delta=None)
        self.assertAlmostEqual(r.D[1], r.D[2], places=10, msg=None, delta=None)
        self.assertAlmostEqual(r.D[1],
                               11.4226e-3,
                               places=7,
                               msg=None,
                               delta=None)

        self.assertAlmostEqual(r.L[0],
                               0.01138414,
                               places=7,
                               msg=None,
                               delta=None)
        self.assertAlmostEqual(r.L[1], 0.0, places=7, msg=None, delta=None)
        self.assertAlmostEqual(r.L[2], 0.0, places=7, msg=None, delta=None)

        r.ACont = 1.  # Ang^-3

        self.assertAlmostEqual(r.C[0],
                               7.769376,
                               places=7,
                               msg=None,
                               delta=None)
        self.assertAlmostEqual(r.C[1], 0.0, places=7, msg=None, delta=None)
        self.assertAlmostEqual(r.C[2], 0.0, places=7, msg=None, delta=None)
예제 #17
0
파일: test_muesr.py 프로젝트: bonfus/muesr
 def test_cubic_sym1(self):
     m = Sample()
     load_xsf(m, os.path.join(self._stdir,'crys.xsf'))
     mm = MM(1)
     mm.k=np.array([0.,0.,0.])
     mm.fc_set(np.array([[1.+0j,0.+0.j,0.+0.j]]))
     m.mm = mm
     m.add_muon([0.5,0.5,0.5])
     r = locfield(m, 's',[100,100,100],240)[0]
     # Bdip should be: [0 tesla, 11.4226 milliteslas, 11.4226 milliteslas]
     #
     #    (magnetic_constant/4pi)⋅((1/(1angstrom⋅sqrt(2.5^2+2.5^2+2.5^2)))^3)⋅(3⋅(1 bohr_magneton)⋅cos(atan(sqrt(2)))⋅([1,1,1]/sqrt(3))−1bohr_magneton⋅[1,0,0])
     #
     #
     self.assertAlmostEqual( r.D[0], r.D[1],places=10, msg=None, delta=None)
     self.assertAlmostEqual( r.D[0], r.D[2], places=10, msg=None, delta=None)
     self.assertAlmostEqual( r.D[0], 0., places=7, msg=None, delta=None)    
예제 #18
0
파일: test_xsf.py 프로젝트: stur86/muesr
 def test_open_invalid_file(self):
     s = Sample()
     with self.assertRaises(OSError):
         load_xsf(s,'ciao')
 
     # check for python2
     with self.assertRaises(OSError):
         load_xsf(s,u'ciao')
예제 #19
0
파일: test_muesr.py 프로젝트: bonfus/muesr
    def test_mcif3(self):
        m = Sample()
        load_mcif(m, os.path.join(self._stdir,'LiFeSO4F.mcif'))
        muon_set_frac(m, '0.25 0.25 0.25')
        
        # symmetry null
        r1 = locfield(m, 's',[np.random.randint(10,14),np.random.randint(10,14),np.random.randint(10,14)],22,4,10.)
        r1[0].ACont = 1.
        
        # total field is 0
        np.testing.assert_array_almost_equal(r1[0].T,np.zeros(3),decimal=7)      
        
        muon_reset(m)
        m.add_muon([float(x) for x in '0.15990  0.17820  0.14580'.split()])  # position of O3

        # one atom, distance is 2.14251 AA
        #  dipolar field should be:
        #
        #       def bfield(r,m):
        #           return 0.9274009*(3*r*np.dot(r,m)/(np.linalg.norm(r)**5)-m/(np.linalg.norm(r)**3))
        #       bfield(np.array([1.07574,1.59328,0.945826]),np.array([-1.70376,3.14961,-1.22045])) 
        #
        r1 = locfield(m, 's',[np.random.randint(1,4),np.random.randint(1,4),np.random.randint(1,4)],2.2,1,2.14251+20.*np.random.ranf())
        r1[0].ACont = 1.

        np.testing.assert_array_almost_equal(r1[0].D,np.array([0.29531083,  -0.09756855, 0.23347458]),decimal=6) 
        
        # contact field is (2/3)⋅magnetic_constant ([-1.70376 bohr_magneton ,3.14961bohr_magneton,-1.22045bohr_magneton])⋅(1angstrom^−3) =
        #   [-13.2372 T, 24.4705 T, -9.48213 T]
        
        np.testing.assert_array_almost_equal(r1[0].C,np.array([-13.2372 , 24.4705 , -9.48213]),decimal=4) 
        
        
        r2 = locfield(m, 's',[np.random.randint(3,5),np.random.randint(3,5),np.random.randint(3,5)],4.2,3,5.+20.*np.random.ranf())
        r2[0].ACont = 1.14299 # effective interaction increased since more nnn involved in acont
                                # still only 14% more
        
        # three atoms, distances are 2.14251 AA, 4.191093 AA, 4.19413AA
        #
        #      bfield(np.array([1.07574,1.59328,0.945826]),np.array([-1.70376,3.14961,-1.22045])) + \
        #      bfield(np.array([2.14396,2.77751,-2.29775]),np.array([1.70376,-3.14961,1.22045])) + \
        #      bfield(np.array([-2.28034,-2.66189,-2.29775]),np.array([1.70376,-3.14961,1.22045]))        
        
        np.testing.assert_array_almost_equal(r2[0].D,np.array([ 0.20781014, -0.07504131,  0.23329355]),decimal=6)
예제 #20
0
파일: test_muesr.py 프로젝트: bonfus/muesr
    def test_compare_ass_and_rass(self):
        m = Sample()
        load_xsf(m, os.path.join(self._stdir,'crys2.xsf'))
        mm = MM(9)
        mm.k=np.array(np.random.rand(3))
        mm.fc_set((np.random.rand(27)+1.j*np.random.rand(27)).reshape(9,3))
        m.mm = mm
        m.add_muon(np.random.rand(3))
        m.add_muon(np.random.rand(3))
        r1 = locfield(m, 's',[10,10,10],20)
        r2 = locfield(m, 'r',[10,10,10],20,axis=[1,1,1],nangles=1)

        r1[0].ACont = 1.0
        r2[0].ACont = 1.0
        r1[1].ACont = 1.0
        r2[1].ACont = 1.0
        
        min_oom = min(self.oom(r1[0].T), self.oom(r2[0].T[0]))
        np.testing.assert_array_almost_equal(r1[0].T,r2[0].T[0],decimal=6-min_oom)
        
        min_oom = min(self.oom(r1[1].T), self.oom(r2[1].T[0]))
        np.testing.assert_array_almost_equal(r1[1].T,r2[1].T[0],decimal=6-min_oom)
예제 #21
0
 def setUp(self):
     self._sample = Sample()
    
     self._sample._reset(cell=True,sym=True,magdefs=True,muon=True)
     
     co_lattice.seek(0)
     atoms, sym = read_cif(co_lattice,0) # selectd index 0
 
     if atoms:
         self._sample._reset(muon=True,sym=True)
         self._sample.cell = atoms
         self._sample.sym = sym
     else:
         raise RuntimeError
예제 #22
0
파일: test_muesr.py 프로젝트: bonfus/muesr
 def test_one_dipole_inside(self):
     m = Sample()
     load_xsf(m, os.path.join(self._stdir,'crys.xsf'))
     mm = MM(1)
     mm.k=np.array([0.,0.,0.])
     mm.fc_set(np.array([[1.+0j,0.+0.j,0.+0.j]]))
     m.mm = mm
     m.add_muon([0.5,0.5,0.5])
     r = locfield(m,'s',[1,1,1],8.67/2.)[0]
     
     # BDip should be: [0 tesla, 11.4226 milliteslas, 11.4226 milliteslas]
     #
     #    (magnetic_constant/4pi)⋅((1/(1angstrom⋅sqrt(2.5^2+2.5^2+2.5^2)))^3)⋅(3⋅(1 bohr_magneton)⋅cos(atan(sqrt(2)))⋅([1,1,1]/sqrt(3))−1bohr_magneton⋅[1,0,0])
     #
     # BLor should be: [ 0.01138414 T, 0 , 0 ]
     #
     #    0.3333333333⋅magnetic_constant (1 bohr_magneton)/((4/3)pi(angstrom 8.67/2.)^3)
     #
     # BCont should be (Assuming ACont = 1 AA^-3 )
     #
     #	 (2/3)⋅magnetic_constant (1 bohr_magneton)⋅(1angstrom^−3) = 7.769376 T
     #
     
     self.assertAlmostEqual( r.D[0], 0.,places=7, msg=None, delta=None)
     self.assertAlmostEqual( r.D[1], r.D[2], places=10, msg=None, delta=None)
     self.assertAlmostEqual( r.D[1], 11.4226e-3, places=7, msg=None, delta=None)
     
     self.assertAlmostEqual( r.L[0], 0.01138414  ,places=7, msg=None, delta=None)
     self.assertAlmostEqual( r.L[1], 0.0  ,places=7, msg=None, delta=None)
     self.assertAlmostEqual( r.L[2], 0.0  ,places=7, msg=None, delta=None)
     
     r.ACont = 1. # Ang^-3
     
     self.assertAlmostEqual( r.C[0], 7.769376  ,places=7, msg=None, delta=None)
     self.assertAlmostEqual( r.C[1], 0.0  ,places=7, msg=None, delta=None)
     self.assertAlmostEqual( r.C[2], 0.0  ,places=7, msg=None, delta=None)
예제 #23
0
파일: test_muesr.py 프로젝트: stur86/muesr
    def test_magmodel_input(self):
        m = Sample()
        load_mcif(m, os.path.join(self._stdir, 'LiFeSO4F.mcif'))

        fcs_mcif = m.mm.fc
        #print(fcs_mcif)

        nmm = MM(m._cell.get_number_of_atoms(), m._cell.get_cell())

        fcs = np.zeros([m.cell.get_number_of_atoms(), 3], dtype=np.complex)
        fcs[1:3] = np.array([[0.166987, -0.248559, 0.188134],
                             [-0.166987, 0.248559, -0.188134]])
        fcs[16:18] = np.array([[0.166987, -0.248559, 0.188134],
                               [-0.166987, 0.248559, -0.188134]])
        fcs[18:20] = -np.array([[0.166987, -0.248559, 0.188134],
                                [0.166987, -0.248559, 0.188134]])
        fcs[20:22] = np.array([[0.166987, -0.248559, 0.188134],
                               [0.166987, -0.248559, 0.188134]])

        np.testing.assert_array_almost_equal(m.mm.fcLattBMA, fcs, decimal=4)

        m.mm.fc_set(fcs, 1)

        np.testing.assert_array_almost_equal(m.mm.fc, fcs_mcif, decimal=4)

        fcs = np.zeros([m.cell.get_number_of_atoms(), 3], dtype=np.complex)
        fcs[1:3] = np.array([[1.73, -2.73, 1.36], [-1.73, 2.73, -1.36]])
        fcs[16:18] = np.array([[1.73, -2.73, 1.36], [-1.73, 2.73, -1.36]])
        fcs[18:20] = -np.array([[1.73, -2.73, 1.36], [1.73, -2.73, 1.36]])
        fcs[20:22] = np.array([[1.73, -2.73, 1.36], [1.73, -2.73, 1.36]])

        np.testing.assert_array_almost_equal(m.mm.fcLattBM, fcs, decimal=4)

        m.mm.fc_set(fcs, 2)

        np.testing.assert_array_almost_equal(m.mm.fc, fcs_mcif, decimal=4)
예제 #24
0
파일: test_muesr.py 프로젝트: stur86/muesr
    def test_symbolic_fourier_components(self):
        m = Sample()
        load_xsf(m, os.path.join(self._stdir, 'crys.xsf'))

        rand_k = np.random.rand(3)
        rand_real_fc = np.random.rand(3).reshape(
            (1, 3)) + 0.j  # make array complex

        mm = MM(1)
        mm.k = np.array(rand_k)
        mm.fc_set(rand_real_fc)
        m.mm = mm

        m.add_muon([0.1, 0.1, 0.1])

        r = locfield(m, 's', [10, 10, 10], 40)[0]

        if have_sympy:
            smm = SMM(1, "x,y,z")
            smm.k = np.array(rand_k)
            smm.set_symFC("[[x,y,z]]")
            smm.set_params(rand_real_fc[0].real.tolist())

            m.mm = smm

            r2 = locfield(m, 's', [10, 10, 10], 40)[0]

            self.assertAlmostEqual(r.D[0],
                                   r2.D[0],
                                   places=10,
                                   msg=None,
                                   delta=None)
            self.assertAlmostEqual(r.D[1],
                                   r2.D[1],
                                   places=10,
                                   msg=None,
                                   delta=None)
            self.assertAlmostEqual(r.D[2],
                                   r2.D[2],
                                   places=7,
                                   msg=None,
                                   delta=None)
예제 #25
0
파일: test_muesr.py 프로젝트: stur86/muesr
    def test_compare_rass_and_incass(self):
        m = Sample()
        load_xsf(m, os.path.join(self._stdir, 'crys.xsf'))
        mm = MM(1)
        # define three orthogonal vectors
        rp1 = np.random.rand(3)  # used for k
        r2 = np.random.rand(3)
        rp2 = np.cross(rp1, r2)
        rp3 = np.cross(rp1, rp2)

        # normalize a and b vectors
        rp2 /= np.linalg.norm(rp2)
        rp3 /= np.linalg.norm(rp3)

        #chose a random value for stag moment
        stm = np.random.ranf() * 10.
        rp2 *= stm
        rp3 *= stm

        mm.k = np.array(rp1)
        mm.fc_set((rp2 + 1.j * rp3).reshape(1, 3))
        m.mm = mm
        m.add_muon(np.random.rand(3))
        m.add_muon(np.random.rand(3))
        r1 = locfield(m, 's', [50, 50, 50], 50)
        r2 = locfield(m, 'r', [50, 50, 50], 50, nangles=1200, axis=rp1)
        r3 = locfield(m, 'i', [50, 50, 50], 50, nangles=1200)

        r1[0].ACont = 1.0
        r2[0].ACont = 1.0
        r3[0].ACont = 1.0
        r1[1].ACont = 1.0
        r2[1].ACont = 1.0
        r3[1].ACont = 1.0

        min_oom = min(self.oom(r1[0].T), self.oom(r2[0].T[0]))
        np.testing.assert_array_almost_equal(r1[0].T,
                                             r2[0].T[0],
                                             decimal=7 - min_oom)
        min_oom = min(self.oom(r1[1].T), self.oom(r2[1].T[0]))
        np.testing.assert_array_almost_equal(r1[1].T,
                                             r2[1].T[0],
                                             decimal=7 - min_oom)

        r2_norms = np.apply_along_axis(np.linalg.norm, 1, r2[0].D)
        r3_norms = np.apply_along_axis(np.linalg.norm, 1, r3[0].D)

        min_oom = min(self.oom(np.max(r2_norms)), self.oom(np.max(r3_norms)))
        np.testing.assert_array_almost_equal(np.max(r2_norms),
                                             np.max(r3_norms),
                                             decimal=4 - min_oom)

        min_oom = min(self.oom(np.min(r2_norms)), self.oom(np.min(r3_norms)))
        np.testing.assert_array_almost_equal(np.min(r2_norms),
                                             np.min(r3_norms),
                                             decimal=4 - min_oom)

        r2_norms = np.apply_along_axis(np.linalg.norm, 1, r2[1].T)
        r3_norms = np.apply_along_axis(np.linalg.norm, 1, r3[1].T)

        min_oom = min(self.oom(np.max(r2_norms)), self.oom(np.max(r3_norms)))
        np.testing.assert_array_almost_equal(np.max(r2_norms),
                                             np.max(r2_norms),
                                             decimal=5 - min_oom)

        min_oom = min(self.oom(np.min(r2_norms)), self.oom(np.min(r3_norms)))
        np.testing.assert_array_almost_equal(np.min(r3_norms),
                                             np.min(r3_norms),
                                             decimal=5 - min_oom)
예제 #26
0
파일: test_muon.py 프로젝트: bonfus/muesr
class TestMuon(unittest.TestCase):
 
    def setUp(self):
        self._sample = Sample()
        
    def test_muon_set_frac(self):
        
        self._sample._reset(cell=True,sym=True,magdefs=True,muon=True)
        
        # check that missing cell raises error
        with self.assertRaises(CellError):
            muon_set_frac(self._sample, "0 0 0")
        
        # ugly way to load a lattice with its symetry
        co_lattice.seek(0)
        atoms, sym = read_cif(co_lattice,0) # selectd index 0
    
        if atoms:
            self._sample._reset(muon=True,sym=True)
            self._sample.cell = atoms
            self._sample.sym = sym
        else:
            raise RuntimeError
            
        muon_set_frac(self._sample, "0 0 0")
        np.testing.assert_array_almost_equal(np.array([0,0,0.]),self._sample.muons[0])

        muon_set_frac(self._sample, "0.125 0.125 0.125")
        np.testing.assert_array_almost_equal(np.array([0.125,0.125,0.125]),self._sample.muons[1])
        
        

        
    
    def test_muon_reset(self):
        # ugly way to load a lattice with its symetry
        co_lattice.seek(0)
        atoms, sym = read_cif(co_lattice,0) # selectd index 0
    
        if atoms:
            self._sample._reset(muon=True,sym=True)
            self._sample.cell = atoms
            self._sample.sym = sym
        else:
            raise RuntimeError
            
        muon_set_frac(self._sample, "0 0 0")
        np.testing.assert_array_almost_equal(np.array([0,0,0.]),self._sample.muons[0])

        muon_set_frac(self._sample, "0.125 0.125 0.125")
        np.testing.assert_array_almost_equal(np.array([0.125,0.125,0.125]),self._sample.muons[1])

    def test_find_equiv(self):
        # ugly way to load a lattice with its symetry
        co_lattice.seek(0)
        atoms, sym = read_cif(co_lattice,0) # selectd index 0
    
        if atoms:
            self._sample._reset(cell=True, muon=True, sym=True, magdefs=True)
            self._sample.cell = atoms
            self._sample.sym = sym
        else:
            raise RuntimeError        
        
        #tests throw error if no muon positions defined
        with self.assertRaises(MuonError):
            muon_find_equiv(self._sample)
        
        muon_set_frac(self._sample, "0 0 0")
        muon_find_equiv(self._sample)
        
        muon_positions = self._sample.muons
        
        self.assertEqual(len(muon_positions),atoms.get_number_of_atoms())
        for atm in atoms:
            self.assertTrue(np.any(np.all((muon_positions-atm[2])==0, axis=1)))
        
        self._sample._reset(muon=True)
        
        muon_set_frac(self._sample, "0.2 0.3 0.4")
        muon_find_equiv(self._sample)
        
        muon_positions = self._sample.muons
        #positions calculated with VESTA
        eqpositions = np.array([[ 0.200000  , 0.300000  , 0.400000],
                                [ 0.800000  , 0.700000  , 0.600000],
                                [ 0.800000  , 0.700000  , 0.400000],
                                [ 0.200000  , 0.300000  , 0.600000],
                                [ 0.800000  , 0.300000  , 0.600000],
                                [ 0.200000  , 0.700000  , 0.400000],
                                [ 0.200000  , 0.700000  , 0.600000],
                                [ 0.800000  , 0.300000  , 0.400000],
                                [ 0.400000  , 0.200000  , 0.300000],
                                [ 0.600000  , 0.800000  , 0.700000],
                                [ 0.400000  , 0.800000  , 0.700000],
                                [ 0.600000  , 0.200000  , 0.300000],
                                [ 0.600000  , 0.800000  , 0.300000],
                                [ 0.400000  , 0.200000  , 0.700000],
                                [ 0.600000  , 0.200000  , 0.700000],
                                [ 0.400000  , 0.800000  , 0.300000],
                                [ 0.300000  , 0.400000  , 0.200000],
                                [ 0.700000  , 0.600000  , 0.800000],
                                [ 0.700000  , 0.400000  , 0.800000],
                                [ 0.300000  , 0.600000  , 0.200000],
                                [ 0.300000  , 0.600000  , 0.800000],
                                [ 0.700000  , 0.400000  , 0.200000],
                                [ 0.700000  , 0.600000  , 0.200000],
                                [ 0.300000  , 0.400000  , 0.800000],
                                [ 0.300000  , 0.200000  , 0.600000],
                                [ 0.700000  , 0.800000  , 0.400000],
                                [ 0.700000  , 0.800000  , 0.600000],
                                [ 0.300000  , 0.200000  , 0.400000],
                                [ 0.300000  , 0.800000  , 0.400000],
                                [ 0.700000  , 0.200000  , 0.600000],
                                [ 0.700000  , 0.200000  , 0.400000],
                                [ 0.300000  , 0.800000  , 0.600000],
                                [ 0.200000  , 0.400000  , 0.700000],
                                [ 0.800000  , 0.600000  , 0.300000],
                                [ 0.800000  , 0.400000  , 0.300000],
                                [ 0.200000  , 0.600000  , 0.700000],
                                [ 0.800000  , 0.600000  , 0.700000],
                                [ 0.200000  , 0.400000  , 0.300000],
                                [ 0.200000  , 0.600000  , 0.300000],
                                [ 0.800000  , 0.400000  , 0.700000],
                                [ 0.400000  , 0.300000  , 0.800000],
                                [ 0.600000  , 0.700000  , 0.200000],
                                [ 0.400000  , 0.700000  , 0.200000],
                                [ 0.600000  , 0.300000  , 0.800000],
                                [ 0.600000  , 0.300000  , 0.200000],
                                [ 0.400000  , 0.700000  , 0.800000],
                                [ 0.600000  , 0.700000  , 0.800000],
                                [ 0.400000  , 0.300000  , 0.200000],
                                [ 0.200000  , 0.800000  , 0.900000],
                                [ 0.800000  , 0.200000  , 0.100000],
                                [ 0.800000  , 0.200000  , 0.900000],
                                [ 0.200000  , 0.800000  , 0.100000],
                                [ 0.800000  , 0.800000  , 0.100000],
                                [ 0.200000  , 0.200000  , 0.900000],
                                [ 0.200000  , 0.200000  , 0.100000],
                                [ 0.800000  , 0.800000  , 0.900000],
                                [ 0.300000  , 0.900000  , 0.700000],
                                [ 0.700000  , 0.100000  , 0.300000],
                                [ 0.700000  , 0.900000  , 0.300000],
                                [ 0.300000  , 0.100000  , 0.700000],
                                [ 0.300000  , 0.100000  , 0.300000],
                                [ 0.700000  , 0.900000  , 0.700000],
                                [ 0.700000  , 0.100000  , 0.700000],
                                [ 0.300000  , 0.900000  , 0.300000],
                                [ 0.300000  , 0.700000  , 0.100000],
                                [ 0.700000  , 0.300000  , 0.900000],
                                [ 0.700000  , 0.300000  , 0.100000],
                                [ 0.300000  , 0.700000  , 0.900000],
                                [ 0.300000  , 0.300000  , 0.900000],
                                [ 0.700000  , 0.700000  , 0.100000],
                                [ 0.700000  , 0.700000  , 0.900000],
                                [ 0.300000  , 0.300000  , 0.100000],
                                [ 0.200000  , 0.900000  , 0.200000],
                                [ 0.800000  , 0.100000  , 0.800000],
                                [ 0.800000  , 0.900000  , 0.800000],
                                [ 0.200000  , 0.100000  , 0.200000],
                                [ 0.800000  , 0.100000  , 0.200000],
                                [ 0.200000  , 0.900000  , 0.800000],
                                [ 0.200000  , 0.100000  , 0.800000],
                                [ 0.800000  , 0.900000  , 0.200000],
                                [ 0.900000  , 0.200000  , 0.800000],
                                [ 0.100000  , 0.800000  , 0.200000],
                                [ 0.900000  , 0.800000  , 0.200000],
                                [ 0.100000  , 0.200000  , 0.800000],
                                [ 0.100000  , 0.800000  , 0.800000],
                                [ 0.900000  , 0.200000  , 0.200000],
                                [ 0.100000  , 0.200000  , 0.200000],
                                [ 0.900000  , 0.800000  , 0.800000],
                                [ 0.900000  , 0.300000  , 0.300000],
                                [ 0.100000  , 0.700000  , 0.700000],
                                [ 0.900000  , 0.700000  , 0.700000],
                                [ 0.100000  , 0.300000  , 0.300000],
                                [ 0.100000  , 0.300000  , 0.700000],
                                [ 0.900000  , 0.700000  , 0.300000],
                                [ 0.100000  , 0.700000  , 0.300000],
                                [ 0.900000  , 0.300000  , 0.700000]])
        for p in eqpositions: 
            self.assertTrue(np.any(np.all(np.abs(muon_positions-p)<1e-10, axis=1)))
예제 #27
0
파일: sampleIO.py 프로젝트: bonfus/muesr
def load_sample(filename="", fileobj=None):

    """
    This function load a sample from a file in YAML format.
        
    :param str filename: the filename used to store data.
    :param file fileobj: an optional file object. If specified, this
                         supersede the filename input.
    :return: a sample object
    :rtype: :py:class:`~Sample` object or None
    :raises: ValueError, FileNotFoundError, IsADirectoryError
    
    """      
    
    # fail if YAML is not available
    if not have_yaml:
        warnings.warn("Warning, YAML python package not present!")
        return
    
    sample = Sample()
    
    data = {}
    if fileobj is None:
        if filename == "":
            raise ValueError("Specify filename or File object")
        with open(filename,'r') as f:
            data = load(f, Loader=Loader)
    else:
        data = load(fileobj, Loader=Loader)

    if not(type(data) is dict):
        raise ValueError('Invalid data file. (problems with YAML?)')

    
    if data is None:
        raise ValueError('Invalid/empty data file. (problems with YAML?)')
    
    if 'Name' in data.keys():
        sample.name = str(data['Name'])
    
    if 'Lattice' in data.keys():
        l = data['Lattice']
        
        spos = None
        cpos = None
        if 'ScaledPositions' in l.keys():
            spos = np.array(l['ScaledPositions'])
        elif 'CartesianPositions' in l.keys():
            cpos = np.array(l['CartesianPositions'])
        
        cell = None
        if 'Cell' in l.keys():
            cell = np.array(l['Cell'])
            
        symbols = None
        if 'Symbols' in l.keys():
            symbols = l['Symbols']
        
        if (cell is None) or (symbols is None):
            warnings.warn('Cell not loaded!', RuntimeWarning)
        else:
            if not spos is None:
                sample.cell = Atoms(symbols = symbols, scaled_positions = spos, cell=cell, pbc=True)
            elif not cpos is None:
                sample.cell = Atoms(symbols = symbols, positions = cpos, cell=cell, pbc=True)
            else:
                warnings.warn('Cell not loaded!', RuntimeWarning)

    else:
        warnings.warn('Cell not loaded!', RuntimeWarning)

            
    if 'Muon' in data.keys():
        m = data['Muon']
        if 'Positions' in m:
            for p in m['Positions']:
                sample.add_muon(p)
        else:
            warnings.warn('Muon positions not loaded!', RuntimeWarning)
    else:
        warnings.warn('Muon positions not loaded!', RuntimeWarning)
                
    if 'Symmetry' in data.keys():
        s = data['Symmetry']
        
        if ('Number' in s.keys()) and \
            ('Symbol' in s.keys()) and \
            ('Rotations' in s.keys()) and \
            ('Translations' in s.keys()):
            sample.sym = spacegroup_from_data(s['Number'],s['Symbol'],
                                            rotations=np.array(s['Rotations']),
                                            translations=np.array(s['Translations'])) 
        else:
            warnings.warn('Symmetry not loaded.', RuntimeWarning)
    else:
        warnings.warn('Symmetry not loaded!', RuntimeWarning)
            
    if 'MagneticOrders' in data.keys():
        m = data['MagneticOrders']
        
        if len(m) > 0:
        
            msize = int(m['Size'])
    
            for mo in m['Orders']:
                
                if 'lattice' in mo.keys():                
                    n = MM(msize, \
                            np.array(mo['lattice']))
                else:
                    n = MM(msize)
                
                sample.mm = n
                sample.mm.k = np.array(mo['k'])
                sample.mm.phi = np.array(mo['phi'])
                
                if 'desc' in mo.keys():
                    sample.mm.desc = str(mo['desc'])
                
                rfcs, ifcs = np.hsplit(np.array(mo['fc']),2)
                
            
                if mo['format'].lower() in ['bohr-cartesian', 'b-c']:
                    sample.mm.fcCart=(rfcs + 1.j*ifcs)
                    
                elif mo['format'].lower() in ['bohr/angstrom-lattic', 'b/a-l']:
                    sample.mm.fcLattBMA=(rfcs + 1.j*ifcs)
                    
                elif mo['format'].lower() in ['bohr-lattice','b-l']:
                    sample.mm.fcLattBM=(rfcs + 1.j*ifcs)
                    
                else:
                    raise ValueError('Invalid Fourier Components format specifier in YAML file.')
        else:
            warnings.warn('Magnetic definitions not loaded!', RuntimeWarning)
    else:
        warnings.warn('Magnetic definitions not loaded!', RuntimeWarning)
                
    return sample
예제 #28
0
 def test_store_and_load_empty_structure(self):
     l = Sample()
     myfile = StringIO()
     save_sample(l, "", myfile)
     myfile.seek(0)
     t = load_sample("", myfile)
예제 #29
0
파일: test_muon.py 프로젝트: stur86/muesr
class TestMuon(unittest.TestCase):
    def setUp(self):
        self._sample = Sample()

    def test_muon_set_frac(self):

        self._sample._reset(cell=True, sym=True, magdefs=True, muon=True)

        # check that missing cell raises error
        with self.assertRaises(CellError):
            muon_set_frac(self._sample, "0 0 0")

        # ugly way to load a lattice with its symetry
        co_lattice.seek(0)
        atoms, sym = read_cif(co_lattice, 0)  # selectd index 0

        if atoms:
            self._sample._reset(muon=True, sym=True)
            self._sample.cell = atoms
            self._sample.sym = sym
        else:
            raise RuntimeError

        muon_set_frac(self._sample, "0 0 0")
        np.testing.assert_array_almost_equal(np.array([0, 0, 0.]),
                                             self._sample.muons[0])

        muon_set_frac(self._sample, "0.125 0.125 0.125")
        np.testing.assert_array_almost_equal(np.array([0.125, 0.125, 0.125]),
                                             self._sample.muons[1])

    def test_muon_reset(self):
        # ugly way to load a lattice with its symetry
        co_lattice.seek(0)
        atoms, sym = read_cif(co_lattice, 0)  # selectd index 0

        if atoms:
            self._sample._reset(muon=True, sym=True)
            self._sample.cell = atoms
            self._sample.sym = sym
        else:
            raise RuntimeError

        muon_set_frac(self._sample, "0 0 0")
        np.testing.assert_array_almost_equal(np.array([0, 0, 0.]),
                                             self._sample.muons[0])

        muon_set_frac(self._sample, "0.125 0.125 0.125")
        np.testing.assert_array_almost_equal(np.array([0.125, 0.125, 0.125]),
                                             self._sample.muons[1])

    def test_find_equiv(self):
        # ugly way to load a lattice with its symetry
        co_lattice.seek(0)
        atoms, sym = read_cif(co_lattice, 0)  # selectd index 0

        if atoms:
            self._sample._reset(cell=True, muon=True, sym=True, magdefs=True)
            self._sample.cell = atoms
            self._sample.sym = sym
        else:
            raise RuntimeError

        #tests throw error if no muon positions defined
        with self.assertRaises(MuonError):
            muon_find_equiv(self._sample)

        muon_set_frac(self._sample, "0 0 0")
        muon_find_equiv(self._sample)

        muon_positions = self._sample.muons

        self.assertEqual(len(muon_positions), atoms.get_number_of_atoms())
        for atm in atoms:
            self.assertTrue(
                np.any(np.all((muon_positions - atm[2]) == 0, axis=1)))

        self._sample._reset(muon=True)

        muon_set_frac(self._sample, "0.2 0.3 0.4")
        muon_find_equiv(self._sample)

        muon_positions = self._sample.muons
        #positions calculated with VESTA
        eqpositions = np.array([[0.200000, 0.300000, 0.400000],
                                [0.800000, 0.700000, 0.600000],
                                [0.800000, 0.700000, 0.400000],
                                [0.200000, 0.300000, 0.600000],
                                [0.800000, 0.300000, 0.600000],
                                [0.200000, 0.700000, 0.400000],
                                [0.200000, 0.700000, 0.600000],
                                [0.800000, 0.300000, 0.400000],
                                [0.400000, 0.200000, 0.300000],
                                [0.600000, 0.800000, 0.700000],
                                [0.400000, 0.800000, 0.700000],
                                [0.600000, 0.200000, 0.300000],
                                [0.600000, 0.800000, 0.300000],
                                [0.400000, 0.200000, 0.700000],
                                [0.600000, 0.200000, 0.700000],
                                [0.400000, 0.800000, 0.300000],
                                [0.300000, 0.400000, 0.200000],
                                [0.700000, 0.600000, 0.800000],
                                [0.700000, 0.400000, 0.800000],
                                [0.300000, 0.600000, 0.200000],
                                [0.300000, 0.600000, 0.800000],
                                [0.700000, 0.400000, 0.200000],
                                [0.700000, 0.600000, 0.200000],
                                [0.300000, 0.400000, 0.800000],
                                [0.300000, 0.200000, 0.600000],
                                [0.700000, 0.800000, 0.400000],
                                [0.700000, 0.800000, 0.600000],
                                [0.300000, 0.200000, 0.400000],
                                [0.300000, 0.800000, 0.400000],
                                [0.700000, 0.200000, 0.600000],
                                [0.700000, 0.200000, 0.400000],
                                [0.300000, 0.800000, 0.600000],
                                [0.200000, 0.400000, 0.700000],
                                [0.800000, 0.600000, 0.300000],
                                [0.800000, 0.400000, 0.300000],
                                [0.200000, 0.600000, 0.700000],
                                [0.800000, 0.600000, 0.700000],
                                [0.200000, 0.400000, 0.300000],
                                [0.200000, 0.600000, 0.300000],
                                [0.800000, 0.400000, 0.700000],
                                [0.400000, 0.300000, 0.800000],
                                [0.600000, 0.700000, 0.200000],
                                [0.400000, 0.700000, 0.200000],
                                [0.600000, 0.300000, 0.800000],
                                [0.600000, 0.300000, 0.200000],
                                [0.400000, 0.700000, 0.800000],
                                [0.600000, 0.700000, 0.800000],
                                [0.400000, 0.300000, 0.200000],
                                [0.200000, 0.800000, 0.900000],
                                [0.800000, 0.200000, 0.100000],
                                [0.800000, 0.200000, 0.900000],
                                [0.200000, 0.800000, 0.100000],
                                [0.800000, 0.800000, 0.100000],
                                [0.200000, 0.200000, 0.900000],
                                [0.200000, 0.200000, 0.100000],
                                [0.800000, 0.800000, 0.900000],
                                [0.300000, 0.900000, 0.700000],
                                [0.700000, 0.100000, 0.300000],
                                [0.700000, 0.900000, 0.300000],
                                [0.300000, 0.100000, 0.700000],
                                [0.300000, 0.100000, 0.300000],
                                [0.700000, 0.900000, 0.700000],
                                [0.700000, 0.100000, 0.700000],
                                [0.300000, 0.900000, 0.300000],
                                [0.300000, 0.700000, 0.100000],
                                [0.700000, 0.300000, 0.900000],
                                [0.700000, 0.300000, 0.100000],
                                [0.300000, 0.700000, 0.900000],
                                [0.300000, 0.300000, 0.900000],
                                [0.700000, 0.700000, 0.100000],
                                [0.700000, 0.700000, 0.900000],
                                [0.300000, 0.300000, 0.100000],
                                [0.200000, 0.900000, 0.200000],
                                [0.800000, 0.100000, 0.800000],
                                [0.800000, 0.900000, 0.800000],
                                [0.200000, 0.100000, 0.200000],
                                [0.800000, 0.100000, 0.200000],
                                [0.200000, 0.900000, 0.800000],
                                [0.200000, 0.100000, 0.800000],
                                [0.800000, 0.900000, 0.200000],
                                [0.900000, 0.200000, 0.800000],
                                [0.100000, 0.800000, 0.200000],
                                [0.900000, 0.800000, 0.200000],
                                [0.100000, 0.200000, 0.800000],
                                [0.100000, 0.800000, 0.800000],
                                [0.900000, 0.200000, 0.200000],
                                [0.100000, 0.200000, 0.200000],
                                [0.900000, 0.800000, 0.800000],
                                [0.900000, 0.300000, 0.300000],
                                [0.100000, 0.700000, 0.700000],
                                [0.900000, 0.700000, 0.700000],
                                [0.100000, 0.300000, 0.300000],
                                [0.100000, 0.300000, 0.700000],
                                [0.900000, 0.700000, 0.300000],
                                [0.100000, 0.700000, 0.300000],
                                [0.900000, 0.300000, 0.700000]])
        for p in eqpositions:
            self.assertTrue(
                np.any(np.all(np.abs(muon_positions - p) < 1e-10, axis=1)))
예제 #30
0
파일: test_clfc.py 프로젝트: stur86/muesr
class TestCLFC(unittest.TestCase):
    def setUp(self):
        self.assertTrue(have_lfclib)
        self.sample = Sample()

    def _set_a_cell(self):
        self.sample.cell = Atoms(symbols=['Co'],
                                 scaled_positions=[[0, 0, 0]],
                                 cell=[[3., 0, 0], [0, 3., 0], [0, 0, 3.]])

    def test_find_largest_sphere(self):

        self.sample._reset(cell=True, muon=True, sym=True, magdefs=True)

        with self.assertRaises(CellError):
            find_largest_sphere(self.sample, [3, 3, 3])

        self._set_a_cell()

        with self.assertRaises(MuonError):
            find_largest_sphere(self.sample, [3, 3, 3])

        self.sample.add_muon([0.5, 0.5, 0.5])

        with self.assertRaises(TypeError):
            find_largest_sphere(self.sample, 1)

        with self.assertRaises(ValueError):
            find_largest_sphere(self.sample, [0, 1, 2])

        self.assertEqual(find_largest_sphere(self.sample, [1, 1, 1]), 1.5)
        self.sample.add_muon([1., 1., 1.], cartesian=True)
        self.assertEqual(find_largest_sphere(self.sample, [1, 1, 1]), 1.)

    def test_locfield(self):

        self.sample._reset(cell=True, muon=True, sym=True, magdefs=True)

        # sample, ctype, supercellsize, radius, nnn = 2, rcont = 10.0, nangles = None, axis = None):
        with self.assertRaises(TypeError):
            locfield(None, None, None, None)

        with self.assertRaises(TypeError):
            locfield(self.sample, None, None, None)

        with self.assertRaises(TypeError):
            locfield(self.sample, 's', None, None)

        with self.assertRaises(TypeError):
            locfield(self.sample, 's', [2, 2, 2], None)

        #with self.assertRaises(TypeError):
        #    locfield(self.sample, 's', [2,2,2], 3.)

        with self.assertRaises(TypeError):
            locfield(self.sample, 's', [2, 2, 2], 3., '2a')

        with self.assertRaises(TypeError):
            locfield(self.sample, 's', [2, 2, 2], 3., '2', '10b')

        with self.assertRaises(ValueError):
            locfield(self.sample, 'a', [2, 2, 2], 3.)

        with self.assertRaises(ValueError):
            locfield(self.sample, 'i', [2, 2, 2], 3.)

        with self.assertRaises(ValueError):
            locfield(self.sample, 's', [0, 2, 2], 3.)
예제 #31
0
파일: test_sample.py 프로젝트: bonfus/muesr
 def setUp(self):
     self._sample = Sample()
예제 #32
0
파일: test_clfc.py 프로젝트: stur86/muesr
 def setUp(self):
     self.assertTrue(have_lfclib)
     self.sample = Sample()
예제 #33
0
파일: test_sample.py 프로젝트: stur86/muesr
class TestSample(unittest.TestCase):
    def setUp(self):
        self._sample = Sample()

    def _set_a_cell(self):
        self._sample.cell = Atoms(symbols=['Co'],
                                  scaled_positions=[[0, 0, 0]],
                                  cell=[[3., 0, 0], [0, 3., 0], [0, 0, 3.]])

    def test_set_attribute(self):
        with self.assertRaises(TypeError):
            self._sample.blabla = 1

    def test_name_property(self):

        self.assertEqual(self._sample.name, "No name")

        self._sample.name = "test"

        self.assertEqual(self._sample.name, "test")

        #must be str or unicode
        with self.assertRaises(TypeError):
            self._sample.name = 1

        self.assertEqual(self._sample.name, "test")

        # test unicode
        self._sample.name = u"àèé"

    def test_muons_property(self):
        self._sample._reset(cell=True, muon=True, sym=True, magdefs=True)
        self._set_a_cell()

        with self.assertRaises(MuonError):
            self._sample.muons

    def test_add_muon_property(self):
        self._sample._reset(cell=True, muon=True, sym=True, magdefs=True)

        with self.assertRaises(CellError):
            self._sample.add_muon([0, 0, 0])

        self._set_a_cell()

        with self.assertRaises(TypeError):
            self._sample.add_muon('0 0 0')

        with self.assertRaises(ValueError):
            self._sample.add_muon([0, 0, 0, 0])
        with self.assertRaises(ValueError):
            self._sample.add_muon(np.array([0, 0, 0, 0]))

        self._sample.add_muon([0, 0, 0])
        np.testing.assert_array_equal(self._sample.muons[0], np.zeros(3))

        self._sample.add_muon([1, 1, 1])
        np.testing.assert_array_equal(self._sample.muons[1], np.ones(3))

        self._sample.add_muon([1, 1, 1], cartesian=True)
        np.testing.assert_array_equal(self._sample.muons[2], np.ones(3) / 3.)

        a = 4.0  # some lattice constant
        b = a / 2
        self._sample.cell = Atoms(symbols=['Au'],
                                  positions=[0, 0, 0],
                                  cell=[(0, b, b), (b, 0, b), (b, b, 0)],
                                  pbc=True)

        self._sample.add_muon([1., 1., 1.], cartesian=True)
        np.testing.assert_array_equal(self._sample.muons[3], np.ones(3) / 4.)

        self._sample.add_muon([.5, .5, .5], cartesian=False)
        self._sample.add_muon([2., 2., 2.], cartesian=True)
        np.testing.assert_array_equal(self._sample.muons[4],
                                      self._sample.muons[5])

    def test_mm_property(self):
        self._sample._reset(cell=True, magdefs=True, muon=True, sym=True)

        with self.assertRaises(MagDefError):
            self._sample.mm

        with self.assertRaises(CellError):
            self._sample.mm = MM(19)  # randomly large number

        self._sample._reset(magdefs=True)
        self._set_a_cell()
        with self.assertRaises(TypeError):
            self._sample.mm = 1

        self._sample._reset(magdefs=True, cell=True, muon=True, sym=True)
        self._sample.cell = Atoms(symbols=['C'],
                                  scaled_positions=[[0, 0, 0]],
                                  cell=[[3., 0, 0], [0, 3., 0], [0, 0, 3.]])

        with self.assertRaises(MagDefError):
            self._sample.mm = MM(198)  # randomly large number

        self._sample.mm = MM(1)

    def test_new_mm(self):
        self._sample._reset(magdefs=True, cell=True, muon=True, sym=True)
        with self.assertRaises(CellError):
            self._sample.new_mm()

        self._set_a_cell()

        self._sample.new_mm()
        self.assertTrue(isinstance(self._sample.mm, MM))
        self.assertEqual(len(self._sample.mm.fc), 1)

    def test_new_smm(self):
        if have_sympy:
            self._sample._reset(magdefs=True, cell=True, muon=True, sym=True)
            with self.assertRaises(CellError):
                self._sample.new_smm("x,y,z")

            # TODO TESTING
        else:
            pass

    def test_current_mm_idx_property(self):
        self._sample._reset(magdefs=True, cell=True, muon=True, sym=True)
        self._sample.cell = Atoms(symbols=['C'],
                                  scaled_positions=[[0, 0, 0]],
                                  cell=[[3., 0, 0], [0, 3., 0], [0, 0, 3.]])

        self._sample.new_mm()
        self._sample.mm.k = np.array([0, 0, 1.])
        self.assertEqual(self._sample.current_mm_idx, 0)
        self._sample.new_mm()
        self._sample.mm.k = np.array([0, 0, 2.])
        self.assertEqual(self._sample.current_mm_idx, 1)
        self._sample.new_mm()
        self._sample.mm.k = np.array([0, 0, 3.])
        self.assertEqual(self._sample.current_mm_idx, 2)

        self._sample.current_mm_idx = 0
        self.assertEqual(self._sample.current_mm_idx, 0)
        np.testing.assert_array_equal(self._sample.mm.k, np.array([0, 0, 1.]))

        with self.assertRaises(IndexError):
            self._sample.current_mm_idx = 3

        with self.assertRaises(IndexError):
            self._sample.current_mm_idx = -1

        self.assertEqual(self._sample.current_mm_idx, 0)

    def test_sym_property(self):

        self._sample._reset(cell=True, muon=True, sym=True, magdefs=True)

        with self.assertRaises(SymmetryError):
            self._sample.sym

        with self.assertRaises(TypeError):
            self._sample.sym = 1

        self._sample.sym = Spacegroup(113)

        self.assertEqual(self._sample.sym.no, 113)
        self.assertEqual(self._sample.sym.setting, 1)

    def test_cell_property(self):
        #needs better testing
        with self.assertRaises(CellError):
            self._sample.cell

        with self.assertRaises(TypeError):
            self._sample.cell = 1

        self._set_a_cell()
        current_cell = self._sample.cell
        self.assertEqual(current_cell.get_chemical_symbols(), ['Co'])
        current_cell.set_chemical_symbols(['Co'])
        self.assertEqual(current_cell.get_chemical_symbols(), ['Co'])

    def test_reset(self):

        # test cell reset
        with warnings.catch_warnings(record=True) as w:
            # Cause all warnings to always be triggered.
            warnings.simplefilter("always")
            self._sample._reset(cell=True)
            assert len(w) == 3
            for wi in w:
                assert issubclass(wi.category, RuntimeWarning)

        self.assertEqual(self._sample._cell, None)
        with self.assertRaises(CellError):
            self._sample.cell

        # test magdef reset
        self._sample._reset(magdefs=True)
        self.assertEqual(self._sample._magdefs, [])
        self.assertEqual(self._sample._selected_mm, -1)
        with self.assertRaises(MagDefError):
            self._sample.mm

        # test muon reset
        self._set_a_cell()
        self._sample.add_muon([0, 1., 2])
        self._sample._reset(muon=True)
        self.assertEqual(self._sample._muon, [])
        with self.assertRaises(MuonError):
            self._sample.muons

        self._sample._reset(cell=True, muon=True, sym=True, magdefs=True)

        #test symmetry reset
        self._sample.sym = Spacegroup(113)
        self._sample._reset(sym=True)
        self.assertEqual(self._sample._sym, None)
        with self.assertRaises(SymmetryError):
            self._sample.sym

    # TODO
    def test_check_sym(self):
        self._sample._reset(cell=True, muon=True, sym=True, magdefs=True)

        with self.assertRaises(SymmetryError):
            self._sample._check_sym()

        # vary bad from user side
        self._sample._sym = 1
        with self.assertRaises(SymmetryError):
            self._sample._check_sym()

        self._sample.sym = Spacegroup(113)
        self.assertTrue(self._sample._check_sym())

    def test_check_lattice(self):
        self._sample._reset(cell=True, muon=True, sym=True, magdefs=True)

        with self.assertRaises(CellError):
            self._sample._check_lattice()

        self._set_a_cell()
        self.assertTrue(self._sample._check_lattice())
        self._sample._cell = 1
        with self.assertRaises(CellError):
            self._sample._check_lattice()

    def test_check_magdefs(self):
        self._sample._reset(cell=True, muon=True, sym=True, magdefs=True)

        with self.assertRaises(MagDefError):
            self._sample._check_magdefs()

        self._set_a_cell()
        self._sample.new_mm()
        self.assertTrue(self._sample._check_magdefs())

        self._sample._magdefs = 1
        with self.assertRaises(MagDefError):
            self._sample._check_magdefs()

    def test_check_muon(self):
        self._sample._reset(cell=True, muon=True, sym=True, magdefs=True)

        with self.assertRaises(MuonError):
            self._sample._check_muon()

        self._set_a_cell()
        self._sample.add_muon(np.zeros(3))
        self.assertTrue(self._sample._check_muon())

        self._sample.add_muon(np.zeros(3))
        self._sample._muon[1] = 'a'
        with self.assertRaises(MuonError):
            self._sample._check_muon()
예제 #34
0
파일: test_clfc.py 프로젝트: bonfus/muesr
 def setUp(self):
     self.assertTrue(have_lfclib)
     self.sample = Sample()
예제 #35
0
파일: test_sample.py 프로젝트: bonfus/muesr
class TestSample(unittest.TestCase):
 
    def setUp(self):
        self._sample = Sample()
        
    def _set_a_cell(self):
        self._sample.cell = Atoms(symbols=['Co'],
                                  scaled_positions=[[0,0,0]],
                                  cell=[[3.,0,0],
                                        [0,3.,0],
                                        [0,0,3.]])
    def test_set_attribute(self):
        with self.assertRaises(TypeError):
            self._sample.blabla = 1
        
        
    def test_name_property(self):
        
        self.assertEqual(self._sample.name,"No name")
        
        self._sample.name = "test"
        
        self.assertEqual(self._sample.name,"test")
        
        #must be str or unicode
        with self.assertRaises(TypeError):
            self._sample.name = 1
            
        self.assertEqual(self._sample.name,"test")
        
        # test unicode
        self._sample.name = u"àèé"
        
    
    def test_muons_property(self):
        self._sample._reset(cell=True,muon=True,sym=True,magdefs=True)
        self._set_a_cell()
        
        with self.assertRaises(MuonError):
            self._sample.muons
            
        
        
        
    def test_add_muon_property(self):
        self._sample._reset(cell=True,muon=True,sym=True,magdefs=True)

        with self.assertRaises(CellError):
            self._sample.add_muon([0,0,0])
            
        self._set_a_cell()
        
        with self.assertRaises(TypeError):
            self._sample.add_muon('0 0 0')
            
        with self.assertRaises(ValueError):
            self._sample.add_muon([0,0,0,0])
        with self.assertRaises(ValueError):
            self._sample.add_muon(np.array([0,0,0,0]))
        
        self._sample.add_muon([0,0,0])
        np.testing.assert_array_equal(self._sample.muons[0], np.zeros(3))
        
        self._sample.add_muon([1,1,1])
        np.testing.assert_array_equal(self._sample.muons[1], np.ones(3))
        
        self._sample.add_muon([1,1,1], cartesian=True)
        np.testing.assert_array_equal(self._sample.muons[2], np.ones(3)/3.)
        
        a = 4.0  # some lattice constant
        b = a / 2
        self._sample.cell = Atoms(symbols=['Au'],
                                  positions=[0,0,0],
                                  cell=[(0, b, b), (b, 0, b), (b, b, 0)],
                                  pbc=True)
        
        self._sample.add_muon([1.,1.,1.], cartesian=True)
        np.testing.assert_array_equal(self._sample.muons[3], np.ones(3)/4.)
        
        self._sample.add_muon([.5,.5,.5], cartesian=False)
        self._sample.add_muon([2.,2.,2.], cartesian=True)
        np.testing.assert_array_equal(self._sample.muons[4], self._sample.muons[5])
        
        
    
    def test_mm_property(self):
        self._sample._reset(cell=True,magdefs=True,muon=True,sym=True)
        
        with self.assertRaises(MagDefError):
            self._sample.mm

        with self.assertRaises(CellError):
            self._sample.mm = MM(19) # randomly large number
                    
        
        self._sample._reset(magdefs=True)
        self._set_a_cell()
        with self.assertRaises(TypeError):
            self._sample.mm = 1
            
        self._sample._reset(magdefs=True, cell=True, muon=True, sym=True)
        self._sample.cell = Atoms(symbols=['C'],
                                  scaled_positions=[[0,0,0]],
                                  cell=[[3.,0,0],
                                        [0,3.,0],
                                        [0,0,3.]])
                                        
        with self.assertRaises(MagDefError):
            self._sample.mm = MM(198) # randomly large number        
            
        self._sample.mm = MM(1)
        

    def test_new_mm(self):
        self._sample._reset(magdefs=True, cell=True, muon=True, sym=True)
        with self.assertRaises(CellError):
            self._sample.new_mm()
        
        self._set_a_cell()
                                        
        self._sample.new_mm()            
        self.assertTrue(isinstance(self._sample.mm, MM))
        self.assertEqual(len(self._sample.mm.fc), 1)
        
        
    
    def test_new_smm(self):
        if have_sympy:
            self._sample._reset(magdefs=True, cell=True, muon=True, sym=True)
            with self.assertRaises(CellError):
                self._sample.new_smm("x,y,z")
            
            # TODO TESTING
        else:
            pass
            
    def test_current_mm_idx_property(self):
        self._sample._reset(magdefs=True, cell=True,muon=True, sym=True)
        self._sample.cell = Atoms(symbols=['C'],
                                  scaled_positions=[[0,0,0]],
                                  cell=[[3.,0,0],
                                        [0,3.,0],
                                        [0,0,3.]])        
        
        self._sample.new_mm()
        self._sample.mm.k=np.array([0,0,1.])
        self.assertEqual(self._sample.current_mm_idx,0)
        self._sample.new_mm()
        self._sample.mm.k=np.array([0,0,2.])
        self.assertEqual(self._sample.current_mm_idx,1)
        self._sample.new_mm()
        self._sample.mm.k=np.array([0,0,3.])
        self.assertEqual(self._sample.current_mm_idx,2)
        
        self._sample.current_mm_idx = 0
        self.assertEqual(self._sample.current_mm_idx,0)
        np.testing.assert_array_equal(self._sample.mm.k, np.array([0,0,1.]))
        
        with self.assertRaises(IndexError):
            self._sample.current_mm_idx = 3

        with self.assertRaises(IndexError):
            self._sample.current_mm_idx = -1

            
        self.assertEqual(self._sample.current_mm_idx,0)

        
    def test_sym_property(self):
        
        self._sample._reset(cell=True,muon=True,sym=True,magdefs=True)
        
        with self.assertRaises(SymmetryError):
            self._sample.sym
            
        with self.assertRaises(TypeError):
            self._sample.sym = 1
            
        self._sample.sym = Spacegroup(113)
        
        self.assertEqual(self._sample.sym.no,113)
        self.assertEqual(self._sample.sym.setting,1)
        

    def test_cell_property(self): 
        #needs better testing
        with self.assertRaises(CellError):
            self._sample.cell
            
        with self.assertRaises(TypeError):
            self._sample.cell = 1
            
        self._set_a_cell()
        current_cell = self._sample.cell
        self.assertEqual(current_cell.get_chemical_symbols(),['Co'])
        current_cell.set_chemical_symbols(['Co'])
        self.assertEqual(current_cell.get_chemical_symbols(),['Co'])

    def test_reset(self):
        
        # test cell reset
        with warnings.catch_warnings(record=True) as w:
            # Cause all warnings to always be triggered.
            warnings.simplefilter("always")
            self._sample._reset(cell=True)
            assert len(w) == 3
            for wi in w:
                assert issubclass(wi.category, RuntimeWarning)
                
        self.assertEqual(self._sample._cell, None)
        with self.assertRaises(CellError):
            self._sample.cell
        
        # test magdef reset    
        self._sample._reset(magdefs=True)
        self.assertEqual(self._sample._magdefs, [])
        self.assertEqual(self._sample._selected_mm, -1)
        with self.assertRaises(MagDefError):
            self._sample.mm
            
        # test muon reset
        self._set_a_cell()
        self._sample.add_muon([0,1.,2])
        self._sample._reset(muon=True)
        self.assertEqual(self._sample._muon, [])
        with self.assertRaises(MuonError):
            self._sample.muons
        
        self._sample._reset(cell=True,muon=True,sym=True,magdefs=True)
            
        #test symmetry reset
        self._sample.sym = Spacegroup(113)
        self._sample._reset(sym=True)
        self.assertEqual(self._sample._sym, None)
        with self.assertRaises(SymmetryError):
            self._sample.sym
            
                       

    # TODO
    def test_check_sym(self):
        self._sample._reset(cell=True,muon=True,sym=True,magdefs=True)
        
        with self.assertRaises(SymmetryError):
            self._sample._check_sym()
        
        # vary bad from user side
        self._sample._sym = 1
        with self.assertRaises(SymmetryError):
            self._sample._check_sym()
        
        self._sample.sym = Spacegroup(113)
        self.assertTrue(self._sample._check_sym())
        
    def test_check_lattice(self):
        self._sample._reset(cell=True,muon=True,sym=True,magdefs=True)
        
        with self.assertRaises(CellError):
            self._sample._check_lattice()
        
        self._set_a_cell()
        self.assertTrue(self._sample._check_lattice())
        self._sample._cell = 1
        with self.assertRaises(CellError):
            self._sample._check_lattice()
        
            
        
    def test_check_magdefs(self):
        self._sample._reset(cell=True,muon=True,sym=True,magdefs=True)
        
        with self.assertRaises(MagDefError):
            self._sample._check_magdefs()
            
        self._set_a_cell()
        self._sample.new_mm()
        self.assertTrue(self._sample._check_magdefs())
        
        self._sample._magdefs = 1
        with self.assertRaises(MagDefError):
            self._sample._check_magdefs()        
        
        
    def test_check_muon(self):
        self._sample._reset(cell=True,muon=True,sym=True,magdefs=True)
        
        with self.assertRaises(MuonError):
            self._sample._check_muon()

        self._set_a_cell()
        self._sample.add_muon(np.zeros(3))
        self.assertTrue(self._sample._check_muon())
        
        self._sample.add_muon(np.zeros(3))
        self._sample._muon[1] = 'a'
        with self.assertRaises(MuonError):
            self._sample._check_muon()        
예제 #36
0
파일: test_muesr.py 프로젝트: stur86/muesr
    def test_mcif3(self):
        m = Sample()
        load_mcif(m, os.path.join(self._stdir, 'LiFeSO4F.mcif'))
        muon_set_frac(m, '0.25 0.25 0.25')

        # symmetry null
        r1 = locfield(m, 's', [
            np.random.randint(10, 14),
            np.random.randint(10, 14),
            np.random.randint(10, 14)
        ], 22, 4, 10.)
        r1[0].ACont = 1.

        # total field is 0
        np.testing.assert_array_almost_equal(r1[0].T, np.zeros(3), decimal=7)

        muon_reset(m)
        m.add_muon([float(x) for x in '0.15990  0.17820  0.14580'.split()
                    ])  # position of O3

        # one atom, distance is 2.14251 AA
        #  dipolar field should be:
        #
        #       def bfield(r,m):
        #           return 0.9274009*(3*r*np.dot(r,m)/(np.linalg.norm(r)**5)-m/(np.linalg.norm(r)**3))
        #       bfield(np.array([1.07574,1.59328,0.945826]),np.array([-1.70376,3.14961,-1.22045]))
        #
        r1 = locfield(m, 's', [
            np.random.randint(1, 4),
            np.random.randint(1, 4),
            np.random.randint(1, 4)
        ], 2.2, 1, 2.14251 + 20. * np.random.ranf())
        r1[0].ACont = 1.

        np.testing.assert_array_almost_equal(
            r1[0].D,
            np.array([0.29531083, -0.09756855, 0.23347458]),
            decimal=6)

        # contact field is (2/3)⋅magnetic_constant ([-1.70376 bohr_magneton ,3.14961bohr_magneton,-1.22045bohr_magneton])⋅(1angstrom^−3) =
        #   [-13.2372 T, 24.4705 T, -9.48213 T]

        np.testing.assert_array_almost_equal(
            r1[0].C, np.array([-13.2372, 24.4705, -9.48213]), decimal=4)

        r2 = locfield(m, 's', [
            np.random.randint(3, 5),
            np.random.randint(3, 5),
            np.random.randint(3, 5)
        ], 4.2, 3, 5. + 20. * np.random.ranf())
        r2[0].ACont = 1.14299  # effective interaction increased since more nnn involved in acont
        # still only 14% more

        # three atoms, distances are 2.14251 AA, 4.191093 AA, 4.19413AA
        #
        #      bfield(np.array([1.07574,1.59328,0.945826]),np.array([-1.70376,3.14961,-1.22045])) + \
        #      bfield(np.array([2.14396,2.77751,-2.29775]),np.array([1.70376,-3.14961,1.22045])) + \
        #      bfield(np.array([-2.28034,-2.66189,-2.29775]),np.array([1.70376,-3.14961,1.22045]))

        np.testing.assert_array_almost_equal(
            r2[0].D,
            np.array([0.20781014, -0.07504131, 0.23329355]),
            decimal=6)
예제 #37
0
파일: test_muesr.py 프로젝트: stur86/muesr
    def test_rotation(self):
        m = Sample()
        load_xsf(m, os.path.join(self._stdir, 'crys3.xsf'))
        mm = MM(1)
        mm.k = np.array([0., 0., 0.])
        mm.fc_set(np.array([[0. + 0j, 1. + 0.j, 0. + 0.j]]))
        m.mm = mm
        m.add_muon([0.0, 0.001, 0.0])
        r = locfield(m,
                     'r', [1, 1, 1],
                     1.2,
                     nnn=0,
                     nangles=300,
                     axis=[1, 0, 0])[0]

        # r for angle = 0 should be: [0 T, 1.8548 T, 0 T]
        #
        #    (magnetic_constant/4pi)⋅((1/(1angstrom⋅sqrt(1^2)))^3)⋅(3⋅(1 bohr_magneton)⋅cos(0)⋅([0,1,0]/sqrt(1))−1bohr_magneton⋅[0,1,0])
        #
        #
        # r for angle = 90 should be: [0 T, 0 T, −0.927401 T]
        #
        #    (magnetic_constant/4pi)⋅((1/(1angstrom⋅sqrt(1^2)))^3)⋅(3⋅(1 bohr_magneton)⋅cos(pi/2)⋅([0,1,0]/sqrt(1))−1bohr_magneton⋅[0,0,1])
        #
        # Opposite moments produce opposite fields. 150 is half of 300
        np.testing.assert_array_almost_equal(r.D[0], -r.D[150], decimal=7)
        np.testing.assert_array_almost_equal(r.D[0],
                                             np.array([0., 1.8548018, 0.]),
                                             decimal=7)
        np.testing.assert_array_almost_equal(r.D[75],
                                             np.array([0., 0, -0.9274009]),
                                             decimal=6)

        rnorms = np.apply_along_axis(np.linalg.norm, 1, r.T - r.L)
        self.assertAlmostEqual(np.min(rnorms),
                               0.9274009,
                               places=6,
                               msg=None,
                               delta=None)
        self.assertAlmostEqual(np.max(rnorms),
                               1.8548018,
                               places=6,
                               msg=None,
                               delta=None)

        r = locfield(m,
                     'r', [5, 1, 1],
                     1.5,
                     nnn=0,
                     nangles=300,
                     axis=[1, 0, 0])[0]
        np.testing.assert_array_almost_equal(r.D[0], -r.D[150], decimal=7)
        np.testing.assert_array_almost_equal(r.D[0],
                                             np.array([0., 2.18268753, 0.]),
                                             decimal=7)
        np.testing.assert_array_almost_equal(r.D[75],
                                             np.array([0., 0, -1.58317237]),
                                             decimal=6)

        rnorms = np.apply_along_axis(np.linalg.norm, 1, r.T - r.L)
        self.assertAlmostEqual(np.min(rnorms),
                               1.58317237,
                               places=6,
                               msg=None,
                               delta=None)
        self.assertAlmostEqual(np.max(rnorms),
                               2.18268753,
                               places=6,
                               msg=None,
                               delta=None)

        # Now test incomm
        mm = MM(1)
        mm.k = np.array([0., 0., 0.])
        mm.fc_set(np.array([[0. + 0j, 1. + 0.j, 0. + 1.j]]))
        m.mm = mm
        i = locfield(m, 'i', [1, 1, 1], 1.2, nnn=0, nangles=300)[0]

        inorms = np.apply_along_axis(np.linalg.norm, 1, i.T - i.L)

        self.assertAlmostEqual(np.min(inorms),
                               0.9274009,
                               places=6,
                               msg=None,
                               delta=None)
        self.assertAlmostEqual(np.max(inorms),
                               1.8548018,
                               places=6,
                               msg=None,
                               delta=None)

        i = locfield(m, 'i', [5, 1, 1], 1.5, nnn=0, nangles=300)[0]

        rnorms = np.apply_along_axis(np.linalg.norm, 1, i.T - i.L)
        self.assertAlmostEqual(np.min(rnorms),
                               1.58317237,
                               places=6,
                               msg=None,
                               delta=None)
        self.assertAlmostEqual(np.max(rnorms),
                               2.18268753,
                               places=6,
                               msg=None,
                               delta=None)
예제 #38
0
파일: test_xsf.py 프로젝트: stur86/muesr
 def test_open_invalid_file(self):
     s = Sample()
     with self.assertRaises(FileNotFoundError):
         load_xsf(s,'ciao')
예제 #39
0
from muesr.i_o.cif.cif import load_cif                  # For loading the structure from cif files
from muesr.i_o.xsf.xsf import load_xsf                  # For loading the structure from xsf files
from muesr.i_o.xsf.xsf import show_supercell, show_cell # For visualisation with xcrysden (http://www.xcrysden.org/)
from muesr.utilities.ms import mago_add                # For magnetic structure description
from muesr.engines.clfc import locfield                # Does the sum and returns the local field in its diff. contributions
from muesr.engines.clfc import find_largest_sphere     # Aids in the calculation of the sphere's radius for the lattice sum.
from muesr.utilities.muon import muon_find_equiv       # For finding and including the symmetry equivalent muon positions in the calculation 

head,tail = os.path.split(os.path.realpath(__file__))
print("working from "+head)

#
np.set_printoptions(suppress=True,precision=5)

#    Declare  and load sample 
fe= Sample()                          
load_cif(fe, os.path.join(head,'Cifs','1534895.cif'))

#    To add the muon position
fe.add_muon([0.50,0.25,0.0])    


#   Finds and includes the symmetry equivalent positions of the above defined muon.
#   For this example there are 12 sites "print (fe.muons)" will show their positions.
muon_find_equiv(fe)  


#   Description of the propagation vector k and fourier components fc 
#   with a new magnetic structure declared with fe.new_mm()
fe.new_mm()     
fe.mm.k=np.array([0.0,0.0,0.0])
예제 #40
0
파일: test_xsf.py 프로젝트: stur86/muesr
 def test_save_file(self):
     s = Sample()
     with self.assertRaises(CellError):
         save_xsf(s,u'ciao')
예제 #41
0
def load_sample(filename="", fileobj=None):
    """
    This function load a sample from a file in YAML format.
        
    :param str filename: the filename used to store data.
    :param file fileobj: an optional file object. If specified, this
                         supersede the filename input.
    :return: a sample object
    :rtype: :py:class:`~Sample` object or None
    :raises: ValueError, FileNotFoundError, IsADirectoryError
    
    """

    # fail if YAML is not available
    if not have_yaml:
        warnings.warn("Warning, YAML python package not present!")
        return

    sample = Sample()

    data = {}
    if fileobj is None:
        if filename == "":
            raise ValueError("Specify filename or File object")
        with open(filename, 'r') as f:
            data = load(f, Loader=Loader)
    else:
        data = load(fileobj, Loader=Loader)

    if not (type(data) is dict):
        raise ValueError('Invalid data file. (problems with YAML?)')

    if data is None:
        raise ValueError('Invalid/empty data file. (problems with YAML?)')

    if 'Name' in data.keys():
        sample.name = str(data['Name'])

    if 'Lattice' in data.keys():
        l = data['Lattice']

        spos = None
        cpos = None
        if 'ScaledPositions' in l.keys():
            spos = np.array(l['ScaledPositions'])
        elif 'CartesianPositions' in l.keys():
            cpos = np.array(l['CartesianPositions'])

        cell = None
        if 'Cell' in l.keys():
            cell = np.array(l['Cell'])

        symbols = None
        if 'Symbols' in l.keys():
            symbols = l['Symbols']

        if (cell is None) or (symbols is None):
            warnings.warn('Cell not loaded!', RuntimeWarning)
        else:
            if not spos is None:
                sample.cell = Atoms(symbols=symbols,
                                    scaled_positions=spos,
                                    cell=cell,
                                    pbc=True)
            elif not cpos is None:
                sample.cell = Atoms(symbols=symbols,
                                    positions=cpos,
                                    cell=cell,
                                    pbc=True)
            else:
                warnings.warn('Cell not loaded!', RuntimeWarning)

    else:
        warnings.warn('Cell not loaded!', RuntimeWarning)

    if 'Muon' in data.keys():
        m = data['Muon']
        if 'Positions' in m:
            for p in m['Positions']:
                sample.add_muon(p)
        else:
            warnings.warn('Muon positions not loaded!', RuntimeWarning)
    else:
        warnings.warn('Muon positions not loaded!', RuntimeWarning)

    if 'Symmetry' in data.keys():
        s = data['Symmetry']

        if ('Number' in s.keys()) and \
            ('Symbol' in s.keys()) and \
            ('Rotations' in s.keys()) and \
            ('Translations' in s.keys()):
            sample.sym = spacegroup_from_data(
                s['Number'],
                s['Symbol'],
                rotations=np.array(s['Rotations']),
                translations=np.array(s['Translations']))
        else:
            warnings.warn('Symmetry not loaded.', RuntimeWarning)
    else:
        warnings.warn('Symmetry not loaded!', RuntimeWarning)

    if 'MagneticOrders' in data.keys():
        m = data['MagneticOrders']

        if len(m) > 0:

            msize = int(m['Size'])

            for mo in m['Orders']:

                if 'lattice' in mo.keys():
                    n = MM(msize, \
                            np.array(mo['lattice']))
                else:
                    n = MM(msize)

                sample.mm = n
                sample.mm.k = np.array(mo['k'])
                sample.mm.phi = np.array(mo['phi'])

                if 'desc' in mo.keys():
                    sample.mm.desc = str(mo['desc'])

                rfcs, ifcs = np.hsplit(np.array(mo['fc']), 2)

                if mo['format'].lower() in ['bohr-cartesian', 'b-c']:
                    sample.mm.fcCart = (rfcs + 1.j * ifcs)

                elif mo['format'].lower() in ['bohr/angstrom-lattic', 'b/a-l']:
                    sample.mm.fcLattBMA = (rfcs + 1.j * ifcs)

                elif mo['format'].lower() in ['bohr-lattice', 'b-l']:
                    sample.mm.fcLattBM = (rfcs + 1.j * ifcs)

                else:
                    raise ValueError(
                        'Invalid Fourier Components format specifier in YAML file.'
                    )
        else:
            warnings.warn('Magnetic definitions not loaded!', RuntimeWarning)
    else:
        warnings.warn('Magnetic definitions not loaded!', RuntimeWarning)

    return sample
예제 #42
0
Crystal structure: space group P213, No. 198; Mn-ion at the position 
(0.138,0.138,0.138), and Si-ion at the position (0.845,0.845,0.845); 
lattice constant 4.558 Å.

The magnetic structure of MnSi is characterized by spins forming a 
left-handed incommensurate helix with a propagation vector k≃0.036 Å^−1
in the [111] direction [5–7]. The static Mn moments ( ∼0.4μB for T→0 K)
point in a plane perpendicular to the propagation vector. 

"""
head,tail = os.path.split(os.path.realpath(__file__))
print("working from "+head)

print("Create sample...")
s = Sample()
print("done!")

print("Load CIF file...")

load_cif(s,os.path.join(head,'MnSi.cif'))
print("done!")

print("Calculate dipolar tensor for equivalent sites...\n")

# this is a general position along the 111, 
# just to identify the form of the dipolar tensor for the sites 
# along the 111
s.add_muon([0.45,0.45,0.45])

# we find the remainig eq muon sites
예제 #43
0
 def setUp(self):
     self._sample = Sample()
예제 #44
0
파일: test_clfc.py 프로젝트: bonfus/muesr
class TestCLFC(unittest.TestCase):
 
    def setUp(self):
        self.assertTrue(have_lfclib)
        self.sample = Sample()

    def _set_a_cell(self):
        self.sample.cell = Atoms(symbols=['Co'],
                                  scaled_positions=[[0,0,0]],
                                  cell=[[3.,0,0],
                                        [0,3.,0],
                                        [0,0,3.]])
        
    def test_find_largest_sphere(self):
        
        self.sample._reset(cell=True,muon=True,sym=True,magdefs=True)
        
        with self.assertRaises(CellError):
            find_largest_sphere(self.sample,[3,3,3])
        
        
        
        self._set_a_cell() 

        with self.assertRaises(MuonError):
            find_largest_sphere(self.sample,[3,3,3])
        
        self.sample.add_muon([0.5,0.5,0.5])
        
        
        with self.assertRaises(TypeError):
            find_largest_sphere(self.sample,1)

        with self.assertRaises(ValueError):
            find_largest_sphere(self.sample,[0,1,2])
            
        self.assertEqual(find_largest_sphere(self.sample,[1,1,1]),1.5)
        self.sample.add_muon([1.,1.,1.],cartesian=True)
        self.assertEqual(find_largest_sphere(self.sample,[1,1,1]),1.)
        
    def test_locfield(self):
        
        self.sample._reset(cell=True,muon=True,sym=True,magdefs=True)
      
        # sample, ctype, supercellsize, radius, nnn = 2, rcont = 10.0, nangles = None, axis = None):
        with self.assertRaises(TypeError):
            locfield(None, None, None, None)
        
        with self.assertRaises(TypeError):
            locfield(self.sample, None, None, None)

        with self.assertRaises(TypeError):
            locfield(self.sample, 's', None, None)
            
        with self.assertRaises(TypeError):
            locfield(self.sample, 's', [2,2,2], None)

        #with self.assertRaises(TypeError):
        #    locfield(self.sample, 's', [2,2,2], 3.)

        with self.assertRaises(TypeError):
            locfield(self.sample, 's', [2,2,2], 3., '2a')

        with self.assertRaises(TypeError):
            locfield(self.sample, 's', [2,2,2], 3., '2', '10b')

        with self.assertRaises(ValueError):
            locfield(self.sample, 'a', [2,2,2], 3.)
            
        with self.assertRaises(ValueError):
            locfield(self.sample, 'i', [2,2,2], 3.)

        with self.assertRaises(ValueError):
            locfield(self.sample, 's', [0,2,2], 3.)