Skip to content

[vangasa] Adding tests #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
358 changes: 358 additions & 0 deletions README.md

Large diffs are not rendered by default.

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), "w must be float"
assert isinstance(h, float), "h must be float"
assert isinstance(dx, float), "dx must be float"
assert isinstance(dy, float), "dy must be 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 float"
assert isinstance(T_cold, float), "T_cold must be float"
assert isinstance(T_hot, float), "T_hot must be float"
self.D = d
self.T_cold = T_cold
self.T_hot = T_hot
Expand Down
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
numpy>=1.26.1
matplotlib>=3.8.4
7 changes: 7 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from setuptools import setup

setup(
name="diffusion2d",
version="1.0",
py_modules=["diffusion2d"],
)
Empty file added tests/__init__.py
Empty file.
Empty file added tests/integration/__init__.py
Empty file.
44 changes: 44 additions & 0 deletions tests/integration/test_diffusion2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Tests for functionality checks in class SolveDiffusion2D
"""

import numpy as np
from diffusion2d import SolveDiffusion2D


Expand All @@ -11,9 +12,52 @@ def test_initialize_physical_parameters():
"""
solver = SolveDiffusion2D()

# Initialize domain
solver.initialize_domain(w=10.0, h=10.0, dx=0.5, dy=0.5)

# Initialize physical parameters
solver.initialize_physical_parameters(d=2.0, T_cold=300.0, T_hot=700.0)

assert solver.D == 2.0, f"Expected D to be 2.0, but got {solver.D}"

# Expected dt calculation
dx2, dy2 = solver.dx ** 2, solver.dy ** 2
expected_dt = dx2 * dy2 / (2 * solver.D * (dx2 + dy2))

assert solver.dt == expected_dt, f"Expected {expected_dt}, got {solver.dt}"


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

# Step 1: Initialize domain with valid dimensions and grid spacing
solver.initialize_domain(w=10.0, h=10.0, dx=0.5, dy=0.5)

# Initialize physical parameters
solver.initialize_physical_parameters(d=4.0, T_cold=300.0, T_hot=700.0)

# Set initial conditions
u = solver.set_initial_condition()

# Step 3: Expected u calculation
nx, ny = solver.nx, solver.ny
T_cold, T_hot = solver.T_cold, solver.T_hot
r, cx, cy = 2.0, 5.0, 5.0

expected_u = T_cold * np.ones((nx, ny))


for i in range(nx):
for j in range(ny):
p2 = (i * solver.dx - cx) ** 2 + (j * solver.dy - cy) ** 2
if p2 < r ** 2:
expected_u[i, j] = T_hot

# Step 4: Assert the result
np.testing.assert_array_equal(u, expected_u, err_msg="Initial condition array mismatch")

# Optional debug prints (can be removed in production)
print("Test passed: set_initial_condition produces expected results.")
Empty file added tests/unit/__init__.py
Empty file.
91 changes: 75 additions & 16 deletions tests/unit/test_diffusion2d_functions.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,85 @@
"""
Tests for functions in class SolveDiffusion2D
"""

import unittest
import numpy as np
from diffusion2d import SolveDiffusion2D

class TestDiffusion2D(unittest.TestCase):

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

def test_initialize_domain(self):
"""
Check function SolveDiffusion2D.initialize_domain
"""
w, h, dx, dy = 10.0, 20.0, 0.5, 0.5
expected_nx, expected_ny = int(w / dx), int(h / dy)

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

self.assertEqual(self.solver.nx, expected_nx, f"Expected nx = {expected_nx}, but got {self.solver.nx}")
self.assertEqual(self.solver.ny, expected_ny, f"Expected ny = {expected_ny}, but got {self.solver.ny}")


def test_initialize_physical_parameters(self):
"""
Checks function SolveDiffusion2D.initialize_domain
"""
d = 4.0 # Diffusivity, analogous to alpha
dx = 0.1 # Grid spacing in the x-direction
dy = 0.1 # Grid spacing in the y-direction

# Calculate expected dt manually using the formula
dx2, dy2 = dx * dx, dy * dy
dt_expected = dx2 * dy2 / (2 * d * (dx2 + dy2))

# Set the dx and dy values manually in the solver (since we're testing only initialize_physical_parameters)
self.solver.dx = dx
self.solver.dy = dy

# Act
self.solver.initialize_physical_parameters(d)

# Assert
self.assertAlmostEqual(self.solver.dt, dt_expected, delta=1e-9, msg=f"Expected dt = {dt_expected}, but got {self.solver.dt}")

def test_set_initial_condition(self):
"""
Checks function SolveDiffusion2D.get_initial_function
"""
T_hot = 700.0 # Hot temperature
T_cold = 300.0 # Cold temperature
dx = 1.0 # Spacing in the x-direction
dy = 1.0 # Spacing in the y-direction
nx = 10 # Number of points in x-direction
ny = 10 # Number of points in y-direction

self.solver.nx = nx
self.solver.ny = ny
self.solver.dx = dx
self.solver.dy = dy
self.solver.T_hot = T_hot
self.solver.T_cold = T_cold

def test_initialize_domain():
"""
Check function SolveDiffusion2D.initialize_domain
"""
solver = SolveDiffusion2D()
# Step 4: Manually compute the expected u array
expected_u = T_cold * np.ones((nx, ny)) # Start with all values as T_cold
r = 2 # Radius of the circle
cx, cy = 5, 5 # Center of the circle

for i in range(nx):
for j in range(ny):
p2 = (i * self.solver.dx - cx) ** 2 + (j * self.solver.dy - cy) ** 2
if p2 < r ** 2:
expected_u[i, j] = T_hot


def test_initialize_physical_parameters():
"""
Checks function SolveDiffusion2D.initialize_domain
"""
solver = SolveDiffusion2D()
# Act
u = self.solver.set_initial_condition()

# Step 5: Assert that the generated u array matches the expected u array
np.testing.assert_array_equal(u, expected_u, err_msg="Initial condition field is not correct.")

def test_set_initial_condition():
"""
Checks function SolveDiffusion2D.get_initial_function
"""
solver = SolveDiffusion2D()
12 changes: 12 additions & 0 deletions tox.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
requires = ["tox>=4"]
env_list = ["pytest", "unittest"]

[env.pytest]
description = "pytest"
deps = ["pytest", "-r requirements.txt"]
commands = [["python", "-m", "pytest", "tests/integration/test_diffusion2d.py"]]

[env.unittest]
description = "unittest"
deps = ["-r requirements.txt"]
commands = [["python", "-m", "unittest", "tests/unit/test_diffusion2d_functions.py"]]