diff --git a/coverage-report.pdf b/coverage-report.pdf new file mode 100644 index 0000000..f42160b Binary files /dev/null and b/coverage-report.pdf differ diff --git a/diffusion2d.py b/diffusion2d.py index 51a07f2..17bc002 100644 --- a/diffusion2d.py +++ b/diffusion2d.py @@ -38,6 +38,10 @@ def __init__(self): self.dt = None def initialize_domain(self, w=10., h=10., dx=0.1, dy=0.1): + assert isinstance(w, float), "w must be a float" + assert isinstance(h, float), "h must be a float" + assert isinstance(dx, float), "dx must be a float" + assert isinstance(dy, float), "dy must be a float" self.w = w self.h = h self.dx = dx @@ -45,7 +49,10 @@ def initialize_domain(self, w=10., h=10., dx=0.1, dy=0.1): self.nx = int(w / dx) self.ny = int(h / dy) - def initialize_physical_parameters(self, d=4., T_cold=300, T_hot=700): + def initialize_physical_parameters(self, d=4., T_cold=300., T_hot=700.): + assert isinstance(d, float), "d must be a float" + assert isinstance(T_cold, float), "T_cold must be a float" + assert isinstance(T_hot, float), "T_hot must be a float" self.D = d self.T_cold = T_cold self.T_hot = T_hot diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..7851745 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,3 @@ +pytest +numpy +matplotlib \ No newline at end of file diff --git a/tests/integration/test_diffusion2d.py b/tests/integration/test_diffusion2d.py index fd026b4..9278aae 100644 --- a/tests/integration/test_diffusion2d.py +++ b/tests/integration/test_diffusion2d.py @@ -3,7 +3,8 @@ """ from diffusion2d import SolveDiffusion2D - +import pytest +import numpy as np def test_initialize_physical_parameters(): """ @@ -11,9 +12,42 @@ def test_initialize_physical_parameters(): """ solver = SolveDiffusion2D() + expected_dt = 0.0005 + + solver.initialize_domain(10., 10., 0.1, 0.1) + solver.initialize_physical_parameters(5.,350.,750.) + + assert solver.dt == pytest.approx(expected_dt, abs=1e-4) + def test_set_initial_condition(): """ Checks function SolveDiffusion2D.get_initial_function """ solver = SolveDiffusion2D() + + w = 20. + h = 20. + dx = 0.2 + dy = 0.2 + nx = int(w / dx) + ny = int(h / dy) + T_cold = 350. + T_hot = 750. + + u = T_cold * np.ones((nx, ny)) + + # Initial conditions - circle of radius r centred at (cx,cy) (mm) + r, cx, cy = 2, 5, 5 + r2 = r ** 2 + for i in range(nx): + for j in range(ny): + p2 = (i * dx - cx) ** 2 + (j * dy - cy) ** 2 + if p2 < r2: + u[i, j] = T_hot + + solver.initialize_domain(w, h, dx, dy) + solver.initialize_physical_parameters(5., 350., 750.) + solver_u = solver.set_initial_condition() + + assert solver_u == pytest.approx(u, abs=1e-4) diff --git a/tests/unit/test_diffusion2d_functions.py b/tests/unit/test_diffusion2d_functions.py index c4277ff..7fb6507 100644 --- a/tests/unit/test_diffusion2d_functions.py +++ b/tests/unit/test_diffusion2d_functions.py @@ -1,6 +1,8 @@ """ Tests for functions in class SolveDiffusion2D """ +import numpy as np +import pytest from diffusion2d import SolveDiffusion2D @@ -10,6 +12,17 @@ def test_initialize_domain(): Check function SolveDiffusion2D.initialize_domain """ solver = SolveDiffusion2D() + w = 20. + h = 20. + dx=0.2 + dy=0.2 + + expected_nx = 100 + expected_ny = 100 + + solver.initialize_domain(w,h,dx,dy) + assert solver.nx == expected_nx, "value of nx is wrong, did the calculation change?" + assert solver.ny == expected_ny, "value of ny is wrong, did the calculation change?" def test_initialize_physical_parameters(): @@ -18,9 +31,53 @@ def test_initialize_physical_parameters(): """ solver = SolveDiffusion2D() + d = 5. + T_cold = 350. + T_hot = 750. + solver.dx = 0.1 + solver.dy = 0.1 + + expected_dt = 0.0005 + + solver.initialize_physical_parameters(d, T_cold, T_hot) + assert solver.dt == pytest.approx(expected_dt, abs=1e-4) + def test_set_initial_condition(): """ Checks function SolveDiffusion2D.get_initial_function """ solver = SolveDiffusion2D() + + w = 20. + h = 20. + dx = 0.2 + dy = 0.2 + nx = int(w / dx) + ny = int(h / dy) + T_cold = 350. + T_hot = 750. + + u = T_cold * np.ones((nx, ny)) + + # Initial conditions - circle of radius r centred at (cx,cy) (mm) + r, cx, cy = 2, 5, 5 + r2 = r ** 2 + for i in range(nx): + for j in range(ny): + p2 = (i * dx - cx) ** 2 + (j * dy - cy) ** 2 + if p2 < r2: + u[i, j] = T_hot + + solver.T_cold = T_cold + solver.T_hot = T_hot + solver.w = w + solver.h = h + solver.dx = dx + solver.dy = dy + solver.nx = nx + solver.ny = ny + + solver_u = solver.set_initial_condition() + + assert solver_u == pytest.approx(u, abs=1e-4) diff --git a/tox.toml b/tox.toml new file mode 100644 index 0000000..e7f4fac --- /dev/null +++ b/tox.toml @@ -0,0 +1,7 @@ +requires = ["tox>=4"] +env_list = ["pytest_testing"] + +[env.pytest_testing] +description = "Run pytest" +deps = ["-rrequirements.txt"] +commands = [["python", "-m", "pytest"]] \ No newline at end of file