Class: RPowerFlow::System
- Inherits:
-
Object
- Object
- RPowerFlow::System
- Defined in:
- lib/rpowerflow/system.rb
Overview
TODO: logging!
Instance Method Summary collapse
- #<<(*args) ⇒ Object
- #[]=(*args) ⇒ Object
-
#branches(bus = nil) ⇒ Object
TODO: check for required accessors.
-
#buses(branch = nil) ⇒ Object
TODO: check for required accessors.
-
#initialize(args = Hash.new) ⇒ System
constructor
A new instance of System.
- #jacobian(print = false) ⇒ Object
- #print_jacobian ⇒ Object
- #print_ybus ⇒ Object
- #size ⇒ Object
- #ybus ⇒ Object
Constructor Details
#initialize(args = Hash.new) ⇒ System
Returns a new instance of System.
33 34 35 36 |
# File 'lib/rpowerflow/system.rb', line 33 def initialize(args = Hash.new) @buses = Hash.new @branches = Hash.new end |
Instance Method Details
#<<(*args) ⇒ Object
38 39 40 41 42 43 44 45 |
# File 'lib/rpowerflow/system.rb', line 38 def <<(*args) args.flatten.each do |bus| @buses[bus] = Array.new end @bus_array = nil @size = nil end |
#[]=(*args) ⇒ Object
47 48 49 50 51 52 53 |
# File 'lib/rpowerflow/system.rb', line 47 def []=(*args) branch = args.pop @branches[branch] = args.flatten @branch_array = nil @ybus = nil end |
#branches(bus = nil) ⇒ Object
TODO: check for required accessors
69 70 71 72 73 74 75 |
# File 'lib/rpowerflow/system.rb', line 69 def branches(bus = nil) if bus return @buses[bus] else return @branch_array ||= @branches.keys end end |
#buses(branch = nil) ⇒ Object
TODO: check for required accessors
60 61 62 63 64 65 66 |
# File 'lib/rpowerflow/system.rb', line 60 def buses(branch = nil) if branch return @branches[branch] else return @bus_array ||= @buses.keys.sort { |a,b| a.id <=> b.id } end end |
#jacobian(print = false) ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/rpowerflow/system.rb', line 105 def jacobian(print = false) if defined?(Linalg) jac = Linalg::DMatrix.new(size * 2, size * 2) else jac = NMatrix.float(size * 2, size * 2) end (0...size).each do |i| source = buses[i] (0...size).each do |j| target = buses[j] if block_given? mw = source.voltage * ybus[i][j].abs * target.voltage * Math.cos(source.angle - target.angle - ybus[i][j].arg) mvar = source.voltage * ybus[i][j].abs * target.voltage * Math.sin(source.angle - target.angle - ybus[i][j].arg) # Yield the index of the current bus and # the incremental Mw and Mvar values. yield i, mw, mvar end if source.slack if i == j jac[i,i] = 1e+10 jac[i + size,i + size] = 1e+10 # jac[i,i] = 1.0 # jac[i + size,i + size] = 1.0 else jac[i,j] = 0.0 jac[i,j + size] = 0.0 jac[j,i] = 0.0 jac[j + size,i] = 0.0 jac[i + size,j] = 0.0 jac[i + size,j + size] = 0.0 jac[j,i + size] = 0.0 jac[j + size,i + size] = 0.0 end else if i == j (0...size).each do |k| jac[i,i] += ybus[i][k].abs * buses[k].voltage * Math.sin(source.angle - buses[k].angle - ybus[i][k].arg) unless i == k if source.avr jac[i + size,i + size] = 1e+10 # jac[i + size,i + size] = 1.0 else jac[i,i + size] += ybus[i][k].abs * buses[k].voltage * Math.cos(source.angle - buses[k].angle - ybus[i][k].arg) jac[i + size,i] += ybus[i][k].abs * buses[k].voltage * Math.cos(source.angle - buses[k].angle - ybus[i][k].arg) unless i == k jac[i + size,i + size] += ybus[i][k].abs * buses[k].voltage * Math.sin(source.angle - buses[k].angle - ybus[i][k].arg) end end jac[i,i] *= -source.voltage unless source.avr jac[i,i + size] += source.voltage * ybus[i][i].abs * Math.cos(ybus[i][i].arg) jac[i + size,i] *= source.voltage jac[i + size,i + size] += -source.voltage * ybus[i][i].abs * Math.sin(ybus[i][i].arg) end else unless target.slack jac[i,j] = source.voltage * ybus[i][j].abs * target.voltage * Math.sin(source.angle - target.angle - ybus[i][j].arg) if source.avr jac[i,j + size] = source.voltage * ybus[i][j].abs * Math.cos(source.angle - target.angle - ybus[i][j].arg) unless target.avr else unless target.avr jac[i,j + size] = source.voltage * ybus[i][j].abs * Math.cos(source.angle - target.angle - ybus[i][j].arg) jac[i + size,j] = -source.voltage * ybus[i][j].abs * target.voltage * Math.cos(source.angle - target.angle - ybus[i][j].arg) jac[i + size,j + size] = source.voltage * ybus[i][j].abs * Math.sin(source.angle - target.angle - ybus[i][j].arg) end end end end end end end return jac end |
#print_jacobian ⇒ Object
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
# File 'lib/rpowerflow/system.rb', line 205 def print_jacobian jac = jacobian (0...size * 2).each do |i| format = Array.new data = Array.new (0...size * 2).each do |j| format << "%.2f" data << jac[i,j] end puts format.join("\t") % data puts end end |
#print_ybus ⇒ Object
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/rpowerflow/system.rb', line 187 def print_ybus (0...size).each do |i| format = Array.new data = Array.new (0...size).each do |j| element = ybus[i][j] format << "%.1f+%.1fj" data << element.real data << element.imag end puts format.join("\t") % data puts end end |
#size ⇒ Object
55 56 57 |
# File 'lib/rpowerflow/system.rb', line 55 def size return @size ||= buses.length end |
#ybus ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/rpowerflow/system.rb', line 77 def ybus return @ybus unless @ybus.nil? @ybus = Array.new(size) { Array.new(size, 0.0) } # TODO: account for transformers branches.each do |branch| impedance = Complex(branch.resistance, branch.reactance) charging = Complex(0, branch.charging) mini = Array.new(2) { Array.new } mini[0][0] = (1 / impedance) + charging mini[0][1] = -(1 / impedance) mini[1][0] = mini[0][1] mini[1][1] = mini[0][0] endpoints = buses(branch) source = buses.index(endpoints.first) target = buses.index(endpoints.last) @ybus[source][source] += mini[0][0] @ybus[source][target] += mini[0][1] @ybus[target][source] += mini[1][0] @ybus[target][target] += mini[1][1] end return @ybus end |