Skip to content

Commit

Permalink
Change simulation mode structs to abstract types
Browse files Browse the repository at this point in the history
This is more in line with the ComponentModelSimulation types
in Interfacer.jl
  • Loading branch information
imreddyTeja committed Dec 17, 2024
1 parent d6f9027 commit 07677c1
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 30 deletions.
10 changes: 5 additions & 5 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,26 @@ 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`
- `ClimaCoupler.Interfacer.SlabplanetAquaMode`
- `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.
Expand Down
20 changes: 10 additions & 10 deletions experiments/ClimaEarth/run_amip.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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 = (;
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion experiments/ClimaEarth/user_io/arg_parsing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions experiments/ClimaEarth/user_io/postprocessing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
20 changes: 10 additions & 10 deletions src/Interfacer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 07677c1

Please sign in to comment.