Class: Bodhi::Simulator

Inherits:
Object
  • Object
show all
Includes:
Properties, Validations
Defined in:
lib/bodhi-slam/simulator.rb

Constant Summary

Constants included from Properties

Properties::SYSTEM_PROPERTIES

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Validations

#errors, included, #invalid?, #valid?, #validate!

Methods included from Properties

#attributes, #id, included, #initialize, #new_record?, #persisted?, #to_json, #update_attributes

Class Method Details

.execute(settings, &block) ⇒ Object

Run a new simulation using the given settings and &block Yields the simulation.current_time to the user defined &block Ignores current_frame settings and zero’s them before the simulation

Bodhi::Simulator.execute(starts_at: "2016-05-10", iterations: 10, time_units: "days") do |current_time|
  # do some simulation stuff!
end

# You can even go crazy and do things like:
Bodhi::Simulator.execute(...) do |outer_time|
  # do some simulation stuff!
  Bodhi::Simulator.execute(...) do |inner_time|
    # do a nested simulation!
  end
  # do more stuff after the netsted simulation...
end


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/bodhi-slam/simulator.rb', line 46

def self.execute(settings, &block)
  simulation = Bodhi::Simulator.new(settings)

  if simulation.invalid?
    raise ArgumentError.new("Invalid settings: #{simulation.errors.to_a}")
  end

  if simulation.current_frame.nil?
    simulation.current_frame = Bodhi::SimulationFrame.new(time: simulation.starts_at)
  else
    simulation.current_frame.iteration = 0
    simulation.current_frame.time = simulation.starts_at
  end

  until simulation.complete?
    yield simulation.current_frame.clone
    simulation.increment
  end
end

Instance Method Details

#complete?Boolean

returns true if the simulation has processed all iterations

Returns:

  • (Boolean)


71
72
73
# File 'lib/bodhi-slam/simulator.rb', line 71

def complete?
  self.current_frame.iteration == self.iterations
end

#incrementObject

Increments the simulation loop by one iteration Updates current_time to the new time offset Updates current_iteration to the next iteration

simulation = Bodhi::Simulator.new(starts_at: "2016-05-13", iterations: 2, time_units: "days")
simulation.increment
simulation #=> #<Bodhi::Simulator @starts_at: "2016-05-13" @iterations: 2, @time_units: "days", @current_time: "2016-05-14", @current_iteration: 1 >


83
84
85
86
# File 'lib/bodhi-slam/simulator.rb', line 83

def increment
  self.current_frame.iteration += 1
  self.current_frame.time = self.starts_at + (self.current_frame.iteration * 1.send(self.time_units) * self.time_scale)
end