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

[heidrifx] Adding tests #17

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Step 4: Added Unit Tests
heidrifx committed Jan 21, 2025
commit e8cfec51b616622c942fe6d8def5f849905a1061
69 changes: 54 additions & 15 deletions tests/unit/test_diffusion2d_functions.py
Original file line number Diff line number Diff line change
@@ -2,25 +2,64 @@
Tests for functions in class SolveDiffusion2D
"""

import unittest
import numpy as np
from diffusion2d import SolveDiffusion2D

class TestDiffusion2DFunctions(unittest.TestCase):

def test_initialize_domain():
"""
Check function SolveDiffusion2D.initialize_domain
"""
solver = SolveDiffusion2D()
def test_initialize_domain(self):
"""
Check function SolveDiffusion2D.initialize_domain
"""
solver = SolveDiffusion2D()

nx, ny = 25., 30.
solver.initialize_domain(5., 6., 0.2, 0.2)

def test_initialize_physical_parameters():
"""
Checks function SolveDiffusion2D.initialize_domain
"""
solver = SolveDiffusion2D()
assert solver.nx == nx, f"Returned nx is incorrect: is {solver.nx}, should be {nx}"
assert solver.ny == ny, f"Returned ny is incorrect: is {solver.ny}, should be {ny}"


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

dt = 0.0019
solver.dx, solver.dy = 0.125, 0.75
solver.initialize_physical_parameters(4., 300., 700.)
self.assertAlmostEqual(solver.dt, dt, places=4,
msg=f"Returned dt is incorrect: is {solver.dt}, should be {dt}")


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

solver.dx = solver.dy = 0.01
solver.nx = solver.ny = 75
solver.T_cold, solver.T_hot = 300., 700.

solver_u = solver.set_initial_condition()

r, cx, cy = 2, 5, 5
r2 = r ** 2

u = solver.T_cold * np.ones((solver.nx, solver.ny))

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:
u[i, j] = solver.T_hot

# print(solver_u, u)

for i in range(solver.nx):
for j in range(solver.ny):
self.assertAlmostEqual(solver_u[i, j], u[i, j], places=4,
msg=f"Returned u is incorrect: is {solver_u[i, j]}, should be {u[i, j]}")