class TestProjectorShell(mytest.MyTestCase):
    """
    Class:

    ProjectorShell(sh_pars, proj_raw)

    Scenarios:
    - **if** a correct input is given **compare** output files
    - **if** a correct input is given **compare** density matrices
    """
    def setUp(self):
        """
        """
        conf_file = _rpath + 'example.cfg'
        self.pars = ConfigParameters(conf_file)
        self.pars.parse_input()
        vasp_data = VaspData(_rpath + 'one_site/')
        self.el_struct = ElectronicStructure(vasp_data)

#        efermi = vasp_data.doscar.efermi
#        eigvals = vasp_data.eigenval.eigs - efermi
        efermi = self.el_struct.efermi
        eigvals = self.el_struct.eigvals - efermi
        emin, emax = self.pars.groups[0]['ewindow']
        struct = self.el_struct.structure
        kmesh = self.el_struct.kmesh

        self.proj_sh = ProjectorShell(self.pars.shells[0], vasp_data.plocar.plo, vasp_data.plocar.proj_params, kmesh, struct, 0)
        self.proj_gr = ProjectorGroup(self.pars.groups[0], [self.proj_sh], eigvals)

# Scenario 1
    def test_example(self):
        testout = _rpath + 'projshells.out.test'
        nion, ns, nk, nlm, nbtot = self.proj_sh.proj_win.shape
        with open(testout, 'wt') as f:
            f.write("pars: %s\n"%(self.pars.shells[0]))
            for ion in xrange(nion):
                for isp in xrange(ns):
                    for ik in xrange(nk):
                        ib1 = self.proj_sh.ib_win[ik, 0, 0]
                        ib2 = self.proj_sh.ib_win[ik, 0, 1]
                        f.write("%i  %i\n"%(ib1, ib2))
                        for ib in xrange(ib2 - ib1 + 1):
                            for ilm in xrange(nlm):
                                p = self.proj_sh.proj_win[ion, isp, ik, ilm, ib]
                                f.write("%5i  %f  %f\n"%(ilm+1, p.real, p.imag))

        expected_file = _rpath + 'projshells.out'
        self.assertFileEqual(testout, expected_file)

# Scenario 2
    def test_dens_mat(self):
        dens_mat, overl = self.proj_sh.density_matrix(self.el_struct)
        testout = _rpath + 'densmat.out.test'
        with open(testout, 'wt') as f:
            f.write("density matrix: %s\n"%(dens_mat))
            f.write("overlap matrix: %s\n"%(overl))

        expected_file = _rpath + 'densmat.out'
        self.assertFileEqual(testout, expected_file)
示例#2
0
    def test_example_no_groups(self):
        conf_pars = ConfigParameters(_rpath + 'example_nogroup.cfg')
        conf_pars.parse_input()
        #        with open('parse_input.output.test', 'wt') as f:
        #            f.write("Shells:\n")
        #            f.write(conf_pars.shells.__repr__() + '\n\n')
        #            f.write("Groups:\n")
        #            f.write(conf_pars.groups.__repr__() + '\n')
        res = "Shells:\n"
        res += conf_pars.shells.__repr__() + '\n\n'
        res += "Groups:\n"
        res += conf_pars.groups.__repr__()

        expected = r"""Shells:
[{'ion_list': array([4, 5, 6, 7]), 'user_index': 1, 'lshell': 2}]

Groups:
[{'normalize': True, 'index': '1', 'ewindow': (-7.6, 3.0), 'shells': [0], 'normion': True}]"""

        self.assertEqual(res, expected)
示例#3
0
    def test_example(self):
        conf_pars = ConfigParameters(_rpath + 'example.cfg')
        conf_pars.parse_input()
        #        with open('parse_input.output.test', 'wt') as f:
        #            f.write("Shells:\n")
        #            f.write(conf_pars.shells.__repr__() + '\n\n')
        #            f.write("Groups:\n")
        #            f.write(conf_pars.groups.__repr__() + '\n')
        res = "Shells:\n"
        res += conf_pars.shells.__repr__() + '\n\n'
        res += "Groups:\n"
        res += conf_pars.groups.__repr__()

        expected = r"""Shells:
[{'ion_list': array([4, 5, 6, 7]), 'user_index': 1, 'lshell': 2}, {'tmatrix': array([[ 0.,  1.,  0.],
       [ 1.,  0.,  0.],
       [ 0.,  0.,  1.]]), 'ion_list': array([0, 1, 2, 3]), 'user_index': 2, 'lshell': 1}, {'ion_list': array([0, 1, 2, 3]), 'user_index': 3, 'lshell': 3}]

Groups:
[{'normalize': True, 'index': 1, 'ewindow': (-7.6, 3.0), 'normion': True, 'shells': [0, 1]}, {'normalize': True, 'index': 2, 'ewindow': (-1.6, 2.0), 'normion': True, 'shells': [2]}]"""

        self.assertEqual(res, expected)
