-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathtest_diffusion2d_functions.py
More file actions
74 lines (56 loc) · 1.83 KB
/
test_diffusion2d_functions.py
File metadata and controls
74 lines (56 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
"""
Tests for functions in class SolveDiffusion2D
"""
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)