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
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.venv
__pycache__
htmlcov
.pytest_cache
.coverage
Binary file added coverage-report.pdf
Binary file not shown.
5 changes: 4 additions & 1 deletion diffusion2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,17 @@ def __init__(self):
self.dt = None

def initialize_domain(self, w=10., h=10., dx=0.1, dy=0.1):
assert(isinstance(w, float) and isinstance(h, float) and isinstance(dx, float) and isinstance(dy, 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.0, T_hot=700.0):
assert(isinstance(d, float) and isinstance(T_cold, float) and isinstance(T_hot, float))

self.D = d
self.T_cold = T_cold
self.T_hot = T_hot
Expand Down
3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
matplotlib
numpy
pytest
34 changes: 34 additions & 0 deletions tests/integration/test_diffusion2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,50 @@

from diffusion2d import SolveDiffusion2D

import pytest
import numpy as np

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

solver.initialize_domain()
solver.initialize_physical_parameters(5.0, 300.0, 700.0)

solver.initialize_physical_parameters(5.0, 300.0, 700.0)
assert solver.dt == pytest.approx(0.0005, rel=1e-4)

solver.initialize_physical_parameters(5.0, 500.0, 800.0)
assert solver.dt == pytest.approx(0.0005, rel=1e-4)

with pytest.raises(AssertionError):
solver.initialize_physical_parameters(5, 500, 800)


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

solver.initialize_domain(8.0, 8.0, 1.0, 1.0)
solver.initialize_physical_parameters(4.0, 0.0, 1.0)

# 8x8 domain with circle at (5,5)
expected_field = np.array(
[[0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 1., 1., 1., 0.],
[0., 0., 0., 0., 1., 1., 1., 0.],
[0., 0., 0., 0., 1., 1., 1., 0.],
[0., 0., 0., 0., 0., 0., 0., 0.]]
)

actual_field = solver.set_initial_condition()

error_norm = np.linalg.norm(expected_field - actual_field)
assert 0 == pytest.approx(error_norm, abs=1e-14)
48 changes: 48 additions & 0 deletions tests/unit/test_diffusion2d_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,71 @@

from diffusion2d import SolveDiffusion2D

import pytest
import numpy as np

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

solver.initialize_domain(1.5, 2.0, 0.2, 0.2)

assert solver.nx == 7
assert solver.ny == 10

with pytest.raises(AssertionError):
solver.initialize_domain(1, 2, 0.2, 0.2)


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

solver.dx = 0.1
solver.dy = 0.1
solver.nx = 100
solver.ny = 100

solver.initialize_physical_parameters(5.0, 300.0, 700.0)
assert solver.dt == pytest.approx(0.0005, rel=1e-4)

solver.initialize_physical_parameters(5.0, 500.0, 800.0)
assert solver.dt == pytest.approx(0.0005, rel=1e-4)

with pytest.raises(AssertionError):
solver.initialize_physical_parameters(5, 500, 800)


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

solver.T_cold = 0.0
solver.T_hot = 1.0
solver.dx = 1.0
solver.dy = 1.0
solver.nx = 8
solver.ny = 8

# 8x8 domain with circle at (5,5)
expected_field = np.array(
[[0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 1., 1., 1., 0.],
[0., 0., 0., 0., 1., 1., 1., 0.],
[0., 0., 0., 0., 1., 1., 1., 0.],
[0., 0., 0., 0., 0., 0., 0., 0.]]
)

actual_field = solver.set_initial_condition()

error_norm = np.linalg.norm(expected_field - actual_field)
assert 0 == pytest.approx(error_norm, abs=1e-14)
8 changes: 8 additions & 0 deletions tox.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[tox]
envlist = ["test"]
requires = ["tox>=4"]

[env.test]
description = "Run test suite with pytest."
deps = "-r requirements.txt"
commands = [["python","-m","pytest"]]