-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathCallbacks.jl
197 lines (159 loc) · 5.7 KB
/
Callbacks.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
"""
ClimaTimeSteppers.Callbacks
A suite of callback functions to be used with the ClimaTimeSteppers.jl ODE solvers.
"""
module Callbacks
import ClimaComms, DiffEqBase
import SciMLBase
"""
ClimaTimeSteppers.Callbacks.initialize!(f!::F, integrator)
Initialize a callback event for callbacks of type `F`. By default this does nothing, but
can be extended for new callback events.
"""
function initialize!(f!, integrator) end
"""
ClimaTimeSteppers.Callbacks.finalize!(f!::F, integrator)
Finalize a callback event for callbacks of type `F`. By default this does nothing, but
can be extended for new callback events.
"""
function finalize!(f!, integrator) end
export EveryXWallTimeSeconds, EveryXSimulationTime, EveryXSimulationSteps
"""
EveryXWallTimeSeconds(
f!,
Δwt,
comm_ctx::ClimaComms.AbstractCommsContext;
atinit=false
)
Trigger `f!(integrator)` every `Δwt` wallclock seconds.
An [ClimaComms context](https://clima.github.io/ClimaComms.jl/) must be provided to synchronize timing across all ranks.
[`Callbacks.initialize!`](@ref) and [`Callbacks.finalize!`](@ref) can be defined for `f!`.
If `atinit=true`, then `f!(integrator)` will additionally be triggered at initialization,
otherwise the first trigger will be after `Δwt` seconds.
"""
function EveryXWallTimeSeconds(f!, Δwt, comm_ctx::ClimaComms.AbstractCommsContext; atinit = false)
wt_next = 0.0
function _initialize(c, u, t, integrator)
wt = ClimaComms.allreduce(comm_ctx, time(), max)
wt_next = wt + Δwt
initialize!(c.affect!, integrator)
if atinit
c.affect!(integrator)
end
end
function _finalize(c, u, t, integrator)
finalize!(c.affect!, integrator)
end
function condition(u, t, integrator)
wt = ClimaComms.allreduce(comm_ctx, time(), max)
if wt >= wt_next
while wt >= wt_next
wt_next += Δwt
end
return true
else
return false
end
end
if isdefined(DiffEqBase, :finalize!)
SciMLBase.DiscreteCallback(condition, f!; initialize = _initialize, finalize = _finalize)
else
SciMLBase.DiscreteCallback(condition, f!; initialize = _initialize)
end
end
"""
EveryXSimulationTime(f!, Δt; atinit=false)
Trigger `f!(integrator)` every `Δt` simulation time.
[`Callbacks.initialize!`](@ref) and [`Callbacks.finalize!`](@ref) can be defined for `f!`.
If `atinit=true`, then `f!` will additionally be triggered at initialization. Otherwise
the first trigger will be after `Δt` simulation time.
If `call_at_end==true`, then `f!` will be triggered at the end of the time span. Otherwise
there is no guaranteed call to `f!` at the end of the time span.
The boolean tuple `save_positions` determines whether to save before or after `f!`.
"""
function EveryXSimulationTime(f!, Δt; atinit = false, call_at_end = false, save_positions = (true, true))
t_next = zero(Δt)
@assert Δt ≠ Inf "Adding callback that never gets called!"
function _initialize(c, u, t, integrator)
t_next = Δt
initialize!(c.affect!, integrator)
if atinit
c.affect!(integrator)
end
end
function _finalize(c, u, t, integrator)
finalize!(c.affect!, integrator)
end
function condition(u, t, integrator)
if t >= t_next
while t >= t_next
t_next += Δt
end
return true
elseif (call_at_end && t == integrator.sol.prob.tspan[2])
return true
else
return false
end
end
if isdefined(DiffEqBase, :finalize!)
SciMLBase.DiscreteCallback(
condition,
f!;
initialize = _initialize,
finalize = _finalize,
save_positions = save_positions,
)
else
SciMLBase.DiscreteCallback(condition, f!; initialize = _initialize, save_positions = save_positions)
end
end
"""
EveryXSimulationSteps(f!, Δsteps; atinit=false)
Trigger `f!(integrator)` every `Δsteps` simulation steps.
[`Callbacks.initialize!`](@ref) and [`Callbacks.finalize!`](@ref) can be defined for `f!`.
If `atinit==true`, then `f!` will additionally be triggered at initialization. Otherwise
the first trigger will be after `Δsteps`.
If `call_at_end==true`, then `f!` will be triggered at the end of the time span. Otherwise
there is no guaranteed call to `f!` at the end of the time span.
The boolean tuple `save_positions` determines whether to save before or after `f!`.
"""
function EveryXSimulationSteps(f!, Δsteps; atinit = false, call_at_end = false, save_positions = (true, true))
steps = 0
steps_next = 0
@assert Δsteps ≠ Inf "Adding callback that never gets called!"
function _initialize(c, u, t, integrator)
steps = 0
steps_next = Δsteps
initialize!(c.affect!, integrator)
if atinit
c.affect!(integrator)
end
end
function _finalize(c, u, t, integrator)
finalize!(c.affect!, integrator)
end
function condition(u, t, integrator)
steps += 1
if steps >= steps_next
steps_next += Δsteps
return true
elseif (call_at_end && t == integrator.sol.prob.tspan[2])
return true
else
return false
end
end
if isdefined(DiffEqBase, :finalize!)
SciMLBase.DiscreteCallback(
condition,
f!;
initialize = _initialize,
finalize = _finalize,
save_positions = save_positions,
)
else
SciMLBase.DiscreteCallback(condition, f!; initialize = _initialize, save_positions = save_positions)
end
end
end # module