Skip to content

Commit 8e6a925

Browse files
committed
use sim_mode only in init
1 parent ea8e62f commit 8e6a925

17 files changed

+70
-134
lines changed

experiments/ClimaEarth/run_amip.jl

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,6 @@ if sim_mode <: AMIPMode
280280
ocean_fraction = FT(1) .- ice_fraction .- land_fraction
281281
ocean_sim = PrescribedOceanSimulation(FT, boundary_space, date0, t_start, ocean_fraction, thermo_params, comms_ctx)
282282

283-
mode_specifics = (; type = sim_mode)
284283
Utilities.show_memory_usage()
285284

286285
elseif (sim_mode <: AbstractSlabplanetSimulationMode) && !(sim_mode <: SlabplanetEisenmanMode)
@@ -335,7 +334,6 @@ elseif (sim_mode <: AbstractSlabplanetSimulationMode) && !(sim_mode <: Slabplane
335334
thermo_params = thermo_params,
336335
))
337336

338-
mode_specifics = (; type = sim_mode)
339337
Utilities.show_memory_usage()
340338

341339
elseif sim_mode <: SlabplanetEisenmanMode
@@ -382,7 +380,6 @@ elseif sim_mode <: SlabplanetEisenmanMode
382380
thermo_params = thermo_params,
383381
)
384382

385-
mode_specifics = (; type = sim_mode)
386383
Utilities.show_memory_usage()
387384
end
388385

@@ -487,14 +484,14 @@ end
487484
#= Set up default AMIP diagnostics
488485
Use ClimaDiagnostics for default AMIP diagnostics, which currently include turbulent energy fluxes.
489486
=#
490-
if sim_mode <: AMIPMode && use_coupler_diagnostics
487+
if use_coupler_diagnostics
488+
@info "Using default coupler diagnostics"
491489
include("user_io/amip_diagnostics.jl")
492490
coupler_diags_path = joinpath(dir_paths.output, "coupler")
493491
isdir(coupler_diags_path) || mkpath(coupler_diags_path)
494-
amip_diags_handler =
495-
amip_diagnostics_setup(coupler_fields, coupler_diags_path, dates.date0[1], tspan[1], calendar_dt)
492+
diags_handler = amip_diagnostics_setup(coupler_fields, coupler_diags_path, dates.date0[1], tspan[1], calendar_dt)
496493
else
497-
amip_diags_handler = nothing
494+
diags_handler = nothing
498495
end
499496

500497
#=
@@ -515,12 +512,11 @@ cs = Interfacer.CoupledSimulation{FT}(
515512
[tspan[1], tspan[2]],
516513
Δt_cpl,
517514
model_sims,
518-
mode_specifics,
519515
callbacks,
520516
dir_paths,
521517
turbulent_fluxes,
522518
thermo_params,
523-
amip_diags_handler,
519+
diags_handler,
524520
);
525521
Utilities.show_memory_usage()
526522

@@ -659,8 +655,7 @@ function solve_coupler!(cs)
659655
## compute/output AMIP diagnostics if scheduled for this timestep
660656
## wrap the current CoupledSimulation fields and time in a NamedTuple to match the ClimaDiagnostics interface
661657
cs_nt = (; u = cs.fields, p = nothing, t = t, step = round(t / Δt_cpl))
662-
(cs.mode.type <: AMIPMode && !isnothing(cs.amip_diags_handler)) &&
663-
CD.orchestrate_diagnostics(cs_nt, cs.amip_diags_handler)
658+
!isnothing(cs.diags_handler) && CD.orchestrate_diagnostics(cs_nt, cs.diags_handler)
664659
end
665660
return nothing
666661
end
@@ -737,13 +732,7 @@ The postprocessing includes:
737732
=#
738733

739734
if ClimaComms.iamroot(comms_ctx)
740-
postprocessing_vars = (;
741-
plot_diagnostics,
742-
use_coupler_diagnostics,
743-
output_default_diagnostics,
744-
t_end,
745-
conservation_softfail,
746-
atmos_output_dir,
747-
)
748-
postprocess_sim(cs.mode.type, cs, postprocessing_vars)
735+
postprocessing_vars =
736+
(; plot_diagnostics, use_coupler_diagnostics, output_default_diagnostics, t_end, conservation_softfail)
737+
postprocess_sim(cs, postprocessing_vars)
749738
end

