Class: Simulator::Run

Inherits:
Object
  • Object
show all
Defined in:
lib/simulator/run.rb

Overview

A run of a model. It has its own context

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model) ⇒ Run

Returns a new instance of Run.



7
8
9
10
11
# File 'lib/simulator/run.rb', line 7

def initialize(model)
  @model = model
  @periods = [Period.new(self)]
  current_context.add_variables *@model.variables
end

Instance Attribute Details

#modelObject (readonly)

Returns the value of attribute model.



4
5
6
# File 'lib/simulator/run.rb', line 4

def model
  @model
end

#periodsObject (readonly)

Returns the value of attribute periods.



5
6
7
# File 'lib/simulator/run.rb', line 5

def periods
  @periods
end

Instance Method Details

#current_contextObject

Returns the current_context, that is the variable context of the last period.



43
44
45
# File 'lib/simulator/run.rb', line 43

def current_context
  @periods.last.context
end

#dataObject



90
91
92
93
94
# File 'lib/simulator/run.rb', line 90

def data
  data = Data.new
  data.periods = @periods
  data
end

#evaluateObject



13
14
15
16
17
18
19
20
21
# File 'lib/simulator/run.rb', line 13

def evaluate
  # evaluate the equations in the current context
  @model.equations.each do | eqtn |
    result = eqtn.evaluate_in current_context, @periods
    var = eqtn.variable
    bound_var = current_context.get(var.name)
    bound_var.value = result
  end
end

#increment_periodObject



23
24
25
26
27
# File 'lib/simulator/run.rb', line 23

def increment_period
  p = Period.new self, @periods.dup
  @periods << p
  p
end

#period_countObject

The number of periods so far. All runs have at least 1 period.



37
38
39
# File 'lib/simulator/run.rb', line 37

def period_count
  @periods.length
end

#set(var_hash) ⇒ Object



51
52
53
# File 'lib/simulator/run.rb', line 51

def set(var_hash)
  current_context.set var_hash
end

#step(times = 1) ⇒ Object



29
30
31
32
33
34
# File 'lib/simulator/run.rb', line 29

def step(times=1)
  times.times do
    evaluate
    increment_period
  end
end

#summaryObject



59
60
61
62
63
64
65
66
67
# File 'lib/simulator/run.rb', line 59

def summary
  var_list = variables.sort_by(&:name).collect do |v|
    "\t#{v.name} = #{v.value}"
  end.join("\n")
  model_summary = %Q{Model "#{@model.name}"}
  period_summary = "Period #{period_count}"

  "#{model_summary}\n#{period_summary}\nVariables:\n#{var_list}"
end

#variable_value(var_name) ⇒ Object



55
56
57
# File 'lib/simulator/run.rb', line 55

def variable_value(var_name)
  current_context.get(var_name).value
end

#variablesObject



47
48
49
# File 'lib/simulator/run.rb', line 47

def variables
  current_context.variables
end

#variables_csvObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/simulator/run.rb', line 69

def variables_csv
  vars = variables.sort_by(&:name)
  var_names = vars.collect(&:name)
  header = "'Period',#{var_names.collect{|n| "'#{n}'"}.join(',')}"

  rows = []
  @periods.each_index do |index|
    period = @periods[index]

    context = period.context
    var_values = var_names.collect do | var_name |
      var = context.get var_name
      var.value
    end.join(',')

    rows << "#{index},#{var_values}"
  end

  "#{header}\n#{rows.join("\n")}"
end