]> gerrit.simantics Code Review - simantics/sysdyn.git/commitdiff
Separated fmu instantiation from initialization. Initial values can be changed after...
authorlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Fri, 18 Jan 2013 08:22:44 +0000 (08:22 +0000)
committerlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Fri, 18 Jan 2013 08:22:44 +0000 (08:22 +0000)
git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@26644 ac1ea38d-2e2b-0410-8846-a27921b304fc

org.simantics.fmu.me.win32/libraries/FMUSimulator.dll
org.simantics.fmu.me.win32/libraries/miniunz.dll
org.simantics.fmu.me.win32/libraries/zlibwapi.dll
org.simantics.fmu/FMUSolution/FMUSimulator/include/org_simantics_fmu_FMUControlJNI.h
org.simantics.fmu/FMUSolution/FMUSimulator/src/fmu_control.cpp
org.simantics.fmu/src/org/simantics/fmu/FMUControlJNI.java
org.simantics.fmu/src/org_simantics_fmu_FMUControlJNI.h
org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SaveResultJob.java
org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynGameExperiment.java

index 52c78ab79ca12790bf8138918d32728dd92a79d4..52dcd12b762d017f1526def213bb484719184623 100644 (file)
Binary files a/org.simantics.fmu.me.win32/libraries/FMUSimulator.dll and b/org.simantics.fmu.me.win32/libraries/FMUSimulator.dll differ
index f3e7ccfaf6f57090ee4446377416159bea3adb7c..56a5b2f564fd09a439e8b181555710eec3e1c22d 100644 (file)
Binary files a/org.simantics.fmu.me.win32/libraries/miniunz.dll and b/org.simantics.fmu.me.win32/libraries/miniunz.dll differ
index 6efda05e77acb088904eecd97768b143062d8135..767b655a9a69443f7f28ee3aff3ceaf9e0aa31de 100644 (file)
Binary files a/org.simantics.fmu.me.win32/libraries/zlibwapi.dll and b/org.simantics.fmu.me.win32/libraries/zlibwapi.dll differ
index 9f2697b477a1d22058c6f608cdb912a6df3965e0..2059987900463e27fac86c89419c80a64e9ba49d 100644 (file)
@@ -23,6 +23,14 @@ JNIEXPORT jint JNICALL Java_org_simantics_fmu_FMUControlJNI_loadFMUFile_1
 JNIEXPORT jint JNICALL Java_org_simantics_fmu_FMUControlJNI_setStepLength_1\r
   (JNIEnv *, jobject, jstring, jdouble);\r
 \r
