2015-08-10 11:32:08 +02:00
|
|
|
r"""
|
|
|
|
Tests of special parseres defined in ConfigParameters class
|
|
|
|
"""
|
2015-10-16 11:16:48 +02:00
|
|
|
import os
|
|
|
|
import rpath
|
|
|
|
_rpath = os.path.dirname(rpath.__file__) + '/'
|
|
|
|
|
2015-08-10 11:32:08 +02:00
|
|
|
import arraytest
|
|
|
|
import numpy as np
|
2018-09-06 13:48:24 +02:00
|
|
|
from triqs_dft_tools.converters.plovasp.inpconf import ConfigParameters
|
2015-08-10 11:32:08 +02:00
|
|
|
|
|
|
|
################################################################################
|
|
|
|
#
|
|
|
|
# TestParseStringLogical
|
|
|
|
#
|
|
|
|
################################################################################
|
|
|
|
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
|
2015-10-16 11:16:48 +02:00
|
|
|
self.cpars = ConfigParameters(_rpath + 'test1.cfg')
|
2015-08-10 11:32:08 +02:00
|
|
|
|
|
|
|
# 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')
|
|
|
|
|
|
|
|
################################################################################
|
|
|
|
#
|
|
|
|
# TestParseStringIonList
|
|
|
|
#
|
|
|
|
################################################################################
|
|
|
|
class TestParseStringIonList(arraytest.ArrayTestCase):
|
|
|
|
"""
|
|
|
|
Function:
|
|
|
|
|
|
|
|
def parse_string_ion_list(self, par_str)
|
|
|
|
|
|
|
|
Scenarios:
|
|
|
|
|
2018-05-04 16:21:08 +02:00
|
|
|
- **if** par_str == '5 6 7 8' **return** 'ions' with ion_list = [[4], [5], [6], [7]]
|
2015-08-10 11:32:08 +02:00
|
|
|
- **if** par_str == 'Ni' **raise** NotImplementedError
|
|
|
|
- **if** par_str == '0 1' **raise** AssertionError
|
2018-05-04 16:21:08 +02:00
|
|
|
- **if** par_str == '5..8' **return** 'ions' with ion_list = [[4], [5], [6], [7]]
|
2015-08-10 11:32:08 +02:00
|
|
|
- **if** par_str == '8..5' **raise** AssertionError
|
2018-05-04 16:21:08 +02:00
|
|
|
- **if** par_str == '[5, 8] [6 7]' **return** 'ions' with ion_list = [[4, 7], [5, 6]]
|
2015-08-10 11:32:08 +02:00
|
|
|
"""
|
|
|
|
def setUp(self):
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
# Dummy ConfigParameters object
|
2015-10-16 11:16:48 +02:00
|
|
|
self.cpars = ConfigParameters(_rpath + 'test1.cfg')
|
2015-08-10 11:32:08 +02:00
|
|
|
|
|
|
|
# Scenario 1
|
|
|
|
def test_simple_list(self):
|
2018-05-04 16:21:08 +02:00
|
|
|
expected = {'nion': 4, 'ion_list': [[4], [5], [6], [7]]}
|
2015-08-10 11:32:08 +02:00
|
|
|
res = self.cpars.parse_string_ion_list('5 6 7 8')
|
2018-05-04 16:21:08 +02:00
|
|
|
self.assertDictEqual(res, expected)
|
2015-08-10 11:32:08 +02:00
|
|
|
|
|
|
|
# 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):
|
2018-05-04 16:21:08 +02:00
|
|
|
expected = {'nion': 4, 'ion_list': [[4], [5], [6], [7]]}
|
2015-08-10 11:32:08 +02:00
|
|
|
res = self.cpars.parse_string_ion_list('5..8')
|
2018-05-04 16:21:08 +02:00
|
|
|
self.assertDictEqual(res, expected)
|
2015-08-10 11:32:08 +02:00
|
|
|
|
|
|
|
# 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')
|
|
|
|
|
2018-05-04 16:21:27 +02:00
|
|
|
# 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)
|
|
|
|
|
2015-08-10 11:32:08 +02:00
|
|
|
|
|
|
|
################################################################################
|
|
|
|
#
|
|
|
|
# TestParseStringTmatrix
|
|
|
|
#
|
|
|
|
################################################################################
|
|
|
|
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
|
2015-10-16 11:16:48 +02:00
|
|
|
self.cpars = ConfigParameters(_rpath + 'test1.cfg')
|
2015-08-10 11:32:08 +02:00
|
|
|
|
|
|
|
# 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)
|
2018-05-04 16:21:27 +02:00
|
|
|
|
2015-08-10 11:32:08 +02:00
|
|
|
# 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)
|
|
|
|
|
|
|
|
|
2015-11-13 14:38:20 +01:00
|
|
|
################################################################################
|
|
|
|
#
|
|
|
|
# TestParseEnergyWindow
|
|
|
|
#
|
|
|
|
################################################################################
|
|
|
|
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')
|
|
|
|
|
|
|
|
|
|
|
|
################################################################################
|
|
|
|
#
|
|
|
|
# TestParseFileTmatrix
|
|
|
|
#
|
|
|
|
################################################################################
|
|
|
|
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]])
|
|
|
|
|
2015-11-13 18:15:21 +01:00
|
|
|
res = self.cpars.parse_file_tmatrix(_rpath + 'tmatrix_file.dat')
|
2015-11-13 14:38:20 +01:00
|
|
|
self.assertEqual(res, expected)
|
|
|
|
|
|
|
|
# Scenario 2
|
|
|
|
def test_wrong_file(self):
|
|
|
|
with self.assertRaises(ValueError):
|
2015-11-13 18:15:21 +01:00
|
|
|
self.cpars.parse_file_tmatrix(_rpath + 'test1.cfg')
|
2015-11-13 14:38:20 +01:00
|
|
|
|
|
|
|
################################################################################
|
|
|
|
#
|
|
|
|
# TestParseStringDosmesh
|
|
|
|
#
|
|
|
|
################################################################################
|
|
|
|
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')
|
|
|
|
|
|
|
|
|
2018-05-04 16:21:08 +02:00
|
|
|
if __name__ == '__main__':
|
|
|
|
import unittest
|
|
|
|
unittest.main(verbosity=2, buffer=False)
|
|
|
|
|