algorithm.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. code AdaptedFMU:
  2. /*
  3. These maps will keep track of which rules execute and which don't
  4. */
  5. var in_condition_executed = empty map
  6. var out_condition_executed = empty map
  7. var fmu1 //FMU fmu1 ref
  8. var fmu2 //FMU fmu2 ref
  9. ...
  10. /*
  11. Keeps track of the last time that the FMU window did a step.
  12. */
  13. var time_last_fmu1
  14. var time_last_fmu2
  15. ...
  16. // parameters
  17. var param_p1 = c1
  18. ...
  19. // in vars
  20. var iv1 = init_iv1
  21. ...
  22. // out vars
  23. var ov1 = init_ov1
  24. ...
  25. // control vars
  26. var cv1 = init_cv1
  27. ...
  28. function instantiate()
  29. fmu1.instantiate()
  30. ...
  31. return
  32. end function
  33. function setup_experiment(t,...)
  34. fmu1.setup_experiment(t,...)
  35. time_last_fmu1 = t
  36. ...
  37. return
  38. end function
  39. function enter_init()
  40. fmu1.enter_init()
  41. ...
  42. return
  43. end function
  44. function exit_init()
  45. fmu1.exit_init()
  46. ...
  47. return
  48. end function
  49. function fmi2SetValues(ports, values)
  50. /*
  51. Evaluates each condition (and sa_in rule) in order.
  52. */
  53. /*
  54. TODO: First eval all conditions, and only then run the blocks.
  55. */
  56. if (in_condition_1) then
  57. in_condition_executed[IN_COND_1] = true
  58. <sa_in_1>
  59. <if AdaptedFMU is mealy then>
  60. {// new scope
  61. /*
  62. These variables are available in the scope of the update_in block.
  63. */
  64. var h = 0
  65. var dt = 0
  66. <update_in_1>
  67. }
  68. <end if>
  69. end if
  70. ...
  71. out_condition_executed := empty map // force output computation.
  72. end function
  73. function do_inner_step(fmu, t, inner_t, h)
  74. /*
  75. Evaluate each update_in block of the rules that were successfully evaluated before the doStep was called.
  76. This is because the window.doStep will be called immediately afterward.
  77. */
  78. if (in_condition_executed[IN_COND_1]) then
  79. /*
  80. These variables are available in the scope of the update_in block.
  81. It's not a good idea to have t also in that scope, because it is not available when getValues/setValues is called.
  82. */
  83. var h = h
  84. var dt = inner_t - t
  85. <update_in_1>
  86. end if
  87. if (in_condition_executed[IN_COND_2]) then
  88. var h = h
  89. var dt = inner_t - t
  90. <update_in_2>
  91. end if
  92. ...
  93. fmi2DoStep(fmu1, inner_t, h)
  94. /*
  95. Executes the update_out blocks.
  96. These always execute after a doStep is called on an internal FMU.
  97. */
  98. /*
  99. TODO: First eval all conditions, and only then run the blocks.
  100. */
  101. if (<out_condition_1>) then
  102. out_condition_executed[OUT_COND_1] = true
  103. var h = h
  104. var dt = inner_t - t
  105. <update_out_1>
  106. end if
  107. ...
  108. end function
  109. function fmi2DoStep(t, H)
  110. /*
  111. A new doStep means that a new set of conditions will be triggered.
  112. */
  113. out_condition_executed = empty map
  114. /*
  115. Calculate the elapsed time since the last transition
  116. */
  117. var elapsed_fmu1 = t - time_last_fmu1
  118. ...
  119. var e = min(elapsed_fmu1, ...)
  120. <control_block_part_1>
  121. do_inner_step(fmu1, t, inner_t, h);
  122. <control_block_part_2>
  123. in_condition_executed = empty map;
  124. end function
  125. function fmi2GetValues(ports)
  126. var values = empty map;
  127. /*
  128. TODO: First eval all conditions, and only then run the blocks.
  129. */
  130. if out_condition_executed == empty map then
  131. // This can happen since the adapted unit is a mealy machine
  132. // So execute the update_out blocks
  133. if (<out_condition_1>) then
  134. out_condition_executed[OUT_COND_1] = true
  135. var h = 0
  136. var dt = 0
  137. <update_out_1>
  138. end if
  139. ...
  140. end if
  141. // Execute the sa_out blocks
  142. if out_condition_executed[OUT_COND_1] then
  143. <sa_out_1>
  144. end if
  145. ...
  146. return values;
  147. end function