forked from huangynj/poisson
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_solvers.py
85 lines (56 loc) · 2.08 KB
/
test_solvers.py
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
84
85
import math
import unittest
from poisson import *
from grids import Domain, Grid
class SolverTests(unittest.TestCase):
def _prepare_gaussian_example(self):
domain = Domain(center=(0, 0, 0), edges=(1, 1, 1))
grid = Grid(domain, shape=(17, 17, 17))
# Prepare exact solution
def exact_solution(x, y, z):
return math.exp(-(x ** 2 + y ** 2 + z ** 2))
exact = grid.field_from_function(exact_solution)
# Prepare problem to solve
def the_rhs(x, y, z):
r2 = x ** 2 + y ** 2 + z ** 2
return (4 * r2 - 6) * math.exp(-r2)
rhs = grid.field_from_function(the_rhs)
bc = {}
for ind in grid.boundary:
x, y, z = grid.loc(ind)
bc[ind] = exact_solution(x, y, z)
return grid, rhs, bc, exact
def _error_of_solution(self, exact, approx):
return np.max(np.abs(exact.values - approx.values))
@unittest.skip("")
def test_simple_jacobi(self):
grid, rhs, bc, exact = self._prepare_gaussian_example()
solver = SimpleSolver(rhs, bc, method='jacobi')
try:
solver.solve()
except Exception as e:
self.fail()
err = self._error_of_solution(exact, solver.solution())
self.assertTrue(err < 2.0E-3)
@unittest.skip("")
def test_simple_gauss_seidl(self):
grid, rhs, bc, exact = self._prepare_gaussian_example()
solver = SimpleSolver(rhs, bc, method='gauss_seidel')
try:
solver.solve()
except Exception as e:
self.fail()
err = self._error_of_solution(exact, solver.solution())
self.assertTrue(err < 2.0E-3)
#@unittest.skip("")
def test_multigrid_solver(self):
grid, rhs, bc, exact = self._prepare_gaussian_example()
solver = MultiGridSolver(rhs, bc)
try:
solver.solve()
except Exception as e:
self.fail()
err = self._error_of_solution(exact, solver.solution())
self.assertTrue(err < 2.0E-3)
if __name__ == '__main__':
unittest.main()