Skip to content
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

[ahmedsa] Adding tests #2

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
Binary file added .coverage
Binary file not shown.
Binary file added Coverage report.pdf
Binary file not shown.
233 changes: 233 additions & 0 deletions README.md

Large diffs are not rendered by default.

Binary file added __pycache__/diffusion2d.cpython-312.pyc
Binary file not shown.
8 changes: 8 additions & 0 deletions diffusion2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ def __init__(self):
self.dt = None

def initialize_domain(self, w=10., h=10., dx=0.1, dy=0.1):
assert isinstance(w, float), "Width (w) must be a float"
assert isinstance(h, float), "Height (h) must be a float"
assert isinstance(dx, float), "Grid spacing dx must be a float"
assert isinstance(dy, float), "Grid spacing dy must be a float"

self.w = w
self.h = h
self.dx = dx
Expand All @@ -46,6 +51,9 @@ def initialize_domain(self, w=10., h=10., dx=0.1, dy=0.1):
self.ny = int(h / dy)

def initialize_physical_parameters(self, d=4., T_cold=300, T_hot=700):
assert isinstance(d, float), "Diffusion coefficient (D) must be a float"
assert isinstance(T_cold, float), "Cold temperature (T_cold) must be a float"
assert isinstance(T_hot, float), "Hot temperature (T_hot) must be a float"
self.D = d
self.T_cold = T_cold
self.T_hot = T_hot
Expand Down
Empty file added logs/test_output.log
Empty file.
5 changes: 5 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[pytest]
log_cli = true
log_cli_level = INFO
log_file = logs/test_output.log
log_file_level = INFO
Binary file not shown.
47 changes: 46 additions & 1 deletion tests/integration/test_diffusion2d.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,64 @@
"""
Tests for functionality checks in class SolveDiffusion2D
"""

import sys
import os
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "../../")))
from diffusion2d import SolveDiffusion2D
import numpy as np


def test_initialize_physical_parameters():
"""
Checks function SolveDiffusion2D.initialize_domain
"""
solver = SolveDiffusion2D()
w, h, dx, dy = 2.0, 3.0, 0.1, 0.1
D, T_cold, T_hot = 2.5, 250.0, 750.0
solver.initialize_domain(w, h, dx, dy)
solver.initialize_physical_parameters(D, T_cold, T_hot)

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

assert solver.D == D, f"Expected D: {D}, but got: {solver.D}"
assert solver.T_cold == T_cold, f"Expected T_cold: {T_cold}, but got: {solver.T_cold}"
assert solver.T_hot == T_hot, f"Expected T_hot: {T_hot}, but got: {solver.T_hot}"
assert np.isclose(solver.dt, expected_dt), f"Expected dt: {expected_dt}, but got: {solver.dt}"

# debug
print(f"Expected dt: {expected_dt}, Computed dt: {solver.dt}")



def test_set_initial_condition():
"""
Checks function SolveDiffusion2D.get_initial_function
"""
# Create an instance of the solver
solver = SolveDiffusion2D()

w, h, dx, dy = 10.0, 10.0, 1.0, 1.0
T_cold, T_hot = 250.0, 750.0
solver.initialize_domain(w, h, dx, dy)
solver.T_cold = T_cold
solver.T_hot = T_hot

# to set initial condition
u = solver.set_initial_condition()

# expected result
expected_u = np.full((solver.nx, solver.ny), T_cold)
r, cx, cy = 2, 5, 5 # Circle radius and center
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:
expected_u[i, j] = T_hot

# Debug
print(f"Expected u:\n{expected_u}")
print(f"Actual u:\n{u}")
assert np.array_equal(u, expected_u), "The set_initial_condition method did not produce the expected output."
Binary file not shown.
24 changes: 24 additions & 0 deletions tests/unit/test_diffusion2d_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
Tests for functions in class SolveDiffusion2D
"""

import sys
import os
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "../../")))
from diffusion2d import SolveDiffusion2D


Expand All @@ -10,17 +13,38 @@ def test_initialize_domain():
Check function SolveDiffusion2D.initialize_domain
"""
solver = SolveDiffusion2D()
w, h, dx, dy = 2.0, 3.0, 0.5, 0.5
expected_nx = int(w / dx)
expected_ny = int(h / dy)
solver.initialize_domain(w, h, dx, dy)
assert solver.nx == expected_nx
assert solver.ny == expected_ny


def test_initialize_physical_parameters():
"""
Checks function SolveDiffusion2D.initialize_domain
"""
solver = SolveDiffusion2D()
D, T_cold, T_hot = 2.5, 250.0, 750.0
solver.dx, solver.dy = 0.1, 0.1
solver.initialize_physical_parameters(D, T_cold, T_hot)
expected_dt = (solver.dx * solver.dy) / (2 * D*10 * (solver.dx + solver.dy))
assert solver.dt == expected_dt, f"Expected dt: {expected_dt}, but got: {solver.dt}"


def test_set_initial_condition():
"""
Checks function SolveDiffusion2D.get_initial_function
"""
solver = SolveDiffusion2D()
D, T_cold, T_hot = 2.5, 250.0, 750.0
solver.D = D
solver.T_cold = T_cold
solver.T_hot = T_hot
solver.dx = 0.1
solver.dy = 0.1
solver.initialize_physical_parameters(D, T_cold, T_hot)
# Calculate expected dt based on the stability criterion
expected_dt = (solver.dx * solver.dy) / (2 * D*10 * (solver.dx + solver.dy))
assert solver.dt == expected_dt
21 changes: 21 additions & 0 deletions tox.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
requires = ["tox>=4"]
env_list = ["unittest", "integrationtest", "coverage"]

[env.unittest]
description = "Run Unit Tests"
deps = ["pytest>=8", "matplotlib", "numpy"]
commands = [["pytest", "tests/unit/test_diffusion2d_functions.py"]]

[env.integrationtest]
description = "Run Integration Tests"
deps = ["pytest>=8", "matplotlib", "numpy"]
commands = [["pytest", "tests/integration/test_diffusion2d.py"]]

[env.coverage]
description = "Check Coverage"
deps = ["pytest>=8", "coverage", "matplotlib", "numpy"]
commands = [
["coverage", "run", "-m", "pytest"],
["coverage", "html"],
["coverage", "report"]
]