]> gerrit.simantics Code Review - simantics/sysdyn.git/commitdiff
First iteration of sensitivity analysis which draws graphs and can be configured...
authormiettinen <miettinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Mon, 8 Apr 2013 08:57:26 +0000 (08:57 +0000)
committermiettinen <miettinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Mon, 8 Apr 2013 08:57:26 +0000 (08:57 +0000)
git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@27259 ac1ea38d-2e2b-0410-8846-a27921b304fc

org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynExperiment.java
org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynSensitivityAnalysisExperiment.java

index 093baadff03d4f504335e1d8876f36255ab9d539..7c5c7f4d6309351185543e664f431383b578409f 100644 (file)
@@ -449,23 +449,18 @@ public class SysdynExperiment extends Experiment implements IDynamicExperiment,
      */\r
     public synchronized void simulate(final IModelicaMonitor monitor, final IProgressMonitor progressMonitor) throws IOException {\r
         canceled = false;\r
-\r
         progressMonitor.subTask("Write modelica classes");\r
 \r
         // Write Modelica files\r
-\r
-        \r
         String modelText = getModelicaCode(monitor, false, getOpenModelicaVersion());\r
         if(modelText == null)\r
             return;\r
-\r
         progressMonitor.worked(1);\r
 \r
         // 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
@@ -473,7 +468,6 @@ public class SysdynExperiment extends Experiment implements IDynamicExperiment,
         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
@@ -501,7 +495,6 @@ public class SysdynExperiment extends Experiment implements IDynamicExperiment,
                 e.printStackTrace();\r
             }\r
         }\r
