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
11 changes: 11 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
__pycache__/

.venv/
venv/

.pytest_cache/

.coverage
htmlcov/

.tox/
Binary file added coverage-report.pdf
Binary file not shown.
11 changes: 10 additions & 1 deletion diffusion2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,23 @@ 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.0, T_hot=700.0):
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
5 changes: 5 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
numpy>=1.26.0
matplotlib>=3.8.0
pytest>=9.0.0
coverage>=7.13.1
tox>=4.23.2
27 changes: 27 additions & 0 deletions tests/integration/test_diffusion2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,36 @@ def test_initialize_physical_parameters():
"""
solver = SolveDiffusion2D()

solver.initialize_domain(w=10.0, h=10.0, dx=1.0, dy=1.0)
solver.initialize_physical_parameters(d=2.0, T_cold=300.0, T_hot=700.0)

expected_dt = 0.125

assert solver.dt == expected_dt

assert solver.D == 2.0
assert solver.T_cold == 300.0
assert solver.T_hot == 700.0


def test_set_initial_condition():
"""
Checks function SolveDiffusion2D.get_initial_function
"""
solver = SolveDiffusion2D()

solver.initialize_domain(w=10.0, h=10.0, dx=1.0, dy=1.0)
solver.initialize_physical_parameters(d=1.0, T_cold=0.0, T_hot=100.0)

u = solver.set_initial_condition()

# shape
assert u.shape == (solver.nx, solver.ny)

# center index
cx_index = int(5 / solver.dx)
cy_index = int(5 / solver.dy)

# check center and corner values
assert u[cx_index, cy_index] == solver.T_hot
assert u[0,0] == solver.T_cold
62 changes: 60 additions & 2 deletions tests/unit/test_diffusion2d_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,74 @@ def test_initialize_domain():
"""
solver = SolveDiffusion2D()

# input values
w = 12.0
h = 8.0
dx = 0.5
dy = 0.25

# expected results
expected_nx = 24
expected_ny = 32

# call function under test
solver.initialize_domain(w=w, h=h, dx=dx, dy=dy)

# assertions
assert solver.nx == expected_nx
assert solver.ny == expected_ny

def test_initialize_physical_parameters():
"""
Checks function SolveDiffusion2D.initialize_domain
Check function SolveDiffusion2D.initialize_physical_parameters
"""
solver = SolveDiffusion2D()

solver.dx = 1.0
solver.dy = 2.0

d = 2.0
T_cold = 300.0
T_hot = 700.0

# dx² = 1
# dy² = 4
#
# dt = (1 * 4) / (2 * 2 * (1 + 4))
# dt = 4 / 20
# dt = 0.2
# expected result
expected_dt = 0.2

solver.initialize_physical_parameters(d=d, T_cold=T_cold, T_hot=T_hot)

# assertions
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
Check function SolveDiffusion2D.set_initial_condition
"""
solver = SolveDiffusion2D()

solver.nx = 10
solver.ny = 10
solver.dx = 1.0
solver.dy = 1.0
solver.T_cold = 0.0
solver.T_hot = 100.0

# call function under test
u = solver.set_initial_condition()

# basic shape check
assert u.shape == (10, 10)

# center should be hot
assert u[5, 5] == solver.T_hot

# corner should be cold
assert u[0, 0] == solver.T_cold
19 changes: 19 additions & 0 deletions tox.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[tool.tox]
env_list = ["py", "coverage"]
skips_dist = true

[tool.tox.testenv.py]
description = "Run unit and integration tests"
deps = ["-r requirements.txt"]
commands = [
"python -m pytest tests/unit",
"python -m pytest tests/integration"
]

[tool.tox.testenv.coverage]
description = "Generate coverage report"
deps = ["-r requirements.txt", "coverage"]
commands = [
"coverage run -m pytest",
"coverage html"
]