Skip to content

Commit

Permalink
Reduced test_permittivity.py for simplicity for debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
JosePizarro3 committed May 28, 2024
1 parent ba1d47d commit fcaebfb
Showing 1 changed file with 86 additions and 73 deletions.
159 changes: 86 additions & 73 deletions tests/test_permittivity.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
from nomad_simulations.properties import Permittivity
from nomad_simulations.variables import Variables, KMesh, Frequency

from .conftest import generate_k_space_simulation


class TestPermittivity:
"""
Expand All @@ -49,85 +51,96 @@ def test_default_quantities(self):
assert permittivity.rank == [3, 3]

@pytest.mark.parametrize(
'variables, result',
[
(None, 'static'),
([], 'static'),
([KMesh()], 'static'),
([KMesh(), Frequency()], 'dynamic'),
],
)
def test_resolve_type(self, variables: Optional[List[Variables]], result: str):
"""
Test the `resolve_type` method.
"""
permittivity = Permittivity()
if variables is not None:
permittivity.variables = [var for var in variables]
assert permittivity.resolve_type() == result

@pytest.mark.parametrize(
'variables, value, result',
'kmesh_grid, variables, result',
[
# Empty case
(None, None, None),
# No `variables`
([], np.eye(3) * (1 + 1j), None),
# If `variables` contain `KMesh`, we cannot extract absorption spectra
(
[KMesh(points=[[1, 1, 1], [2, 2, 2], [3, 3, 3], [4, 4, 4]])],
np.array([np.eye(3) * k_point * (1 + 1j) for k_point in range(1, 5)]),
None,
),
# Even if `variables` contain `Frequency`, we cannot extract absorption spectra if `value` depends on `KMesh`
(
[
Frequency(points=[0, 1, 2, 3, 4]),
KMesh(points=[[1, 1, 1], [2, 2, 2], [3, 3, 3], [4, 4, 4]]),
],
np.array(
[
[
np.eye(3) * k_point * (1 + 1j)
+ np.eye(3) * freq_point * 0.5j
for k_point in range(1, 5)
]
for freq_point in range(5)
]
),
None,
),
# Valid case: `value` does not depend on `KMesh` and we can extract absorption spectra
(
[
Frequency(points=[0, 1, 2, 3, 4]),
],
np.array([np.eye(3) * freq_point * 0.5j for freq_point in range(5)]),
[0.0, 0.5, 1.0, 1.5, 2.0],
),
(None, None, 'static'),
([], None, 'static'),
([3, 3, 3], None, 'static'),
([3, 3, 3], [Frequency()], 'dynamic'),
],
)
def test_extract_absorption_spectra(
def test_resolve_type(
self,
kmesh_grid: Optional[List[int]],
variables: Optional[List[Variables]],
value: Optional[np.ndarray],
result: Optional[List[float]],
result: str,
):
"""
Test the `extract_absorption_spectra` method. The `result` in the last valid case corresponds to the imaginary part of
the diagonal of the `Permittivity.value` for each frequency point.
Test the `resolve_type` method.
"""
simulation = generate_k_space_simulation(grid=kmesh_grid)
k_mesh = simulation.model_method[0].numerical_settings[0].k_mesh[0]
k_mesh.points, _ = k_mesh.resolve_points_and_offset(logger)
permittivity = Permittivity()
if variables is not None:
permittivity.variables = [var for var in variables]
permittivity.value = value
absorption_spectra = permittivity.extract_absorption_spectra(logger)
if absorption_spectra is not None:
assert len(absorption_spectra) == 3
spectrum = absorption_spectra[1]
assert spectrum.rank == []
assert spectrum.axis == 'yy'
assert len(spectrum.value) == len(variables[0].points)
assert np.allclose(spectrum.value, result)
else:
assert absorption_spectra == result
if kmesh_grid is not None and len(kmesh_grid) > 0:
kmesh_variables = KMesh(points=k_mesh)
permittivity.variables.append(kmesh_variables)
if variables is not None and len(variables) > 0:
permittivity.variables.append(variables)
assert permittivity.resolve_type() == result

# @pytest.mark.parametrize(
# 'variables, value, result',
# [
# # Empty case
# (None, None, None),
# # No `variables`
# ([], np.eye(3) * (1 + 1j), None),
# # If `variables` contain `KMesh`, we cannot extract absorption spectra
# (
# [KMesh(points=[[1, 1, 1], [2, 2, 2], [3, 3, 3], [4, 4, 4]])],
# np.array([np.eye(3) * k_point * (1 + 1j) for k_point in range(1, 5)]),
# None,
# ),
# # Even if `variables` contain `Frequency`, we cannot extract absorption spectra if `value` depends on `KMesh`
# (
# [
# Frequency(points=[0, 1, 2, 3, 4]),
# KMesh(points=[[1, 1, 1], [2, 2, 2], [3, 3, 3], [4, 4, 4]]),
# ],
# np.array(
# [
# [
# np.eye(3) * k_point * (1 + 1j)
# + np.eye(3) * freq_point * 0.5j
# for k_point in range(1, 5)
# ]
# for freq_point in range(5)
# ]
# ),
# None,
# ),
# # Valid case: `value` does not depend on `KMesh` and we can extract absorption spectra
# (
# [
# Frequency(points=[0, 1, 2, 3, 4]),
# ],
# np.array([np.eye(3) * freq_point * 0.5j for freq_point in range(5)]),
# [0.0, 0.5, 1.0, 1.5, 2.0],
# ),
# ],
# )
# def test_extract_absorption_spectra(
# self,
# variables: Optional[List[Variables]],
# value: Optional[np.ndarray],
# result: Optional[List[float]],
# ):
# """
# Test the `extract_absorption_spectra` method. The `result` in the last valid case corresponds to the imaginary part of
# the diagonal of the `Permittivity.value` for each frequency point.
# """
# permittivity = Permittivity()
# if variables is not None:
# permittivity.variables = [var for var in variables]
# permittivity.value = value
# absorption_spectra = permittivity.extract_absorption_spectra(logger)
# if absorption_spectra is not None:
# assert len(absorption_spectra) == 3
# spectrum = absorption_spectra[1]
# assert spectrum.rank == []
# assert spectrum.axis == 'yy'
# assert len(spectrum.value) == len(variables[0].points)
# assert np.allclose(spectrum.value, result)
# else:
# assert absorption_spectra == result

0 comments on commit fcaebfb

Please sign in to comment.