experiments/ClimaEarth/run_cloudless_aquaplanet.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,12 +241,11 @@ cs = Interfacer.CoupledSimulation{FT}(
241241
[tspan[1], tspan[2]],
242242
Δt_cpl,
243243
model_sims,
244-
(;), # mode_specifics
245244
callbacks,
246245
dir_paths,
247246
turbulent_fluxes,
248247
thermo_params,
249-
nothing, # amip_diags_handler
248+
nothing, # diags_handler
250249
);
251250

252251
#=

experiments/ClimaEarth/run_cloudy_aquaplanet.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,12 +265,11 @@ cs = Interfacer.CoupledSimulation{FT}(
265265
[tspan[1], tspan[2]],
266266
Δt_cpl,
267267
model_sims,
268-
(;), # mode_specifics
269268
callbacks,
270269
dir_paths,
271270
turbulent_fluxes,
272271
thermo_params,
273-
nothing, # amip_diags_handler
272+
nothing, # diags_handler
274273
);
275274

276275
#=

experiments/ClimaEarth/run_cloudy_slabplanet.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,12 +301,11 @@ cs = Interfacer.CoupledSimulation{FT}(
301301
[tspan[1], tspan[2]],
302302
Δt_cpl,
303303
model_sims,
304-
(;), # mode_specifics
305304
callbacks,
306305
dir_paths,
307306
turbulent_fluxes,
308307
thermo_params,
309-
nothing, # amip_diags_handler
308+
nothing, # diags_handler
310309
);
311310

312311
#=

experiments/ClimaEarth/run_dry_held_suarez.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,12 +189,11 @@ cs = Interfacer.CoupledSimulation{FT}(
189189
[tspan[1], tspan[2]],
190190
Δt_cpl,
191191
model_sims,
192-
(;), # mode_specifics
193192
callbacks,
194193
dir_paths,
195194
nothing, # turbulent_fluxes
196195
thermo_params,
197-
nothing, # amip_diags_handler
196+
nothing, # diags_handler
198197
);
199198

200199
#=

experiments/ClimaEarth/run_moist_held_suarez.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,12 +239,11 @@ cs = Interfacer.CoupledSimulation{FT}(
239239
[tspan[1], tspan[2]],
240240
Δt_cpl,
241241
model_sims,
242-
(;), # mode_specifics
243242
callbacks,
244243
dir_paths,
245244
turbulent_fluxes,
246245
thermo_params,
247-
nothing, # amip_diags_handler
246+
nothing, # diags_handler
248247
);
249248

250249
#=

