diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d9739ac --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.venv/ +__pycache__/ +*.ipynb diff --git a/coverage-report.pdf b/coverage-report.pdf new file mode 100644 index 0000000..8d3f6c4 Binary files /dev/null and b/coverage-report.pdf differ diff --git a/diffusion2d.py b/diffusion2d.py index 51a07f2..2278bae 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) + assert isinstance(h, float) + assert isinstance(dx, float) + assert isinstance(dy, 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) + 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..a850b08 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,15 @@ +contourpy==1.3.3 +cycler==0.12.1 +fonttools==4.61.1 +iniconfig==2.3.0 +kiwisolver==1.4.9 +matplotlib==3.10.8 +numpy==2.4.1 +packaging==25.0 +pillow==12.1.0 +pluggy==1.6.0 +Pygments==2.19.2 +pyparsing==3.3.1 +pytest==9.0.2 +python-dateutil==2.9.0.post0 +six==1.17.0 diff --git a/tests/integration/test_diffusion2d.py b/tests/integration/test_diffusion2d.py index fd026b4..1cff2fd 100644 --- a/tests/integration/test_diffusion2d.py +++ b/tests/integration/test_diffusion2d.py @@ -2,6 +2,9 @@ Tests for functionality checks in class SolveDiffusion2D """ +import numpy +import pytest + from diffusion2d import SolveDiffusion2D @@ -10,6 +13,9 @@ def test_initialize_physical_parameters(): Checks function SolveDiffusion2D.initialize_domain """ solver = SolveDiffusion2D() + solver.initialize_domain(w=20.0, h=20.0, dx=5.0, dy=5.0) + solver.initialize_physical_parameters(d=5.0, T_cold=400.0, T_hot=600.0) + assert solver.dt == pytest.approx(1.25, rel=1e-6) def test_set_initial_condition(): @@ -17,3 +23,15 @@ def test_set_initial_condition(): Checks function SolveDiffusion2D.get_initial_function """ solver = SolveDiffusion2D() + solver.initialize_domain(w=20.0, h=20.0, dx=2.0, dy=2.0) + solver.initialize_physical_parameters(d=5.0, T_cold=400.0, T_hot=600.0) + actual_result = solver.set_initial_condition() + + expected_result = 400.0 * numpy.ones((10, 10)) + for i in range(10): + for j in range(10): + p2 = (i * 2.0 - 5.0) ** 2 + (j * 2.0 - 5.0) ** 2 + if p2 < 4: + expected_result[i, j] = 600.0 + + assert numpy.allclose(actual_result, expected_result) diff --git a/tests/unit/test_diffusion2d_functions.py b/tests/unit/test_diffusion2d_functions.py index c4277ff..bd74863 100644 --- a/tests/unit/test_diffusion2d_functions.py +++ b/tests/unit/test_diffusion2d_functions.py @@ -2,6 +2,9 @@ Tests for functions in class SolveDiffusion2D """ +import numpy +import pytest + from diffusion2d import SolveDiffusion2D @@ -10,6 +13,9 @@ def test_initialize_domain(): Check function SolveDiffusion2D.initialize_domain """ solver = SolveDiffusion2D() + solver.initialize_domain(w=20.0, h=20.0, dx=0.5, dy=0.5) + assert solver.nx == 40 + assert solver.ny == 40 def test_initialize_physical_parameters(): @@ -17,6 +23,10 @@ def test_initialize_physical_parameters(): Checks function SolveDiffusion2D.initialize_domain """ solver = SolveDiffusion2D() + solver.dx = 0.5 + solver.dy = 0.5 + solver.initialize_physical_parameters(d=3.0, T_cold=200.0, T_hot=800.0) + assert solver.dt == pytest.approx(0.02083333333, rel=1e-6) def test_set_initial_condition(): @@ -24,3 +34,52 @@ def test_set_initial_condition(): Checks function SolveDiffusion2D.get_initial_function """ solver = SolveDiffusion2D() + solver.nx = 5 + solver.ny = 5 + solver.dx = 3 + solver.dy = 3 + solver.T_cold = 200.0 + solver.T_hot = 800.0 + + actual_result = solver.set_initial_condition() + expected_result = numpy.array( + [ + [ + 200.0, + 200.0, + 200.0, + 200.0, + 200.0, + ], + [ + 200.0, + 200.0, + 200.0, + 200.0, + 200.0, + ], + [ + 200.0, + 200.0, + 800.0, + 200.0, + 200.0, + ], + [ + 200.0, + 200.0, + 200.0, + 200.0, + 200.0, + ], + [ + 200.0, + 200.0, + 200.0, + 200.0, + 200.0, + ], + ] + ) + + assert numpy.array_equal(actual_result, expected_result) diff --git a/tox.toml b/tox.toml new file mode 100644 index 0000000..3e2a990 --- /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"]]