From: jkauttio Date: Tue, 17 Jun 2014 11:34:16 +0000 (+0000) Subject: Change modelica code generation for stocks to correctly define initial values when... X-Git-Tag: 1.8.1~23 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=901b4039cc6a3e927fb6913d2e17cd7ee4c86a46;p=simantics%2Fsysdyn.git Change modelica code generation for stocks to correctly define initial values when enumerations are used. fixes #4932 git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@29635 ac1ea38d-2e2b-0410-8846-a27921b304fc --- 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 e2421490..f2c0a1b7 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelica/ModelicaWriter.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelica/ModelicaWriter.java @@ -265,7 +265,9 @@ public class ModelicaWriter { b.append("// Variable definitions\n"); for(IndependentVariable variable : variables) { app = variable.getDeclaration(); - if (app != null) b.append(app); + if (app != null) { + b.append(app); + } } if(defTime) { diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/IndependentVariable.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/IndependentVariable.java index 3c0decd6..42452998 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/IndependentVariable.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/IndependentVariable.java @@ -72,7 +72,7 @@ public abstract class IndependentVariable extends Variable { sb.append(addition); } } - + return sb.toString(); } @@ -113,6 +113,7 @@ public abstract class IndependentVariable extends Variable { if(initialEquation != null) sb.append(initialEquation); } + String result = sb.toString(); return result.length() > 0 ? result : null; } @@ -122,8 +123,7 @@ public abstract class IndependentVariable extends Variable { * @return Equation or null */ public String getEquation() { - Variability variability = Variability.getVariability(this); - if(variability == Variability.CONTINUOUS) { + if(Variability.getVariability(this) == Variability.CONTINUOUS) { return getVariableEquation(); } else { return null; diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Stock.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Stock.java index 54c1d29d..e706bc22 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Stock.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Stock.java @@ -56,13 +56,13 @@ public class Stock extends IndependentVariable { @Override public String getDeclaration() { - - String each = ""; + // is it really guaranteed that variability is the only necessary + // parameter to consider when deciding whether to use start=value + // notation or initial equations? + String each = (getArrayIndexes() != null && !getArrayIndexes().isEmpty()) ? "each " : ""; // each is required when a single value is used for all dimensions e.g. Stock[30](each start = 0) if (Variability.getVariability(this) == Variability.CONTINUOUS) { // start parameter is not used, everything needs to be fixed=false - if(getArrayIndexes() != null && !getArrayIndexes().isEmpty()) - each = "each "; return " " + getType() + " " + getModelicaName() + getRange() + "(" + each + "fixed=false);\n"; } else { // Start parameter is used. Parameter guarantees that there is only one expression. @@ -70,17 +70,12 @@ public class Stock extends IndependentVariable { String initialEquation = e.getModelicaExpression(); initialEquation = SheetFormatUtils.reformatSheetReferences(this, initialEquation); - if(getArrayIndexes() != null && !getArrayIndexes().isEmpty()) - each = "each "; - - // TODO: there is probably a bug with startvalue here return " " + getType() + " " + getModelicaName() + getRange() + "(" + - (e.getStartValue() != null ? each : "") + "start=" + initialEquation + ", " + diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/StockExpression.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/StockExpression.java index a6efc5e2..3fb5f59f 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/StockExpression.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/StockExpression.java @@ -11,21 +11,14 @@ *******************************************************************************/ package org.simantics.sysdyn.representation.expressions; -import java.io.StringReader; import java.util.ArrayList; -import java.util.Set; import org.simantics.db.ReadGraph; import org.simantics.objmap.annotations.GraphType; import org.simantics.objmap.annotations.RelatedValue; import org.simantics.sysdyn.SysdynResource; -import org.simantics.sysdyn.expressionParser.ExpressionParser; -import org.simantics.sysdyn.expressionParser.ParseException; import org.simantics.sysdyn.manager.SysdynModel; -import org.simantics.sysdyn.representation.Book; import org.simantics.sysdyn.representation.Enumeration; -import org.simantics.sysdyn.representation.IElement; -import org.simantics.sysdyn.representation.Sheet; import org.simantics.sysdyn.representation.Stock; import org.simantics.sysdyn.representation.Valve; import org.simantics.sysdyn.representation.utils.FormatUtils; @@ -112,69 +105,13 @@ public class StockExpression extends Expression { } return b.toString(); } - - /** - * Check whether to use fixed=true and start=... in Modelica code - * @return - */ - private boolean useStartValue() { - // If no variables are used in the equation, start value is used - - // First the equation is formatted and parsed - String equation = FormatUtils.formatExpressionForModelica(parent, initialEquation); - ExpressionParser parser = new ExpressionParser(new StringReader(equation)); - try { - parser.expr(); - if(parser.getReferences().isEmpty()) { - // if equation did not contain any references, start value is used - return true; - } else { - // Check if references are references to sheets. - // Sheet references are allowed, since sheets contain only constants - boolean found = false; - Set references = parser.getReferences().keySet(); - - // Go through each reference - for(String reference : references) { - // We only need the first element to know that it is a Sheet (SheetName.CellOrRange) - reference = reference.split("\\.")[0]; - found = false; - for(IElement element : parent.getParentConfiguration().getElements()) { - if(element instanceof Book) { - for(Sheet sheet : ((Book)element).getSheets()) { - if(reference.equals(sheet.getName())) { - found = true; - break; - } - } - } - if(found) - break; - } - - // If there was no sheet for this reference name, return false - if(!found) - return false; - } - } - } catch (ParseException e) { - e.printStackTrace(); - } - return true; - } - @Override public String getInitialEquation() { String initialEquation = FormatUtils.replaceWhitespace(this.initialEquation); - // if start value is used, no initial equation is returned - if(useStartValue()) - return null; // format the initial equation for modelica execution String equation = FormatUtils.formatExpressionForModelica(parent, initialEquation, false); String range = IndexUtils.rangeToIndexes(parent, this.getArrayRange()); - if(range == null) - range = ""; - return " " + parent.getModelicaName() + range + " = " + equation + ";\n"; + return " " + parent.getModelicaName() + (range != null ? range : "") + " = " + equation + ";\n"; }