Class: RedShift::Flow
- Defined in:
- lib/redshift/component.rb,
lib/redshift/target/c/flow-gen.rb
Overview
rename to equation? formula? put in meta?
Direct Known Subclasses
AlgebraicFlow, CexprGuard, DelayFlow, DerivativeFlow, EulerDifferentialFlow, Expr, RK4DifferentialFlow
Constant Summary collapse
- CT_STRUCT_NAME =
"Context"
Class Attribute Summary collapse
-
.needed ⇒ Object
Returns the value of attribute needed.
Instance Attribute Summary collapse
-
#formula ⇒ Object
readonly
Returns the value of attribute formula.
-
#strict ⇒ Object
readonly
Strict flows change value only over continuous time, and not within the steps of the discrete update.
-
#var ⇒ Object
readonly
Returns the value of attribute var.
Instance Method Summary collapse
- #external_constant?(var) ⇒ Boolean
-
#initialize(v, f) ⇒ Flow
constructor
A new instance of Flow.
-
#make_ct_struct(flow_fn, cl) ⇒ Object
Since MSVC doesn’t support nested functions…
- #translate(flow_fn, result_var, rk_level, cl, orig_formula = nil) ⇒ Object
-
#translate_link(link, var, translation, flow_fn, cl, expr, rk_level) ⇒ Object
l.x ==> get_l__x()->value_n.
Constructor Details
#initialize(v, f) ⇒ Flow
Returns a new instance of Flow.
93 94 95 96 97 |
# File 'lib/redshift/component.rb', line 93 def initialize v, f @var, @formula = v, f @strict = true self.class.needed = true end |
Class Attribute Details
.needed ⇒ Object
Returns the value of attribute needed.
99 100 101 |
# File 'lib/redshift/component.rb', line 99 def needed @needed end |
Instance Attribute Details
#formula ⇒ Object (readonly)
Returns the value of attribute formula.
85 86 87 |
# File 'lib/redshift/component.rb', line 85 def formula @formula end |
#strict ⇒ Object (readonly)
Strict flows change value only over continuous time, and not within the steps of the discrete update. This can be false only for an AlgebraicFlow which depends on non-strict variables. In the algebraic case, a flow is strict iff the RHS of the eqn. has only strictly continuous variables.
91 92 93 |
# File 'lib/redshift/component.rb', line 91 def strict @strict end |
#var ⇒ Object (readonly)
Returns the value of attribute var.
85 86 87 |
# File 'lib/redshift/component.rb', line 85 def var @var end |
Instance Method Details
#external_constant?(var) ⇒ Boolean
143 144 145 |
# File 'lib/redshift/target/c/flow-gen.rb', line 143 def external_constant? var RedShift.library.external_constant? var end |
#make_ct_struct(flow_fn, cl) ⇒ Object
Since MSVC doesn’t support nested functions…
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/redshift/target/c/flow-gen.rb', line 150 def make_ct_struct(flow_fn, cl) sf = flow_fn.parent ct_struct = sf.declare![CT_STRUCT_NAME] if ct_struct ct_struct = ct_struct[1] ## hacky else ct_struct = sf.declare_struct(CT_STRUCT_NAME) ct_struct.declare :shadow => "#{cl.shadow_struct.name} *shadow" flow_fn.declare :ct => "#{CT_STRUCT_NAME} ct" flow_fn.setup :ct_shadow => "ct.shadow = shadow" end ct_struct end |
#translate(flow_fn, result_var, rk_level, cl, orig_formula = nil) ⇒ Object
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 67 68 69 70 71 72 73 74 75 76 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 104 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 |
# File 'lib/redshift/target/c/flow-gen.rb', line 11 def translate flow_fn, result_var, rk_level, cl, orig_formula = nil translation = {} setup = [] ## should use accumulator orig_formula ||= @formula c_formula = orig_formula.dup strict = true re = /(?:([A-Za-z_]\w*)\.)?([A-Za-z_]\w*)(?!\w*\s*[(])/ c_formula.gsub! re do |expr| unless translation[expr] link, var = $1, $2 if link result = translate_link(link, var, translation, flow_fn, cl, expr, rk_level) strict &&= result # Don't combine with method call!!! else # expr == 'var' varsym = var.intern link_type, link_strictness = cl.link_variables[varsym] if link_type # l ==> link_l strict &&= (link_strictness == :strict) link = var link_cname = "link_#{link}" unless translation[link] translation[link] = "ct.#{link_cname}" ct_struct = make_ct_struct(flow_fn, cl) link_type_ssn = link_type.shadow_struct.name ct_struct.declare link_cname => "#{link_type_ssn} *#{link_cname}" flow_fn.setup link_cname => "ct.#{link_cname} = shadow->#{link}" end elsif (kind = cl.constant_variables[varsym]) strict &&= (kind == :strict) translation[var] = "shadow->#{var}" elsif (kind = cl.continuous_variables[varsym]) # x ==> var_x strict &&= (kind == :strict) var_cname = "var_#{var}" sh_cname = "shadow" cs_cname = "cont_state" cont_var = "#{cs_cname}->#{var}" translation[var] = var_cname flow_fn.declare var_cname => "double #{var_cname}" flow_fn.setup var_cname => %{ if (#{cont_var}.algebraic) { if (#{cont_var}.rk_level < shadow->world->rk_level || (shadow->world->rk_level == 0 && (#{cont_var}.strict ? !#{cont_var}.d_tick : #{cont_var}.d_tick != shadow->world->d_tick) )) (*#{cont_var}.flow)((ComponentShadow *)#{sh_cname}); } else { #{cont_var}.d_tick = shadow->world->d_tick; } } # The d_tick assignment is explained in component-gen.rb. setup << "#{var_cname} = #{cont_var}.value_#{rk_level};" elsif (kind = cl.input_variables[varsym]) # x ==> var_x strict &&= (kind == :strict) # note that we check in Component#connect that the # source var is strict var_cname = "var_#{var}" translation[var] = var_cname src_comp = cl.src_comp(varsym) flow_fn.declare var_cname => "double #{var_cname}" flow_fn.setup var_cname => %{\ #{var_cname} = rs_eval_input_var(shadow, &shadow->#{src_comp}); } elsif (event_idx = cl.exported_events[varsym]) unless self.kind_of? ResetExpr raise NoValueError, "Event #{varsym.inspect} has no value in this context." end strict = false # strict doesn't matter var_cname = "event_#{var}" translation[var] = var_cname flow_fn.declare var_cname => "double #{var_cname}" flow_fn.setup var_cname => %{\ VALUE event_values, event_val; event_values = shadow->event_values; assert(event_values != Qnil); event_val = RARRAY(event_values)->ptr[#{event_idx}]; #{var_cname} = NUM2DBL(event_val); } elsif /\A[eE]\z/ =~ var translation[expr] = expr # scientific notation elsif cl.const_defined?(var) translation[expr] = cl.const_get(var) elsif external_constant?(var) translation[expr] = expr else raise NameError, "Unknown variable: #{var}" end end end translation[expr] end yield strict if block_given? ## funky way to return another value setup << "#{result_var} = (#{c_formula})" rescue NameError, ArgumentError => ex ex. << "\nclass: #{cl.name}\nformula:\n#{orig_formula}\n\n" raise ex end |
#translate_link(link, var, translation, flow_fn, cl, expr, rk_level) ⇒ Object
l.x ==> get_l__x()->value_n
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 |
# File 'lib/redshift/target/c/flow-gen.rb', line 170 def translate_link(link, var, translation, flow_fn, cl, expr, rk_level) link_type, link_strictness = cl.link_variables[link.intern] raise(NameError, "No such link, #{link}") unless link_type strict = (link_strictness == :strict) flow_fn.include link_type.shadow_library_include_file link_cname = "link_#{link}" get_var_cname = "get_#{link}__#{var}" ct_struct = make_ct_struct(flow_fn, cl) varsym = var.intern if (kind = link_type.constant_variables[varsym]) var_type = :constant elsif (kind = link_type.continuous_variables[varsym]) var_type = :continuous elsif (kind = link_type.input_variables[varsym]) var_type = :input elsif (a = link_type.link_variables[varsym]) var_type = :link link_link_type, kind = a elsif (event_idx = link_type.exported_events[varsym]) unless self.kind_of? ResetExpr raise NoValueError, "Event #{varsym.inspect} has no value in this context." end var_type = :event kind = false # strict doesn't matter else raise NameError, "Unknown variable: #{var} in #{link_type}" end strict &&= (kind == :strict) if var_type == :continuous or var_type == :input checked_var_cname = "checked_#{link}__#{var}" ## not quite unambig. ct_struct.declare checked_var_cname => "int #{checked_var_cname}" flow_fn.setup checked_var_cname => "ct.#{checked_var_cname} = 0" end if var_type == :continuous link_cs_ssn = link_type.cont_state_class.shadow_struct.name link_cs_cname = "link_cs_#{link}" ct_struct.declare link_cs_cname => "#{link_cs_ssn} *#{link_cs_cname}" end unless translation[link + "."] translation[link + "."] = true # just so we know we've handled it unless translation[link] translation[link] = link_cname link_type_ssn = link_type.shadow_struct.name ct_struct.declare link_cname => "#{link_type_ssn} *#{link_cname}" flow_fn.setup link_cname => "ct.#{link_cname} = shadow->#{link}" end ## same as below end sf = flow_fn.parent exc_nil = flow_fn.declare_class(NilLinkError) ## class var msg_nil = "Link #{link} is nil." case var_type when :continuous cs_cname = "ct->#{link_cs_cname}" cont_var = "#{cs_cname}->#{var}" sf.declare get_var_cname => %{ inline static ContVar *#{get_var_cname}(#{CT_STRUCT_NAME} *ct) { if (!ct->#{checked_var_cname}) { ct->#{checked_var_cname} = 1; if (!ct->#{link_cname}) rs_raise(#{exc_nil}, ct->shadow->self, #{msg_nil.inspect}); #{cs_cname} = (#{link_cs_ssn} *)ct->#{link_cname}->cont_state; if (#{cont_var}.algebraic) { if (#{cont_var}.rk_level < ct->shadow->world->rk_level || (ct->shadow->world->rk_level == 0 && (#{cont_var}.strict ? !#{cont_var}.d_tick : #{cont_var}.d_tick != ct->shadow->world->d_tick) )) (*#{cont_var}.flow)((ComponentShadow *)ct->#{link_cname}); } else { #{cont_var}.d_tick = ct->shadow->world->d_tick; } } return &(#{cont_var}); } } ## algebraic test is same as above translation[expr] = "#{get_var_cname}(&ct)->value_#{rk_level}" when :constant sf.declare get_var_cname => %{ inline static double #{get_var_cname}(#{CT_STRUCT_NAME} *ct) { if (!ct->#{link_cname}) rs_raise(#{exc_nil}, ct->shadow->self, #{msg_nil.inspect}); return ct->#{link_cname}->#{var}; } } translation[expr] = "#{get_var_cname}(&ct)" when :link link_link_type_ssn = link_link_type.shadow_struct.name sf.declare get_var_cname => %{ inline static #{link_link_type_ssn} *#{get_var_cname}(#{CT_STRUCT_NAME} *ct) { if (!ct->#{link_cname}) rs_raise(#{exc_nil}, ct->shadow->self, #{msg_nil.inspect}); return ct->#{link_cname}->#{var}; } } translation[expr] = "#{get_var_cname}(&ct)" when :input src_comp = link_type.src_comp(var.intern) result_name = "value_#{link}__#{var}" ct_struct.declare result_name => "double #{result_name}" sf.declare get_var_cname => %{ inline static double #{get_var_cname}(#{CT_STRUCT_NAME} *ct) { if (!ct->#{checked_var_cname}) { ct->#{checked_var_cname} = 1; if (!ct->#{link_cname}) rs_raise(#{exc_nil}, ct->shadow->self, #{msg_nil.inspect}); ct->#{result_name} = rs_eval_input_var(ct->#{link_cname}, &ct->#{link_cname}->#{src_comp}); } return ct->#{result_name}; } } translation[expr] = "#{get_var_cname}(&ct)" when :event sf.declare get_var_cname => %{ inline static double #{get_var_cname}(#{CT_STRUCT_NAME} *ct) { VALUE event_values, event_val; if (!ct->#{link_cname}) rs_raise(#{exc_nil}, ct->shadow->self, #{msg_nil.inspect}); event_values = ct->#{link_cname}->event_values; assert(event_values != Qnil); event_val = RARRAY(event_values)->ptr[#{event_idx}]; return NUM2DBL(event_val); } } translation[expr] = "#{get_var_cname}(&ct)" else raise ArgumentError, "Bad var_type: #{var_type.inspect}" end return strict end |