⚠ This page is served via a proxy. Original site: https://github.com
This service does not collect credentials or authentication data.
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-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):
# assertion: float check
assert isinstance(w, float), "w should be a float"
assert isinstance(h, float), "h should be a float"
assert isinstance(dx, float), "dx should be a float"
assert isinstance(dy, float), "dy should 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., T_hot=700.):
# assertion: float check
assert isinstance(d, float), "d should be a float"
assert isinstance(T_cold, float), "T_cold should be a float"
assert isinstance(T_hot, float), "T_hot should 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
40 changes: 39 additions & 1 deletion tests/integration/test_diffusion2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,55 @@
"""

from diffusion2d import SolveDiffusion2D

import pytest
import numpy as np

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

w = 20.
h = 10.
dx = 0.5
dy = 0.2
d = 5.

solver.initialize_domain(w=w, h=h, dx=dx, dy=dy)
solver.initialize_physical_parameters(d=d, T_cold=200., T_hot=800.)

expected_dt = (dx ** 2 * dy ** 2) / (2 * d * (dx ** 2 + dy ** 2))
assert solver.dt == pytest.approx(expected_dt)

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

w = 20.
h = 10.
dx = 0.5
dy = 0.2
d = 5.
T_cold = 200.
T_hot = 800.
r, cx, cy = 2, 5, 5
r2 = r ** 2
nx = int(w / dx)
ny = int(h / dy)

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_actual = solver.set_initial_condition()
u_expected = T_cold * np.ones((nx, ny))

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

assert np.allclose(u_actual, u_expected), "Integration test failed with set_initial_condition."
63 changes: 60 additions & 3 deletions tests/unit/test_diffusion2d_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,81 @@
"""

from diffusion2d import SolveDiffusion2D

import pytest
import numpy as np

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

w = 20.
h = 10.
dx = 0.5
dy = 0.2

expected_nx = int(w / dx)
expected_ny = int(h / dy)

solver.initialize_domain(w=w, h=h, dx=dx, dy=dy)

assert solver.nx == expected_nx
assert solver.ny == expected_ny

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

d = 5.
T_cold = 200.
T_hot = 800.
dx = 0.2
dy = 0.1

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

solver.initialize_physical_parameters(d=d, T_cold=T_cold, T_hot=T_hot)

assert solver.dt == pytest.approx(expected_dt)

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

w = 20.
h = 10.
dx = 0.5
dy = 0.2
T_cold = 200.
T_hot = 800.
r, cx, cy = 2, 5, 5
r2 = r ** 2

solver.w = w
solver.h = h
solver.dx = dx
solver.dy = dy
solver.T_cold = T_cold
solver.T_hot = T_hot
solver.nx = int(w / dx)
solver.ny = int(h / dy)

u_actual = solver.set_initial_condition()
u_expected = 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_expected[i, j] = solver.T_hot

assert np.allclose(u_actual, u_expected), "Unit test failed with set_initial_condition."
11 changes: 11 additions & 0 deletions tox.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[tox]
env_list = ["test_env"]
no_package = true

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

commands = [
["coverage", "run", "-m", "pytest"],
["coverage", "report", "-m"]
]