-
Notifications
You must be signed in to change notification settings - Fork 0
/
Grad_Div_Curl_Laplace2.py
325 lines (268 loc) · 12.2 KB
/
Grad_Div_Curl_Laplace2.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
# -*- coding: utf-8 -*-
"""
Created on Wed Apr 22 12:46:19 2020
@author: user
"""
import numpy as np
class BoundaryConditionError(Exception):
"""경계조건과 관련된 예외"""
pass
class __GDCL__:
def __init__(self, n, map_size, UP='close', DOWN='close', LEFT='close', RIGHT='close'):
self.n = n
self.map_size = map_size
self.unit_size = map_size/n
self.UP = UP
self.DOWN = DOWN
self.LEFT = LEFT
self.RIGHT = RIGHT
boundary_list = [self.UP, self.DOWN, self.LEFT, self.RIGHT]
boundary_condition_list =['close', 'open', 'loop']
for bl in boundary_list:
Error = True
for bcl in boundary_condition_list:
if bl==bcl:
Error = False
break
if Error:
raise BoundaryConditionError('''경계조건 값은 'close', 'open', 'loop'만 가능''')
if (self.UP=='loop' and self.DOWN!='loop') or (self.UP!='loop' and self.DOWN=='loop'):
raise BoundaryConditionError('''UP이 'loop'면 DOWN도 'loop'여야함. 그 반대도 마찬가지임''')
elif (self.LEFT=='loop' and self.RIGHT!='loop') or (self.LEFT!='loop' and self.RIGHT=='loop'):
raise BoundaryConditionError('''LEFT가 'loop'면 RIGHT도 'loop'여야함. 그 반대도 마찬가지임''')
def Grad(self, field):
Gradient = [[[0.0 for i in range(self.n)] for j in range(self.n)]for k in range(2)]
for i in range(self.n):
for j in range(self.n):
if field[i][j] != None and not np.isnan(field[i][j]):
G = 0
try:
if j>0 and not np.isnan(field[i][j-1]):
G += field[i][j]-field[i][j-1]
except:
pass
try:
if not np.isnan(field[i][j+1]):
G += field[i][j+1]-field[i][j]
except:
pass
Gradient[0][i][j] = G/(2*self.unit_size)
G = 0
try:
if i>0 and not np.isnan(field[i-1][j]):
G += field[i][j]-field[i-1][j]
except:
pass
try:
if not np.isnan(field[i+1][j]):
G += field[i+1][j]-field[i][j]
except:
pass
Gradient[1][i][j] = G/(2*self.unit_size)
elif np.isnan(field[i][j]):
Gradient[0][i][j] = np.nan
Gradient[1][i][j] = np.nan
else:
Gradient[0][i][j] = None
Gradient[1][i][j] = None
return Gradient
def Div(self, vector_field):
Divergence = [[0.0 for i in range(self.n)] for j in range(self.n)]
for i in range(self.n):
for j in range(self.n):
if vector_field[0][i][j] != None and not np.isnan(vector_field[0][i][j]):
D = 0
try:
if j>0 and not np.isnan(vector_field[0][i][j-1]):
D += vector_field[0][i][j]-vector_field[0][i][j-1]
elif j==0:
if self.LEFT == 'close':
D += vector_field[0][i][j]
else:
D += vector_field[0][i][j]
except:
if self.LEFT == 'close':
D += vector_field[0][i][j]
pass
try:
if not np.isnan(vector_field[0][i][j+1]):
D += vector_field[0][i][j+1]-vector_field[0][i][j]
else:
D += -vector_field[0][i][j]
except:
if self.RIGHT == 'close':
D += -vector_field[0][i][j]
pass
Divergence[i][j] += D/(2*self.unit_size)
D = 0
try:
if i>0 and not np.isnan(vector_field[1][i-1][j]):
D += vector_field[1][i][j]-vector_field[1][i-1][j]
elif i==0:
if self.DOWN == 'close':
D += vector_field[1][i][j]
else:
D += vector_field[1][i][j]
except:
if self.DOWN == 'close':
D += vector_field[1][i][j]
pass
try:
if not np.isnan(vector_field[1][i+1][j]):
D += vector_field[1][i+1][j]-vector_field[1][i][j]
else:
D += -vector_field[1][i][j]
except:
if self.UP == 'close':
D += -vector_field[1][i][j]
pass
Divergence[i][j] += D/(2*self.unit_size)
elif np.isnan(vector_field[0][i][j]):
Divergence[i][j] = np.nan
else:
Divergence[i][j] = None
return Divergence
def Curl(self, vector_field):
curl = [[0.0 for i in range(self.n)] for j in range(self.n)]
for i in range(self.n):
for j in range(self.n):
if vector_field[0][i][j] != None and not np.isnan(vector_field[0][i][j]):
C = 0
try:
if j>0 and not np.isnan(vector_field[1][i][j-1]):
C += vector_field[1][i][j]-vector_field[1][i][j-1]
#else:
# C += vector_field[1][i][j]
except:
#C += vector_field[1][i][j]
pass
try:
if not np.isnan(vector_field[1][i][j+1]):
C += vector_field[1][i][j+1]-vector_field[1][i][j]
except:
#C += -vector_field[1][i][j]
pass
try:
if i>0 and not np.isnan(vector_field[0][i-1][j]):
C -= vector_field[0][i][j]-vector_field[0][i-1][j]
#else:
# C -= vector_field[0][i][j]
except:
#C -= vector_field[0][i][j]
pass
try:
if not np.isnan(vector_field[0][i+1][j]):
C -= vector_field[0][i+1][j]-vector_field[0][i][j]
except:
#C -= -vector_field[0][i][j]
pass
curl[i][j] += C/(2*self.unit_size)
elif np.isnan(vector_field[0][i][j]):
curl[i][j] = np.nan
else:
curl[i][j] = None
return curl
def Laplacian(self, field):
Laplace = [[0.0 for i in range(self.n)] for j in range(self.n)]
for i in range(self.n):
for j in range(self.n):
L = 0
num = 0
try:
if i>0 and not np.isnan(field[i-1][j]):
L += field[i-1][j]
num += 1
except:
pass
try:
if not np.isnan(field[i+1][j]):
L += field[i+1][j]
num += 1
except:
pass
try:
if j>0 and not np.isnan(field[i][j-1]):
L += field[i][j-1]
num += 1
except:
pass
try:
if not np.isnan(field[i][j+1]):
L += field[i][j+1]
num += 1
except:
pass
try:
if not np.isnan(field[i][j]):
L -= num*field[i][j]
Laplace[i][j] = L/(self.unit_size**2)
else:
Laplace[i][j] = np.nan
except:
Laplace[i][j] = None
return Laplace
if __name__ == "__main__":
import matplotlib.pyplot as plt
from matplotlib import animation
import copy
n = 64
map_size = 6
unit_size = map_size/n
GDCL = __GDCL__(n, map_size)
time = 100
dt = 0.1
x = np.linspace(-map_size/2, map_size/2, n)
y = np.linspace(-map_size/2, map_size/2, n)
X,Y = np.meshgrid(x, y)
T = np.array([[0. for i in range(n)] for j in range(n)])
for i in range(n):
for j in range(n):
if (i-32)**2+(j-32)**2 < 100:
T[i][j] = 100.
if (i-32)**2+(j-63)**2 < 400:
T[i][j] = np.nan
if (i-32)**2+j**2 < 400:
T[i][j] = np.nan
#T = (1 - X / 2 + X**5 + Y**3) * np.exp(-X**2 - Y**2)
G_T = np.array(GDCL.Grad(T))
level = np.linspace(T.reshape(-1,1).min(), T.reshape(-1,1).max(), 50)
fig = plt.figure(figsize=(10, 8))
# Varying density along a streamline
ax0 = plt.axes(xlim=(-map_size/2, map_size/2), ylim=(-map_size/2, map_size/2))
cp = ax0.contourf(X, Y, T, 8, levels = level, cmap = 'hot')
ax0.contour(X, Y, T, 8, colors='black',levels = level, linewidth=.5)
fig.colorbar(cp)
ax0.streamplot(X, Y, G_T[0], G_T[1], density=[.5, 1])
ax0.set_title('Varying Density')
plt.show()
D_G_T = np.array(GDCL.Div(G_T))
level = np.linspace(D_G_T.reshape(-1,1).min(), D_G_T.reshape(-1,1).max(), 50)
fig1 = plt.figure(figsize=(10, 8))
# Varying density along a streamline
ax1 = plt.axes(xlim=(-map_size/2, map_size/2), ylim=(-map_size/2, map_size/2))
cp = ax1.contourf(X, Y, D_G_T, 8, levels = level, cmap = 'hot')
ax1.contour(X, Y, D_G_T, 8, colors='black',levels = level, linewidth=.5)
fig1.colorbar(cp)
ax1.set_title('Varying Density')
plt.show()
L_T = np.array(GDCL.Laplacian(T))
level = np.linspace(L_T.reshape(-1,1).min(), L_T.reshape(-1,1).max(), 50)
fig2 = plt.figure(figsize=(10, 8))
# Varying density along a streamline
ax2 = plt.axes(xlim=(-map_size/2, map_size/2), ylim=(-map_size/2, map_size/2))
cp = ax2.contourf(X, Y, L_T, 8, levels = level, cmap = 'hot')
ax2.contour(X, Y, L_T, 8, colors='black',levels = level, linewidth=.5)
fig2.colorbar(cp)
ax2.set_title('Varying Density')
plt.show()
C_G_T = np.array(GDCL.Curl(G_T))
level = np.linspace(C_G_T.reshape(-1,1).min(), C_G_T.reshape(-1,1).max(), 50)
fig3 = plt.figure(figsize=(10, 8))
# Varying density along a streamline
ax3 = plt.axes(xlim=(-map_size/2, map_size/2), ylim=(-map_size/2, map_size/2))
cp = ax3.contourf(X, Y, C_G_T, 8, levels = level, cmap = 'hot')
ax3.contour(X, Y, C_G_T, 8, colors='black',levels = level, linewidth=.5)
fig3.colorbar(cp)
ax3.set_title('Varying Density')
plt.show()
GDCL2 = __GDCL__(n, map_size,RIGHT='loop')