-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathtest_diffusion2d_functions.py
More file actions
67 lines (49 loc) · 1.69 KB
/
test_diffusion2d_functions.py
File metadata and controls
67 lines (49 loc) · 1.69 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
"""
Tests for functions in class SolveDiffusion2D
"""
from diffusion2d import SolveDiffusion2D
import numpy as np
import pytest
@pytest.fixture
def solver():
"""Fixture to provide a fresh instance of the solver for each test."""
return SolveDiffusion2D()
def test_initialize_domain(solver):
"""initialize_domain sets attributes and computes nx/ny correctly."""
solver.initialize_domain(w=2.0, h=4.0, dx=0.1, dy=0.1)
assert solver.nx == 20
assert solver.ny == 40
assert solver.dx == 0.1
assert solver.dy == 0.1
assert solver.w == 2.0
assert solver.h == 4.0
def test_initialize_physical_parameters(solver):
"""initialize_physical_parameters stores values and computes stable dt."""
# Manually setting dependencies to avoid calling initialize_domain
solver.dx = 0.1
solver.dy = 0.1
solver.initialize_physical_parameters(d=4.0, T_cold=300.0, T_hot=700.0)
expected_dt = 0.000625
assert solver.D == 4.0
assert solver.T_cold == 300.0
assert solver.T_hot == 700.0
assert pytest.approx(solver.dt) == expected_dt
def test_set_initial_condition(solver):
"""
Checks function SolveDiffusion2D.set_initial_function
"""
solver.nx = 100
solver.ny = 100
solver.dx = 0.1
solver.dy = 0.1
solver.T_cold = 300.0
solver.T_hot = 700.0
u = solver.set_initial_condition()
# Check shape
assert u.shape == (100, 100)
# Check a corner (should be cold)
assert u[0, 0] == 300.0
# Check the center (5, 5) -> index (50, 50) (should be hot)
assert u[50, 50] == 700.0
# Check that we actually have both temperatures present
assert np.all((u == 300.0) | (u == 700.0))