Skip to content

Commit

Permalink
[tests] switch all pytest to unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
the-hampel committed Mar 4, 2024
1 parent e0c2f43 commit a8d4239
Show file tree
Hide file tree
Showing 13 changed files with 679 additions and 655 deletions.
2 changes: 1 addition & 1 deletion Docker/openmpi_dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ ENV CPATH=/triqs/include:/usr/include/mkl:${CPATH} \
CMAKE_PREFIX_PATH=/triqs/share/cmake:${CMAKE_PREFIX_PATH} \
CMAKE_BUILD_PARALLEL_LEVEL=${NCORES} \
CTEST_OUTPUT_ON_FAILURE=1 \
CTEST_PARALLEL_LEVEL=${NCORES} \
CTEST_PARALLEL_LEVEL=1 \
BLA_VENDOR=Intel10_64_dyn \
TRIQS_ROOT=/triqs

Expand Down
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
numpy
scipy
argparse
pytest
scikit-image
# From python 3.11 and newer, toml is part of standard implementation
tomli
35 changes: 9 additions & 26 deletions test/python/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,29 +1,12 @@
# all pytest unittests
set (all_pytests
test_afm_mapping
test_dict_to_h5
test_interaction_hamiltonian
test_manipulate_chemical_potential.py
test_observables.py
test_postproc_toml_dict.py
test_postproc_toml_dict_integration.py
)

foreach(test ${all_pytests})
get_filename_component(test_name ${test} NAME_WE)
get_filename_component(test_dir ${test} DIRECTORY)

add_test(NAME ${test_name}
COMMAND ${TRIQS_PYTHON_EXECUTABLE} -m pytest -vv ${CMAKE_CURRENT_SOURCE_DIR}/${test_dir}/${test_name}.py
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/${test_dir})

set_property(TEST ${test_name} APPEND PROPERTY ENVIRONMENT PYTHONPATH=${PROJECT_BINARY_DIR}/python:$ENV{PYTHONPATH})
endforeach()

# ------------------------------#

