diff --git a/coverage-report.pdf b/coverage-report.pdf new file mode 100644 index 0000000..cfc3b5b Binary files /dev/null and b/coverage-report.pdf differ diff --git a/diffusion2d.py b/diffusion2d.py index 51a07f2..0d0448e 100644 --- a/diffusion2d.py +++ b/diffusion2d.py @@ -38,6 +38,11 @@ def __init__(self): self.dt = None def initialize_domain(self, w=10., h=10., dx=0.1, dy=0.1): + assert isinstance(w, float) + assert isinstance(h, float) + assert isinstance(dx, float) + assert isinstance(dy, float) + self.w = w self.h = h self.dx = dx @@ -45,7 +50,11 @@ 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) + assert isinstance(T_cold, float) + assert isinstance(T_hot, 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..021aae7 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,4 @@ +numpy +matplotlib +pytest +coverage diff --git a/tests/integration/test_diffusion2d.py b/tests/integration/test_diffusion2d.py index fd026b4..9e0dc85 100644 --- a/tests/integration/test_diffusion2d.py +++ b/tests/integration/test_diffusion2d.py @@ -1,19 +1,84 @@ """ Tests for functionality checks in class SolveDiffusion2D """ - +import numpy as np from diffusion2d import SolveDiffusion2D def test_initialize_physical_parameters(): """ - Checks function SolveDiffusion2D.initialize_domain + Integration test: initialize_domain + initialize_physical_parameters + Check computed dt matches the expected formula along with expected values from previous funcations. """ solver = SolveDiffusion2D() + w, h = 12.0, 7.5 + dx, dy = 0.3, 0.5 + D = 3.0 + T_cold, T_hot = 250.0, 900.0 + + solver.initialize_domain(w=w, h=h, dx=dx, dy=dy) + solver.initialize_physical_parameters(d=D, T_cold=T_cold, T_hot=T_hot) + + expected_nx = int(w / dx) + expected_ny = int(h / dy) + dx2 = dx * dx + dy2 = dy * dy + expected_dt = dx2 * dy2 / (2.0 * D * (dx2 + dy2)) + + assert solver.w == w + assert solver.h == h + assert solver.dx == dx + assert solver.dy == dy + assert solver.nx == expected_nx + assert solver.ny == expected_ny + assert solver.T_cold == T_cold + assert solver.T_hot == T_hot + assert solver.D == D + assert solver.dt == expected_dt + + def test_set_initial_condition(): """ - Checks function SolveDiffusion2D.get_initial_function + Integration test: initialize_domain + initialize_physical_parameters + set_initial_condition + Compute expected u array and compare along with expected values from previous funcations. """ solver = SolveDiffusion2D() + + w, h = 10.0, 5.0 + dx, dy = 1.0, 0.5 + D = 4.0 + T_cold, T_hot = 300.0, 700.0 + + solver.initialize_domain(w=w, h=h, dx=dx, dy=dy) + solver.initialize_physical_parameters(d=D, T_cold=T_cold, T_hot=T_hot) + u = solver.set_initial_condition() + + expected_nx = int(w / dx) + expected_ny = int(h / dy) + + dx2 = dx * dx + dy2 = dy * dy + expected_dt = dx2 * dy2 / (2.0 * D * (dx2 + dy2)) + expected_u = T_cold * np.ones((expected_nx, expected_ny)) + + r, cx, cy = 2, 5, 5 + r2 = r ** 2 + for i in range(expected_nx): + for j in range(expected_ny): + p2 = (i * dx - cx) ** 2 + (j * dy - cy) ** 2 + if p2 < r2: + expected_u[i, j] = T_hot + + assert solver.w == w + assert solver.h == h + assert solver.dx == dx + assert solver.dy == dy + assert solver.nx == expected_nx + assert solver.ny == expected_ny + assert solver.T_cold == T_cold + assert solver.T_hot == T_hot + assert solver.D == D + assert solver.dt == expected_dt + assert np.array_equal(u, expected_u) \ No newline at end of file diff --git a/tests/unit/test_diffusion2d_functions.py b/tests/unit/test_diffusion2d_functions.py index c4277ff..49ddb43 100644 --- a/tests/unit/test_diffusion2d_functions.py +++ b/tests/unit/test_diffusion2d_functions.py @@ -3,7 +3,7 @@ """ from diffusion2d import SolveDiffusion2D - +import numpy as np def test_initialize_domain(): """ @@ -11,6 +11,22 @@ def test_initialize_domain(): """ solver = SolveDiffusion2D() + w = 12.0 + h = 7.5 + dx = 0.3 + dy = 0.5 + + expected_nx = int(w / dx) + expected_ny = int(h / dy) + + solver.initialize_domain(w=w, h=h, dx=dx, dy=dy) + + assert solver.w == w + assert solver.h == h + assert solver.dx == dx + assert solver.dy == dy + assert solver.nx == expected_nx + assert solver.ny == expected_ny def test_initialize_physical_parameters(): """ @@ -18,9 +34,49 @@ def test_initialize_physical_parameters(): """ solver = SolveDiffusion2D() + solver.dx = 0.2 + solver.dy = 0.5 + + D = 3.0 + T_cold = 250.0 + T_hot = 900.0 + + dx2 = solver.dx * solver.dx + dy2 = solver.dy * solver.dy + expected_dt = dx2 * dy2 / (2.0 * D * (dx2 + dy2)) + + solver.initialize_physical_parameters(d=D, T_cold=T_cold, T_hot=T_hot) + + assert solver.D == D + assert solver.T_cold == T_cold + assert solver.T_hot == T_hot + assert solver.dt == expected_dt def test_set_initial_condition(): """ Checks function SolveDiffusion2D.get_initial_function """ solver = SolveDiffusion2D() + + solver.dx = 1.0 + solver.dy = 1.0 + solver.nx = 11 + solver.ny = 11 + + solver.T_cold = 300.0 + solver.T_hot = 700.0 + + u = solver.set_initial_condition() + + u_expected = solver.T_cold * np.ones((solver.nx, solver.ny)) + + r, cx, cy = 2, 5, 5 + r2 = r ** 2 + + for i in range(solver.nx): + for j in range(solver.ny): + p2 = (i * solver.dx - cx) ** 2 + (j * solver.dy - cy) ** 2 + if p2 < r2: + u_expected[i, j] = solver.T_hot + + assert np.array_equal(u, u_expected) diff --git a/tox.toml b/tox.toml new file mode 100644 index 0000000..76f6b5f --- /dev/null +++ b/tox.toml @@ -0,0 +1,21 @@ +requires = ["tox>=4"] +env_list = ["unit", "integration", "report"] + +[env_run_base] +deps = ["-rrequirements.txt"] +set_env = { PYTHONPATH = "{tox_root}" } + +[env.unit] +description = "Run unit tests" +commands = [["pytest", "-q", "tests/unit"]] + +[env.integration] +description = "Run integration tests" +commands = [["pytest", "-q", "tests/integration"]] + +[env.report] +description = "Generate coverage HTML report" +commands = [ + ["coverage", "run", "-m", "pytest"], + ["coverage", "html"] +]