-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththesis_oriol_cayon.jl
More file actions
579 lines (494 loc) · 18.9 KB
/
Copy paththesis_oriol_cayon.jl
File metadata and controls
579 lines (494 loc) · 18.9 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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
"""
Post-process results to get global forces and aerodynamic coefficients
Parameters
----------
Fmag : Lift, Drag and Moment magnitudes
aero_coeffs : alpha, cl, cd, cm
ringvec : List of dictionaries containing the vectors that define each ring
Uinf : Wind speed velocity vector
controlpoints : List of dictionaries with the variables needed to define each wing section
Atot : Planform area
Returns
-------
F_rel : Lift and drag forces relative to the local angle of attack
F_gl : Lift and drag forces relative to the wind direction
Ltot : Total lift
Dtot : Total drag
CL : Global CL
CD : Global CD
"""
function output_results(Fmag, aero_coeffs, ringvec, Uinf, controlpoints, Atot)
rho = 1.225
alpha = aero_coeffs[:, 1]
F_rel = Vector{Vector{Float64}}[]
F_gl = Vector{Vector{Float64}}[]
Fmag_gl = Vector{Float64}[]
SideF = Float64[]
Ltot = 0.0
Dtot = 0.0
SFtot = 0.0
for i in eachindex(alpha)
r0 = ringvec[i]["r0"]
# Relative wind speed direction
dir_urel = cos(alpha[i]) * controlpoints[i]["tangential"] +
sin(alpha[i]) * controlpoints[i]["normal"]
dir_urel = dir_urel / norm(dir_urel)
# Lift direction relative to Urel
dir_L = cross(dir_urel, r0)
dir_L = dir_L / norm(dir_L)
# Drag direction relative to Urel
dir_D = cross([0.0, 1.0, 0.0], dir_L)
dir_D = dir_D / norm(dir_D)
# Lift and drag relative to Urel
L_rel = dir_L * Fmag[i, 1]
D_rel = dir_D * Fmag[i, 2]
push!(F_rel, [L_rel, D_rel])
# Lift direction relative to the wind speed
dir_L_gl = cross(Uinf, [0.0, 1.0, 0.0])
dir_L_gl = dir_L_gl / norm(dir_L_gl)
# Lift and drag relative to the windspeed
L_gl = vector_projection(L_rel, dir_L_gl) + vector_projection(D_rel, dir_L_gl)
D_gl = vector_projection(L_rel, Uinf) + vector_projection(D_rel, Uinf)
push!(F_gl, [L_gl, D_gl])
push!(Fmag_gl, [
dot(L_rel, dir_L_gl) + dot(D_rel, dir_L_gl),
dot(L_rel, Uinf / norm(Uinf)) + dot(D_rel, Uinf / norm(Uinf))
])
push!(SideF, dot(L_rel, [0.0, 1.0, 0.0]) + dot(D_rel, [0.0, 1.0, 0.0]))
end
# Calculate total aerodynamic forces
for i in eachindex(Fmag_gl)
Ltot += Fmag_gl[i][1] * norm(ringvec[i]["r0"])
Dtot += Fmag_gl[i][2] * norm(ringvec[i]["r0"])
SFtot += SideF[i] * norm(ringvec[i]["r0"])
end
va = norm(Uinf)
CL = Ltot / (0.5 * va^2 * Atot * rho)
CD = Dtot / (0.5 * va^2 * Atot * rho)
CS = SFtot / (0.5 * va^2 * Atot * rho)
return F_rel, F_gl, Ltot, Dtot, CL, CD, CS
end
"""
Find the projection of a vector into a direction
Parameters
----------
v : vector to be projected
u : direction
Returns
-------
proj : projection of the vector v onto u
"""
function vector_projection(v, u)
unit_u = u / norm(u)
proj = dot(v, unit_u) * unit_u
return proj
end
"""
Create geometry structures necessary for solving the system of circualtion
Parameters
----------
coordinates : coordinates the nodes (each section is defined by two nodes,
the first is the LE, so each section
defined by a pair of coordinates)
Uinf : Wind speed vector
N : Number of sections
ring_geo : - '3fil': Each horsehoe is defined by 3 filaments
- '5fil': Each horseshoe is defined by 5 filaments
model : VSM: Vortex Step method/ LLT: Lifting Line Theory
Returns
-------
controlpoints : List of dictionaries with the variables needed to define each wing section
rings : List of list with the definition of each vortex filament
wingpanels : List with the points defining each wing pannel
ringvec : List of dictionaries containing the vectors that define each ring
coord_L : coordinates of the aerodynamic centers of each wing panel
"""
function create_geometry_general(coordinates, Uinf, N, ring_geo, model)
filaments = Dict{String, Any}[]
controlpoints = Dict{String, Any}[]
rings = Vector{Dict{String, Any}}[]
wingpanels = Dict{String, Vector{Float64}}[]
ringvec = Dict{String, Vector{Float64}}[]
coord_L = Vector{Float64}[]
# Go through all wing panels
for i in 1:N-1
# Identify points defining the panel
section = Dict(
"p1" => coordinates[2i-1, :],
"p2" => coordinates[2i+1, :],
"p3" => coordinates[2i+2, :],
"p4" => coordinates[2i, :]
)
push!(wingpanels, section)
di = norm(
coordinates[2i-1, :] * 0.75 +
coordinates[2i, :] * 0.25 -
(coordinates[2i+1, :] * 0.75 + coordinates[2i+2, :] * 0.25)
)
if i == 1
diplus = norm(
coordinates[2*(i+1)-1, :] * 0.75 +
coordinates[2*(i+1), :] * 0.25 -
(coordinates[2*(i+1)+1, :] * 0.75 + coordinates[2*(i+1)+2, :] * 0.25)
)
ncp = di / (di + diplus)
elseif i == N-1
dimin = norm(
coordinates[2*(i-1)-1, :] * 0.75 +
coordinates[2*(i-1), :] * 0.25 -
(coordinates[2*(i-1)+1, :] * 0.75 + coordinates[2*(i-1)+2, :] * 0.25)
)
ncp = dimin / (dimin + di)
else
dimin = norm(
coordinates[2*(i-1)-1, :] * 0.75 +
coordinates[2*(i-1), :] * 0.25 -
(coordinates[2*(i-1)+1, :] * 0.75 + coordinates[2*(i-1)+2, :] * 0.25)
)
diplus = norm(
coordinates[2*(i+1)-1, :] * 0.75 +
coordinates[2*(i+1), :] * 0.25 -
(coordinates[2*(i+1)+1, :] * 0.75 + coordinates[2*(i+1)+2, :] * 0.25)
)
ncp = 0.25 * (dimin/(dimin + di) + di/(di + diplus) + 1)
end
ncp = 1 - ncp
chord = norm((section["p2"] + section["p1"])/2 - (section["p3"] + section["p4"])/2)
LLpoint = (section["p2"] * (1-ncp) + section["p1"] * ncp) * 3/4 +
(section["p3"] * (1-ncp) + section["p4"] * ncp) * 1/4
VSMpoint = (section["p2"] * (1-ncp) + section["p1"] * ncp) * 1/4 +
(section["p3"] * (1-ncp) + section["p4"] * ncp) * 3/4
push!(coord_L, LLpoint)
# Define bound vortex filament
bound = Dict(
"id" => "bound",
"x1" => section["p1"] * 3/4 + section["p4"] * 1/4,
"x2" => section["p2"] * 3/4 + section["p3"] * 1/4,
"Gamma" => 0
)
push!(filaments, bound)
z_airf = bound["x2"] - bound["x1"]
x_airf = cross(VSMpoint - LLpoint, z_airf)
x_airf = x_airf / norm(x_airf)
y_airf = VSMpoint - LLpoint
y_airf = y_airf / norm(y_airf)
z_airf = z_airf / norm(z_airf)
airf_coord = hcat(x_airf, y_airf, z_airf)
normal = x_airf
tangential = y_airf
if model == VSM
cp = Dict(
"coordinates" => VSMpoint,
"chord" => chord,
"normal" => normal,
"tangential" => tangential,
"airf_coord" => airf_coord,
"coordinates_aoa" => LLpoint
)
push!(controlpoints, cp)
elseif model == LLT
cp = Dict(
"coordinates" => LLpoint,
"chord" => chord,
"normal" => normal,
"tangential" => tangential,
"airf_coord" => airf_coord
)
push!(controlpoints, cp)
end
temp = Dict(
"r0" => bound["x2"] - bound["x1"],
"r1" => cp["coordinates"] - bound["x1"],
"r2" => cp["coordinates"] - bound["x2"],
"r3" => cp["coordinates"] - (bound["x2"] + bound["x1"])/2
)
push!(ringvec, temp)
temp = Uinf / norm(Uinf)
if ring_geo == "3fil"
# create trailing filaments, at x1 of bound filament
temp1 = Dict("dir" => temp, "id" => "trailing_inf1", "x1" => bound["x1"], "Gamma" => 0)
push!(filaments, temp1)
# create trailing filaments, at x2 of bound filament
temp1 = Dict("x1" => bound["x2"], "dir" => temp, "id" => "trailing_inf2", "Gamma" => 0)
push!(filaments, temp1)
elseif ring_geo == "5fil"
temp1 = Dict(
"x1" => section["p4"],
"x2" => bound["x1"],
"Gamma" => 0,
"id" => "trailing1"
)
push!(filaments, temp1)
temp1 = Dict(
"dir" => temp,
"id" => "trailing_inf1",
"x1" => section["p4"],
"Gamma" => 0
)
push!(filaments, temp1)
# create trailing filaments, at x2 of bound filament
temp1 = Dict(
"x2" => section["p3"],
"x1" => bound["x2"],
"Gamma" => 0,
"id" => "trailing1"
)
push!(filaments, temp1)
temp1 = Dict(
"x1" => section["p3"],
"dir" => temp,
"id" => "trailing_inf2",
"Gamma" => 0
)
push!(filaments, temp1)
end
push!(rings, filaments)
filaments = Dict{String, Any}[]
end
coord_L = hcat(coord_L...)
return controlpoints, rings, wingpanels, ringvec, coord_L
end
"""
Solve the VSM or LLM by finding the distribution of Gamma
Parameters
----------
ringvec : List of dictionaries containing the vectors that define each ring
controlpoints : List of dictionaries with the variables needed to define each wing section
rings : List of list with the definition of each vortex filament
Uinf : Wind speed velocity vector
data_airf : 2D airfoil data with alpha, Cl, Cd, Cm
recalc_alpha : True if you want to recalculate the induced angle of attack at 1/4 of the chord (VSM)
Gamma0 : Initial Guess of Gamma
model : VSM: Vortex Step method/ LLT: Lifting Line Theory
Returns
-------
MatrixU, MatrixV, MatrixW : Induction matrices
"""
function thesis_induction_matrix_creation(ringvec, controlpoints, rings, Uinf, Gamma0, data_airf, conv_crit, model)
nocore = false # To shut down core corrections input true
# Initialization of parameters
N = length(rings)
MatrixU = zeros(N, N)
MatrixV = zeros(N, N)
MatrixW = zeros(N, N)
U_2D = zeros(N, 3)
coord_cp = [controlpoints[icp]["coordinates"] for icp in 1:N]
chord = [controlpoints[icp]["chord"] for icp in 1:N]
airf_coord = [controlpoints[icp]["airf_coord"] for icp in 1:N]
for icp in 1:N
if model == VSM
# Velocity induced by infinite bound vortex with Gamma = 1
U_2D[icp,:] = velocity_induced_bound_2D(ringvec[icp])
end
for jring in 1:N
rings[jring] = update_Gamma_single_ring(rings[jring], 1, 1)
# Calculate velocity induced by ring at control point
velocity_induced = velocity_induced_single_ring_semiinfinite(
rings[jring], coord_cp[icp], model, norm(Uinf)
)
# If CORE corrections are deactivated
if nocore
velocity_induced = velocity_induced_single_ring_semiinfinite_nocore(
rings[jring], coord_cp[icp], model
)
end
# AIC Matrix
MatrixU[icp,jring] = velocity_induced[1]
MatrixV[icp,jring] = velocity_induced[2]
MatrixW[icp,jring] = velocity_induced[3]
# Different from old thesis code as this was considered wrong
if icp == jring
if model == VSM
MatrixU[icp,jring] -= U_2D[icp,1]
MatrixV[icp,jring] -= U_2D[icp,2]
MatrixW[icp,jring] -= U_2D[icp,3]
end
end
end
end
return MatrixU, MatrixV, MatrixW
end
"""
Calculate the velocity induced by a trailing vortex filament in a point in space
Vortex core correction from:
Rick Damiani et al. "A vortex step method for nonlinear airfoil polar data as implemented in
KiteAeroDyn".
Parameters
----------
XV1 : Point A of the vortex filament (vector)
XV2 : Point B of the vortex filament (vector)
XVP : Controlpoint (vector)
gamma : Strength of the vortex (scalar)
Uinf : Inflow velocity modulus (scalar)
Returns
-------
vel_ind : induced velocity by the trailing fil. (vector)
"""
function velocity_3D_trailing_vortex(XV1, XV2, XVP, gamma, Uinf)
r0 = XV2 - XV1 # Vortex filament
r1 = XVP - XV1 # Controlpoint to one end of the vortex filament
r2 = XVP - XV2 # Controlpoint to one end of the vortex filament
alpha0 = 1.25643 # Oseen parameter
nu = 1.48e-5 # Kinematic viscosity of air
r_perp = dot(r1, r0) * r0 / (norm(r0)^2) # Vector from XV1 to XVP perpendicular to the core radius
epsilon = sqrt(4 * alpha0 * nu * norm(r_perp) / Uinf) # Cut-off radius
# Cross products used for later computations
r1Xr0 = cross(r1, r0)
r2Xr0 = cross(r2, r0)
if norm(r1Xr0) / norm(r0) > epsilon # Perpendicular distance from XVP to vortex filament (r0)
r1Xr2 = cross(r1, r2)
vel_ind = gamma / (4π) * r1Xr2 / (norm(r1Xr2)^2) *
dot(r0, r1/norm(r1) - r2/norm(r2))
else
# The control point is placed on the edge of the radius core
# proj stands for the vectors respect to the new controlpoint
r1_proj = dot(r1, r0) * r0 / (norm(r0)^2) + epsilon * r1Xr0 / norm(r1Xr0)
r2_proj = dot(r2, r0) * r0 / (norm(r0)^2) + epsilon * r2Xr0 / norm(r2Xr0)
r1Xr2_proj = cross(r1_proj, r2_proj)
vel_ind_proj = gamma / (4π) * r1Xr2_proj / (norm(r1Xr2_proj)^2) *
dot(r0, r1_proj/norm(r1_proj) - r2_proj/norm(r2_proj))
vel_ind = norm(r1Xr0) / (norm(r0) * epsilon) * vel_ind_proj
end
return vel_ind
end
"""
Calculate the velocity induced by a semiinfinite trailing vortex filament in a point in space
Vortex core correction from:
Rick Damiani et al. "A vortex step method for nonlinear airfoil polar data as implemented in
KiteAeroDyn".
Parameters
----------
XV1 : Point A of the vortex filament (vector)
Vf : Direction vector of the filament
XVP : Controlpoint (vector)
GAMMA : Strength of the vortex (scalar)
Uinf : Inflow velocity modulus (scalar)
Returns
-------
vel_ind : induced velocity by the trailing fil. (vector)
"""
function velocity_3D_trailing_vortex_semiinfinite(XV1, Vf, XVP, GAMMA, Uinf)
r1 = XVP - XV1 # Vector from XV1 to XVP
r1XVf = cross(r1, Vf)
alpha0 = 1.25643 # Oseen parameter
nu = 1.48e-5 # Kinematic viscosity of air
r_perp = dot(r1, Vf) * Vf # Vector from XV1 to XVP perpendicular to the core radius
epsilon = sqrt(4 * alpha0 * nu * norm(r_perp) / Uinf) # Cut-off radius
if norm(r1XVf) / norm(Vf) > epsilon
# determine scalar
K = GAMMA / (4π * norm(r1XVf)^2) * (1 + dot(r1, Vf) / norm(r1))
# determine the three velocity components
vel_ind = K * r1XVf
else
r1_proj = dot(r1, Vf) * Vf + epsilon * (r1/norm(r1) - Vf) / norm(r1/norm(r1) - Vf)
r1XVf_proj = cross(r1_proj, Vf)
K = GAMMA / (4π * norm(r1XVf_proj)^2) * (1 + dot(r1_proj, Vf) / norm(r1_proj))
# determine the three velocity components
vel_ind = K * r1XVf_proj
end
return vel_ind
end
"""
Calculate induced velocity for 2D bound vortex
"""
function velocity_induced_bound_2D(ringvec)
r0 = ringvec["r0"]
r3 = ringvec["r3"]
cross_prod = cross(r0, r3)
ind_vel = cross_prod / (dot(cross_prod, cross_prod)) / (2π) * norm(r0)
return ind_vel
end
"""
Calculates the velocity induced by a ring at a certain controlpoint
Parameters
----------
ring : List of dictionaries defining the filaments of a vortex ring
controlpoint : Dictionary defining a controlpoint
model : VSM: Vortex Step method/ LLT: Lifting Line Theory
Uinf : Wind speed vector
Returns
-------
velind : Induced velocity
"""
function velocity_induced_single_ring_semiinfinite(ring, controlpoint, model, Uinf)
velind = [0.0, 0.0, 0.0]
for filament in ring
GAMMA = filament["Gamma"]
XV1 = filament["x1"]
XVP = controlpoint
if filament["id"] == "trailing_inf1"
Vf = filament["dir"]
tempvel = velocity_3D_trailing_vortex_semiinfinite(
XV1, Vf, XVP, GAMMA, Uinf
)
elseif filament["id"] == "trailing_inf2"
Vf = filament["dir"]
tempvel = velocity_3D_trailing_vortex_semiinfinite(
XV1, Vf, XVP, -GAMMA, Uinf
)
elseif filament["id"] == "bound"
if model == VSM
XV2 = filament["x2"]
tempvel = velocity_3D_bound_vortex(XV1, XV2, XVP, GAMMA)
else
tempvel = [0.0, 0.0, 0.0]
end
else
XV2 = filament["x2"]
tempvel = velocity_3D_trailing_vortex(XV1, XV2, XVP, GAMMA, Uinf)
end
velind[1] += tempvel[1]
velind[2] += tempvel[2]
velind[3] += tempvel[3]
end
return velind
end
"""
Calculate the velocity induced by a bound vortex filament in a point in space
Vortex core correction from:
Rick Damiani et al. "A vortex step method for nonlinear airfoil polar data as implemented in
KiteAeroDyn".
Parameters
----------
XV1 : Point A of Bound vortex (vector)
XV2 : Point B of Bound vortex (vector)
XVP : Control point (vector)
gamma : Strength of the vortex (scalar)
Returns
-------
vel_ind : Induced velocity (vector)
"""
function velocity_3D_bound_vortex(XV1, XV2, XVP, gamma)
r0 = XV2 - XV1 # Vortex filament
r1 = XVP - XV1 # Controlpoint to one end of the vortex filament
r2 = XVP - XV2 # Controlpoint to one end of the vortex filament
# Cross products used for later computations
r1Xr0 = cross(r1, r0)
r2Xr0 = cross(r2, r0)
epsilon = 0.05 * norm(r0) # Cut-off radius
if norm(r1Xr0) / norm(r0) > epsilon # Perpendicular distance from XVP to vortex filament (r0)
r1Xr2 = cross(r1, r2)
vel_ind = gamma / (4π) * r1Xr2 / (norm(r1Xr2)^2) *
dot(r0, r1/norm(r1) - r2/norm(r2))
else
# The control point is placed on the edge of the radius core
# proj stands for the vectors respect to the new controlpoint
r1_proj = dot(r1, r0) * r0 / (norm(r0)^2) + epsilon * r1Xr0 / norm(r1Xr0)
r2_proj = dot(r2, r0) * r0 / (norm(r0)^2) + epsilon * r2Xr0 / norm(r2Xr0)
r1Xr2_proj = cross(r1_proj, r2_proj)
vel_ind_proj = gamma / (4π) * r1Xr2_proj / (norm(r1Xr2_proj)^2) *
dot(r0, r1_proj/norm(r1_proj) - r2_proj/norm(r2_proj))
vel_ind = norm(r1Xr0) / (norm(r0) * epsilon) * vel_ind_proj
end
return vel_ind
end
"""
Update Gamma of all the filaments in a horshoe ring
"""
function update_Gamma_single_ring(ring, GammaNew, WeightNew)
# Runs through all filaments
for filament in ring
filament["Gamma"] = filament["Gamma"] * (1 - WeightNew) + WeightNew * GammaNew
end
return ring
end