Skip to content

Commit

Permalink
clean
Browse files Browse the repository at this point in the history
  • Loading branch information
LenkaNovak committed Oct 2, 2023
1 parent 01d4128 commit c524c17
Showing 1 changed file with 56 additions and 32 deletions.
88 changes: 56 additions & 32 deletions experiments/AMIP/modular/user_io/debug_plots.jl
Original file line number Diff line number Diff line change
@@ -1,34 +1,72 @@
using Plots
using ClimaCorePlots
using Printf
using ClimaCoupler.Interfacer: ComponentModelSimulation, SurfaceModelSimulation

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

"""
debug(cs_fields::NamedTuple, dir)
Plot useful coupler fields (in `field_names`) and save plots to a directory.
"""
function debug(cs_fields::NamedTuple, dir)
field_names = (:F_turb_energy, :F_turb_moisture, :P_liq, :T_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)) )
push!(all_plots, Plots.plot(field, title = string(field_name) * print_extrema(field)))
end
fig = Plots.plot(all_plots..., size = (1500, 800))
Plots.png(joinpath(dir, "debug_coupler"))
end

# plotting functions for each model
function debug(sim, dir)
"""
debug(sim::ComponentModelSimulation, dir)
Plot the fields of a component model simulation and save plots to a directory.
"""
function debug(sim::ComponentModelSimulation, dir)

field_names = plot_field_names(sim)

all_plots = []
for field_name in field_names
field = get_field(sim, Val(field_name))
push!(all_plots, Plots.plot(field, title = string(field_name)* print_extrema(field)) )
push!(all_plots, Plots.plot(field, title = string(field_name) * print_extrema(field)))
end
fig = Plots.plot(all_plots..., size = (1500, 800))
Plots.png(joinpath(dir, "debug_$(name(sim))"))

end

"""
print_extrema(field::ClimaCore.Fields.Field)
Return the minimum and maximum values of a field as a string.
"""
function print_extrema(field::ClimaCore.Fields.Field)
ext_vals = extrema(field)
min = @sprintf("%.2E", ext_vals[1])
max = @sprintf("%.2E", ext_vals[2])
return " [$min, $max]"
end

# below are additional fields specific to this experiment (ourside of the required coupler fields) that we are interested in plotting for debugging purposes

# additional ClimaAtmos model debug fields
function get_field(sim::ClimaAtmosSimulation, ::Val{:w})
w_c = ones(boundary_space)
Expand All @@ -37,37 +75,23 @@ function get_field(sim::ClimaAtmosSimulation, ::Val{:w})
end
get_field(sim::ClimaAtmosSimulation, ::Val{:ρq_tot}) = sim.integrator.u.c.ρq_tot
get_field(sim::ClimaAtmosSimulation, ::Val{:ρe_tot}) = sim.integrator.u.c.ρe_tot
get_field(sim::ClimaAtmosSimulation, ::Val{:col_integrated_rain}) = sim.integrator.p.col_integrated_rain
get_field(sim::ClimaAtmosSimulation, ::Val{:dif_flux_energy_bc}) = Geometry.WVector.(sim.integrator.p.sfc_conditions.ρ_flux_h_tot).components.data.:1
get_field(sim::ClimaAtmosSimulation, ::Val{:dif_flux_ρq_tot_bc}) = Geometry.WVector.(sim.integrator.p.sfc_conditions.ρ_flux_q_tot).components.data.:1
get_field(sim::ClimaAtmosSimulation, ::Val{:radiation_flux}) = ones(boundary_space)

# additional BucketSimulation debug fields
get_field(sim::BucketSimulation, ::Val{:σS}) = sim.integrator.u.bucket.σS
get_field(sim::BucketSimulation, ::Val{:Ws}) = sim.integrator.u.bucket.Ws
get_field(sim::BucketSimulation, ::Val{:W}) = sim.integrator.u.bucket.W
get_field(sim::BucketSimulation, ::Val{:T}) = sim.integrator.u.bucket.T
get_field(sim::BucketSimulation, ::Val{:P_snow}) = sim.integrator.p.bucket.P_snow
get_field(sim::BucketSimulation, ::Val{:P_liq}) = sim.integrator.p.bucket.P_liq

# chosen plot fields
# currently selected plot fields
plot_field_names(sim::SurfaceModelSimulation) = (:area_fraction, :surface_temperature, :surface_humidity, :air_density)
plot_field_names(sim::BucketSimulation) = (:area_fraction, :surface_temperature, :surface_humidity, :air_density, :σS, :Ws, :W, :T, :P_snow, :P_liq)
plot_field_names(sim::ClimaAtmosSimulation) = (:w, :ρq_tot, :ρe_tot, :col_integrated_rain, :dif_flux_energy_bc, :dif_flux_ρq_tot_bc, :radiation_flux)

function debug(cs::CoupledSimulation, dir = "debug")
mkpath(dir)
@info "plotting debug in " * dir
for sim in cs.model_sims
debug(sim, dir)
end
debug(cs.fields, dir)
end

using Printf
function print_extrema(field::ClimaCore.Fields.Field)
ext_vals = extrema(field)
min = @sprintf("%.2E", ext_vals[1])
max = @sprintf("%.2E", ext_vals[2])
return " [$min, $max]"
end
plot_field_names(sim::BucketSimulation) =
(:area_fraction, :surface_temperature, :surface_humidity, :air_density, :σS, :Ws, :W)
plot_field_names(sim::ClimaAtmosSimulation) = (
:w,
:ρq_tot,
:ρe_tot,
:liquid_precipitation,
:snow_precipitation,
:turbulent_energy_flux,
:turbulent_moisture_flux,
:radiative_energy_flux,
)

0 comments on commit c524c17

Please sign in to comment.