-
Notifications
You must be signed in to change notification settings - Fork 5
/
poisson.py
212 lines (158 loc) · 5.98 KB
/
poisson.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
""" Solve the Poisson equation on cartesian coordinates in 3 dimensions
with Dirichlet boundary conditions.
\Delta u = f
"""
from grids import *
class Solver:
""" The base class for the other solvers """
def __init__(self, rhs, bc, atol=1.0E-6):
self.rhs = rhs
self.bc = bc
self.atol = atol
self.converged = False
self.sol = rhs.grid.field()
self.stepper = None
self.max_steps = 10000
self.impose_boundary_conditions()
def impose_boundary_conditions(self):
for key, value in self.bc.items():
self.sol.values[key] = value
def residual(self, sol, rhs):
nx, ny, nz = sol.grid.shape
dx, dy, dz = sol.grid.spacing()
dxi2, dyi2, dzi2 = dx**-2, dy**-2, dz**-2
u = sol.values
f = rhs.values
resid = sol.grid.field()
r = resid.values
for i in range(1, nx-1):
for j in range(1, ny-1):
for k in range(1, nz-1):
val = (u[i+1,j,k] -2*u[i,j,k] + u[i-1,j,k]) * dxi2 \
+(u[i,j+1,k] -2*u[i,j,k] + u[i,j-1,k]) * dyi2 \
+(u[i,j,k+1] -2*u[i,j,k] + u[i,j,k-1]) * dzi2 - f[i,j,k]
resid.values[(i, j, k)] = val
return resid
def solve(self):
step_count = 0
while True:
step_count += 1
self.step()
err = self.check_convergence()
print("{}, {:12.4e}".format(step_count, err))
if self.converged or step_count == self.max_steps:
break
if not self.converged:
raise Exception("No convergence")
def check_convergence(self):
resid = self.residual(self.sol, self.rhs)
max_err = np.max(np.abs(resid.values))
self.converged = False
if max_err < self.atol:
self.converged = True
return max_err
def solution(self):
return self.sol
class SimpleSolver(Solver):
""" A simple relaxation solver """
def __init__(self, rhs, bc, method="jacobi", atol=1.0E-6):
super(SimpleSolver, self).__init__(rhs, bc, atol)
if method == "jacobi":
self.stepper = JacobiStepper()
elif method == "gauss_seidel":
self.stepper = GaussSeidelStepper()
else:
raise Exception("No such stepper")
def step(self):
self.stepper.step(self.sol, self.rhs)
class MultiGridSolver(Solver):
""" The actual multigrid solver """
def __init__(self, rhs, bc, atol=1.0E-6):
super(MultiGridSolver, self).__init__(rhs, bc, atol)
self.multi_grid = MultiGrid(rhs.grid)
self.pre_smooth_iter = 3
self.post_smooth_iter = 3
self.max_steps = 1000
self.stepper = GaussSeidelStepper()
def step(self):
self.sol = self._do_multi_grid_step(self.sol, self.rhs)
def _do_multi_grid_step(self, sol, rhs):
self._do_smooth(sol, rhs, self.pre_smooth_iter)
if self.multi_grid.has_coarser(sol.grid):
resid = self.residual(sol, rhs)
rhs_c = self.multi_grid.coarsify(resid)
e_c = rhs_c.grid.field()
e_c = self._do_multi_grid_step(e_c, rhs_c)
sol_delta = self.multi_grid.refine(e_c)
sol.values = sol.values - sol_delta.values
self._do_smooth(sol, rhs, self.post_smooth_iter)
return sol
def _do_smooth(self, sol, rhs, num_iter):
for it in range(num_iter):
self.stepper.step(sol, rhs)
class JacobiStepper:
""" Jacobi method:
Discretized Poisson equation:
(u[i+1,j,k] + u[i-1,j,k]) * dxi2
+ (u[i,j+1,k] + u[i,j-1,k]) * dyi2
+ (u[i,j,k+1] + u[i,j,k-1]) * dzi2
- 2* (dxi2 + dyi2 + dzi2) u[i,j,k]
+ f[i,j,k] = 0
(u[i+1,j,k] + u[i-1,j,k]) * dxi2
+ (u[i,j+1,k] + u[i,j-1,k]) * dyi2
+ (u[i,j,k+1] + u[i,j,k-1]) * dzi2
+ f[i,j,k] = 2* (dxi2 + dyi2 + dzi2) u[i,j,k]
u[i,j,k] = (
(u[i+1,j,k] + u[i-1,j,k]) * dxi2
+ (u[i,j+1,k] + u[i,j-1,k]) * dyi2
+ (u[i,j,k+1] + u[i,j,k-1]) * dzi2
+ f[i,j,k]
) / (2*(dxi2 + dyi2 + dzi2))
"""
def step(self, field, rhs):
u = field.values
f = rhs.values
nx, ny, nz = field.grid.shape
dx, dy, dz = field.grid.spacing()
dxi2, dyi2, dzi2 = 1./dx**2, 1./dy**2, 1./dz**2
inv_denom = 1. / (2 * (dxi2 + dyi2 + dzi2))
u_new = np.array(u)
for i in range(1,nx-1):
for j in range(1,ny-1):
for k in range(1,nz-1):
u_new[i,j,k] = (
(u[i+1,j,k] + u[i-1,j,k]) * dxi2
+ (u[i,j+1,k] + u[i,j-1,k]) * dyi2
+ (u[i,j,k+1] + u[i,j,k-1]) * dzi2
- f[i,j,k]
) * inv_denom
field.values = u_new
class GaussSeidelStepper:
""" Gauss Seidel Method
"""
def step(self, field, rhs):
u = field.values
f = rhs.values
nx, ny, nz = field.grid.shape
dx, dy, dz = field.grid.spacing()
dxi2, dyi2, dzi2 = 1./dx**2, 1./dy**2, 1./dz**2
inv_denom = 1. / (2*(dxi2 + dyi2 + dzi2))
un = np.array(u)
for i in range(1,nx-1):
for j in range(1,ny-1):
for k in range(1,nz-1):
un[i,j,k] = (
(un[i+1,j,k] + un[i-1,j,k]) * dxi2
+ (un[i,j+1,k] + un[i,j-1,k]) * dyi2
+ (un[i,j,k+1] + un[i,j,k-1]) * dzi2
- f[i,j,k]
) * inv_denom
field.values = un
def solve(rhs, bc, edges):
""" Interface to the outside world for those who do not want to use the module classes """
domain = Domain(center=(0,0,0), edges=edges)
grid = Grid(domain, tuple(rhs.shape))
field_rhs = Field(grid)
field_rhs.values = np.array(rhs)
solver = MultiGridSolver(field_rhs, bc)
return solver.solve().solution()