Skip to content

Commit

Permalink
- BackendVariable.mo
Browse files Browse the repository at this point in the history
  - reimplement isVariable
- BackendDAEOptimize.mo
  - use new feature removeParameterEqns
- SimCode.mo
  - implement new feature: sort the bound parameter equations in right order. 
- fix some testcases of mosfiles
  - AlgorithmArrayEqn.mos: delete generated files
  - EventIteration.mos: delete generated files
  - InOutStrings.mo: use parameter to test also simulation
  - ModelBalance1.mos: delete generated files
  - ModelBalance3.mos: change number of equations because of new feature remove parameter eqns

git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@7302 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Jens Frenkel committed Dec 7, 2010
1 parent 7ffa927 commit 18d67a8
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 63 deletions.
21 changes: 10 additions & 11 deletions Compiler/BackendDAEOptimize.mo
Expand Up @@ -644,7 +644,6 @@ algorithm
list<Integer> meqns;
case (dlow as BackendDAE.DAE(ordvars,knvars,exobj,av,eqns,remeqns,inieqns,arreqns,algorithms,einfo,eoc),m,mT)
equation
true = RTOpts.debugFlag("optimizeParameter");
// check equations
(m_1,(ordvars1,knvars1,eqns1,_,movedVars,meqns)) = traverseIncidenceMatrix(m,removeParameterEqnsFinder,(ordvars,knvars,eqns,mT,BackendDAE.emptyBintree,{}));
// move changed variables
Expand Down Expand Up @@ -753,8 +752,8 @@ algorithm
eqn = BackendDAEUtil.equationNth(eqns,pos_1);
BackendDAE.EQUATION(exp=e1,scalar=e2) = eqn;
// variable time not there
((_,false)) = Expression.traverseExp(e1, traversingComponentRefTimeFinder, false);
((_,false)) = Expression.traverseExp(e2, traversingComponentRefTimeFinder, false);
((_,false)) = Expression.traverseExpTopDown(e1, traversingParameterEqnsFinder, false);
((_,false)) = Expression.traverseExpTopDown(e2, traversingParameterEqnsFinder, false);
cre = Expression.crefExp(cr);
(es,{}) = ExpressionSolve.solve(e1,e2,cre);
// set kind to PARAM
Expand All @@ -780,23 +779,23 @@ algorithm
end matchcontinue;
end removeParameterEqnsFinder;

public function traversingComponentRefTimeFinder "
public function traversingParameterEqnsFinder "
Author: Frenkel 2010-12"
input tuple<DAE.Exp, Boolean > inExp;
output tuple<DAE.Exp, Boolean > outExp;
output tuple<DAE.Exp,Boolean, Boolean > outExp;
algorithm
outExp := matchcontinue(inExp)
local
DAE.Exp e;
Boolean b;

case((e as DAE.CREF(DAE.CREF_IDENT(ident = "time",subscriptLst = {}),_), _))
then
((e, true ));

case(inExp) then inExp;
case((e as DAE.CREF(DAE.CREF_IDENT(ident = "time",subscriptLst = {}),_), _)) then ((e,false,true));
case((e as DAE.CALL(path = Absyn.IDENT(name = "sample"), expLst = {_,_}), _)) then ((e,false,true ));
case((e as DAE.CALL(path = Absyn.IDENT(name = "pre"), expLst = {_}), _)) then ((e,false,true ));
case((e,b)) then ((e,not b,b));

end matchcontinue;
end traversingComponentRefTimeFinder;
end traversingParameterEqnsFinder;

protected function setbindValue
" function: setbindValue
Expand Down
56 changes: 29 additions & 27 deletions Compiler/BackendVariable.mo
Expand Up @@ -2190,44 +2190,46 @@ algorithm
local
DAE.ComponentRef cr;
BackendDAE.Variables vars,knvars;
BackendDAE.VarKind kind;
case (cr,vars,_)
equation
((BackendDAE.VAR(varKind = BackendDAE.VARIABLE()) :: _),_) = getVar(cr, vars);
then
();
case (cr,vars,_)
equation
((BackendDAE.VAR(varKind = BackendDAE.STATE()) :: _),_) = getVar(cr, vars);
then
();
case (cr,vars,_)
equation
((BackendDAE.VAR(varKind = BackendDAE.DUMMY_STATE()) :: _),_) = getVar(cr, vars);
then
();
case (cr,vars,_)
equation
((BackendDAE.VAR(varKind = BackendDAE.DUMMY_DER()) :: _),_) = getVar(cr, vars);
((BackendDAE.VAR(varKind = kind) :: _),_) = getVar(cr, vars);
isVarKindVariable(kind);
then
();
case (cr,_,knvars)
equation
((BackendDAE.VAR(varKind = BackendDAE.VARIABLE()) :: _),_) = getVar(cr, knvars);
then
();
case (cr,_,knvars)
equation
((BackendDAE.VAR(varKind = BackendDAE.DUMMY_STATE()) :: _),_) = getVar(cr, knvars);
then
();
case (cr,_,knvars)
equation
((BackendDAE.VAR(varKind = BackendDAE.DUMMY_DER()) :: _),_) = getVar(cr, knvars);
((BackendDAE.VAR(varKind = kind) :: _),_) = getVar(cr, knvars);
isVarKindVariable(kind);
then
();
end matchcontinue;
end isVariable;

