Skip to content

Commit

Permalink
Replace mode_name with mode_type in run_amip
Browse files Browse the repository at this point in the history
This commit adds a struct for each different mode_name.
A struct is returned by get_cupler_args as mode_type.
This is used everywhere mode_name was. The structs do have hierarchy,
but the Abstract types should be given better names.
  • Loading branch information
imreddyTeja committed Dec 12, 2024
1 parent 5318c3b commit 22fe7b7
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 21 deletions.
37 changes: 17 additions & 20 deletions experiments/ClimaEarth/run_amip.jl
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,6 @@ include("components/ocean/slab_ocean.jl")
include("components/ocean/prescr_seaice.jl")
include("components/ocean/eisenman_seaice.jl")

## helpers for user-specified IO
include("user_io/debug_plots.jl")

#=
### Configuration Dictionaries
Each simulation mode has its own configuration dictionary. The `config_dict` of each simulation is a merge of the default configuration
Expand All @@ -95,7 +92,7 @@ add_extra_diagnostics!(config_dict)

(;
job_id,
mode_name,
mode_type,
random_seed,
FT,
comms_ctx,
Expand Down Expand Up @@ -238,8 +235,8 @@ In this section of the code, we initialize all component models and read in the
The specific models and data that are set up depend on which mode we're running.
=#

@info(mode_name)
if mode_name == "amip"
@info(mode_type)
if mode_type isa AMIP_mode
@info("AMIP boundary conditions - do not expect energy conservation")

## land model
Expand Down Expand Up @@ -327,18 +324,18 @@ if mode_name == "amip"
CO2_field = Interfacer.update_field!(atmos_sim, Val(:co2), CO2_init)

mode_specifics = (;
name = mode_name,
type = mode_type,
SST_timevaryinginput = SST_timevaryinginput,
SIC_timevaryinginput = SIC_timevaryinginput,
CO2_timevaryinginput = CO2_timevaryinginput,
)
Utilities.show_memory_usage()

elseif mode_name in ("slabplanet", "slabplanet_aqua", "slabplanet_terra")
elseif mode_type isa AbstractSlabPlanetModeSubType1


land_area_fraction = mode_name == "slabplanet_aqua" ? land_area_fraction .* 0 : land_area_fraction
land_area_fraction = mode_name == "slabplanet_terra" ? land_area_fraction .* 0 .+ 1 : land_area_fraction
land_area_fraction = mode_type isa slabplanet_aqua_mode ? land_area_fraction .* 0 : land_area_fraction
land_area_fraction = mode_type isa slabplanet_terra_mode ? land_area_fraction .* 0 .+ 1 : land_area_fraction

## land model
land_sim = bucket_init(
Expand Down Expand Up @@ -385,10 +382,10 @@ elseif mode_name in ("slabplanet", "slabplanet_aqua", "slabplanet_terra")
thermo_params = thermo_params,
))

mode_specifics = (; name = mode_name, SST_timevaryinginput = nothing, SIC_timevaryinginput = nothing)
mode_specifics = (; type = mode_type, SST_timevaryinginput = nothing, SIC_timevaryinginput = nothing)
Utilities.show_memory_usage()

elseif mode_name == "slabplanet_eisenman"
elseif mode_type isa slabplanet_eisenman_mode

## land model
land_sim = bucket_init(
Expand Down Expand Up @@ -431,7 +428,7 @@ elseif mode_name == "slabplanet_eisenman"
thermo_params = thermo_params,
)

mode_specifics = (; name = mode_name, SST_timevaryinginput = nothing, SIC_timevaryinginput = nothing)
mode_specifics = (; type = mode_type, SST_timevaryinginput = nothing, SIC_timevaryinginput = nothing)
Utilities.show_memory_usage()
end

