Ejemplo n.º 1
0
    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)           
Ejemplo n.º 2
0
    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)        
Ejemplo n.º 3
0
    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)
Ejemplo n.º 4
0
 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)
Ejemplo n.º 5
0
    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)
Ejemplo n.º 6
0
    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)
Ejemplo n.º 7
0
 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.)
Ejemplo n.º 8
0
 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.)
Ejemplo n.º 9
0
    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)
Ejemplo n.º 10
0
 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)    
Ejemplo n.º 11
0
    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)
Ejemplo n.º 12
0
 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)
Ejemplo n.º 13
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
Ejemplo n.º 14
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
Ejemplo n.º 15
0
    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)
Ejemplo n.º 16
0
    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)