Skip to content

Commit

Permalink
fix bug computing initial internal event
Browse files Browse the repository at this point in the history
  • Loading branch information
RomainFranceschini committed Apr 29, 2020
1 parent aa8ccf0 commit 631588a
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)

Expand All @@ -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
Expand Down
5 changes: 4 additions & 1 deletion src/quartz/multi_component/simulator.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand Down
5 changes: 4 additions & 1 deletion src/quartz/simulator.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 631588a

Please sign in to comment.