begin
import Pkg
Pkg.activate(joinpath(@__DIR__, ".."))
using CoupledDipole
using CairoMakie
using Statistics
end
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.
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
301
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
begin
rms_rel(a, b) = sqrt(mean(abs2, (a .- b) ./ max.(abs.(b), 1e-30)))
ref_ext = xref[:, 1]
end
26-element Vector{Float64}:
0.8437261258102499
0.34314347415013347
0.1846647674586155
0.11564111874360208
0.07952965314623918
0.058271588497692595
0.044686625650143856
⋮
0.006695497508589973
0.00618623306474778
0.00573668678041939
0.005337667304587046
0.004981695614602175
0.004662638458752621
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
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
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
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
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.