algorithm.c 2.9 KB

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