-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathtest_diffusion2d_functions.py
More file actions
83 lines (66 loc) · 1.81 KB
/
test_diffusion2d_functions.py
File metadata and controls
83 lines (66 loc) · 1.81 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
75
76
77
78
79
80
81
82
83
"""
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()
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_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.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."