Skip to content

Commit

Permalink
TimeManager module complete
Browse files Browse the repository at this point in the history
  • Loading branch information
juliasloan25 committed Jan 11, 2023
1 parent 5c486a7 commit 7949bd4
Show file tree
Hide file tree
Showing 17 changed files with 254 additions and 150 deletions.
1 change: 1 addition & 0 deletions docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ interface_pages = [
"utilities.md",
"bcreader.md",
"testhelper.md",
"timemanager.md",
]

pages = Any["Home" => "index.md", "Examples" => experiment_pages, "Coupler Interface" => interface_pages]
Expand Down
13 changes: 13 additions & 0 deletions docs/src/timemanager.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# TimeManager

This module contains functions that handle dates and times
in simulations. The functions in this module often call
functions from Julia's [Dates](https://docs.julialang.org/en/v1/stdlib/Dates/) module.

## TimeManager API

```@docs
ClimaCoupler.TimeManager.current_date
ClimaCoupler.TimeManager.strdate_to_datetime
ClimaCoupler.TimeManager.datetime_to_strdate
```
70 changes: 37 additions & 33 deletions experiments/AMIP/moist_mpi_earth/coupler_driver_modular.jl
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ run_name = parsed_args["run_name"]
energy_check = parsed_args["energy_check"]
const FT = parsed_args["FLOAT_TYPE"] == "Float64" ? Float64 : Float32
land_sim_name = "bucket"
t_end = FT(time_to_seconds(parsed_args["t_end"]))
tspan = (0, t_end)
Δt_cpl = FT(parsed_args["dt_cpl"])
t_end = Int(time_to_seconds(parsed_args["t_end"]))
tspan = (Int(0), t_end)
Δt_cpl = Int(parsed_args["dt_cpl"])
saveat = time_to_seconds(parsed_args["dt_save_to_sol"])
date0 = date = DateTime(parsed_args["start_date"], dateformat"yyyymmdd")
mono_surface = parsed_args["mono_surface"]
Expand All @@ -96,6 +96,7 @@ import ClimaCoupler.ConservationChecker:
import ClimaCoupler.Utilities: CoupledSimulation, float_type_cs, swap_space!
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

pkg_dir = pkgdir(ClimaCoupler)
COUPLER_OUTPUT_DIR = joinpath(pkg_dir, "experiments/AMIP/moist_mpi_earth/output", joinpath(mode_name, run_name))
Expand All @@ -113,7 +114,6 @@ mask_data = joinpath(mask_dataset_path(), "seamask.nc")

# import coupler utils
include("coupler_utils/flux_calculator.jl")
include("coupler_utils/calendar_timer.jl")
include("coupler_utils/variable_definer.jl")
include("coupler_utils/diagnostics_gatherer.jl")
include("coupler_utils/offline_postprocessor.jl")
Expand Down Expand Up @@ -184,7 +184,7 @@ if mode_name == "amip"
)