public function isVarKindVariable
"function: isVarKindVariable

This function takes a DAE.ComponentRef and two Variables. It searches
the two sets of variables and succeed if the variable is STATE or
VARIABLE. Otherwise it fails.
Note: An array variable is currently assumed that each scalar element has
the same type.
inputs: (DAE.ComponentRef,
Variables, /* vars */
Variables) /* known vars */
outputs: ()"
input BackendDAE.VarKind inVarKind;
algorithm
_:=
matchcontinue (inVarKind)
case (BackendDAE.VARIABLE()) then ();
case (BackendDAE.STATE()) then ();
case (BackendDAE.DUMMY_STATE()) then ();
case (BackendDAE.DUMMY_DER()) then ();
case (BackendDAE.DISCRETE()) then ();
end matchcontinue;
end isVarKindVariable;

public function moveVariables
"function: moveVariables
This function takes the two variable lists of a dae (states+alg) and
Expand Down
100 changes: 75 additions & 25 deletions Compiler/SimCode.mo
Expand Up @@ -4613,19 +4613,48 @@ algorithm
parameterEquations := matchcontinue (dlow)
local
list<BackendDAE.Equation> parameterEquationsTmp;
BackendDAE.Variables knvars;
BackendDAE.Variables knvars,extobj,v,kn;
array<Algorithm.Algorithm> algs;
BackendDAE.EquationArray ie;
BackendDAE.EquationArray ie,pe,emptyeqns;
list<SimEqSystem> inalgs;
list<DAE.Algorithm> ialgs;
list<DAE.Algorithm> ialgs;
BackendDAE.BackendDAE paramdlow,paramdlow1;
array<BackendDAE.MultiDimEquation> arrayEqs;
BackendDAE.ExternalObjectClasses extObjClasses;
BackendDAE.AliasVariables alisvars;
BackendDAE.IncidenceMatrix m;
BackendDAE.IncidenceMatrixT mT;
array<Integer> v1,v2;
list<Integer> lv1,lv2;
list<list<Integer>> comps;
list<BackendDAE.Var> lv,lkn;
list<HelpVarInfo> helpVarInfo;

case (BackendDAE.DAE(knownVars=knvars,initialEqs=ie,algorithms=algs))
case (BackendDAE.DAE(knownVars=knvars,externalObjects=extobj,initialEqs=ie,algorithms=algs,arrayEqs=arrayEqs,extObjClasses=extObjClasses))
equation
// kvars params
parameterEquationsTmp = BackendVariable.traverseBackendDAEVars(knvars,createInitialParamAssignments,{});

parameterEquations = Util.listMap1(parameterEquationsTmp,
dlowEqToSimEqSystem,algs);
((parameterEquationsTmp,lv,lkn,lv1,lv2,_)) = BackendVariable.traverseBackendDAEVars(knvars,createInitialParamAssignments,({},{},{},{},{},1));

// sort the equations
emptyeqns = BackendDAEUtil.listEquation({});
pe = BackendDAEUtil.listEquation(parameterEquationsTmp);
alisvars = BackendDAEUtil.emptyAliasVariables();
v = BackendDAEUtil.listVar(lv);
kn = BackendDAEUtil.listVar(lkn);
paramdlow = BackendDAE.DAE(v,kn,extobj,alisvars,pe,emptyeqns,emptyeqns,arrayEqs,algs,BackendDAE.EVENT_INFO({},{}),extObjClasses);
m = BackendDAEUtil.incidenceMatrix(paramdlow,BackendDAE.NORMAL());
mT = BackendDAEUtil.transposeMatrix(m);
v1 = listArray(lv1);
v2 = listArray(lv2);
Debug.fcall("paramdlowdump", BackendDump.dump,paramdlow);
Debug.fcall("paramdlowdump", BackendDump.dumpIncidenceMatrix,m);
Debug.fcall("paramdlowdump", BackendDump.dumpIncidenceMatrixT,mT);
Debug.fcall("paramdlowdump", BackendDump.dumpMatching,v1);
(comps) = BackendDAETransform.strongComponents(m, mT, v1, v2);
Debug.fcall("paramdlowdump", BackendDump.dumpComponents,comps);

(helpVarInfo, paramdlow1) = generateHelpVarInfo(paramdlow, comps);
parameterEquations = createEquations(false, false, true, false, false, paramdlow1, v1, v2, comps, helpVarInfo);

ialgs = BackendEquation.getUsedAlgorithmsfromEquations(ie,algs);
inalgs = Util.listMap(ialgs,dlowAlgToSimEqSystem);
Expand Down Expand Up @@ -4674,29 +4703,38 @@ algorithm
end createInitialAssignmentsFromStart;

