|
| 1 | +using Distributed, Timers, Serialization, SharedArrays |
| 2 | +using Interpolations |
| 3 | +using Xfoil |
| 4 | +using ControlPlots |
| 5 | +using Logging |
| 6 | + |
| 7 | +const SPEED_OF_SOUND = 343 # https://en.wikipedia.org/wiki/Speed_of_sound |
| 8 | +const KINEMATIC_VISCOSITY = 1.460e-5 # https://en.wikipedia.org/wiki/Reynolds_number |
| 9 | + |
| 10 | +@info "Creating polars. This can take several minutes." |
| 11 | +tic() |
| 12 | + |
| 13 | +procs = addprocs() |
| 14 | + |
| 15 | +try |
| 16 | + function normalize!(x, y) |
| 17 | + x_min = minimum(x) |
| 18 | + x_max = maximum(x) |
| 19 | + for i in eachindex(x) |
| 20 | + x[i] = (x[i] - x_min) / (x_max - x_min) |
| 21 | + y[i] = (y[i] - x_min) / (x_max - x_min) |
| 22 | + end |
| 23 | + end |
| 24 | + |
| 25 | + @eval @everywhere using VortexStepMethod, Xfoil, Statistics, SharedArrays |
| 26 | + |
| 27 | + alphas = -deg2rad(5):deg2rad(1.0):deg2rad(20) |
| 28 | + d_trailing_edge_angles = -deg2rad(5):deg2rad(1.0):deg2rad(20) |
| 29 | + |
| 30 | + cl_matrix = SharedArray{Float64}((length(alphas), length(d_trailing_edge_angles))) |
| 31 | + cd_matrix = SharedArray{Float64}((length(alphas), length(d_trailing_edge_angles))) |
| 32 | + cm_matrix = SharedArray{Float64}((length(alphas), length(d_trailing_edge_angles))) |
| 33 | + |
| 34 | + @everywhere begin |
| 35 | + function turn_trailing_edge!(angle, x, y, lower_turn, upper_turn, x_turn) |
| 36 | + turn_distance = upper_turn - lower_turn |
| 37 | + smooth_idx = [] |
| 38 | + rm_idx = [] |
| 39 | + |
| 40 | + sign = angle > 0 ? 1 : -1 |
| 41 | + y_turn = angle > 0 ? upper_turn : lower_turn |
| 42 | + for i in eachindex(x) |
| 43 | + if x_turn - turn_distance < x[i] < x_turn + turn_distance && sign * y[i] > 0 |
| 44 | + append!(smooth_idx, i) |
| 45 | + elseif sign * y[i] < 0 && x_turn > x[i] > x_turn - turn_distance |
| 46 | + append!(rm_idx, i) |
| 47 | + end |
| 48 | + if x[i] > x_turn |
| 49 | + x_rel = x[i] - x_turn |
| 50 | + y_rel = y[i] - y_turn |
| 51 | + x[i] = x_turn + x_rel * cos(angle) + y_rel * sin(angle) |
| 52 | + y[i] = y_turn - x_rel * sin(angle) + y_rel * cos(angle) |
| 53 | + if angle > 0 && x[i] < x_turn - turn_distance/2 && y[i] > lower_turn |
| 54 | + append!(rm_idx, i) |
| 55 | + elseif angle < 0 && x[i] < x_turn - turn_distance/2 && y[i] < upper_turn |
| 56 | + append!(rm_idx, i) |
| 57 | + end |
| 58 | + end |
| 59 | + end |
| 60 | + |
| 61 | + #TODO: lower and upper is slightly off because of smoothing |
| 62 | + lower_i, upper_i = minimum(smooth_idx), maximum(smooth_idx) |
| 63 | + for i in smooth_idx |
| 64 | + window = min(i - lower_i + 1, upper_i - i + 1) |
| 65 | + x[i] = mean(x[i-window:i+window]) |
| 66 | + end |
| 67 | + deleteat!(x, rm_idx) |
| 68 | + deleteat!(y, rm_idx) |
| 69 | + nothing |
| 70 | + end |
| 71 | + |
| 72 | + function solve_alpha!(cls, cds, cms, alphas, alpha_idxs, d_trailing_edge_angle, re, x_, y_, lower, upper, kite_speed, speed_of_sound, x_turn) |
| 73 | + x = deepcopy(x_) |
| 74 | + y = deepcopy(y_) |
| 75 | + turn_trailing_edge!(d_trailing_edge_angle, x, y, lower, upper, x_turn) |
| 76 | + Xfoil.set_coordinates(x, y) |
| 77 | + x, y = Xfoil.pane(npan=140) |
| 78 | + reinit = true |
| 79 | + for (alpha, alpha_idx) in zip(alphas, alpha_idxs) |
| 80 | + converged = false |
| 81 | + cl = 0.0 |
| 82 | + cd = 0.0 |
| 83 | + # Solve for the given angle of attack |
| 84 | + cl, cd, _, cm, converged = Xfoil.solve_alpha(rad2deg(alpha), re; iter=50, reinit=reinit, mach=kite_speed/speed_of_sound, xtrip=(0.05, 0.05)) |
| 85 | + reinit = false |
| 86 | + if converged |
| 87 | + cls[alpha_idx] = cl |
| 88 | + cds[alpha_idx] = cd |
| 89 | + cms[alpha_idx] = cm |
| 90 | + end |
| 91 | + end |
| 92 | + return nothing |
| 93 | + end |
| 94 | + |
| 95 | + function run_solve_alpha(alphas, d_trailing_edge_angle, re, x_, y_, lower, upper, kite_speed, speed_of_sound, x_turn) |
| 96 | + @info "solving alpha with trailing edge angle: $(rad2deg(d_trailing_edge_angle)) degrees" |
| 97 | + cls = Float64[NaN for _ in alphas] |
| 98 | + cds = Float64[NaN for _ in alphas] |
| 99 | + cms = Float64[NaN for _ in alphas] |
| 100 | + neg_idxs = sort(findall(alphas .< 0.0), rev=true) |
| 101 | + neg_alphas = alphas[neg_idxs] |
| 102 | + pos_idxs = sort(findall(alphas .>= 0.0)) |
| 103 | + pos_alphas = alphas[pos_idxs] |
| 104 | + solve_alpha!(cls, cds, cms, neg_alphas, neg_idxs, d_trailing_edge_angle, |
| 105 | + re, x_, y_, lower, upper, kite_speed, speed_of_sound, x_turn) |
| 106 | + solve_alpha!(cls, cds, cms, pos_alphas, pos_idxs, d_trailing_edge_angle, |
| 107 | + re, x_, y_, lower, upper, kite_speed, speed_of_sound, x_turn) |
| 108 | + return cls, cds, cms |
| 109 | + end |
| 110 | + end |
| 111 | + |
| 112 | + function get_lower_upper(x, y) |
| 113 | + lower_trailing_edge = 0.0 |
| 114 | + upper_trailing_edge = 0.0 |
| 115 | + min_lower_distance = Inf |
| 116 | + min_upper_distance = Inf |
| 117 | + for (xi, yi) in zip(x, y) |
| 118 | + if yi < 0 |
| 119 | + lower_distance = abs(xi - x_turn) |
| 120 | + if lower_distance < min_lower_distance |
| 121 | + min_lower_distance = lower_distance |
| 122 | + lower_trailing_edge = yi |
| 123 | + end |
| 124 | + else |
| 125 | + upper_distance = abs(xi - x_turn) |
| 126 | + if upper_distance < min_upper_distance |
| 127 | + min_upper_distance = upper_distance |
| 128 | + upper_trailing_edge = yi |
| 129 | + end |
| 130 | + end |
| 131 | + end |
| 132 | + return lower_trailing_edge, upper_trailing_edge |
| 133 | + end |
| 134 | + |
| 135 | + function replace_nan!(matrix) |
| 136 | + rows, cols = size(matrix) |
| 137 | + distance = max(rows, cols) |
| 138 | + for i in 1:rows |
| 139 | + for j in 1:cols |
| 140 | + if isnan(matrix[i, j]) |
| 141 | + neighbors = [] |
| 142 | + for d in 1:distance |
| 143 | + found = false |
| 144 | + if i-d >= 1 && !isnan(matrix[i-d, j]); |
| 145 | + push!(neighbors, matrix[i-d, j]) |
| 146 | + found = true |
| 147 | + end |
| 148 | + if i+d <= rows && !isnan(matrix[i+d, j]) |
| 149 | + push!(neighbors, matrix[i+d, j]) |
| 150 | + found = true |
| 151 | + end |
| 152 | + if j-d >= 1 && !isnan(matrix[i, j-d]) |
| 153 | + push!(neighbors, matrix[i, j-d]) |
| 154 | + found = true |
| 155 | + end |
| 156 | + if j+d <= cols && !isnan(matrix[i, j+d]) |
| 157 | + push!(neighbors, matrix[i, j+d]) |
| 158 | + found = true |
| 159 | + end |
| 160 | + if found; break; end |
| 161 | + end |
| 162 | + if !isempty(neighbors) |
| 163 | + matrix[i, j] = sum(neighbors) / length(neighbors) |
| 164 | + end |
| 165 | + end |
| 166 | + end |
| 167 | + end |
| 168 | + return nothing |
| 169 | + end |
| 170 | + |
| 171 | + kite_speed = v_wind |
| 172 | + chord_length = area / width |
| 173 | + local reynolds_number = kite_speed * chord_length / KINEMATIC_VISCOSITY # https://en.wikipedia.org/wiki/Reynolds_number |
| 174 | + |
| 175 | + # Read airfoil coordinates from a file. |
| 176 | + local x, y = open(foil_path, "r") do f |
| 177 | + x = Float64[] |
| 178 | + y = Float64[] |
| 179 | + for line in eachline(f) |
| 180 | + entries = split(chomp(line)) |
| 181 | + try |
| 182 | + push!(x, parse(Float64, entries[1])) |
| 183 | + push!(y, parse(Float64, entries[2])) |
| 184 | + catch ArgumentError |
| 185 | + println(entries) |
| 186 | + end |
| 187 | + end |
| 188 | + x, y |
| 189 | + end |
| 190 | + normalize!(x, y) |
| 191 | + Xfoil.set_coordinates(x, y) |
| 192 | + x, y = Xfoil.pane(npan=140) |
| 193 | + lower, upper = get_lower_upper(x, y) |
| 194 | + |
| 195 | + @everywhere begin |
| 196 | + x = $x |
| 197 | + y = $y |
| 198 | + x_turn = $x_turn |
| 199 | + reynolds_number = $reynolds_number |
| 200 | + end |
| 201 | + |
| 202 | + @sync @distributed for j in eachindex(d_trailing_edge_angles) |
| 203 | + cl_matrix[:, j], cd_matrix[:, j], cm_matrix[:, j] = run_solve_alpha(alphas, d_trailing_edge_angles[j], |
| 204 | + reynolds_number, x, y, lower, upper, kite_speed, SPEED_OF_SOUND, x_turn) |
| 205 | + end |
| 206 | + display(cl_matrix) |
| 207 | + |
| 208 | + function plot_values(alphas, d_trailing_edge_angles, matrix, interp, name) |
| 209 | + fig = plt.figure() |
| 210 | + ax = fig.add_subplot(projection="3d") |
| 211 | + |
| 212 | + X_data = collect(d_trailing_edge_angles) .+ zeros(length(alphas))' |
| 213 | + Y_data = collect(alphas)' .+ zeros(length(d_trailing_edge_angles)) |
| 214 | + |
| 215 | + interp_matrix = similar(matrix) |
| 216 | + int_alphas, int_d_trailing_edge_angles = alphas .+ deg2rad(0.5), d_trailing_edge_angles .+ deg2rad(0.5) |
| 217 | + interp_matrix .= [interp(alpha, d_trailing_edge_angle) for alpha in int_alphas, d_trailing_edge_angle in int_d_trailing_edge_angles] |
| 218 | + X_int = collect(int_d_trailing_edge_angles) .+ zeros(length(int_alphas))' |
| 219 | + Y_int = collect(int_alphas)' .+ zeros(length(int_d_trailing_edge_angles)) |
| 220 | + |
| 221 | + ax.plot_wireframe(X_data, Y_data, matrix, edgecolor="royalblue", lw=0.5, rstride=5, cstride=5, alpha=0.6) |
| 222 | + ax.plot_wireframe(X_int, Y_int, interp_matrix, edgecolor="orange", lw=0.5, rstride=5, cstride=5, alpha=0.6) |
| 223 | + plt.xlabel("Alpha") |
| 224 | + plt.ylabel("Flap angle") |
| 225 | + plt.zlabel("$name values") |
| 226 | + plt.title("$name for different d_flap and angle") |
| 227 | + plt.legend() |
| 228 | + plt.grid(true) |
| 229 | + plt.show() |
| 230 | + end |
| 231 | + |
| 232 | + cl_interp = extrapolate(scale(interpolate(cl_matrix, BSpline(Linear())), alphas, d_trailing_edge_angles), NaN) |
| 233 | + cd_interp = extrapolate(scale(interpolate(cd_matrix, BSpline(Linear())), alphas, d_trailing_edge_angles), NaN) |
| 234 | + cm_interp = extrapolate(scale(interpolate(cm_matrix, BSpline(Linear())), alphas, d_trailing_edge_angles), NaN) |
| 235 | + |
| 236 | + plot_values(alphas, d_trailing_edge_angles, cl_matrix, cl_interp, "Cl") |
| 237 | + plot_values(alphas, d_trailing_edge_angles, cd_matrix, cd_interp, "Cd") |
| 238 | + plot_values(alphas, d_trailing_edge_angles, cm_matrix, cm_interp, "Cm") |
| 239 | + |
| 240 | + @info "Relative trailing_edge height: $(upper - lower)" |
| 241 | + @info "Reynolds number for flying speed of $kite_speed is $reynolds_number" |
| 242 | + |
| 243 | + serialize(polar_path, (cl_interp, cd_interp, cm_interp)) |
| 244 | + |
| 245 | +catch e |
| 246 | + @info "Removing processes" |
| 247 | + rmprocs(procs) |
| 248 | + throw(e) |
| 249 | +finally |
| 250 | + @info "Removing processes" |
| 251 | + rmprocs(procs) |
| 252 | +end |
| 253 | + |
| 254 | +toc() |
0 commit comments