Expand Down Expand Up @@ -486,7 +483,7 @@ saved in a global `ConservationChecks` struct, `conservation_checks`, which is t
conservation_checks = nothing
if energy_check
@assert(
mode_name[1:10] == "slabplanet" && !CA.is_distributed(ClimaComms.context(boundary_space)),
mode_type isa AbstractSlabPlanetModeType && !CA.is_distributed(ClimaComms.context(boundary_space)),
"Only non-distributed slabplanet allowable for energy_check"
)
conservation_checks = (;
Expand Down Expand Up @@ -528,7 +525,7 @@ albedo_cb = TimeManager.HourlyCallback(
dt = dt_water_albedo,
func = FluxCalculator.water_albedo_from_atmosphere!,
ref_date = [dates.date[1]],
active = mode_name == "amip",
active = mode_type isa AMIP_mode,
)
callbacks =
(; checkpoint = checkpoint_cb, update_firstdayofmonth! = update_firstdayofmonth!_cb, water_albedo = albedo_cb)
Expand All @@ -550,7 +547,7 @@ end
#= Set up default AMIP diagnostics
Use ClimaDiagnostics for default AMIP diagnostics, which currently include turbulent energy fluxes.
=#
if mode_name == "amip" && use_coupler_diagnostics
if mode_type isa AMIP_mode && use_coupler_diagnostics
include("user_io/amip_diagnostics.jl")
amip_diags_handler = amip_diagnostics_setup(coupler_fields, dir_paths.output, dates.date0[1], tspan[1], calendar_dt)
else
Expand Down Expand Up @@ -676,7 +673,7 @@ function solve_coupler!(cs)
## print date on the first of month
cs.dates.date[1] >= cs.dates.date1[1] && @info(cs.dates.date[1])

if cs.mode.name == "amip"
if cs.mode.type isa AMIP_mode

evaluate!(Interfacer.get_field(ocean_sim, Val(:surface_temperature)), cs.mode.SST_timevaryinginput, t)
evaluate!(Interfacer.get_field(ice_sim, Val(:area_fraction)), cs.mode.SIC_timevaryinginput, t)
Expand Down Expand Up @@ -737,7 +734,7 @@ function solve_coupler!(cs)
## compute/output AMIP diagnostics if scheduled for this timestep
## wrap the current CoupledSimulation fields and time in a NamedTuple to match the ClimaDiagnostics interface
cs_nt = (; u = cs.fields, p = nothing, t = t, step = round(t / Δt_cpl))
(cs.mode.name == "amip" && !isnothing(cs.amip_diags_handler)) &&
(cs.mode.type isa AMIP_mode && !isnothing(cs.amip_diags_handler)) &&
CD.orchestrate_diagnostics(cs_nt, cs.amip_diags_handler)
end
return nothing
Expand Down Expand Up @@ -822,7 +819,7 @@ The postprocessing includes:
if ClimaComms.iamroot(comms_ctx)

## energy check plots
if !isnothing(cs.conservation_checks) && cs.mode.name[1:10] == "slabplanet"
if !isnothing(cs.conservation_checks) && cs.mode.type isa AbstractSlabPlanetModeType
@info "Conservation Check Plots"
plot_global_conservation(
cs.conservation_checks.energy,
Expand All @@ -841,7 +838,7 @@ if ClimaComms.iamroot(comms_ctx)
end

## plotting AMIP results
if cs.mode.name == "amip"
if cs.mode.type isa AMIP_mode
if use_coupler_diagnostics
## plot data that correspond to the model's last save_hdf5 call (i.e., last month)
@info "AMIP plots"
Expand Down
19 changes: 18 additions & 1 deletion experiments/ClimaEarth/user_io/arg_parsing.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
import YAML

abstract type AbstractModeType end
abstract type AbstractSlabPlanetModeType <: AbstractModeType end
abstract type AbstractSlabPlanetModeSubType1 <: AbstractSlabPlanetModeType end
struct AMIP_mode <: AbstractModeType end
struct slabplanet_mode <: AbstractSlabPlanetModeSubType1 end
struct slabplanet_aqua_mode <: AbstractSlabPlanetModeSubType1 end
struct slabplanet_terra_mode <: AbstractSlabPlanetModeSubType1 end
struct slabplanet_eisenman_mode <: AbstractSlabPlanetModeType end
mode_name_dict = Dict(
"amip" => AMIP_mode,
"slabplanet" => slabplanet_mode,
"slabplanet_aqua" => slabplanet_aqua_mode,
"slabplanet_terra" => slabplanet_terra_mode,
"slabplanet_eisenman" => slabplanet_eisenman_mode,
)

"""
get_coupler_config()
Expand Down Expand Up @@ -42,6 +58,7 @@ function get_coupler_args(config_dict::Dict)
config_dict["print_config_dict"] && @info(config_dict)
job_id = config_dict["job_id"]
mode_name = config_dict["mode_name"]
mode_type = mode_name_dict[mode_name]()

# Computational simulation setup information
random_seed = config_dict["unique_seed"] ? time_ns() : 1234
Expand Down Expand Up @@ -90,7 +107,7 @@ function get_coupler_args(config_dict::Dict)

return (;
job_id,
mode_name,
mode_type,
random_seed,
FT,
comms_ctx,
Expand Down

0 comments on commit 22fe7b7

Please sign in to comment.