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