Class: RedShift::RK4DifferentialFlow

Inherits:
Flow show all
Defined in:
lib/redshift/component.rb,
lib/redshift/target/c/flow/rk4.rb

Constant Summary

Constants inherited from Flow

Flow::CT_STRUCT_NAME

Instance Attribute Summary

Attributes inherited from Flow

#formula, #strict, #var

Instance Method Summary collapse

Methods inherited from Flow

#external_constant?, #initialize, #make_ct_struct, #translate, #translate_link

Constructor Details

This class inherits a constructor from RedShift::Flow

Instance Method Details

#flow_wrapper(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
# File 'lib/redshift/target/c/flow/rk4.rb', line 2

def flow_wrapper cl, state
  var_name = @var
  flow = self
  
  flow_name = "flow_#{CGenerator.make_c_name cl.name}_#{var_name}_#{state}"
  
  Component::FlowWrapper.make_subclass flow_name do
    @inspect_str = "#{cl.name}:#{state}: #{var_name} = #{flow.formula}"

    ssn = cl.shadow_struct.name
    cont_state_ssn = cl.cont_state_class.shadow_struct.name
    
    # We need the struct
    shadow_library_source_file.include(cl.shadow_library_include_file)
    
    shadow_library_source_file.define(flow_name).instance_eval do
      arguments "ComponentShadow *comp_shdw"
      declare :shadow => %{
        struct #{ssn} *shadow;
        struct #{cont_state_ssn} *cont_state;
        ContVar  *var;
        double    ddt_#{var_name};
        double    value_4;
        double    time_step;
      }
      setup :shadow => %{
        shadow = (#{ssn} *)comp_shdw;
        cont_state = (#{cont_state_ssn} *)shadow->cont_state;
        var = &cont_state->#{var_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
      body %{
        switch (shadow->world->rk_level) {
        case 0:
          #{flow.translate(self, "ddt_#{var_name}", 0, cl).join("
          ")};
          var->value_1 = var->value_0 + ddt_#{var_name} * time_step/2;
          break;

        case 1:
          #{flow.translate(self, "ddt_#{var_name}", 1, cl).join("
          ")};
          var->value_2 = var->value_0 + ddt_#{var_name} * time_step/2;
          break;

        case 2:
          #{flow.translate(self, "ddt_#{var_name}", 2, cl).join("
          ")};
          var->value_3 = var->value_0 + ddt_#{var_name} * time_step;
          break;

        case 3:
          #{flow.translate(self, "ddt_#{var_name}", 3, cl).join("
          ")};
          value_4 = var->value_0 + ddt_#{var_name} * time_step;
          var->value_0 = 
            (-3*var->value_0 + 2*var->value_1 + 4*var->value_2 +
              2*var->value_3 + value_4) / 6;
          break;

        default:
          rb_raise(#{declare_class RuntimeError},
            "Bad rk_level, %d!", shadow->world->rk_level);
        }
        
        shadow->world->rk_level++;
        var->rk_level = shadow->world->rk_level;
      }
    end

    define_c_method :calc_function_pointer do
      body "shadow->flow = &#{flow_name}"
    end
  end
end