fmus.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. from fmpy.fmi2 import fmi2True
  2. from cosimlibrary.virtual_fmus import VirtualFMU
  3. class MSD1(VirtualFMU):
  4. def __init__(self, instanceName):
  5. ref = 0
  6. self.x = ref;
  7. ref += 1
  8. self.v = ref;
  9. ref += 1
  10. self.m = ref;
  11. ref += 1
  12. self.c = ref;
  13. ref += 1
  14. self.cf = ref;
  15. ref += 1
  16. self.fe = ref;
  17. ref += 1
  18. super().__init__(instanceName, ref)
  19. def reset(self):
  20. super().reset()
  21. self.state[self.x] = 1.0
  22. self.state[self.v] = 0.0
  23. self.state[self.m] = 1.0
  24. self.state[self.c] = 1.0
  25. self.state[self.cf] = 1.0
  26. self.state[self.fe] = 0.0
  27. def doStep(self, currentCommunicationPoint, communicationStepSize, noSetFMUStatePriorToCurrentPoint=fmi2True):
  28. n = 10
  29. h = communicationStepSize / n
  30. for i in range(n):
  31. der_x = self.state[self.v]
  32. der_v = (1.0 / self.state[self.m]) * (- self.state[self.c] * self.state[self.x]
  33. - self.state[self.cf] * self.state[self.v]
  34. + self.state[self.fe])
  35. self.state[self.x] = self.state[self.x] + der_x * h
  36. self.state[self.v] = self.state[self.v] + der_v * h
  37. class MSD2(VirtualFMU):
  38. def __init__(self, instanceName):
  39. ref = 0
  40. self.x = ref;
  41. ref += 1
  42. self.v = ref;
  43. ref += 1
  44. self.m = ref;
  45. ref += 1
  46. self.c = ref;
  47. ref += 1
  48. self.cf = ref;
  49. ref += 1
  50. self.ce = ref;
  51. ref += 1
  52. self.cef = ref;
  53. ref += 1
  54. self.fe = ref;
  55. ref += 1
  56. self.xe = ref;
  57. ref += 1
  58. self.ve = ref;
  59. ref += 1
  60. super().__init__(instanceName, ref)
  61. def reset(self):
  62. super().reset()
  63. self.state[self.x] = 0.0
  64. self.state[self.v] = 0.0
  65. self.state[self.m] = 1.0
  66. self.state[self.c] = 1.0
  67. self.state[self.cf] = 0.0
  68. self.state[self.ce] = 1.0
  69. self.state[self.cef] = 1.0
  70. self.state[self.fe] = 0.0
  71. self.state[self.xe] = 0.0
  72. self.state[self.ve] = 0.0
  73. def doStep(self, currentCommunicationPoint, communicationStepSize, noSetFMUStatePriorToCurrentPoint=fmi2True):
  74. n = 10
  75. h = communicationStepSize / n
  76. for i in range(n):
  77. self.state[self.fe] = self.state[self.ce] * (self.state[self.x] - self.state[self.xe]) \
  78. + self.state[self.cef] * (self.state[self.v] - self.state[self.ve])
  79. der_x = self.state[self.v]
  80. der_v = (1.0 / self.state[self.m]) * (- self.state[self.c] * self.state[self.x]
  81. - self.state[self.cf] * self.state[self.v]
  82. - self.state[self.fe])
  83. self.state[self.x] = self.state[self.x] + der_x * h
  84. self.state[self.v] = self.state[self.v] + der_v * h