Skip to content

Commit

Permalink
Compiler/BackEnd/Initialization.mo
Browse files Browse the repository at this point in the history
  - introduce some kind of structure - first try
  - next steps to solve under-determined initial systems
  - add some fallback-clauses, to provide current testsuite (probably not so clever... ?!)
Compiler/BackEnd/BackendDAEUtil.mo
  - improve validity of some names
  - internally generated call start(v) depends not on v
  - simplify some snippets
Compiler/BackEnd/SimCodeUtil.mo
  - remove unneeded output
Compiler/BackEnd/BackendEquation.mo
  - add function emptyEqns() - as listEquation({}) is often used instead
testsuite/simulation/modelica/initialization/
  - conform expected outputs to current messages for
    - underdeterminedTest1.mos
    - underdeterminedTest2.mos
  - new test for under-determined initial systems
    - underdeterminedTest4.mos
    - underdeterminedTest5.mos
    - underdeterminedTest6.mos


git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@14304 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
lochel committed Dec 10, 2012
1 parent 70a9142 commit 1f78036
Show file tree
Hide file tree
Showing 6 changed files with 602 additions and 411 deletions.
14 changes: 7 additions & 7 deletions Compiler/BackEnd/BackendDAEOptimize.mo
Expand Up @@ -5496,7 +5496,7 @@ algorithm
knownVars = BackendVariable.mergeVariables(knownVars,vars1); //?????
tVariables = BackendVariable.listVar(var_lst);
knownVars = BackendVariable.mergeVariables(knownVars,tVariables);
emptyEqns = BackendEquation.listEquation({});
emptyEqns = BackendEquation.emptyEqns();
emptyVars = BackendVariable.emptyVars();
eqSystem = BackendDAE.EQSYSTEM(variables,eqArray,NONE(),NONE(),BackendDAE.NO_MATCHING());
shared = BackendDAE.SHARED(knownVars,externalObjects,aliasVars,emptyEqns,removedEqs,constraints,classAttrs,cache,env,functionTree,BackendDAE.EVENT_INFO({},{},{},{},0,0),{},BackendDAE.SIMULATION(),{});
Expand Down Expand Up @@ -8006,7 +8006,7 @@ algorithm
/*
(blt_states, _) = BackendDAEUtil.generateStatePartition(syst);

newEqns = BackendEquation.listEquation({});
newEqns = BackendEquation.emptyEqns();
newVars = BackendVariable.emptyVars();
(newEqns, newVars) = BackendDAEUtil.splitoutEquationAndVars(blt_states,e,v,newEqns,newVars);
backendDAE2 = BackendDAE.DAE(BackendDAE.EQSYSTEM(newVars,newEqns,NONE(),NONE(),BackendDAE.NO_MATCHING())::{},shared);
Expand Down Expand Up @@ -8305,9 +8305,9 @@ algorithm
jacKnownVars = BackendVariable.emptyVars();
jacExternalObjects = BackendVariable.emptyVars();
jacAliasVars = BackendVariable.emptyVars();
jacOrderedEqs = BackendEquation.listEquation({});
jacRemovedEqs = BackendEquation.listEquation({});
jacInitialEqs = BackendEquation.listEquation({});
jacOrderedEqs = BackendEquation.emptyEqns();
jacRemovedEqs = BackendEquation.emptyEqns();
jacInitialEqs = BackendEquation.emptyEqns();
constrs = listArray({});
clsAttrs = listArray({});
functions = DAEUtil.avlTreeNew();
Expand Down Expand Up @@ -8347,8 +8347,8 @@ algorithm
jacExternalObjects = BackendVariable.emptyVars();
jacAliasVars = BackendVariable.emptyVars();
jacOrderedEqs = BackendEquation.listEquation(derivedEquations);
jacRemovedEqs = BackendEquation.listEquation({});
jacInitialEqs = BackendEquation.listEquation({});
jacRemovedEqs = BackendEquation.emptyEqns();
jacInitialEqs = BackendEquation.emptyEqns();
constrs = listArray({});
clsAttrs = listArray({});
functions = DAEUtil.avlTreeNew();
Expand Down
2 changes: 1 addition & 1 deletion Compiler/BackEnd/BackendDAETransform.mo
Expand Up @@ -1041,7 +1041,7 @@ algorithm
end match;
end analyseStrongComponent;

protected function analyseStrongComponentBlock"function: analyseStrongComponentBlock
protected function analyseStrongComponentBlock "function: analyseStrongComponentBlock
author: Frenkel TUD 2011-05
helper for analyseStrongComponent."
input list<Integer> inComp;
Expand Down
117 changes: 49 additions & 68 deletions Compiler/BackEnd/BackendDAEUtil.mo
Expand Up @@ -551,7 +551,7 @@ protected
DAE.FunctionTree funcTree;
algorithm
emptyvars := BackendVariable.emptyVars();
emptyEqns := BackendEquation.listEquation({});
emptyEqns := BackendEquation.emptyEqns();
emptyAliasVars := BackendVariable.emptyVars();
constrs := listArray({});
clsAttrs := listArray({});
Expand Down Expand Up @@ -3182,35 +3182,31 @@ public function incidenceMatrix
Calculates the incidence matrix, i.e. which variables are present in each equation.
You can ask for absolute indexes or normal (negative for der) via the IndexType.
wbraun: beware dim(IncidenceMatrix) != dim(IncidenceMatrixT) due to array equations. "
input BackendDAE.EqSystem syst;
input BackendDAE.EqSystem inEqSystem;
input BackendDAE.IndexType inIndexType;
output BackendDAE.IncidenceMatrix outIncidenceMatrix;
output BackendDAE.IncidenceMatrixT outIncidenceMatrixT;
algorithm
(outIncidenceMatrix,outIncidenceMatrixT) := matchcontinue (syst, inIndexType)
(outIncidenceMatrix, outIncidenceMatrixT) := matchcontinue(inEqSystem, inIndexType)
local
BackendDAE.IncidenceMatrix arr;
BackendDAE.IncidenceMatrixT arrT;
BackendDAE.Variables vars;
EquationArray eqns;
Integer numberOfEqs,numberofVars;

case (BackendDAE.EQSYSTEM(orderedVars = vars,orderedEqs = eqns), _)
equation
// get the size
numberOfEqs = equationArraySize(eqns);
numberofVars = BackendVariable.varsSize(vars);
// create the array to hold the incidence matrix
arrT = arrayCreate(numberofVars, {});
(arr,arrT) = incidenceMatrixDispatch(vars, eqns, {},arrT, 0, numberOfEqs, intLt(0, numberOfEqs), inIndexType);
then
(arr,arrT);

else
equation
Error.addMessage(Error.INTERNAL_ERROR,{"BackendDAEUtil.incidenceMatrix failed"});
then
fail();
Integer numberOfEqs, numberofVars;

case (BackendDAE.EQSYSTEM(orderedVars=vars, orderedEqs=eqns), _) equation
// get the size
numberOfEqs = equationArraySize(eqns);
numberofVars = BackendVariable.varsSize(vars);
// create the array to hold the incidence matrix
arrT = arrayCreate(numberofVars, {});
(arr, arrT) = incidenceMatrixDispatch(vars, eqns, {}, arrT, 0, numberOfEqs, intLt(0, numberOfEqs), inIndexType);
then (arr,arrT);

else equation
Error.addMessage(Error.INTERNAL_ERROR, {"./Compiler/BackEnd/BackendDAEUtil.mo: function incidenceMatrix failed"});
then fail();
end matchcontinue;
end incidenceMatrix;

Expand Down Expand Up @@ -3289,8 +3285,7 @@ protected function incidenceMatrixDispatch
output BackendDAE.IncidenceMatrix outIncidenceArray;
output BackendDAE.IncidenceMatrixT outIncidenceArrayT;
algorithm
(outIncidenceArray,outIncidenceArrayT) :=
match (vars, inEqsArr, inIncidenceArray, inIncidenceArrayT, index, numberOfEqs, stop, inIndexType)
(outIncidenceArray, outIncidenceArrayT) := match(vars, inEqsArr, inIncidenceArray, inIncidenceArrayT, index, numberOfEqs, stop, inIndexType)
local
list<Integer> row;
BackendDAE.Equation e;
Expand All @@ -3299,31 +3294,22 @@ algorithm
Integer i1;

// i = n (we reach the end)
case (_, _, iArr, iArrT, _, _, false, _) then (listArray(listReverse(iArr)),iArrT);
case (_, _, iArr, iArrT, _, _, false, _)
then (listArray(listReverse(iArr)), iArrT);

// i < n
case (_, _, iArr, iArrT, _, _, true, _)
equation
// get the equation
e = equationNth(inEqsArr, index);
// compute the row
(row,_) = incidenceRow(e, vars, inIndexType, {});
i1 = index+1;
// put it in the arrays
iArr = row::iArr;
iArrT = fillincidenceMatrixT(row,{i1},iArrT);
// next equation
(outIncidenceArray,iArrT) = incidenceMatrixDispatch(vars, inEqsArr, iArr, iArrT, i1, numberOfEqs, intLt(i1, numberOfEqs), inIndexType);
then
(outIncidenceArray,iArrT);

/* Unreachable due to tail recursion, which we really need
case (vars, eqArr, wc, iArr, iArrT, i, n, inIndexType)
equation
print("- BackendDAEUtil.incidenceMatrixDispatch failed\n");
then
fail();
*/
case (_, _, iArr, iArrT, _, _, true, _) equation
// get the equation
e = equationNth(inEqsArr, index);
// compute the row
(row, _) = incidenceRow(e, vars, inIndexType, {});
i1 = index+1;
// put it in the arrays
iArr = row::iArr;
iArrT = fillincidenceMatrixT(row, {i1}, iArrT);
// next equation
(outIncidenceArray, iArrT) = incidenceMatrixDispatch(vars, inEqsArr, iArr, iArrT, i1, numberOfEqs, intLt(i1, numberOfEqs), inIndexType);
then (outIncidenceArray,iArrT);
end match;
end incidenceMatrixDispatch;

