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
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
11 changes: 11 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[project]
name = "testing-python-exercise"
version = "0.1.0"
description = "Add your description here"
readme = "README.md"
requires-python = ">=3.10"
dependencies = [
"matplotlib>=3.10.8",
"numpy>=2.2.6",
"pytest>=9.0.2",
]
26 changes: 24 additions & 2 deletions tests/integration/test_diffusion2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,39 @@
"""

from diffusion2d import SolveDiffusion2D

import numpy as np

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

solver.initialize_domain(w=10., h=10., dx=0.1, dy=0.1)
solver.initialize_physical_parameters(d=5., T_cold=200., T_hot=800.)
#test this: dx2, dy2 = self.dx * self.dx, self.dy * self.dy
# self.dt = dx2 * dy2 / (2 * self.D * (dx2 + dy2))
# expected value: dx2 = dy2 = 0.01
# dt = 0.0001 / (2 * 5 * 0.02) = 0.0001 / 0.2 = 0.0005
assert((solver.dt - 0.0005) < 0.00000001)

def test_set_initial_condition():
"""
Checks function SolveDiffusion2D.get_initial_function
"""
solver = SolveDiffusion2D()
solver.initialize_domain(w=10., h=10., dx=0.1, dy=0.1)
solver.initialize_physical_parameters(d=5., T_cold=200., T_hot=800.)
u = solver.set_initial_condition()

referenceU = solver.T_cold * np.ones((solver.nx, solver.ny))

# Initial conditions - circle of radius r centred at (cx,cy) (mm)
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:
referenceU[i, j] = solver.T_hot

np.testing.assert_array_equal(u, referenceU)
21 changes: 19 additions & 2 deletions tests/unit/test_diffusion2d_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,40 @@

from diffusion2d import SolveDiffusion2D


def test_initialize_domain():
"""
Check function SolveDiffusion2D.initialize_domain
"""
solver = SolveDiffusion2D()
solver.initialize_domain(w=20., h=19., dx=0.4, dy=0.4)
#expected nx = 50 and ny = 47
assert(solver.nx == 50)
assert(solver.ny == 47)


def test_initialize_physical_parameters():
"""
Checks function SolveDiffusion2D.initialize_domain
Checks function SolveDiffusion2D.initialize_physical_parameters
"""
solver = SolveDiffusion2D()
solver.dx = 0.1
solver.dy = 0.1
solver.initialize_physical_parameters(d=5., T_cold=200., T_hot=800.)
#test this: dx2, dy2 = self.dx * self.dx, self.dy * self.dy
# self.dt = dx2 * dy2 / (2 * self.D * (dx2 + dy2))
# expected value: dx2 = dy2 = 0.01
# dt = 0.0001 / (2 * 5 * 0.02) = 0.0001 / 0.2 = 0.0005
assert((solver.dt - 0.0005) < 0.00000001)



def test_set_initial_condition():
"""
Checks function SolveDiffusion2D.get_initial_function
"""
solver = SolveDiffusion2D()
solver.initialize_domain()
solver.initialize_physical_parameters(d=5., T_cold=200., T_hot=800.)
u = solver.set_initial_condition()
assert(u[0, 0] == 200)

12 changes: 12 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[tox]
envlist = pytest

[testenv]
description = Install using uv
installer = uv

[testenv:pytest]
commands = pytest

[testenv:unittest]
commands = python -m unittest discover -s tests
Loading