+/*\r
+ * Class:     org_simantics_fmu_FMUControlJNI\r
+ * Method:    instantiateSimulation_\r
+ * Signature: (Ljava/lang/String;)I\r
+ */\r
+JNIEXPORT jint JNICALL Java_org_simantics_fmu_FMUControlJNI_instantiateSimulation_1\r
+  (JNIEnv *, jobject, jstring);\r
+\r
 /*\r
  * Class:     org_simantics_fmu_FMUControlJNI\r
  * Method:    initializeSimulation_\r
@@ -95,6 +103,14 @@ JNIEXPORT jdoubleArray JNICALL Java_org_simantics_fmu_FMUControlJNI_getSubscribe
 JNIEXPORT jint JNICALL Java_org_simantics_fmu_FMUControlJNI_unloadFMU_1\r
   (JNIEnv *, jobject, jstring);\r
 \r
+/*\r
+ * Class:     org_simantics_fmu_FMUControlJNI\r
+ * Method:    isInitialized_\r
+ * Signature: (Ljava/lang/String;)Z\r
+ */\r
+JNIEXPORT jboolean JNICALL Java_org_simantics_fmu_FMUControlJNI_isInitialized_1\r
+  (JNIEnv *, jobject, jstring);\r
+\r
 /*\r
  * Class:     org_simantics_fmu_FMUControlJNI\r
  * Method:    getTime_\r
index f1384510f3e4966bda5345b9c8c72b968c9fffbd..43a1be983045c47bf85779eae5edb8aeede0720e 100644 (file)
@@ -56,6 +56,8 @@ struct FMUControlStruct {
        int nz;                         // number of state event indicators\r
        double *z;                                              // state event indicators\r
        double *prez;                                   // previous values of state event indicators\r
+       \r
+       bool initialized;                               // has the fmu been initialized\r
 \r
        vector<int> subscription;               // result subscriptions\r
        vector<string> allVariables;    // all variables in an initialized model\r
@@ -385,6 +387,22 @@ JNIEXPORT jint JNICALL Java_org_simantics_fmu_FMUControlJNI_setBooleanValue_1
        }\r
 }\r
 \r
+JNIEXPORT jboolean JNICALL Java_org_simantics_fmu_FMUControlJNI_isInitialized_1\r
+  (JNIEnv *env, jobject obj, jstring id) {\r
+       const char *fmuId = env->GetStringUTFChars(id, 0);\r
+       if(exists(fmuId)) {\r
+               FMUControlStruct& fmuStruct = fmus[fmuId];\r
+               env->ReleaseStringUTFChars(id, fmuId);\r
+               return fmuStruct.initialized;\r
+       } else {\r
+               string message = fmuId;\r
+               env->ReleaseStringUTFChars(id, fmuId);\r
+               throwException(env, "getTime: Model id " + message + " not found");\r
+               return false;\r
+       }\r
+}\r
+\r
+\r
 JNIEXPORT jdouble JNICALL Java_org_simantics_fmu_FMUControlJNI_getTime_1\r
   (JNIEnv *env, jobject obj, jstring id) {\r
        const char *fmuId = env->GetStringUTFChars(id, 0);\r
@@ -462,7 +480,7 @@ JNIEXPORT jdoubleArray JNICALL Java_org_simantics_fmu_FMUControlJNI_getSubscribe
        return result;\r
 }\r
 \r
-JNIEXPORT jint JNICALL Java_org_simantics_fmu_FMUControlJNI_initializeSimulation_1\r
+JNIEXPORT jint JNICALL Java_org_simantics_fmu_FMUControlJNI_instantiateSimulation_1\r
   (JNIEnv *env, jobject obj, jstring id) {\r
 \r
        const char *fmuId = env->GetStringUTFChars(id, 0);\r
@@ -480,8 +498,6 @@ JNIEXPORT jint JNICALL Java_org_simantics_fmu_FMUControlJNI_initializeSimulation
                if(fmu.modelDescription == NULL)\r
                        return throwException(env, "No FMU loaded");\r
 \r
-               fmiBoolean toleranceControlled = fmiFalse;\r
-\r
                fmuStruct.currentTime = 0; // start time\r
 \r
                // instantiate the fmu\r
@@ -507,11 +523,9 @@ JNIEXPORT jint JNICALL Java_org_simantics_fmu_FMUControlJNI_initializeSimulation
                //if (!x || !xdot) return error("out of memory");\r
                if (!fmuStruct.x || !fmuStruct.xdot || fmuStruct.nz>0 && (!fmuStruct.z || !fmuStruct.prez)) return throwException(env, "out of memory");\r
         \r
-               // set the start time and initialize\r
+               // set the start time\r
                fmuStruct.fmiFlag = fmu.setTime(fmuStruct.c, fmuStruct.currentTime);\r
                if (fmuStruct.fmiFlag > fmiWarning) return throwException(env, "could not set time");\r
-               fmuStruct.fmiFlag =  fmu.initialize(fmuStruct.c, toleranceControlled, fmuStruct.currentTime, &(fmuStruct.eventInfo));\r
-               if (fmuStruct.fmiFlag > fmiWarning)  return throwException(env, "could not initialize model");\r
 \r
                // Clear all variables -vector\r
                fmuStruct.allVariables.clear();\r
@@ -524,6 +538,42 @@ JNIEXPORT jint JNICALL Java_org_simantics_fmu_FMUControlJNI_initializeSimulation
                        fmuStruct.indexes.insert ( pair<string,int>(s,k) );\r
                }\r
 \r
+               // Set initialized flag to 0\r
+               fmuStruct.initialized = false;\r
+\r
+               fflush(stdout);\r
+               return 1;\r
+\r
+       } else {\r
+               string message = fmuId;\r
+               env->ReleaseStringUTFChars(id, fmuId);\r
+               return throwException(env, "instantiateSimulation: Model id " + message + " not found");\r
+       }\r
+}\r
+\r
+JNIEXPORT jint JNICALL Java_org_simantics_fmu_FMUControlJNI_initializeSimulation_1\r
+  (JNIEnv *env, jobject obj, jstring id) {\r
+\r
+       const char *fmuId = env->GetStringUTFChars(id, 0);\r
+       if(exists(fmuId)) {\r
+               FMUControlStruct& fmuStruct = fmus[fmuId];\r
+               env->ReleaseStringUTFChars(id, fmuId);\r
+\r
+               FMU& fmu = fmuStruct.fmu;\r
+\r
+               if(fmu.modelDescription == NULL)\r
+                       return throwException(env, "No FMU loaded");\r
+\r
+               if(fmuStruct.initialized == true)\r
+                       return throwException(env, "FMU already initialized. Instantiate it first.");\r
+\r
+               fmiBoolean toleranceControlled = fmiFalse;\r
+\r
+               fmuStruct.fmiFlag =  fmu.initialize(fmuStruct.c, toleranceControlled, fmuStruct.currentTime, &(fmuStruct.eventInfo));\r
+               if (fmuStruct.fmiFlag > fmiWarning)  return throwException(env, "could not initialize model");\r
+\r
+               fmuStruct.initialized = true;\r
+\r
                fflush(stdout);\r
                return 1;\r
 \r
@@ -617,7 +667,14 @@ JNIEXPORT jint JNICALL Java_org_simantics_fmu_FMUControlJNI_simulateStep_1
                }\r
 \r
                if(fmuStruct.x == NULL) {\r
-                       return throwException(env, "Simulate step failed - fmu not initialized");\r
+                       return throwException(env, "Simulate step failed - fmu not instantiated");\r
+               }\r
+\r
+               if(fmuStruct.initialized == false) {\r
+                       fmiBoolean toleranceControlled = fmiFalse;\r
+                       fmuStruct.fmiFlag =  fmuStruct.fmu.initialize(fmuStruct.c, toleranceControlled, fmuStruct.currentTime, &(fmuStruct.eventInfo));\r
+                       if (fmuStruct.fmiFlag > fmiWarning)  return throwException(env, "could not initialize model");\r
+                       fmuStruct.initialized = true;\r
                }\r
 \r
                FMU& fmu = fmuStruct.fmu;\r
index f49c26272511b845d9a0c8b4116295c866a29080..716a864b0b8f71006e55e021d7ba08fd823cccf0 100644 (file)
@@ -200,11 +200,37 @@ public class FMUControlJNI {
     private native int setStepLength_(String id, double step);\r
 \r
     /**\r
-     * Initializes a simulation. \r
+     * Instantiates a simulation. \r
      * <p>\r
      * Make sure that an FMU is loaded first.\r
      * @throws FMUJNIException\r
      */\r
