Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use ClimaUtilities CallbackManager #952

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ interface_pages = [
"interfacer.md",
"postprocessor.md",
"regridder.md",
"timemanager.md",
"utilities.md",
]
performance_pages = ["performance.md"]
Expand Down
24 changes: 0 additions & 24 deletions docs/src/timemanager.md

This file was deleted.

41 changes: 17 additions & 24 deletions experiments/ClimaEarth/run_amip.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,12 @@ import ClimaCore as CC
# ## Coupler specific imports
import ClimaCoupler
import ClimaCoupler:
ConservationChecker,
Checkpointer,
Diagnostics,
FieldExchanger,
FluxCalculator,
Interfacer,
Regridder,
TimeManager,
Utilities
ConservationChecker, Checkpointer, Diagnostics, FieldExchanger, FluxCalculator, Interfacer, Regridder, Utilities

import ClimaUtilities.SpaceVaryingInputs: SpaceVaryingInput
import ClimaUtilities.TimeVaryingInputs: TimeVaryingInput, evaluate!
import ClimaUtilities.ClimaArtifacts: @clima_artifact
import ClimaUtilities: CallbackManager
import Interpolations

pkg_dir = pkgdir(ClimaCoupler)
Expand Down Expand Up @@ -460,7 +453,7 @@ Utilities.show_memory_usage(comms_ctx)
model_sims = (atmos_sim = atmos_sim, ice_sim = ice_sim, land_sim = land_sim, ocean_sim = ocean_sim);

## dates
dates = (; date = [date], date0 = [date0], date1 = [Dates.firstdayofmonth(date0)], new_month = [false])
dates = (; date = [date], date0 = [date0], first_day_of_month = [Dates.firstdayofmonth(date0)])

