]> gerrit.simantics Code Review - simantics/sysdyn.git/commitdiff
Eliminated the need to build separate full modelica code file for models (OMC versio...
authorlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Fri, 17 May 2013 09:05:55 +0000 (09:05 +0000)
committerlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Fri, 17 May 2013 09:05:55 +0000 (09:05 +0000)
Instead, the experiment creates full modelica structure directly to memory and it is created only once per simulation.

(fixes #3968)

git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@27422 ac1ea38d-2e2b-0410-8846-a27921b304fc

org.simantics.modelica/src/org/simantics/modelica/ModelicaManager.java
org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynExperiment.java
org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynGameExperiment.java
org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynSensitivityAnalysisExperiment.java

index 2de275d8a9782e4b4147946e9219c889a5e1f949..e0eaa2105bb4bf80e35b8a5d8a06dbe5a5c4d8ec 100644 (file)
@@ -22,6 +22,7 @@ import java.io.IOException;
 import java.io.InputStream;\r
 import java.io.InputStreamReader;\r
 import java.io.PrintStream;\r
+import java.io.Reader;\r
 import java.net.URL;\r
 import java.net.URLDecoder;\r
 import java.util.ArrayList;\r
@@ -45,7 +46,7 @@ import javax.xml.xpath.XPathFactory;
 \r
 import org.eclipse.core.runtime.FileLocator;\r
 import org.eclipse.core.runtime.Platform;\r
-import org.eclipse.core.runtime.preferences.DefaultScope;\r
+import org.eclipse.core.runtime.preferences.ConfigurationScope;\r
 import org.eclipse.core.runtime.preferences.IScopeContext;\r
 import org.osgi.framework.Bundle;\r
 import org.osgi.service.prefs.Preferences;\r
@@ -100,7 +101,7 @@ public class ModelicaManager {
     public static File getModelicaHome() {\r
         \r
         // Try preferences\r
-        IScopeContext context = DefaultScope.INSTANCE;\r
+        IScopeContext context = ConfigurationScope.INSTANCE;\r
         Preferences node = context.getNode(Activator.PLUGIN_ID);\r
         String omHomePath = node.get(OpenModelicaPreferences.OM_HOME, null);\r
         if(omHomePath != null) {\r
@@ -205,8 +206,14 @@ public class ModelicaManager {
             BufferedReader in = new BufferedReader(r);\r
             StringBuilder output = new StringBuilder();\r
             String line;\r
-            while ((line = in.readLine()) != null)\r
+            boolean first = true;\r
+            while ((line = in.readLine()) != null) {\r
+                if(!first)\r
+                    output.append(System.getProperty("line.separator"));\r
+                first = false;\r
+                \r
                 output.append(line);\r
+            }\r
             return output.toString();\r
         } catch (Exception e) { // exception thrown\r
             System.err.println("getProcessOutput failed!");\r
@@ -457,7 +464,6 @@ public class ModelicaManager {
         ArrayList<String> parameters = new ArrayList<String>();\r
         parameters.add(simulationLocation.mosFile.getAbsolutePath());\r
         runOMC(simulationLocation.simulationDir, simulationLocation.omcHome, monitor, parameters);\r
-        createFullMo(simulationLocation, monitor);\r
 \r
         if(!simulationLocation.executableFile.isFile())\r
             // If .exe file was not created, something went wrong\r
@@ -551,15 +557,23 @@ public class ModelicaManager {
                 String version = experimentParameters.get(OMC_VERSION);\r
                 if(version == null)\r
                         version = getOMCVersion(simulationLocation.omcHome);\r
-                if(version.startsWith("1.9")) {\r
+                \r
+                Double versionNumber = 1.9;\r
+                try {\r
+                    versionNumber = Double.parseDouble(version.substring(0,3));\r
+                } catch (NumberFormatException e) {}\r
+                \r
+                if(versionNumber < 1.9) {\r
+                    writeInits(simulationLocation, experimentParameters, null);\r
+                } else {\r
                     // Handled in experiment\r
                     if(parameterChanges.size() == 1) {\r
                         commands.add("-override");\r
                         commands.add(parameterChanges.keySet().iterator().next() + "=" + parameterChanges.values().iterator().next());\r
-                    } else\r
+                    } else {\r
+                        // FIXME: if you change 1 parameter AND some experiment parameters, only the parameter change takes effect\r
                         updateInitFile(simulationLocation, experimentParameters, parameterChanges);\r
-                } else {\r
-                    writeInits(simulationLocation, experimentParameters, monitor);\r
+                    }\r
                 }\r
 \r
             }\r
@@ -593,35 +607,49 @@ public class ModelicaManager {
         }\r
         return null;\r
     }\r
+    \r
+    public static String getFlatModelText(SimulationLocation simulationLocation, IModelicaMonitor monitor, List<String> additionalPaths) {\r
+        \r
+        ArrayList<String> commands = new ArrayList<String>();\r
+        File omHome = getModelicaHome();\r
+        commands.add(omHome + "\\bin\\omc.exe");\r
+        String path = simulationLocation.mosFile.getAbsolutePath();\r
+        path = path.substring(0, path.length() - 1);\r
+        commands.add(path);\r
+        \r
+        for(String additionalPath : additionalPaths) {\r
+            commands.add(additionalPath);\r
+        }\r
+        \r
+        ProcessBuilder processBuilder = new ProcessBuilder(commands)\r
+        .redirectErrorStream(true).directory(simulationLocation.simulationDir);\r
+     \r
+        Map<String, String> env = processBuilder.environment();\r
+        env.put("OPENMODELICAHOME", omHome.getAbsolutePath());\r
+        \r
+        // Start the building process\r
+        try {\r
+            Process process = processBuilder.start();\r
+            return getProcessOutput(process);\r
+        } catch (IOException e) {\r
+            e.printStackTrace();\r
+        }\r
+        \r
+        return null;\r
+    }\r
+\r
 \r
     /**\r
-     * Get all parameter variables and their values from the full model description\r
-     * @param simulationLocation\r
-     * @param monitor\r
-     * @return\r
+     *  Get all parameter variables and their values from model. Model text is\r
+     *  provided in a Reader.\r
+     * @param reader Reader containing model text\r
+     * @return all parameters and their values\r
      */\r
-    public static HashMap<String, String> getFullModelParameters(SimulationLocation simulationLocation, IModelicaMonitor monitor) {\r
+    public static HashMap<String, String> getModelParameters(Reader reader) {\r
         HashMap<String, String> result = new HashMap<String, String>();\r
 \r
         try {\r
-            ArrayList<String> commands = new ArrayList<String>();\r
-            File omHome = getModelicaHome();\r
-            commands.add(omHome + "\\bin\\omc.exe");\r
-            commands.add(simulationLocation.fullModel.getAbsolutePath());\r
-\r
-            // Create the build process\r
-            ProcessBuilder processBuilder = new ProcessBuilder(commands)\r
-            .redirectErrorStream(true);\r
-\r
-            Map<String, String> env = processBuilder.environment();\r
-            env.put("OPENMODELICAHOME", omHome.getAbsolutePath());\r
-            \r
-            // Start the building process\r
-            Process process = processBuilder.start();\r
-\r
-            InputStream lsOut = process.getInputStream();\r
-            InputStreamReader r = new InputStreamReader(lsOut);\r
-            BufferedReader in = new BufferedReader(r);\r
+            BufferedReader in = new BufferedReader(reader);\r
             String line;\r
             String name;\r
             String value;\r
@@ -637,18 +665,13 @@ public class ModelicaManager {
                 }\r
             }\r
             in.close();\r
-            r.close();\r
-            lsOut.close();   \r
-\r
-            process.waitFor();\r
         } catch (IOException e) {\r
-\r
-        } catch (InterruptedException e) {\r
             e.printStackTrace();\r
         }\r
         return result;\r
     }\r
 \r
+\r
     /**\r
      * Updates inits.xml with the latest values. This is not needed if the model structure\r
      * has changed. When model structure is changed, the model is compiled and a new xml is \r
@@ -664,16 +687,10 @@ public class ModelicaManager {
         \r
         try {\r
             // Create the full model code into one file\r
-            ArrayList<String> parameters = new ArrayList<String>();\r
-            parameters.add(simulationLocation.fullMosFile.getAbsolutePath());\r
-            \r
-            Process process = runOMC(simulationLocation.simulationDir, simulationLocation.omcHome, null, parameters);\r
-            process.waitFor();\r
-            \r
-            trimExtraFromFullModel(simulationLocation);\r
+            createFullMo(simulationLocation, null);\r
             \r
             // Create simulation files from the full description\r
-            parameters.clear();\r
+            ArrayList<String> parameters = new ArrayList<String>();\r
             parameters.add("+s");\r
             parameters.add(simulationLocation.fullModel.getAbsolutePath());\r
            \r
index 46433abd4fb490a06d617c851d4a66b3cc8db249..97a59806260479a0d2559b41c5523905ab9ff529 100644 (file)
@@ -85,7 +85,8 @@ public class SysdynExperiment extends Experiment implements IDynamicExperiment,
 \r
     protected String                                experimentName;\r
     protected static String                         omcVersion = null;\r
-    \r
+    protected static String                         omcHome = null;\r
+\r
     public static SysdynExperiment                                     INSTANCE;\r
 \r
     public SysdynExperiment(Resource experiment, Resource model) {\r
@@ -252,13 +253,11 @@ public class SysdynExperiment extends Experiment implements IDynamicExperiment,
      * @param modelText\r
      * @param monitor\r
      */\r
-    protected void buildModel(SimulationLocation simulationLocation, String modelText, IModelicaMonitor monitor) {\r
+    protected void buildModel(SimulationLocation simulationLocation, IModelicaMonitor monitor) {\r
 \r
         try {\r
             simulationLocation.executableFile.delete();\r
-            previousModelStructure = modelText;\r
             ModelicaManager.buildModel(simulationLocation, monitor);\r
-            previousParameters = ModelicaManager.getFullModelParameters(simulationLocation, monitor);\r
         } catch (ModelicaException e) {\r
             if(e.getMessage() != null)\r
                 monitor.message(e.getMessage());\r
@@ -451,8 +450,14 @@ public class SysdynExperiment extends Experiment implements IDynamicExperiment,
         canceled = false;\r
         progressMonitor.subTask("Write modelica classes");\r
 \r
-        // Write Modelica files\r
-        String modelText = getModelicaCode(monitor, false, getOpenModelicaVersion());\r
+        File home = ModelicaManager.getModelicaHome();\r
+        if (omcHome == null || !home.getAbsolutePath().equals(omcHome)) {\r
+            omcVersion = ModelicaManager.getOMCVersion(home);\r
+            omcHome = home.getAbsolutePath();\r
+        }\r
+        \r
+        // Get Modelica code\r
+        String modelText = getModelicaCode(monitor, false, omcVersion);\r
         if(modelText == null)\r
             return;\r
         progressMonitor.worked(1);\r
@@ -460,47 +465,47 @@ public class SysdynExperiment extends Experiment implements IDynamicExperiment,
         // Write initial files and add init-parameters\r
         progressMonitor.subTask("Write simulation files");\r
         HashMap<String, String> experimentParameters = getExperimentParameters(monitor);\r
-\r
+        \r
         // add loadFile script to load all related functions and function libraries\r
         String additionalScript = getAdditionalScripts();\r
-\r
+        \r
         // Create simulation files\r
         SimulationLocation simulationLocation = createSimulationFiles(sysdynModel, modelText, experimentParameters, additionalScript, false);\r
         progressMonitor.worked(1);\r
-\r
+        \r
         // Build the model and store previous model structure and inits that affect the building\r
         // If there is no exe file OR the model structure has not changed, no need to build\r
-        boolean structureChanged = hasStructureChanged(modelText);\r
+        String flatModelText = ModelicaManager.getFlatModelText(simulationLocation, monitor, FunctionUtils.getLibraryPathsForModelica(this));\r
+        \r
+        boolean structureChanged = hasStructureChanged(flatModelText);\r
         if (!simulationLocation.executableFile.isFile() || structureChanged) {\r
             progressMonitor.subTask("Build model");\r
-            buildModel(simulationLocation, modelText, monitor);\r
+            previousModelStructure = flatModelText;\r
+            StringReader reader = new StringReader(previousModelStructure);\r
+            previousParameters = ModelicaManager.getModelParameters(reader);\r
+            reader.close();\r
         }\r
         \r
         // Add changed parameters in case that structure has not changed\r
         HashMap<String, String> changes = structureChanged ? null : new HashMap<String, String>();\r
-        if (omcVersion == null) {\r
-            omcVersion = ModelicaManager.getOMCVersion(simulationLocation.omcHome);\r
-        }\r
         if(!structureChanged && previousParameters != null && omcVersion.startsWith("1.9")) {\r
-            try {\r
-                ModelicaManager.createFullMo(simulationLocation, monitor);\r
-                HashMap<String, String> newParameters = ModelicaManager.getFullModelParameters(simulationLocation, monitor);\r
+                StringReader reader = new StringReader(flatModelText);\r
+                HashMap<String, String> newParameters = ModelicaManager.getModelParameters(reader);\r
+                reader.close();\r
                 for(String key : previousParameters.keySet()) {\r
                     if(!previousParameters.get(key).equals(newParameters.get(key))) {\r
                         changes.put(key, newParameters.get(key));\r
                     }\r
                 }\r
                 previousParameters = newParameters;\r
-            } catch (ModelicaException e) {\r
-                e.printStackTrace();\r
-            }\r
         }\r
         progressMonitor.worked(1);\r
-\r
+        \r
         if(simulationLocation != null && !canceled) {\r
             // Simulate the model\r
             runModelica(simulationLocation, modelText, monitor, progressMonitor, experimentParameters, changes);\r
         }\r
+        \r
         if(canceled)\r
             simulate(false);\r
         process = null;\r
@@ -561,9 +566,7 @@ public class SysdynExperiment extends Experiment implements IDynamicExperiment,
             while (c != null && p != null) {\r
                 // if the lines are the same, no need for further examination\r
                 if(!c.equals(p)) {\r
-                    if(\r
-                            c.contains("parameter") && c.contains("/* Actual value read from init file */") &&\r
-                            p.contains("parameter") && p.contains("/* Actual value read from init file */")) {\r
+                    if(c.contains("parameter") && p.contains("parameter")) {\r
                         /*\r
                          *  The line is a parameter definition.\r
                          *  In this case only what is before '=' matters\r
index b64c5265cbfc0753da4e4ab6ed2e686544f2bd53..00bc3b7031054d7f223f2d0d9600d7f10e3f0fda 100644 (file)
@@ -206,7 +206,7 @@ public class SysdynGameExperiment extends SysdynExperiment {
                // If there is no exe file OR the model structure has not changed, no need to build\r
                if (fmu == null && (!simulationLocation.executableFile.isFile() || hasStructureChanged(modelText))) {\r
                        progressMonitor.subTask("Build model");\r
-                       buildModel(simulationLocation, modelText, monitor);\r
+                       buildModel(simulationLocation, monitor);\r
                        previousModelStructure = modelText;\r
                        \r
                        saveModelFmu(simulationLocation);\r
index 0d3d096bc0d8e11239d0b9f60a6e9a1eec80610a..588efe2574f82deefb1fb9f058d7e6c021560a14 100644 (file)
@@ -311,4 +311,31 @@ public class SysdynSensitivityAnalysisExperiment extends SysdynExperiment {
 \r
     }\r
     \r
+    \r
+    @Override\r
+    public synchronized void simulate(IModelicaMonitor monitor, IProgressMonitor progressMonitor) throws IOException {\r
+        \r
+        File home = ModelicaManager.getModelicaHome();\r
+        if (omcHome == null || !home.getAbsolutePath().equals(omcHome)) {\r
+            omcVersion = ModelicaManager.getOMCVersion(home);\r
+            omcHome = home.getAbsolutePath();\r
+        }\r
+\r
+        // Make sure that omc version is 1.9 or higher. \r
+        // Builtin version during this change is 1.9 beta 4\r
+        if(omcVersion != null) {\r
+            try {\r
+                double v = Double.parseDouble(omcVersion.substring(0, 3));\r
+                if(v < 1.9) {\r
+                    monitor.message("Error: Sensitivity analysis requires OMC version >= 1.9 \n" +\r
+                               "Current version is " + omcVersion +"\n" +\r
+                                               "Change version from Window->Preferences->Modelica");\r
+                    simulate(false);\r
+                    return;\r
+                }\r
+            } catch (NumberFormatException e) {}\r
+        }\r
+        \r
+        super.simulate(monitor, progressMonitor);\r
+    }\r
 }\r