Skip to content

Commit c19a3ff

Browse files
committed
Runs with less allocs
1 parent 06e605c commit c19a3ff

File tree

2 files changed

+121
-120
lines changed

2 files changed

+121
-120
lines changed

src/body_aerodynamics.jl

Lines changed: 11 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,10 @@ function init!(body_aero::BodyAerodynamics;
109109

110110
idx = 1
111111
for wing in body_aero.wings
112-
@time panel_props = init!(wing; aero_center_location, control_point_location)
112+
println("init wing")
113+
@time init!(wing; aero_center_location, control_point_location)
114+
# @assert false
115+
panel_props = wing.panel_props
113116

114117
# Create panels
115118
@time for i in 1:wing.n_panels
@@ -122,19 +125,18 @@ function init!(body_aero::BodyAerodynamics;
122125
body_aero.panels[idx],
123126
wing.refined_sections[i],
124127
wing.refined_sections[i+1],
125-
panel_props.aero_centers[i],
126-
panel_props.control_points[i],
127-
panel_props.bound_points_1[i],
128-
panel_props.bound_points_2[i],
129-
panel_props.x_airf[i],
130-
panel_props.y_airf[i],
131-
panel_props.z_airf[i],
128+
panel_props.aero_centers[i, :],
129+
panel_props.control_points[i, :],
130+
panel_props.bound_points_1[i, :],
131+
panel_props.bound_points_2[i, :],
132+
panel_props.x_airf[i, :],
133+
panel_props.y_airf[i, :],
134+
panel_props.z_airf[i, :],
132135
delta;
133136
remove_nan=wing.remove_nan
134137
)
135138
idx += 1
136139
end
137-
@assert false
138140
end
139141

140142
# Initialize rest of the struct
@@ -161,116 +163,6 @@ function Base.setproperty!(obj::BodyAerodynamics, sym::Symbol, val)
161163
end
162164
end
163165

164-
"""
165-
PanelProperties
166-
167-
Structure to hold calculated panel properties.
168-
169-
# Fields
170-
- `aero_centers`::Matrix{Float64}
171-
- `control_points`::Matrix{Float64}
172-
- `bound_points_1`::Matrix{Float64}
173-
- `bound_points_2`::Matrix{Float64}
174-
- `x_airf`::Matrix{Float64}: Vector of unit vectors tangential to chord line
175-
- `y_airf`::Matrix{Float64}: Vector of unit vectors in spanwise direction
176-
- `z_airf`::Matrix{Float64}: Vector of unit vectors pointing up (cross of x_airf and y_airf)
177-
"""
178-
@with_kw mutable struct PanelProperties{P}
179-
aero_centers::Matrix{Float64} = zeros(P, 3)
180-
control_points::Matrix{Float64} = zeros(P, 3)
181-
bound_points_1::Matrix{Float64} = zeros(P, 3)
182-
bound_points_2::Matrix{Float64} = zeros(P, 3)
183-
x_airf::Matrix{Float64} = zeros(P, 3)
184-
y_airf::Matrix{Float64} = zeros(P, 3)
185-
z_airf::Matrix{Float64} = zeros(P, 3)
186-
coords::Matrix{Float64} = zeros(2(P+1), 3)
187-
end
188-
189-
"""
190-
update_panel_properties!(section_list::Vector{Section}, n_panels::Int,
191-
aero_center_loc::Float64, control_point_loc::Float64)
192-
193-
Calculate geometric properties for each panel.
194-
195-
# Arguments
196-
- section_list::Vector{Section}: List of [Section](@ref)s
197-
- `n_panels`::Int: Number of [Panel](@ref)s
198-
- `aero_center_loc`::Float64: Location of the aerodynamic center
199-
- `control_point_loc`::Float64: Location of the control point
200-
201-
# Returns:
202-
[PanelProperties](@ref) containing vectors for each property
203-
"""
204-
function update_panel_properties!(panel_props::PanelProperties, section_list::Vector{Section}, n_panels::Int,
205-
aero_center_loc::Float64, control_point_loc::Float64)
206-
coords = panel_props.coords
207-
aero_centers = panel_props.aero_centers
208-
control_points = panel_props.control_points
209-
bound_points_1 = panel_props.bound_points_1
210-
bound_points_2 = panel_props.bound_points_2
211-
x_airf = panel_props.x_airf
212-
y_airf = panel_props.y_airf
213-
z_airf = panel_props.z_airf
214-
@debug "Shape of coordinates: $(size(coords))"
215-
216-
for i in 1:n_panels
217-
coords[2i-1, :] .= section_list[i].LE_point
218-
coords[2i, :] .= section_list[i].TE_point
219-
coords[2i+1, :] .= section_list[i+1].LE_point
220-
coords[2i+2, :] .= section_list[i+1].TE_point
221-
end
222-
223-
@debug "Coordinates: $coords"
224-
225-
for i in 1:n_panels
226-
# Define panel points
227-
section = Dict(
228-
"p1" => coords[2i-1, :], # LE_1
229-
"p2" => coords[2i+1, :], # LE_2
230-
"p3" => coords[2i+2, :], # TE_2
231-
"p4" => coords[2i, :] # TE_1
232-
)
233-
234-
# Calculate control point position
235-
di = norm(coords[2i-1, :] * 0.75 + coords[2i, :] * 0.25 -
236-
(coords[2i+1, :] * 0.75 + coords[2i+2, :] * 0.25))
237-
238-
ncp = if i == 1
239-
diplus = norm(coords[2i+1, :] * 0.75 + coords[2i+2, :] * 0.25 -
240-
(coords[2i+3, :] * 0.75 + coords[2i+4, :] * 0.25))
241-
di / (di + diplus)
242-
elseif i == n_panels
243-
dimin = norm(coords[2i-3, :] * 0.75 + coords[2i-2, :] * 0.25 -
244-
(coords[2i-1, :] * 0.75 + coords[2i, :] * 0.25))
245-
dimin / (dimin + di)
246-
else
247-
dimin = norm(coords[2i-3, :] * 0.75 + coords[2i-2, :] * 0.25 -
248-
(coords[2i-1, :] * 0.75 + coords[2i, :] * 0.25))
249-
diplus = norm(coords[2i+1, :] * 0.75 + coords[2i+2, :] * 0.25 -
250-
(coords[2i+3, :] * 0.75 + coords[2i+4, :] * 0.25))
251-
0.25 * (dimin / (dimin + di) + di / (di + diplus) + 1)
252-
end
253-
254-
ncp = 1 - ncp
255-
256-
# Calculate points
257-
@. aero_centers[i, :] = (section["p2"] * (1 - ncp) + section["p1"] * ncp) * 0.75 +
258-
(section["p3"] * (1 - ncp) + section["p4"] * ncp) * 0.25
259-
260-
@. control_points[i, :] = (section["p2"] * (1 - ncp) + section["p1"] * ncp) * 0.25 +
261-
(section["p3"] * (1 - ncp) + section["p4"] * ncp) * 0.75
262-
263-
@. bound_points_1[i, :] = section["p1"] * 0.75 + section["p4"] * 0.25
264-
bound_points_2[i, :] = section["p2"] * 0.75 + section["p3"] * 0.25
265-
266-
# Calculate reference frame vectors
267-
@. z_airf[i, :] = normalize(cross(control_points[i, :] - aero_centers[i, :], section["p1"] - section["p2"]))
268-
@. x_airf[i, :] = normalize(control_points[i, :] - aero_centers[i, :])
269-
@. y_airf[i, :] = normalize(bound_points_1[i, :] - bound_points_2[i, :])
270-
end
271-
return nothing
272-
end
273-
274166
"""
275167
calculate_AIC_matrices!(body_aero::BodyAerodynamics, model::Model,
276168
core_radius_fraction::Float64,

src/wing_geometry.jl

Lines changed: 110 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,115 @@ function init!(refined_section::Section, section::Section)
5858
end
5959
end
6060

61+
"""
62+
PanelProperties
63+
64+
Structure to hold calculated panel properties.
65+
66+
# Fields
67+
- `aero_centers`::Matrix{Float64}
68+
- `control_points`::Matrix{Float64}
69+
- `bound_points_1`::Matrix{Float64}
70+
- `bound_points_2`::Matrix{Float64}
71+
- `x_airf`::Matrix{Float64}: Vector of unit vectors tangential to chord line
72+
- `y_airf`::Matrix{Float64}: Vector of unit vectors in spanwise direction
73+
- `z_airf`::Matrix{Float64}: Vector of unit vectors pointing up (cross of x_airf and y_airf)
74+
"""
75+
@with_kw mutable struct PanelProperties{P}
76+
aero_centers::Matrix{Float64} = zeros(P, 3)
77+
control_points::Matrix{Float64} = zeros(P, 3)
78+
bound_points_1::Matrix{Float64} = zeros(P, 3)
79+
bound_points_2::Matrix{Float64} = zeros(P, 3)
80+
x_airf::Matrix{Float64} = zeros(P, 3)
81+
y_airf::Matrix{Float64} = zeros(P, 3)
82+
z_airf::Matrix{Float64} = zeros(P, 3)
83+
coords::Matrix{Float64} = zeros(2(P+1), 3)
84+
end
85+
86+
"""
87+
update_panel_properties!(section_list::Vector{Section}, n_panels::Int,
88+
aero_center_loc::Float64, control_point_loc::Float64)
89+
90+
Calculate geometric properties for each panel.
91+
92+
# Arguments
93+
- section_list::Vector{Section}: List of [Section](@ref)s
94+
- `n_panels`::Int: Number of [Panel](@ref)s
95+
- `aero_center_loc`::Float64: Location of the aerodynamic center
96+
- `control_point_loc`::Float64: Location of the control point
97+
98+
# Returns:
99+
[PanelProperties](@ref) containing vectors for each property
100+
"""
101+
function update_panel_properties!(panel_props::PanelProperties, section_list::Vector{Section}, n_panels::Int,
102+
aero_center_loc::Float64, control_point_loc::Float64)
103+
coords = panel_props.coords
104+
aero_centers = panel_props.aero_centers
105+
control_points = panel_props.control_points
106+
bound_points_1 = panel_props.bound_points_1
107+
bound_points_2 = panel_props.bound_points_2
108+
x_airf = panel_props.x_airf
109+
y_airf = panel_props.y_airf
110+
z_airf = panel_props.z_airf
111+
@debug "Shape of coordinates: $(size(coords))"
112+
113+
for i in 1:n_panels
114+
coords[2i-1, :] .= section_list[i].LE_point
115+
coords[2i, :] .= section_list[i].TE_point
116+
coords[2i+1, :] .= section_list[i+1].LE_point
117+
coords[2i+2, :] .= section_list[i+1].TE_point
118+
end
119+
120+
@debug "Coordinates: $coords"
121+
122+
for i in 1:n_panels
123+
# Define panel points
124+
p1 = coords[2i-1, :] # LE_1
125+
p2 = coords[2i+1, :] # LE_2
126+
p3 = coords[2i+2, :] # TE_2
127+
p4 = coords[2i, :] # TE_1
128+
129+
# Calculate control point position
130+
di = norm(coords[2i-1, :] * 0.75 + coords[2i, :] * 0.25 -
131+
(coords[2i+1, :] * 0.75 + coords[2i+2, :] * 0.25))
132+
133+
ncp = if i == 1
134+
diplus = norm(coords[2i+1, :] * 0.75 + coords[2i+2, :] * 0.25 -
135+
(coords[2i+3, :] * 0.75 + coords[2i+4, :] * 0.25))
136+
di / (di + diplus)
137+
elseif i == n_panels
138+
dimin = norm(coords[2i-3, :] * 0.75 + coords[2i-2, :] * 0.25 -
139+
(coords[2i-1, :] * 0.75 + coords[2i, :] * 0.25))
140+
dimin / (dimin + di)
141+
else
142+
dimin = norm(coords[2i-3, :] * 0.75 + coords[2i-2, :] * 0.25 -
143+
(coords[2i-1, :] * 0.75 + coords[2i, :] * 0.25))
144+
diplus = norm(coords[2i+1, :] * 0.75 + coords[2i+2, :] * 0.25 -
145+
(coords[2i+3, :] * 0.75 + coords[2i+4, :] * 0.25))
146+
0.25 * (dimin / (dimin + di) + di / (di + diplus) + 1)
147+
end
148+
149+
ncp = 1 - ncp
150+
151+
# Calculate points
152+
@. aero_centers[i, :] = (p2 * (1 - ncp) + p1 * ncp) * 0.75 +
153+
(p3 * (1 - ncp) + p4 * ncp) * 0.25
154+
155+
@. control_points[i, :] = (p2 * (1 - ncp) + p1 * ncp) * 0.25 +
156+
(p3 * (1 - ncp) + p4 * ncp) * 0.75
157+
158+
@. bound_points_1[i, :] = p1 * 0.75 + p4 * 0.25
159+
@. bound_points_2[i, :] = p2 * 0.75 + p3 * 0.25
160+
161+
# Calculate reference frame vectors
162+
z_airf[i, :] .= normalize((control_points[i, :] .- aero_centers[i, :]) × (p1 .- p2))
163+
x_airf[i, :] .= normalize(control_points[i, :] .- aero_centers[i, :])
164+
y_airf[i, :] .= normalize(bound_points_1[i, :] .- bound_points_2[i, :])
165+
end
166+
return nothing
167+
end
168+
169+
61170
"""
62171
Wing
63172
@@ -116,7 +225,7 @@ function init!(wing::AbstractWing; aero_center_location::Float64=0.25, control_p
116225
aero_center_location,
117226
control_point_location
118227
)
119-
return panel_props
228+
return nothing
120229
end
121230

122231

0 commit comments

Comments
 (0)