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
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
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
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
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
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
}\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
// 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
// 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
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
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
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
*/\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
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
*\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
* @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
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
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
\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
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
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