-
Couldn't load subscription status.
- Fork 250
Enable AveragedSpecifiedTimes to be used in output writers
#4876
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
base: main
Are you sure you want to change the base?
Conversation
AveragedSpecifiedTimes to be used in output writers
…dation for overlapping windows
… improve time sorting
|
Apart from writing some tests I think this is now ready for review. For a script like: using Oceananigans
using Oceananigans.OutputWriters: AveragedSpecifiedTimes
using Printf
using Random
arch = CPU()
grid = RectilinearGrid(arch, size=(4, 4, 4), extent=(1, 1, 1))
model = NonhydrostaticModel(grid=grid)
simulation = Simulation(model, Δt=0.0001, stop_time=1)
times = [0.1, 0.25, 0.5, 0.6, 1]
window = [0.1, 0.05, 0.2, 0.1, 0.3]
true_u = vcat([0], times .- window ./ 2)
# window = 0.1
perm = randperm(length(times))
times .= times[perm]
window .= window[perm]
schedule = AveragedSpecifiedTimes(times; window)
wall_time = Ref(time_ns())
function progress(sim)
msg = @sprintf("iter: %d, time: %s, Δt: %.4f",
iteration(sim), prettytime(time(sim)), sim.Δt)
elapsed = 1e-9 * (time_ns() - wall_time[])
msg *= @sprintf(", max u: %6.3e, max v: %6.3e, max w: %6.3e, wall time: %s",
maximum(sim.model.velocities.u),
maximum(sim.model.velocities.v),
maximum(sim.model.velocities.w),
prettytime(elapsed))
@info msg
wall_time[] = time_ns()
return nothing
end
function set_synthetic_values!(sim)
t = time(sim)
set!(sim.model, u=t, v=0, w=0)
return nothing
end
simulation.callbacks[:progress] = Callback(progress, IterationInterval(1000))
simulation.callbacks[:set_synthetic_values] = Callback(set_synthetic_values!, IterationInterval(1))
simulation.output_writers[:jld2] = JLD2Writer(model, model.velocities;
filename = "averagedspecified_times",
schedule,
overwrite_existing = true)
run!(simulation)
u = FieldTimeSeries("averagedspecified_times.jld2", "u")
@info "Recorded times: ", u.times
@info "Recorded u: ", interior(u, 1, 1, 1, :)
@info "Expected u: ", true_uit gives an output of [ Info: ("Recorded times: ", [0.0, 0.1, 0.25, 0.5, 0.6, 1.0])
[ Info: ("Recorded u: ", [0.0, 0.049949999898672104, 0.22495000064373016, 0.3999499976634979, 0.5499500036239624, 0.8499500155448914])
[ Info: ("Expected u: ", [0.0, 0.05, 0.225, 0.4, 0.5499999999999999, 0.85])Now if window = 0.1we get results which are [ Info: ("Recorded times: ", [0.0, 0.1, 0.25, 0.5, 0.6, 1.0])
[ Info: ("Recorded u: ", [0.0, 0.049949999898672104, 0.19994999468326569, 0.4499500095844269, 0.5499500036239624, 0.9499499797821045])
[ Info: ("Expected u: ", [0.0, 0.05, 0.2, 0.45, 0.5499999999999999, 0.95])which are "correct". These answers are approximate as the I will write tests like this if people think this is good enough of when we can come up with tests that return correct answers exactly. Now in terms of edge cases. When the windows overlap e.g. (note that the 4th window overlaps with the 3rd time point times = [0.1, 0.25, 0.5, 0.6, 1]
window = [0.1, 0.05, 0.2, 0.11, 0.3]we get an error of ERROR: ArgumentError: Averaging windows overlap. Ensure that for each specified time tᵢ, tᵢ - windowᵢ ≥ tᵢ₋₁.
Stacktrace:
[1] AveragedSpecifiedTimes(times::Vector{Float64}, window::Vector{Float64}; kw::@Kwargs{})
@ Oceananigans.OutputWriters c:\Users\xinle\Dropbox\MIT\main_oceananigans\Oceananigans.jl\src\OutputWriters\windowed_time_average.jl:130
[2] AveragedSpecifiedTimes
@ c:\Users\xinle\Dropbox\MIT\main_oceananigans\Oceananigans.jl\src\OutputWriters\windowed_time_average.jl:120 [inlined]
[3] #AveragedSpecifiedTimes#21
@ c:\Users\xinle\Dropbox\MIT\main_oceananigans\Oceananigans.jl\src\OutputWriters\windowed_time_average.jl:117 [inlined]
[4] top-level scope
@ c:\Users\xinle\Dropbox\MIT\main_oceananigans\Oceananigans.jl\test_averagedspecified_times.jl:23Similarly when window = 0.11we get ERROR: ArgumentError: Averaging window 0.11 is too large and causes overlapping windows. Ensure that for each specified time tᵢ, tᵢ - window ≥ tᵢ₋₁.
Stacktrace:
[1] AveragedSpecifiedTimes(times::Vector{Float64}, window::Float64; kw::@Kwargs{})
@ Oceananigans.OutputWriters c:\Users\xinle\Dropbox\MIT\main_oceananigans\Oceananigans.jl\src\OutputWriters\windowed_time_average.jl:135
[2] AveragedSpecifiedTimes
@ c:\Users\xinle\Dropbox\MIT\main_oceananigans\Oceananigans.jl\src\OutputWriters\windowed_time_average.jl:135 [inlined]
[3] #AveragedSpecifiedTimes#21
@ c:\Users\xinle\Dropbox\MIT\main_oceananigans\Oceananigans.jl\src\OutputWriters\windowed_time_average.jl:117 [inlined]
[4] top-level scope
@ c:\Users\xinle\Dropbox\MIT\main_oceananigans\Oceananigans.jl\test_averagedspecified_times.jl:23In order to use the type dispatch style on
to reroute the function to the right dispatch with window as positional argument.
Alternatively there is also a method which is currently commented out Oceananigans.jl/src/OutputWriters/windowed_time_average.jl Lines 140 to 160 in 6d16825
which checks the type of window in the function loop.
@glwagner @simone-silvestri @navidcy let me know what do you think and give suggestions on better model design! PS: also why is Oceananigans.jl/src/OutputWriters/windowed_time_average.jl Lines 107 to 112 in f07428b
Why was that choice made? |
| A schedule for averaging over windows that precede SpecifiedTimes. | ||
| """ | ||
| mutable struct AveragedSpecifiedTimes <: AbstractSchedule | ||
| mutable struct AveragedSpecifiedTimes{W <: Union{Float64, Vector{Float64}}} <: AbstractSchedule |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| mutable struct AveragedSpecifiedTimes{W <: Union{Float64, Vector{Float64}}} <: AbstractSchedule | |
| mutable struct AveragedSpecifiedTimes{W} <: AbstractSchedule |
| collecting :: Bool | ||
| end | ||
|
|
||
| const VaryingWindowAveragedSpecifiedTimes = AveragedSpecifiedTimes{Vector{Float64}} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| const VaryingWindowAveragedSpecifiedTimes = AveragedSpecifiedTimes{Vector{Float64}} | |
| const VaryingWindowAveragedSpecifiedTimes = AveragedSpecifiedTimes{<:Vector} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think you want to limit to Float64. That won't work with dates.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we relax AveragedTimeInterval as well?
Oceananigans.jl/src/OutputWriters/windowed_time_average.jl
Lines 15 to 22 in f07428b
| mutable struct AveragedTimeInterval <: AbstractSchedule | |
| interval :: Float64 | |
| window :: Float64 | |
| stride :: Int | |
| first_actuation_time :: Float64 | |
| actuations :: Int | |
| collecting :: Bool | |
| end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes! good catch. I can do that in another PR if you like
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in the above, interval and window have a "time period" type, while first_actuation_time has a "time" type. These are different when using dates.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is there currently a TimePeriod type? I can try to define one.
Should we also make a SpecifiedWindows like SpecifiedTimes in the case for AveragedSpecifiedTimes?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't want a specific type, because sometimes a "period" is Float64 (the assumption here), but with dates, it may need to be Dates.Second or Dates.Day (for example). However, a "time type" can be Float64 (the assumption here) or DateTime.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we relax it and make it parametric? or constrain it so that interval :: Union{Float64, Dates.Period}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, we need to use type parameters, and there need to be two of them.
We don't use the type union approach because it is not concrete; with that approach types can no longer be inferred.
…mprove overlap validation
…erval and first actuation time
| """ | ||
| mutable struct AveragedSpecifiedTimes{W <: Union{Float64, Vector{Float64}}} <: AbstractSchedule | ||
| mutable struct AveragedSpecifiedTimes{W} <: AbstractSchedule | ||
| specified_times :: SpecifiedTimes |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since SpecifiedTimes is not a concrete type, this should also be a type parameter
| function determine_epsilon(eltype) | ||
| if eltype <: AbstractFloat | ||
| return eps(eltype) | ||
| elseif eltype <: Period | ||
| return Second(0) | ||
| else | ||
| return 0 | ||
| end | ||
| end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| function determine_epsilon(eltype) | |
| if eltype <: AbstractFloat | |
| return eps(eltype) | |
| elseif eltype <: Period | |
| return Second(0) | |
| else | |
| return 0 | |
| end | |
| end | |
| determine_epsilon(eltype) = 0 | |
| determine_epsilon(eltype::AbstractType) = eps(eltype) | |
| determine_epsilon(::Period) = Second(0) |
the name of the function is slightly problematic
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you meant
Oceananigans.jl/src/OutputWriters/windowed_time_average.jl
Lines 120 to 122 in 0efdbc8
| determine_epsilon(eltype) = 0 | |
| determine_epsilon(::Type{T}) where T <: AbstractFloat = eps(T) | |
| determine_epsilon(::Period) = Second(0) |
right?
I agree the name is terrible, but I haven't figured out a better alternative...
| end | ||
|
|
||
| function AveragedSpecifiedTimes(times, window::Float64; kw...) | ||
| function AveragedSpecifiedTimes(times, window::Union{<:Number, <:Period}; kw...) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| function AveragedSpecifiedTimes(times, window::Union{<:Number, <:Period}; kw...) | |
| function AveragedSpecifiedTimes(times, window; kw...) |
can this be the fallback?
| """ | ||
| mutable struct AveragedSpecifiedTimes{W} <: AbstractSchedule | ||
| specified_times :: SpecifiedTimes | ||
| mutable struct AveragedSpecifiedTimes{S<:SpecifiedTimes, W} <: AbstractSchedule |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| mutable struct AveragedSpecifiedTimes{S<:SpecifiedTimes, W} <: AbstractSchedule | |
| mutable struct AveragedSpecifiedTimes{S, W} <: AbstractSchedule |
not that its bad, but we don't use this style right now in Oceananigans. Basically we want type restrictions to have a specific purpose
Closes #4875.
This PR aims to extend output writers so that we can pass in schedules which are
AveragedSpecifiedTimes(times::Array{<:Number}, window=w::Array{<:Number}, ...)into output writers.One compelling use case for this (at least for me) is to perform time averaging in
Yearunits when running realistic coupled simulations inClimaOcean.jl. For example, if I want to compute yearly averages over realistic years, each year would have different number of days, and the time interval between the end of each year is also not uniform. This calls forschedule = AveragedSpecifiedTimes(...), thus making this feature request a compelling one.