diff --git a/coverage-report.pdf b/coverage-report.pdf new file mode 100644 index 0000000..080c203 Binary files /dev/null and b/coverage-report.pdf differ diff --git a/diffusion2d.py b/diffusion2d.py index 51a07f2..c49a76c 100644 --- a/diffusion2d.py +++ b/diffusion2d.py @@ -38,6 +38,9 @@ def __init__(self): self.dt = None def initialize_domain(self, w=10., h=10., dx=0.1, dy=0.1): + for name, value in {"w": w, "h": h, "dx": dx, "dy": dy}.items(): + assert isinstance(value, float), f"{name} must be of type float, got {type(value).__name__}." + self.w = w self.h = h self.dx = dx @@ -45,7 +48,9 @@ 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.0, T_hot=700.0): + for name, value in {"d": d, "T_cold": T_cold, "T_hot": T_hot}.items(): + assert isinstance(value, float), f"{name} must be of type float, got {type(value).__name__}." 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..8f87fb4 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,4 @@ +numpy +matplotlib +pytest +coverage \ No newline at end of file diff --git a/tests/integration/test_diffusion2d.py b/tests/integration/test_diffusion2d.py index fd026b4..3296771 100644 --- a/tests/integration/test_diffusion2d.py +++ b/tests/integration/test_diffusion2d.py @@ -3,17 +3,33 @@ """ from diffusion2d import SolveDiffusion2D - +import pytest def test_initialize_physical_parameters(): """ Checks function SolveDiffusion2D.initialize_domain """ solver = SolveDiffusion2D() + w, h, dx, dy = 10.0, 10.0, 0.1, 0.1 + d = 4.0 + expected_dt = 0.000625 + + solver.initialize_domain(w, h, dx, dy) + solver.initialize_physical_parameters(d=d) + + assert solver.dt == pytest.approx(expected_dt) def test_set_initial_condition(): """ - Checks function SolveDiffusion2D.get_initial_function + Checks function SolveDiffusion2D.set_initial_condition """ solver = SolveDiffusion2D() + solver.initialize_domain(w=10.0, h=10.0, dx=0.1, dy=0.1) + solver.initialize_physical_parameters(d=4.0, T_cold=300.0, T_hot=700.0) + + u = solver.set_initial_condition() + + assert u.shape == (100, 100) + assert u[50, 50] == 700.0 + assert u[0, 0] == 300.0 \ No newline at end of file diff --git a/tests/unit/test_diffusion2d_functions.py b/tests/unit/test_diffusion2d_functions.py index c4277ff..493d674 100644 --- a/tests/unit/test_diffusion2d_functions.py +++ b/tests/unit/test_diffusion2d_functions.py @@ -3,24 +3,46 @@ """ from diffusion2d import SolveDiffusion2D +import pytest +import numpy as np - -def test_initialize_domain(): +def test_initialize_physical_parameters(): """ - Check function SolveDiffusion2D.initialize_domain + Checks function SolveDiffusion2D.initialize_physical_parameters """ solver = SolveDiffusion2D() + w, h, dx, dy = 10.0, 10.0, 0.1, 0.1 + d = 4.0 + expected_dt = 0.000625 + + solver.initialize_domain(w, h, dx, dy) + solver.initialize_physical_parameters(d=d) -def test_initialize_physical_parameters(): - """ - Checks function SolveDiffusion2D.initialize_domain - """ - solver = SolveDiffusion2D() + assert solver.dt == pytest.approx(expected_dt) def test_set_initial_condition(): """ - Checks function SolveDiffusion2D.get_initial_function + Checks function SolveDiffusion2D.set_initial_condition """ solver = SolveDiffusion2D() + + w, h, dx, dy = 10.0, 10.0, 0.1, 0.1 + T_cold = 300.0 + T_hot = 700.0 + solver.initialize_domain(w, h, dx, dy) + solver.initialize_physical_parameters(T_cold=T_cold, T_hot=T_hot) + + nx, ny = 100, 100 + expected_u = T_cold * np.ones((nx, ny)) + 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: + expected_u[i, j] = T_hot + + actual_u = solver.set_initial_condition() + assert np.allclose(actual_u, expected_u) \ No newline at end of file diff --git a/tox.toml b/tox.toml new file mode 100644 index 0000000..72e0fc8 --- /dev/null +++ b/tox.toml @@ -0,0 +1,7 @@ +requires = ["tox>=4"] +env_list = ["pytest_testing"] + +[env.pytest_testing] +description = "Run pytest" +deps = ["-r requirements.txt"] +commands = [["python", "-m", "pytest"]] \ No newline at end of file