protected function createInitialParamAssignments
input tuple<BackendDAE.Var, list<BackendDAE.Equation>> inTpl;
output tuple<BackendDAE.Var, list<BackendDAE.Equation>> outTpl;
input tuple<BackendDAE.Var, tuple<list<BackendDAE.Equation>,list<BackendDAE.Var>,list<BackendDAE.Var>,list<Integer>,list<Integer>,Integer>> inTpl;
output tuple<BackendDAE.Var, tuple<list<BackendDAE.Equation>,list<BackendDAE.Var>,list<BackendDAE.Var>,list<Integer>,list<Integer>,Integer>> outTpl;
algorithm
outTpl:=
matchcontinue (inTpl)
local
BackendDAE.Var var;
BackendDAE.Var var,var1;
BackendDAE.Equation initialEquation;
list<BackendDAE.Equation> eqns;
Option<DAE.VariableAttributes> attr;
DAE.ComponentRef cr;
DAE.Exp startv;
DAE.Exp e;
DAE.ElementSource source "the origin of the element";
DAE.Exp e,cre;
DAE.ElementSource source;
list<Integer> v1,v2;
Integer pos,epos;
list<BackendDAE.Var> v,kn;

case ((var as BackendDAE.VAR(varName=cr, bindExp=SOME(e), source = source),eqns))
case ((var as BackendDAE.VAR(varName=cr, bindExp=SOME(e), source = source),(eqns,v,kn,v1,v2,pos)))
equation
false = Expression.isConst(e);
initialEquation = BackendDAE.SOLVED_EQUATION(cr, e, source);
cre = Expression.crefExp(cr);
initialEquation = BackendDAE.EQUATION(cre, e, source);
epos = listLength(v1)+1;
var1 = BackendVariable.setVarKind(var,BackendDAE.VARIABLE);
then
((var,initialEquation :: eqns));
((var,(initialEquation :: eqns,var1::v,kn,epos::v1,pos::v2,pos+1)));

case (inTpl) then inTpl;
case ((var,(eqns,v,kn,v1,v2,pos)))
equation
var1 = BackendVariable.setVarKind(var,BackendDAE.PARAM);
then ((var,(eqns,v,var1::kn,v1,v2,pos)));
end matchcontinue;
end createInitialParamAssignments;

Expand Down Expand Up @@ -7940,6 +7978,12 @@ algorithm
Option<DAE.VariableAttributes> dae_var_attr;
Values.Value value;
DAE.Exp e;
case (BackendDAE.VAR(varKind = BackendDAE.VARIABLE(), bindValue = SOME(value)))
equation
e = ValuesUtil.valueExp(value);
true = Expression.isConst(e);
then
SOME(e);
case (BackendDAE.VAR(varKind = BackendDAE.VARIABLE(), varType = BackendDAE.STRING(), values = dae_var_attr))
equation
e = DAEUtil.getStartAttrFail(dae_var_attr);
Expand All @@ -7952,36 +7996,42 @@ algorithm
true = Expression.isConst(e);
then
SOME(e);
case (BackendDAE.VAR(varKind = BackendDAE.VARIABLE(), bindValue = SOME(value)))
case (BackendDAE.VAR(varKind = BackendDAE.DISCRETE(), bindValue = SOME(value)))
equation
e = ValuesUtil.valueExp(value);
true = Expression.isConst(e);
then
SOME(e);
SOME(e);
case (BackendDAE.VAR(varKind = BackendDAE.DISCRETE(), values = dae_var_attr))
equation
e = DAEUtil.getStartAttrFail(dae_var_attr);
true = Expression.isConst(e);
then
SOME(e);
case (BackendDAE.VAR(varKind = BackendDAE.DISCRETE(), bindValue = SOME(value)))
equation
e = ValuesUtil.valueExp(value);
true = Expression.isConst(e);
then
SOME(e);
case (BackendDAE.VAR(varKind = BackendDAE.STATE(), values = dae_var_attr))
equation
e = DAEUtil.getStartAttrFail(dae_var_attr);
true = Expression.isConst(e);
then
SOME(e);
case (BackendDAE.VAR(varKind = BackendDAE.DUMMY_DER(), bindValue = SOME(value)))
equation
e = ValuesUtil.valueExp(value);
true = Expression.isConst(e);
then
SOME(e);
case (BackendDAE.VAR(varKind = BackendDAE.DUMMY_DER(), values = dae_var_attr))
equation
e = DAEUtil.getStartAttrFail(dae_var_attr);
true = Expression.isConst(e);
then
SOME(e);
case (BackendDAE.VAR(varKind = BackendDAE.DUMMY_STATE(), bindValue = SOME(value)))
equation
e = ValuesUtil.valueExp(value);
true = Expression.isConst(e);
then
SOME(e);
case (BackendDAE.VAR(varKind = BackendDAE.DUMMY_STATE(), values = dae_var_attr))
equation
e = DAEUtil.getStartAttrFail(dae_var_attr);
Expand Down

0 comments on commit 18d67a8

Please sign in to comment.