windowSA.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. #include "WindowSA.h"
  2. namespace adaptation
  3. {
  4. WindowSA::WindowSA(shared_ptr<string> resourceLocation, const fmi2CallbackFunctions* functions) :
  5. SemanticAdaptation(resourceLocation, createInputRules(),createOutputRules(), functions)
  6. {
  7. this->stored_window_reaction_torque = 0.0;
  8. this->stored_window_height = 0.0;
  9. this->stored_windowsa_reaction_force = 0.0;
  10. this->stored_windowsa_displacement = 0.0;
  11. this->stored_windowsa_speed = 0.0;
  12. const char* path = Fmu::combinePath(resourceLocation, make_shared<string>("Window.fmu"))->c_str();
  13. auto windowFmu = make_shared<fmi2::Fmu>(path);
  14. windowFmu->initialize();
  15. this->window = windowFmu->instantiate("window",fmi2CoSimulation, "{efb4a002-4c0c-487b-8816-c0311d2f04d9}", true, true, make_shared<Callback>());
  16. }
  17. void WindowSA::initialize()
  18. {
  19. const char* path = Fmu::combinePath(resourceLocation, make_shared<string>("Window.fmu"))->c_str();
  20. auto windowFmu = make_shared<fmi2::Fmu>(*path);
  21. windowFmu->initialize();
  22. this->window = windowFmu->instantiate("window",fmi2CoSimulation, "{efb4a002-4c0c-487b-8816-c0311d2f04d9}", true, true, shared_from_this());
  23. if(this->window->component == NULL)
  24. this->lastErrorState = fmi2Fatal;
  25. this->instances->push_back(this->window);
  26. }
  27. WindowSA::~WindowSA()
  28. {
  29. }
  30. WindowSA* WindowSA::getRuleThis()
  31. {
  32. return this;
  33. }
  34. double WindowSA::getFmiValueReal(fmi2ValueReference id)
  35. {
  36. switch (id)
  37. {
  38. case WINDOWSADISP:
  39. {
  40. return this->disp;
  41. }
  42. case WINDOWSATAU:
  43. {
  44. return this->tau;
  45. }
  46. default:
  47. {
  48. return 0.0;
  49. }
  50. }
  51. }
  52. string WindowSA::getFmiValueString(fmi2ValueReference id)
  53. {
  54. return "";
  55. }
  56. int WindowSA::getFmiValueInteger(fmi2ValueReference id)
  57. {
  58. return 0;
  59. }
  60. bool WindowSA::getFmiValueBoolean(fmi2ValueReference id)
  61. {
  62. return false;
  63. }
  64. void WindowSA::setFmiValue(fmi2ValueReference id, double value)
  65. {
  66. switch (id)
  67. {
  68. case WINDOWSAREACTION_FORCE:
  69. {
  70. this->reaction_force = value;
  71. this->isSetreaction_force = true;
  72. break;
  73. }
  74. case WINDOWSADISPLACEMENT:
  75. {
  76. this->displacement = value;
  77. this->isSetdisplacement = true;
  78. break;
  79. }
  80. case WINDOWSASPEED:
  81. {
  82. this->speed = value;
  83. this->isSetspeed = true;
  84. break;
  85. }
  86. default:
  87. {
  88. }
  89. }
  90. }
  91. void WindowSA::setFmiValue(fmi2ValueReference id, string value)
  92. {
  93. }
  94. void WindowSA::setFmiValue(fmi2ValueReference id, int value)
  95. {
  96. }
  97. void WindowSA::setFmiValue(fmi2ValueReference id, bool value)
  98. {
  99. }
  100. bool WindowSA::in_rule_condition1(){
  101. return true;
  102. }
  103. void WindowSA::in_rule_body1(){
  104. if(this->isSetreaction_force){
  105. this->stored_windowsa_reaction_force = this->reaction_force;
  106. }
  107. }
  108. void WindowSA::in_rule_flush1(){
  109. setValue(window,WINDOWREACTION_FORCE,this->stored_windowsa_reaction_force);
  110. }
  111. bool WindowSA::in_rule_condition2(){
  112. return true;
  113. }
  114. void WindowSA::in_rule_body2(){
  115. if(this->isSetdisplacement){
  116. this->stored_windowsa_displacement = this->displacement;
  117. }
  118. }
  119. void WindowSA::in_rule_flush2(){
  120. setValue(window,WINDOWDISPLACEMENT,this->stored_windowsa_displacement);
  121. }
  122. bool WindowSA::in_rule_condition3(){
  123. return true;
  124. }
  125. void WindowSA::in_rule_body3(){
  126. if(this->isSetspeed){
  127. this->stored_windowsa_speed = this->speed;
  128. }
  129. }
  130. void WindowSA::in_rule_flush3(){
  131. setValue(window,WINDOWSPEED,this->stored_windowsa_speed);
  132. }
  133. shared_ptr<list<Rule<WindowSA>>> WindowSA::createInputRules()
  134. {
  135. auto list = make_shared<std::list<Rule<WindowSA>>>();
  136. list->push_back(
  137. (Rule<WindowSA>){
  138. &WindowSA::in_rule_condition1(),
  139. &WindowSA::in_rule_body1(),
  140. &WindowSA::in_rule_flush1()
  141. });
  142. list->push_back(
  143. (Rule<WindowSA>){
  144. &WindowSA::in_rule_condition2(),
  145. &WindowSA::in_rule_body2(),
  146. &WindowSA::in_rule_flush2()
  147. });
  148. list->push_back(
  149. (Rule<WindowSA>){
  150. &WindowSA::in_rule_condition3(),
  151. &WindowSA::in_rule_body3(),
  152. &WindowSA::in_rule_flush3()
  153. });
  154. return list;
  155. }
  156. void WindowSA::executeInternalControlFlow(double h, double dt)
  157. {
  158. this->do_step(window,h,dt);
  159. }
  160. bool WindowSA::out_rule_condition1(){
  161. return true;
  162. }
  163. void WindowSA::out_rule_body1(){
  164. this->stored_window_reaction_torque = getValueDouble(window,WINDOWREACTION_TORQUE);
  165. }
  166. void WindowSA::out_rule_flush1(){
  167. this->tau = this->stored_window_reaction_torque;
  168. }
  169. bool WindowSA::out_rule_condition2(){
  170. return true;
  171. }
  172. void WindowSA::out_rule_body2(){
  173. this->stored_window_height = getValueDouble(window,WINDOWHEIGHT);
  174. }
  175. void WindowSA::out_rule_flush2(){
  176. this->disp = this->stored_window_height * 100;
  177. }
  178. shared_ptr<list<Rule<WindowSA>>> WindowSA::createOutputRules()
  179. {
  180. auto list = make_shared<std::list<Rule<WindowSA>>>();
  181. list->push_back(
  182. (Rule<WindowSA>){
  183. &WindowSA::out_rule_condition1(),
  184. &WindowSA::out_rule_body1(),
  185. &WindowSA::out_rule_flush1()
  186. });
  187. list->push_back(
  188. (Rule<WindowSA>){
  189. &WindowSA::out_rule_condition2(),
  190. &WindowSA::out_rule_body2(),
  191. &WindowSA::out_rule_flush2()
  192. });
  193. return list;
  194. }
  195. }