Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.venv/
__pycache__/
*.ipynb
Binary file added coverage-report.pdf
Binary file not shown.
9 changes: 8 additions & 1 deletion diffusion2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,21 @@ 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
self.dy = dy
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
Expand Down
15 changes: 15 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -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
18 changes: 18 additions & 0 deletions tests/integration/test_diffusion2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
Tests for functionality checks in class SolveDiffusion2D
"""

import numpy
import pytest

from diffusion2d import SolveDiffusion2D


Expand All @@ -10,10 +13,25 @@ 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():
"""
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)
59 changes: 59 additions & 0 deletions tests/unit/test_diffusion2d_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
Tests for functions in class SolveDiffusion2D
"""

import numpy
import pytest

from diffusion2d import SolveDiffusion2D


Expand All @@ -10,17 +13,73 @@ 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():
"""
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():
"""
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)
7 changes: 7 additions & 0 deletions tox.toml
Original file line number Diff line number Diff line change
@@ -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"]]