]> gerrit.simantics Code Review - simantics/sysdyn.git/commitdiff
Implement global variables startTime, stopTime and timeStep which can be used to...
authorjkauttio <jkauttio@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Tue, 3 Feb 2015 12:34:23 +0000 (12:34 +0000)
committerjkauttio <jkauttio@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Tue, 3 Feb 2015 12:34:23 +0000 (12:34 +0000)
fixes #5033

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

org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/modelica/ModelicaSourceViewerConfiguration.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/modelica/SysdynModelicaEditor.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/ShortcutTabWidget.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/utils/ExpressionUtils.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/validation/DependencyFunction.java
org.simantics.sysdyn/src/org/simantics/sysdyn/manager/OldSysdynExperiment.java
org.simantics.sysdyn/src/org/simantics/sysdyn/modelica/ModelicaWriter.java
org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Model.java
org.simantics.sysdyn/src/org/simantics/sysdyn/solver/InternalSolver.java

index 84c0055fc5699b8f726404792a5a25eba586b723..f6f67f696452fc64d28e706844437515b7e0264e 100644 (file)
@@ -28,6 +28,7 @@ import org.eclipse.jface.text.source.ISourceViewer;
 import org.eclipse.jface.text.source.SourceViewerConfiguration;\r
 import org.eclipse.swt.SWT;\r
 import org.eclipse.swt.graphics.Color;\r
+import org.simantics.sysdyn.modelica.ModelicaWriter;\r
 \r
 public class ModelicaSourceViewerConfiguration extends SourceViewerConfiguration {\r
 \r
@@ -151,6 +152,11 @@ public class ModelicaSourceViewerConfiguration extends SourceViewerConfiguration
         keywords.add("Boolean");\r
         keywords.add("Integer");\r
         keywords.add("String");\r
+        // TODO: is this the correct place to add these extra variables?\r
+        keywords.add(ModelicaWriter.VAR_TIME);\r
+        keywords.add(ModelicaWriter.VAR_START);\r
+        keywords.add(ModelicaWriter.VAR_STOP);\r
+        keywords.add(ModelicaWriter.VAR_STEP);\r
     }\r
 \r
        static IToken modelicaKeywordToken = null;\r
