# ==============================================================================
# 第 2 讲课堂作业：扑翼如何产生推力（直接复用第 1 讲 FoilLab.jl！）
# 用法（在 REPL 中交互调参）：
#   julia> include("flapping_lab.jl")
#   julia> try_flapping(St=0.30, h0=0.5, θ0_deg=20.0, ϕ_deg=90.0)
#   julia> sweep_st()   # 扫描不同 St，观察“阻力 -> 推力”的转变！
# ==============================================================================

const _FOILLAB = isfile(joinpath(@__DIR__, "FoilLab.jl")) ?
    joinpath(@__DIR__, "FoilLab.jl") : joinpath(@__DIR__, "..", "FoilLab.jl")
isfile(_FOILLAB) || error("找不到 FoilLab.jl！请将 FoilLab.jl 与本脚本放在同一个文件夹中。")
isdefined(Main, :FoilLab) || include(_FOILLAB)
using .FoilLab

# ---------- 【学生调参函数】切换运动模式为 FlappingMotion ----------
"""
    try_flapping(; St=0.30, h0=0.5, θ0_deg=20.0, ϕ_deg=90.0,
                   m=0.00, p=0.40, t=0.12, L=32)

在 Re=1000 下测试沉浮 + 俯仰扑翼的推力系数 Ct（=-Cd）和沉浮推进效率 η：
- `St`：斯特劳哈尔数（经典自然界游动/飞行高效区间 0.2 ~ 0.4）
- `h0`：沉浮幅值 / 弦长（默认 0.5，即峰峰值 1 倍弦长）
- `θ0_deg`：俯仰幅值（度，试对比 0° 纯沉浮 vs 15°~30° 耦合俯仰）
- `ϕ_deg`：俯仰超前沉浮的相位差（度，试对比 90° vs 0° vs -90°！）
- `m, p, t`：复用第 1 讲翼型参数（默认对称 NACA 0012，也可换用第 1 讲的弯翼观察升推耦合）
"""
function try_flapping(; St=0.30, h0=0.5, θ0_deg=20.0, ϕ_deg=90.0,
                        m=0.00, p=0.40, t=0.12, Re=1000.0, L=32,
                        t_end=nothing, t_avg=nothing,
                        viz=:live, save_prefix="lec02_flap")
    foil   = NACA4(m, p, t)
    motion = FlappingMotion(St=St, h0=h0, θ0_deg=θ0_deg, ϕ_deg=ϕ_deg)
    return run_sim(foil; motion=motion, Re=Re, L=L,
                   t_end=t_end, t_avg=t_avg,
                   viz=viz, save_prefix=save_prefix)
end

# ---------- 【课堂实验】扫描斯特劳哈尔数 St，找到“零阻力/净推力”临界点 ----------
function sweep_st(St_list=[0.10, 0.20, 0.30, 0.40]; θ0_deg=20.0, ϕ_deg=90.0, L=24)
    println("St\tCt(推力)\tCl(升力)\tCp(功率)\tη(效率)")
    for St in St_list
        r = run_sim(NACA4(0.0, 0.4, 0.12);
                    motion=FlappingMotion(St=St, h0=0.5, θ0_deg=θ0_deg, ϕ_deg=ϕ_deg),
                    Re=1000.0, L=L, viz=:none, verbose=false)
        println("$(St)\t$(round(r.Ct, digits=3))\t\t$(round(r.Cl, digits=3))\t\t$(round(r.Cp_heave, digits=3))\t\t$(round(r.η_heave, digits=3))")
    end
end

if abspath(PROGRAM_FILE) == @__FILE__
    sweep_st()
    println("\n--- 展示典型推力工况 (St=0.35) 反卡门涡街 ---")
    try_flapping(St=0.35, viz=:live, save_prefix="flap_St035")
end
