-
Notifications
You must be signed in to change notification settings - Fork 0
/
BEM.py
170 lines (132 loc) · 4.15 KB
/
BEM.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
# -*- coding: utf-8 -*-
import math
from mpl_toolkits.mplot3d import axes3d, Axes3D
import matplotlib.pyplot as plt
import numpy as np
def distance(point_one, point_two):
sumation = 0
for i in range(3):
sumation += (point_two[i] - point_one[i])**2
return math.sqrt(sumation)
def convert_to_cartesian(r):
theta = r[1]
phi = r[2]
r = r[0]
x = r * math.sin(theta) * math.cos(phi)
y = r * math.sin(theta) * math.sin(phi)
z = r * math.cos(theta)
return [x, y, z]
def segmentation(n):
radius = 4
nodes = [[radius, 0, 0]]
steps = 2 * math.pi / n
for phi in range(0, n):
for theta in range(1, int(n/2)):
nodes.append([radius, theta*steps, phi*steps])
nodes.append([radius, math.pi, 0])
return nodes
def draw_plot(segmentaion, target_point):
ax = plt.axes(projection ='3d')
# defining all 3 axes
dots = segmentation(segmentaion)
x = []
y = []
z = []
for item in dots:
x.append(convert_to_cartesian(item)[0])
y.append(convert_to_cartesian(item)[1])
z.append(convert_to_cartesian(item)[2])
x.append(target_point[0])
y.append(target_point[1])
z.append(target_point[2])
# plotting
ax.plot3D(x, y, z, 'k.', alpha=.2)
ax.set_title('BEM (Boundary Element Method)')
plt.show()
def gradian_u_dot_n(point_one, point_two):
xi = point_one[0]
yi = point_one[1]
zi = point_one[2]
xj = point_two[0]
yj = point_two[1]
zj = point_two[2]
alpha = xj**2 + yj**2 + zj**2
u = (xi-xj)**2 + (yi-yj)**2 + (zi-zj)**2
image = xj*(xi-xj) + yj*(yi-yj) + zj*(zi-zj)
return pow(alpha, -0.5)*pow(u, -1.5)*image
def potential(point):
const = 1
return const * math.sin(point[1])
def one_over_r(point1, point2):
xi = point1[0]
yi = point1[1]
zi = point1[2]
xj = point2[0]
yj = point2[1]
zj = point2[2]
u = (xi-xj)**2 + (yi-yj)**2 + (zi-zj)**2
return pow(u, -0.5)
def potential_of_target_point(point):
return round(2*math.pi*potential(point), 4)
def value_matrix(seg):
carte_points = []
dots = segmentation(seg)
for item in dots:
carte_points.append(convert_to_cartesian(item))
final_matrix = []
for i in carte_points:
row = []
for j in carte_points:
if (i == j):
row.append(potential_of_target_point(i))
else:
row.append(gradian_u_dot_n(i, j))
final_matrix.append(row)
return final_matrix
def coef_matrix(seg):
carte_points = []
dots = segmentation(seg)
for item in dots:
carte_points.append(convert_to_cartesian(item))
final_matrix = []
for i in carte_points:
row = []
for j in carte_points:
if (i == j):
row.append(0.0)
else:
row.append(one_over_r(j, i))
final_matrix.append(row)
return final_matrix
def calc_electric_field(seg):
count_dots = int((seg**2 - 2*seg + 4)/2)
I = []
for i in range(0,count_dots):
I.append(1)
A = np.array(value_matrix(seg)).dot(I)
B = np.array(coef_matrix(seg))
result = np.linalg.inv(B).dot(A)
rounded = []
for i in result:
rounded.append(round(i, 3))
return rounded
def electric_field_on_target_point(seg, r):
carte_points = []
dots = segmentation(seg)
for item in dots:
carte_points.append(convert_to_cartesian(item))
electric_field_matrix = calc_electric_field(seg)
print('dots: ', int((seg**2 - 2*seg + 4)/2))
print('target point coordinate: ', r)
sum_electric_field = 0
sum_potential = 0
index = 0
for i in carte_points:
sum_electric_field += one_over_r(r, i)*electric_field_matrix[index]
sum_potential += potential(i) * gradian_u_dot_n(r, i)
index += 1
print('value of electric field on target point: ', round((sum_electric_field - sum_potential) / (4*math.pi), 4))
seg = 32
target_point = [5, 0, 5]
draw_plot(seg, target_point)
electric_field_on_target_point(seg, target_point)