Skip to content

Commit 7317bc4

Browse files
committed
non-allocating gamma loop
1 parent 786b2dc commit 7317bc4

File tree

4 files changed

+63
-36
lines changed

4 files changed

+63
-36
lines changed

examples/code_bench.jl

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,13 @@ U_2D = @MVector zeros(3) # StaticArraysCore.MVector{3, Float64}
8686
$work_vectors
8787
)
8888

89-
# model = "VSM"
90-
# va_norm_array = zeros(wa.n_panels)
91-
# va_unit_array = zeros(wa.n_panels, 3)
92-
# @time VortexStepMethod.calculate_AIC_matrices!(wa, model,
93-
# core_radius_fraction,
94-
# va_norm_array,
95-
# va_unit_array)
89+
model = "VSM"
90+
va_norm_array = zeros(wa.n_panels)
91+
va_unit_array = zeros(wa.n_panels, 3)
92+
@btime VortexStepMethod.calculate_AIC_matrices!(
93+
$wa, model,
94+
$core_radius_fraction,
95+
$va_norm_array,
96+
$va_unit_array)
9697

9798
nothing

src/panel.jl

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Represents a panel in a vortex step method simulation.
2424
- `width::Float64`: Panel width
2525
- `filaments::Vector{BoundFilament}`: Panel filaments
2626
"""
27-
mutable struct Panel{P}
27+
mutable struct Panel
2828
TE_point_1::MVec3
2929
LE_point_1::MVec3
3030
TE_point_2::MVec3
@@ -36,7 +36,7 @@ mutable struct Panel{P}
3636
cl_coefficients::Union{Nothing,Vector{Float64}}
3737
cd_coefficients::Union{Nothing,Vector{Float64}}
3838
cm_coefficients::Union{Nothing,Vector{Float64}}
39-
polar_data::P
39+
polar_data::Union{Nothing, Tuple{Extrapolation}}
4040
aerodynamic_center::MVec3
4141
control_point::MVec3
4242
bound_point_1::MVec3
@@ -92,6 +92,22 @@ mutable struct Panel{P}
9292
throw(ArgumentError("Polar data must have same shape"))
9393
end
9494
polar_data = (aero_1 + aero_2) / 2
95+
cl_interp = linear_interpolation(
96+
polar_data[:,1],
97+
polar_data[:,2];
98+
extrapolation_bc=NaN
99+
)
100+
cd_interp = linear_interpolation(
101+
polar_data[:,1],
102+
polar_data[:,3];
103+
extrapolation_bc=NaN
104+
)
105+
cm_interp = linear_interpolation(
106+
polar_data[:,1],
107+
polar_data[:,4];
108+
extrapolation_bc=NaN
109+
)
110+
polar_data = (cl_interp, cd_interp, cm_interp)
95111
elseif aero_model == "interpolations"
96112
cl_left, cd_left, cm_left = section_1.aero_input[2]
97113
cl_right, cd_right, cm_right = section_2.aero_input[2]
@@ -113,7 +129,7 @@ mutable struct Panel{P}
113129
BoundFilament(TE_point_2, bound_point_2)
114130
]
115131

116-
new{typeof(polar_data)}(
132+
new(
117133
TE_point_1, LE_point_1, TE_point_2, LE_point_2,
118134
chord, nothing, corner_points, aero_model,
119135
cl_coeffs, cd_coeffs, cm_coeffs, polar_data,
@@ -178,7 +194,7 @@ function compute_lei_coefficients(section_1::Section, section_2::Section)
178194
)
179195

180196
# Compute S values
181-
S = Dict{Int,Float64}()
197+
S = Dict{Int64,Float64}()
182198
S[9] = C[20]*t^2 + C[21]*t + C[22]
183199
S[10] = C[23]*t^2 + C[24]*t + C[25]
184200
S[11] = C[26]*t^2 + C[27]*t + C[28]
@@ -253,18 +269,18 @@ function calculate_cl(panel::Panel, alpha::Float64)
253269
elseif panel.aero_model == "inviscid"
254270
return 2π * alpha
255271
elseif panel.aero_model == "polar_data"
256-
return linear_interpolation(
257-
panel.polar_data[:,1],
258-
panel.polar_data[:,2];
259-
extrapolation_bc=Line()
260-
)(alpha)
272+
return panel.polar_data[1](alpha)
261273
elseif panel.aero_model == "interpolations"
262274
return panel.polar_data[1](alpha, 0.0)
263275
else
264276
throw(ArgumentError("Unsupported aero model: $(panel.aero_model)"))
265277
end
266278
end
267279

280+
function calculate_cl(polar_data::Nothing, model_type::String, alpha::Float64)
281+
return 2π * alpha
282+
end
283+
268284
"""
269285
calculate_cd_cm(panel::Panel, alpha::Float64)
270286
@@ -281,19 +297,13 @@ function calculate_cd_cm(panel::Panel, alpha::Float64)
281297
elseif panel.aero_model == "inviscid"
282298
return 0.0, 0.0
283299
elseif panel.aero_model == "polar_data"
284-
cd = linear_interpolation(
285-
panel.polar_data[:,1],
286-
panel.polar_data[:,3];
287-
extrapolation_bc=Line()
288-
)(alpha)
289-
cm = linear_interpolation(
290-
panel.polar_data[:,1],
291-
panel.polar_data[:,4];
292-
extrapolation_bc=Line()
293-
)(alpha)
300+
cd = panel.polar_data[2](alpha)
301+
cm = panel.polar_data[3](alpha)
294302
return cd, cm
295303
elseif panel.aero_model == "interpolations"
296-
return panel.polar_data[2](alpha, 0.0), panel.polar_data[3](alpha, 0.0)
304+
cd = panel.polar_data[2](alpha, 0.0)
305+
cm = panel.polar_data[3](alpha, 0.0)
306+
return cd, cm
297307
else
298308
throw(ArgumentError("Unsupported aero model: $(panel.aero_model)"))
299309
end