index 6d4757de62ff680c45703de38fd5728143087b4c..bc00c02ab7fea12d841c311b327fdbc87e05d6e0 100644 (file)
@@ -93,7 +93,12 @@ public class SysdynModelicaEditor extends TextEditor {
                     getConfigurations(configuration, configurations);\r
                     \r
                     String version = ModelicaManager.getDefaultOMVersion();\r
-                    return new Document(ModelicaWriter.write(configurations, configuration.getModel().getStartTime(), RepresentationUtils.isGameExperimentActive(), version));\r
+                    return new Document(ModelicaWriter.write(configurations, \r
+                               configuration.getModel().getStartTime(), \r
+                               configuration.getModel().getStopTime(), \r
+                               configuration.getModel().getSimulationStepLength(), \r
+                               RepresentationUtils.isGameExperimentActive(), \r
+                               version));\r
                 } catch (DatabaseException e) {\r
                     e.printStackTrace();\r
                     throw new CoreException(STATUS_ERROR);\r
index 56488ec76f5bb829d04ca1a67605e3099093b086..9b69fc07d2d94aaff2cd2b5462c6f5dcc1267a9b 100644 (file)
@@ -44,6 +44,7 @@ import org.simantics.db.management.ISessionContext;
 import org.simantics.db.procedure.AsyncListener;\r
 import org.simantics.db.request.Read;\r
 import org.simantics.layer0.Layer0;\r
+import org.simantics.sysdyn.modelica.ModelicaWriter;\r
 import org.simantics.sysdyn.ui.Activator;\r
 import org.simantics.sysdyn.ui.validation.ValidationUtils;\r
 import org.simantics.sysdyn.utils.Function;\r
@@ -192,8 +193,20 @@ public class ShortcutTabWidget implements Widget {
                                     item.setData(selfName);\r
                                     \r
                                     item = new TableItem(variableTable, SWT.NONE);\r
-                                    item.setText("time");\r
-                                    item.setData("time");\r
+                                    item.setText(ModelicaWriter.VAR_TIME);\r
+                                    item.setData(ModelicaWriter.VAR_TIME);\r
+                                    \r
+                                    item = new TableItem(variableTable, SWT.NONE);\r
+                                    item.setText(ModelicaWriter.VAR_START);\r
+                                    item.setData(ModelicaWriter.VAR_START);\r
+                                    \r
+                                    item = new TableItem(variableTable, SWT.NONE);\r
+                                    item.setText(ModelicaWriter.VAR_STOP);\r
+                                    item.setData(ModelicaWriter.VAR_STOP);\r
+                                    \r
+                                    item = new TableItem(variableTable, SWT.NONE);\r
+                                    item.setText(ModelicaWriter.VAR_STEP);\r
+                                    item.setData(ModelicaWriter.VAR_STEP);\r
                                                }\r
                                                \r
                                                                synchronized(dependencyListeners) {\r
index 70a0d43fe82da96edeacd4cdeb9b111b993353d3..46fa9c5dc99fdbf8dbdbc171e079489146798b80 100644 (file)
@@ -43,6 +43,7 @@ import org.simantics.sysdyn.expressionParser.Token;
 import org.simantics.sysdyn.expressionParser.TokenMgrError;\r
 import org.simantics.sysdyn.manager.SysdynModel;\r
 import org.simantics.sysdyn.manager.SysdynModelManager;\r
+import org.simantics.sysdyn.modelica.ModelicaWriter;\r
 import org.simantics.sysdyn.representation.Configuration;\r
 import org.simantics.sysdyn.representation.Enumeration;\r
 import org.simantics.sysdyn.representation.EnumerationIndex;\r
@@ -220,10 +221,11 @@ public class ExpressionUtils {
                 }\r
             }\r
 \r
-            // VARIABLE NAMES\r
-\r
-            if(variables.contains("time"))\r
-                variables.remove("time");\r
+            // Handle reserved variables later\r
+            variables.remove(ModelicaWriter.VAR_TIME);\r
+            variables.remove(ModelicaWriter.VAR_START);\r
+            variables.remove(ModelicaWriter.VAR_STOP);\r
+            variables.remove(ModelicaWriter.VAR_STEP);\r
             \r
             // Remove variable references to for indices\r
             for(ExpressionField ef : forIndices.keySet()) {\r
@@ -298,13 +300,18 @@ public class ExpressionUtils {
             // Color the items in the table\r
             TableItem[] connectedVariables = variableTable.getItems();\r
             for(TableItem ti : connectedVariables) {\r
-                if (ti.getText().equals("time") || ti.getText().equals(selfName)) {\r
+               String text = ti.getText();\r
+                if (text.equals(ModelicaWriter.VAR_TIME) || \r
+                               text.equals(ModelicaWriter.VAR_START) || \r
+                               text.equals(ModelicaWriter.VAR_STOP) || \r
+                               text.equals(ModelicaWriter.VAR_STEP) || \r
+                               text.equals(selfName)) {\r
                     ti.setForeground(variableTimeAndSelfColor(resourceManager));\r
-                } else if (parsingSucceeded && !variables.contains(ti.getText())) {\r
+                } else if (parsingSucceeded && !variables.contains(text)) {\r
                     ti.setForeground(variableNotFoundColor(resourceManager));\r
                 } else if (parsingSucceeded) {\r
                     ti.setForeground(variableFoundColor(resourceManager));\r
-                    variables.remove(ti.getText());\r
+                    variables.remove(text);\r
                 }\r
             }\r
 \r
@@ -331,8 +338,9 @@ public class ExpressionUtils {
             // ENUMERATION REFERENCES IN FOR-LOOPS\r
             errors.addAll(examineEnumerationReferences(conf, enumerationReferences.get(ef)));\r
 \r
-            for(SyntaxError error : errors)\r
+            for(SyntaxError error : errors) {\r
                 ef.setSyntaxError(error);\r
+            }\r
         }                                      \r
     }\r
     \r
index 99865d007c2258fd182b2c99e75fb60e2bd7a390..090ca367408ee73a344ea9c8099b8d52ef8eb779 100644 (file)
@@ -28,6 +28,7 @@ import org.simantics.sysdyn.SysdynResource;
 import org.simantics.sysdyn.expressionParser.Token;\r
 import org.simantics.sysdyn.manager.SysdynModel;\r
 import org.simantics.sysdyn.manager.SysdynModelManager;\r
+import org.simantics.sysdyn.modelica.ModelicaWriter;\r
 import org.simantics.sysdyn.representation.Configuration;\r
 import org.simantics.sysdyn.ui.properties.widgets.expressions.ExpressionField;\r
 import org.simantics.sysdyn.ui.utils.ExpressionUtils;\r
@@ -44,7 +45,8 @@ import org.simantics.utils.datastructures.collections.CollectionUtils;
 public class DependencyFunction {\r
        \r
        // Set containing the names of variables that can be used everywhere, like "time"\r
-       private static Set<String> GLOBAL_VARIABLES = CollectionUtils.toSet("time");\r
+       private static Set<String> GLOBAL_VARIABLES = CollectionUtils.toSet(ModelicaWriter.VAR_TIME, \r
+                       ModelicaWriter.VAR_START, ModelicaWriter.VAR_STOP, ModelicaWriter.VAR_STEP);\r
 \r
     /**\r
      * Evaluates dependency-related issues for a component.\r
index f6e2d507ec7b6d35a08bd29811a7aceb8e92c274..f1701ee9f3ec5dab0e74fa4c1bee7b22a84c2e83 100644 (file)
@@ -245,7 +245,11 @@ public class OldSysdynExperiment extends SysdynExperiment {
         String modelText = null;\r
         try {\r
             // Write all configurations once\r
-            modelText = ModelicaWriter.write(sysdynModel.getModules(), sysdynModel.getConfiguration().getModel().getStartTime(), isGame, modelicaVersion);\r
+               Model representation = sysdynModel.getConfiguration().getModel();\r
+               double start = representation.getStartTime();\r
+               double stop = representation.getStopTime();\r
+               double step = representation.getSimulationStepLength();\r
+            modelText = ModelicaWriter.write(sysdynModel.getModules(), start, stop, step, isGame, modelicaVersion);\r
         } catch (Exception e) {\r
             // Stop experiment and show console\r
             /*setExperimentStopped(experiment); =>*/ simulate(false);\r
@@ -262,7 +266,6 @@ public class OldSysdynExperiment extends SysdynExperiment {
      */\r
     protected HashMap<String, String> getExperimentParameters(IModelicaMonitor monitor) {\r
         Configuration configuration = sysdynModel.getConfiguration();\r
-//        HashMap<String, String> inits = sysdynModel.getInits(configuration, ""); Not needed anymore, whole xml content is replaced\r
         HashMap<String, String> parameters = new HashMap<String, String>();\r
         Model model = configuration.getModel();\r
         Double startTime = model.getStartTime();\r
@@ -273,15 +276,11 @@ public class OldSysdynExperiment extends SysdynExperiment {
         parameters.put(ModelicaKeys.OUTPUT_FORMAT, outputFormat);\r
 \r
         Double simulationStepLength = model.getSimulationStepLength();\r
-        if(simulationStepLength != null) {\r
-            parameters.put(ModelicaKeys.STEP_VALUE, simulationStepLength.toString());\r
-            parameters.put(ModelicaKeys.NUMBER_OF_INTERVALS, "" + ((int)((stopTime - startTime) / simulationStepLength)));\r
-        } else {\r
-            parameters.put(ModelicaKeys.STEP_VALUE, "" + (stopTime - startTime) / 500);\r
-        }\r
+        parameters.put(ModelicaKeys.STEP_VALUE, simulationStepLength.toString());\r
+        parameters.put(ModelicaKeys.NUMBER_OF_INTERVALS, Integer.toString((int)((stopTime - startTime) / simulationStepLength)));\r
 \r
         Double outputInterval = model.getOutputInterval();\r
-        parameters.put(ModelicaKeys.OUTPUT_INTERVAL, outputInterval != null ? outputInterval.toString() : parameters.get(ModelicaKeys.STEP_VALUE));\r
+        parameters.put(ModelicaKeys.OUTPUT_INTERVAL, outputInterval.toString());\r
 \r
         String method = "\"" + model.getSolver() + "\"";\r
         parameters.put(ModelicaKeys.METHOD, method);\r
index 87dd09e2ca44985ddf162e99e77d34ebab700365..931b1f3845ea6c922f929e4e3acdc90f8dcc7f6f 100644 (file)
@@ -42,6 +42,11 @@ import org.simantics.sysdyn.representation.expressions.IExpression;
  *\r
  */\r
 public class ModelicaWriter {\r
+       \r
+       public static final String VAR_TIME = "time";\r
+       public static final String VAR_START = "startTime";\r
+       public static final String VAR_STOP = "stopTime";\r
+       public static final String VAR_STEP = "timeStep";\r
 \r
        /**\r
         * Write a collection of configurations into a single Modelica code\r
@@ -50,7 +55,7 @@ public class ModelicaWriter {
         * @param Configurations Configurations, one main configuration and possible modules\r
         * @return Complete Modelica code of a model\r
         */\r
-       public static String write(Collection<Configuration> _configurations, double startTime, boolean isGame, String omVersion) {\r
+       public static String write(Collection<Configuration> _configurations, double startTime, double stopTime, double timeStep, boolean isGame, String omVersion) {\r
 \r
                ArrayList<Configuration> configurations = new ArrayList<Configuration>(_configurations);\r
                Collections.sort(configurations, new Comparator<Configuration>() {\r
@@ -137,6 +142,13 @@ public class ModelicaWriter {
                HashSet<String> sheetNames = new HashSet<String>();\r
                for(Sheet sheet : getSpreadSheets(configurations))\r
                        sheetNames.add(sheet.getModelicaName());\r
+               \r
+               // write global time parameters (could be moved to writeConfiguration())\r
+               b.append("// Simulation parameters\n");\r
+               b.append("inner parameter Real ").append(VAR_START).append(" = ").append(startTime).append(";\n");\r
+               b.append("inner parameter Real ").append(VAR_STOP).append(" = ").append(stopTime).append(";\n");\r
+               b.append("inner parameter Real ").append(VAR_STEP).append(" = ").append(timeStep).append(";\n");\r
+               b.append('\n');\r
 \r
                // Write all module configurations to the declarations part (first)\r
                for(Configuration conf : configurations) {\r
@@ -278,6 +290,13 @@ public class ModelicaWriter {
                                b.append(e.getDeclaration());\r
                        }\r
                }\r
+               \r
+               if (mt != null) {\r
+                       b.append("// References to simulation parameters\n");\r
+                       b.append("    outer Real ").append(VAR_START).append(";\n");\r
+                       b.append("    outer Real ").append(VAR_STOP).append(";\n");\r
+                       b.append("    outer Real ").append(VAR_STEP).append(";\n");\r
+               }\r
 \r
                b.append("// Variable definitions\n");\r
                for(IndependentVariable variable : variables) {\r
index 76f28a476979fd13b025d374a688d7b895820307..51a1e843526afa501af5425b4257191f6b46877e 100644 (file)
@@ -91,18 +91,24 @@ public class Model {
 \r
     /**\r
      * Simulation output interval\r
+     * \r
+     * Default value: simulation step\r
+     * \r
      * @return\r
      */\r
     public Double getOutputInterval() {\r
-        return outputInterval;\r
+        return outputInterval != null ? outputInterval : getSimulationStepLength();\r
     }\r
 \r
     /**\r
      * Simulation step length\r
+     * \r
+     * Default value: Simulation duration / 500\r
+     * \r
      * @return\r
      */\r
     public Double getSimulationStepLength() {\r
-       return stepLength;\r
+       return stepLength != null ? stepLength : (getStopTime() - getStartTime()) / 500;\r
     }\r
     /**\r
      * \r
index e7cd7bf152d82444d40581e567057b5a1996c8b2..846f3701db919ffbdb1ed2fb235dc080c1e43beb 100644 (file)
@@ -58,13 +58,13 @@ public class InternalSolver implements ISolver {
                Model representation = model.getConfiguration().getModel();\r
                // TODO: should probably check that the values actually make sense\r
                // i.e. start and stop >= 0, start <= stop, stop - start >= step\r
-               if (representation.getStartTime() != null)\r
-                       start = representation.getStartTime();\r
-               if (representation.getStopTime() != null)\r
-                       stop = representation.getStopTime();\r
+               start = representation.getStartTime();\r
+               stop = representation.getStopTime();\r
+               step = representation.getSimulationStepLength();\r
+               interval = representation.getOutputInterval();\r
                \r
 //             String omVersion = ModelicaManager.getDefaultOMVersion();\r
-               String modelContent = ModelicaWriter.write(model.getModules(), start, isGame, "1.9");\r
+               String modelContent = ModelicaWriter.write(model.getModules(), start, stop, step, isGame, "1.9");\r
 \r
                // update some stuff\r
                FunctionUtils.updateFunctionFilesForExperiment(experiment);\r
@@ -72,16 +72,6 @@ public class InternalSolver implements ISolver {
                location = ModelicaManager.createSimulationLocation(experiment.getExperimentDir(), \r
                                model.getConfiguration().getLabel(), modelContent, null, false);\r
                \r
-               if (representation.getSimulationStepLength() != null)\r
-                       step = representation.getSimulationStepLength();\r
-               else\r
-                       step = (stop - start) / 500;\r
-                       \r
-               if (representation.getOutputInterval() != null)\r
-                       interval = representation.getOutputInterval();\r
-               else\r
-                       interval = step;\r
-               \r
                solver.setStep(step);\r
                solver.setStart(start);\r
        }\r