-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
452: Add AMIP debug plots r=LenkaNovak a=LenkaNovak Co-authored-by: LenkaNovak <lenka@caltech.edu>
- Loading branch information
Showing
5 changed files
with
173 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
using Plots | ||
using ClimaCorePlots | ||
using Printf | ||
using ClimaCoupler.Interfacer: ComponentModelSimulation, SurfaceModelSimulation | ||
|
||
# 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, :ρ_sfc, :q_sfc) | ||
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.png(joinpath(dir, "debug_coupler")) | ||
end | ||
|
||
""" | ||
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))) | ||
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(sim.domain.face_space.horizontal_space) | ||
parent(w_c) .= parent(Fields.level(Geometry.WVector.(sim.integrator.u.f.u₃), 5 .+ half)) | ||
return w_c | ||
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 | ||
|
||
# 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 | ||
|
||
# currently selected plot fields | ||
plot_field_names(sim::SurfaceModelSimulation) = (:area_fraction, :surface_temperature, :surface_humidity) | ||
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
# testing functions used to produce user-defined debugging plots for AMIP experiments | ||
|
||
using Test | ||
using ClimaCore | ||
using ClimaCoupler: TestHelper | ||
import ClimaCoupler.Interfacer: | ||
update_field!, AtmosModelSimulation, SurfaceModelSimulation, SurfaceStub, get_field, update_field!, name | ||
using ClimaCoupler.Utilities: CoupledSimulation, CoupledSimulation | ||
|
||
FT = Float64 | ||
|
||
struct ClimaAtmosSimulation{C} <: AtmosModelSimulation | ||
cache::C | ||
end | ||
name(sim::ClimaAtmosSimulation) = "ClimaAtmosSimulation" | ||
get_field(sim::AtmosModelSimulation, ::Val{:atmos_field}) = sim.cache.atmos_field | ||
|
||
struct BucketSimulation{C} <: SurfaceModelSimulation | ||
cache::C | ||
end | ||
name(sim::BucketSimulation) = "BucketSimulation" | ||
|
||
include("../../experiments/AMIP/modular/user_io/debug_plots.jl") | ||
|
||
get_field(sim::BucketSimulation, ::Val{:surface_field}) = sim.cache.surface_field | ||
get_field(sim::SurfaceStub, ::Val{:stub_field}) = sim.cache.stub_field | ||
|
||
plot_field_names(sim::ClimaAtmosSimulation) = (:atmos_field,) | ||
plot_field_names(sim::BucketSimulation) = (:surface_field,) | ||
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) | ||
atmos_names = (:atmos_field,) | ||
surface_names = (:surface_field,) | ||
stub_names = (:stub_field,) | ||
|
||
atmos_fields = NamedTuple{atmos_names}(ntuple(i -> ClimaCore.Fields.ones(boundary_space), length(atmos_names))) | ||
surface_fields = | ||
NamedTuple{surface_names}(ntuple(i -> ClimaCore.Fields.ones(boundary_space), length(surface_names))) | ||
stub_fields = NamedTuple{stub_names}(ntuple(i -> ClimaCore.Fields.ones(boundary_space), length(stub_names))) | ||
coupler_fields = | ||
NamedTuple{coupler_names}(ntuple(i -> ClimaCore.Fields.zeros(boundary_space), length(coupler_names))) | ||
|
||
model_sims = (; | ||
atmos_sim = ClimaAtmosSimulation(atmos_fields), | ||
surface_sim = BucketSimulation(surface_fields), | ||
ice_sim = SurfaceStub(stub_fields), | ||
) | ||
cs = CoupledSimulation{FT}( | ||
nothing, # comms_ctx | ||
nothing, # dates | ||
nothing, # boundary_space | ||
coupler_fields, # fields | ||
nothing, # parsed_args | ||
nothing, # conservation_checks | ||
(Int(0), Int(1)), # tspan | ||
Int(200), # t | ||
Int(200), # Δt_cpl | ||
(;), # surface_masks | ||
model_sims, # model_sims | ||
(;), # mode | ||
(), # diagnostics | ||
) | ||
|
||
output_plots = "test_debug" | ||
debug(cs, output_plots) | ||
@test isfile("test_debug/debug_ClimaAtmosSimulation.png") | ||
@test isfile("test_debug/debug_BucketSimulation.png") | ||
@test isfile("test_debug/debug_SurfaceStub.png") | ||
@test isfile("test_debug/debug_coupler.png") | ||
|
||
# remove output | ||
rm(output_plots; recursive = true) | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters