From 07677c12f99cff377df51028aa38b3085ac1c24b Mon Sep 17 00:00:00 2001 From: imreddyTeja Date: Tue, 17 Dec 2024 15:25:21 -0800 Subject: [PATCH] Change simulation mode structs to abstract types This is more in line with the ComponentModelSimulation types in Interfacer.jl --- NEWS.md | 10 +++++----- experiments/ClimaEarth/run_amip.jl | 20 +++++++++---------- experiments/ClimaEarth/user_io/arg_parsing.jl | 2 +- .../ClimaEarth/user_io/postprocessing.jl | 8 ++++---- src/Interfacer.jl | 20 +++++++++---------- 5 files changed, 30 insertions(+), 30 deletions(-) diff --git a/NEWS.md b/NEWS.md index 52d3c8a74b..affd8ae78c 100644 --- a/NEWS.md +++ b/NEWS.md @@ -29,9 +29,9 @@ pressure levels are being created. ### Code cleanup -#### Structs representing simulation modes & postprocessing changes - PR [#1120](https://github.com/CliMA/ClimaCoupler.jl/pull/1120) +#### Abstract types representing simulation modes & postprocessing changes - PR [#1120](https://github.com/CliMA/ClimaCoupler.jl/pull/1120) -The available simulation modes are now represented by the following structs: +The available simulation modes are now represented by the following abstract types: - `ClimaCoupler.Interfacer.AMIPMode` - `ClimaCoupler.Interfacer.SlabplanetMode` @@ -39,16 +39,16 @@ The available simulation modes are now represented by the following structs: - `ClimaCoupler.Interfacer.SlabplanetTerraMode` - `ClimaCoupler.Interfacer.SlabplanetEisenmanMode` -All of the above structs are subtypes of the abstract +All of the above types are subtypes of the abstract `ClimaCoupler.Interfacer.AbstractSlabplanetSimulationMode`, and all of them except `ClimaCoupler.Interfacer.AMIPMode` are subtypes of `ClimaCoupler.Interfacer.AbstractSlabplanetSimulationMode`. -These structs are used in `experiments/ClimaEarth/run_amip.jl` instead of representing the +These types are used in `experiments/ClimaEarth/run_amip.jl` instead of representing the simulation mode as a string. The postprocessing in `experiments/ClimaEarth/run_amip.jl` is now moved into functions in the new `experiments/ClimaEarth/user_io/postprocessing.jl`. When the simulation is complete, -`postprocess_sim` is called using the struct representing the simulation mode for dispatch. +`postprocess_sim` is called using the type representing the simulation mode for dispatch. `postprocess_sim` has one method for all slabplanet simulation modes and another for the AMIP simulation mode. All postprocessing common to all simulation modes is done in the `common_postprocessing` function, which is called by both `postprocess_sim` methods. diff --git a/experiments/ClimaEarth/run_amip.jl b/experiments/ClimaEarth/run_amip.jl index 454442f43d..c609d9de49 100644 --- a/experiments/ClimaEarth/run_amip.jl +++ b/experiments/ClimaEarth/run_amip.jl @@ -249,7 +249,7 @@ The specific models and data that are set up depend on which mode we're running. =# @info(sim_mode) -if sim_mode isa AMIPMode +if sim_mode <: AMIPMode @info("AMIP boundary conditions - do not expect energy conservation") ## land model @@ -344,11 +344,11 @@ if sim_mode isa AMIPMode ) Utilities.show_memory_usage() -elseif (sim_mode isa AbstractSlabplanetSimulationMode) && !(sim_mode isa SlabplanetEisenmanMode) +elseif (sim_mode <: AbstractSlabplanetSimulationMode) && !(sim_mode <: SlabplanetEisenmanMode) - land_area_fraction = sim_mode isa SlabplanetAquaMode ? land_area_fraction .* 0 : land_area_fraction - land_area_fraction = sim_mode isa SlabplanetTerraMode ? land_area_fraction .* 0 .+ 1 : land_area_fraction + land_area_fraction = sim_mode <: SlabplanetAquaMode ? land_area_fraction .* 0 : land_area_fraction + land_area_fraction = sim_mode <: SlabplanetTerraMode ? land_area_fraction .* 0 .+ 1 : land_area_fraction ## land model land_sim = bucket_init( @@ -398,7 +398,7 @@ elseif (sim_mode isa AbstractSlabplanetSimulationMode) && !(sim_mode isa Slabpla mode_specifics = (; type = sim_mode, SST_timevaryinginput = nothing, SIC_timevaryinginput = nothing) Utilities.show_memory_usage() -elseif sim_mode isa SlabplanetEisenmanMode +elseif sim_mode <: SlabplanetEisenmanMode ## land model land_sim = bucket_init( @@ -496,7 +496,7 @@ saved in a global `ConservationChecks` struct, `conservation_checks`, which is t conservation_checks = nothing if energy_check @assert( - sim_mode isa AbstractSlabplanetSimulationMode && !CA.is_distributed(ClimaComms.context(boundary_space)), + sim_mode <: AbstractSlabplanetSimulationMode && !CA.is_distributed(ClimaComms.context(boundary_space)), "Only non-distributed slabplanet allowable for energy_check" ) conservation_checks = (; @@ -538,7 +538,7 @@ albedo_cb = TimeManager.HourlyCallback( dt = dt_water_albedo, func = FluxCalculator.water_albedo_from_atmosphere!, ref_date = [dates.date[1]], - active = sim_mode isa AMIPMode, + active = sim_mode <: AMIPMode, ) callbacks = (; checkpoint = checkpoint_cb, update_firstdayofmonth! = update_firstdayofmonth!_cb, water_albedo = albedo_cb) @@ -560,7 +560,7 @@ end #= Set up default AMIP diagnostics Use ClimaDiagnostics for default AMIP diagnostics, which currently include turbulent energy fluxes. =# -if sim_mode isa AMIPMode && use_coupler_diagnostics +if sim_mode <: AMIPMode && use_coupler_diagnostics include("user_io/amip_diagnostics.jl") coupler_diags_path = joinpath(dir_paths.output, "coupler") isdir(coupler_diags_path) || mkpath(coupler_diags_path) @@ -689,7 +689,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.type isa AMIPMode + if cs.mode.type <: AMIPMode 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) @@ -750,7 +750,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.type isa AMIPMode && !isnothing(cs.amip_diags_handler)) && + (cs.mode.type <: AMIPMode && !isnothing(cs.amip_diags_handler)) && CD.orchestrate_diagnostics(cs_nt, cs.amip_diags_handler) end return nothing diff --git a/experiments/ClimaEarth/user_io/arg_parsing.jl b/experiments/ClimaEarth/user_io/arg_parsing.jl index 3443776cda..8fafd945fa 100644 --- a/experiments/ClimaEarth/user_io/arg_parsing.jl +++ b/experiments/ClimaEarth/user_io/arg_parsing.jl @@ -50,7 +50,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"] - sim_mode = mode_name_dict[mode_name]() + sim_mode = mode_name_dict[mode_name] # Computational simulation setup information random_seed = config_dict["unique_seed"] ? time_ns() : 1234 diff --git a/experiments/ClimaEarth/user_io/postprocessing.jl b/experiments/ClimaEarth/user_io/postprocessing.jl index ec392d7459..f4dd4d3266 100644 --- a/experiments/ClimaEarth/user_io/postprocessing.jl +++ b/experiments/ClimaEarth/user_io/postprocessing.jl @@ -3,12 +3,12 @@ include("debug_plots.jl") include("diagnostics_plots.jl") """ - postprocess_sim(sim_mode::AbstractSlabplanetSimulationMode, cs, postprocessing_vars) + postprocess_sim(::Type{AbstractSlabplanetSimulationMode}, cs, postprocessing_vars) Call `common_postprocessing` to perform common postprocessing tasks that are common to all simulation types. Then, if conservation checks exist, perform them. """ -function postprocess_sim(sim_mode::AbstractSlabplanetSimulationMode, cs, postprocessing_vars) +function postprocess_sim(::Type{AbstractSlabplanetSimulationMode}, cs, postprocessing_vars) (; conservation_softfail,) = postprocessing_vars common_postprocessing(cs, postprocessing_vars) @@ -33,12 +33,12 @@ function postprocess_sim(sim_mode::AbstractSlabplanetSimulationMode, cs, postpro end """ - postprocess_sim(sim_mode::AMIPMode, cs, postprocessing_vars) + postprocess_sim(::Type{AMIPMode}, cs, postprocessing_vars) Call `common_postprocessing` to perform postprocessing tasks that are common to all simulation types, and then conditionally plot AMIP diagnostics """ -function postprocess_sim(sim_mode::AMIPMode, cs, postprocessing_vars) +function postprocess_sim(::Type{AMIPMode}, cs, postprocessing_vars) (; use_coupler_diagnostics, output_default_diagnostics, t_end) = postprocessing_vars common_postprocessing(cs, postprocessing_vars) diff --git a/src/Interfacer.jl b/src/Interfacer.jl index 362a617958..628aab3a3e 100644 --- a/src/Interfacer.jl +++ b/src/Interfacer.jl @@ -267,46 +267,46 @@ abstract type AbstractSlabplanetSimulationMode <: AbstractSimulationMode end """ AMIPMode -A struct representing the AMIP simulation mode. AMIP is currently the most complex +An abstract type representing the AMIP simulation mode. AMIP is currently the most complex configuration of the ClimaEarth model. It runs a ClimaAtmos.jl atmosphere model, ClimaLand.jl bucket land model, a prescribed ocean model, and a simple thermal sea ice model. """ -struct AMIPMode <: AbstractSimulationMode end +abstract type AMIPMode <: AbstractSimulationMode end """ SlabplanetMode -A struct represeting the slabplanet simulation mode with a ClimaAtmos.jl atmosphere model, +An abstract type represeting the slabplanet simulation mode with a ClimaAtmos.jl atmosphere model, a ClimaLand.jl bucket land model, a thermal slab ocean model, and no sea ice model. Instead of using a sea ice model, the ocean evaluated in areas that would be covered in ice. """ -struct SlabplanetMode <: AbstractSlabplanetSimulationMode end +abstract type SlabplanetMode <: AbstractSlabplanetSimulationMode end """ SlabplanetAquaMode -A struct representing the slabplanet simulation mode with a ClimaAtmos.jl atmosphere model, +An abstract type representing the slabplanet simulation mode with a ClimaAtmos.jl atmosphere model, and only once surface model, a thermal slab ocean model, which is evaluated over the entire surface. There are no land or sea ice models. """ -struct SlabplanetAquaMode <: AbstractSlabplanetSimulationMode end +abstract type SlabplanetAquaMode <: AbstractSlabplanetSimulationMode end """ SlabplanetTerraMode -A struct representing the slabplanet simulation mode with a ClimaAtmos.jl atmosphere model, +An abstract type representing the slabplanet simulation mode with a ClimaAtmos.jl atmosphere model, and only once surface model, a ClimaLand.jl bucket land model, which is evaluated over the entire surface. There are no ocean or sea ice models. """ -struct SlabplanetTerraMode <: AbstractSlabplanetSimulationMode end +abstract type SlabplanetTerraMode <: AbstractSlabplanetSimulationMode end """ SlabplanetEisenmanMode -A struct representing the slabplanet simulation mode with a ClimaAtmos.jl atmosphere model, +An abstract type representing the slabplanet simulation mode with a ClimaAtmos.jl atmosphere model, a ClimaLand.jl bucket land model, and Eisenman sea ice model. The ocean model is included in the Eisenman sea ice model. """ -struct SlabplanetEisenmanMode <: AbstractSlabplanetSimulationMode end +abstract type SlabplanetEisenmanMode <: AbstractSlabplanetSimulationMode end end # module