--- /dev/null
+package org.simantics.sysdyn.tests.fmi;\r
+\r
+import java.util.Arrays;\r
+import java.util.List;\r
+\r
+import junit.framework.Assert;\r
+\r
+import org.junit.Test;\r
+import org.simantics.modelica.fmi.FMUControlJNI;\r
+import org.simantics.modelica.fmi.FMUJNIException;\r
+\r
+public class MultipleSimultaneousFMUSimulationsTest {\r
+\r
+\r
+ /**\r
+ * This test runs simultaneously {@link FMURealTest}, {@link FMUIntegerTest} and {@link FMUBooleanTest} to\r
+ * see that they do not conflict with each other.\r
+ */\r
+\r
+ @Test\r
+ public void testMultipleSimulations() {\r
+\r
+ FMUControlJNI control1 = new FMUControlJNI();\r
+ FMUControlJNI control2 = new FMUControlJNI();\r
+ FMUControlJNI control3 = new FMUControlJNI();\r
+\r
+ String path1 = ".\\Models\\FMUTests\\FMURealTest\\FMURealTestModel.fmu";\r
+ String path2 = ".\\Models\\FMUTests\\FMUIntegerTest\\FMUIntegerTestModel.fmu";\r
+ String path3 = ".\\Models\\FMUTests\\FMUBooleanTest\\FMUBooleanTestModel.fmu"; \r
+\r
+ try {\r
+ control1.loadFMUFile(path1);\r
+ control2.loadFMUFile(path2);\r
+ control3.loadFMUFile(path3);\r
+\r
+ control1.setStepLength(0.1);\r
+ control2.setStepLength(0.1);\r
+ control3.setStepLength(0.1);\r
+\r
+ control1.initializeSimulation();\r
+ control2.initializeSimulation();\r
+ control3.initializeSimulation();\r
+\r
+\r
+ String[] s1 = control1.getAllVariables();\r
+ String[] s2 = control2.getAllVariables();\r
+ String[] s3 = control3.getAllVariables();\r
+\r
+\r
+ List<String> nameList1 = Arrays.asList(s1);\r
+ List<String> nameList2 = Arrays.asList(s2);\r
+ List<String> nameList3 = Arrays.asList(s3);\r
+ int valve1 = nameList1.indexOf("valve");\r
+ int valve2 = nameList2.indexOf("valve");\r
+ int valve3 = nameList3.indexOf("valve");\r
+\r
+ int stock1 = nameList1.indexOf("stock");\r
+ int stock2 = nameList2.indexOf("stock");\r
+ int stock3 = nameList3.indexOf("stock");\r
+\r
+ control1.subscribe(s1);\r
+ control2.subscribe(s2);\r
+ control3.subscribe(s3);\r
+\r
+\r
+ double value;\r
+ double[] results1 = new double[s1.length];\r
+ double[] results2 = new double[s2.length];\r
+ double[] results3 = new double[s3.length];\r
+\r
+ for(int i = 0; i < 60; i++) {\r
+\r
+ // Real\r
+ results1 = control1.getSubscribedResults(results1);\r
+\r
+ if(i == 30) {\r
+ value = results1[stock1];\r
+ Assert.assertFalse("stock != 6 when time == 3. (stock == " + value + ")", value < 5.999 || value > 6.001);\r
+ control1.setRealValue("r1", 3);\r
+ }\r
+\r
+ value = results1[valve1];\r
+ if(i > 30) {\r
+ Assert.assertFalse("valve != 4 after time == 3. (valve == " + value + ")", value < 3.999 || value > 4.001);\r
+ } else {\r
+ Assert.assertFalse("valve != 2 before time == 3. (valve == " + value + ")", value < 1.999 || value > 2.001);\r
+\r
+ }\r
+ control1.simulateStep();\r
+\r
+ // Integer\r
+ results2 = control2.getSubscribedResults(results2);\r
+\r
+ if(i == 30) {\r
+ value = results2[stock2];\r
+ Assert.assertFalse("stock != 6 when time == 3. (stock == " + value + ")", value < 5.999 || value > 6.001);\r
+ control2.setIntegerValue("i1", 3);\r
+ }\r
+\r
+ value = results2[valve2];\r
+ if(i > 30) {\r
+ Assert.assertFalse("valve != 4 after time == 3. (valve == " + value + ")", value < 3.999 || value > 4.001);\r
+ } else {\r
+ Assert.assertFalse("valve != 2 before time == 3. (valve == " + value + ")", value < 1.999 || value > 2.001);\r
+\r
+ }\r
+ control2.simulateStep();\r
+\r
+ // Boolean\r
+ results3 = control3.getSubscribedResults(results3);\r
+\r
+ if(i == 30) {\r
+ value = results3[stock3];\r
+ Assert.assertFalse("stock != 3 when time == 3. (stock == " + value + ")", value < 2.999 || value > 3.001);\r
+ control3.setBooleanValue("b", false);\r
+ }\r
+\r
+ value = results3[valve3];\r
+ if(i > 30) {\r
+ Assert.assertFalse("valve != -1 after time == 3. (valve == " + value + ")", value > -0.999 || value < -1.001);\r
+ } else {\r
+ Assert.assertFalse("valve != 1 before time == 3. (valve == " + value + ")", value < 0.999 || value > 1.001);\r
+\r
+ }\r
+ control3.simulateStep();\r
+\r
+ }\r
+\r
+ // Real\r
+ results1 = control1.getSubscribedResults(results1);\r
+ value = results1[stock1];\r
+ Assert.assertFalse("stock != 18 when time == 6. (stock == " + value + ")", value < 17.999 || value > 18.001);\r
+\r
+ // Integer\r
+ results2 = control2.getSubscribedResults(results2);\r
+ value = results2[stock2];\r
+ Assert.assertFalse("stock != 18 when time == 6. (stock == " + value + ")", value < 17.999 || value > 18.001);\r
+\r
+ // Boolean\r
+ results3 = control3.getSubscribedResults(results3);\r
+ value = results3[stock3];\r
+ Assert.assertFalse("stock != 0 when time == 6. (stock == " + value + ")", value < -0.001 || value > 0.001);\r
+\r
+\r
+ } catch (FMUJNIException e) {\r
+ Assert.fail("Exception in native fmu code: " + e.getMessage());\r
+ }\r
+\r
+ try {\r
+ control1.unloadFMU();\r
+ control2.unloadFMU();\r
+ control3.unloadFMU();\r
+ } catch (FMUJNIException e) {\r
+ Assert.fail("Exception in native fmu code: " + e.getMessage());\r
+ }\r
+\r
+ }\r
+}\r
+\r