+    public void instantiateSimulation() throws FMUJNIException {\r
+        synchronized(syncObject) {\r
+\r
+            try {\r
+\r
+                int ret = instantiateSimulation_(getModelID()); \r
+                if(ret == ERROR)\r
+                    throw new FMUJNIException(getLastErrorMessage());\r
+\r
+            } catch (UnsatisfiedLinkError err) {\r
+                throw new FMUJNIException(UNSATISFIED_LINK);\r
+            } catch (Exception e) {\r
+                throw new FMUJNIException(e.getMessage());\r
+            }\r
+        }\r
+    }\r
+\r
+    private native int instantiateSimulation_(String id);\r
+\r
+    \r
+    /**\r
+     * Initializes a simulation. \r
+     * <p>\r
+     * Make sure that simulation is instantiated first!\r
+     * @throws FMUJNIException\r
+     */\r
     public void initializeSimulation() throws FMUJNIException {\r
         synchronized(syncObject) {\r
 \r
@@ -426,6 +452,24 @@ public class FMUControlJNI {
         }\r
     }\r
     private native int unloadFMU_(String id);\r
+    \r
+    /**\r
+     * Checks if fmu has been initialized\r
+     * @return current simulation time\r
+     */\r
+    public boolean isInitialized() throws FMUJNIException {\r
+        synchronized(syncObject) {\r
+            try {\r
+                return isInitialized_(getModelID());\r
+            } catch (UnsatisfiedLinkError err) {\r
+                throw new FMUJNIException(UNSATISFIED_LINK);\r
+            } catch (Exception e) {\r
+                throw new FMUJNIException(e.getMessage());\r
+            }\r
+        }\r
+    }\r
+\r
+    private native boolean isInitialized_(String id);\r
 \r
     /**\r
      * Get the current simulation time\r
index 9f2697b477a1d22058c6f608cdb912a6df3965e0..2059987900463e27fac86c89419c80a64e9ba49d 100644 (file)
@@ -23,6 +23,14 @@ JNIEXPORT jint JNICALL Java_org_simantics_fmu_FMUControlJNI_loadFMUFile_1
 JNIEXPORT jint JNICALL Java_org_simantics_fmu_FMUControlJNI_setStepLength_1\r
   (JNIEnv *, jobject, jstring, jdouble);\r
 \r
+/*\r
+ * Class:     org_simantics_fmu_FMUControlJNI\r
+ * Method:    instantiateSimulation_\r
+ * Signature: (Ljava/lang/String;)I\r
+ */\r
+JNIEXPORT jint JNICALL Java_org_simantics_fmu_FMUControlJNI_instantiateSimulation_1\r
+  (JNIEnv *, jobject, jstring);\r
+\r
 /*\r
  * Class:     org_simantics_fmu_FMUControlJNI\r
  * Method:    initializeSimulation_\r
@@ -95,6 +103,14 @@ JNIEXPORT jdoubleArray JNICALL Java_org_simantics_fmu_FMUControlJNI_getSubscribe
 JNIEXPORT jint JNICALL Java_org_simantics_fmu_FMUControlJNI_unloadFMU_1\r
   (JNIEnv *, jobject, jstring);\r
 \r
+/*\r
+ * Class:     org_simantics_fmu_FMUControlJNI\r
+ * Method:    isInitialized_\r
+ * Signature: (Ljava/lang/String;)Z\r
+ */\r
+JNIEXPORT jboolean JNICALL Java_org_simantics_fmu_FMUControlJNI_isInitialized_1\r
+  (JNIEnv *, jobject, jstring);\r
+\r
 /*\r
  * Class:     org_simantics_fmu_FMUControlJNI\r
  * Method:    getTime_\r
index 6269318e47bf0266602ab3c1de6d5c5da5b22d8e..859bbc7a50deff3ab871edbff05bc055147c1ff9 100644 (file)
@@ -19,7 +19,6 @@ import org.eclipse.core.runtime.IStatus;
 import org.eclipse.core.runtime.Platform;\r
 import org.eclipse.core.runtime.Status;\r
 import org.eclipse.core.runtime.jobs.Job;\r
-import org.simantics.Simantics;\r
 import org.simantics.db.Resource;\r
 import org.simantics.db.Session;\r
 import org.simantics.db.WriteGraph;\r
@@ -55,7 +54,7 @@ public class SaveResultJob extends Job {
         int resultItemsNumber = sysdynResult.numberOfVariables();\r
         monitor.beginTask("Save result", resultItemsNumber * 2);        \r
         try {\r
-            File file = Simantics.getSession().syncRequest(new WriteResultRequest<File>() {\r
+            File file = session.syncRequest(new WriteResultRequest<File>() {\r
 \r
                 @Override\r
                 public File perform(WriteGraph graph) throws DatabaseException {\r
index ed13664259f6a1a2c1cd65f438082fa3408400ec..77332a9e7c2601a20edec6a41d57562c6e3c14a1 100644 (file)
@@ -22,6 +22,7 @@ import org.eclipse.core.runtime.jobs.Job;
 import org.simantics.db.AsyncReadGraph;\r
 import org.simantics.db.ReadGraph;\r
 import org.simantics.db.Resource;\r
+import org.simantics.db.Session;\r
 import org.simantics.db.exception.DatabaseException;\r
 import org.simantics.db.procedure.AsyncListener;\r
 import org.simantics.db.request.Read;\r
@@ -187,25 +188,7 @@ public class SysdynGameExperiment extends SysdynExperiment {
                        try {\r
                                control.loadFMUFile(simulationLocation.executableFile.getAbsolutePath()); // unzip and load fmu\r
                                control.setStepLength(stepLength); // FIXME: fixed step lenghth\r
-                               control.initializeSimulation(); // initialize simulation\r
-                               if(inits.get("variableFilter") == null || inits.get("variableFilter").equals(".*"))\r
-                                       subscription = control.getAllVariables();\r
-                               else\r
-                                       subscription = control.filterVariables(inits.get("variableFilter"));\r
-                               control.subscribe(subscription); // subscribe all variables\r
-                               \r
-                               // Initialize results\r
-                               results.clear();\r
-                               \r
-                               double[] initialValues = new double[subscription.length];\r
-                               initialValues = control.getSubscribedResults(initialValues);\r
-                               for(int k = 0; k < subscription.length; k++) {\r
-                                       results.put(subscription[k], new ArrayList<Double>());\r
-                                       results.get(subscription[k]).add(initialValues[k]);\r
-                               }\r
-                               \r
-                               getCurrentResult().setResult(new GameResult(this.results, this.subscription));\r
-                               resultsChanged();\r
+                               instantiate();\r
                                \r
                        } catch (FMUJNIException e) {\r
                                System.err.println("SysdynGameExperiment initialization failed:\n\t" + e.getMessage());\r
@@ -260,6 +243,13 @@ public class SysdynGameExperiment extends SysdynExperiment {
                        \r
                        \r
                        try {\r
+                           \r
+                           // initialize if not initialized\r
+                           if(!control.isInitialized()) {\r
+                               control.initializeSimulation();\r
+                               getInitialResultValues();\r
+                           }\r
+                           \r
                                control.setStepLength(stepLength); // Set step length each time in case there has been changes\r
 \r
                                double time = control.getTime();\r
@@ -343,38 +333,58 @@ public class SysdynGameExperiment extends SysdynExperiment {
             return;\r
         \r
         if(time >-0.001 && time < 0.001) {\r
-            initialize();\r
+            instantiate();\r
         } else {\r
             System.out.println("rewindTo");\r
         }\r
     }\r
     \r
-    private synchronized void initialize() {\r
+    @Override\r
+    public void refresh(Session session) {\r
+        try {\r
+            control.initializeSimulation();\r
+            getInitialResultValues();\r
+            \r
+        } catch (FMUJNIException e) {\r
+            System.err.println("SysdynGameExperiment instantiate failed: " + e.getMessage());\r
+        }\r
+    }\r
+    \r
+    private synchronized void instantiate() {\r
         try {\r
             HashMap<String, String> inits = getExperimentParameters(null);\r
 \r
             control.setStepLength(stepLength); // FIXME: fixed step lenghth\r
-            control.initializeSimulation(); // initialize simulation\r
+            control.instantiateSimulation(); // instantiate simulation\r
             if(inits.get("variableFilter") == null || inits.get("variableFilter").equals(".*"))\r
                 subscription = control.getAllVariables();\r
             else\r
                 subscription = control.filterVariables(inits.get("variableFilter"));\r
             control.subscribe(subscription); // subscribe all variables\r
             \r
+            getInitialResultValues();\r
+            \r
+        } catch (FMUJNIException e) {\r
+            System.err.println("SysdynGameExperiment instantiate failed: " + e.getMessage());\r
+        }\r
+    }\r
+\r
+    private synchronized void getInitialResultValues() {\r
+        try {\r
             // Initialize results\r
             results.clear();\r
-            \r
+\r
             double[] initialValues = new double[subscription.length];\r
             initialValues = control.getSubscribedResults(initialValues);\r
             for(int k = 0; k < subscription.length; k++) {\r
                 results.put(subscription[k], new ArrayList<Double>());\r
                 results.get(subscription[k]).add(initialValues[k]);\r
             }\r
-            \r
+\r
             getCurrentResult().setResult(new GameResult(this.results, this.subscription));\r
-            resultsChanged();                \r
+            resultsChanged();  \r
         } catch (FMUJNIException e) {\r
-            System.err.println("SysdynGameExperiment rewind failed: " + e.getMessage());\r
+            System.err.println("SysdynGameExperiment getInitialResultValues failed: " + e.getMessage());\r
         }\r
     }\r
  }\r