1 package org.simantics.sysdyn.tests.fmi;
\r
3 import java.util.Arrays;
\r
4 import java.util.List;
\r
6 import junit.framework.Assert;
\r
8 import org.junit.Test;
\r
9 import org.simantics.modelica.fmi.FMUControlJNI;
\r
10 import org.simantics.modelica.fmi.FMUJNIException;
\r
12 public class MultipleSimultaneousFMUSimulationsTest {
\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
21 public void testMultipleSimulations() {
\r
23 FMUControlJNI control1 = new FMUControlJNI();
\r
24 FMUControlJNI control2 = new FMUControlJNI();
\r
25 FMUControlJNI control3 = new FMUControlJNI();
\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
32 control1.loadFMUFile(path1);
\r
33 control2.loadFMUFile(path2);
\r
34 control3.loadFMUFile(path3);
\r
36 control1.setStepLength(0.1);
\r
37 control2.setStepLength(0.1);
\r
38 control3.setStepLength(0.1);
\r
40 control1.initializeSimulation();
\r
41 control2.initializeSimulation();
\r
42 control3.initializeSimulation();
\r
45 String[] s1 = control1.getAllVariables();
\r
46 String[] s2 = control2.getAllVariables();
\r
47 String[] s3 = control3.getAllVariables();
\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
57 int stock1 = nameList1.indexOf("stock");
\r
58 int stock2 = nameList2.indexOf("stock");
\r
59 int stock3 = nameList3.indexOf("stock");
\r
61 control1.subscribe(s1);
\r
62 control2.subscribe(s2);
\r
63 control3.subscribe(s3);
\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
71 for(int i = 0; i < 60; i++) {
\r
74 results1 = control1.getSubscribedResults(results1);
\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
82 value = results1[valve1];
\r
84 Assert.assertFalse("valve != 4 after time == 3. (valve == " + value + ")", value < 3.999 || value > 4.001);
\r
86 Assert.assertFalse("valve != 2 before time == 3. (valve == " + value + ")", value < 1.999 || value > 2.001);
\r
89 control1.simulateStep();
\r
92 results2 = control2.getSubscribedResults(results2);
\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
100 value = results2[valve2];
\r
102 Assert.assertFalse("valve != 4 after time == 3. (valve == " + value + ")", value < 3.999 || value > 4.001);
\r
104 Assert.assertFalse("valve != 2 before time == 3. (valve == " + value + ")", value < 1.999 || value > 2.001);
\r
107 control2.simulateStep();
\r
110 results3 = control3.getSubscribedResults(results3);
\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
118 value = results3[valve3];
\r
120 Assert.assertFalse("valve != -1 after time == 3. (valve == " + value + ")", value > -0.999 || value < -1.001);
\r
122 Assert.assertFalse("valve != 1 before time == 3. (valve == " + value + ")", value < 0.999 || value > 1.001);
\r
125 control3.simulateStep();
\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
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
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
145 } catch (FMUJNIException e) {
\r
146 Assert.fail("Exception in native fmu code: " + e.getMessage());
\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