-
Notifications
You must be signed in to change notification settings - Fork 0
Add stability_derivatives (angle of attack, sideslip) and trim_angle, built on linearize #346
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
1-Bort-1
wants to merge
5
commits into
main
Choose a base branch
from
agent/330-add-rigid-body-stability-derivatives-and
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
56e7dcc
Add stability_derivatives and trim_angle on linearize
1-Bort-1 7227529
Bisect the trim bracket with a generic sign-change helper
1-Bort-1 d5becc0
Merge origin/main into agent/330-add-rigid-body-stability-derivatives…
1-Bort-1 ba3f07d
Merge origin/main into agent/330-add-rigid-body-stability-derivatives…
1-Bort-1 b50a2b5
trim_angle throws on an unconverged solve and takes backend, not kwargs
1-Bort-1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| """ | ||
| stability_derivatives(solver, body_aero, alpha, beta, wind_speed; kwargs...) | ||
|
|
||
| Aerodynamic coefficients `[CFx, CFy, CFz, CMx, CMy, CMz]` of `body_aero` at angle of attack | ||
| `alpha` [rad], sideslip `beta` [rad] and `wind_speed` [m/s], and their derivatives with | ||
| respect to `alpha` and `beta` [1/rad], at the rotation rate `body_aero.omega` and with | ||
| moments about `solver.reference_point`. `kwargs` go to [`linearize`](@ref), which leaves | ||
| `body_aero` at this inflow. | ||
|
|
||
| Returns `(coeffs, dalpha, dbeta, converged)`. | ||
| """ | ||
| function stability_derivatives(solver::Solver, body_aero::BodyAerodynamics, alpha, beta, | ||
| wind_speed; kwargs...) | ||
| va_vec = apparent_wind(alpha, beta, wind_speed) | ||
| jac, results, converged = linearize(solver, body_aero, va_vec; | ||
| theta_idxs=nothing, va_idxs=1:3, aero_coeffs=true, kwargs...) | ||
| dva_dalpha = ForwardDiff.derivative( | ||
| angle -> apparent_wind(angle, beta, wind_speed), alpha) | ||
| dva_dbeta = ForwardDiff.derivative( | ||
| angle -> apparent_wind(alpha, angle, wind_speed), beta) | ||
| coeff_jac = jac[1:6, :] | ||
| return (coeffs=results[1:6], dalpha=coeff_jac * dva_dalpha, | ||
| dbeta=coeff_jac * dva_dbeta, converged) | ||
| end | ||
|
|
||
| """ | ||
| trim_angle(solver, body_aero, beta, wind_speed; alpha_range=deg2rad.(-5:2:15), | ||
| alpha_tol=1e-5, backend=AutoForwardDiff()) | ||
|
|
||
| Angles of attack [rad] at which `CMy` of `body_aero` about `solver.reference_point` changes | ||
| sign between neighbouring entries of `alpha_range`, bisected to `alpha_tol` [rad], at | ||
| sideslip `beta` [rad] and `wind_speed` [m/s]. Returns one `(alpha, dCMy_dalpha)` per trim, | ||
| the slope [1/rad] from [`stability_derivatives`](@ref) with `backend`; a trim is statically | ||
| stable where `dCMy_dalpha < 0`. Throws a [`SolveFailure`](@ref) if a solve misses the | ||
| solver's tolerances. | ||
| """ | ||
| function trim_angle(solver::Solver, body_aero::BodyAerodynamics, beta, wind_speed; | ||
| alpha_range=deg2rad.(-5:2:15), alpha_tol=1e-5, backend=AutoForwardDiff()) | ||
| is_nose_down = alpha -> nose_down(solver, body_aero, alpha, beta, wind_speed) | ||
| nose_down_range = is_nose_down.(alpha_range) | ||
| trims = @NamedTuple{alpha::Float64, dCMy_dalpha::Float64}[] | ||
| for i in 1:length(alpha_range)-1 | ||
| nose_down_range[i] == nose_down_range[i+1] && continue | ||
| alpha = bisect_sign_change(is_nose_down, alpha_range[i], alpha_range[i+1], | ||
| alpha_tol) | ||
| derivatives = stability_derivatives(solver, body_aero, alpha, beta, wind_speed; | ||
| backend, throw_on_fail=true) | ||
| push!(trims, (alpha=alpha, dCMy_dalpha=derivatives.dalpha[5])) | ||
| end | ||
| return trims | ||
| end | ||
|
|
||
| """ | ||
| coeffs_at_angles(solver, body_aero, alpha, beta, wind_speed) | ||
|
|
||
| Aerodynamic coefficients `[CFx, CFy, CFz, CMx, CMy, CMz]` of `body_aero` solved at angle of | ||
| attack `alpha` [rad], sideslip `beta` [rad] and `wind_speed` [m/s], at the rotation rate | ||
| `body_aero.omega`. Throws a [`SolveFailure`](@ref) if the solve misses the solver's | ||
| tolerances. | ||
| """ | ||
| function coeffs_at_angles(solver, body_aero, alpha, beta, wind_speed) | ||
| set_va!(body_aero, apparent_wind(alpha, beta, wind_speed), body_aero.omega) | ||
| sol = solve!(solver, body_aero; throw_on_fail=true) | ||
| return [sol.force_coeffs; sol.moment_coeffs] | ||
| end | ||
|
|
||
| """ | ||
| nose_down(solver, body_aero, alpha, beta, wind_speed) | ||
|
|
||
| Whether `CMy` from [`coeffs_at_angles`](@ref) is negative. | ||
| """ | ||
| nose_down(solver, body_aero, alpha, beta, wind_speed) = | ||
| coeffs_at_angles(solver, body_aero, alpha, beta, wind_speed)[5] < 0 | ||
|
|
||
| """ | ||
| bisect_sign_change(predicate, low, high, tol) | ||
|
|
||
| Bisect `[low, high]`, across which the boolean `predicate` flips, to a width of `tol` and | ||
| return the midpoint. | ||
| """ | ||
| function bisect_sign_change(predicate, low, high, tol) | ||
| predicate_low = predicate(low) | ||
| while high - low > tol | ||
| middle = (low + high) / 2 | ||
| if predicate(middle) == predicate_low | ||
| low = middle | ||
| else | ||
| high = middle | ||
| end | ||
| end | ||
| return (low + high) / 2 | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| using VortexStepMethod | ||
| using VortexStepMethod: coeffs_at_angles | ||
| using Test | ||
|
|
||
| """ | ||
| trimmable_wing_aero(cm) | ||
|
|
||
| A rectangular wing with lift slope 2π, `cl = 0.1` at zero angle of attack and a constant | ||
| section `cm`. | ||
| """ | ||
| function trimmable_wing_aero(cm) | ||
| chord, span = 1.0, 6.0 | ||
| alpha_range = deg2rad.(-10.0:5.0:20.0) | ||
| polar = (alpha_range, 2π .* alpha_range .+ 0.1, fill(0.02, length(alpha_range)), | ||
| fill(cm, length(alpha_range))) | ||
| wing = Wing(10) | ||
| add_section!(wing, [0.0, span / 2, 0.0], [chord, span / 2, 0.0], POLAR_VECTORS, polar) | ||
| add_section!(wing, [0.0, -span / 2, 0.0], [chord, -span / 2, 0.0], POLAR_VECTORS, | ||
| polar) | ||
| refine!(wing) | ||
| return BodyAerodynamics([wing]) | ||
| end | ||
|
|
||
| @testset "stability_derivatives match central differences of solve!" begin | ||
| body_aero = trimmable_wing_aero(0.05) | ||
| solver = Solver(body_aero; reference_point=[0.25, 0.5, 0.1], use_gamma_prev=false) | ||
| alpha, beta, wind_speed, step = deg2rad(4.0), deg2rad(3.0), 20.0, 1e-4 | ||
|
|
||
| derivatives = stability_derivatives(solver, body_aero, alpha, beta, wind_speed) | ||
| @test derivatives.converged | ||
| @test derivatives.coeffs ≈ | ||
| coeffs_at_angles(solver, body_aero, alpha, beta, wind_speed) | ||
|
|
||
| central_difference(coeffs_plus, coeffs_minus) = (coeffs_plus - coeffs_minus) / 2step | ||
| dalpha = central_difference( | ||
| coeffs_at_angles(solver, body_aero, alpha + step, beta, wind_speed), | ||
| coeffs_at_angles(solver, body_aero, alpha - step, beta, wind_speed)) | ||
| dbeta = central_difference( | ||
| coeffs_at_angles(solver, body_aero, alpha, beta + step, wind_speed), | ||
| coeffs_at_angles(solver, body_aero, alpha, beta - step, wind_speed)) | ||
| @test !iszero(dbeta) | ||
| @test derivatives.dalpha ≈ dalpha rtol = 1e-4 atol = 1e-6 | ||
| @test derivatives.dbeta ≈ dbeta rtol = 1e-4 atol = 1e-6 | ||
| end | ||
|
|
||
| @testset "trim_angle finds where CMy changes sign" begin | ||
| beta, wind_speed = 0.0, 20.0 | ||
|
|
||
| @testset "moments about the leading edge: stable trim" begin | ||
| body_aero = trimmable_wing_aero(0.05) | ||
| solver = Solver(body_aero) | ||
| trims = trim_angle(solver, body_aero, beta, wind_speed) | ||
| @test length(trims) == 1 | ||
| trim = only(trims) | ||
| trim_coeffs = coeffs_at_angles(solver, body_aero, trim.alpha, beta, wind_speed) | ||
| @test abs(trim_coeffs[5]) < 1e-5 | ||
| @test trim.dCMy_dalpha < 0 | ||
| derivatives = stability_derivatives(solver, body_aero, trim.alpha, beta, wind_speed) | ||
| @test trim.dCMy_dalpha ≈ derivatives.dalpha[5] | ||
| end | ||
|
|
||
| @testset "moments about the trailing edge: unstable trim" begin | ||
| body_aero = trimmable_wing_aero(-0.05) | ||
| solver = Solver(body_aero; reference_point=[1.0, 0.0, 0.0]) | ||
| trim = only(trim_angle(solver, body_aero, beta, wind_speed)) | ||
| trim_coeffs = coeffs_at_angles(solver, body_aero, trim.alpha, beta, wind_speed) | ||
| @test abs(trim_coeffs[5]) < 1e-5 | ||
| @test trim.dCMy_dalpha > 0 | ||
| end | ||
|
|
||
| @testset "a NONLIN solver with backend=nothing finds the same trim" begin | ||
| body_aero = trimmable_wing_aero(0.05) | ||
| trim_loop = only(trim_angle(Solver(body_aero), body_aero, beta, wind_speed)) | ||
| solver = Solver(body_aero; solver_type=NONLIN) | ||
| trim = only(trim_angle(solver, body_aero, beta, wind_speed; backend=nothing)) | ||
| @test trim.alpha ≈ trim_loop.alpha atol = 1e-4 | ||
| @test trim.dCMy_dalpha ≈ trim_loop.dCMy_dalpha rtol = 1e-4 | ||
| end | ||
|
|
||
| @testset "a solve that misses the tolerances throws" begin | ||
| body_aero = trimmable_wing_aero(0.05) | ||
| solver = Solver(body_aero; max_iterations=2) | ||
| @test_throws SolveFailure trim_angle(solver, body_aero, beta, wind_speed) | ||
| end | ||
|
|
||
| @testset "no sign change in alpha_range: no trim" begin | ||
| body_aero = trimmable_wing_aero(0.05) | ||
| solver = Solver(body_aero) | ||
| @test isempty(trim_angle(solver, body_aero, beta, wind_speed; | ||
| alpha_range=deg2rad.(4:2:12))) | ||
| end | ||
| end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
MINOR:
kwargsreachlinearizeand sosolve!for the slope, but not theCMysweep inpitch_moment_coeff. A caller passingreference_point=(asolve!keyword) gets trims found aboutsolver.reference_pointbut slopes about another point, so the stable/unstable verdict can be silently wrong.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in b50a2b5: trim_angle takes an explicit backend instead of splatting kwargs, so the sweep, the bisection and the slope all take moments about solver.reference_point.