示例#4
0
 def test_shell_outside_groups(self):
     conf_pars = ConfigParameters(_rpath + 'input_test_4.cfg')
     err_mess = "Some shells are not inside"
     with self.assertRaisesRegexp(AssertionError, err_mess):
         conf_pars.parse_input()
示例#5
0
 def test_no_shell(self):
     conf_pars = ConfigParameters(_rpath + 'input_test_3.cfg')
     err_mess = "Shell 3 referenced in"
     with self.assertRaisesRegexp(Exception, err_mess):
         conf_pars.parse_input()
示例#6
0
 def test_gr_required(self):
     conf_pars = ConfigParameters(_rpath + 'input_test_2.cfg')
     err_mess = "One \[Shell\] section is"
     with self.assertRaisesRegexp(KeyError, err_mess):
         conf_pars.parse_input()
示例#7
0
 def test_no_group(self):
     conf_pars = ConfigParameters(_rpath + 'input_test_1.cfg')
     err_mess = "At least one group"
     with self.assertRaisesRegexp(AssertionError, err_mess):
         conf_pars.parse_input()
示例#8
0
class TestSelectBands(mytest.MyTestCase):
    """
    Function:

    def ProjectorGroup.select_bands(eigvals)

    Scenarios:
    - compare output for a correct input
    - **if** emin > max(eigvals) **raise** Exception
    - **if** emax > min(eigvals) **raise** Exception
    """
    def setUp(self):
        conf_file = _rpath + 'simple.cfg'
        self.pars = ConfigParameters(conf_file)
        self.pars.parse_input()
        vasp_data = VaspData(_rpath + 'simple/')
        self.el_struct = ElectronicStructure(vasp_data)

        efermi = self.el_struct.efermi
        self.eigvals = self.el_struct.eigvals - efermi
        struct = self.el_struct.structure
        kmesh = self.el_struct.kmesh

        self.proj_sh = ProjectorShell(self.pars.shells[0],
                                      vasp_data.plocar.plo,
                                      vasp_data.plocar.proj_params, kmesh,
                                      struct, 0)
        self.proj_gr = ProjectorGroup(self.pars.groups[0], [self.proj_sh],
                                      self.eigvals)

# Scenario 1

    def test_correct(self):
        ib_win, nb_min, nb_max = self.proj_gr.select_bands(self.eigvals)

        nb_min_exp = 3
        nb_max_exp = 8
        ib_win_exp = np.array([[[3, 8]], [[3, 7]], [[3, 7]], [[3, 7]], [[3,
                                                                         7]],
                               [[3, 7]], [[3, 7]], [[3, 4]]])

        self.assertEqual(nb_min, nb_min_exp)
        self.assertEqual(nb_max, nb_max_exp)
        self.assertEqual(ib_win, ib_win_exp)

# Scenario 2

    def test_emin_too_large(self):
        self.proj_gr.emin = 20.0
        self.proj_gr.emax = 25.0
        with self.assertRaisesRegexp(Exception, "No bands inside the window"):
            ib_win, nb_min, nb_max = self.proj_gr.select_bands(self.eigvals)

# Scenario 3

    def test_emax_too_small(self):
        self.proj_gr.emin = -50.0
        self.proj_gr.emax = -55.0
        with self.assertRaisesRegexp(Exception,
                                     "Energy window does not overlap"):
            ib_win, nb_min, nb_max = self.proj_gr.select_bands(self.eigvals)
class TestBlockMap(mytest.MyTestCase):
    """
    Function:

    def ProjectorGroup.get_block_matrix_map()

    Scenarios:
    - **test** block matrix for NORMION = False
    - **test** block matrix for NORMION = True
    """
    def setUp(self):
# Mock data
        self.mock_eigvals = np.zeros((1, 11, 1))

        nproj = 16
        self.mock_plo = np.zeros((nproj, 1, 1, 11), dtype=np.complex128)
        self.mock_proj_params = [{} for i in xrange(nproj)]
        ip = 0
# Mock d-sites
        for isite in xrange(2):
            for im in xrange(5):
                self.mock_proj_params[ip]['label'] = 'd-orb'
                self.mock_proj_params[ip]['isite'] = isite + 1
                self.mock_proj_params[ip]['l'] = 2
                self.mock_proj_params[ip]['m'] = im
                ip += 1
# Mock p-sites
        for isite in xrange(2, 4):
            for im in xrange(3):
                self.mock_proj_params[ip]['label'] = 'p-orb'
                self.mock_proj_params[ip]['isite'] = isite + 1
                self.mock_proj_params[ip]['l'] = 1
                self.mock_proj_params[ip]['m'] = im
                ip += 1
# Mock k-mesh
        self.mock_kmesh = {'kpoints': np.zeros((1, 3))}
# Mock structure
        self.mock_struct = {'qcoords': np.zeros((4, 3))}

