Skip to content

Commit 34a2b48

Browse files
committed
add non-allocating update pos functions
1 parent c8ca59c commit 34a2b48

File tree

9 files changed

+111
-38
lines changed

9 files changed

+111
-38
lines changed

Project.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
1313
Logging = "56ddb016-857b-54e1-b83d-db4d58db5568"
1414
Measures = "442fdcdd-2543-5da2-b0f3-8c86c306513e"
1515
NonlinearSolve = "8913a72c-1f9b-4ce2-8d82-65094dcecaec"
16+
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
1617
Serialization = "9e88b42a-f829-5b0c-bbe9-9e923198166b"
1718
SharedArrays = "1a1011a3-84de-559e-8e89-a11a2f7dc383"
1819
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
@@ -34,13 +35,13 @@ Timers = "0.1"
3435
julia = "1.10, 1.11"
3536

3637
[extras]
38+
BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf"
3739
CSV = "336ed68f-0bac-5ca0-87d4-7b16caf5d00b"
3840
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
3941
Distributed = "8ba89e20-285c-5b6f-9357-94700520ee1b"
4042
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
4143
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
4244
Xfoil = "19641d66-a62d-11e8-2441-8f57a969a9c4"
43-
BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf"
4445

4546
[targets]
4647
test = ["Test", "DataFrames", "CSV", "Distributed", "Documenter", "Xfoil", "BenchmarkTools"]

examples/update_pos.jl

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using VortexStepMethod
2+
using VortexStepMethod: BoundFilament, MVec3, SemiInfiniteFilament, update_pos!, Panel, Section
3+
4+
5+
x1 = zeros(MVec3)
6+
direction = zeros(MVec3)
7+
vel_mag = zero(Float64)
8+
filament_direction = zero(Int64)
9+
@time filament = SemiInfiniteFilament(x1, direction, vel_mag, filament_direction)
10+
@time update_pos!(filament, x1, direction, vel_mag, filament_direction)
11+
12+
bound_point_1 = zeros(MVec3)
13+
bound_point_2 = zeros(MVec3)
14+
@time filament = BoundFilament(bound_point_1, bound_point_2)
15+
@time update_pos!(filament, bound_point_1, bound_point_2)
16+
17+
LE_point = zeros(MVec3)
18+
TE_point = zeros(MVec3)
19+
aero_input = :inviscid
20+
@time section = Section(LE_point, TE_point, aero_input)
21+
@time update_pos!(section, LE_point, TE_point)
22+
23+

src/VortexStepMethod.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ using Measures
1111
using LaTeXStrings
1212
using NonlinearSolve
1313
using Interpolations
14-
using Interpolations: linear_interpolation, Line, Extrapolation, FilledExtrapolation
14+
using Interpolations: Extrapolation
1515
using Serialization
1616
using SharedArrays
1717

@@ -39,14 +39,14 @@ const MVec3 = MVector{3, Float64}
3939
4040
Position vector, either a `MVec3` or a `Vector` for use in function signatures.
4141
"""
42-
const PosVector=Union{MVec3, Vector, SizedVector{3, Float64, Vector{Float64}}}
42+
const PosVector=Union{MVec3, Vector}
4343

4444
"""
4545
const VelVector=Union{MVec3, Vector}
4646
4747
Velocity vector, either a `MVec3` or a `Vector` for use in function signatures.
4848
"""
49-
const VelVector=Union{MVec3, Vector, SizedVector{3, Float64, Vector{Float64}}}
49+
const VelVector=Union{MVec3, Vector}
5050

5151
"""
5252
Model `VSM` `LLT`

src/body_aerodynamics.jl

Lines changed: 50 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,39 @@ function Base.setproperty!(obj::BodyAerodynamics, sym::Symbol, val)
104104
end
105105
end
106106

107+
function update_pos!(body_aero::BodyAerodynamics, le_pos::AbstractMatrix, te_pos::AbstractMatrix)
108+
panels = Panel[]
109+
for wing in body_aero.wings
110+
section_list = refine_aerodynamic_mesh(wing)
111+
n_panels_per_wing = length(section_list) - 1
112+
113+
# Calculate panel properties
114+
panel_props = calculate_panel_properties(
115+
section_list,
116+
n_panels_per_wing,
117+
aero_center_location,
118+
control_point_location
119+
)
120+
121+
# Create panels
122+
for panel in body_aero.panels
123+
124+
update_pos!(
125+
panel,
126+
section_list[i],
127+
section_list[i+1],
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]
135+
)
136+
end
137+
end
138+
end
139+
107140
"""
108141
PanelProperties
109142
@@ -650,20 +683,13 @@ Set velocity array and update wake filaments.
650683
- `va`: Either a velocity vector or a list of velocity vectors
651684
- `omega`: Turn rate around x y and z axis
652685
"""
653-
function set_va!(body_aero::BodyAerodynamics, va, omega=zeros(MVec3))
654-
omega = MVec3(omega)
655-
656-
# Add length check for va
657-
if va isa Vector{Float64} && length(va) != 3 && length(va) != length(body_aero.panels)
658-
throw(ArgumentError("va must be length 3 or match number of panels"))
659-
end
686+
function set_va!(body_aero::BodyAerodynamics, va::VelVector, omega=zeros(MVec3))
687+
length(va) != 3 && throw(ArgumentError("va must be length 3"))
660688

