Skip to content

Commit

Permalink
use ClimaUtilities TimeManager module
Browse files Browse the repository at this point in the history
  • Loading branch information
juliasloan25 committed Sep 21, 2023
1 parent c435636 commit d2ecee2
Show file tree
Hide file tree
Showing 21 changed files with 93 additions and 180 deletions.
2 changes: 2 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ ClimaComms = "3a4d1b5c-c61d-41fd-a00a-5873ba7a1b0d"
ClimaCore = "d414da3d-4745-48bb-8d80-42e94e092884"
ClimaCoreTempestRemap = "d934ef94-cdd4-4710-83d6-720549644b70"
ClimaLSM = "7884a58f-fab6-4fd0-82bb-ecfedb2d8430"
ClimaUtilities = "b3f4f4ca-9299-4f7f-bd9b-81e1242a7513"
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
DocStringExtensions = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae"
Insolation = "e98cc03f-d57e-4e3c-b70c-8d51efe9e0d8"
Expand All @@ -34,6 +35,7 @@ ClimaComms = "0.5"
ClimaCore = "0.10"
ClimaCoreTempestRemap = "0.3"
ClimaLSM = "0.3.2"
ClimaUtilities = "0.1.1"
DocStringExtensions = "0.8, 0.9"
Insolation = "0.6"
JLD2 = "0.4"
Expand Down
1 change: 1 addition & 0 deletions docs/Project.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[deps]
ClimaCoupler = "4ade58fe-a8da-486c-bd89-46df092ec0c7"
ClimaUtilities = "b3f4f4ca-9299-4f7f-bd9b-81e1242a7513"
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
Literate = "98b081ad-f1c9-55d3-8b20-4c87d4299306"
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
Expand Down
1 change: 0 additions & 1 deletion docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ interface_pages = [
"utilities.md",
"bcreader.md",
"testhelper.md",
"timemanager.md",
]
performance_pages = ["performance.md"]

Expand Down
16 changes: 0 additions & 16 deletions docs/src/timemanager.md

This file was deleted.

12 changes: 10 additions & 2 deletions experiments/AMIP/modular/Manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

julia_version = "1.8.5"
manifest_format = "2.0"
project_hash = "0c52bcb1446b52cbf673aa87a7b4c42cc1fdbcf2"
project_hash = "32aebffcff9cfbbd3d7e402ba60cde756ded70bb"

[[deps.ADTypes]]
git-tree-sha1 = "f5c25e8a5b29b5e941b7408bc8cc79fea4d9ef9a"
Expand Down Expand Up @@ -239,7 +239,7 @@ uuid = "d934ef94-cdd4-4710-83d6-720549644b70"
version = "0.3.5"

[[deps.ClimaCoupler]]
deps = ["CLIMAParameters", "ClimaAtmos", "ClimaComms", "ClimaCore", "ClimaCoreTempestRemap", "ClimaLSM", "Dates", "DocStringExtensions", "Insolation", "JLD2", "NCDatasets", "OrdinaryDiffEq", "Plots", "PrettyTables", "SciMLBase", "StaticArrays", "Statistics", "SurfaceFluxes", "TempestRemap_jll", "TerminalLoggers", "Thermodynamics", "UnPack"]
deps = ["CLIMAParameters", "ClimaAtmos", "ClimaComms", "ClimaCore", "ClimaCoreTempestRemap", "ClimaLSM", "ClimaUtilities", "Dates", "DocStringExtensions", "Insolation", "JLD2", "NCDatasets", "OrdinaryDiffEq", "Plots", "PrettyTables", "SciMLBase", "StaticArrays", "Statistics", "SurfaceFluxes", "TempestRemap_jll", "TerminalLoggers", "Thermodynamics", "UnPack"]
path = "../../.."
uuid = "4ade58fe-a8da-486c-bd89-46df092ec0c7"
version = "0.1.0"
Expand All @@ -256,6 +256,14 @@ git-tree-sha1 = "54b602435b0107b6c2dfe7664e0f7ff5fc78fb91"
uuid = "595c0a79-7f3d-439a-bc5a-b232dc3bde79"
version = "0.7.7"