# Scenario 1
    def test_normion_false(self):
        conf_file = _rpath + 'block_matrix.cfg'
        self.pars = ConfigParameters(conf_file)
        self.pars.parse_input()

        shells = []
        for sh_par in self.pars.shells:
            shells.append(ProjectorShell(sh_par, self.mock_plo, self.mock_proj_params, self.mock_kmesh, self.mock_struct, 0))

        proj_gr = ProjectorGroup(self.pars.groups[0], shells, self.mock_eigvals)

        proj_gr.normion = False
        block_maps, ndim = proj_gr.get_block_matrix_map()

        ndim_exp = 16
        block_maps_exp = [[{'bmat_range': (0, 5), 'shell_ion': (0, 0)},
                           {'bmat_range': (5, 10), 'shell_ion': (0, 1)},
                           {'bmat_range': (10, 13), 'shell_ion': (1, 0)},
                           {'bmat_range': (13, 16), 'shell_ion': (1, 1)}]]

        self.assertEqual(ndim, ndim_exp)
        self.assertEqual(block_maps, block_maps_exp)

# Scenario 2
    def test_normion_true(self):
        conf_file = _rpath + 'block_matrix.cfg'
        self.pars = ConfigParameters(conf_file)
        self.pars.parse_input()

        shells = []
        for sh_par in self.pars.shells:
            shells.append(ProjectorShell(sh_par, self.mock_plo, self.mock_proj_params, self.mock_kmesh, self.mock_struct, 0))

        proj_gr = ProjectorGroup(self.pars.groups[0], shells, self.mock_eigvals)

        proj_gr.normion = True
        block_maps, ndim = proj_gr.get_block_matrix_map()

        ndim_exp = 5
        block_maps_exp = [[{'bmat_range': (0, 5), 'shell_ion': (0, 0)}],
                          [{'bmat_range': (0, 5), 'shell_ion': (0, 1)}],
                          [{'bmat_range': (0, 3), 'shell_ion': (1, 0)}],
                          [{'bmat_range': (0, 3), 'shell_ion': (1, 1)}]]

        self.assertEqual(ndim, ndim_exp)
        self.assertEqual(block_maps, block_maps_exp)
示例#10
0
class TestProjectorGroupTwoSite(mytest.MyTestCase):
    """
    Tests for a two-site problem.

    Class:

    ProjectorGroup(sh_pars, proj_raw)

    Scenarios:
    - **test** that orthogonalization with NORMION = False is correct
    - **test** that orthogonalization with NORMION = True is correct
    """
    def setUp(self):
        conf_file = _rpath + 'example_two_site.cfg'
        self.pars = ConfigParameters(conf_file)
        self.pars.parse_input()
        vasp_data = VaspData(_rpath + 'two_site/')
        self.el_struct = ElectronicStructure(vasp_data)

        efermi = self.el_struct.efermi
        self.eigvals = self.el_struct.eigvals - efermi
        struct = self.el_struct.structure
        kmesh = self.el_struct.kmesh

        self.proj_sh = ProjectorShell(self.pars.shells[0],
                                      vasp_data.plocar.plo,
                                      vasp_data.plocar.proj_params, kmesh,
                                      struct, 0)
        self.proj_gr = ProjectorGroup(self.pars.groups[0], [self.proj_sh],
                                      self.eigvals)

# Scenario 1

    def test_ortho(self):
        self.proj_gr.normion = False
        self.proj_gr.orthogonalize()

        dens_mat, overl = self.proj_sh.density_matrix(self.el_struct)

        #        testout = _rpath + 'projortho_2site.out.test'
        #        with open(testout, 'wt') as f:
        #            f.write("density matrix: %s\n"%(dens_mat))
        #            f.write("overlap matrix: %s\n"%(overl))
        testout = _rpath + 'projortho_2site.test.h5'
        with HDFArchive(testout, 'w') as h5test:
            h5test['density_matrix'] = dens_mat
            h5test['overlap_matrix'] = overl

# FIXME: redundant
        self.assertEqual(overl[0, 0, ...], np.eye(5))
        self.assertEqual(overl[0, 1, ...], np.eye(5))

        #        expected_file = _rpath + 'projortho_2site.out'
        #        self.assertFileEqual(testout, expected_file)
        expected_file = _rpath + 'projortho_2site.out.h5'
        self.assertH5FileEqual(testout, expected_file)

# Scenario 2

    def test_ortho_normion(self):
        self.proj_gr.normion = True
        self.proj_gr.orthogonalize()

        dens_mat, overl = self.proj_sh.density_matrix(self.el_struct)

        #        testout = _rpath + 'projortho_normion.out.test'
        #        with open(testout, 'wt') as f:
        #            f.write("density matrix: %s\n"%(dens_mat))
        #            f.write("overlap matrix: %s\n"%(overl))
        testout = _rpath + 'projortho_normion.test.h5'
        with HDFArchive(testout, 'w') as h5test:
            h5test['density_matrix'] = dens_mat
            h5test['overlap_matrix'] = overl

# FIXME: redundant
        self.assertEqual(overl[0, 0, ...], np.eye(5))
        self.assertEqual(overl[0, 1, ...], np.eye(5))

        #        expected_file = _rpath + 'projortho_normion.out'
        #        self.assertFileEqual(testout, expected_file)
        expected_file = _rpath + 'projortho_normion.out.h5'
        self.assertH5FileEqual(testout, expected_file)