661689
# Calculate va_distribution based on input type
662-
va_distribution = if length(va) == 3 && all(omega .== 0.0)
690+
va_distribution = if all(omega .== 0.0)
663691
repeat(reshape(va, 1, 3), length(body_aero.panels))
664-
elseif length(va) == length(body_aero.panels)
665-
va
666-
elseif !all(omega .== 0.0) && length(va) == 3
692+
elseif !all(omega .== 0.0)
667693
va_dist = zeros(length(body_aero.panels), 3)
668694

669695
for wing in body_aero.wings
@@ -677,8 +703,6 @@ function set_va!(body_aero::BodyAerodynamics, va, omega=zeros(MVec3))
677703
end
678704
end
679705
va_dist
680-
else
681-
throw(ArgumentError("Invalid va distribution: length(va)=$(length(va)) ≠ n_panels=$(length(body_aero.panels))"))
682706
end
683707

684708
# Update panel velocities
@@ -691,3 +715,16 @@ function set_va!(body_aero::BodyAerodynamics, va, omega=zeros(MVec3))
691715
body_aero._va = va
692716
return nothing
693717
end
718+
719+
function set_va!(body_aero::BodyAerodynamics, va_distribution::Vector{VelVector}, omega=zeros(MVec3))
720+
length(va) != length(body_aero.panels) && throw(ArgumentError("Length of va distribution should be equal to number of panels."))
721+
722+
for (i, panel) in enumerate(body_aero.panels)
723+
panel.va = va_distribution[i]
724+
end
725+
726+
# Update wake elements
727+
body_aero.panels = frozen_wake(va_distribution, body_aero.panels)
728+
body_aero._va = va
729+
return nothing
730+
end

src/filament.jl

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Represents a bound vortex filament defined by two points.
1919
- length: Filament length
2020
- r0::MVec3: Vector from x1 to x2
2121
"""
22-
struct BoundFilament <: Filament
22+
mutable struct BoundFilament <: Filament
2323
x1::MVec3 # First point
2424
x2::MVec3 # Second point
2525
length::Float64 # Filament length
@@ -30,6 +30,14 @@ struct BoundFilament <: Filament
3030
end
3131
end
3232

33+
function update_pos!(filament::BoundFilament, x1::PosVector, x2::PosVector)
34+
filament.x1 .= x1
35+
filament.x2 .= x2
36+
filament.length = norm(x2 - x1)
37+
filament.r0 .= x2 .- x1
38+
return nothing
39+
end
40+
3341
"""
3442
velocity_3D_bound_vortex(filament::BoundFilament, XVP::Vector{Float64},
3543
gamma::Float64, core_radius_fraction::Float64)
@@ -151,13 +159,21 @@ end
151159
152160
Represents a semi-infinite vortex filament.
153161
"""
154-
struct SemiInfiniteFilament <: Filament
162+
mutable struct SemiInfiniteFilament <: Filament
155163
x1::MVec3 # Starting point
156164
direction::MVec3 # Direction vector
157165
vel_mag::Float64 # Velocity magnitude
158166
filament_direction::Int64 # Direction indicator (-1 or 1)
159167
end
160168

169+
function update_pos!(filament::SemiInfiniteFilament, x1::PosVector, direction::PosVector, vel_mag, filament_direction)
170+
filament.x1 .= x1
171+
filament.direction .= direction
172+
filament.vel_mag = vel_mag
173+
filament.filament_direction = filament_direction
174+
return nothing
175+
end
176+
161177
"""
162178
velocity_3D_trailing_vortex_semiinfinite(filament::SemiInfiniteFilament,
163179
Vf::Vector{Float64}, XVP::Vector{Float64},

src/kite_geometry.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ mutable struct KiteWing <: AbstractWing
205205
area_interp::Extrapolation
206206

207207
function KiteWing(obj_path, dat_path; alpha=0.0, crease_frac=0.75, wind_vel=10., mass=1.0,
208-
n_panels=54, n_sections=n_panels+1, spanwise_panel_distribution=LINEAR, spanwise_direction=[0.0, 1.0, 0.0])
208+
n_panels=54, n_sections=n_panels+1, spanwise_panel_distribution=UNCHANGED, spanwise_direction=[0.0, 1.0, 0.0])
209209