[[deps.ClimaUtilities]]
deps = ["CFTime", "Dates"]
git-tree-sha1 = "1d8a2368e0290a71301cb515e564b8b48ba1b759"
repo-rev = "js/timemanager"
repo-url = "https://github.com/CliMA/ClimaUtilities.jl"
uuid = "b3f4f4ca-9299-4f7f-bd9b-81e1242a7513"
version = "0.1.0"

[[deps.CloseOpenIntervals]]
deps = ["Static", "StaticArrayInterface"]
git-tree-sha1 = "70232f82ffaab9dc52585e0dd043b5e0c6b714f1"
Expand Down
1 change: 1 addition & 0 deletions experiments/AMIP/modular/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ ClimaCoreTempestRemap = "d934ef94-cdd4-4710-83d6-720549644b70"
ClimaCoupler = "4ade58fe-a8da-486c-bd89-46df092ec0c7"
ClimaLSM = "7884a58f-fab6-4fd0-82bb-ecfedb2d8430"
ClimaTimeSteppers = "595c0a79-7f3d-439a-bc5a-b232dc3bde79"
ClimaUtilities = "b3f4f4ca-9299-4f7f-bd9b-81e1242a7513"
CloudMicrophysics = "6a9e3e04-43cd-43ba-94b9-e8782df3c71b"
Colors = "5ae59095-9a9b-59fe-a467-6f913c188581"
Dierckx = "39dd38d3-220a-591b-8e3c-4c3a8c710a94"
Expand Down
26 changes: 16 additions & 10 deletions experiments/AMIP/modular/coupler_driver_modular.jl
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ using Statistics: mean
using ClimaCore.Utilities: half, PlusHalf
using ClimaCore: InputOutput, Fields
import ClimaCore.Spaces as Spaces
import ClimaUtilities: TimeManager

if !(@isdefined parsed_args)
include("cli_options.jl")
Expand Down Expand Up @@ -115,10 +116,9 @@ import ClimaCoupler.Regridder:
update_surface_fractions!, combine_surfaces!, combine_surfaces_from_sol!, dummmy_remap!, binary_mask
import ClimaCoupler.ConservationChecker:
EnergyConservationCheck, WaterConservationCheck, check_conservation!, plot_global_conservation
import ClimaCoupler.Utilities: CoupledSimulation, float_type, swap_space!
import ClimaCoupler.Utilities: CoupledSimulation, float_type, swap_space!, current_date
import ClimaCoupler.BCReader:
bcfile_info_init, float_type_bcf, update_midmonth_data!, next_date_in_file, interpolate_midmonth_to_daily
import ClimaCoupler.TimeManager: current_date, datetime_to_strdate, trigger_callback, Monthly, EveryTimestep
import ClimaCoupler.Diagnostics: get_var, init_diagnostics, accumulate_diagnostics!, save_diagnostics, TimeMean
import ClimaCoupler.PostProcessor: postprocess

