PowerwindowRequired.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. * PowerwindowRequired.c
  3. *
  4. * Created on: Jan 26, 2017
  5. * Author: Joachim
  6. */
  7. #include "PowerwindowRequired.h"
  8. #include <math.h>
  9. /*
  10. * I only check on the bigger than compare with the double
  11. * This should be changed
  12. */
  13. double relativeError(double a, double b){
  14. double rv = fabs((a - b) / a);
  15. return rv;
  16. }
  17. /*
  18. * Helper function for absolute error
  19. */
  20. double absoluteError(double a, double b){
  21. double rv=fabs(a - b);
  22. return rv;
  23. }
  24. /*
  25. * is_close function for double comparison
  26. */
  27. int is_close(double a, double b, double REL_TOL, double ABS_TOL){
  28. int ae = absoluteError(a,b)<ABS_TOL;
  29. int re = relativeError(a,b)<REL_TOL;
  30. return (ae && re);
  31. }
  32. void powerwindow_timeradvance(fmi_timer *theTimer, double currentTime){
  33. theTimer->currentTime = currentTime;
  34. if (theTimer->active && is_close(theTimer->nextTime, theTimer->currentTime,1e-4,1e-8)){
  35. /* We have a timer event */
  36. theTimer->callback(theTimer->handle, theTimer->evid);
  37. if(theTimer->isPeriodic){
  38. theTimer->nextTime = currentTime + theTimer->period;
  39. }else{
  40. theTimer->active = 0;
  41. }
  42. }
  43. }
  44. void powerwindow_initTimer(fmi_timer *timer){
  45. thePWTimer = timer;
  46. }
  47. void powerwindow_setTimer(Powerwindow* handle, const sc_eventid evid, const sc_integer time_ms, const sc_boolean periodic){
  48. thePWTimer->active = 1;
  49. thePWTimer->handle = handle;
  50. thePWTimer->evid = evid;
  51. thePWTimer->period = time_ms;
  52. thePWTimer->nextTime = thePWTimer->currentTime + time_ms;
  53. thePWTimer->isPeriodic = periodic;
  54. }
  55. void powerwindow_unsetTimer(Powerwindow* handle, const sc_eventid evid){
  56. thePWTimer->active = 0;
  57. }