]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
6a4d7affdf4f18291b912a0408f3d6a7dd8674ee
[simantics/sysdyn.git] /
1 package org.simantics.sysdyn.tests.fmi;\r
2 \r
3 import java.util.Arrays;\r
4 import java.util.List;\r
5 \r
6 import junit.framework.Assert;\r
7 \r
8 import org.junit.Test;\r
9 import org.simantics.modelica.fmi.FMUControlJNI;\r
10 import org.simantics.modelica.fmi.FMUJNIException;\r
11 \r
12 public class MultipleSimultaneousFMUSimulationsTest {\r
13 \r
14 \r
15         /**\r
16          * This test runs simultaneously {@link FMURealTest}, {@link FMUIntegerTest} and {@link FMUBooleanTest} to\r
17          * see that they do not conflict with each other.\r
18          */\r
19 \r
20         @Test\r
21         public void testMultipleSimulations() {\r
22 \r
23                 FMUControlJNI control1 = new FMUControlJNI();\r
24                 FMUControlJNI control2 = new FMUControlJNI();\r
25                 FMUControlJNI control3 = new FMUControlJNI();\r
26 \r
27                 String path1 = ".\\Models\\FMUTests\\FMURealTest\\FMURealTestModel.fmu";\r
28                 String path2 = ".\\Models\\FMUTests\\FMUIntegerTest\\FMUIntegerTestModel.fmu";\r
29                 String path3 = ".\\Models\\FMUTests\\FMUBooleanTest\\FMUBooleanTestModel.fmu";  \r
30 \r
31                 try {\r
32                         control1.loadFMUFile(path1);\r
33                         control2.loadFMUFile(path2);\r
34                         control3.loadFMUFile(path3);\r
35 \r
36                         control1.setStepLength(0.1);\r
37                         control2.setStepLength(0.1);\r
38                         control3.setStepLength(0.1);\r
39 \r
40                         control1.initializeSimulation();\r
41                         control2.initializeSimulation();\r
42                         control3.initializeSimulation();\r
43 \r
44 \r
45                         String[] s1 = control1.getAllVariables();\r
46                         String[] s2 = control2.getAllVariables();\r
47                         String[] s3 = control3.getAllVariables();\r
48 \r
49 \r
50                         List<String> nameList1 = Arrays.asList(s1);\r
51                         List<String> nameList2 = Arrays.asList(s2);\r
52                         List<String> nameList3 = Arrays.asList(s3);\r
53                         int valve1 = nameList1.indexOf("valve");\r
54                         int valve2 = nameList2.indexOf("valve");\r
55                         int valve3 = nameList3.indexOf("valve");\r
56 \r
57                         int stock1 = nameList1.indexOf("stock");\r
58                         int stock2 = nameList2.indexOf("stock");\r
59                         int stock3 = nameList3.indexOf("stock");\r
60 \r
61                         control1.subscribe(s1);\r
62                         control2.subscribe(s2);\r
63                         control3.subscribe(s3);\r
64 \r
65 \r
66                         double value;\r
67                         double[] results1 = new double[s1.length];\r
68                         double[] results2 = new double[s2.length];\r
69                         double[] results3 = new double[s3.length];\r
70 \r
71                         for(int i = 0; i < 60; i++) {\r
72 \r
73                                 // Real\r
74                                 results1 = control1.getSubscribedResults(results1);\r
75 \r
76                                 if(i == 30) {\r
77                                         value = results1[stock1];\r
78                                         Assert.assertFalse("stock != 6 when time == 3. (stock == " + value + ")",  value < 5.999 || value > 6.001);\r
79                                         control1.setRealValue("r1", 3);\r
80                                 }\r
81 \r
82                                 value = results1[valve1];\r
83                                 if(i > 30) {\r
84                                         Assert.assertFalse("valve != 4 after time == 3. (valve == " + value + ")",  value < 3.999 || value > 4.001);\r
85                                 } else {\r
86                                         Assert.assertFalse("valve != 2 before time == 3. (valve == " + value + ")",  value < 1.999 || value > 2.001);\r
87 \r
88                                 }\r
89                                 control1.simulateStep();\r
90 \r
91                                 // Integer\r
92                                 results2 = control2.getSubscribedResults(results2);\r
93 \r
94                                 if(i == 30) {\r
95                                         value = results2[stock2];\r
96                                         Assert.assertFalse("stock != 6 when time == 3. (stock == " + value + ")",  value < 5.999 || value > 6.001);\r
97                                         control2.setIntegerValue("i1", 3);\r
98                                 }\r
99 \r
100                                 value = results2[valve2];\r
101                                 if(i > 30) {\r
102                                         Assert.assertFalse("valve != 4 after time == 3. (valve == " + value + ")",  value < 3.999 || value > 4.001);\r
103                                 } else {\r
104                                         Assert.assertFalse("valve != 2 before time == 3. (valve == " + value + ")",  value < 1.999 || value > 2.001);\r
105 \r
106                                 }\r
107                                 control2.simulateStep();\r
108 \r
109                                 // Boolean\r
110                                 results3 = control3.getSubscribedResults(results3);\r
111 \r
112                                 if(i == 30) {\r
113                                         value = results3[stock3];\r
114                                         Assert.assertFalse("stock != 3 when time == 3. (stock == " + value + ")",  value < 2.999 || value > 3.001);\r
115                                         control3.setBooleanValue("b", false);\r
116                                 }\r
117 \r
118                                 value = results3[valve3];\r
119                                 if(i > 30) {\r
120                                         Assert.assertFalse("valve != -1 after time == 3. (valve == " + value + ")",  value > -0.999 || value < -1.001);\r
121                                 } else {\r
122                                         Assert.assertFalse("valve != 1 before time == 3. (valve == " + value + ")",  value < 0.999 || value > 1.001);\r
123 \r
124                                 }\r
125                                 control3.simulateStep();\r
126 \r
127                         }\r
128 \r
129                         // Real\r
130                         results1 = control1.getSubscribedResults(results1);\r
131                         value = results1[stock1];\r
132                         Assert.assertFalse("stock != 18 when time == 6. (stock == " + value + ")",  value < 17.999 || value > 18.001);\r
133 \r
134                         // Integer\r
135                         results2 = control2.getSubscribedResults(results2);\r
136                         value = results2[stock2];\r
137                         Assert.assertFalse("stock != 18 when time == 6. (stock == " + value + ")",  value < 17.999 || value > 18.001);\r
138 \r
139                         // Boolean\r
140                         results3 = control3.getSubscribedResults(results3);\r
141                         value = results3[stock3];\r
142                         Assert.assertFalse("stock != 0 when time == 6. (stock == " + value + ")",  value < -0.001 || value > 0.001);\r
143 \r
144 \r
145                 } catch (FMUJNIException e) {\r
146                         Assert.fail("Exception in native fmu code: " + e.getMessage());\r
147                 }\r
148 \r
149                 try {\r
150                         control1.unloadFMU();\r
151                         control2.unloadFMU();\r
152                         control3.unloadFMU();\r
153                 } catch (FMUJNIException e) {\r
154                         Assert.fail("Exception in native fmu code: " + e.getMessage());\r
155                 }\r
156 \r
157         }\r
158 }\r
159 \r