# all other tests
set(all_tests
set(basic_tests
test_afm_mapping
test_dict_to_h5
test_interaction_hamiltonian
test_manipulate_chemical_potential
test_observables
test_postproc_toml_dict
test_postproc_toml_dict_integration
test_convergence
test_matheval
test_plot_correlated_bands
Expand All @@ -42,7 +25,7 @@ set(all_tests
# copy reference data for respack test
FILE(COPY respack_sfo_data DESTINATION ${test_dir})

foreach(test ${all_tests})
foreach(test ${basic_tests})
get_filename_component(test_name ${test} NAME_WE)
get_filename_component(test_dir ${test} DIRECTORY)
add_test(NAME ${test_name} COMMAND ${TRIQS_PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/${test_dir}/${test_name}.py WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/${test_dir})
Expand Down
6 changes: 3 additions & 3 deletions test/python/nio_cthyb_hartree/dmft_config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ jobname = "out"
mu_initial_guess = 13.751
enforce_off_diag = [false, true]

n_iw = 501
beta = 20
n_iw = 201
beta = 10
w_range = [-10, 10]
eta = 0.05
n_w = 501
Expand All @@ -31,7 +31,7 @@ dc_dmft = false
type = "cthyb"
idx_impurities = [0]
n_l = 25
length_cycle = 1000
length_cycle = 200
n_warmup_cycles = 1e+3
n_cycles_tot = 1e+4
imag_threshold = 1e-5
Expand Down
Binary file modified test/python/nio_cthyb_hartree/ref.h5
Binary file not shown.
10 changes: 5 additions & 5 deletions test/python/svo_cthyb_basic_tf/dmft_config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ seedname = "inp"
jobname = "out"
enforce_off_diag = false
block_threshold = 0.001
mu_initial_guess = -0.027041
mu_initial_guess = -0.0289

prec_mu = 0.001
n_iw = 501
prec_mu = 0.01
n_iw = 301

h_int_type = "kanamori"
U = 8.0
Expand Down Expand Up @@ -34,8 +34,8 @@ load_sigma = false

[solver]
type = "cthyb"
length_cycle = 120
n_warmup_cycles = 8000
length_cycle = 400
n_warmup_cycles = 1e+3
n_cycles_tot = 1e+4
imag_threshold = 1e-5
delta_interface = true
Expand Down
83 changes: 45 additions & 38 deletions test/python/test_afm_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,56 +10,63 @@
from solid_dmft.dmft_tools import afm_mapping
from helper import are_iterables_equal

def test_determine_afm_mapping():
general_params = {'magmom': [+1, -1, +1, -1], 'afm_order': True}
archive = {'DMFT_input': {}}
n_inequiv_shells = 4
import unittest

expected_general_params = general_params.copy()
# copy, source, switch
expected_general_params['afm_mapping'] = [[False, 0, False], [True, 0, True],
[True, 0, False], [True, 0, True]]
class test_afm_mapping(unittest.TestCase):
def test_determine_afm_mapping(self):
general_params = {'magmom': [+1, -1, +1, -1], 'afm_order': True}
archive = {'DMFT_input': {}}
n_inequiv_shells = 4

general_params = afm_mapping.determine(general_params, archive, n_inequiv_shells)
expected_general_params = general_params.copy()
# copy, source, switch
expected_general_params['afm_mapping'] = [[False, 0, False], [True, 0, True],
[True, 0, False], [True, 0, True]]

assert are_iterables_equal(general_params, expected_general_params)
general_params = afm_mapping.determine(general_params, archive, n_inequiv_shells)

general_params = {'magmom': [+1, -1, +2, +2], 'afm_order': True}
archive = {'DMFT_input': {}}
n_inequiv_shells = 4
assert are_iterables_equal(general_params, expected_general_params)

expected_general_params = general_params.copy()
# copy, source, switch
expected_general_params['afm_mapping'] = [[False, 0, False], [True, 0, True],
[False, 2, False], [True, 2, False]]
general_params = {'magmom': [+1, -1, +2, +2], 'afm_order': True}
archive = {'DMFT_input': {}}
n_inequiv_shells = 4

general_params = afm_mapping.determine(general_params, archive, n_inequiv_shells)
expected_general_params = general_params.copy()
# copy, source, switch
expected_general_params['afm_mapping'] = [[False, 0, False], [True, 0, True],
[False, 2, False], [True, 2, False]]

assert are_iterables_equal(general_params, expected_general_params)
general_params = afm_mapping.determine(general_params, archive, n_inequiv_shells)

# Reading in the afm_mapping from the archive
general_params = {'magmom': [+1, -1, +2], 'afm_order': True}
archive = {'DMFT_input': {'afm_mapping': [[False, 0, False], [False, 1, False],
[False, 2, False]]}}
n_inequiv_shells = 3
assert are_iterables_equal(general_params, expected_general_params)

expected_general_params = general_params.copy()
# copy, source, switch
expected_general_params['afm_mapping'] = [[False, 0, False], [False, 1, False],
[False, 2, False]]
# Reading in the afm_mapping from the archive
general_params = {'magmom': [+1, -1, +2], 'afm_order': True}
archive = {'DMFT_input': {'afm_mapping': [[False, 0, False], [False, 1, False],
[False, 2, False]]}}
n_inequiv_shells = 3

general_params = afm_mapping.determine(general_params, archive, n_inequiv_shells)
expected_general_params = general_params.copy()
# copy, source, switch
expected_general_params['afm_mapping'] = [[False, 0, False], [False, 1, False],
[False, 2, False]]

assert are_iterables_equal(general_params, expected_general_params)
general_params = afm_mapping.determine(general_params, archive, n_inequiv_shells)

general_params = {'magmom': [+1, -1, +2, +2], 'afm_order': True}
archive = {'DMFT_input': {}}
n_inequiv_shells = 3
assert are_iterables_equal(general_params, expected_general_params)

expected_general_params = general_params.copy()
# copy, source, switch
expected_general_params['afm_order'] = False
general_params = {'magmom': [+1, -1, +2, +2], 'afm_order': True}
archive = {'DMFT_input': {}}
n_inequiv_shells = 3

general_params = afm_mapping.determine(general_params, archive, n_inequiv_shells)
expected_general_params = general_params.copy()
# copy, source, switch
expected_general_params['afm_order'] = False

general_params = afm_mapping.determine(general_params, archive, n_inequiv_shells)

assert are_iterables_equal(general_params, expected_general_params)

if __name__ == '__main__':
unittest.main()

assert are_iterables_equal(general_params, expected_general_params)
22 changes: 14 additions & 8 deletions test/python/test_dict_to_h5.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
from solid_dmft.io_tools import dict_to_h5

def test_prep_params_for_h5():
inp = {'a': None, 'b': {'c': None, 'd': 'e'}, 'f': [None, 'g']}
expected = {'a': 'none', 'b': {'c': 'none', 'd': 'e'}, 'f': ['none', 'g']}
import unittest

assert dict_to_h5.prep_params_for_h5(inp) == expected
class test_dict_to_h5(unittest.TestCase):
def test_prep_params_for_h5(self):
inp = {'a': None, 'b': {'c': None, 'd': 'e'}, 'f': [None, 'g']}
expected = {'a': 'none', 'b': {'c': 'none', 'd': 'e'}, 'f': ['none', 'g']}

def test_prep_params_from_h5():
inp = {'a': 'none', 'b': {'c': 'none', 'd': 'e'}, 'f': ['none', 'g']}
expected = {'a': None, 'b': {'c': None, 'd': 'e'}, 'f': [None, 'g']}
assert dict_to_h5.prep_params_for_h5(inp) == expected

assert dict_to_h5.prep_params_from_h5(inp) == expected
def test_prep_params_from_h5(self):
inp = {'a': 'none', 'b': {'c': 'none', 'd': 'e'}, 'f': ['none', 'g']}
expected = {'a': None, 'b': {'c': None, 'd': 'e'}, 'f': [None, 'g']}

assert dict_to_h5.prep_params_from_h5(inp) == expected

if __name__ == '__main__':
unittest.main()
36 changes: 21 additions & 15 deletions test/python/test_interaction_hamiltonian.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,27 @@
from solid_dmft.dmft_tools.interaction_hamiltonian import _load_crpa_interaction_matrix
from helper import Dummy

def test_load_crpa_interaction_matrix():
sum_k = Dummy()
sum_k.n_inequiv_shells = 2
sum_k.n_corr_shells = 4
sum_k.corr_shells = [{'dim': 5}, {'dim': 5}, {'dim': 5}, {'dim': 5}]
sum_k.corr_to_inequiv = [0, 1, 1, 0]
sum_k.inequiv_to_corr = [0, 1]
sum_k.gf_struct_solver = [{'down_0' : 5, 'up_0' : 5},{'down_0' : 5, 'up_0' : 5}]
import unittest

crpa_matrix = _load_crpa_interaction_matrix(sum_k, 'UIJKL')
class test_interaction_hamiltonian(unittest.TestCase):
def test_load_crpa_interaction_matrix(self):
sum_k = Dummy()
sum_k.n_inequiv_shells = 2
sum_k.n_corr_shells = 4
sum_k.corr_shells = [{'dim': 5}, {'dim': 5}, {'dim': 5}, {'dim': 5}]
sum_k.corr_to_inequiv = [0, 1, 1, 0]
sum_k.inequiv_to_corr = [0, 1]
sum_k.gf_struct_solver = [{'down_0' : 5, 'up_0' : 5},{'down_0' : 5, 'up_0' : 5}]

assert [c.shape for c in crpa_matrix] == [(5, 5, 5, 5), (5, 5, 5, 5)]
crpa_matrix = _load_crpa_interaction_matrix(sum_k, 'UIJKL')

# Warning: these assert were just taken from the current implementation
# and might be wrong if the implementation is buggy
assert np.isclose(np.max(crpa_matrix), 2.5363308, rtol=0, atol=1e-6)
assert np.isclose(np.min(crpa_matrix), -0.1894974, rtol=0, atol=1e-6)
assert np.isclose(np.min(np.abs(crpa_matrix)), 7.76e-05, rtol=0, atol=1e-6)
assert [c.shape for c in crpa_matrix] == [(5, 5, 5, 5), (5, 5, 5, 5)]

# Warning: these assert were just taken from the current implementation
# and might be wrong if the implementation is buggy
assert np.isclose(np.max(crpa_matrix), 2.5363308, rtol=0, atol=1e-6)
assert np.isclose(np.min(crpa_matrix), -0.1894974, rtol=0, atol=1e-6)
assert np.isclose(np.min(np.abs(crpa_matrix)), 7.76e-05, rtol=0, atol=1e-6)

if __name__ == '__main__':
unittest.main()
Loading

0 comments on commit a8d4239

Please sign in to comment.