def test_example(self): conf_pars = ConfigParameters(_rpath + 'example.cfg') conf_pars.parse_general() res = conf_pars.general expected = {'basename': 'test_base', 'efermi': 0.1, 'dosmesh': {'n_points': 101, 'emin': -8.0, 'emax': 4.0}} self.assertDictEqual(res, expected)
def test_two_shells(self): conf_pars = ConfigParameters(_rpath + 'parse_shells_4.cfg') conf_pars.parse_shells() res = conf_pars.shells expected = [{ 'user_index': 1, 'lshell': 2, 'ion_list': np.array([4, 5, 6, 7]) }, { 'user_index': 2, 'lshell': 1, 'ion_list': np.array([0, 1, 2, 3]), 'tmatrix': np.array([[0., 1., 0.], [1., 0., 0.], [0., 0., 1.]]) }] # ...lousy way to test equality of two dictionaries containing numpy arrays self.assertEqual(len(res), len(expected)) arr = res[0].pop('ion_list') arr_exp = expected[0].pop('ion_list') self.assertEqual(arr, arr_exp) arr = res[1].pop('ion_list') arr_exp = expected[1].pop('ion_list') self.assertEqual(arr, arr_exp) arr = res[1].pop('tmatrix') arr_exp = expected[1].pop('tmatrix') self.assertEqual(arr, arr_exp) self.assertListEqual(res, expected)
class TestParseStringLogical(arraytest.ArrayTestCase): """ Function: def parse_string_logical(self, par_str) Scenarios: - **if** par_str == 'True' **return** True - **if** par_str == 'False' **return** False - **if** par_str == '0' **raise** assertion """ def setUp(self): """ """ # Dummy ConfigParameters object self.cpars = ConfigParameters(_rpath + 'test1.cfg') # Scenario 1 def test_true(self): res = self.cpars.parse_string_logical('True') self.assertEqual(res, True) # Scenario 2 def test_false(self): res = self.cpars.parse_string_logical('False') self.assertEqual(res, False) # Scenario 3 def test_incorrect(self): with self.assertRaises(AssertionError): self.cpars.parse_string_logical('0')
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)
class TestParseFileTmatrix(arraytest.ArrayTestCase): """ Function: def parse_file_tmatrix(self, par_str) Scenarios: - **if** file is correct **return** array() - **if** file is incorrect **raise** (-1.5, 3.0) """ def setUp(self): """ """ # Dummy ConfigParameters object self.cpars = ConfigParameters(_rpath + 'test1.cfg') # Scenario 1 def test_correct_file(self): expected = np.array( [[ -2.52000000e-04, -0.00000000e+00, -4.27145000e-02, 3.00000000e-07, -9.99087300e-01], [ -4.13570000e-03, -2.00000000e-07, -9.99078700e-01, -1.00000000e-07, 4.27152000e-02], [ -3.80200000e-04, 0.00000000e+00, 6.04452000e-02, -1.00000000e-07, -9.98171400e-01], [ -5.14500000e-04, -0.00000000e+00, -9.98171400e-01, 0.00000000e+00, -6.04450000e-02]]) res = self.cpars.parse_file_tmatrix(_rpath + 'tmatrix_file.dat') self.assertEqual(res, expected) # Scenario 2 def test_wrong_file(self): with self.assertRaises(ValueError): self.cpars.parse_file_tmatrix(_rpath + 'test1.cfg')
class TestParseParameterSet(arraytest.ArrayTestCase): """ Function: def parse_parameter_set(self, section, param_set, excpetion=False) Scenarios: - **if** config-file section [Shell 1] contains 'LSHELL = 2' **and** 'lshell' and 'ions' are in `param_set` **return** a dictionary {'lshell': 2} - **if** config-file section [Shell 1] contains 'LSHELL = 2' **and** 'lshell' and 'ions' are in `param_set` and exception=True **raise** Exception """ def setUp(self): """ """ # Dummy ConfigParameters object self.cpars = ConfigParameters(_rpath + 'test1.cfg') # Scenario 1 def test_sh_required(self): param_set = self.cpars.sh_required # contains 'lshell' and 'ions' res = self.cpars.parse_parameter_set('Shell 1', param_set) expected = {'lshell': 2} self.assertDictEqual(res, expected) # Scenario 2 def test_sh_required_exception(self): section = 'Shell 1' param_set = self.cpars.sh_required # contains 'lshell' and 'ions' err_mess = "Required parameter" # .* in section [%s]"%(section) with self.assertRaisesRegexp(Exception, err_mess): self.cpars.parse_parameter_set(section, param_set, exception=True)
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)
class TestParseParameterSet(arraytest.ArrayTestCase): """ Function: def parse_parameter_set(self, section, param_set, excpetion=False) Scenarios: - **if** config-file section [Shell 1] contains 'LSHELL = 2' **and** 'lshell' and 'ions' are in `param_set` **return** a dictionary {'lshell': 2} - **if** config-file section [Shell 1] contains 'LSHELL = 2' **and** 'lshell' and 'ions' are in `param_set` and exception=True **raise** Exception """ def setUp(self): """ """ # Dummy ConfigParameters object self.cpars = ConfigParameters(_rpath + 'test1.cfg') # Scenario 1 def test_sh_required(self): param_set = self.cpars.sh_required # contains 'lshell' and 'ions' res = self.cpars.parse_parameter_set('Shell 1', param_set) expected = {'lshell': 2} self.assertDictEqual(res, expected) # Scenario 2 def test_sh_required_exception(self): section = 'Shell 1' param_set = self.cpars.sh_required # contains 'lshell' and 'ions' err_mess = "Required parameter" # .* in section [%s]"%(section) with self.assertRaisesRegex(Exception, err_mess): self.cpars.parse_parameter_set(section, param_set, exception=True)
def test_two_shells_with_corr_false(self): conf_pars = ConfigParameters(_rpath + 'parse_shells_5.cfg') conf_pars.parse_shells() res = conf_pars.shells expected = [{ 'user_index': 1, 'lshell': 2, 'ions': { 'nion': 4, 'ion_list': [[4], [5], [6], [7]] }, 'corr': True, 'ion_sort': None }, { 'user_index': 2, 'lshell': 1, 'ions': { 'nion': 4, 'ion_list': [[0], [1], [2], [3]] }, 'corr': False, 'ion_sort': None }] self.assertEqual(len(res), len(expected)) arr = res[0].pop('ions') arr_exp = expected[0].pop('ions') self.assertDictEqual(arr, arr_exp) arr = res[1].pop('ions') arr_exp = expected[1].pop('ions') self.assertDictEqual(arr, arr_exp) self.assertListEqual(res, expected)
def test_example(self): conf_pars = ConfigParameters(_rpath + 'example.cfg') conf_pars.parse_groups() res = conf_pars.groups expected = [{'index': 1, 'shells': [1, 2], 'ewindow': (-7.6, 3.0), 'normalize': True, 'normion': True}, {'index': 2, 'shells': [3], 'ewindow': (-1.6, 2.0), 'normalize': True, 'normion': True}] self.assertListEqual(res, expected)
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)
def test_example(self): conf_pars = ConfigParameters(_rpath + 'example.cfg') conf_pars.parse_general() res = conf_pars.general expected = { 'basename': 'test_base', 'efermi': 0.1, 'dosmesh': { 'n_points': 101, 'emin': -8.0, 'emax': 4.0 } } self.assertDictEqual(res, expected)
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 = 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)
class TestParseStringIonList(arraytest.ArrayTestCase): """ Function: def parse_string_ion_list(self, par_str) Scenarios: - **if** par_str == '5 6 7 8' **return** 'ions' with ion_list = [[4], [5], [6], [7]] - **if** par_str == 'Ni' **raise** NotImplementedError - **if** par_str == '0 1' **raise** AssertionError - **if** par_str == '5..8' **return** 'ions' with ion_list = [[4], [5], [6], [7]] - **if** par_str == '8..5' **raise** AssertionError - **if** par_str == '[5, 8] [6 7]' **return** 'ions' with ion_list = [[4, 7], [5, 6]] """ def setUp(self): """ """ # Dummy ConfigParameters object self.cpars = ConfigParameters(_rpath + 'test1.cfg') # Scenario 1 def test_simple_list(self): expected = {'nion': 4, 'ion_list': [[4], [5], [6], [7]]} res = self.cpars.parse_string_ion_list('5 6 7 8') self.assertDictEqual(res, expected) # Scenario 2 def test_atomic_symbol(self): with self.assertRaises(NotImplementedError): self.cpars.parse_string_ion_list('Ni') # Scenario 3 def test_out_of_bounds(self): err_mess = "Lowest ion index is" with self.assertRaisesRegexp(AssertionError, err_mess): self.cpars.parse_string_ion_list('0 1') # Scenario 4 def test_list_range(self): expected = {'nion': 4, 'ion_list': [[4], [5], [6], [7]]} res = self.cpars.parse_string_ion_list('5..8') self.assertDictEqual(res, expected) # Scenario 5 def test_range_wrong_order(self): err_mess = "First index of the range" with self.assertRaisesRegexp(AssertionError, err_mess): self.cpars.parse_string_ion_list('8..5') # Scenario 6 def test_eq_classes(self): expected = {'nion': 4, 'ion_list': [[4, 7], [5, 6]]} res = self.cpars.parse_string_ion_list('[5, 8] [6 7]') self.assertDictEqual(res, expected)
class TestParseBandWindow(arraytest.ArrayTestCase): """ Function: def parse_band_window(self, par_str) Scenarios: - **if** par_str == '1 10' **return** (1, 10) - **if** par_str == '3.0 -1.5' **raise** AssertionError - **if** par_str == '1.0' **raise** AssertionError - **if** par_str == 'aaa' **raise** ValueError - **if** par_str == '1.5 3.0 2.0' **raise** AssertionError """ def setUp(self): """ """ # Dummy ConfigParameters object self.cpars = ConfigParameters(_rpath + 'test1.cfg') # Scenario 1 def test_correct_range(self): expected = (1, 10) res = self.cpars.parse_band_window('1 10') self.assertEqual(res, expected) # Scenario 2 def test_wrong_range(self): err_mess = "The first int in BANDS" with self.assertRaisesRegex(AssertionError, err_mess): self.cpars.parse_band_window('10 1') # Scenario 3 def test_one_float(self): err_mess = "BANDS must be specified" with self.assertRaisesRegex(AssertionError, err_mess): self.cpars.parse_band_window('1') # Scenario 4 def test_wrong_string(self): with self.assertRaises(ValueError): self.cpars.parse_band_window('aaa') # Scenario 5 def test_three_ints(self): err_mess = "BANDS must be specified" with self.assertRaisesRegex(AssertionError, err_mess): self.cpars.parse_band_window('1 2 3')
class TestParseStringTmatrix(arraytest.ArrayTestCase): """ Function: def parse_string_tmatrix(self, par_str) Parses a matrix defined as a set of rows in the conf-file. Scenarios: - **if** number of columns is not the same **raise** AssertionError - **if** complex matrix is read and the number of columns is odd **raise** AssertionError - **if** a correct matrix is given **return** an array """ def setUp(self): """ """ # Dummy ConfigParameters object self.cpars = ConfigParameters(_rpath + 'test1.cfg') # Scenario 1 def test_number_of_columns(self): par_str = "1.0 0.0\n1.0" err_mess = "Number of columns" with self.assertRaisesRegex(AssertionError, err_mess): self.cpars.parse_string_tmatrix(par_str, real=True) # Scenario 2 def test_complex_matrix_odd(self): par_str = "1.0 0.0 2.0 1.0 0.0\n0.0 1.0 2.0 3.0 -1.0" err_mess = "Complex matrix must" with self.assertRaisesRegex(AssertionError, err_mess): self.cpars.parse_string_tmatrix(par_str, real=False) # Scenario 3 def test_complex_matrix(self): par_str = "1.0 0.0 2.0 -3.0\n0.0 1.0 -1.0 1.0" res = self.cpars.parse_string_tmatrix(par_str, real=False) expected = np.array([[1.0, 2.0 - 3.0j], [1.0j, -1.0 + 1.0j]]) self.assertEqual(res, expected)
def test_two_shells(self): conf_pars = ConfigParameters(_rpath + 'parse_shells_4.cfg') conf_pars.parse_shells() res = conf_pars.shells expected = [{ 'user_index': 1, 'lshell': 2, 'ions': { 'nion': 4, 'ion_list': [[4], [5], [6], [7]] }, 'corr': True, 'ion_sort': None }, { 'user_index': 2, 'lshell': 1, 'ions': { 'nion': 4, 'ion_list': [[0], [1], [2], [3]] }, 'tmatrix': np.array([[0., 1., 0.], [1., 0., 0.], [0., 0., 1.]]), 'corr': True, 'ion_sort': None }] # ...lousy way to test equality of two dictionaries containing numpy arrays self.assertEqual(len(res), len(expected)) arr = res[0].pop('ions') arr_exp = expected[0].pop('ions') self.assertDictEqual(arr, arr_exp) arr = res[1].pop('ions') arr_exp = expected[1].pop('ions') self.assertDictEqual(arr, arr_exp) arr = res[1].pop('tmatrix') arr_exp = expected[1].pop('tmatrix') self.assertEqual(arr, arr_exp) self.assertListEqual(res, expected)
class TestParseEnergyWindow(arraytest.ArrayTestCase): """ Function: def parse_energy_window(self, par_str) Scenarios: - **if** par_str == '-1.5 3.0' **return** (-1.5, 3.0) - **if** par_str == '3.0 -1.5' **raise** AssertionError - **if** par_str == '1.0' **raise** AssertionError - **if** par_str == 'aaa' **raise** ValueError - **if** par_str == '1.5 3.0 2.0' **raise** AssertionError """ def setUp(self): """ """ # Dummy ConfigParameters object self.cpars = ConfigParameters(_rpath + 'test1.cfg') # Scenario 1 def test_correct_range(self): expected = (-1.5, 3.0) res = self.cpars.parse_energy_window('-1.5 3.0') self.assertEqual(res, expected) # Scenario 2 def test_wrong_range(self): err_mess = "The first float in EWINDOW" with self.assertRaisesRegexp(AssertionError, err_mess): self.cpars.parse_energy_window('3.0 -1.5') # Scenario 3 def test_one_float(self): err_mess = "EWINDOW must be specified" with self.assertRaisesRegexp(AssertionError, err_mess): self.cpars.parse_energy_window('1.0') # Scenario 4 def test_wrong_string(self): with self.assertRaises(ValueError): self.cpars.parse_energy_window('aaa') # Scenario 5 def test_three_floats(self): err_mess = "EWINDOW must be specified" with self.assertRaisesRegexp(AssertionError, err_mess): self.cpars.parse_energy_window('1.5 3.0 2.0')
class TestParseStringTmatrix(arraytest.ArrayTestCase): """ Function: def parse_string_tmatrix(self, par_str) Parses a matrix defined as a set of rows in the conf-file. Scenarios: - **if** number of columns is not the same **raise** AssertionError - **if** complex matrix is read and the number of columns is odd **raise** AssertionError - **if** a correct matrix is given **return** an array """ def setUp(self): """ """ # Dummy ConfigParameters object self.cpars = ConfigParameters(_rpath + 'test1.cfg') # Scenario 1 def test_number_of_columns(self): par_str = "1.0 0.0\n1.0" err_mess = "Number of columns" with self.assertRaisesRegexp(AssertionError, err_mess): self.cpars.parse_string_tmatrix(par_str, real=True) # Scenario 2 def test_complex_matrix_odd(self): par_str = "1.0 0.0 2.0 1.0 0.0\n0.0 1.0 2.0 3.0 -1.0" err_mess = "Complex matrix must" with self.assertRaisesRegexp(AssertionError, err_mess): self.cpars.parse_string_tmatrix(par_str, real=False) # Scenario 3 def test_complex_matrix(self): par_str = "1.0 0.0 2.0 -3.0\n0.0 1.0 -1.0 1.0" res = self.cpars.parse_string_tmatrix(par_str, real=False) expected = np.array([[1.0, 2.0 - 3.0j], [1.0j, -1.0 + 1.0j]]) self.assertEqual(res, expected)
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__() res = res.replace(" ","") # Remove spaces for comparison 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)
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__() res = res.replace(" ","") # Remove spaces for comparison expected = r"""Shells: [{'ions':{'nion':4,'ion_list':[[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)
class TestParseStringIonList(arraytest.ArrayTestCase): """ Function: def parse_string_ion_list(self, par_str) Scenarios: - **if** par_str == '5 6 7 8' **return** array([4, 5, 6, 7]) - **if** par_str == 'Ni' **raise** NotImplementedError - **if** par_str == '0 1' **raise** AssertionError - **if** par_str == '5..8' **return** array([4, 5, 6, 7]) - **if** par_str == '8..5' **raise** AssertionError """ def setUp(self): """ """ # Dummy ConfigParameters object self.cpars = ConfigParameters(_rpath + 'test1.cfg') # Scenario 1 def test_simple_list(self): expected = np.array([4, 5, 6, 7]) res = self.cpars.parse_string_ion_list('5 6 7 8') self.assertEqual(res, expected) # Scenario 2 def test_atomic_symbol(self): with self.assertRaises(NotImplementedError): self.cpars.parse_string_ion_list('Ni') # Scenario 3 def test_out_of_bounds(self): err_mess = "Lowest ion index is" with self.assertRaisesRegexp(AssertionError, err_mess): self.cpars.parse_string_ion_list('0 1') # Scenario 4 def test_list_range(self): expected = np.array([4, 5, 6, 7]) res = self.cpars.parse_string_ion_list('5..8') self.assertEqual(res, expected) # Scenario 5 def test_range_wrong_order(self): err_mess = "First index of the range" with self.assertRaisesRegexp(AssertionError, err_mess): self.cpars.parse_string_ion_list('8..5')
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)
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__() res = res.replace(" ", "") # Remove spaces for comparison expected = r"""Shells: [{'ions':{'nion':4,'ion_list':[[4],[5],[6],[7]]},'user_index':1,'lshell':2},{'tmatrix':array([[0.,1.,0.], [1.,0.,0.], [0.,0.,1.]]),'ions':{'nion':4,'ion_list':[[0],[1],[2],[3]]},'user_index':2,'lshell':1},{'ions':{'nion':4,'ion_list':[[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)
class TestParseStringDosmesh(arraytest.ArrayTestCase): """ Function: def parse_string_dosmesh(self, par_str) Scenarios: - **if** par_str == '-8.0 4.0 101' **return** dictionary - **if** par_str == '101' **return** dictionary - **if** par_str == '-8.0 101' **raise** ValueError - **if** par_str == '8.0' **raise** ValueError """ def setUp(self): """ """ # Dummy ConfigParameters object self.cpars = ConfigParameters(_rpath + 'test1.cfg') # Scenario 1 def test_range_npoints(self): expected = {'n_points': 101, 'emin': -8.0, 'emax': 4.0} res = self.cpars.parse_string_dosmesh('-8.0 4.0 101') self.assertEqual(res, expected) # Scenario 2 def test_only_npoints(self): expected_npoints = 101 res = self.cpars.parse_string_dosmesh('101') self.assertTrue(np.isnan(res['emin'])) self.assertTrue(np.isnan(res['emax'])) self.assertEqual(res['n_points'], expected_npoints) # Scenario 3 def test_two_numbers(self): err_mess = "DOSMESH must be either" with self.assertRaisesRegex(ValueError, err_mess): self.cpars.parse_string_dosmesh('-8.0 101') # Scenario 4 def test_wrong_input(self): with self.assertRaises(ValueError): self.cpars.parse_string_dosmesh('8.0')
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)
class TestParseStringDosmesh(arraytest.ArrayTestCase): """ Function: def parse_string_dosmesh(self, par_str) Scenarios: - **if** par_str == '-8.0 4.0 101' **return** dictionary - **if** par_str == '101' **return** dictionary - **if** par_str == '-8.0 101' **raise** ValueError - **if** par_str == '8.0' **raise** ValueError """ def setUp(self): """ """ # Dummy ConfigParameters object self.cpars = ConfigParameters(_rpath + 'test1.cfg') # Scenario 1 def test_range_npoints(self): expected = {'n_points': 101, 'emin': -8.0, 'emax': 4.0} res = self.cpars.parse_string_dosmesh('-8.0 4.0 101') self.assertEqual(res, expected) # Scenario 2 def test_only_npoints(self): expected_npoints = 101 res = self.cpars.parse_string_dosmesh('101') self.assertTrue(np.isnan(res['emin'])) self.assertTrue(np.isnan(res['emax'])) self.assertEqual(res['n_points'], expected_npoints) # Scenario 3 def test_two_numbers(self): err_mess = "DOSMESH must be either" with self.assertRaisesRegexp(ValueError, err_mess): self.cpars.parse_string_dosmesh('-8.0 101') # Scenario 4 def test_wrong_input(self): with self.assertRaises(ValueError): self.cpars.parse_string_dosmesh('8.0')
class TestProjectorGroup(mytest.MyTestCase): """ Class: ProjectorGroup(sh_pars, proj_raw) Scenarios: - **test** that orthogonalization is correct - **test** that NORMION = True gives the same result """ 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 = 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.orthogonalize() dens_mat, overl = self.proj_sh.density_matrix(self.el_struct) # testout = _rpath + 'projortho.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.test.h5' with HDFArchive(testout, 'w') as h5test: h5test['density_matrix'] = dens_mat h5test['overlap_matrix'] = overl # FIXME: seems redundant, as 'overl' is written to the file anyway self.assertEqual(overl, np.eye(5)) # expected_file = _rpath + 'projortho.out' expected_file = _rpath + 'projortho.out.h5' # self.assertFileEqual(testout, expected_file) 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.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.test.h5' with HDFArchive(testout, 'w') as h5test: h5test['density_matrix'] = dens_mat h5test['overlap_matrix'] = overl # FIXME: seems redundant, as 'overl' is written to the file anyway self.assertEqual(overl, np.eye(5)) # expected_file = _rpath + 'projortho.out' # self.assertFileEqual(testout, expected_file) expected_file = _rpath + 'projortho.out.h5' self.assertH5FileEqual(testout, expected_file)
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()
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()
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()
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()
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)
def setUp(self): """ """ # Dummy ConfigParameters object self.cpars = ConfigParameters(_rpath + 'test1.cfg')
def test_no_shell(self): conf_pars = ConfigParameters(_rpath + 'parse_shells_1.cfg') err_mess = "No projected shells" with self.assertRaisesRegexp(AssertionError, err_mess): conf_pars.parse_shells()
class TestProjectorGroupCompl(mytest.MyTestCase): """ Class: ProjectorGroupCompl(sh_pars, proj_raw) Scenarios: - **test** that unequal number of bands at different k-points gives error - **test** that COMLEMENT=TRUE gives orthonormal projectors """ 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 = 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) def test_num_bands(self): self.pars.groups[0]['complement'] = True err_mess = "At each band the same number" with self.assertRaisesRegex(AssertionError, err_mess): self.proj_gr = ProjectorGroup(self.pars.groups[0], [self.proj_sh], self.eigvals) def test_compl(self): self.pars.groups[0]['complement'] = True self.pars.groups[0]['bands'] = [10, 25] self.proj_gr = ProjectorGroup(self.pars.groups[0], [self.proj_sh], self.eigvals) self.proj_gr.orthogonalize() self.proj_gr.calc_complement(self.eigvals) temp = self.proj_gr.normion self.proj_gr.normion = False block_maps, ndim = self.proj_gr.get_block_matrix_map() self.proj_gr.normion = temp _, ns, nk, _, _ = self.proj_gr.shells[0].proj_win.shape # Note that 'ns' and 'nk' are the same for all shells for isp in range(ns): for ik in range(nk): print(('ik', ik)) bmin = self.proj_gr.ib_win[ik, isp, 0] bmax = self.proj_gr.ib_win[ik, isp, 1] + 1 nb = bmax - bmin p_mat = np.zeros((ndim, nb), dtype=np.complex128) #print(bmin,bmax,nb) # Combine all projectors of the group to one block projector for bl_map in block_maps: p_mat[:, :] = 0.0j # !!! Clean-up from the last k-point and block! for ibl, block in enumerate(bl_map): i1, i2 = block['bmat_range'] ish, ion = block['shell_ion'] nlm = i2 - i1 + 1 shell = self.proj_gr.shells[ish] p_mat[i1:i2, :nb] = shell.proj_win[ion, isp, ik, :nlm, :nb] overlap_L = np.dot(p_mat.conjugate().transpose(), p_mat) overlap_N = np.dot(p_mat, p_mat.conjugate().transpose()) assert np.all( np.abs(np.eye(overlap_N.shape[0]) - overlap_N) < 1e-13) assert np.all( np.abs(np.eye(overlap_L.shape[0]) - overlap_L) < 1e-13)
def test_sh_required(self): conf_pars = ConfigParameters(_rpath + 'parse_shells_3.cfg') err_mess = "Required parameter" with self.assertRaisesRegexp(Exception, err_mess): conf_pars.parse_shells()
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.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.ref.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.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.ref.h5' self.assertH5FileEqual(testout, expected_file)
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 range(nproj)] ip = 0 # Mock d-sites for isite in range(2): for im in range(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 range(2, 4): for im in range(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)
def test_bad_indices(self): conf_pars = ConfigParameters(_rpath + 'parse_shells_2.cfg') err_mess = "Failed to extract shell indices" with self.assertRaisesRegexp(ValueError, err_mess): conf_pars.parse_shells()