experiments/ClimaEarth/user_io/amip_diagnostics.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,6 @@ function amip_diagnostics_setup(fields, output_dir, start_date, t_start, calenda
4848

4949
# Create the diagnostics handler containing the scheduled diagnostics
5050
scheduled_diags = [F_turb_energy_diag_sched]
51-
amip_diags_handler = CD.DiagnosticsHandler(scheduled_diags, fields, nothing, t_start)
52-
return amip_diags_handler
51+
diags_handler = CD.DiagnosticsHandler(scheduled_diags, fields, nothing, t_start)
52+
return diags_handler
5353
end

experiments/ClimaEarth/user_io/arg_parsing.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ function get_coupler_config()
2828
job_id = isnothing(job_id) ? string(split(split(config_file, '/')[end], '.')[1]) : job_id
2929

3030
# Read in config dictionary from file, overriding the defaults in `parsed_args`
31-
config_dict = merge(parsed_args, YAML.load_file(parsed_args["config_file"]))
31+
config_dict = merge(parsed_args, YAML.load_file(config_file))
3232
config_dict["job_id"] = job_id
3333
return config_dict
3434
end

experiments/ClimaEarth/user_io/diagnostics_plots.jl

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -147,31 +147,27 @@ end
147147
make_diagnostics_plots(
148148
output_path::AbstractString,
149149
plot_path::AbstractString;
150-
short_names::Vector{<:AbstractString} = ["mse", "lr", "edt", "ts"],
151-
reduction::String = "average",
152150
output_prefix = "",
153151
)
154152
Create plots for diagnostics. The plots are saved to `plot_path`.
155-
This is the default plotting function for diagnostics and it can be extended
156-
to include additional diagnostics.
153+
This function will plot all variables that have been saved in `output_path`.
157154
The `reduction` keyword argument should be consistent with the reduction used to save the diagnostics.
158155
"""
159-
function make_diagnostics_plots(
160-
output_path::AbstractString,
161-
plot_path::AbstractString;
162-
short_names::Vector{<:AbstractString} = ["mse", "lr", "edt", "ts"],
163-
reduction::String = "average",
164-
output_prefix = "",
165-
)
166-
156+
function make_diagnostics_plots(output_path::AbstractString, plot_path::AbstractString; output_prefix = "")
167157
simdir = CAN.SimDir(output_path)
168-
if !all(v -> v in CAN.available_vars(simdir), short_names)
169-
@warn "Not all variables are available in the output directory $output_path"
170-
return
171-
end
158+
short_names = CAN.available_vars(simdir)
159+
160+
# Return if there are no variables to plot
161+
isempty(short_names) && return
172162

173163
# Create a CAN.OutputVar for each input field
174-
vars = [get(simdir; short_name, reduction) for short_name in short_names]
164+
vars = Array{CAN.OutputVar}(undef, length(short_names))
165+
for (i, short_name) in enumerate(short_names)
166+
# Use "average" if available, otherwise use the first reduction
167+
reductions = CAN.available_reductions(simdir; short_name)
168+
"average" in reductions ? (reduction = "average") : (reduction = first(reductions))
169+
vars[i] = get(simdir; short_name, reduction)
170+
end
175171

176172
# Filter vars into 2D and 3D variable diagnostics vectors
177173
# 3D fields are zonally averaged platted on the lat-z plane

experiments/ClimaEarth/user_io/postprocessing.jl

Lines changed: 32 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,40 @@ include("diagnostics_plots.jl")
44
include("../leaderboard/leaderboard.jl")
55

66
"""
7-
postprocess_sim(::Type{AbstractSlabplanetSimulationMode}, cs, postprocessing_vars)
7+
postprocess_sim(cs, postprocessing_vars)
88
9-
Call `common_postprocessing` to perform common postprocessing tasks that are common to all simulation types.
10-
Then, if conservation checks exist, perform them.
9+
Perform all postprocessing operations. This includes plotting all available
10+
diagnostics, plotting all model states and coupler fields for debugging,
11+
producing the leaderboard if monthly data is available, performing
12+
conservation checks if enabled, and closing all diagnostics file writers.
1113
"""
12-
function postprocess_sim(::Type{<:AbstractSlabplanetSimulationMode}, cs, postprocessing_vars)
13-
(; conservation_softfail,) = postprocessing_vars
14+
function postprocess_sim(cs, postprocessing_vars)
15+
(; use_coupler_diagnostics, output_default_diagnostics, t_end, conservation_softfail) = postprocessing_vars
16+
output_dir = cs.dirs.output
17+
artifact_dir = cs.dirs.artifacts
18+
coupler_output_dir = joinpath(output_dir, "coupler")
19+
atmos_output_dir = joinpath(output_dir, "clima_atmos")
20+
land_output_dir = joinpath(output_dir, "clima_land")
21+
22+
# Plot generic diagnostics if requested
23+
if use_coupler_diagnostics && plot_diagnostics
24+
@info "Plotting diagnostics for coupler, atmos, and land"
25+
make_diagnostics_plots(coupler_output_dir, artifact_dir, output_prefix = "coupler_")
26+
make_diagnostics_plots(atmos_output_dir, artifact_dir, output_prefix = "atmos_")
27+
make_diagnostics_plots(land_output_dir, artifact_dir, output_prefix = "land_")
28+
end
29+
30+
# Plot all model states and coupler fields (useful for debugging)
31+
!CA.is_distributed(cs.comms_ctx) && debug(cs, artifact_dir)
1432

15-
common_postprocessing(cs, postprocessing_vars)
33+
# If we have monthly data, plot the leaderboard
34+
if t_end > 84600 * 31 * 3 && output_default_diagnostics
35+
leaderboard_base_path = artifact_dir
36+
compute_leaderboard(leaderboard_base_path, atmos_output_dir)
37+
compute_pfull_leaderboard(leaderboard_base_path, atmos_output_dir)
38+
end
1639

40+
# Perform conservation checks if they exist
1741
if !isnothing(cs.conservation_checks)
1842
@info "Conservation Check Plots"
1943
plot_global_conservation(
@@ -31,66 +55,7 @@ function postprocess_sim(::Type{<:AbstractSlabplanetSimulationMode}, cs, postpro
3155
figname2 = joinpath(cs.dirs.artifacts, "total_water_log_bucket.png"),
3256
)
3357
end
34-
end
35-
36-
"""
37-
postprocess_sim(::Type{AMIPMode}, cs, postprocessing_vars)
38-
39-
Call `common_postprocessing` to perform postprocessing tasks that are common to all simulation
40-
types, and then conditionally plot AMIP diagnostics
41-
"""
42-
function postprocess_sim(::Type{AMIPMode}, cs, postprocessing_vars)
43-
(; use_coupler_diagnostics, output_default_diagnostics, t_end) = postprocessing_vars
44-
45-
common_postprocessing(cs, postprocessing_vars)
46-
47-
if use_coupler_diagnostics
48-
## plot data that correspond to the model's last save_hdf5 call (i.e., last month)
49-
@info "AMIP plots"
50-
# define variable names and output directories for each diagnostic
51-
amip_short_names_atmos = ["ta", "ua", "hus", "clw", "pr", "ts", "toa_fluxes_net"]
52-
amip_short_names_coupler = ["F_turb_energy"]
53-
output_dir_coupler = cs.dirs.output
54-
55-
# Check if all output variables are available in the specified directories
56-
make_diagnostics_plots(
57-
atmos_output_dir,
58-
cs.dirs.artifacts,
59-
short_names = amip_short_names_atmos,
60-
output_prefix = "atmos_",
61-
)
62-
make_diagnostics_plots(
63-
output_dir_coupler,
64-
cs.dirs.artifacts,
65-
short_names = amip_short_names_coupler,
66-
output_prefix = "coupler_",
67-
)
68-
end
69-
70-
# Check this because we only want monthly data for making plots
71-
if t_end > 84600 * 31 * 3 && output_default_diagnostics
72-
leaderboard_base_path = cs.dirs.artifacts
73-
compute_leaderboard(leaderboard_base_path, atmos_output_dir)
74-
compute_pfull_leaderboard(leaderboard_base_path, atmos_output_dir)
75-
end
76-
77-
# close all AMIP diagnostics file writers
78-
!isnothing(cs.amip_diags_handler) &&
79-
map(diag -> close(diag.output_writer), cs.amip_diags_handler.scheduled_diagnostics)
80-
end
81-
82-
"""
83-
common_postprocessing(cs, postprocessing_vars)
84-
85-
Perform postprocessing common to all simulation types.
86-
"""
87-
function common_postprocessing(cs, postprocessing_vars)
88-
(; plot_diagnostics, atmos_output_dir) = postprocessing_vars
89-
if plot_diagnostics
90-
@info "Plotting diagnostics"
91-
make_diagnostics_plots(atmos_output_dir, cs.dirs.artifacts)
92-
end
9358

94-
# plot all model states and coupler fields (useful for debugging)
95-
!CA.is_distributed(cs.comms_ctx) && debug(cs, cs.dirs.artifacts)
59+
# Close all diagnostics file writers
60+
!isnothing(cs.diags_handler) && map(diag -> close(diag.output_writer), cs.diags_handler.scheduled_diagnostics)
9661
end

0 commit comments

Comments
 (0)