Expand Down Expand Up @@ -3704,8 +3690,7 @@ algorithm
end match;
end incidenceRow1;

public function incidenceRowExp
"function: incidenceRowExp
public function incidenceRowExp "function: incidenceRowExp
author: PA
Helper function to incidenceRow, investigates expressions for
variables, returning variable indexes."
Expand Down Expand Up @@ -3829,8 +3814,8 @@ algorithm
end traversingincidenceRowExpSolvableFinder;

public function traversingincidenceRowExpFinder "
Author: Frenkel TUD 2010-11
Helper for statesAndVarsExp"
author: Frenkel TUD 2010-11
Helper for statesAndVarsExp"
input tuple<DAE.Exp, tuple<BackendDAE.Variables,list<Integer>>> inTpl;
output tuple<DAE.Exp, Boolean, tuple<BackendDAE.Variables,list<Integer>>> outTpl;
algorithm
Expand Down Expand Up @@ -3864,6 +3849,10 @@ algorithm
res = incidenceRowExp1(varslst,p,pa,false);
then
((e,false,(vars,res)));

// lochel: internally generated call start(v) depends not on v
case ((e as DAE.CALL(path = Absyn.IDENT(name = "$_start")), (vars, pa))) then ((e, false, (vars, pa)));

/* pre(v) is considered a known variable */
case (((e as DAE.CALL(path = Absyn.IDENT(name = "pre"),expLst = {DAE.CREF(componentRef = cr)}),(vars,pa)))) then ((e,false,(vars,pa)));