-\r
         progressMonitor.worked(1);\r
 \r
         if(simulationLocation != null && !canceled) {\r
index 2a0d2c1d4c31106fbf88bdb249041203e35cac9d..abaa273e716a61aeeb43d572466e2f48fa9ee8dc 100644 (file)
@@ -20,15 +20,19 @@ import java.util.HashMap;
 import java.util.concurrent.ScheduledExecutorService;\r
 \r
 import org.eclipse.core.runtime.IProgressMonitor;\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.db.ReadGraph;\r
 import org.simantics.db.Resource;\r
+import org.simantics.db.common.request.ReadRequest;\r
+import org.simantics.db.exception.DatabaseException;\r
 import org.simantics.modelica.IModelicaMonitor;\r
-import org.simantics.modelica.ModelicaException;\r
 import org.simantics.modelica.ModelicaKeys;\r
 import org.simantics.modelica.ModelicaManager;\r
 import org.simantics.modelica.SimulationLocation;\r
 import org.simantics.modelica.data.CSVSimulationResult;\r
 import org.simantics.modelica.data.MatSimulationResult;\r
 import org.simantics.modelica.data.SimulationResult;\r
+import org.simantics.sysdyn.SysdynResource;\r
 \r
 /**\r
  * Sensitivity analysis experiment\r
@@ -42,113 +46,76 @@ public class SysdynSensitivityAnalysisExperiment extends SysdynExperiment {
         NORMAL\r
     }\r
     \r
-    private final Distribution propabilityDistribution = DEFAULT_PROPABILITY_DISTRIBUTION;\r
-    private final double min_value = DEFAULT_MIN_VALUE;\r
-    private final double max_value = DEFAULT_MAX_VALUE;\r
-    private final double num_values = DEFAULT_NUM_VALUES;\r
+    private Distribution propabilityDistribution = DEFAULT_PROPABILITY_DISTRIBUTION;\r
+    private double minValue = DEFAULT_MIN_VALUE;\r
+    private double maxValue = DEFAULT_MAX_VALUE;\r
+    private int numValues = DEFAULT_NUM_VALUES;\r
+    private String variedParameter = "Auxiliary1";\r
     \r
     public static Distribution DEFAULT_PROPABILITY_DISTRIBUTION = Distribution.UNIFORM;\r
     public static double DEFAULT_MIN_VALUE = 1.0;\r
     public static double DEFAULT_MAX_VALUE = 3.0;\r
     public static int DEFAULT_NUM_VALUES = 3;\r
-    \r
-    private final Collection<Runnable> timeListeners = new ArrayList<Runnable>();\r
 \r
     ScheduledExecutorService playbackExecutionService;\r
-    //SensitivityAnalysisConfiguration playbackConfiguration;\r
     private ArrayList<MemoryResult> results = null;\r
 \r
     public SysdynSensitivityAnalysisExperiment(Resource experiment, Resource model) {\r
         super(experiment, model);\r
     }\r
 \r
-    \r
-    /**\r
-     * Starts simulating a model. Call only from inside simulation job! Start simulation using simulate(true).\r
-     * @param monitor\r
-     * @param progressMonitor\r
-     * @throws IOException\r
-     */\r
     @Override\r
-    public synchronized void simulate(final IModelicaMonitor monitor, final IProgressMonitor progressMonitor) throws IOException {\r
-        canceled = false;\r
-\r
-        progressMonitor.subTask("Write modelica classes");\r
-\r
-        // Write Modelica files\r
-\r
-        \r
-        String modelText = getModelicaCode(monitor, false, getOpenModelicaVersion());\r
-        if(modelText == null)\r
-            return;\r
-\r
-        progressMonitor.worked(1);\r
-\r
-        // 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
-        // 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
-        if (!simulationLocation.executableFile.isFile() || structureChanged) {\r
-            progressMonitor.subTask("Build model");\r
-            buildModel(simulationLocation, modelText, monitor);\r
-        }\r
+    protected void runModelica(SimulationLocation simulationLocation, String modelText, IModelicaMonitor monitor, IProgressMonitor progressMonitor, HashMap<String, String> experimentParameters, HashMap<String, String> changes) throws IOException {\r
+        results = null;\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
+        loadConfiguration();\r
+        if (changes == null) {\r
+            changes = new HashMap<String, String>();\r
         }\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
-                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
+        if (propabilityDistribution == Distribution.UNIFORM) {\r
+            double intervalLength = (maxValue - minValue) / (numValues - 1);\r
+            for (int i = 0; i < numValues; ++i) {\r
+                double value = minValue + (intervalLength * i);\r
+                \r
+                // TODO: What if changes already include similarly named variable?\r
+                /* If the changes is altered, it affects also to the next simulation. So,\r
+                 * All the parameters that are to be altered need to be defined explicitly\r
+                 * before each new simulation.   \r
+                 */\r
+                changes.put(variedParameter, Double.toString(value));\r
+                \r
+                // Simulate the model for one parameter set\r
+                runModelica(simulationLocation, modelText, monitor, progressMonitor, experimentParameters, changes, i);\r
             }\r
+        } else if (propabilityDistribution == Distribution.NORMAL) {\r
+            //  NOT IMPLEMENTED YET\r
         }\r
+    }\r
 \r
-        progressMonitor.worked(1);\r
-\r
-        if(simulationLocation != null && !canceled) {\r
-            \r
-            results = null;\r
-            // Simulate the model\r
-            \r
-            /* If the changes is altered, it affects also to the next simulation. So,\r
-             * All the parameters that are to be altered need to be defined explicitly\r
-             * before each new simulation.   \r
-             */\r
-            \r
-            changes = new HashMap<String, String>();\r
-            changes.put("Auxiliary1", "444.0");\r
-            runModelica(simulationLocation, modelText, monitor, progressMonitor, experimentParameters, changes, 0);\r
-            changes.put("Auxiliary1", "333.0");\r
-            runModelica(simulationLocation, modelText, monitor, progressMonitor, experimentParameters, changes, 1);\r
-            changes.put("Auxiliary1", "222.0");\r
-            runModelica(simulationLocation, modelText, monitor, progressMonitor, experimentParameters, changes, 2);\r
+    private void loadConfiguration(){\r
+        try {\r
+            session.syncRequest(new ReadRequest() {\r
+                @Override\r
+                public void run(ReadGraph graph) throws DatabaseException {\r
+                    \r
+                    SysdynResource sr = SysdynResource.getInstance(graph);\r
+                    maxValue = graph.getPossibleRelatedValue(experiment, sr.SensitivityAnalysisExperiment_maxValue, Bindings.DOUBLE);\r
+                    minValue = graph.getPossibleRelatedValue(experiment, sr.SensitivityAnalysisExperiment_minValue, Bindings.DOUBLE);\r
+                    numValues = graph.getPossibleRelatedValue(experiment, sr.SensitivityAnalysisExperiment_numValues, Bindings.INTEGER);\r
+                    variedParameter = graph.getPossibleRelatedValue(experiment, sr.SensitivityAnalysisExperiment_variedParameter, Bindings.STRING);\r
+                    String propabilityDistributionTemp = graph.getPossibleRelatedValue(experiment, sr.SensitivityAnalysisExperiment_propabilityDistribution, Bindings.STRING);\r
+                    if (propabilityDistributionTemp.equalsIgnoreCase("uniform"))\r
+                        propabilityDistribution = Distribution.UNIFORM;\r
+                    else if (propabilityDistributionTemp.equalsIgnoreCase("normal"))\r
+                        propabilityDistribution = Distribution.NORMAL;\r
+                    else\r
+                        propabilityDistribution = DEFAULT_PROPABILITY_DISTRIBUTION;\r
+                }\r
+            });\r
+        } catch (DatabaseException e) {\r
+            // TODO Auto-generated catch block\r
+            e.printStackTrace();\r
         }\r
-        if(canceled)\r
-            simulate(false);\r
-        process = null;\r
     }\r
     \r
     protected File renameResultFile(SimulationLocation simulationLocation, int resFileIndex) {\r
@@ -324,114 +291,4 @@ public class SysdynSensitivityAnalysisExperiment extends SysdynExperiment {
         //saveResultJob.schedule();\r
     }\r
     \r
-    //this.results.add(new MemoryResult(null, null));\r
-\r
-    \r
-    /**\r
-     * Interrupts a possible ongoing playback\r
-     * \r
-     * @param time\r
-     */\r
-//    public void setTimeInterrupting(double time) {\r
-//        stopPlayback();\r
-//        setTime(time);\r
-//    }\r
-//\r
-//    /**\r
-//     * Sets a new time and continues playback from that point if \r
-//     * playback was running  \r
-//     * @param time\r
-//     */\r
-//    public void setTimeAndContinue(double time) {\r
-//        if(isPlaybackRunning()) {\r
-//            stopPlayback();\r
-//            setTime(time);\r
-//            startPlayback(500);\r
-//        } else {\r
-//            setTime(time);\r
-//        }\r
-//    }\r
-/*\r
-  private void setTime(double time) {\r
-        this.time = time;\r
-        resultsChanged(); \r
-    }\r
-\r
-    public double getTime() {\r
-        return this.time;\r
-    }\r
-\r
-    public double getStartTime() {\r
-        return this.startTime;\r
-    }\r
-\r
-    public double getEndTime() {\r
-        return this.endTime;\r
-    }\r
-\r
-    public void setPlaybackDuration(long duration) {\r
-        this.playbackDuration = duration;\r
-        if(isPlaybackRunning()) {\r
-            //Restart playback with different time settings\r
-            startPlayback();\r
-        }\r
-    }\r
-\r
-    public long getPlaybackDuration() {\r
-        return this.playbackDuration;\r
-    }\r
-\r
-    @Override\r
-    public void init(ReadGraph g) {\r
-        super.init(g);\r
-        this.session = g.getSession();\r
-        session.asyncRequest(new ReadRequest() {\r
-\r
-            @Override\r
-            public void run(ReadGraph graph) throws DatabaseException {\r
-                changeState(ExperimentState.RUNNING);\r
-                final Resource configuration = graph.getPossibleObject(model, SimulationResource.getInstance(graph).HasConfiguration);\r
-                sysdynModel = SysdynModelManager.getInstance(session).getModel(graph, configuration);\r
-                toggleActivation(graph, true);\r
-                //getPlaybackConfiguration(graph);\r
-                startSimulationJob();\r
-            }\r
-        });\r
-    }\r
-\r
-\r
-\r
-    private class SensitivityAnalysisConfiguration {\r
-        public double simulationDuration, simulationStepLength, intervals, endTime, startTime;\r
-        public long playbackDuration;\r
-    }\r
-\r
-    @Override\r
-    protected void localStateChange() {\r
-        super.localStateChange();\r
-    }\r
-\r
-    // TIME LISTENERS\r
-    public void addTimeListener(Runnable timeListener) {\r
-        if(!this.timeListeners.contains(timeListener))\r
-            this.timeListeners.add(timeListener);\r
-    }\r
-\r
-    public Collection<Runnable> getTimeListeners() {\r
-        return this.timeListeners;\r
-    }\r
-\r
-    public void removeTimeListener(Runnable timeListener) {\r
-        this.timeListeners.remove(timeListener);\r
-    }\r
-\r
-    @Override\r
-    public void resultsChanged() {\r
-        for(Runnable listener : timeListeners) {\r
-            listener.run();\r
-        }\r
-        super.resultsChanged();\r
-\r
-    }\r
-*/\r
 }\r