@@ -88,8 +88,13 @@ Trigger `f!(integrator)` every `Δt` simulation time.
88
88
89
89
If `atinit=true`, then `f!` will additionally be triggered at initialization. Otherwise
90
90
the first trigger will be after `Δt` simulation time.
91
+
92
+ If `call_at_end==true`, then `f!` will be triggered at the end of the time span. Otherwise
93
+ there is no call to `f!` at the end of the time span.
94
+
95
+ The tuple `save_positions` determines whether to save before or after `f!`.
91
96
"""
92
- function EveryXSimulationTime (f!, Δt; atinit = false )
97
+ function EveryXSimulationTime (f!, Δt; atinit = false , call_at_end = false , save_positions = ( true , true ) )
93
98
t_next = zero (Δt)
94
99
95
100
function _initialize (c, u, t, integrator)
@@ -111,14 +116,22 @@ function EveryXSimulationTime(f!, Δt; atinit = false)
111
116
t_next += Δt
112
117
end
113
118
return true
119
+ elseif (call_at_end && t == integrator. sol. prob. tspan[2 ])
120
+ return true
114
121
else
115
122
return false
116
123
end
117
124
end
118
125
if isdefined (DiffEqBase, :finalize! )
119
- SciMLBase. DiscreteCallback (condition, f!; initialize = _initialize, finalize = _finalize)
126
+ SciMLBase. DiscreteCallback (
127
+ condition,
128
+ f!;
129
+ initialize = _initialize,
130
+ finalize = _finalize,
131
+ save_positions = save_positions,
132
+ )
120
133
else
121
- SciMLBase. DiscreteCallback (condition, f!; initialize = _initialize)
134
+ SciMLBase. DiscreteCallback (condition, f!; initialize = _initialize, save_positions = save_positions )
122
135
end
123
136
end
124
137
@@ -131,8 +144,13 @@ Trigger `f!(integrator)` every `Δsteps` simulation steps.
131
144
132
145
If `atinit==true`, then `f!` will additionally be triggered at initialization. Otherwise
133
146
the first trigger will be after `Δsteps`.
147
+
148
+ If `call_at_end==true`, then `f!` will be triggered at the end of the time span. Otherwise
149
+ there is no call to `f!` at the end of the time span.
150
+
151
+ The tuple `save_positions` determines whether to save before or after `f!`.
134
152
"""
135
- function EveryXSimulationSteps (f!, Δsteps; atinit = false )
153
+ function EveryXSimulationSteps (f!, Δsteps; atinit = false , call_at_end = false , save_positions = ( true , true ) )
136
154
steps = 0
137
155
steps_next = 0
138
156
@@ -154,15 +172,23 @@ function EveryXSimulationSteps(f!, Δsteps; atinit = false)
154
172
if steps >= steps_next
155
173
steps_next += Δsteps
156
174
return true
175
+ elseif (call_at_end && t == integrator. sol. prob. tspan[2 ])
176
+ return true
157
177
else
158
178
return false
159
179
end
160
180
end
161
181
162
182
if isdefined (DiffEqBase, :finalize! )
163
- SciMLBase. DiscreteCallback (condition, f!; initialize = _initialize, finalize = _finalize)
183
+ SciMLBase. DiscreteCallback (
184
+ condition,
185
+ f!;
186
+ initialize = _initialize,
187
+ finalize = _finalize,
188
+ save_positions = save_positions,
189
+ )
164
190
else
165
- SciMLBase. DiscreteCallback (condition, f!; initialize = _initialize)
191
+ SciMLBase. DiscreteCallback (condition, f!; initialize = _initialize, save_positions = save_positions )
166
192
end
167
193
end
168
194
0 commit comments