update_midmonth_data!(date0, SST_info)
SST_init = interpolate_midmonth_to_daily(FT, date0, SST_info)
SST_init = interpolate_midmonth_to_daily(date0, SST_info)
ocean_params = OceanSlabParameters(FT(20), FT(1500.0), FT(800.0), FT(280.0), FT(1e-3), FT(1e-5), FT(0.06))
ocean_sim = (;
integrator = (;
Expand All @@ -208,7 +208,7 @@ if mode_name == "amip"
mono = mono_surface,
)
update_midmonth_data!(date0, SIC_info)
SIC_init = interpolate_midmonth_to_daily(FT, date0, SIC_info)
SIC_init = interpolate_midmonth_to_daily(date0, SIC_info)
ice_mask = get_ice_mask.(SIC_init, mono_surface)
ice_sim = ice_init(FT; tspan = tspan, dt = Δt_cpl, space = boundary_space, saveat = saveat, ice_mask = ice_mask)
mode_specifics = (; name = mode_name, SST_info = SST_info, SIC_info = SIC_info)
Expand Down Expand Up @@ -290,14 +290,14 @@ end
## coupler simulation
cs = CoupledSimulation{FT}(
comms_ctx,
tspan,
dates,
boundary_space,
coupler_fields,
parsed_args,
conservation_checks,
tspan,
integrator.t,
FT(Δt_cpl),
Δt_cpl,
(; land = land_mask, ocean = zeros(boundary_space), ice = zeros(boundary_space)),
model_sims,
mode_specifics,
Expand Down Expand Up @@ -336,22 +336,23 @@ function solve_coupler!(cs)

cs.dates.date[1] = current_date(cs, t) # if not global, `date` is not updated.

## print date on the first of month
@calendar_callback :(@show(cs.dates.date[1])) cs.dates.date[1] cs.dates.date1[1]
## print date on the first of month
if cs.dates.date[1] >= cs.dates.date1[1]
@show(cs.dates.date[1])
end

if cs.mode.name == "amip"

## monthly read of boundary condition data for SST and SIC
@calendar_callback :(update_midmonth_data!(cs.dates.date[1], cs.mode.SST_info)) cs.dates.date[1] next_date_in_file(
cs.mode.SST_info,
)
SST =
ocean_sim.integrator.u.T_sfc .=
interpolate_midmonth_to_daily(FT, cs.dates.date[1], cs.mode.SST_info)
@calendar_callback :(update_midmonth_data!(cs.dates.date[1], cs.mode.SIC_info)) cs.dates.date[1] next_date_in_file(
cs.mode.SIC_info,
)
SIC = interpolate_midmonth_to_daily(FT, cs.dates.date[1], cs.mode.SIC_info)
if cs.dates.date[1] >= next_date_in_file(cs.mode.SST_info)
update_midmonth_data!(cs.dates.date[1], cs.mode.SST_info)
end
SST = ocean_sim.integrator.u.T_sfc .= interpolate_midmonth_to_daily(cs.dates.date[1], cs.mode.SST_info)

if cs.dates.date[1] >= next_date_in_file(cs.mode.SIC_info)
update_midmonth_data!(cs.dates.date[1], cs.mode.SIC_info)
end
SIC = interpolate_midmonth_to_daily(cs.dates.date[1], cs.mode.SIC_info)

ice_mask = ice_sim.integrator.p.ice_mask .= get_ice_mask.(SIC_init, mono_surface)

Expand All @@ -360,30 +361,31 @@ function solve_coupler!(cs)
accumulate_diags(collect_diags(cs, propertynames(cs.monthly_2d_diags.fields)), cs.monthly_2d_diags)

## save and reset monthly averages
@calendar_callback :(
map(x -> x ./= cs.monthly_3d_diags.ct[1], cs.monthly_3d_diags.fields),
if cs.dates.date[1] >= cs.dates.date1[1]
map(x -> x ./= cs.monthly_3d_diags.ct[1], cs.monthly_3d_diags.fields)
save_hdf5(
cs.comms_ctx,
cs.monthly_3d_diags.fields,
cs.dates.date[1],
COUPLER_OUTPUT_DIR,
name_tag = "3d_",
),
map(x -> x .= FT(0), cs.monthly_3d_diags.fields),
cs.monthly_3d_diags.ct .= FT(0),
) cs.dates.date[1] cs.dates.date1[1]
@calendar_callback :(
map(x -> x ./= cs.monthly_2d_diags.ct[1], cs.monthly_2d_diags.fields),
)
map(x -> x .= FT(0), cs.monthly_3d_diags.fields)
cs.monthly_3d_diags.ct .= FT(0)
end

if cs.dates.date[1] >= cs.dates.date1[1]
map(x -> x ./= cs.monthly_2d_diags.ct[1], cs.monthly_2d_diags.fields)
save_hdf5(
cs.comms_ctx,
cs.monthly_2d_diags.fields,
cs.dates.date[1],
COUPLER_OUTPUT_DIR,
name_tag = "2d_",
),
map(x -> x .= FT(0), cs.monthly_2d_diags.fields),
cs.monthly_2d_diags.ct .= FT(0),
) cs.dates.date[1] cs.dates.date1[1]
)
map(x -> x .= FT(0), cs.monthly_2d_diags.fields)
cs.monthly_2d_diags.ct .= FT(0)
end

end

Expand Down Expand Up @@ -415,7 +417,9 @@ function solve_coupler!(cs)
end

## step to the next calendar month
@calendar_callback :(cs.dates.date1[1] += Dates.Month(1)) cs.dates.date[1] cs.dates.date1[1]
if cs.dates.date[1] >= cs.dates.date1[1]
cs.dates.date1[1] += Dates.Month(1)
end

end
@show walltime
Expand Down
16 changes: 8 additions & 8 deletions experiments/ClimaCore/sea_breeze/Manifest.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This file is machine-generated - editing it directly is not advised

