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-311.pyc
Binary file not shown.
Binary file added coverage-report.pdf
Binary file not shown.
13 changes: 12 additions & 1 deletion diffusion2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,25 @@ 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.0, T_hot=700.0):

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.
Binary file not shown.
69 changes: 55 additions & 14 deletions tests/integration/test_diffusion2d.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,60 @@
"""
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(self):
"""
Checks function SolveDiffusion2D.initialize_physical_parameters
working with initialize_domain
"""
solver = SolveDiffusion2D()
w = 10.0
h = 10.0
dx = 1.0
dy = 1.0
d = 4.0

# Expected dt calculation (Integration test: we rely on initialize_domain working too)
# dt = (dx*dx * dy*dy) / (2 * d * (dx*dx + dy*dy))
# dt = (1 / (2 * 4 * 2)) = 1 / 16 = 0.0625
expected_dt = 0.0625

def test_initialize_physical_parameters():
"""
Checks function SolveDiffusion2D.initialize_domain
"""
solver = SolveDiffusion2D()
solver.initialize_domain(w, h, dx, dy)
solver.initialize_physical_parameters(d=d)

self.assertAlmostEqual(solver.dt, expected_dt)

def test_set_initial_condition():
"""
Checks function SolveDiffusion2D.get_initial_function
"""
solver = SolveDiffusion2D()
def test_get_initial_function(self):
"""
Checks function SolveDiffusion2D.get_initial_function
working with initialize_domain and initialize_physical_parameters
"""
solver = SolveDiffusion2D()

# 1. Initialize the solver fully
solver.initialize_domain(w=10., h=10., dx=1., dy=1.)
solver.initialize_physical_parameters(T_cold=100., T_hot=500.)

# 2. Call the function
u_actual = solver.set_initial_condition()

# 3. Manually calculate Expected Result
expected_u = 100.0 * np.ones((10, 10))

# Loop to create the hot circle
for i in range(10):
for j in range(10):
# Calculate distance from center (5.0, 5.0)
# Note: The code uses coordinates based on dx, dy, so:
x = i * 1.0
y = j * 1.0
p2 = (x - 5.0)**2 + (y - 5.0)**2

# Radius is squared in the comparison (radius=2, r^2=4)
if p2 < 4.0:
expected_u[i, j] = 500.0

# 4. Compare the full arrays
np.testing.assert_array_equal(u_actual, expected_u)
Binary file not shown.
Binary file not shown.
73 changes: 57 additions & 16 deletions tests/unit/test_diffusion2d_functions.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,67 @@
"""
Tests for functions in class SolveDiffusion2D
"""

import unittest
from diffusion2d import SolveDiffusion2D

class TestDiffusion2D(unittest.TestCase):
def test_initialize_domain(self):
"""
Check function SolveDiffusion2D.initialize_domain
"""
# Fixture (Input data)
w = 20.0
h = 10.0
dx = 2.0
dy = 2.0

# Expected Result (Manual calculation)
expected_nx = 10 # 20 / 2
expected_ny = 5 # 10 / 2

# Action (Call the function)
solver = SolveDiffusion2D()
solver.initialize_domain(w, h, dx, dy)

# Assertion (Check if code matches expectation)
self.assertEqual(solver.nx, expected_nx)
self.assertEqual(solver.ny, expected_ny)


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

def test_initialize_domain():
"""
Check function SolveDiffusion2D.initialize_domain
"""
solver = SolveDiffusion2D()
# Manually set the state (mocking domain initialization)
solver.dx = 2.0
solver.dy = 2.0
d = 4.0

# Expected dt calculation:
# dt = (dx*dx * dy*dy) / (2 * d * (dx*dx + dy*dy))
# dt = (4 * 4) / (2 * 4 * (4 + 4)) = 16 / 64 = 0.25
expected_dt = 0.25

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

self.assertAlmostEqual(solver.dt, expected_dt)

def test_set_initial_condition():
"""
Checks function SolveDiffusion2D.get_initial_function
"""
solver = SolveDiffusion2D()
def test_set_initial_conditions(self):
"""
Check function SolveDiffusion2D.set_initial_conditions
"""
solver = SolveDiffusion2D()
solver.initialize_domain(w=10., h=10., dx=1., dy=1.)
solver.initialize_physical_parameters(T_cold=100., T_hot=500.)

# Action
u = solver.set_initial_condition()

# Assertion: Check a known hot spot (center) and cold spot (corner)
# Center should be 500.0 (Hot)
self.assertEqual(u[5, 5], 500.0)

# Corner (0,0) should be 100.0 (Cold)
self.assertEqual(u[0, 0], 100.0)
12 changes: 12 additions & 0 deletions tox.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

[tox]
env_list = ["test_env"]
no_package = true

[testenv]
deps = [
"-rrequirements.txt"
]
commands = [
["pytest"]
]