Skip to content

Commit

Permalink
add tests for coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
jhdark committed Feb 20, 2025
1 parent 9e4e8db commit aac0a52
Showing 1 changed file with 199 additions and 1 deletion.
200 changes: 199 additions & 1 deletion test/test_helpers.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
from mpi4py import MPI

import basix
import dolfinx.mesh
import numpy as np
import pytest
import ufl
Expand All @@ -8,6 +12,15 @@
test_mesh = F.Mesh1D(vertices=np.array([0.0, 1.0, 2.0, 3.0, 4.0]))
x = ufl.SpatialCoordinate(test_mesh.mesh)

element_CG = basix.ufl.element(
basix.ElementFamily.P,
test_mesh.mesh.basix_cell(),
1,
basix.LagrangeVariant.equispaced,
)
test_function_space = fem.functionspace(test_mesh.mesh, element_CG)
test_function = fem.Function(test_function_space)


@pytest.mark.parametrize(
"value",
Expand All @@ -22,8 +35,193 @@
def test_temperature_type_and_processing(value):
"""Test that the temperature type is correctly set"""

if not isinstance(value, (fem.Constant, int, float)):
if not isinstance(value, fem.Constant | int | float):
with pytest.raises(TypeError):
F.as_fenics_constant(value, test_mesh.mesh)
else:
assert isinstance(F.as_fenics_constant(value, test_mesh.mesh), fem.Constant)


@pytest.mark.parametrize(
"input_value, expected_output_type", [(1.0, fem.Constant), (3, fem.Constant)]
)
def test_value_convert_float_int_inputs(input_value, expected_output_type):
"""Test that float and value is correctly converted"""

mesh = dolfinx.mesh.create_unit_interval(MPI.COMM_WORLD, 10)

test_value = F.Value(input_value)

test_value.convert_input_value(mesh=mesh)

assert isinstance(test_value.fenics_object, expected_output_type)


@pytest.mark.parametrize(
"input_value, expected_output_type",
[
(lambda t: t, fem.Constant),
(lambda t: 1.0 + t, fem.Constant),
(lambda x: 1.0 + x[0], ufl.core.expr.Expr),
(lambda x, t: 1.0 + x[0] + t, ufl.core.expr.Expr),
(lambda x, t, T: 1.0 + x[0] + t + T, ufl.core.expr.Expr),
(
lambda x, t: ufl.conditional(ufl.lt(t, 1.0), 100.0 + x[0], 0.0),
ufl.core.expr.Expr,
),
],
)
def test_value_convert_up_to_ufl_inputs(input_value, expected_output_type):
"""Test that float and value is correctly converted"""

my_mesh = dolfinx.mesh.create_unit_interval(MPI.COMM_WORLD, 10)
my_t = fem.Constant(my_mesh, default_scalar_type(10))
my_T = fem.Constant(my_mesh, default_scalar_type(3))

test_value = F.Value(input_value)

test_value.convert_input_value(
mesh=my_mesh,
t=my_t,
temperature=my_T,
up_to_ufl_expr=True,
)

assert isinstance(test_value.fenics_object, expected_output_type)


@pytest.mark.parametrize(
"input_value, expected_output_type",
[
(lambda t: t, fem.Constant),
(lambda t: 1.0 + t, fem.Constant),
(lambda x: 1.0 + x[0], fem.Function),
(lambda x, t: 1.0 + x[0] + t, fem.Function),
(lambda x, t, T: 1.0 + x[0] + t + T, fem.Function),
(
lambda x, t: ufl.conditional(ufl.lt(t, 1.0), 100.0 + x[0], 0.0),
ufl.core.expr.Expr,
),
],
)
def test_value_convert_callable_inputs(input_value, expected_output_type):
"""Test that float and value is correctly converted"""

my_mesh = dolfinx.mesh.create_unit_interval(MPI.COMM_WORLD, 12)
my_t = fem.Constant(my_mesh, default_scalar_type(8))
my_T = fem.Constant(my_mesh, default_scalar_type(5))

element_CG = basix.ufl.element(
basix.ElementFamily.P,
my_mesh.basix_cell(),
1,
basix.LagrangeVariant.equispaced,
)
my_function_space = fem.functionspace(my_mesh, element_CG)

test_value = F.Value(input_value)

test_value.convert_input_value(
function_space=my_function_space,
mesh=my_mesh,
t=my_t,
temperature=my_T,
)

assert isinstance(test_value.fenics_object, expected_output_type)


def test_error_raised_wehn_input_value_is_not_accepted():
"""Test that an error is raised when the input value is not accepted"""

with pytest.raises(
TypeError,
match="Value must be a float, int, fem.Constant, fem.Function, or callable",
):
F.Value("coucou")


@pytest.mark.parametrize(
"input_value, expected_output",
[
(1.0, False),
(None, False),
(fem.Constant(test_mesh.mesh, default_scalar_type(1.0)), False),
(lambda t: t, True),
(lambda t: 1.0 + t, True),
(lambda x: 1.0 + x[0], False),
(lambda x, t: 1.0 + x[0] + t, True),
(lambda x, t, T: 1.0 + x[0] + t + T, True),
(
lambda x, t: ufl.conditional(ufl.lt(t, 1.0), 100.0 + x[0], 0.0),
True,
),
],
)
def test_time_dependent_values(input_value, expected_output):
"""Test that the time_dependent attribute is correctly set"""

test_value = F.Value(input_value)

assert test_value.time_dependent == expected_output


@pytest.mark.parametrize(
"input_value, expected_output",
[
(1.0, False),
(None, False),
(fem.Constant(test_mesh.mesh, default_scalar_type(1.0)), False),
(lambda t: t, False),
(lambda T: 1.0 + T, True),
(lambda x: 1.0 + x[0], False),
(lambda x, t: 1.0 + x[0] + t, False),
(lambda x, t, T: 1.0 + x[0] + t + T, True),
(
lambda T, t: ufl.conditional(ufl.lt(t, 1.0), 100.0 + T[0], 0.0),
True,
),
],
)
def test_temperature_dependent_values(input_value, expected_output):
"""Test that the time_dependent attribute is correctly set"""

test_value = F.Value(input_value)

assert test_value.temperature_dependent == expected_output


@pytest.mark.parametrize(
"value",
[
fem.Constant(test_mesh.mesh, default_scalar_type(1.0)),
test_function,
],
)
def test_input_values_of_constants_and_functions_are_accepted(value):
"""Test that the input values of constants and functions are accepted"""

test_value = F.Value(value)

test_value.convert_input_value()

assert test_value.fenics_object == value


def test_ValueError_raised_when_callable_returns_wrong_type():
"""The create_value_fenics method should raise a ValueError when the callable
returns an object which is not a float or int"""

def my_value(t):
return ufl.conditional(ufl.lt(t, 0.5), 100, 0)

test_value = F.Value(my_value)

T = fem.Constant(test_mesh.mesh, 550.0)
t = fem.Constant(test_mesh.mesh, 0.0)

with pytest.raises(
ValueError,
match="self.value should return a float or an int, not <class 'ufl.conditional.Conditional'",
):
test_value.convert_input_value(mesh=test_mesh.mesh, temperature=T, t=t)

0 comments on commit aac0a52

Please # to comment.