julia_version = "1.8.2"
julia_version = "1.8.3"
manifest_format = "2.0"
project_hash = "fa48b7601e1a6a64eebe6fc08aa62a04b4cc12b9"

Expand Down Expand Up @@ -156,7 +156,7 @@ uuid = "052768ef-5323-5732-b1bb-66c8b64840ba"
version = "3.12.0"

[[deps.Cairo_jll]]
deps = ["Artifacts", "Bzip2_jll", "Fontconfig_jll", "FreeType2_jll", "Glib_jll", "JLLWrappers", "LZO_jll", "Libdl", "Pixman_jll", "Pkg", "Xorg_libXext_jll", "Xorg_libXrender_jll", "Zlib_jll", "libpng_jll"]
deps = ["Artifacts", "Bzip2_jll", "CompilerSupportLibraries_jll", "Fontconfig_jll", "FreeType2_jll", "Glib_jll", "JLLWrappers", "LZO_jll", "Libdl", "Pixman_jll", "Pkg", "Xorg_libXext_jll", "Xorg_libXrender_jll", "Zlib_jll", "libpng_jll"]
git-tree-sha1 = "4b859a208b2397a7a623a03449e4636bdb17bcf2"
uuid = "83423d85-b0ee-5818-9007-b63ccbeb887a"
version = "1.16.1+1"
Expand Down Expand Up @@ -613,9 +613,9 @@ version = "0.1.0"

[[deps.Glib_jll]]
deps = ["Artifacts", "Gettext_jll", "JLLWrappers", "Libdl", "Libffi_jll", "Libiconv_jll", "Libmount_jll", "PCRE2_jll", "Pkg", "Zlib_jll"]
git-tree-sha1 = "fb83fbe02fe57f2c068013aa94bcdf6760d3a7a7"
git-tree-sha1 = "d3b3624125c1474292d0d8ed0f65554ac37ddb23"
uuid = "7746bdde-850d-59dc-9ae8-88ece973131d"
version = "2.74.0+1"
version = "2.74.0+2"

