-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBackup.py
205 lines (178 loc) · 5.88 KB
/
Backup.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
from matplotlib.pylab import *
import numpy as np
import math as math
def make_points(num_points,r_a, r_b):
dx = 2*pi/num_points
x = np.zeros(num_points+1)
y = np.zeros(num_points+1)
for i in range(num_points+1):
x[i] = r_a*np.cos(i*dx)
y[i] = r_b*np.sin(i*dx)
return x,y
"""x,y = make_points(4,2,2)
plot(x,y)
show()
print x
print y"""
"""
def make_points(N,r_a,r_b):
N = N/4 *4
Np = N/4
x = np.zeros(N+1)
y = np.zeros(N+1)
d1 = -r_a
d2 = r_a
for i in range(Np+1):
x[i] = d1 +(d2-d1)/2 *(1-np.cos(i*np.pi/Np))
y[i] = -r_a
x[i+Np] = r_a
y[i+Np] = d1 +(d2-d1)/2 *(1-np.cos(i*np.pi/Np))
x[i+2*Np] = -(d1 +(d2-d1)/2 *(1-np.cos(i*np.pi/Np)))
y[i+2*Np] = r_a
x[i+3*Np] = -r_a
y[i+3*Np] = -(d1 +(d2-d1)/2 *(1-np.cos(i*np.pi/Np)))
return x ,y
"""
x,y =make_points(100,1,1)
plt.figure()
plt.plot(x,y, "-o")
plt.axis([-3,3,-3,3])
plt.show()
def make_angle(N,r_a,r_b):
angle = np.zeros(N-1)
x,y = make_points(N,r_a,r_b)
matrix_ = np.zeros((N,N))
for i in range(N):
x_first = (x[i]+x[i+1])/2.0
y_first = (y[i]+y[i+1])/2.0
for k in range (N):
if i == k:
matrix_[i][k] = -np.pi
else:
xa = x[k] - x_first
xb = x[k+1] - x_first
ya = y[k] - y_first
yb = y[k+1] - y_first
matrix_[i][k] = -np.arccos((xa*xb + ya*yb)/(np.sqrt(xa**2 + ya**2) * np.sqrt(xb**2 + yb**2)))
if (np.isnan(matrix_[i][k])):
matrix_[i][k] = 0
return matrix_
def integral_1(N,r_a,r_b,direction1):
x,y = make_points(N,r_a,r_b)
traps1 = 0
traps2 = 0
Matrix_B = np.zeros(N)
for i in range(N):
integral_x = 0
integral_66 = 0
x0 = 0.5*(x[i]+x[i+1])
y0 = 0.5*(y[i]+y[i+1])
for j in range(N-1):
rad_1 = np.sqrt((x0-x[j])**2 + (y0-y[j])**2)
rad_2 = np.sqrt((x0-x[j+1])**2 + (y0-y[j+1])**2)
n1 = -((x[j]+x[j+1])/(2*r_a**2)) / np.sqrt( ((x[j]+x[j+1])/(2*r_a**2))**2 + ((y[j]+y[j+1])/(2*r_b**2))**2 )
if direction1 == 11:
traps1x = n1 * np.log(rad_1)
traps2x = n1 * np.log(rad_2) #-((x[j+1]+x[j+2])/(2*r_a**2)) / np.sqrt( ((x[j+1]+x[j+2])/(2*r_a**2))**2 + ((y[j+1]+y[j+2])/(2*r_b**2))**2 )* np.log(rad_2)
ds = np.sqrt((x[j+1]-x[j])**2 + (y[j+1] - y[j])**2)
integral_x = integral_x + (traps1x + traps2x) * ds * 0.5
else :
r1a = (x[j] + x[j+1])*0.5 ; r2a =0.5*(y[j] + y[j+1])
n1a = -((x[j]+x[j+1])/ (2*r_a**2)) / np.sqrt( ((x[j]+x[j+1])/(2*r_a**2))**2 + ((y[j]+y[j+1])/(2*r_b**2))**2 )
n2a = -((y[j]+y[j+1])/(2*r_b**2))/np.sqrt( ((x[j]+x[j+1])/(2*r_a**2))**2 + ((y[j]+y[j+1])/(2*r_b**2))**2 )
crosses1 = r1a*n2a - r2a*n1a
r1b = (x[j+1] + x[j+2])*0.5 ; r2b =0.5*(y[j+1] + y[j+2])
n1b = -((x[j+1]+x[j+2])/(2*r_a**2))/np.sqrt( ((x[j+1]+x[j+2])/(2*r_a**2))**2 + ((y[j+1]+y[j+2])/(2*r_b**2))**2 )
n2b = -((y[j+1]+y[j+2])/(2*r_b**2))/np.sqrt( ((x[j+1]+x[j+2])/(2*r_a**2))**2 + ((y[j+1]+y[j+2])/(2*r_b**2))**2 )
crosses2 = r1b*n2b - r2b*n1b
traps3 = np.log(rad_1) * crosses1
traps4 = np.log(rad_2) * crosses2
ds = np.sqrt((x[j+1]-x[j])**2 + (y[j+1] - y[j])**2)
integral_66 = integral_66 + (traps3 +traps4)* ds * 0.5
if direction1 == 11:
Matrix_B[i] = integral_x
else :
Matrix_B[i] = integral_66
return Matrix_B
def solver1(N,r_a,r_b,direction1):
return np.linalg.solve(make_angle(N,r_a,r_b),integral_1(N,r_a,r_b,direction1))
def added_mass(N,r_a,r_b):
phix = solver1(N,r_a,r_b,direction1 =11)
phi6 = solver1(N,r_a,r_b,direction1 =66)
x,y = make_points(N,r_a,r_b)
add_m11 = 0
add_m22 = 0
add_m66 = 0
for i in range(N-2):
rad_1 = np.sqrt( ((x[i]+x[i+1])/(2*r_a**2))**2 + ((y[i] +y[i+1])/(2*r_b**2))**2)
rad_2 = np.sqrt( ((x[i+1]+x[i+2])/(2*r_a**2))**2 + ((y[i+1]+y[i+2])/(2*r_b**2))**2)
ds = np.sqrt((x[i+1]-x[i])**2 + (y[i+1] - y[i])**2)
traps1 = phix[i] * -(x[i]+x[i+1])/(2*r_a**2)/rad_1
traps2 = phix[i+1] * -(x[i+1]+x[i+2])/(2*r_a**2)/rad_2
r1x = (x[i] + x[i+1])*0.5 ; r2x = 0.5*(y[i] + y[i+1])
n1x = -(x[i] + x[i+1])/(2*r_a**2)/rad_1
n2x = -(y[i] + y[i+1])/(2*r_b**2)/rad_1
crosses1 = r1x*n2x - r2x*n1x
r1y = (x[i+1] + x[i+2])*0.5 ; r2y = 0.5*(y[i+1] + y[i+2])
n1y = -(x[i+1] + x[i+2])/(2*r_a**2)/rad_2
n2y = -(y[i+1] + y[i+2])/(2*r_b**2)/rad_2
crosses2 = r1y*n2y - r2y*n1y
traps3 = phi6[i] * crosses1
traps4 = phi6[i+1] * crosses2
add_m11 = add_m11 + (traps1 + traps2)*ds * 0.5
add_m66 = add_m66 + (traps3 + traps4)*ds *0.5
return add_m11, add_m66
N = 360
r_a = 1
r_b = 1
a11, a66 = added_mass(N,r_a,r_b)
#a11_e = 4.754 *r_a**2
#a66_e = 0.725 *r_a**2
a11_e = np.pi*r_b**2
a66_e = (1.0/8.0)* np.pi*(r_a**2-r_b**2)**2
print a11#,a66
print "exact a11: ",a11_e#, a66_e
print a66
print "exact a66: ",a66_e
"""
N = 1000
r_a = 1
r_b = 1
equal = r_a - r_b
a11_e = 4.754 *r_a**2
a66_e = 0.725 *r_a**2
a11, a66 = added_mass(N,r_a,r_b)
print a11
print a11_e
print "----------"
print a66
print a66_e
"""
"""
if equal == 0:
a_m11,a_m66 = added_mass(N,r_a,r_b)
error1 = ((((r_a**2*np.pi)-float(a_m11))/(r_a**2*np.pi))*100)
print "m11= %.3f m66= %.f,m11 error in percent: %.2f %% " %(a_m11,a_m66, error1)
thet = np.zeros(N)
om = 2.0*pi*r_a
dx = 2*pi/N
for i in range(N):
thet[i] = i*dx
solution = solver1(N,r_a,r_b,direction1 = 11)
exact_1 = -np.cos(thet)
diff = max(exact_1 - solution)
plot(exact_1,"g")
plot(solution, "r")
plt.legend(['Exact ', 'Numerical'])
title("Fluid Potential over Circle, running %.d times, error: %.2f %% \n m11 = %.2f, m66 = %.f , error in m11 = %.2f %% " %(N,100*diff/max(exact_1), a_m11 , a_m66, error1))
show()
else :
a_m11,a_m66 = added_mass(N,r_a,r_b)
error1 = (((r_b**2*np.pi)-a_m11)/(r_b**2*np.pi))*100
exact6 = (np.pi/8)*(r_a**2-r_b**2)**2
error6 = (((exact6-a_m66 ) / (exact6)))*100
print "m11: " ,a_m11 ,"error m11 : %.2f %%, error m66 %.2f %% running %.d times" %(error1,error6, N)
plot(solver1(N,r_a,r_b,direction1 = 11), "r")
title("Distribution of potential over an ellipse running %.d times \n m11: %.2f , error m11 : %.2f %% \n m66 = %.2f , error m66= %.2f %%, r_a = %.d, r_b= %.d " %(N,a_m11,error1,a_m66,error6,r_a,r_b))
show()
"""