Skip to content

Commit 538f1b8

Browse files
committed
Improve jacobian calculation
1 parent cb18276 commit 538f1b8

File tree

3 files changed

+53
-27
lines changed

3 files changed

+53
-27
lines changed

src/kite_geometry.jl

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -522,14 +522,15 @@ function RamAirWing(obj_path, dat_path; alpha=0.0, crease_frac=0.75, wind_vel=10
522522
end
523523

524524
"""
525-
smooth_deform!(wing::RamAirWing, theta_angles::AbstractVector, delta_angles::AbstractVector)
525+
group_deform!(wing::RamAirWing, theta_angles::AbstractVector, delta_angles::AbstractVector)
526526
527527
Distribute control angles across wing panels and apply smoothing using a moving average filter.
528528
529529
# Arguments
530530
- `wing::RamAirWing`: The wing to deform
531531
- `theta_angles::AbstractVector`: Twist angles in radians for each control section
532532
- `delta_angles::AbstractVector`: Trailing edge deflection angles in radians for each control section
533+
- `smooth::Bool`: Wether to apply smoothing or not
533534
534535
# Algorithm
535536
1. Distributes each control input to its corresponding group of panels
@@ -541,7 +542,7 @@ Distribute control angles across wing panels and apply smoothing using a moving
541542
# Returns
542543
- `nothing` (modifies wing in-place)
543544
"""
544-
function smooth_deform!(wing::RamAirWing, theta_angles::AbstractVector, delta_angles::AbstractVector)
545+
function group_deform!(wing::RamAirWing, theta_angles::AbstractVector, delta_angles=nothing; smooth=false)
545546
!(wing.n_panels % length(theta_angles) == 0) && throw(ArgumentError("Number of angles has to be a multiple of number of panels"))
546547
n_panels = wing.n_panels
547548
theta_dist = wing.theta_dist
@@ -552,22 +553,29 @@ function smooth_deform!(wing::RamAirWing, theta_angles::AbstractVector, delta_an
552553
for _ in 1:(wing.n_panels ÷ length(theta_angles))
553554
dist_idx += 1
554555
theta_dist[dist_idx] = theta_angles[angle_idx]
555-
delta_dist[dist_idx] = delta_angles[angle_idx]
556+
!isnothing(delta_angles) && (delta_dist[dist_idx] = delta_angles[angle_idx])
556557
end
557558
end
558559
@assert (dist_idx == wing.n_panels)
559560

560-
window_size = wing.n_panels ÷ length(theta_angles)
561-
if n_panels > window_size
562-
smoothed = wing.cache[1][theta_dist]
563-
for i in (window_size÷2 + 1):(n_panels - window_size÷2)
564-
@views smoothed[i] = mean(theta_dist[(i - window_size÷2):(i + window_size÷2)])
565-
end
566-
theta_dist .= smoothed
567-
for i in (window_size÷2 + 1):(n_panels - window_size÷2)
568-
@views smoothed[i] = mean(delta_dist[(i - window_size÷2):(i + window_size÷2)])
561+
if smooth
562+
window_size = wing.n_panels ÷ length(theta_angles)
563+
if n_panels > window_size
564+
smoothed = wing.cache[1][theta_dist]
565+
smoothed .= theta_dist
566+
for i in (window_size÷2 + 1):(n_panels - window_size÷2)
567+
@views smoothed[i] = mean(theta_dist[(i - window_size÷2):(i + window_size÷2)])
568+
end
569+
theta_dist .= smoothed
570+
571+
if !isnothing(delta_angles)
572+
smoothed .= delta_dist
573+
for i in (window_size÷2 + 1):(n_panels - window_size÷2)
574+
@views smoothed[i] = mean(delta_dist[(i - window_size÷2):(i + window_size÷2)])
575+
end
576+
delta_dist .= smoothed
577+
end
569578
end
570-
delta_dist .= smoothed
571579
end
572580
deform!(wing)
573581
return nothing

src/panel.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ function init_pos!(
113113
panel.x_airf .= x_airf
114114
panel.y_airf .= y_airf
115115
panel.z_airf .= z_airf
116-
panel.delta = Float64(delta)
116+
panel.delta = delta
117117
return nothing
118118
end
119119

@@ -361,7 +361,7 @@ function calculate_cd_cm(panel::Panel, alpha::Float64)
361361
elseif panel.aero_model == POLAR_VECTORS
362362
cd = panel.cd_interp(alpha)::Float64
363363
cm = panel.cm_interp(alpha)::Float64
364-
elseif panel.aero_model == POLAR_MATRICES
364+
elseif panel.aero_model == POLAR_MATRICES
365365
cd = panel.cd_interp(alpha, panel.delta)::Float64
366366
cm = panel.cm_interp(alpha, panel.delta)::Float64
367367
elseif !(panel.aero_model == INVISCID)

src/solver.jl

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ sol::VSMSolution = VSMSolution(): The result of calling [solve!](@ref)
110110
artificial_damping::NamedTuple{(:k2, :k4), Tuple{Float64, Float64}} =(k2=0.1, k4=0.0)
111111

112112
# Additional settings
113-
type_initial_gamma_distribution::InitialGammaDistribution = ELLIPTIC
113+
type_initial_gamma_distribution::InitialGammaDistribution = ZEROS
114114
core_radius_fraction::Float64 = 1e-20
115115
mu::Float64 = 1.81e-5
116116
is_only_f_and_gamma_output::Bool = false
@@ -121,6 +121,7 @@ sol::VSMSolution = VSMSolution(): The result of calling [solve!](@ref)
121121
cache::Vector{PreallocationTools.LazyBufferCache{typeof(identity), typeof(identity)}} = [LazyBufferCache() for _ in 1:11]
122122
cache_base::Vector{PreallocationTools.LazyBufferCache{typeof(identity), typeof(identity)}} = [LazyBufferCache()]
123123
cache_solve::Vector{PreallocationTools.LazyBufferCache{typeof(identity), typeof(identity)}} = [LazyBufferCache()]
124+
cache_lin::Vector{PreallocationTools.LazyBufferCache{typeof(identity), typeof(identity)}} = [LazyBufferCache() for _ in 1:4]
124125

125126
# Solution
126127
sol::VSMSolution{P} = VSMSolution(P)
@@ -624,26 +625,43 @@ Linearize the ram air wing aero model around an operating point using automatic
624625
- `forces_op`: Forces at the operating point [Fx, Fy, Fz, Mx, My, Mz]
625626
"""
626627
function linearize(solver::Solver, body_aero::BodyAerodynamics, wing::RamAirWing, y;
627-
alpha_idxs=1:4,
628+
theta_idxs=1:4,
628629
delta_idxs=nothing,
629630
va_idxs=nothing,
630631
omega_idxs=nothing,
631632
kwargs...)
632633

633-
init_va = copy(body_aero.va)
634-
init_alpha_beta = copy(y[alpha_idxs])
635-
zero_delta = zeros(alpha_idxs)
634+
init_va = solver.cache_lin[1][body_aero.va]
635+
init_va .= body_aero.va
636+
if !isnothing(theta_idxs)
637+
last_theta = solver.cache_lin[2][y[theta_idxs]]
638+
last_theta .= y[theta_idxs]
639+
end
640+
if !isnothing(delta_idxs)
641+
last_delta = solver.cache_lin[3][y[delta_idxs]]
642+
last_delta .= y[delta_idxs]
643+
end
636644

637645
# Function to compute forces for given control inputs
638646
function calc_results!(results, y)
639-
if !isnothing(alpha_idxs) && isnothing(delta_idxs)
640-
@views VortexStepMethod.smooth_deform!(wing, y[alpha_idxs], zero_delta)
641-
elseif !isnothing(alpha_idxs) && !isnothing(delta_idxs)
642-
@views VortexStepMethod.smooth_deform!(wing, y[alpha_idxs], y[delta_idxs])
647+
@views theta_angles = isnothing(theta_idxs) ? nothing : y[theta_idxs]
648+
@views delta_angles = isnothing(delta_idxs) ? nothing : y[delta_idxs]
649+
650+
if !isnothing(theta_angles)
651+
if isnothing(delta_angles)
652+
if !all(theta_angles .== last_theta)
653+
VortexStepMethod.group_deform!(wing, theta_angles; smooth=false)
654+
VortexStepMethod.init!(body_aero; init_aero=false)
655+
last_theta .= theta_angles
656+
end
657+
elseif !all(delta_angles .== last_delta) || !all(theta_angles .== last_theta)
658+
VortexStepMethod.group_deform!(wing, theta_angles, delta_angles; smooth=false)
659+
VortexStepMethod.init!(body_aero; init_aero=false)
660+
last_theta .= theta_angles
661+
last_delta .= delta_angles
662+
end
643663
end
644664

645-
VortexStepMethod.init!(body_aero; init_aero=false)
646-
647665
if !isnothing(va_idxs) && isnothing(omega_idxs)
648666
set_va!(body_aero, y[va_idxs])
649667
elseif !isnothing(va_idxs) && !isnothing(omega_idxs)
@@ -661,7 +679,7 @@ function linearize(solver::Solver, body_aero::BodyAerodynamics, wing::RamAirWing
661679

662680
results = zeros(3+3+length(solver.sol.moment_coeff_dist))
663681
jac = zeros(length(results), length(y))
664-
backend = AutoFiniteDiff(absstep=1e-3, relstep=1e-3)
682+
backend = AutoFiniteDiff(absstep=1e2solver.atol, relstep=1e2solver.rtol)
665683
prep = prepare_jacobian(calc_results!, results, backend, y)
666684
jacobian!(calc_results!, results, jac, prep, backend, y)
667685

0 commit comments

Comments
 (0)