[[deps.Graphite2_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"]
Expand Down Expand Up @@ -893,9 +893,9 @@ version = "1.42.0+0"

[[deps.Libiconv_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"]
git-tree-sha1 = "42b62845d70a619f063a7da093d995ec8e15e778"
git-tree-sha1 = "c7cb1f5d892775ba13767a87c7ada0b980ea0a71"
uuid = "94ce4f54-9a6c-5748-9c1c-f9c7231a4531"
version = "1.16.1+1"
version = "1.16.1+2"

[[deps.Libmount_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"]
Expand Down Expand Up @@ -1522,9 +1522,9 @@ version = "0.12.2"

[[deps.TempestRemap_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "NetCDF_jll", "OpenBLAS32_jll", "Pkg"]
git-tree-sha1 = "27e81d7684e47875688952432d178d7f45130482"
git-tree-sha1 = "88c3818a492ad1a94b1aa440b01eab5d133209ff"
uuid = "8573a8c5-1df0-515e-a024-abad257ee284"
version = "2.1.6+0"
version = "2.1.6+1"

[[deps.TensorCore]]
deps = ["LinearAlgebra"]
Expand Down
25 changes: 12 additions & 13 deletions src/BCReader.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ monthly to daily intervals.
=#
module BCReader

using ..Utilities, ..Regridder, ..CallbackManager
using ..Utilities, ..Regridder, ..TimeManager
using ClimaCore: Fields
using ClimaComms
using Dates
Expand Down Expand Up @@ -65,7 +65,8 @@ BCFileInfo's land_mask without scaling.
- `field`: [Fields.Field] contains the values to be remapped.
- `bcf_info`: [BCFileInfo] contains a land_mask to remap onto the space of.
"""
no_scaling(field::Fields.Field, bcf_info::BCFileInfo) = Utilities.swap_space!(field, axes(bcf_info.land_mask))
no_scaling(field::Fields.Field, bcf_info::BCFileInfo{FT}) where {FT} =
Utilities.swap_space!(field, axes(bcf_info.land_mask))

"""
bcfile_info_init(
Expand Down Expand Up @@ -144,8 +145,8 @@ function bcfile_info_init(
[
argmin(
abs.(
parse(FT, CallbackManager.datetime_to_strdate(date0)) .-
parse.(FT, CallbackManager.datetime_to_strdate.(data_dates[:]))
parse(FT, TimeManager.datetime_to_strdate(date0)) .-
parse.(FT, TimeManager.datetime_to_strdate.(data_dates[:]))
),
),
]
Expand All @@ -168,7 +169,7 @@ end

# IO - monthly
"""
update_midmonth_data!(date, bcf_info)
update_midmonth_data!(date, bcf_info::BCFileInfo{FT}) where {FT}
Extracts boundary condition data from regridded (to model grid) NetCDF files.
The times for which data is extracted depends on the specifications in the
Expand All @@ -178,10 +179,9 @@ The times for which data is extracted depends on the specifications in the
- `date`: [Dates.DateTime] start date for data.
- `bcf_info`: [BCFileInfo] containing boundary condition data.
"""
function update_midmonth_data!(date, bcf_info::BCFileInfo)
function update_midmonth_data!(date, bcf_info::BCFileInfo{FT}) where {FT}
# monthly count
(; bcfile_dir, comms_ctx, hd_outfile_root, varname, all_dates, scaling_function) = bcf_info
FT = float_type_bcf(bcf_info)
midmonth_idx = bcf_info.segment_idx[1]
midmonth_idx0 = bcf_info.segment_idx0[1]

Expand Down Expand Up @@ -215,8 +215,8 @@ function update_midmonth_data!(date, bcf_info::BCFileInfo)
elseif Dates.days(date - all_dates[Int(midmonth_idx + 1)]) > 2
nearest_idx = argmin(
abs.(
parse(FT, CallbackManager.datetime_to_strdate(date)) .-
parse.(FT, CallbackManager.datetime_to_strdate.(all_dates[:]))
parse(FT, TimeManager.datetime_to_strdate(date)) .-
parse.(FT, TimeManager.datetime_to_strdate.(all_dates[:]))
),
)
# TODO test this
Expand Down Expand Up @@ -256,24 +256,23 @@ return the same value unless `segment_idx` is modified elsewhere in between.
# Returns
- Dates.DateTime
"""
next_date_in_file(bcf_info::BCFileInfo) = bcf_info.all_dates[bcf_info.segment_idx[1] + Int(1)]
next_date_in_file(bcf_info::BCFileInfo{FT}) where {FT} = bcf_info.all_dates[bcf_info.segment_idx[1] + Int(1)]

# IO - daily
"""
interpolate_midmonth_to_daily(FT, date, bcf_info::BCFileInfo)
interpolate_midmonth_to_daily(date, bcf_info::BCFileInfo{FT}) where {FT}
Interpolates linearly between two `Fields` in the `bcf_info` struct,
or returns the first Field if interpolation is switched off.
# Arguments
- `FT`: [DataType] Float type.
- `date`: [Dates.DateTime] start date for data.
- `bcf_info`: [BCFileInfo] contains fields to be interpolated.
# Returns
- Fields.field
"""
function interpolate_midmonth_to_daily(FT, date, bcf_info::BCFileInfo)
function interpolate_midmonth_to_daily(date, bcf_info::BCFileInfo{FT}) where {FT}
if bcf_info.interpolate_daily && bcf_info.segment_length[1] > FT(0)
(; segment_length, segment_idx, all_dates, monthly_fields) = bcf_info

Expand Down
43 changes: 0 additions & 43 deletions src/CallbackManager.jl

This file was deleted.

2 changes: 1 addition & 1 deletion src/ClimaCoupler.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ 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("CallbackManager.jl")
include("BCReader.jl")


Expand Down
3 changes: 2 additions & 1 deletion src/Regridder.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ via ClimaCoreTempestRemap wrappers.
module Regridder

using ClimaCoupler.Utilities
using ClimaCoupler.TimeManager
using ClimaCore: Meshes, Domains, Topologies, Spaces, Fields, InputOutput
using ClimaComms
using NCDatasets
Expand Down Expand Up @@ -132,7 +133,7 @@ function hdwrite_regridfile_rll_to_cgll(
if "time" in ds
data_dates = Dates.DateTime.(ds["time"][:])
elseif "date" in ds
data_dates = strdate_to_datetime.(string.(ds["date"][:]))
data_dates = TimeManager.strdate_to_datetime.(string.(ds["date"][:]))
else
@warn "No dates available in file $datafile_rll"
data_dates = [Dates.DateTime(0)]
Expand Down
Loading

0 comments on commit 7949bd4

Please sign in to comment.