-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsource_panel_method_wr.py
More file actions
310 lines (263 loc) · 10.8 KB
/
source_panel_method_wr.py
File metadata and controls
310 lines (263 loc) · 10.8 KB
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
#===============================================================================
import numpy as np
import math
import matplotlib
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
#===============================================================================
# Prediction of the wave resistance of a bluff body
#
# Last review at 11-2-2020
# Written by: Anevlavi Dimitra
#===============================================================================
def constantStrengthSource(x1, y1, x2, y2, xp, yp):
# The induced velocity and velocity potential due to SL panel
# At the centre of the element
# curvilinear coordinate system
# from analytic formulas and sign convention
uu = 0.;
vv = -0.5;
# local coordinate system
xi1 = 0;
xi2 = math.sqrt((x2-x1)**2+(y2-y1)**2);
th0 = math.atan2(y2-y1,x2-x1);
xm = 0.5*(x1+x2); ym = 0.5*(y1+y2);
rcon=math.sqrt((xp-xm)**2+(yp-ym)**2)/xi2;
#flag = (rcon>0.0001); % if flag=0 self-induced quantities
flag = (rcon>1e-10);
# Local coordinate system, aligned with panel j with
# (0,0) positioned at the previous (x1,y1) node.
xip= (xp-x1)*math.cos(th0)+(yp-y1)*math.sin(th0); #from global to jt
yip=-(xp-x1)*math.sin(th0)+(yp-y1)*math.cos(th0);
r1=math.sqrt((xp-x1)**2+(yp-y1)**2);
r2=math.sqrt((xp-x2)**2+(yp-y2)**2);
th1=math.atan2(yip,xip-xi1);
th2=math.atan2(yip,xip-xi2);
uu = flag*math.log(r1/r2)/2/math.pi + (flag-1)*uu;
vv = flag*(th2-th1)/2/math.pi + (flag-1)*vv;
# for source
fi= (2*(xip-xi1)*math.log(r1)-2*(xip-xi2)*math.log(r2)- 2*(xi2-xi1) + 2*yip*(th2-th1))/(4*math.pi);
#for sinks
#fi= -(2*(xip-xi1)*math.log(r1)-2*(xip-xi2)*math.log(r2)- 2*xi2 + 2*yip*(th2-th1))/(4*math.pi);
# back to the global coordinate system
u = uu*math.cos(th0)-vv*math.sin(th0); #u
v = uu*math.sin(th0)+vv*math.cos(th0); #v
f = [fi, u, v]
return f
def lineParametrization(xa, ya, xb, yb, Npanels):
# line segment from point A to point B
t = np.linspace(0,1,Npanels)
xline = xa + t*(xb-xa)
yline = ya + t*(yb-ya)
return xline, yline
def collocationScheme(xi, yi):
#normal and tangent unit vectors
sizeN= len(xi)-1
xcolloc = np.zeros(sizeN);ycolloc = np.zeros(sizeN)
sin_theta = np.zeros(sizeN);cos_theta = np.zeros(sizeN)
nx = np.zeros(sizeN);ny = np.zeros(sizeN)
tx = np.zeros(sizeN);ty = np.zeros(sizeN)
for ii in range(0,len(xcolloc)):
xcolloc[ii] = 0.5*(xi[ii+1]+xi[ii])
ycolloc[ii] = 0.5*(yi[ii+1]+yi[ii])
lpanel = np.sqrt((xi[ii+1]-xi[ii])**2+(yi[ii+1]-yi[ii])**2) #panel length
sin_theta[ii] = (yi[ii+1]-yi[ii])/lpanel
cos_theta[ii] = (xi[ii+1]-xi[ii])/lpanel
# The normal vector inside the domain
nx[ii] = sin_theta[ii]
ny[ii] = -cos_theta[ii]
tx[ii] = -ny[ii]
ty[ii] = nx[ii]
return xcolloc, ycolloc, nx, ny, tx, ty
#===============================================================================
# CREATE MESH FOR SQUARE DOMAIN & BOYNDARY CONDITIONS
#===============================================================================
plotMesh = 0 #Switch to 1 to view the mesh plot
plotResults = 0 #Switch to 1 to view the results plotted
debuggTool = 0 #Switch to 1 to enable debugg mode
R = 0.5 #radius of cylinder
h = 2*R #cylinder submergence
Froude = 1.2
gi = 9.81
U = Froude*np.sqrt(gi*h)
kappa = gi/U**2
Np1 = 100 #number of nodes on free-surface
Np2 = 50 #number of nodes on cylinder
print("Mesh consists of ", Np1, " nodes on the free-surface (FS) and ", Np2, " on the body (B)")
# free domain creation
xa, ya, xb, yb = 3, 0, -15, 0
[x1, y1] =lineParametrization(xa, ya, xb, yb, Np1)
# cylinder surface
theta = np.linspace(2*np.pi, 0, Np2)
dll = R*abs(theta[1]-theta[0]) #ds approximation
x2 = R*np.cos(theta)
y2 = R*np.sin(theta) - h
#nodes on the boundary with clockwise numbering
[xcolloc1, ycolloc1, nx1, ny1, tx1, ty1] = collocationScheme(x1, y1)
[xcolloc2, ycolloc2, nx2, ny2, tx2, ty2] = collocationScheme(x2, y2)
#make numpy array
xi = np.array([x1,x2]) #nodal values
yi = np.array([y1,y2]) #nodal values
if debuggTool == 1:
print("First row of xi -> free surface x-nodes \n", xi[0])
print("Second row of xi -> cylinder x-nodes \n", xi[1])
print("\n", yi[0])
print("\n", yi[1])
xcolloc = np.array([xcolloc1,xcolloc2])
ycolloc = np.array([ycolloc1,ycolloc2])
Nel = len(xcolloc[0][:]) + len(xcolloc[1][:])
Nfs = len(xcolloc[0][:]); Nbody = len(xcolloc[1][:])
if debuggTool == 1: print(Nel)
nx = np.array([nx1,nx2])
ny = np.array([ny1,ny2])
tx = np.array([tx1,tx2])
ty = np.array([ty1,ty2])
# plot the discretized domain
if (plotMesh==1):
plt.plot(xi[0], yi[0], xi[1], yi[1])
plt.plot(xi[0], yi[0], 'bo', xi[1], yi[1], 'ro')
plt.grid(linestyle='-', linewidth=0.5)
plt.plot(xcolloc[0], ycolloc[0], 'gs', xcolloc[1], ycolloc[1], 'gs')
plt.quiver(xcolloc[0][0], ycolloc[0][0], nx[0][0], ny[0][0])
plt.quiver(xcolloc[1][0], ycolloc[1][0], nx[1][0], ny[1][0])
plt.quiver(xcolloc[0][0], ycolloc[0][0], tx[0][0], ty[0][0])
plt.quiver(xcolloc[1][0], ycolloc[1][0], tx[1][0], ty[1][0])
plt.grid(linestyle='-', linewidth=0.5)
plt.title('Nodes, control points and orientation vectors')
plt.axis("equal")
plt.show()
#===============================================================================
# LINEAR SYSTEM OF EQUATIONS
#===============================================================================
Bv = np.zeros((Nel, 1)) #RHS: Bvector
Am = np.zeros(Nel) #LHS: Amatrix
kel = -1
for isec in range(2):
#print("BOUNDARY:", isec)
for i in range(len(xcolloc[isec][:])): # it runs through all the collocation points
kel = kel + 1
#print("index = ", i, " xi=", xcolloc[isec][i])
#print("kel", kel)
if isec == 1:
Bv[kel][0] = U*nx[1][i]
if debuggTool == 1: print("RHS Matrix =>", Bv)
#===============================================================================
Potm = np.zeros((Nel, Nel))
xalfa = np.zeros((Nel, Nel))
yalfa = np.zeros((Nel, Nel))
un = np.zeros((Nel, Nel))
kel = -1
for isec in range(2): #for every boundary [0, 1]
for i in range(len(xcolloc[isec][:])): # for every panel on that boundary
kel = kel + 1
xp = xcolloc[isec][i]; yp = ycolloc[isec][i]
jel = -1
for jsec in range(2): #[0, 1]
for j in range(len(xcolloc[jsec][:])):
jel = jel + 1
f = constantStrengthSource(xi[jsec][j], yi[jsec][j], xi[jsec][j+1], yi[jsec][j+1], xp, yp)
fi = f[0]; up = f[1]; vp = f[2]
Potm[kel][jel] = fi
xalfa[kel][jel] = up
yalfa[kel][jel] = vp
if kel == jel: #for self induced velocities expression on the boundary
# save values of matrices
xalfa[kel][jel] = -0.5*nx[isec][i] #sinks
yalfa[kel][jel]= -0.5*ny[isec][i]
#print("matrix: ", kel, jel)
un[kel][jel] = up*nx[isec][i] + vp*ny[isec][i]
if debuggTool == 1: print("LHS Matrix =>", un)
Am = un
#===============================================================================
# DAWSON FINITE DIFFERENT SCHEME
#===============================================================================
delx = xcolloc[0][0]-xcolloc[0][1]
for i in range(len(xcolloc[0][:])): # for every panel on the free surface boundary
ir = len(xcolloc[0][:])-1 -i;
irDawson = ir + 1
if irDawson>3:
xp1 = xcolloc[0][ir]
xp2 = xcolloc[0][ir-1]
xp3 = xcolloc[0][ir-2]
xp4 = xcolloc[0][ir-3]
if irDawson==3:
xp1 = xcolloc[0][ir]
xp2 = xcolloc[0][ir-1]
xp3 = xcolloc[0][ir-2]
xp4 = xp3 - delx
if irDawson==2:
xp1 = xcolloc[0][ir]
xp2 = xcolloc[0][ir-1]
xp3 = xp2 - delx
xp4 = xp3 - delx
if irDawson==1:
xp1 = xcolloc[0][ir]
xp2 = xp1 - delx
xp3 = xp2 - delx
xp4 = xp3 - delx
for mm in range(Nel):
pol1=0
if irDawson>3:
#print("\n", xp1, xp2, xp3, xp4, irDawson)
Di = (xp2-xp1)*(xp3-xp1)*(xp4-xp1)*(xp4-xp2)*(xp3-xp2)*(xp4-xp3)*(xp4+xp3+xp2-3*xp1)
CDi = ((xp2-xp1)**2)*((xp3-xp1)**2)*(xp3-xp2)*(xp3+xp2-2*xp1)/Di
CCi = -((xp2-xp1)**2)*((xp4-xp1)**2)*(xp4-xp2)*(xp4+xp2-2*xp1)/Di
CBi = ((xp3-xp1)**2)*((xp4-xp1)**2)*(xp4-xp3)*(xp4+xp3-2*xp1)/Di
CAi = -(CBi+CCi+CDi);
p1 = CAi; p2 = CBi;
p3 = CCi; p4 = CDi;
pol1 = p1*xalfa[ir][mm] + p2*xalfa[ir-1][mm] + p3*xalfa[ir-2][mm]+ p4*xalfa[ir-3][mm]
Am[ir][mm] = Am[ir][mm] + pol1/kappa;
#===============================================================================
# SOLUTION OF THE LINEAR SYSTEM
#===============================================================================
S = np.linalg.solve(Am,Bv)
if debuggTool==1: print("The piecewise-constant source density vector is: ", S)
#===============================================================================
# POST-PROCESSING
#===============================================================================
Potx = -U + np.matmul(xalfa,S) #the defivative of potential in x
Poty = np.matmul(yalfa,S) #the defivative of potential in z direction
Cp = 1-(Potx**2+Poty**2)/U**2
eta = (U/gi)*(Potx[1:Nfs]+U) # free surface elevation
if plotResults == 1:
plt.plot( xcolloc[0][0:Nfs-1], eta, 'b--', label='heta')
plt.plot( xcolloc[0][:], ycolloc[0][:], 'k')
plt.plot( xi[1][:], yi[1][:])
plt.axis("equal")
plt.title("FIGURE.2 Free surface elevation")
plt.xlabel("x [m]")
plt.ylabel("z [m]")
plt.legend(bbox_to_anchor=(1.05, 1), loc='upper right', borderaxespad=0.)
plt.grid(linestyle='-', linewidth=0.5)
plt.show()
# analytic solution for the pressure coefficient around the cylinder
#print(theta)
start = len(theta)
thi = 0.5*(theta[0:start-1] + theta[1:start])
uanal=U*(-1-np.cos(-math.pi+2*thi))
vanal=U*np.sin(-math.pi+2*thi)
Cpa=1-(uanal**2+vanal**2)/U**2
if debuggTool == 1:
plt.plot( xi[1][0:2], yi[1][0:2], 'bo')
plt.plot( xi[1][:], yi[1][:], 'k')
plt.show()
if plotResults == 1:
plt.plot(thi, Cp[Nfs:len(Cp)], 'bo', label="numerical")
plt.plot(thi, Cp[Nfs:len(Cp)], 'b')
plt.plot(thi, Cpa, label='analytic')
plt.title("FIGURE.1 Pressure coefficient around the cylinder")
plt.xlabel("theta (deg)")
plt.ylabel("Cp")
plt.legend(bbox_to_anchor=(1.05, 1), loc='upper right', borderaxespad=0.)
plt.grid(linestyle='-', linewidth=0.5)
plt.show()
# Calculate resistance
# Cp: one column array
# nx[1][:]: one row array
temp = np.matmul(nx[1][:], Cp[Nfs:len(Cp)] )
waveResistance = dll*np.sum(temp)
print("The (non-dimensional) wave resistance of the body is:", waveResistance)
print("Simulation OK...")
#===============================================================================