From 057a5645e0bf9f46b757c2e021559663e9d92a69 Mon Sep 17 00:00:00 2001 From: lempinen Date: Fri, 11 May 2012 06:38:26 +0000 Subject: [PATCH] Test for simultaneous fmu simulation (refs #3398) git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@24941 ac1ea38d-2e2b-0410-8846-a27921b304fc --- ...ultipleSimultaneousFMUSimulationsTest.java | 159 ++++++++++++++++++ 1 file changed, 159 insertions(+) create mode 100644 org.simantics.sysdyn.tests/src/org/simantics/sysdyn/tests/fmi/MultipleSimultaneousFMUSimulationsTest.java diff --git a/org.simantics.sysdyn.tests/src/org/simantics/sysdyn/tests/fmi/MultipleSimultaneousFMUSimulationsTest.java b/org.simantics.sysdyn.tests/src/org/simantics/sysdyn/tests/fmi/MultipleSimultaneousFMUSimulationsTest.java new file mode 100644 index 00000000..6a4d7aff --- /dev/null +++ b/org.simantics.sysdyn.tests/src/org/simantics/sysdyn/tests/fmi/MultipleSimultaneousFMUSimulationsTest.java @@ -0,0 +1,159 @@ +package org.simantics.sysdyn.tests.fmi; + +import java.util.Arrays; +import java.util.List; + +import junit.framework.Assert; + +import org.junit.Test; +import org.simantics.modelica.fmi.FMUControlJNI; +import org.simantics.modelica.fmi.FMUJNIException; + +public class MultipleSimultaneousFMUSimulationsTest { + + + /** + * This test runs simultaneously {@link FMURealTest}, {@link FMUIntegerTest} and {@link FMUBooleanTest} to + * see that they do not conflict with each other. + */ + + @Test + public void testMultipleSimulations() { + + FMUControlJNI control1 = new FMUControlJNI(); + FMUControlJNI control2 = new FMUControlJNI(); + FMUControlJNI control3 = new FMUControlJNI(); + + String path1 = ".\\Models\\FMUTests\\FMURealTest\\FMURealTestModel.fmu"; + String path2 = ".\\Models\\FMUTests\\FMUIntegerTest\\FMUIntegerTestModel.fmu"; + String path3 = ".\\Models\\FMUTests\\FMUBooleanTest\\FMUBooleanTestModel.fmu"; + + try { + control1.loadFMUFile(path1); + control2.loadFMUFile(path2); + control3.loadFMUFile(path3); + + control1.setStepLength(0.1); + control2.setStepLength(0.1); + control3.setStepLength(0.1); + + control1.initializeSimulation(); + control2.initializeSimulation(); + control3.initializeSimulation(); + + + String[] s1 = control1.getAllVariables(); + String[] s2 = control2.getAllVariables(); + String[] s3 = control3.getAllVariables(); + + + List nameList1 = Arrays.asList(s1); + List nameList2 = Arrays.asList(s2); + List nameList3 = Arrays.asList(s3); + int valve1 = nameList1.indexOf("valve"); + int valve2 = nameList2.indexOf("valve"); + int valve3 = nameList3.indexOf("valve"); + + int stock1 = nameList1.indexOf("stock"); + int stock2 = nameList2.indexOf("stock"); + int stock3 = nameList3.indexOf("stock"); + + control1.subscribe(s1); + control2.subscribe(s2); + control3.subscribe(s3); + + + double value; + double[] results1 = new double[s1.length]; + double[] results2 = new double[s2.length]; + double[] results3 = new double[s3.length]; + + for(int i = 0; i < 60; i++) { + + // Real + results1 = control1.getSubscribedResults(results1); + + if(i == 30) { + value = results1[stock1]; + Assert.assertFalse("stock != 6 when time == 3. (stock == " + value + ")", value < 5.999 || value > 6.001); + control1.setRealValue("r1", 3); + } + + value = results1[valve1]; + if(i > 30) { + Assert.assertFalse("valve != 4 after time == 3. (valve == " + value + ")", value < 3.999 || value > 4.001); + } else { + Assert.assertFalse("valve != 2 before time == 3. (valve == " + value + ")", value < 1.999 || value > 2.001); + + } + control1.simulateStep(); + + // Integer + results2 = control2.getSubscribedResults(results2); + + if(i == 30) { + value = results2[stock2]; + Assert.assertFalse("stock != 6 when time == 3. (stock == " + value + ")", value < 5.999 || value > 6.001); + control2.setIntegerValue("i1", 3); + } + + value = results2[valve2]; + if(i > 30) { + Assert.assertFalse("valve != 4 after time == 3. (valve == " + value + ")", value < 3.999 || value > 4.001); + } else { + Assert.assertFalse("valve != 2 before time == 3. (valve == " + value + ")", value < 1.999 || value > 2.001); + + } + control2.simulateStep(); + + // Boolean + results3 = control3.getSubscribedResults(results3); + + if(i == 30) { + value = results3[stock3]; + Assert.assertFalse("stock != 3 when time == 3. (stock == " + value + ")", value < 2.999 || value > 3.001); + control3.setBooleanValue("b", false); + } + + value = results3[valve3]; + if(i > 30) { + Assert.assertFalse("valve != -1 after time == 3. (valve == " + value + ")", value > -0.999 || value < -1.001); + } else { + Assert.assertFalse("valve != 1 before time == 3. (valve == " + value + ")", value < 0.999 || value > 1.001); + + } + control3.simulateStep(); + + } + + // Real + results1 = control1.getSubscribedResults(results1); + value = results1[stock1]; + Assert.assertFalse("stock != 18 when time == 6. (stock == " + value + ")", value < 17.999 || value > 18.001); + + // Integer + results2 = control2.getSubscribedResults(results2); + value = results2[stock2]; + Assert.assertFalse("stock != 18 when time == 6. (stock == " + value + ")", value < 17.999 || value > 18.001); + + // Boolean + results3 = control3.getSubscribedResults(results3); + value = results3[stock3]; + Assert.assertFalse("stock != 0 when time == 6. (stock == " + value + ")", value < -0.001 || value > 0.001); + + + } catch (FMUJNIException e) { + Assert.fail("Exception in native fmu code: " + e.getMessage()); + } + + try { + control1.unloadFMU(); + control2.unloadFMU(); + control3.unloadFMU(); + } catch (FMUJNIException e) { + Assert.fail("Exception in native fmu code: " + e.getMessage()); + } + + } +} + -- 2.47.1