@@ -31,6 +31,13 @@ export time_over, time
3131
3232
3333# ########### Clock ############
34+ """
35+ Clock
36+
37+ # Arguments
38+ max_t: maximum (minimum) terminal time for forward (backward) integration,
39+ respectively.
40+ """
3441mutable struct Clock
3542 t:: Float64
3643 dt:: Float64
@@ -51,8 +58,8 @@ function init!(clock::Clock, dt, ode_step_len; max_t=10.0)
5158 return clock
5259end
5360
54- function reset! (clock:: Clock )
55- clock. t = 0.0
61+ function reset! (clock:: Clock ; t = 0.0 )
62+ clock. t = t
5663 return clock
5764end
5865
7279" Check if the time is larger than max_t."
7380function time_over (clock:: Clock ; t= nothing )
7481 if t == nothing
75- return time (clock) >= clock. max_t
82+ return sign (clock . dt) * ( time (clock) - clock. max_t) >= 0.0
7683 else
77- return t >= clock. max_t
84+ return sign (clock . dt) * (t - clock. max_t) >= 0.0
7885 end
7986end
8087
8188function thist (clock:: Clock )
8289 thist = clock. thist .+ time (clock)
8390 if time_over (clock, t= thist[end ])
84- index = findfirst (thist .> clock. max_t)
91+ index = findfirst (sign (clock. dt)* (thist .- clock. max_t) .> 0.0 )
92+ # index = findfirst(thist .> clock.max_t)
8593 if index == nothing
8694 return thist
8795 else
106114
107115function _show (sys:: BaseSystem ; i= 0 )
108116 result = []
109- for symbol in [:name , :state , :dot , :initial_state ,
110- :state_size , :flat_index ]
111- if isdefined (sys, symbol)
112- value = getproperty (sys, symbol)
113- else
114- value = " undef"
115- end
116- if symbol == :name
117- space = " +---"
118- else
119- space = " | "
120- end
121- push! (result,
122- _add_space (" $(String (symbol)) : $value " , i, space= space))
123- end
117+ _stack_property! (result, sys,
118+ [:name , :state , :dot , :initial_state ,
119+ :state_size , :flat_index ], i= i)
124120 return join (result, " \n " )
125121end
126122
@@ -169,6 +165,7 @@ mutable struct BaseEnv
169165 dyn
170166 step
171167
168+ initial_time
172169 dt:: Float64
173170 clock:: Clock
174171 progressbar
@@ -191,9 +188,29 @@ function _add_space(string, i; space=" "^4)
191188 return space^ i * string
192189end
193190
191+ function _stack_property! (result, sys, symbol_array; i= 0 , space= " " )
192+ for symbol in symbol_array
193+ if isdefined (sys, symbol)
194+ value = getproperty (sys, symbol)
195+ else
196+ value = " undef"
197+ end
198+ if symbol == :name
199+ space = " +---"
200+ else
201+ space = " | "
202+ end
203+ push! (result, _add_space (" $(String (symbol)) : $value " , i, space= space))
204+ # push!(result, _add_space("$(String(symbol)): $value", 0))
205+ end
206+ end
207+
194208function _show (env:: BaseEnv ; i= 0 )
195209 result = []
196- push! (result, _add_space (" name: $(env. name) " , i, space= " +---" ))
210+ _stack_property! (result, env, [:name ]) # from env
211+ if i == 0
212+ _stack_property! (result, env. clock, [:max_t , :dt ]) # from env.clock
213+ end
197214 for system in _systems (env)
198215 if typeof (system) == BaseSystem
199216 v_str = _show (system, i= i+ 1 )
@@ -213,15 +230,16 @@ end
213230function init! (env:: BaseEnv ;
214231 systems= Dict (), dyn= nothing , step= nothing ,
215232 # params=Dict(),
216- dt= 0.01 , max_t= 1.0 , ode_step_len= 1 ,
233+ initial_time= 0.0 , dt= 0.01 , max_t= 1.0 ,
234+ ode_step_len= 1 ,
217235 logger= nothing , ode_option= Dict (), solver= " rk4" ,
218236 name= nothing ,
219237 )
220238 env. name = name
221239 systems! (env, systems)
222240
223241 env. clock = Clock (dt, ode_step_len, max_t= max_t)
224- env. dt = env . clock . dt
242+ env. initial_time = initial_time
225243
226244 env. logger = logger
227245
@@ -279,7 +297,7 @@ function reset!(env::BaseEnv)
279297 for system in _systems (env)
280298 reset! (system)
281299 end
282- reset! (env. clock)
300+ reset! (env. clock, t = env . initial_time )
283301end
284302
285303function state (env:: BaseEnv )
0 commit comments