Expand Down Expand Up @@ -4517,30 +4506,22 @@ algorithm
end match;
end getIncidenceMatrixfromOption;

public function getIncidenceMatrix
"function getIncidenceMatrix
public function getIncidenceMatrix "function getIncidenceMatrix
this function returns the incidence matrix,
if the system contains multi"
input BackendDAE.EqSystem syst;
input BackendDAE.EqSystem inEqSystem;
input BackendDAE.IndexType inIndxType;
output BackendDAE.EqSystem osyst;
output BackendDAE.EqSystem outEqSystem;
output BackendDAE.IncidenceMatrix outM;
output BackendDAE.IncidenceMatrix outMT;
protected
BackendDAE.Variables v;
EquationArray eq;
BackendDAE.Matching matching;
algorithm
(osyst,outM,outMT):=
match (syst,inIndxType)
local
BackendDAE.IncidenceMatrix m,mT;
BackendDAE.Variables v;
EquationArray eq;
BackendDAE.Matching matching;
BackendDAE.IndexType it;
case(BackendDAE.EQSYSTEM(v,eq,_,_,matching),it)
equation
(m,mT) = incidenceMatrix(syst, it);
then
(BackendDAE.EQSYSTEM(v,eq,SOME(m),SOME(mT),matching),m,mT);
end match;
BackendDAE.EQSYSTEM(v, eq, _, _, matching) := inEqSystem;
(outM, outMT) := incidenceMatrix(inEqSystem, inIndxType);
outEqSystem := BackendDAE.EQSYSTEM(v, eq, SOME(outM), SOME(outMT), matching);
end getIncidenceMatrix;

public function getIncidenceMatrixScalar "function getIncidenceMatrixScalar"
Expand Down
5 changes: 5 additions & 0 deletions Compiler/BackEnd/BackendEquation.mo
Expand Up @@ -85,6 +85,11 @@ algorithm
outEquationArray := BackendDAE.EQUATION_ARRAY(size,len,arrsize,newarr);
end listEquation;

public function emptyEqns
output BackendDAE.EquationArray eqns;
algorithm
eqns := listEquation({});
end emptyEqns;

public function equationList "function equationList
author: PA
Expand Down

0 comments on commit 1f78036

Please sign in to comment.