Skip to content

[puranivt] Adding tests #12

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 6 commits 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
432 changes: 432 additions & 0 deletions README.md

Large diffs are not rendered by default.

Empty file added __init__.py
Empty file.
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
Empty file added tests/integration/__init__.py
Empty file.
28 changes: 27 additions & 1 deletion tests/integration/test_diffusion2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,43 @@
"""

from diffusion2d import SolveDiffusion2D

import numpy as np

def test_initialize_physical_parameters():
"""
Checks function SolveDiffusion2D.initialize_domain
"""
solver = SolveDiffusion2D()
w = 20.
h = 30.
dx = 0.2
dy = 0.4
d = 2.
T_cold = 200.
T_hot = 500.
solver.initialize_domain(w=w, h=h, dx=dx, dy=dy)
solver.initialize_physical_parameters(d=d, T_cold=T_cold, T_hot=T_hot)
assert round(solver.dt, 3) == 0.008


def test_set_initial_condition():
"""
Checks function SolveDiffusion2D.get_initial_function
"""
solver = SolveDiffusion2D()
w = 3.
h = 2.5
dx = 1.
dy = 0.5
d = 2.
T_cold = 200.
T_hot = 500.
solver.initialize_domain(w=w, h=h, dx=dx, dy=dy)
solver.initialize_physical_parameters(d=d, T_cold=T_cold, T_hot=T_hot)
u = solver.set_initial_condition()
u_expected = np.array([
[200., 200., 200., 200., 200.],
[200., 200., 200., 200., 200.],
[200., 200., 200., 200., 200.]
])
np.testing.assert_array_equal(u, u_expected, "Expected array:\n{}\nGot:\n{}\n".format(u_expected, u))
Empty file added tests/unit/__init__.py
Empty file.
65 changes: 50 additions & 15 deletions tests/unit/test_diffusion2d_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,59 @@
"""

from diffusion2d import SolveDiffusion2D
import unittest

class TestDiffusion2D(unittest.TestCase):

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


def test_initialize_physical_parameters():
"""
Checks function SolveDiffusion2D.initialize_domain
"""
solver = SolveDiffusion2D()
def test_initialize_domain(self):
"""
Check function SolveDiffusion2D.initialize_domain
"""
self.solver.initialize_domain(w=20.,h=30.,dx=0.2,dy=0.4)

# Check that nx and nx are correctly computed based on w, dx and h, dy resp.
self.assertEqual(self.solver.nx, 100)
self.assertEqual(self.solver.ny, 75)

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

def test_initialize_physical_parameters(self):
"""
Checks function SolveDiffusion2D.initialize_domain
"""
self.solver.dx = 0.2
self.solver.dy = 0.4
self.solver.initialize_physical_parameters(d=2.,T_cold=200.,T_hot=500.)

# Check that value of dt is correctly computed based on input settings
self.assertAlmostEqual(self.solver.dt, 0.008)


def test_set_initial_condition(self):
"""
Checks function SolveDiffusion2D.get_initial_function
"""
self.solver.nx = 100
self.solver.ny = 150
self.solver.T_cold = 100.
self.solver.dx = 0.2
self.solver.dy = 0.2
self.solver.T_hot = 400.
u = self.solver.set_initial_condition()

# Check shape is appropriate
self.assertEqual(u.shape, (self.solver.nx, self.solver.ny))

# Check that corners are cold
self.assertEqual(u[0, 0], self.solver.T_cold)
self.assertEqual(u[0, -1], self.solver.T_cold)
self.assertEqual(u[-1, 0], self.solver.T_cold)
self.assertEqual(u[-1, -1], self.solver.T_cold)

# checking that values in the circle center are hot cannot be tested as it depends on the values
# of r, cx, cy. These values are not parameters of the class and hardcoding them here would bind
# the test to the settings of the actual function thus losing generality. This would have been
# testable if there was a choice to set these values by the user.
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"]]
Binary file added tox_output.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.