diff --git a/.coveragerc b/.coveragerc new file mode 100644 index 0000000..d24bfde --- /dev/null +++ b/.coveragerc @@ -0,0 +1,7 @@ +[run] +branch = True +source = . +omit = + tests/* + */site-packages/* + */dist-packages/* diff --git a/coverage-report.pdf b/coverage-report.pdf new file mode 100644 index 0000000..b3db110 Binary files /dev/null and b/coverage-report.pdf differ diff --git a/diffusion2d.py b/diffusion2d.py index 51a07f2..20870d1 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 type(w) == float, "w must be a float" + assert type(h) == float, "h must be a float" + assert type(dx) == float, "dx must be a float" + assert type(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 type(d) == float, "d must be a float" + assert type(T_cold) == float, "T_cold must be a float" + assert type(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..f6ac7e2 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,5 @@ +numpy +matplotlib +pytest +coverage +unittest \ No newline at end of file diff --git a/tests/integration/test_diffusion2d.py b/tests/integration/test_diffusion2d.py index fd026b4..942e735 100644 --- a/tests/integration/test_diffusion2d.py +++ b/tests/integration/test_diffusion2d.py @@ -3,6 +3,7 @@ """ from diffusion2d import SolveDiffusion2D +import numpy as np def test_initialize_physical_parameters(): @@ -10,6 +11,22 @@ def test_initialize_physical_parameters(): Checks function SolveDiffusion2D.initialize_domain """ solver = SolveDiffusion2D() + w = 12.0 + h = 12.0 + dx = 0.4 + dy = 0.4 + d = 5.0 + T_cold = 300.0 + T_hot = 700.0 + + solver.initialize_domain(w, h, dx, dy) + solver.initialize_physical_parameters(d, T_cold, T_hot) + + dx2 = dx * dx + dy2 = dy * dy + expected_dt = dx2 * dy2 / (2 * d * (dx2 + dy2)) + + assert solver.dt == expected_dt def test_set_initial_condition(): @@ -17,3 +34,27 @@ def test_set_initial_condition(): Checks function SolveDiffusion2D.get_initial_function """ solver = SolveDiffusion2D() + w = 12.0 + h = 12.0 + dx = 1.0 + dy = 1.0 + d = 5.0 + T_cold = 300.0 + T_hot = 700.0 + + solver.initialize_domain(w, h, dx, dy) + solver.initialize_physical_parameters(d, T_cold, T_hot) + + u = solver.set_initial_condition() + + expected_u = 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 * dx - cx) ** 2 + (j * dy - cy) ** 2 + if p2 < r2: + expected_u[i, j] = T_hot + + assert np.allclose(u, expected_u) diff --git a/tests/unit/test_diffusion2d_functions.py b/tests/unit/test_diffusion2d_functions.py index c4277ff..d671341 100644 --- a/tests/unit/test_diffusion2d_functions.py +++ b/tests/unit/test_diffusion2d_functions.py @@ -1,26 +1,65 @@ -""" -Tests for functions in class SolveDiffusion2D -""" - +import unittest +import numpy as np from diffusion2d import SolveDiffusion2D +class TestDiffusion2D(unittest.TestCase): + + def setUp(self): + self.solver = SolveDiffusion2D() -def test_initialize_domain(): - """ - Check function SolveDiffusion2D.initialize_domain - """ - solver = SolveDiffusion2D() - - -def test_initialize_physical_parameters(): - """ - Checks function SolveDiffusion2D.initialize_domain - """ - solver = SolveDiffusion2D() + def test_initialize_domain(self): + """ + Check function SolveDiffusion2D.initialize_domain + """ + w = 20. + h = 10. + dx = 0.2 + dy = 0.5 + + self.solver.initialize_domain(w=w, h=h, dx=dx, dy=dy) + + self.assertEqual(self.solver.nx, int(w / dx)) + self.assertEqual(self.solver.ny, int(h / dy)) + def test_initialize_physical_parameters(self): + """ + Check function SolveDiffusion2D.initialize_physical_parameters + """ + d = 5. + T_cold = 200. + T_hot = 600. + + self.solver.dx = 0.2 + self.solver.dy = 0.5 + + self.solver.initialize_physical_parameters(d=d, T_cold=T_cold, T_hot=T_hot) + + dx2 = self.solver.dx * self.solver.dx + dy2 = self.solver.dy * self.solver.dy + expected_dt = dx2 * dy2 / (2 * d * (dx2 + dy2)) + + self.assertAlmostEqual(self.solver.dt, expected_dt) -def test_set_initial_condition(): - """ - Checks function SolveDiffusion2D.get_initial_function - """ - solver = SolveDiffusion2D() + def test_set_initial_condition(self): + """ + Check function SolveDiffusion2D.set_initial_condition + """ + self.solver.nx = 10 + self.solver.ny = 10 + self.solver.dx = 1.0 + self.solver.dy = 1.0 + self.solver.T_cold = 300. + self.solver.T_hot = 700. + + u = self.solver.set_initial_condition() + + expected_u = self.solver.T_cold * np.ones((self.solver.nx, self.solver.ny)) + r, cx, cy = 2, 5, 5 + r2 = r ** 2 + for i in range(self.solver.nx): + for j in range(self.solver.ny): + p2 = (i * self.solver.dx - cx) ** 2 + (j * self.solver.dy - cy) ** 2 + if p2 < r2: + expected_u[i, j] = self.solver.T_hot + + self.assertTrue(np.array_equal(u, expected_u)) diff --git a/tox.toml b/tox.toml new file mode 100644 index 0000000..664c283 --- /dev/null +++ b/tox.toml @@ -0,0 +1,27 @@ +[tox] +env_list = ["unit", "integration", "report"] +skipsdist = true + +[testenv] +deps = ["-rrequirements.txt"] + +["testenv:unit"] +commands = [ + ["coverage", "run", "-a", "-m", "unittest", "discover", "tests/unit"] +] +set_env = { PYTHONPATH = "{toxinidir}" } + +["testenv:integration"] +commands = [ + ["coverage", "run", "-a", "-m", "pytest", "tests/integration"] +] +set_env = { PYTHONPATH = "{toxinidir}" } + +["testenv:report"] +deps = ["coverage"] +skip_install = true +commands = [ + ["coverage", "report"], + ["coverage", "html"] +] +