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
Binary file not shown.
Binary file added __pycache__/diffusion2d.cpython-312.pyc
Binary file not shown.
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), "w must be a float"
assert isinstance(h, float), "h must be a float"
assert isinstance(dx, float), "dx must be a float"
assert isinstance(dy, float), "dy must be a 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), "d must be a float"
assert isinstance(T_cold, float), "T_cold must be a float"
assert isinstance(T_hot, float), "T_hot must be a float"

self.D = d
self.T_cold = T_cold
self.T_hot = T_hot
Expand Down
4 changes: 4 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
numpy
matplotlib
pytest
coverage
Binary file not shown.
57 changes: 47 additions & 10 deletions tests/integration/test_diffusion2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,55 @@
Tests for functionality checks in class SolveDiffusion2D
"""

import unittest
import numpy as np
from diffusion2d import SolveDiffusion2D

class TestDiffusion2D(unittest.TestCase):

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

def test_initialize_physical_parameters(self):
"""
Checks function initialize_physical_parameters.
"""
w = 10.0
h = 10.0
dx = 1.0
dy = 1.0

self.solver.initialize_domain(w, h, dx, dy)

def test_set_initial_condition():
"""
Checks function SolveDiffusion2D.get_initial_function
"""
solver = SolveDiffusion2D()
d = 1.0
T_cold = 300.0
T_hot = 700.0

# Expected result
# dt = dx*dy / ( 2*d * (dx^2 + dy^2) )
# dt = 1 / (2 * 2) = 0.25
expected_dt = 0.25

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

self.assertAlmostEqual(self.solver.dt, expected_dt)

def test_set_initial_condition(self):
"""
Checks function set_initial_condition.
"""
w = 10.0
h = 10.0
dx = 1.0
dy = 1.0
d = 4.0
T_cold = 300.0
T_hot = 700.0

self.solver.initialize_domain(w, h, dx, dy)
self.solver.initialize_physical_parameters(d, T_cold, T_hot)
u = self.solver.set_initial_condition()

self.assertEqual(u.shape, (10, 10))

self.assertEqual(u[0, 0], 300.0)
Binary file not shown.
69 changes: 53 additions & 16 deletions tests/unit/test_diffusion2d_functions.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,63 @@
"""
Tests for functions in class SolveDiffusion2D
"""

import unittest
from diffusion2d import SolveDiffusion2D

class TestDiffusion2D(unittest.TestCase):

def setUp(self):
self.solver = SolveDiffusion2D()

def test_initialize_domain(self):
"""
Check if nx and ny are calculated correctly.
"""
w = 20.0
h = 10.0
dx = 2.0
dy = 5.0

expected_nx = 10
expected_ny = 2

self.solver.initialize_domain(w, h, dx, dy)

self.assertEqual(self.solver.nx, expected_nx)
self.assertEqual(self.solver.ny, expected_ny)

def test_initialize_physical_parameters(self):
"""
Check if dt is calculated correctly.
"""
d = 5.0
T_cold = 200.0
T_hot = 500.0

self.solver.dx = 2.0
self.solver.dy = 2.0

# 2. Expected output
# dt = (dx^2 * dy^2) / (2 * d * (dx^2 + dy^2))
# dt = (4 * 4) / (2 * 5 * (4 + 4))
# dt = 16 / (10 * 8) = 16 / 80 = 0.2
expected_dt = 0.2

def test_initialize_domain():
"""
Check function SolveDiffusion2D.initialize_domain
"""
solver = SolveDiffusion2D()
self.solver.initialize_physical_parameters(d, T_cold, T_hot)

self.assertAlmostEqual(self.solver.dt, expected_dt, places=5)

def test_initialize_physical_parameters():
"""
Checks function SolveDiffusion2D.initialize_domain
"""
solver = SolveDiffusion2D()
def test_set_initial_condition(self):
"""
Check if u matrix is initialized correctly.
"""
self.solver.nx = 3
self.solver.ny = 3
self.solver.dx = 1.0
self.solver.dy = 1.0
self.solver.T_cold = 100.0
self.solver.T_hot = 200.0

u = self.solver.set_initial_condition()

def test_set_initial_condition():
"""
Checks function SolveDiffusion2D.get_initial_function
"""
solver = SolveDiffusion2D()
self.assertEqual(u[0, 0], 100.0)
10 changes: 10 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[tox]
envlist = py3
skipsdist = True

[testenv]
deps = -rrequirements.txt
commands =
python3 -m pytest tests/
coverage run -m pytest tests/
coverage report