src/solver.jl

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -204,17 +204,21 @@ function gamma_loop(
204204
v_normal_array = zeros(n_panels)
205205
v_tangential_array = zeros(n_panels)
206206

207-
AIC_x, AIC_y, AIC_z = @views wing_aero.AIC[1, :, :], wing_aero.AIC[2, :, :], wing_aero.AIC[3, :, :]
208-
207+
AIC_x, AIC_y, AIC_z = wing_aero.AIC[1, :, :], wing_aero.AIC[2, :, :], wing_aero.AIC[3, :, :]
208+
209+
velocity_view_x = @view induced_velocity_all[:, 1]
210+
velocity_view_y = @view induced_velocity_all[:, 2]
211+
velocity_view_z = @view induced_velocity_all[:, 3]
212+
209213
iters = 0
210214
for i in 1:solver.max_iterations
211215
iters += 1
212216
gamma .= gamma_new
213217

214218
# Calculate induced velocities
215-
mul!(view(induced_velocity_all, :, 1), AIC_x, gamma)
216-
mul!(view(induced_velocity_all, :, 2), AIC_y, gamma)
217-
mul!(view(induced_velocity_all, :, 3), AIC_z, gamma)
219+
mul!(velocity_view_x, AIC_x, gamma)
220+
mul!(velocity_view_y, AIC_y, gamma)
221+
mul!(velocity_view_z, AIC_z, gamma)
218222

219223
relative_velocity_array .= va_array .+ induced_velocity_all
220224
for i in 1:n_panels
@@ -240,7 +244,7 @@ function gamma_loop(
240244
end
241245

242246
for (i, (panel, alpha)) in enumerate(zip(panels, alpha_array))
243-
cl_array[i] = calculate_cl(panel, alpha)
247+
cl_array[i] = calculate_cl(panel.polar_data, panel.aero_model, alpha)
244248
end
245249
gamma_new .= 0.5 .* v_a_array.^2 ./ Umagw_array .* cl_array .* chord_array
246250

src/wing_geometry.jl

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -239,9 +239,21 @@ function calculate_new_aero_input(aero_input,
239239
cl_left, cd_left, cm_left = aero_input[section_index][2]
240240
cl_right, cd_right, cm_right = aero_input[section_index + 1][2]
241241

242-
CL_interp = (α, β) -> left_weight * cl_left[α, β] + right_weight * cl_right[α, β]
243-
CD_interp = (α, β) -> left_weight * cd_left[α, β] + right_weight * cd_right[α, β]
244-
CM_interp = (α, β) -> left_weight * cm_left[α, β] + right_weight * cm_right[α, β]
242+
@info "Interpolation object comparison" begin
243+
objects_equal = (
244+
cl_left === cl_right,
245+
cd_left === cd_right,
246+
cm_left === cm_right
247+
)
248+
(; objects_equal)
249+
end
250+
251+
CL_interp = cl_left === cl_right ? cl_left :
252+
(α, β) -> left_weight * cl_left[α, β] + right_weight * cl_right[α, β]
253+
CD_interp = cd_left === cd_right ? cd_left :
254+
(α, β) -> left_weight * cd_left[α, β] + right_weight * cd_right[α, β]
255+
CM_interp = cm_left === cm_right ? cm_left :
256+
(α, β) -> left_weight * cm_left[α, β] + right_weight * cm_right[α, β]
245257

246258
return ("interpolations", (CL_interp, CD_interp, CM_interp))
247259

0 commit comments

Comments
 (0)