### A Pluto.jl notebook ###
# v1.0.0

using Markdown
using InteractiveUtils

# ╔═╡ cec36d86-0375-4395-a667-54753a99761f
begin
    import Pkg
    Pkg.activate(joinpath(@__DIR__, ".."))
    using CoupledDipole
    using CairoMakie
    using Statistics
end

# ╔═╡ 5b7d8d0c-95d2-47bc-af99-cb02cfef9754
md"""
# Core-shell convergence and cost sweeps

This notebook benchmarks convergence vs runtime for:

- `N_esa` (ESA truncation),
- `N_mie` (exact Mie truncation),
- `N_theta` (angular interpolation grid).

Reference spectra are computed once with a larger setting, and each sweep is
compared by relative RMS error on the extinction spectrum.
"""

# ╔═╡ bf0a247b-4325-4d4e-a703-f29557e72cb7
begin
    wavelengths = collect(400.0:8.0:600.0)
    rho_dye = 0.2
    tensor_type = 1
    position_type = 1
    medium = 1.33
    R0 = 3.0
    d = 0.5
    N_inc = 8
    N_sca = 10

    # "reference" settings (moderate to keep this notebook runnable quickly)
    ref_N_mie = 20
    ref_N_esa = 240
    ref_N_theta = 301
end

# ╔═╡ 4a1ad786-601d-47a9-b460-a9ff93fdf47f
begin
    function run_coreshell(N_mie, N_esa, N_theta)
        t = @elapsed x = simulate_core_shell(
            rho_dye,
            tensor_type,
            position_type,
            wavelengths,
            medium,
            R0,
            d,
            N_inc;
            N_sca=N_sca,
            N_mie=N_mie,
            N_esa=N_esa,
            N_theta=N_theta,
            coupling=true,
            self_reaction=true,
        )
        return x, t
    end

    xref, tref = run_coreshell(ref_N_mie, ref_N_esa, ref_N_theta)
    @assert all(isfinite, xref)
end

# ╔═╡ e5fc7f8c-f8dd-4f6e-b6f8-23ec57fb64fd
begin
    rms_rel(a, b) = sqrt(mean(abs2, (a .- b) ./ max.(abs.(b), 1e-30)))
    ref_ext = xref[:, 1]
end

# ╔═╡ 6c6e4f21-68af-432c-b15d-13f9ef6f30bb
begin
    sweep_esa = [40, 80, 120, 180, 240]
    err_esa = similar(float.(sweep_esa))
    t_esa = similar(float.(sweep_esa))
    for (i, nesa) in enumerate(sweep_esa)
        x, t = run_coreshell(ref_N_mie, nesa, ref_N_theta)
        err_esa[i] = rms_rel(x[:, 1], ref_ext)
        t_esa[i] = t
    end
end

# ╔═╡ 33248f9d-9979-4248-b95a-40e97b036a6a
begin
    sweep_mie = [8, 12, 16, 20]
    err_mie = similar(float.(sweep_mie))
    t_mie = similar(float.(sweep_mie))
    for (i, nmie) in enumerate(sweep_mie)
        x, t = run_coreshell(nmie, ref_N_esa, ref_N_theta)
        err_mie[i] = rms_rel(x[:, 1], ref_ext)
        t_mie[i] = t
    end
end

# ╔═╡ 2dbca5f0-aa1b-4b5f-b41d-0860902ea8d2
begin
    sweep_theta = [121, 181, 241, 301]
    err_theta = similar(float.(sweep_theta))
    t_theta = similar(float.(sweep_theta))
    for (i, ntheta) in enumerate(sweep_theta)
        x, t = run_coreshell(ref_N_mie, ref_N_esa, ntheta)
        err_theta[i] = rms_rel(x[:, 1], ref_ext)
        t_theta[i] = t
    end
end

# ╔═╡ 2d46cb6e-a48a-4be1-a219-f6509f266b2d
let
    fig = Figure(size=(980, 730))

    ax1 = Axis(fig[1, 1], xlabel="N_esa", ylabel="Relative RMS error (extinction)", yscale=log10)
    ax2 = Axis(fig[1, 2], xlabel="N_esa", ylabel="Runtime / s")
    lines!(ax1, sweep_esa, err_esa)
    scatter!(ax1, sweep_esa, err_esa)
    lines!(ax2, sweep_esa, t_esa)
    scatter!(ax2, sweep_esa, t_esa)

    ax3 = Axis(fig[2, 1], xlabel="N_mie", ylabel="Relative RMS error (extinction)", yscale=log10)
    ax4 = Axis(fig[2, 2], xlabel="N_mie", ylabel="Runtime / s")
    lines!(ax3, sweep_mie, err_mie)
    scatter!(ax3, sweep_mie, err_mie)
    lines!(ax4, sweep_mie, t_mie)
    scatter!(ax4, sweep_mie, t_mie)

    ax5 = Axis(fig[3, 1], xlabel="N_theta", ylabel="Relative RMS error (extinction)", yscale=log10)
    ax6 = Axis(fig[3, 2], xlabel="N_theta", ylabel="Runtime / s")
    lines!(ax5, sweep_theta, err_theta)
    scatter!(ax5, sweep_theta, err_theta)
    lines!(ax6, sweep_theta, t_theta)
    scatter!(ax6, sweep_theta, t_theta)

    Label(fig[0, :], "Reference: N_mie=$(ref_N_mie), N_esa=$(ref_N_esa), N_theta=$(ref_N_theta), runtime=$(round(tref, digits=3)) s")
    fig
end

# ╔═╡ 30858f2f-47f4-4cb9-850d-f8130c3f015f
md"""
### Notes

- Use these sweeps to identify a practical "elbow" where error reduction slows.
- Convergence behavior can depend strongly on wavelength range, gap `d`, and medium.
- For stricter studies, increase the reference settings and re-run.
"""

# ╔═╡ Cell order:
# ╠═cec36d86-0375-4395-a667-54753a99761f
# ╟─5b7d8d0c-95d2-47bc-af99-cb02cfef9754
# ╠═bf0a247b-4325-4d4e-a703-f29557e72cb7
# ╠═4a1ad786-601d-47a9-b460-a9ff93fdf47f
# ╠═e5fc7f8c-f8dd-4f6e-b6f8-23ec57fb64fd
# ╠═6c6e4f21-68af-432c-b15d-13f9ef6f30bb
# ╠═33248f9d-9979-4248-b95a-40e97b036a6a
# ╠═2dbca5f0-aa1b-4b5f-b41d-0860902ea8d2
# ╠═2d46cb6e-a48a-4be1-a219-f6509f266b2d
# ╟─30858f2f-47f4-4cb9-850d-f8130c3f015f
