Class: RedShift::DerivativeFlow
- Defined in:
- lib/redshift/component.rb,
lib/redshift/target/c/flow/derivative.rb
Constant Summary
Constants inherited from Flow
Instance Attribute Summary collapse
-
#feedback ⇒ Object
readonly
Returns the value of attribute feedback.
Attributes inherited from Flow
#fname, #formula, #generator, #inspect_str, #strict, #var
Instance Method Summary collapse
-
#initialize(v, f, feedback) ⇒ DerivativeFlow
constructor
A new instance of DerivativeFlow.
- #make_generator(cl, state) ⇒ Object
Methods inherited from Flow
#become_generatable, #external_constant?, #generate, #inspect, #make_ct_struct, #translate, #translate_link, #wrapper
Constructor Details
#initialize(v, f, feedback) ⇒ DerivativeFlow
Returns a new instance of DerivativeFlow.
111 112 113 114 |
# File 'lib/redshift/component.rb', line 111 def initialize v, f, feedback super(v, f) @feedback = feedback end |
Instance Attribute Details
#feedback ⇒ Object (readonly)
Returns the value of attribute feedback.
110 111 112 |
# File 'lib/redshift/component.rb', line 110 def feedback @feedback end |
Instance Method Details
#make_generator(cl, state) ⇒ Object
2 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 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 |
# File 'lib/redshift/target/c/flow/derivative.rb', line 2 def make_generator cl, state @fname = "flow_#{CGenerator.make_c_name cl.name}_#{var}_#{state}" @inspect_str = "#{cl.name}:#{state}: #{var} = #{formula}" @generator = proc do sl = cl.shadow_library ssn = cl.shadow_struct_name cont_state_ssn = cl.cont_state_class.shadow_struct_name sl.init_library_function.body \ "s_init_flow(#{fname}, #{fname.inspect}, #{inspect_str.inspect}, NONALGEBRAIC);" include_file, source_file = sl.add_file fname # We need the struct source_file.include(cl.shadow_library_include_file) init_rhs_name = "#{var}_init_rhs" cl.class_eval do shadow_attr_accessor init_rhs_name => "double #{init_rhs_name}" end flow = self var_name = @var feedback = @feedback source_file.define(fname).instance_eval do arguments "ComponentShadow *comp_shdw" scope :extern declare :shadow => %{ struct #{ssn} *shadow; struct #{cont_state_ssn} *cont_state; ContVar *var; double antiddt, *scratch; double time_step; } setup :shadow => %{ shadow = (#{ssn} *)comp_shdw; cont_state = (struct #{cont_state_ssn} *)shadow->cont_state; var = &cont_state->#{var_name}; scratch = &shadow->#{init_rhs_name}; time_step = shadow->world->time_step; } setup :rk_level => %{ shadow->world->rk_level--; } # has to happen before referenced alg flows are called in other setups if feedback ## possible to unite these cases somehow? body %{ switch (shadow->world->rk_level) { case 0: #{flow.translate(self, "antiddt", cl, 0).join(" ")}; var->value[0] = var->value[1] = var->value[2] = var->value[3] = (antiddt - *scratch) / time_step; *scratch = antiddt; } shadow->world->rk_level++; var->rk_level = shadow->world->rk_level; } else body %{ #{flow.translate(self, "antiddt", cl).join(" ")}; switch (shadow->world->rk_level) { case 0: var->value[1] = var->value[0]; *scratch = antiddt; break; case 1: var->value[2] = (antiddt - *scratch) / (time_step/2); *scratch = antiddt; break; case 2: var->value[3] = (antiddt - *scratch) / (time_step/2); break; case 3: var->value[0] = (antiddt - *scratch) / (time_step/2); break; default: rb_raise(#{declare_class RuntimeError}, "Bad rk_level, %ld!", shadow->world->rk_level); } shadow->world->rk_level++; var->rk_level = shadow->world->rk_level; } end end end end |