Class: RangesMerger

Inherits:
Object
  • Object
show all
Extended by:
RangesMergerDivider, RangesMergerDividerEqInterval, RangesMergerExclusion, RangesMergerMerging, RangesMergerUtils
Defined in:
lib/ranges_merger.rb,
lib/ranges_merger/ranges_merger_energy_calculation.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from RangesMergerMerging

merge

Methods included from RangesMergerUtils

array_to_ranges, normalize_array

Methods included from RangesMergerExclusion

exclude, two_way_exclusion

Constructor Details

#initialize(_array = []) ⇒ RangesMerger

Returns a new instance of RangesMerger.



17
18
19
20
21
22
# File 'lib/ranges_merger.rb', line 17

def initialize(_array = [])
  @total_min = nil
  @ranges = self.class.merge(
    self.class.normalize_array(_array)
  )
end

Instance Attribute Details

#rangesObject (readonly)

Returns the value of attribute ranges.



15
16
17
# File 'lib/ranges_merger.rb', line 15

def ranges
  @ranges
end

Class Method Details

.energy_calculation(voltages, currents, _debug = false) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/ranges_merger/ranges_merger_energy_calculation.rb', line 3

def self.energy_calculation(voltages, currents, _debug = false)
  return 0 if voltages.size == 0 or currents.size == 0

  # sort time ranges
  voltages = voltages.sort { |a, b| a[0] <=> b[0] }
  currents = currents.sort { |a, b| a[0] <=> b[0] }
  # without checking of overlaps

  # for loop
  voltage_i = 0
  current_i = 0

  # output
  energy = 0.0

  voltage_first = true
  voltage_first = false if currents[0][0] < voltages[0][0]

  while voltage_i < voltages.size and current_i < currents.size
    v = voltages[voltage_i]
    c = currents[current_i]

    # common time range
    _time_from = v[0]
    _time_from = c[0] if c[0] > v[0]

    _time_to = v[1]
    _time_to = c[1] if c[1] < v[1]

    # adding to output
    if _time_to > _time_from
      _energy_part = v[2] * c[2] * (_time_to - _time_from)
    else
      # it should not enter here... probably
      _energy_part = 0.0
    end
    energy += _energy_part

    puts "#{voltage_i}, #{current_i} = #{_energy_part}, #{v.inspect}, #{c.inspect}" if _debug

    # next from array
    if c[1] > v[1]
      voltage_i += 1
    else
      current_i += 1
    end

  end

  #currents = [
  #  [1, 2] => 1,
  #  [2, 3] => 2
  #]
  #
  #voltages = [
  #  [1, 2] => 2,
  #  [2, 3] => 1
  #]


  start_time = voltages

  return energy
end

Instance Method Details

#%(interval) ⇒ Object



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

def %(interval)
  divide(interval, false)
end

#+(_array) ⇒ Object



30
31
32
# File 'lib/ranges_merger.rb', line 30

def +(_array)
  add(_array)
end

#-(_array) ⇒ Object



39
40
41
# File 'lib/ranges_merger.rb', line 39

def -(_array)
  remove(_array)
end

#/(interval) ⇒ Object



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

def /(interval)
  divide(interval, true)
end

#add(_array) ⇒ Object



24
25
26
27
28
# File 'lib/ranges_merger.rb', line 24

def add(_array)
  register_min(_array)
  @ranges = self.class.merge(@ranges + _array)
  return self
end

#divide(interval, allow_partials = true) ⇒ Object



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

def divide(interval, allow_partials = true)
  return self.class.divide(@ranges, interval, allow_partials)
end

#divide_eqi(interval, total_min = @total_min) ⇒ Object



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

def divide_eqi(interval, total_min = @total_min)
  return self.class.divide_eqi(@ranges, interval, total_min)
end

#remove(_array) ⇒ Object



34
35
36
37
# File 'lib/ranges_merger.rb', line 34

def remove(_array)
  @ranges = self.class.exclude(@ranges, _array)
  return self
end

#to_arrayObject



63
64
65
# File 'lib/ranges_merger.rb', line 63

def to_array
  @ranges
end

#to_rangesObject



59
60
61
# File 'lib/ranges_merger.rb', line 59

def to_ranges
  self.class.array_to_ranges(@ranges)
end