|
| 1 | +using VortexStepMethod |
| 2 | +using LinearAlgebra |
| 3 | +using Test |
| 4 | + |
| 5 | +@testset "Unrefined Arrays Tests" begin |
| 6 | + @testset "Unrefined section array aggregation" begin |
| 7 | + # Create a simple wing with unrefined sections |
| 8 | + n_panels = 20 |
| 9 | + n_unrefined_sections = 5 # 5 unrefined sections |
| 10 | + |
| 11 | + # Create a test wing settings file |
| 12 | + settings_file = create_temp_wing_settings("solver", "solver_test_wing.yaml"; alpha=5.0, beta=0.0, wind_speed=10.0) |
| 13 | + |
| 14 | + try |
| 15 | + # Modify settings to use specific panel configuration |
| 16 | + settings = VSMSettings(settings_file) |
| 17 | + settings.wings[1].n_panels = n_panels |
| 18 | + settings.solver_settings.n_panels = n_panels |
| 19 | + |
| 20 | + # Create wing and solver |
| 21 | + wing = Wing(settings) |
| 22 | + body_aero = BodyAerodynamics([wing]) |
| 23 | + solver = Solver(body_aero, settings) |
| 24 | + |
| 25 | + # Set conditions and solve |
| 26 | + va = [10.0, 0.0, 0.0] |
| 27 | + set_va!(body_aero, va) |
| 28 | + sol = solve!(solver, body_aero) |
| 29 | + |
| 30 | + # Test 1: Unrefined arrays exist and have correct size |
| 31 | + @test length(sol.cl_unrefined_dist) == wing.n_unrefined_sections |
| 32 | + @test length(sol.cd_unrefined_dist) == wing.n_unrefined_sections |
| 33 | + @test length(sol.cm_unrefined_dist) == wing.n_unrefined_sections |
| 34 | + |
| 35 | + # Test 2: Unrefined arrays are not all zeros (solver computed them) |
| 36 | + @test !all(sol.cl_unrefined_dist .== 0.0) |
| 37 | + @test !all(sol.cd_unrefined_dist .== 0.0) |
| 38 | + |
| 39 | + # Test 3: Verify unrefined coefficients are averaged from refined panels |
| 40 | + # refined_panel_mapping maps each refined panel to its unrefined section index |
| 41 | + for unrefined_idx in 1:wing.n_unrefined_sections |
| 42 | + # Find all refined panels that map to this unrefined section |
| 43 | + refined_panel_indices = findall(x -> x == unrefined_idx, wing.refined_panel_mapping) |
| 44 | + |
| 45 | + if !isempty(refined_panel_indices) |
| 46 | + # Calculate expected average from refined panel coefficients |
| 47 | + expected_cl = sum(sol.cl_dist[refined_panel_indices]) / length(refined_panel_indices) |
| 48 | + expected_cd = sum(sol.cd_dist[refined_panel_indices]) / length(refined_panel_indices) |
| 49 | + expected_cm = sum(sol.cm_dist[refined_panel_indices]) / length(refined_panel_indices) |
| 50 | + |
| 51 | + # Check if unrefined coefficients match expected averages |
| 52 | + # Handle NaN values that can occur in INVISCID models |
| 53 | + if isnan(expected_cl) |
| 54 | + @test isnan(sol.cl_unrefined_dist[unrefined_idx]) |
| 55 | + else |
| 56 | + @test isapprox(sol.cl_unrefined_dist[unrefined_idx], expected_cl, rtol=1e-10) |
| 57 | + end |
| 58 | + if isnan(expected_cd) |
| 59 | + @test isnan(sol.cd_unrefined_dist[unrefined_idx]) |
| 60 | + else |
| 61 | + @test isapprox(sol.cd_unrefined_dist[unrefined_idx], expected_cd, rtol=1e-10) |
| 62 | + end |
| 63 | + if isnan(expected_cm) |
| 64 | + @test isnan(sol.cm_unrefined_dist[unrefined_idx]) |
| 65 | + else |
| 66 | + @test isapprox(sol.cm_unrefined_dist[unrefined_idx], expected_cm, rtol=1e-10) |
| 67 | + end |
| 68 | + end |
| 69 | + end |
| 70 | + |
| 71 | + # Test 4: Verify physical consistency (lift coefficients should be positive at positive AoA) |
| 72 | + # Skip test if values are NaN |
| 73 | + if !any(isnan.(sol.cl_unrefined_dist)) |
| 74 | + @test all(sol.cl_unrefined_dist .> 0.0) |
| 75 | + end |
| 76 | + |
| 77 | + finally |
| 78 | + rm(settings_file; force=true) |
| 79 | + end |
| 80 | + end |
| 81 | + |
| 82 | + @testset "Unrefined arrays with different panel counts" begin |
| 83 | + # Test with various panel/section combinations |
| 84 | + test_cases = [ |
| 85 | + (n_panels=40, n_unrefined_expected=21), # From YAML file sections |
| 86 | + (n_panels=30, n_unrefined_expected=21), |
| 87 | + (n_panels=24, n_unrefined_expected=21), |
| 88 | + ] |
| 89 | + |
| 90 | + for (n_panels, n_unrefined_expected) in test_cases |
| 91 | + settings_file = create_temp_wing_settings("solver", "solver_test_wing.yaml"; alpha=5.0, beta=0.0, wind_speed=10.0) |
| 92 | + |
| 93 | + try |
| 94 | + settings = VSMSettings(settings_file) |
| 95 | + settings.wings[1].n_panels = n_panels |
| 96 | + settings.solver_settings.n_panels = n_panels |
| 97 | + |
| 98 | + wing = Wing(settings) |
| 99 | + body_aero = BodyAerodynamics([wing]) |
| 100 | + solver = Solver(body_aero, settings) |
| 101 | + |
| 102 | + va = [10.0, 0.0, 0.0] |
| 103 | + set_va!(body_aero, va) |
| 104 | + sol = solve!(solver, body_aero) |
| 105 | + |
| 106 | + # Verify arrays have correct size |
| 107 | + @test length(sol.cl_unrefined_dist) == wing.n_unrefined_sections |
| 108 | + @test length(sol.cd_unrefined_dist) == wing.n_unrefined_sections |
| 109 | + @test length(sol.cm_unrefined_dist) == wing.n_unrefined_sections |
| 110 | + |
| 111 | + # Verify unrefined coefficients are computed correctly using mapping |
| 112 | + for unrefined_idx in 1:wing.n_unrefined_sections |
| 113 | + refined_panel_indices = findall(x -> x == unrefined_idx, wing.refined_panel_mapping) |
| 114 | + |
| 115 | + if !isempty(refined_panel_indices) |
| 116 | + expected_cl = sum(sol.cl_dist[refined_panel_indices]) / length(refined_panel_indices) |
| 117 | + expected_cd = sum(sol.cd_dist[refined_panel_indices]) / length(refined_panel_indices) |
| 118 | + expected_cm = sum(sol.cm_dist[refined_panel_indices]) / length(refined_panel_indices) |
| 119 | + |
| 120 | + # Handle NaN for all coefficients |
| 121 | + if isnan(expected_cl) |
| 122 | + @test isnan(sol.cl_unrefined_dist[unrefined_idx]) |
| 123 | + else |
| 124 | + @test isapprox(sol.cl_unrefined_dist[unrefined_idx], expected_cl, rtol=1e-10) |
| 125 | + end |
| 126 | + if isnan(expected_cd) |
| 127 | + @test isnan(sol.cd_unrefined_dist[unrefined_idx]) |
| 128 | + else |
| 129 | + @test isapprox(sol.cd_unrefined_dist[unrefined_idx], expected_cd, rtol=1e-10) |
| 130 | + end |
| 131 | + if isnan(expected_cm) |
| 132 | + @test isnan(sol.cm_unrefined_dist[unrefined_idx]) |
| 133 | + else |
| 134 | + @test isapprox(sol.cm_unrefined_dist[unrefined_idx], expected_cm, rtol=1e-10) |
| 135 | + end |
| 136 | + end |
| 137 | + end |
| 138 | + |
| 139 | + finally |
| 140 | + rm(settings_file; force=true) |
| 141 | + end |
| 142 | + end |
| 143 | + end |
| 144 | +end |
0 commit comments