210210
!isapprox(spanwise_direction, [0.0, 1.0, 0.0]) && @error "Spanwise direction has to be [0.0, 1.0, 0.0]"
211211

@@ -272,4 +272,4 @@ function rotate_v_around_k(v, k, θ)
272272
k = normalize(k)
273273
v_rot = v * cos(θ) + (k × v) * sin(θ) + k * (k v) * (1 - cos(θ))
274274
return v_rot
275-
end
275+
end

src/panel.jl

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,6 @@ mutable struct Panel
8484
throw(ArgumentError("Both sections must have the same aero_input, not $aero_model and $aero_model_2"))
8585
end
8686

87-
# Initialize aerodynamic properties
88-
cl_coeffs, cd_coeffs, cm_coeffs = zeros(3), zeros(3), zeros(3)
89-
9087
# Calculate width
9188
width = norm(bound_point_2 - bound_point_1)
9289

@@ -95,9 +92,12 @@ mutable struct Panel
9592
BoundFilament(bound_point_2, bound_point_1),
9693
BoundFilament(bound_point_1, TE_point_1),
9794
BoundFilament(TE_point_2, bound_point_2)
98-
]
99-
95+
]
96+
10097
cl_interp, cd_interp, cm_interp = nothing, nothing, nothing
98+
99+
# Initialize aerodynamic properties
100+
cl_coeffs, cd_coeffs, cm_coeffs = zeros(3), zeros(3), zeros(3)
101101

102102
if aero_model === :lei_airfoil_breukels
103103
cl_coeffs, cd_coeffs, cm_coeffs = compute_lei_coefficients(section_1, section_2)
@@ -171,12 +171,9 @@ function update_pos!(
171171
y_airf::PosVector,
172172
z_airf::PosVector
173173
)
174-
width = norm(bound_point_2 - bound_point_1)
175-
filaments = [
176-
BoundFilament(bound_point_2, bound_point_1),
177-
BoundFilament(bound_point_1, TE_point_1),
178-
BoundFilament(TE_point_2, bound_point_2)
179-
]
174+
update_filament!(filaments[1], bound_point_2, bound_point_1)
175+
update_filament!(filaments[2], bound_point_1, TE_point_1)
176+
update_filament!(filaments[3], TE_point_2, bound_point_2)
180177

181178
panel.TE_point_1 .= section_1.TE_point
182179
panel.LE_point_1 .= section_1.LE_point
@@ -194,7 +191,7 @@ function update_pos!(
194191
panel.x_airf .= x_airf
195192
panel.y_airf .= y_airf
196193
panel.z_airf .= z_airf
197-
panel.width = width
194+
panel.width = norm(bound_point_2 - bound_point_1)
198195
panel.filaments .= filaments
199196
nothing
200197
end

src/wing_geometry.jl

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,13 @@ struct Section{T}
2424
end
2525

2626
"""
27-
update_section!(wing::Wing, LE_point::PosVector, TE_point::PosVector)
27+
update_pos!(wing::Wing, LE_point::PosVector, TE_point::PosVector)
2828
2929
Update the section leading edge and trailing edge positions.
3030
"""
31-
function update_section!(section::Section, LE_point::AbstractVector, TE_point::AbstractVector)
32-
size(LE_point) != (3,) && throw(ArgumentError("LE_point has wrong size"))
33-
size(TE_point) != (3,) && throw(ArgumentError("TE_point has wrong size"))
31+
function update_pos!(section::Section, LE_point::AbstractVector, TE_point::AbstractVector)
3432
section.LE_point .= LE_point
33+
section.TE_point .= TE_point
3534
return nothing
3635
end
3736

test/test_kite_geometry.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ using Serialization
141141
wing = KiteWing(test_obj_path, test_dat_path)
142142

143143
@test wing.n_panels == 54 # Default value
144-
@test wing.spanwise_panel_distribution == LINEAR
144+
@test wing.spanwise_panel_distribution == UNCHANGED
145145
@test wing.spanwise_direction [0.0, 1.0, 0.0]
146146
@test length(wing.sections) > 0 # Should have sections now
147147
@test wing.mass 1.0

0 commit comments

Comments
 (0)