Skip to content

Commit ae3724e

Browse files
committed
Add csv functions
1 parent d25f9b2 commit ae3724e

File tree

2 files changed

+86
-25
lines changed

2 files changed

+86
-25
lines changed

src/polars.jl

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,4 +165,77 @@ function create_polars(; dat_path, polar_path, wind_vel, area, width, crease_fra
165165
serialize(polar_path, (alpha_range, delta_range, cl_matrix, cd_matrix, cm_matrix))
166166

167167
toc()
168-
end
168+
end
169+
170+
171+
"""
172+
write_aero_matrix(filepath::String, matrix::Matrix{Float64},
173+
alpha_range::Vector{Float64}, delta_range::Vector{Float64};
174+
label::String="C_l")
175+
176+
Write an aerodynamic coefficient matrix to CSV with angle labels.
177+
The first row contains flap deflection angles, first column contains angles of attack.
178+
179+
# Arguments
180+
- `filepath`: Path to output CSV file
181+
- `matrix`: Matrix of aerodynamic coefficients
182+
- `alpha_range`: Vector of angle of attack values in radians
183+
- `delta_range`: Vector of flap deflection angles in radians
184+
- `label`: Coefficient label for the header (default: "C_l")
185+
"""
186+
function write_aero_matrix(filepath::String, matrix::Matrix{Float64},
187+
alpha_range::Vector{Float64}, delta_range::Vector{Float64};
188+
label::String="C_l")
189+
open(filepath, "w") do io
190+
# Write header with delta values
191+
println(io, "$label/delta," * join(["δ=$(round(rad2deg(δ), digits=1))°" for δ in delta_range], ","))
192+
193+
# Write data rows with alpha values and coefficients
194+
for i in eachindex(alpha_range)
195+
row = "α=$(round(rad2deg(alpha_range[i]), digits=1))°," * join(matrix[i,:], ",")
196+
println(io, row)
197+
end
198+
end
199+
end
200+
201+
"""
202+
read_aero_matrix(filepath::String) -> (Matrix{Float64}, Vector{Float64}, Vector{Float64})
203+
204+
Read an aerodynamic coefficient matrix from CSV with angle labels.
205+
Returns the coefficient matrix and corresponding angle ranges.
206+
207+
# Returns
208+
- `matrix`: Matrix of aerodynamic coefficients
209+
- `alpha_range`: Vector of angle of attack values in radians
210+
- `delta_range`: Vector of flap deflection angles in radians
211+
"""
212+
function read_aero_matrix(filepath::String)
213+
lines = readlines(filepath)
214+
215+
# Parse header to get delta values
216+
header = split(lines[1], ',')
217+
delta_values = map(header[2:end]) do δ_str
218+
# Extract number between "δ=" and "°"
219+
m = match(r"δ=(-?\d+\.?\d*)°", δ_str)
220+
deg2rad(parse(Float64, m.captures[1]))
221+
end
222+
223+
# Initialize matrix
224+
n_rows = length(lines) - 1 # Subtract header
225+
n_cols = length(delta_values)
226+
matrix = zeros(n_rows, n_cols)
227+
alpha_values = zeros(n_rows)
228+
229+
# Parse data rows
230+
for (i, line) in enumerate(lines[2:end])
231+
entries = split(line, ',')
232+
# Extract alpha value
233+
m = match(r"α=(-?\d+\.?\d*)°", entries[1])
234+
alpha_values[i] = deg2rad(parse(Float64, m.captures[1]))
235+
# Parse coefficient values
236+
matrix[i,:] .= parse.(Float64, entries[2:end])
237+
end
238+
239+
return matrix, alpha_values, delta_values
240+
end
241+

src/ram_geometry.jl

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -473,38 +473,26 @@ function RamAirWing(
473473
# Load or create polars
474474
(!endswith(dat_path, ".dat")) && (dat_path *= ".dat")
475475
(!isfile(dat_path)) && error("DAT file not found: $dat_path")
476-
polar_path = dat_path[1:end-4] * "_polar.bin"
476+
polar_path = dat_path[1:end-4] * "_polar.csv"
477477

478478
(!endswith(obj_path, ".obj")) && (obj_path *= ".obj")
479479
(!isfile(obj_path)) && error("OBJ file not found: $obj_path")
480-
info_path = obj_path[1:end-4] * "_info.bin"
481480

482-
if !ispath(info_path)
483-
! prn || @info "Reading $obj_path"
484-
vertices, faces = read_faces(obj_path)
485-
T_cad_body = center_to_com!(vertices, faces)
486-
inertia_tensor = calculate_inertia_tensor(vertices, faces, mass, zeros(3))
481+
! prn || @info "Reading $obj_path"
482+
vertices, faces = read_faces(obj_path)
483+
T_cad_body = center_to_com!(vertices, faces)
484+
inertia_tensor = calculate_inertia_tensor(vertices, faces, mass, zeros(3))
487485

488-
if align_to_principal
489-
inertia_tensor, R_cad_body = calc_inertia_y_rotation(inertia_tensor)
490-
else
491-
R_cad_body = I(3)
492-
end
493-
circle_center_z, radius, gamma_tip = find_circle_center_and_radius(vertices)
494-
le_interp, te_interp, area_interp = create_interpolations(vertices, circle_center_z, radius, gamma_tip, R_cad_body; interp_steps)
495-
496-
! prn || @info "Writing $info_path"
497-
serialize(info_path, (inertia_tensor, T_cad_body, R_cad_body, radius, gamma_tip,
498-
le_interp, te_interp, area_interp))
486+
if align_to_principal
487+
inertia_tensor, R_cad_body = calc_inertia_y_rotation(inertia_tensor)
488+
else
489+
R_cad_body = I(3)
499490
end
491+
circle_center_z, radius, gamma_tip = find_circle_center_and_radius(vertices)
492+
le_interp, te_interp, area_interp = create_interpolations(vertices, circle_center_z, radius, gamma_tip, R_cad_body; interp_steps)
500493

501-
! prn || @info "Loading kite info from $info_path and polars from $polar_path"
494+
! prn || @info "Loading 2d polars from $polar_path"
502495
try
503-
(inertia_tensor::Matrix, T_cad_body::Vector, R_cad_body::Matrix,
504-
radius::Real, gamma_tip::Real, le_interp, te_interp, area_interp) = deserialize(info_path)
505-
506-
((R_cad_body == I(3)) == align_to_principal) && throw(ArgumentError("Delete $info_path and try again."))
507-
508496
if !ispath(polar_path)
509497
width = 2gamma_tip * radius
510498
area = area_interp(gamma_tip)

0 commit comments

Comments
 (0)