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.
7 changes: 6 additions & 1 deletion diffusion2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,19 @@ def __init__(self):
self.dt = None

def initialize_domain(self, w=10., h=10., dx=0.1, dy=0.1):
for name, value in {"w": w, "h": h, "dx": dx, "dy": dy}.items():
assert isinstance(value, float), f"{name} must be a float, got {type(value).__name__}."

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.):
for name, value in {"d": d, "T_cold": T_cold, "T_hot": T_hot}.items():
assert isinstance(value, float), f"{name} must be a float, got {type(value).__name__}."
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 @@
coverage>=7.13.1
matplotlib>=3.10.8
numpy>=2.4.1
pytest>=9.0.2
tox>=4.34.1
22 changes: 18 additions & 4 deletions tests/integration/test_diffusion2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,30 @@

from diffusion2d import SolveDiffusion2D

import pytest
import numpy as np

def test_initialize_physical_parameters():
@pytest.fixture
def input_values():
# w, h, dx, dy, d, T_cold, T_hot
return [40., 50., 0.1, 0.1, 5., 200., 800.]

def test_initialize_physical_parameters(input_values):
"""
Checks function SolveDiffusion2D.initialize_domain
Checks function SolveDiffusion2D.initialize_physical_parameters
"""
solver = SolveDiffusion2D()
solver.initialize_domain(*input_values[:4])
solver.initialize_physical_parameters(*input_values[4:])
assert solver.dt == pytest.approx(0.0005)


def test_set_initial_condition():
def test_set_initial_condition(input_values):
"""
Checks function SolveDiffusion2D.get_initial_function
Checks function SolveDiffusion2D.set_initial_condition
"""
solver = SolveDiffusion2D()
solver.initialize_domain(*input_values[:4])
solver.initialize_physical_parameters(*input_values[4:])
u = solver.set_initial_condition()
np.testing.assert_array_almost_equal(u, np.genfromtxt("tests/u_ref.csv", delimiter=","))
400 changes: 400 additions & 0 deletions tests/u_ref.csv

Large diffs are not rendered by default.

104 changes: 97 additions & 7 deletions tests/unit/test_diffusion2d_functions.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,116 @@
"""
Tests for functions in class SolveDiffusion2D
"""
import numpy as np
import pytest

from diffusion2d import SolveDiffusion2D

@pytest.fixture
def solver():
solver = SolveDiffusion2D()
solver.dx = 0.2
solver.dy = 0.2
solver.nx = 200
solver.ny = 250
solver.T_cold = 200
solver.T_hot = 800

return solver

def test_initialize_domain():
@pytest.mark.parametrize("w, h, dx, dy, exp_nx, exp_ny", [
(40., 50., 0.2, 0.2, 200, 250),
(40., 50., 0.2, 0.4, 200, 125)
])
def test_initialize_domain(solver, w, h, dx, dy, exp_nx, exp_ny):
"""
Check function SolveDiffusion2D.initialize_domain
"""
solver = SolveDiffusion2D()
solver.initialize_domain(w, h, dx, dy)

assert solver.w == w
assert solver.h == h
assert solver.dx == dx
assert solver.dy == dy
assert solver.nx == exp_nx
assert solver.ny == exp_ny

def test_initialize_physical_parameters():
@pytest.mark.parametrize("w, h, dx, dy", [
(40, 50., 0.2, 0.2),
(40., 50, 0.2, 0.2),
(40., 50., 1, 0.2),
(40., 50., 0.2, 1)
])
def test_initialize_domain_assertion_error(solver, w, h, dx, dy):
"""
Checks function SolveDiffusion2D.initialize_domain
Check function SolveDiffusion2D.initialize_domain.

Check with the condition, that a parameter is not of type float.
"""
solver = SolveDiffusion2D()
with pytest.raises(AssertionError):
solver.initialize_domain(w, h, dx, dy)

@pytest.mark.parametrize("dx, dy", [
(.2, 0.),
(0., .2)
])
def test_initialize_domain_zero_division(solver, dx, dy):
"""
Check function SolveDiffusion2D.initialize_domain.

Check with the condition, that either disk width or height is zero (error).
"""
with pytest.raises(ZeroDivisionError):
solver.initialize_domain(40., 50., dx, dy)

@pytest.mark.parametrize("d, T_cold, T_hot, exp_dt", [
(5., 200., 800., 0.002)
])
def test_initialize_physical_parameters(solver, d, T_cold, T_hot, exp_dt):
"""
Checks function SolveDiffusion2D.initialize_physical_parameters.
"""
solver.initialize_physical_parameters(d, T_cold, T_hot)

assert solver.D == d
assert solver.T_cold == T_cold
assert solver.T_hot == T_hot
assert solver.dt == pytest.approx(exp_dt)

def test_set_initial_condition():
@pytest.mark.parametrize("d, T_cold, T_hot", [
(5, 200., 800.),
(5., 200, 800.),
(5., 200., 800)
])
def test_initialize_physical_parameters_assertion_error(solver, d, T_cold, T_hot):
"""
Checks function SolveDiffusion2D.initialize_physical_parameters.

Check with the condition, that a parameter is not of type float.
"""
with pytest.raises(AssertionError):
solver.initialize_physical_parameters(d, T_cold, T_hot)


def test_initialize_physical_parameters_zero_division(solver):
"""
Checks function SolveDiffusion2D.initialize_physical_parameters.

Check that the correct error is thrown when zero division happens.
"""
with pytest.raises(ZeroDivisionError):
solver.initialize_physical_parameters(0., 200., 800.)

with pytest.raises(ZeroDivisionError):
solver.dx = 0.
solver.dy = 0.
solver.initialize_physical_parameters(5., 200., 800.)


def test_set_initial_condition(solver):
"""
Checks function SolveDiffusion2D.get_initial_function
"""
solver = SolveDiffusion2D()
solver.nx = 2
solver.ny = 2
np.testing.assert_array_almost_equal(solver.set_initial_condition(), 200 * np.ones((solver.nx, solver.ny)))
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"]

[env.pytest]
description = "Run pytest"
deps = ["-rrequirements.txt"]
commands = [["pytest"]]