From 631588a4a1f9683e7d91e52d3a3252549d5e9e6a Mon Sep 17 00:00:00 2001 From: Romain Franceschini Date: Wed, 29 Apr 2020 15:53:06 +0200 Subject: [PATCH] fix bug computing initial internal event --- ...me_spec.cr => sim_initial_elapsed_time_spec.cr} | 14 ++++++-------- src/quartz/multi_component/simulator.cr | 5 ++++- src/quartz/simulator.cr | 5 ++++- 3 files changed, 14 insertions(+), 10 deletions(-) rename spec/scenarios/{sim_neg_time_spec.cr => sim_initial_elapsed_time_spec.cr} (72%) diff --git a/spec/scenarios/sim_neg_time_spec.cr b/spec/scenarios/sim_initial_elapsed_time_spec.cr similarity index 72% rename from spec/scenarios/sim_neg_time_spec.cr rename to spec/scenarios/sim_initial_elapsed_time_spec.cr index 3ed666d..f937c64 100644 --- a/spec/scenarios/sim_neg_time_spec.cr +++ b/spec/scenarios/sim_initial_elapsed_time_spec.cr @@ -1,14 +1,13 @@ require "../spec_helper" -private module NegativeInitialTimeScenario +private module InitialElapsedTimeScenario class TestInitialElapsed < Quartz::AtomicModel getter output_calls : Int32 = 0 getter internal_calls : Int32 = 0 getter generated : Bool = false - getter elapsed_values : Array(Duration) = Array(Duration).new - # Set the initial elapsed time to 4 so that initial time may be negative - @elapsed = Duration.new(4) + # Set the initial elapsed time to 1 + @elapsed = Duration.new(1) def external_transition(bag) end @@ -24,13 +23,12 @@ private module NegativeInitialTimeScenario def internal_transition @generated = true @internal_calls += 1 - @elapsed_values << @elapsed end end describe TestInitialElapsed do describe "simulation" do - it "time might be negative at initialization" do + it "first event depends on initial elapsed time" do atom = TestInitialElapsed.new(:testneg) sim = Quartz::Simulation.new(atom, duration: Quartz::Duration::INFINITY) @@ -39,15 +37,15 @@ private module NegativeInitialTimeScenario atom.output_calls.should eq(0) atom.internal_calls.should eq(0) atom.generated.should be_false - atom.elapsed_values.empty?.should be_true + sim.virtual_time.to_i.should eq(0) sim.step + sim.virtual_time.to_i.should eq(1) atom.output_calls.should eq(1) atom.internal_calls.should eq(1) atom.generated.should be_true atom.time_advance.should eq(Quartz::Duration::INFINITY) - atom.elapsed_values.first.should eq(Duration.new(0)) end end end diff --git a/src/quartz/multi_component/simulator.cr b/src/quartz/multi_component/simulator.cr index a3b160d..597089a 100644 --- a/src/quartz/multi_component/simulator.cr +++ b/src/quartz/multi_component/simulator.cr @@ -69,7 +69,10 @@ module Quartz @components.each_value do |component| component.__initialize_state__(self) elapsed = component.elapsed - planned_duration = fixed_planned_duration(component.time_advance.as(Duration), component) + planned_duration = fixed_planned_duration( + component.time_advance.as(Duration) - elapsed, + component + ) Log.debug { String.build { |str| diff --git a/src/quartz/simulator.cr b/src/quartz/simulator.cr index 763c91a..4864a1c 100644 --- a/src/quartz/simulator.cr +++ b/src/quartz/simulator.cr @@ -37,7 +37,10 @@ module Quartz atomic.__initialize_state__(self) elapsed = atomic.elapsed - planned_duration = fixed_planned_duration(atomic.time_advance.as(Duration), atomic.class.precision_level) + planned_duration = fixed_planned_duration( + atomic.time_advance.as(Duration) - elapsed, + atomic.class.precision_level + ) if @run_validations && atomic.invalid?(:initialization) Log.error {