Skip to content

Commit b018c6e

Browse files
glwagnernavidcy
andauthored
initialize! for ReactantSimulation plus better find_time_index (#4215)
* Add support for Nothing callbacks, output writers, diagnostics * Add a test * Bump * Bugfix * Extend initialize! for ReactantSimulation * Add find_time_index for StepRangeLen * Silly reordering * Try to fix compile problem * Revert changes to Simulation construcotr * Bump version * Fix * Make linear extrapolation work * Uncomment tests * Fix time step import --------- Co-authored-by: Navid C. Constantinou <[email protected]>
1 parent b4176ce commit b018c6e

File tree

5 files changed

+36
-23
lines changed

5 files changed

+36
-23
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "Oceananigans"
22
uuid = "9e8cae18-63c1-5223-a75c-80ca9d6e9a09"
33
authors = ["Climate Modeling Alliance and contributors"]
4-
version = "0.95.27"
4+
version = "0.95.28"
55

66
[deps]
77
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"

ext/OceananigansReactantExt/OceananigansReactantExt.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ using .Fields
2222
include("TimeSteppers.jl")
2323
using .TimeSteppers
2424

25+
include("TurbulenceClosures.jl")
26+
using .TurbulenceClosures
27+
2528
include("Models.jl")
2629
using .Models
2730

2831
include("Simulations/Simulations.jl")
2932
using .Simulations
3033

31-
include("TurbulenceClosures.jl")
32-
using .TurbulenceClosures
33-
3434
#####
3535
##### Telling Reactant how to construct types
3636
#####

ext/OceananigansReactantExt/Simulations/run.jl

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import ..TimeSteppers: first_time_step!
1+
import Oceananigans: initialize!
2+
import Oceananigans.TimeSteppers: first_time_step!
23

34
""" Step `sim`ulation forward by one time step. """
45
time_step!(sim::ReactantSimulation) = time_step!(sim.model, sim.Δt)
@@ -18,3 +19,11 @@ function time_step_for!(sim::ReactantSimulation, Nsteps)
1819
return nothing
1920
end
2021

22+
function initialize!(sim::ReactantSimulation)
23+
model = sim.model
24+
initialize!(model)
25+
update_state!(model)
26+
sim.initialized = true
27+
return nothing
28+
end
29+

src/OutputReaders/field_time_series_indexing.jl

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,24 +68,23 @@ end
6868
##### fine_time_index
6969
#####
7070

71-
#=
7271
@inline function find_time_index(times::StepRangeLen, t)
7372
n₂ = searchsortedfirst(times, t)
74-
n₁ = max(1, n₂ - 1)
7573

7674
Nt = length(times)
7775
n₂ = min(Nt, n₂) # cap
76+
n₁ = max(1, n₂ - 1)
7877

7978
@inbounds begin
8079
t₁ = times[n₁]
8180
t₂ = times[n₂]
8281
end
8382

8483
= (t - t₁) / (t₂ - t₁)
84+
= ifelse(n₂ == n₁, zero(ñ), ñ)
8585

8686
return ñ, n₁, n₂
8787
end
88-
=#
8988

9089
@inline function find_time_index(times, t)
9190
Nt = length(times)

test/test_output_readers.jl

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ include("dependencies_for_runtests.jl")
22

33
using Oceananigans.Utils: Time
44
using Oceananigans.Fields: indices, interpolate!
5-
using Oceananigans.OutputReaders: Cyclical, Clamp
5+
using Oceananigans.OutputReaders: Cyclical, Clamp, Linear
66

77
function generate_some_interesting_simulation_data(Nx, Ny, Nz; architecture=CPU())
88
grid = RectilinearGrid(architecture, size=(Nx, Ny, Nz), extent=(64, 64, 32))
@@ -485,9 +485,9 @@ end
485485

486486
sinf(t) = sin(2π * t / 3)
487487

488-
f = CenterField(grid)
489488
fts = FieldTimeSeries{Center, Center, Center}(grid, times; backend=OnDisk(), path=filepath_sine, name="f")
490-
489+
490+
f = CenterField(grid)
491491
for (i, time) in enumerate(fts.times)
492492
set!(f, (x, y, z) -> sinf(time))
493493
set!(fts, f, i)
@@ -496,9 +496,9 @@ end
496496
# Now we load the FTS partly in memory
497497
# using different time indexing strategies
498498
M = 5
499-
fts1 = FieldTimeSeries(filepath_sine, "f"; backend = InMemory(M))
500-
fts2 = FieldTimeSeries(filepath_sine, "f"; backend = InMemory(M), time_indexing = Cyclical())
501-
fts3 = FieldTimeSeries(filepath_sine, "f"; backend = InMemory(M), time_indexing = Clamp())
499+
fts_lin = FieldTimeSeries(filepath_sine, "f"; backend = InMemory(M), time_indexing = Linear())
500+
fts_cyc = FieldTimeSeries(filepath_sine, "f"; backend = InMemory(M), time_indexing = Cyclical())
501+
fts_clp = FieldTimeSeries(filepath_sine, "f"; backend = InMemory(M), time_indexing = Clamp())
502502

503503
# Test that linear interpolation is correct within the time domain
504504
for time in 0:0.01:last(fts.times)
@@ -509,24 +509,29 @@ end
509509

510510
Δt⁺ = (time - t⁻) / (t⁺ - t⁻)
511511

512-
@test fts1[Time(time)][1, 1, 1] (sinf(t⁻) * (1 - Δt⁺) + sinf(t⁺) * Δt⁺)
513-
@test fts2[Time(time)][1, 1, 1] (sinf(t⁻) * (1 - Δt⁺) + sinf(t⁺) * Δt⁺)
514-
@test fts3[Time(time)][1, 1, 1] (sinf(t⁻) * (1 - Δt⁺) + sinf(t⁺) * Δt⁺)
512+
@test fts_lin[Time(time)][1, 1, 1] (sinf(t⁻) * (1 - Δt⁺) + sinf(t⁺) * Δt⁺)
513+
@test fts_cyc[Time(time)][1, 1, 1] (sinf(t⁻) * (1 - Δt⁺) + sinf(t⁺) * Δt⁺)
514+
@test fts_clp[Time(time)][1, 1, 1] (sinf(t⁻) * (1 - Δt⁺) + sinf(t⁺) * Δt⁺)
515515
end
516516
end
517517

518-
Δt = fts.times[end] - fts.times[end - 1]
519-
520518
# Test that the time interpolation is correct outside the time domain
521-
for time in last(fts.times) + 1 : 0.01 : last(fts.times) * 2
522-
@test fts1[Time(time)][1, 1, 1] (fts1[end][1, 1, 1] - fts1[end-1][1, 1, 1]) / Δt * (time - last(fts.times))
523-
@test fts3[Time(time)][1, 1, 1] fts3[end][1, 1, 1]
519+
Δt = fts.times[end] - fts.times[end-1]
520+
Tf = last(fts.times)
521+
from = Tf+1
522+
to = 2Tf
523+
524+
for t in from:0.01:to
525+
dfdt = (fts_lin[end][1, 1, 1] - fts_lin[end-1][1, 1, 1]) / Δt
526+
extrapolated = (t - Tf) * dfdt
527+
@test fts_lin[Time(t)][1, 1, 1] extrapolated
528+
@test fts_clp[Time(t)][1, 1, 1] fts_clp[end][1, 1, 1]
524529
end
525-
526530
end
527531

528532
rm(filepath1d)
529533
rm(filepath2d)
530534
rm(filepath3d)
531535
rm(filepath_sine)
532536
end
537+

0 commit comments

Comments
 (0)