-
Notifications
You must be signed in to change notification settings - Fork 1
/
Laplace.py
99 lines (77 loc) · 2.12 KB
/
Laplace.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
# Laplaceova rovnice - reseni
import numpy as np
import matplotlib.pyplot as plt
d=10.0 # velikost oblasti dxd
Nd=10 # pocet deleni ve smeru x a y
N=Nd*Nd # pocet neznamych
dx=d/(Nd-1) # krok site ve smeru x
dy=dx # krok site ve smeru y
A=np.zeros([N,N]) # matice soustavy
B=np.zeros(N) # vektor prave strany
def nastav_rovnici_i(i):
"""
Nastavi koeficienty pro i-tou rovnici
"""
A[i,i]=-4.0
A[i,i-1]=1.0
A[i,i-Nd]=1.0
A[i,i+1]=1.0
A[i,i+Nd]=1.0
def nastav_dirichlet_i(i,Phi_i):
"""
Nastavi radek v i-te rovnici na Dirichletovu okrajovou podminku
"""
A[i,:]=0.0 # vynuluj koeficienty
B[i]=Phi_i # nastav pozadovany potencial do prave strany
def nastav_okrajovou_podminku(ix,iy):
"""
Nastavi hodnotu okrajove podminky pro bod ix,iy
"""
if (ix==0):
# leva hranice
nastav_dirichlet_i(ix,0.0)
return
if (ix==Nd-1):
# prava hranice
nastav_dirichlet_i(ix,1.0)
return
if (iy==0):
# dolni hranice
nastav_dirichlet_i(iy,0.0)
return
if (iy==Nd-1):
# horni hranice
nastav_dirichlet_i(iy,1.0)
return
# nastav koeficienty v matici A
for i in range(N):
nastav_rovnici_i(i)
# nastav Dirichletovu podminku na leve a prave strane
for iy in range(Nd):
for ix in range(0,Nd,Nd-1):
nastav_okrajovou_podminku(ix,iy)
# nastav Dirichletovu podminku na dolni a horni strane
for iy in range(0,Nd,Nd-1):
for ix in range(Nd):
nastav_okrajovou_podminku(ix,iy)
# vyres soustavu rovnic
Phi=np.linalg.solve(A,B)
# vykresleni reseni
plt.figure(figsize=(6, 6))
plt.imshow(Phi, aspect='auto', cmap='rainbow')
plt.colorbar()
plt.title('Potencial')
plt.show()
# nebo
x = np.arange(0, d+dx/2, dx) # hodnoty x souradnic sitovych bodu
y = np.arange(0, d+dy/2, dy)
fig = plt.figure()
ax = fig.add_subplot(111, projection = '3d')
surface=ax.plot_surface(x, y, Phi,cmap='viridis')
fig.colorbar(surface, ax=ax, shrink=0.5, aspect=5)
ax.set_xlabel('x [m]')
ax.set_ylabel('y [m]')
ax.set_zlabel('Phi [V]')
plt.savefig('plot.png')
plt.show()
print("konec")