-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNew_shitz.py
167 lines (150 loc) · 4.16 KB
/
New_shitz.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
from matplotlib.pylab import *
import numpy as np
import math as math
import time
start_time = time.time()
def make_points(num_points,r_a, r_b):
dx = 2*pi/num_points
x = np.zeros(num_points*2)
y = np.zeros(num_points*2)
for i in range(num_points*2):
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
#print "matrix", matrix_
return matrix_
#make_angle(100,1,1)
def integral(dirr,N,r_a,r_b,x1,x2,y1,y2):
x,y = make_points(N,r_a,r_b)
# middle of this segment
x_m = (x1+x2)/2.0
y_m = (y1+y2)/2.0
# middle of every segment
x_c = (x[0:N]+x[1:N+1])*0.5
y_c = (y[0:N]+y[1:N+1])*0.5
rad_1 = np.sqrt((x_m-x[0:N])**2+(y_m-y[0:N])**2)
rad_2 = np.sqrt((x_m-x[1:N+1])**2+(y_m-y[1:N+1])**2)
ds = np.sqrt((x[1:N+1]-x[0:N])**2 + (y[1:N+1]-y[0:N])**2)
if dirr == 11:
n = -(y_c/(r_a**2)) / np.sqrt((x_c**2/(r_a**4)) + ((y_c**2)/(r_b**4)))
#n[np.isnan(n)] = 0
if dirr == 22:
n = -(x_c/(r_b**2)) / np.sqrt((x_c**2/(r_a**4)) + ((y_c**2)/(r_b**4)) )
#n[np.isnan(n)] = 0
if dirr == 66:
nx = (x_c/(r_b**2)) / np.sqrt((x_c**2/(r_a**4)) + ((y_c**2)/(r_b**4)) )
ny = (y_c/(r_a**2)) / np.sqrt((x_c**2/(r_a**4)) + ((y_c**2)/(r_b**4)))
#r_x = x_c
#r_y = y_c
n = x_c*ny-r_y*nx
traps1 =np.log(rad_1)
traps2 = np.log(rad_2)
"""
non_1 = np.nonzero(rad_1)
non_2 = np.nonzero(rad_2)
traps1 = np.log(rad_1[non_1])
traps2 = np.log(rad_2[non_2])"""
integ = 0.5*sum((traps1+traps2)*ds*n)
return integ
def integral_matrix(dirr,r_a,r_b,N):
x,y = make_points(N,r_a,r_b,)
integral_matrixes = np.zeros(N)
for i in range(N):
integral_matrixes[i] = integral(dirr,N,r_a,r_b,x[i],x[i+1],y[i],y[i+1])
return integral_matrixes
def solver(dirr,N,r_a,r_b):
return np.linalg.solve(make_angle(N,r_a,r_b),integral_matrix(dirr,r_a,r_b,N))
def added_mass(dirr,r_a,r_b,N):
phi = solver(dirr,N,r_a,r_b)
x,y = make_points(N,r_a,r_b)
ds = np.sqrt((x[1:N+1]-x[:N])**2 + (y[1:N+1]-y[:N])**2)
# middle of every segment
x_c = (x[:N]+x[1:N+1])*0.5
y_c = (y[:N]+y[1:N+1])*0.5
a = phi[:N]
b = phi[0:N+1]
if dirr == 11:
n = ((-y_c)/(r_b**2)) \
/ np.sqrt(((x_c**2)/(r_a**4)) + ((y_c**2)/(r_b**4)) )
n[np.isnan(n)] = 0
exact = np.pi*r_b**2
if dirr == 22:
n = ((-x_c)/(r_a**2)) \
/ np.sqrt(((x_c**2)/(r_a**4)) + ((y_c**2)/(r_b**4)) )
n[np.isnan(n)] = 0
exact = np.pi*r_a**2
if dirr == 66:
nx = -(x_c/(r_b**2)) / np.sqrt((x_c**2/(r_a**4)) + ((y_c**2)/(r_b**4)) )
ny = -(y_c/(r_a**2)) / np.sqrt((x_c**2/(r_a**4)) + ((y_c**2)/(r_b**4)))
r_x = x_c
r_y = y_c
n = ny*r_x - nx*r_y
exact = np.pi*(r_a**2-r_b**2)**2/8.0
integ = 0.5*sum((a+b)*ds*n)
print N," Elements"
print "Direction :", dirr
print "Calculated Added mass: ",integ
print "Exact added mass: ",exact
print "Error: ",100-(100*integ/exact),"%"
def exact(r_a,N):
dtet = 2*pi/N
value = np.zeros(N)
for i in range(N):
value[i] = -r_a*np.cos(dtet*i)
return value
added_mass(11,1,1,360)
print("--- %.2f seconds ---" % (time.time() - start_time))
start_time = time.time()
added_mass(11,1,1,720)
print("--- %.2f seconds ---" % (time.time() - start_time))
#print exact(1,10)
#print exact(1,100) - solver(11,100,1,1)