Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance interactive debugging tools #620

Merged
merged 1 commit into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 28 additions & 7 deletions experiments/AMIP/user_io/debug_plots.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,54 @@ using ClimaCoupler.Interfacer: ComponentModelSimulation, SurfaceModelSimulation

# plotting functions for the coupled simulation
"""
debug(cs::CoupledSimulation, dir = "debug")
debug(cs::CoupledSimulation, dir = "debug", cs_fields_ref = nothing)

Plot the fields of a coupled simulation and save plots to a directory.
"""
function debug(cs::CoupledSimulation, dir = "debug")
function debug(cs::CoupledSimulation, dir = "debug", cs_fields_ref = nothing)
mkpath(dir)
@info "plotting debug in " * dir
for sim in cs.model_sims
debug(sim, dir)
end
debug(cs.fields, dir)
debug(cs.fields, dir, cs_fields_ref)
end

"""
debug(cs_fields::NamedTuple, dir)
debug(cs_fields::NamedTuple, dir, cs_fields_ref = nothing)

Plot useful coupler fields (in `field_names`) and save plots to a directory.

If `cs_fields_ref` is provided (e.g., using a copy of cs.fields from the initialization),
plot the anomalies of the fields with respect to `cs_fields_ref`.
"""
function debug(cs_fields::NamedTuple, dir)
field_names = (:F_turb_energy, :F_turb_moisture, :P_liq, :T_S, :ρ_sfc, :q_sfc)
function debug(cs_fields::NamedTuple, dir, cs_fields_ref = nothing)
field_names =
(:albedo, :F_radiative, :F_turb_energy, :F_turb_moisture, :P_liq, :T_S, :ρ_sfc, :q_sfc, :beta, :z0b_S, :z0m_S)
all_plots = []
for field_name in field_names
field = getproperty(cs_fields, field_name)
push!(all_plots, Plots.plot(field, title = string(field_name) * print_extrema(field)))
end
fig = Plots.plot(all_plots..., size = (1500, 800))
Plots.plot(all_plots..., size = (1500, 800))
Plots.png(joinpath(dir, "debug_coupler"))

# plot anomalies if a reference cs.fields, `cs_fields_ref`, are provided
if !isnothing(cs_fields_ref)
all_plots = []
for field_name in field_names
field = getproperty(cs_fields, field_name)
push!(
all_plots,
Plots.plot(
field .- getproperty(cs_fields_ref, field_name),
title = string(field_name) * print_extrema(field),
),
)
end
Plots.plot(all_plots..., size = (1500, 800))
Plots.png(joinpath(dir, "debug_coupler_amomalies"))
end
end

"""
Expand Down
3 changes: 2 additions & 1 deletion test/debug/debug_amip_plots.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ plot_field_names(sim::SurfaceStub) = (:stub_field,)
@testset "import_atmos_fields!" begin

boundary_space = TestHelper.create_space(FT)
coupler_names = (:F_turb_energy, :F_turb_moisture, :P_liq, :T_S, :ρ_sfc, :q_sfc)
coupler_names =
(:albedo, :F_radiative, :F_turb_energy, :F_turb_moisture, :P_liq, :T_S, :ρ_sfc, :q_sfc, :beta, :z0b_S, :z0m_S)
atmos_names = (:atmos_field,)
surface_names = (:surface_field,)
stub_names = (:stub_field,)
Expand Down
Loading