Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Small refactoring of tests #726

Merged
merged 5 commits into from
Apr 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion test/h_transport_problem/test_initialisation.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,18 @@
import pytest
import fenics
from pathlib import Path
import os


def test_initialisation_from_xdmf(tmpdir):
"""
Test that initialise_solutions interpolates correctly
from an XDMF-file

Args:
tmpdir (os.PathLike): path to the pytest temporary folder
"""

mesh = fenics.UnitSquareMesh(5, 5)
V = fenics.VectorFunctionSpace(mesh, "P", 1, 2)
u = fenics.Function(V)
Expand All @@ -19,7 +28,6 @@ def test_initialisation_from_xdmf(tmpdir):
d = tmpdir.mkdir("Initial solutions")
file1 = d.join("u_1out.xdmf")
file2 = d.join("u_2out.xdmf")
print(Path(file1))
with fenics.XDMFFile(str(Path(file1))) as f:
f.write_checkpoint(
u.sub(0), "1", 2, fenics.XDMFFile.Encoding.HDF5, append=False
Expand Down
7 changes: 5 additions & 2 deletions test/h_transport_problem/test_solving.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ def test_default_dt_min_value():

t = 0
dt = festim.Stepsize(
initial_value=0.5, stepsize_change_ratio=1.1, t_stop=430, stepsize_stop_max=0.5
initial_value=0.5,
stepsize_change_ratio=1.1,
max_stepsize=None if t < 430 else 0.5,
)

my_settings = festim.Settings(
Expand All @@ -32,7 +34,8 @@ def test_default_dt_min_value():
my_problem.F = f.dot(f.grad(my_problem.u), f.grad(my_problem.v)) * f.dx

du = f.TrialFunction(my_problem.u.function_space())
my_problem.J = f.derivative(my_problem.F, my_problem.u, du) # Define the Jacobian
# Define the Jacobian
my_problem.J = f.derivative(my_problem.F, my_problem.u, du)
# run & test
my_problem.update(t, dt)

Expand Down
95 changes: 38 additions & 57 deletions test/simulation/test_construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,85 +2,66 @@
import pytest


def test_setting_traps():
"""Checks traps can be set with the expected types (F.Trap, list, or F.Traps)"""
class TestSettingTrapsMaterialsExprots:
my_sim = F.Simulation()
my_mat = F.Material(1, 1, 0)
trap1 = F.Trap(1, 1, 1, 1, [my_mat], density=1)
trap2 = F.Trap(2, 2, 2, 2, [my_mat], density=1)

combinations = [trap1, [trap1], [trap1, trap2], F.Traps([trap1, trap2])]

for combination in combinations:
my_sim.traps = combination
mat1 = F.Material(1, 1, 1)
mat2 = F.Material(2, 2, 2)
trap1 = F.Trap(1, 1, 1, 1, [mat1], density=1)
trap2 = F.Trap(2, 2, 2, 2, [mat1], density=1)
export1 = F.XDMFExport("solute")
export2 = F.XDMFExport("solute")
wrong_types = ["coucou", True]

@pytest.mark.parametrize(
"trap", [trap1, [trap1], [trap1, trap2], F.Traps([trap1, trap2])]
)
def test_setting_traps(self, trap):
"""Checks traps can be set with the expected types (F.Trap, list, or F.Traps)"""

def test_setting_traps_wrong_type():
"""Checks an error is raised when traps is set with the wrong type"""
my_sim = F.Simulation()
self.my_sim.traps = trap

combinations = ["coucou", True]
@pytest.mark.parametrize("trap", wrong_types)
def test_setting_traps_wrong_type(self, trap):
"""Checks an error is raised when traps is set with the wrong type"""

for combination in combinations:
with pytest.raises(
TypeError,
match="Accepted types for traps are list, festim.Traps or festim.Trap",
):
my_sim.traps = combination


def test_setting_materials():
"""Checks materials can be set with the expected types (F.Material, list, or F.Materials)"""
my_sim = F.Simulation()
mat1 = F.Material(1, 1, 1)
mat2 = F.Material(2, 2, 2)
self.my_sim.traps = trap

combinations = [mat1, [mat1], [mat1, mat2], F.Materials([mat1, mat2])]
@pytest.mark.parametrize(
"mat", [mat1, [mat1], [mat1, mat2], F.Materials([mat1, mat2])]
)
def test_setting_materials(self, mat):
"""Checks materials can be set with the expected types (F.Material, list, or F.Materials)"""

for combination in combinations:
my_sim.materials = combination
self.my_sim.materials = mat

@pytest.mark.parametrize("mat", wrong_types)
def test_setting_materials_wrong_type(self, mat):
"""Checks an error is raised when materials is set with the wrong type"""

def test_setting_materials_wrong_type():
"""Checks an error is raised when materials is set with the wrong type"""
my_sim = F.Simulation()

combinations = ["coucou", True]

for combination in combinations:
with pytest.raises(
TypeError,
match="accepted types for materials are list, festim.Material or festim.Materials",
):
my_sim.materials = combination
self.my_sim.materials = mat

@pytest.mark.parametrize(
"exp", [export1, [export1], [export1, export2], F.Exports([export1, export2])]
)
def test_setting_exports(self, exp):
"""Checks exports can be set with the expected types (F.Material, list, or F.Exports)"""

def test_setting_exports():
"""Checks exports can be set with the expected types (F.Material, list, or F.Exports)"""
my_sim = F.Simulation()
export1 = F.XDMFExport("solute")
export2 = F.XDMFExport("solute")

combinations = [
export1,
[export1],
[export1, export2],
F.Exports([export1, export2]),
]

for trap_combination in combinations:
my_sim.exports = trap_combination


def test_setting_exports_wrong_type():
"""Checks an error is raised when exports is set with the wrong type"""
my_sim = F.Simulation()
self.my_sim.exports = exp

combinations = ["coucou", True]
@pytest.mark.parametrize("exp", wrong_types)
def test_setting_exports_wrong_type(self, exp):
"""Checks an error is raised when exports is set with the wrong type"""

for trap_combination in combinations:
with pytest.raises(
TypeError,
match="accepted types for exports are list, festim.Export or festim.Exports",
):
my_sim.exports = trap_combination
self.my_sim.exports = exp
10 changes: 9 additions & 1 deletion test/simulation/test_initialise.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ def test_TXTExport_times_added_to_milestones(tmpdir):
"""Creates a Simulation object and checks that, if no dt.milestones
are given and TXTExport.times are given, TXTExport.times are
are added to dt.milestones by .initialise()

Args:
tmpdir (os.PathLike): path to the pytest temporary folder
"""
# tmpdir
d = tmpdir.mkdir("test_folder")
Expand Down Expand Up @@ -120,8 +123,13 @@ def test_TXTExport_times_added_to_milestones(tmpdir):
)
@pytest.mark.parametrize("sys", ["cylindrical", "spherical"])
def test_cartesian_and_surface_flux_warning(quantity, sys):
"""Creates a Simulation object and checks that, if either a cylindrical
"""
Creates a Simulation object and checks that, if either a cylindrical
or spherical meshes are given with a SurfaceFlux, a warning is raised.

Args:
quantity (festim.DerivedQuantity): a festim.DerivedQuantity object
sys (str): type of the coordinate system
"""
# build
my_model = F.Simulation()
Expand Down
3 changes: 3 additions & 0 deletions test/simulation/test_meshing.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
def test_define_markers(tmpdir):
"""Checks that markers can be defined from XDMF files and that the mesh
functions have the correct values.

Args:
tmpdir (os.PathLike): path to the pytest temporary folder
"""
# build
mesh = fenics.UnitSquareMesh(16, 16)
Expand Down
22 changes: 20 additions & 2 deletions test/simulation/test_postprocessing_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
class TestPostProcessing:
@pytest.fixture
def my_sim(self):
"""A pytest fixture defining the festim.Simulation object"""
my_sim = festim.Simulation({})
my_sim.t = 0
my_sim.mesh = festim.MeshFromRefinements(10, 1)
Expand Down Expand Up @@ -42,6 +43,13 @@ def my_sim(self):
return my_sim

def test_derived_quantities_size(self, my_sim):
"""
Checks that the length of data produced by F.DerivedQuantities
equals to the number of post-processing calls

Args:
my_sim (festim.Simulation): the simulation object
"""
derived_quantities = festim.DerivedQuantities(
[
festim.SurfaceFlux("solute", 1),
Expand All @@ -64,6 +72,12 @@ def test_derived_quantities_size(self, my_sim):
assert my_sim.exports[0].data[i][0] == t

def test_pure_diffusion(self, my_sim):
"""
Checks that run_post_processing() assigns data correctly

Args:
my_sim (festim.Simulation): the simulation object
"""
my_sim.materials = festim.Materials(
[
festim.Material(1, D_0=1, E_D=1, borders=[0, 0.5]),
Expand Down Expand Up @@ -164,6 +178,10 @@ def test_performance_xdmf_export_every_N_iterations(self, my_sim, tmpdir):
"""Runs run_post_processing several times with different export.mode
values and checks that the xdmf
files have the correct timesteps

Args:
my_sim (festim.Simulation): a festim.Simulation object
tmpdir (os.PathLike): path to the pytest temporary folder
"""
# build
d = tmpdir.mkdir("test_folder")
Expand Down Expand Up @@ -205,8 +223,8 @@ def test_xdmf_export_only_last_timestep(self, my_sim, tmpdir):
correct timesteps

Args:
my_sim (_type_): _description_
tmpdir (_type_): _description_
my_sim (festim.Simulation): the simulation object
tmpdir (os.PathLike): path to the pytest temporary folder
"""
d = tmpdir.mkdir("test_folder")

Expand Down
13 changes: 12 additions & 1 deletion test/system/test_chemical_potential.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,16 @@


def compute_error(exact, computed, t, norm):
exact_sol = fenics.Expression(sp.printing.ccode(exact), degree=4, t=t)
"""
An auxiliary method to compute the error

Args:
exact (sympy.Expr): exact solution
computed (fenics.Function): computed solution
t (float): simulation time
norm (str): type of norm (maximum absolute error or L2 norm)
"""
exact_sol = fenics.Expression(sp.printing.ccode(exact), degree=4, t=t)
if norm == "error_max":
mesh = computed.function_space().mesh()
vertex_values_u = computed.compute_vertex_values(mesh)
Expand All @@ -24,6 +32,9 @@ def test_run_MMS_chemical_pot(tmpdir):
"""
Test function run() with conservation of chemical potential henry
(1 material)

Args:
tmpdir (os.PathLike): path to the pytest temporary folder
"""
d = tmpdir.mkdir("Solution_Test")
u = 1 + sp.sin(2 * fenics.pi * festim.x) * festim.t + festim.t
Expand Down
22 changes: 20 additions & 2 deletions test/system/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@
import os


def test_convective_flux():
def test_convective_flux(tmpdir):
"""
Tests that convective boundary condition works correctly

Args:
tmpdir (os.PathLike): path to the pytest temporary folder
"""
sim = F.Simulation()

sim.mesh = F.MeshFromVertices(np.linspace(0, 1, num=50))
Expand All @@ -19,7 +25,13 @@ def test_convective_flux():

sim.materials = F.Materials([F.Material(1, D_0=1, E_D=0, thermal_cond=2)])

sim.exports = F.Exports([F.XDMFExport("T", checkpoint=False)])
sim.exports = F.Exports(
[
F.XDMFExport(
"T", checkpoint=False, filename="{}/temperature.xdmf".format(tmpdir)
)
]
)

sim.settings = F.Settings(1e-10, 1e-10, transient=False)

Expand Down Expand Up @@ -186,6 +198,9 @@ def test_txt_export_desired_times(tmp_path, final_time, stepsize, export_times):
def test_txt_export_all_times(tmp_path):
"""
Tests that TXTExport can be exported at all timesteps

Args:
tmp_path (os.PathLike): path to a temporary folder
"""
my_model = F.Simulation()

Expand Down Expand Up @@ -214,6 +229,9 @@ def test_txt_export_all_times(tmp_path):
def test_txt_export_steady_state(tmp_path):
"""
Tests that TXTExport can be exported in steady state

Args:
tmp_path (os.PathLike): path to a temporary folder
"""
my_model = F.Simulation()

Expand Down
Loading
Loading