|
| 1 | +using VortexStepMethod |
| 2 | +using LinearAlgebra |
| 3 | +using Test |
| 4 | + |
| 5 | +# Load test support |
| 6 | +include("../TestSupport.jl") |
| 7 | +using .TestSupport |
| 8 | + |
| 9 | +@testset "Group Coefficient Arrays Tests" begin |
| 10 | + @testset "Group coefficients with EQUAL_SIZE method" begin |
| 11 | + # Create a simple wing with groups |
| 12 | + n_panels = 20 |
| 13 | + n_groups = 4 |
| 14 | + |
| 15 | + # Create a test wing settings file |
| 16 | + settings_file = create_temp_wing_settings("solver", "solver_test_wing.yaml"; alpha=5.0, beta=0.0, wind_speed=10.0) |
| 17 | + |
| 18 | + try |
| 19 | + # Modify settings to use specific panel/group configuration |
| 20 | + settings = VSMSettings(settings_file) |
| 21 | + settings.wings[1].n_panels = n_panels |
| 22 | + settings.wings[1].n_groups = n_groups |
| 23 | + settings.wings[1].grouping_method = EQUAL_SIZE |
| 24 | + settings.solver_settings.n_panels = n_panels |
| 25 | + settings.solver_settings.n_groups = n_groups |
| 26 | + |
| 27 | + # Create wing and solver |
| 28 | + wing = Wing(settings) |
| 29 | + body_aero = BodyAerodynamics([wing]) |
| 30 | + solver = Solver(body_aero, settings) |
| 31 | + |
| 32 | + # Set conditions and solve |
| 33 | + va = [10.0, 0.0, 0.0] |
| 34 | + set_va!(body_aero, va) |
| 35 | + sol = solve!(solver, body_aero) |
| 36 | + |
| 37 | + # Test 1: Group arrays exist and have correct size |
| 38 | + @test length(sol.cl_group_array) == n_groups |
| 39 | + @test length(sol.cd_group_array) == n_groups |
| 40 | + @test length(sol.cm_group_array) == n_groups |
| 41 | + |
| 42 | + # Test 2: Group arrays are not all zeros (solver computed them) |
| 43 | + @test !all(sol.cl_group_array .== 0.0) |
| 44 | + @test !all(sol.cd_group_array .== 0.0) |
| 45 | + |
| 46 | + # Test 3: Verify group coefficients are averages of panel coefficients |
| 47 | + panels_per_group = n_panels ÷ n_groups |
| 48 | + for group_idx in 1:n_groups |
| 49 | + panel_start = (group_idx - 1) * panels_per_group + 1 |
| 50 | + panel_end = group_idx * panels_per_group |
| 51 | + |
| 52 | + # Calculate expected average from panel coefficients |
| 53 | + expected_cl = sum(sol.cl_array[panel_start:panel_end]) / panels_per_group |
| 54 | + expected_cd = sum(sol.cd_array[panel_start:panel_end]) / panels_per_group |
| 55 | + expected_cm = sum(sol.cm_array[panel_start:panel_end]) / panels_per_group |
| 56 | + |
| 57 | + # Check if group coefficients match expected averages |
| 58 | + # Handle NaN values that can occur in INVISCID models |
| 59 | + if isnan(expected_cl) |
| 60 | + @test isnan(sol.cl_group_array[group_idx]) |
| 61 | + else |
| 62 | + @test isapprox(sol.cl_group_array[group_idx], expected_cl, rtol=1e-10) |
| 63 | + end |
| 64 | + if isnan(expected_cd) |
| 65 | + @test isnan(sol.cd_group_array[group_idx]) |
| 66 | + else |
| 67 | + @test isapprox(sol.cd_group_array[group_idx], expected_cd, rtol=1e-10) |
| 68 | + end |
| 69 | + if isnan(expected_cm) |
| 70 | + @test isnan(sol.cm_group_array[group_idx]) |
| 71 | + else |
| 72 | + @test isapprox(sol.cm_group_array[group_idx], expected_cm, rtol=1e-10) |
| 73 | + end |
| 74 | + end |
| 75 | + |
| 76 | + # Test 4: Verify physical consistency (lift coefficients should be positive at positive AoA) |
| 77 | + # Skip test if values are NaN |
| 78 | + if !any(isnan.(sol.cl_group_array)) |
| 79 | + @test all(sol.cl_group_array .> 0.0) |
| 80 | + end |
| 81 | + |
| 82 | + finally |
| 83 | + rm(settings_file; force=true) |
| 84 | + end |
| 85 | + end |
| 86 | + |
| 87 | + @testset "Group coefficients with n_groups=0 (no grouping)" begin |
| 88 | + # Create a wing with no groups |
| 89 | + n_panels = 20 |
| 90 | + n_groups = 0 |
| 91 | + |
| 92 | + settings_file = create_temp_wing_settings("solver", "solver_test_wing.yaml"; alpha=5.0, beta=0.0, wind_speed=10.0) |
| 93 | + |
| 94 | + try |
| 95 | + settings = VSMSettings(settings_file) |
| 96 | + settings.wings[1].n_panels = n_panels |
| 97 | + settings.wings[1].n_groups = n_groups |
| 98 | + settings.solver_settings.n_panels = n_panels |
| 99 | + settings.solver_settings.n_groups = n_groups |
| 100 | + |
| 101 | + wing = Wing(settings) |
| 102 | + body_aero = BodyAerodynamics([wing]) |
| 103 | + solver = Solver(body_aero, settings) |
| 104 | + |
| 105 | + va = [10.0, 0.0, 0.0] |
| 106 | + set_va!(body_aero, va) |
| 107 | + sol = solve!(solver, body_aero) |
| 108 | + |
| 109 | + # Test: Group arrays should be empty when n_groups=0 |
| 110 | + @test length(sol.cl_group_array) == 0 |
| 111 | + @test length(sol.cd_group_array) == 0 |
| 112 | + @test length(sol.cm_group_array) == 0 |
| 113 | + |
| 114 | + finally |
| 115 | + rm(settings_file; force=true) |
| 116 | + end |
| 117 | + end |
| 118 | + |
| 119 | + @testset "Group coefficients with different group sizes" begin |
| 120 | + # Test with various panel/group combinations |
| 121 | + test_cases = [ |
| 122 | + (n_panels=40, n_groups=8), |
| 123 | + (n_panels=30, n_groups=5), |
| 124 | + (n_panels=24, n_groups=6), |
| 125 | + ] |
| 126 | + |
| 127 | + for (n_panels, n_groups) in test_cases |
| 128 | + settings_file = create_temp_wing_settings("solver", "solver_test_wing.yaml"; alpha=5.0, beta=0.0, wind_speed=10.0) |
| 129 | + |
| 130 | + try |
| 131 | + settings = VSMSettings(settings_file) |
| 132 | + settings.wings[1].n_panels = n_panels |
| 133 | + settings.wings[1].n_groups = n_groups |
| 134 | + settings.wings[1].grouping_method = EQUAL_SIZE |
| 135 | + settings.solver_settings.n_panels = n_panels |
| 136 | + settings.solver_settings.n_groups = n_groups |
| 137 | + |
| 138 | + wing = Wing(settings) |
| 139 | + body_aero = BodyAerodynamics([wing]) |
| 140 | + solver = Solver(body_aero, settings) |
| 141 | + |
| 142 | + va = [10.0, 0.0, 0.0] |
| 143 | + set_va!(body_aero, va) |
| 144 | + sol = solve!(solver, body_aero) |
| 145 | + |
| 146 | + # Verify arrays have correct size |
| 147 | + @test length(sol.cl_group_array) == n_groups |
| 148 | + @test length(sol.cd_group_array) == n_groups |
| 149 | + @test length(sol.cm_group_array) == n_groups |
| 150 | + |
| 151 | + # Verify group coefficients are computed correctly |
| 152 | + panels_per_group = n_panels ÷ n_groups |
| 153 | + for group_idx in 1:n_groups |
| 154 | + panel_start = (group_idx - 1) * panels_per_group + 1 |
| 155 | + panel_end = group_idx * panels_per_group |
| 156 | + |
| 157 | + expected_cl = sum(sol.cl_array[panel_start:panel_end]) / panels_per_group |
| 158 | + expected_cd = sum(sol.cd_array[panel_start:panel_end]) / panels_per_group |
| 159 | + expected_cm = sum(sol.cm_array[panel_start:panel_end]) / panels_per_group |
| 160 | + |
| 161 | + # Handle NaN for all coefficients |
| 162 | + if isnan(expected_cl) |
| 163 | + @test isnan(sol.cl_group_array[group_idx]) |
| 164 | + else |
| 165 | + @test isapprox(sol.cl_group_array[group_idx], expected_cl, rtol=1e-10) |
| 166 | + end |
| 167 | + if isnan(expected_cd) |
| 168 | + @test isnan(sol.cd_group_array[group_idx]) |
| 169 | + else |
| 170 | + @test isapprox(sol.cd_group_array[group_idx], expected_cd, rtol=1e-10) |
| 171 | + end |
| 172 | + if isnan(expected_cm) |
| 173 | + @test isnan(sol.cm_group_array[group_idx]) |
| 174 | + else |
| 175 | + @test isapprox(sol.cm_group_array[group_idx], expected_cm, rtol=1e-10) |
| 176 | + end |
| 177 | + end |
| 178 | + |
| 179 | + finally |
| 180 | + rm(settings_file; force=true) |
| 181 | + end |
| 182 | + end |
| 183 | + end |
| 184 | +end |
0 commit comments