Expand Down Expand Up @@ -357,7 +357,7 @@ User can write custom diagnostics in the `user_diagnostics.jl`.
monthly_3d_diags = init_diagnostics(
(:T, :u, :q_tot, :q_liq_ice),
atmos_sim.domain.center_space;
save = Monthly(),
save = TimeManager.Monthly(),
operations = (; accumulate = TimeMean([Int(0)])),
output_dir = COUPLER_OUTPUT_DIR,
name_tag = "monthly_mean_3d_",
Expand All @@ -366,7 +366,7 @@ monthly_3d_diags = init_diagnostics(
monthly_2d_diags = init_diagnostics(
(:precipitation_rate, :toa_fluxes, :T_sfc, :tubulent_energy_fluxes),
boundary_space;
save = Monthly(),
save = TimeManager.Monthly(),
operations = (; accumulate = TimeMean([Int(0)])),
output_dir = COUPLER_OUTPUT_DIR,
name_tag = "monthly_mean_2d_",
Expand Down Expand Up @@ -544,18 +544,24 @@ function solve_coupler!(cs)
import_atmos_fields!(cs.fields, cs.model_sims, cs.boundary_space, turbulent_fluxes) # radiative and/or turbulent

## monthly callbacks
if trigger_callback(cs, Monthly())
## step to the next calendar month
cs.dates.date1[1] += Dates.Month(1)
## checkpoint model state
if monthly_checkpoint
## function to checkpoint model state
checkpoint_func =
(monthly_checkpoint, cs, comms_ctx, t, COUPLER_ARTIFACTS_DIR) -> if monthly_checkpoint
for sim in cs.model_sims
if get_model_state_vector(sim) !== nothing
checkpoint_model_state(sim, comms_ctx, Int(t), output_dir = COUPLER_ARTIFACTS_DIR)
end
end
end
end
func_args = (monthly_checkpoint, cs, comms_ctx, t, COUPLER_ARTIFACTS_DIR)
## perform monthly callback and increment `cs.dates.date1` if `cs.dates.date` passes to next month
TimeManager.trigger_callback(
cs.dates.date1[1],
cs.dates.date[1],
TimeManager.Monthly(),
checkpoint_func,
func_args,
)

end
@show walltime
Expand Down
3 changes: 2 additions & 1 deletion src/BCReader.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ monthly to daily intervals.
"""
module BCReader

using ..Utilities, ..Regridder, ..TimeManager
using ..Utilities, ..Regridder
using ClimaCore: Fields
using ClimaComms
import ClimaUtilities: TimeManager
using Dates
using JLD2

Expand Down
1 change: 0 additions & 1 deletion src/Checkpointer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ module Checkpointer
using ClimaCore: Fields, InputOutput
using ClimaCoupler: Interfacer
using Dates
using ClimaCoupler.TimeManager: AbstractFrequency, Monthly, EveryTimestep, trigger_callback
using ClimaComms
export get_model_state_vector, checkpoint_model_state, restart_model_state!

Expand Down
1 change: 0 additions & 1 deletion src/ClimaCoupler.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ include("CoupledSimulations/coupled_simulation.jl")
include("CouplerState/coupler_state.jl")
include("../test/TestHelper.jl")
include("Utilities.jl")
include("TimeManager.jl")
include("Regridder.jl")
include("ConservationChecker.jl")
include("BCReader.jl")
Expand Down
11 changes: 9 additions & 2 deletions src/Diagnostics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module Diagnostics
using ClimaCore: Spaces, Fields, InputOutput
using ClimaCoupler.Utilities: CoupledSimulation
using Dates
using ClimaCoupler.TimeManager: AbstractFrequency, Monthly, EveryTimestep, trigger_callback
using ClimaUtilities.TimeManager: Monthly, EveryTimestep, trigger_callback
using ClimaComms

export get_var, init_diagnostics, accumulate_diagnostics!, save_diagnostics, TimeMean
Expand Down Expand Up @@ -131,13 +131,20 @@ end
Saves all entries in `dg` in separate HDF5 files per variable in `output_dir`.
"""
function save_diagnostics(cs::CoupledSimulation)
for dg in cs.diagnostics
# extract dates for callback condition check
date_cutoff = cs.dates.date1[1]
date_current = cs.dates.date[1]
# define function to perform diagnostic saving if callback is triggered
save_func = (cs) -> for dg in cs.diagnostics
if trigger_callback(cs, dg.save)
pre_save(dg.operations.accumulate, cs, dg)
save_diagnostics(cs, dg)
post_save(dg.operations.accumulate, cs, dg)
end
end
func_args = (cs,)
# perform monthly callback and increment `cs.dates.date1` if `date_current` passes to next month
trigger_callback(date_cutoff, date_current, Monthly(), save_func, func_args)
end
function save_diagnostics(cs::CoupledSimulation, dg::DiagnosticsGroup)

Expand Down
4 changes: 2 additions & 2 deletions src/Regridder.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ via ClimaCoreTempestRemap wrappers.
module Regridder

using ..Utilities
using ..TimeManager
using ..Interfacer
using ClimaCore: Meshes, Domains, Topologies, Spaces, Fields, InputOutput
using ClimaComms
using ClimaUtilities.TimeManager
using NCDatasets
using ClimaCoreTempestRemap
using Dates
Expand Down Expand Up @@ -144,7 +144,7 @@ function hdwrite_regridfile_rll_to_cgll(
if "time" in ds
data_dates = Dates.DateTime.(ds["time"][:])
elseif "date" in ds
data_dates = TimeManager.strdate_to_datetime.(string.(ds["date"][:]))
data_dates = strdate_to_datetime.(string.(ds["date"][:]))
else
@warn "No dates available in file $datafile_rll"
data_dates = [Dates.DateTime(0)]
Expand Down
66 changes: 0 additions & 66 deletions src/TimeManager.jl

This file was deleted.

14 changes: 13 additions & 1 deletion src/Utilities.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ modules in the coupler.
module Utilities

using ClimaCore: Fields, Spaces
using Dates

export CoupledSimulation, float_type, swap_space!
export CoupledSimulation, float_type, swap_space!, current_date

"""
AbstractSimulation
Expand Down Expand Up @@ -74,4 +75,15 @@ function swap_space!(field_out, field_in::Fields.Field)
return field_out
end

"""
current_date(cs::CoupledSimulation, t::Int)
Return the model date at the current timestep.
# Arguments
- `cs`: [CoupledSimulation] containing info about the simulation
- `t`: [Real] number of seconds since simulation began
"""
current_date(cs::CoupledSimulation, t::Real) = cs.dates.date0[1] + Dates.Second(t)

end # module
1 change: 1 addition & 0 deletions test/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ ClimaCore = "d414da3d-4745-48bb-8d80-42e94e092884"
ClimaCoupler = "4ade58fe-a8da-486c-bd89-46df092ec0c7"
ClimaLSM = "7884a58f-fab6-4fd0-82bb-ecfedb2d8430"
ClimaTimeSteppers = "595c0a79-7f3d-439a-bc5a-b232dc3bde79"
ClimaUtilities = "b3f4f4ca-9299-4f7f-bd9b-81e1242a7513"
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
Downloads = "f43a241f-c20a-4ad4-852c-f6b1247861c6"
IntervalSets = "8197267c-284f-5f27-9208-e0e47529a953"
Expand Down
5 changes: 3 additions & 2 deletions test/bcreader_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
Unit tests for ClimaCoupler BCReader module
=#

using ClimaCoupler: Regridder, BCReader, TimeManager, Utilities
using ClimaCoupler: Regridder, BCReader, Utilities
using ClimaCore: Fields, Meshes, Domains, Topologies, Spaces
using ClimaComms
using ClimaUtilities: TimeManager
using Test
using Dates
using NCDatasets
Expand Down Expand Up @@ -198,7 +199,7 @@ for FT in (Float32, Float64)

# step in time
walltime = @elapsed for t in ((tspan[1] + Δt):Δt:tspan[end])
cs_t.dates.date[1] = TimeManager.current_date(cs_t, t) # if not global, `date`` is not updated. Check that this still runs when distributed.
cs_t.dates.date[1] = Utilities.current_date(cs_t, t) # if not global, `date`` is not updated. Check that this still runs when distributed.

model_date = cs_t.dates.date[1]
callback_date = BCReader.next_date_in_file(bcf_info)
Expand Down
2 changes: 1 addition & 1 deletion test/diagnostics_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ using Test
using Dates
using ClimaCore: InputOutput
using ClimaComms
using ClimaUtilities.TimeManager: Monthly, EveryTimestep
using ClimaCoupler: Utilities
using ClimaCoupler.TimeManager: EveryTimestep, Monthly
using ClimaCoupler.TestHelper: create_space
import ClimaCoupler.Diagnostics:
get_var,
Expand Down
3 changes: 2 additions & 1 deletion test/mpi_tests/bcreader_mpi_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ that MPI can be enabled for testing of these functions.
using ClimaCoupler: Regridder, BCReader, TimeManager, Utilities
using ClimaCore: Fields, Meshes, Domains, Topologies, Spaces
using ClimaComms
import ClimaUtilities: TimeManager
using Test
using Dates
using NCDatasets
Expand Down Expand Up @@ -142,7 +143,7 @@ end

# step in time
walltime = @elapsed for t in ((tspan[1] + Δt):Δt:tspan[end])
cs_t.dates.date[1] = TimeManager.current_date(cs_t, t) # if not global, `date`` is not updated. Check that this still runs when distributed.
cs_t.dates.date[1] = Utilities.current_date(cs_t, t) # if not global, `date`` is not updated. Check that this still runs when distributed.

model_date = cs_t.dates.date[1]
callback_date = BCReader.next_date_in_file(bcf_info)
Expand Down
3 changes: 0 additions & 3 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ end
@safetestset "Utilities tests" begin
include("utilities_tests.jl")
end
@safetestset "TimeManager tests" begin
include("time_manager_tests.jl")
end
@safetestset "FieldExchanger tests" begin
include("field_exchanger_tests.jl")
end
Expand Down
Loading

0 comments on commit d2ecee2

Please sign in to comment.