#=
### Online Diagnostics
Expand All @@ -472,7 +465,7 @@ if use_coupler_diagnostics
monthly_3d_diags = Diagnostics.init_diagnostics(
(:T, :u, :q_tot, :q_liq_ice),
atmos_sim.domain.center_space;
save = TimeManager.Monthly(),
save = CallbackManager.Monthly(),
operations = (; accumulate = Diagnostics.TimeMean([Int(0)])),
output_dir = dir_paths.output,
name_tag = "monthly_mean_3d_",
Expand All @@ -481,7 +474,7 @@ if use_coupler_diagnostics
monthly_2d_diags = Diagnostics.init_diagnostics(
(:precipitation_rate, :toa_fluxes, :T_sfc, :turbulent_energy_fluxes),
boundary_space;
save = TimeManager.Monthly(),
save = CallbackManager.Monthly(),
operations = (; accumulate = Diagnostics.TimeMean([Int(0)])),
output_dir = dir_paths.output,
name_tag = "monthly_mean_2d_",
Expand Down Expand Up @@ -517,7 +510,8 @@ end
#=
## Initialize Callbacks
Callbacks are used to update at a specified interval. The callbacks are initialized here and
saved in a global `Callbacks` struct, `callbacks`. The `trigger_callback!` function is used to call the callback during the simulation below.
saved in a global `Callbacks` struct, `callbacks`. The `trigger_callback!` function is used to call the callback
when required during the simulation below.

The frequency of the callbacks is specified in the `HourlyCallback` and `MonthlyCallback` structs. The `func` field specifies the function to be called,
the `ref_date` field specifies the first date for the callback, and the `active` field specifies whether the callback is active or not.
Expand All @@ -530,20 +524,20 @@ The currently implemented callbacks are:
NB: Eventually, we will call all of radiation from the coupler, in addition to the albedo calculation.
=#

checkpoint_cb = TimeManager.HourlyCallback(
checkpoint_cb = CallbackManager.HourlyCallback(
dt = hourly_checkpoint_dt,
func = checkpoint_sims,
ref_date = [dates.date[1]],
active = hourly_checkpoint,
) # 20 days
update_firstdayofmonth!_cb = TimeManager.MonthlyCallback(
update_firstdayofmonth!_cb = CallbackManager.MonthlyCallback(
dt = FT(1),
func = TimeManager.update_firstdayofmonth!,
ref_date = [dates.date1[1]],
func = Interfacer.update_firstdayofmonth!,
ref_date = [dates.first_day_of_month[1]],
active = true,
)
dt_water_albedo = parse(FT, filter(x -> !occursin(x, "hours"), dt_rad))
albedo_cb = TimeManager.HourlyCallback(
albedo_cb = CallbackManager.HourlyCallback(
dt = dt_water_albedo,
func = FluxCalculator.water_albedo_from_atmosphere!,
ref_date = [dates.date[1]],
Expand Down Expand Up @@ -683,10 +677,10 @@ function solve_coupler!(cs)
## step in time
for t in ((tspan[begin] + Δt_cpl):Δt_cpl:tspan[end])

cs.dates.date[1] = TimeManager.current_date(cs, t)
cs.dates.date[1] = Interfacer.current_date(cs, t)

## print date on the first of month
if cs.dates.date[1] >= cs.dates.date1[1]
if cs.dates.date[1] >= cs.dates.first_day_of_month[1]
ClimaComms.iamroot(comms_ctx) && @show(cs.dates.date[1])
end

Expand Down Expand Up @@ -717,8 +711,7 @@ function solve_coupler!(cs)

## update water albedo from wind at dt_water_albedo
## (this will be extended to a radiation callback from the coupler)
TimeManager.trigger_callback!(cs, cs.callbacks.water_albedo)

CallbackManager.trigger_callback!(cs.callbacks.water_albedo, cs.dates.date[1])

## update the surface fractions for surface models,
## and update all component model simulations with the current fluxes stored in the coupler
Expand Down Expand Up @@ -752,10 +745,10 @@ function solve_coupler!(cs)
FieldExchanger.import_atmos_fields!(cs.fields, cs.model_sims, cs.boundary_space, cs.turbulent_fluxes) # radiative and/or turbulent

## callback to update the fist day of month if needed
TimeManager.trigger_callback!(cs, cs.callbacks.update_firstdayofmonth!)
CallbackManager.trigger_callback!(cs.callbacks.update_firstdayofmonth!, cs.dates.date[1])

## callback to checkpoint model state
TimeManager.trigger_callback!(cs, cs.callbacks.checkpoint)
CallbackManager.trigger_callback!(cs.callbacks.checkpoint, cs.dates.date[1])
end
return nothing
end
Expand Down
24 changes: 12 additions & 12 deletions experiments/ClimaEarth/run_cloudless_aquaplanet.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import ClimaCoupler:
FluxCalculator,
Interfacer,
Regridder,
TimeManager,
CallbackManager,
Utilities

pkg_dir = pkgdir(ClimaCoupler)
Expand Down Expand Up @@ -211,26 +211,26 @@ model_sims = (atmos_sim = atmos_sim, ocean_sim = ocean_sim);

## dates
date0 = date = Dates.DateTime(start_date, Dates.dateformat"yyyymmdd")
dates = (; date = [date], date0 = [date0], date1 = [Dates.firstdayofmonth(date0)], new_month = [false])
dates = (; date = [date], date0 = [date0], first_day_of_month = [Dates.firstdayofmonth(date0)])

#=
## Initialize Callbacks
=#

checkpoint_cb = TimeManager.HourlyCallback(
checkpoint_cb = CallbackManager.HourlyCallback(
dt = FT(480),
func = checkpoint_sims,
ref_date = [dates.date[1]],
active = hourly_checkpoint,
) # 20 days
update_firstdayofmonth!_cb = TimeManager.MonthlyCallback(
update_firstdayofmonth!_cb = CallbackManager.MonthlyCallback(
dt = FT(1),
func = TimeManager.update_firstdayofmonth!,
ref_date = [dates.date1[1]],
func = Interfacer.update_firstdayofmonth!,
ref_date = [dates.first_day_of_month[1]],
active = true,
)
dt_water_albedo = parse(FT, filter(x -> !occursin(x, "hours"), dt_rad))
albedo_cb = TimeManager.HourlyCallback(
albedo_cb = CallbackManager.HourlyCallback(
dt = dt_water_albedo,
func = FluxCalculator.water_albedo_from_atmosphere!,
ref_date = [dates.date[1]],
Expand Down Expand Up @@ -325,17 +325,17 @@ function solve_coupler!(cs)
## step in time
for t in ((tspan[begin] + Δt_cpl):Δt_cpl:tspan[end])

cs.dates.date[1] = TimeManager.current_date(cs, t)
cs.dates.date[1] = Interfacer.current_date(cs, t)

## print date on the first of month
if cs.dates.date[1] >= cs.dates.date1[1]
if cs.dates.date[1] >= cs.dates.first_day_of_month[1]
ClimaComms.iamroot(comms_ctx) && @show(cs.dates.date[1])
end

ClimaComms.barrier(comms_ctx)

## update water albedo from wind at dt_water_albedo (this will be extended to a radiation callback from the coupler)
TimeManager.trigger_callback!(cs, cs.callbacks.water_albedo)
CallbackManager.trigger_callback!(cs.callbacks.water_albedo, cs.dates.date[1])

## run component models sequentially for one coupling timestep (Δt_cpl)
FieldExchanger.update_model_sims!(cs.model_sims, cs.fields, cs.turbulent_fluxes)
Expand All @@ -350,10 +350,10 @@ function solve_coupler!(cs)
FieldExchanger.import_atmos_fields!(cs.fields, cs.model_sims, cs.boundary_space, cs.turbulent_fluxes) # radiative and/or turbulent

## callback to update the fist day of month if needed
TimeManager.trigger_callback!(cs, cs.callbacks.update_firstdayofmonth!)
CallbackManager.trigger_callback!(cs.callbacks.update_firstdayofmonth!, cs.dates.date[1])

## callback to checkpoint model state
TimeManager.trigger_callback!(cs, cs.callbacks.checkpoint)
CallbackManager.trigger_callback!(cs.callbacks.checkpoint, cs.dates.date[1])

end

Expand Down
20 changes: 10 additions & 10 deletions experiments/ClimaEarth/run_cloudy_aquaplanet.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import ClimaCoupler:
FluxCalculator,
Interfacer,
Regridder,
TimeManager,
CallbackManager,
Utilities

pkg_dir = pkgdir(ClimaCoupler)
Expand Down Expand Up @@ -232,22 +232,22 @@ model_sims = (atmos_sim = atmos_sim, ocean_sim = ocean_sim);

## dates
date0 = date = Dates.DateTime(start_date, Dates.dateformat"yyyymmdd")
dates = (; date = [date], date0 = [date0], date1 = [Dates.firstdayofmonth(date0)], new_month = [false])
dates = (; date = [date], date0 = [date0], first_day_of_month = [Dates.firstdayofmonth(date0)])

#=
## Initialize Callbacks
=#

checkpoint_cb = TimeManager.HourlyCallback(
checkpoint_cb = CallbackManager.HourlyCallback(
dt = FT(480),
func = checkpoint_sims,
ref_date = [dates.date[1]],
active = hourly_checkpoint,
) # 20 days
update_firstdayofmonth!_cb = TimeManager.MonthlyCallback(
update_firstdayofmonth!_cb = CallbackManager.MonthlyCallback(
dt = FT(1),
func = TimeManager.update_firstdayofmonth!,
ref_date = [dates.date1[1]],
func = Interfacer.update_firstdayofmonth!,
ref_date = [dates.first_day_of_month[1]],
active = true,
)
callbacks = (; checkpoint = checkpoint_cb, update_firstdayofmonth! = update_firstdayofmonth!_cb)
Expand Down Expand Up @@ -337,10 +337,10 @@ function solve_coupler!(cs)
## step in time
for t in ((tspan[begin] + Δt_cpl):Δt_cpl:tspan[end])

cs.dates.date[1] = TimeManager.current_date(cs, t)
cs.dates.date[1] = Interfacer.current_date(cs, t)

## print date on the first of month
if cs.dates.date[1] >= cs.dates.date1[1]
if cs.dates.date[1] >= cs.dates.first_day_of_month[1]
ClimaComms.iamroot(comms_ctx) && @show(cs.dates.date[1])
end

Expand All @@ -359,10 +359,10 @@ function solve_coupler!(cs)
FieldExchanger.import_atmos_fields!(cs.fields, cs.model_sims, cs.boundary_space, cs.turbulent_fluxes) # radiative and/or turbulent

## callback to update the fist day of month if needed
TimeManager.trigger_callback!(cs, cs.callbacks.update_firstdayofmonth!)
CallbackManager.trigger_callback!(cs.callbacks.update_firstdayofmonth!, cs.dates.date[1])

## callback to checkpoint model state
TimeManager.trigger_callback!(cs, cs.callbacks.checkpoint)
CallbackManager.trigger_callback!(cs.callbacks.checkpoint, cs.dates.date[1])

end

Expand Down
24 changes: 12 additions & 12 deletions experiments/ClimaEarth/run_cloudy_slabplanet.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import ClimaCoupler:
FluxCalculator,
Interfacer,
Regridder,
TimeManager,
CallbackManager,
Utilities

pkg_dir = pkgdir(ClimaCoupler)
Expand Down Expand Up @@ -175,7 +175,7 @@ land_mask_data = artifact_data(mask_dataset_path(), "seamask")

## dates
date0 = date = Dates.DateTime(start_date, Dates.dateformat"yyyymmdd")
dates = (; date = [date], date0 = [date0], date1 = [Dates.firstdayofmonth(date0)], new_month = [false])
dates = (; date = [date], date0 = [date0], first_day_of_month = [Dates.firstdayofmonth(date0)])


#=
Expand Down Expand Up @@ -284,20 +284,20 @@ model_sims = (atmos_sim = atmos_sim, ocean_sim = ocean_sim);
## Initialize Callbacks
=#

checkpoint_cb = TimeManager.HourlyCallback(
checkpoint_cb = CallbackManager.HourlyCallback(
dt = FT(480),
func = checkpoint_sims,
ref_date = [dates.date[1]],
active = hourly_checkpoint,
) # 20 days
update_firstdayofmonth!_cb = TimeManager.MonthlyCallback(
update_firstdayofmonth!_cb = CallbackManager.MonthlyCallback(
dt = FT(1),
func = TimeManager.update_firstdayofmonth!,
ref_date = [dates.date1[1]],
func = Interfacer.update_firstdayofmonth!,
ref_date = [dates.first_day_of_month[1]],
active = true,
)
dt_water_albedo = parse(FT, filter(x -> !occursin(x, "hours"), dt_rad))
albedo_cb = TimeManager.HourlyCallback(
albedo_cb = CallbackManager.HourlyCallback(
dt = dt_water_albedo,
func = FluxCalculator.water_albedo_from_atmosphere!,
ref_date = [dates.date[1]],
Expand Down Expand Up @@ -389,17 +389,17 @@ function solve_coupler!(cs)
## step in time
for t in ((tspan[begin] + Δt_cpl):Δt_cpl:tspan[end])

cs.dates.date[1] = TimeManager.current_date(cs, t)
cs.dates.date[1] = Interfacer.current_date(cs, t)

## print date on the first of month
if cs.dates.date[1] >= cs.dates.date1[1]
if cs.dates.date[1] >= cs.dates.first_day_of_month[1]
ClimaComms.iamroot(comms_ctx) && @show(cs.dates.date[1])
end

ClimaComms.barrier(comms_ctx)

## update water albedo from wind at dt_water_albedo (this will be extended to a radiation callback from the coupler)
TimeManager.trigger_callback!(cs, cs.callbacks.water_albedo)
CallbackManager.trigger_callback!(cs.callbacks.water_albedo, cs.dates.date[1])

## run component models sequentially for one coupling timestep (Δt_cpl)
FieldExchanger.update_model_sims!(cs.model_sims, cs.fields, cs.turbulent_fluxes)
Expand All @@ -414,10 +414,10 @@ function solve_coupler!(cs)
FieldExchanger.import_atmos_fields!(cs.fields, cs.model_sims, cs.boundary_space, cs.turbulent_fluxes) # radiative and/or turbulent

## callback to update the fist day of month if needed
TimeManager.trigger_callback!(cs, cs.callbacks.update_firstdayofmonth!)
CallbackManager.trigger_callback!(cs.callbacks.update_firstdayofmonth!, cs.dates.date[1])

## callback to checkpoint model state
TimeManager.trigger_callback!(cs, cs.callbacks.checkpoint)
CallbackManager.trigger_callback!(cs.callbacks.checkpoint, cs.dates.date[1])

end

Expand Down
Loading
Loading