Skip to content

Commit

Permalink
Merging
Browse files Browse the repository at this point in the history
  • Loading branch information
sjoelund committed Oct 13, 2010
2 parents bea1e3a + 97beedd commit 4c500ba
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 183 deletions.
2 changes: 1 addition & 1 deletion Compiler/Builtin.mo
Expand Up @@ -2347,7 +2347,7 @@ algorithm
_:=
matchcontinue (inPath)
case (Absyn.IDENT(name = "der")) then ();
case (Absyn.FULLYQUALIFIED(inPath)) equation isAbs(inPath); then ();
case (Absyn.FULLYQUALIFIED(inPath)) equation isDer(inPath); then ();
end matchcontinue;
end isDer;

Expand Down
90 changes: 88 additions & 2 deletions Compiler/DAELow.mo
Expand Up @@ -3094,7 +3094,7 @@ algorithm
end matchcontinue;
end equationStr;

public function removeSimpleEquations
protected function removeSimpleEquations
"function: removeSimpleEquations
This function moves simple equations on the form a=b from equations 2nd
in DAELow to simple equations 3rd in DAELow to speed up assignment alg.
Expand Down Expand Up @@ -17378,6 +17378,92 @@ algorithm
outAliasVariables := ALIASVARS(aliasMappings,aliasVariables);
end emptyAliasVariables;


public function generateLinearMatrix
// function: generateLinearModel
// author: wbraun
input DAELow inDAELow;
input DAE.FunctionTree functionTree;
input list<DAE.ComponentRef> inComRef1; // eqnvars
input list<DAE.ComponentRef> inComRef2; // vars to differentiate
input list<Var> inAllVar;
output DAELow outJacobian;
output Integer[:] outV1;
output Integer[:] outV2;
output list<list<Integer>> outComps1;
algorithm
(outJacobian,outv1,outv2,outComps1) :=
matchcontinue (inDAELow,functionTree,inComRef1,inComRef2,inAllVar)
local
DAE.DAElist dae;
DAELow dlow;

list<DAE.ComponentRef> eqvars,diffvars;
list<Var> varlst;
Integer[:] v1,v2;
list<list<Integer>> comps1;
list<Var> derivedVariables;
BinTree jacElements;
list<tuple<String,Integer>> varTuple;
list<Integer>[:] m,mT;

Variables v,kv,exv;
AliasVariables av;
EquationArray e,re,ie;
MultiDimEquation[:] ae;
DAE.Algorithm[:] al;
EventInfo ev;
ExternalObjectClasses eoc;
list<Equation> e_lst,re_lst,ie_lst;
list<DAE.Algorithm> algs;
list<MultiDimEquation> ae_lst;
case(dlow,_,{},_,_) then (dlow,listArray({}),listArray({}),{});
case(dlow,_,_,{},_) then (dlow,listArray({}),listArray({}),{});
case(dlow as DAELOW(v,kv,exv,av,e,re,ie,ae,al,ev,eoc),functionTree,eqvars,diffvars,varlst)
equation

// prepare index for Matrix and variables for simpleEquations
derivedVariables = varList(v);
(varTuple) = determineIndices(eqvars, diffvars, 0, varlst);
printTuple(varTuple);
jacElements = emptyBintree;
(derivedVariables,jacElements) = changeIndices(derivedVariables, varTuple, jacElements);
v = listVar(derivedVariables);
/*
// Remove simple Equtaion and
e_lst = equationList(e);
re_lst = equationList(re);
ie_lst = equationList(ie);
ae_lst = arrayList(ae);
algs = arrayList(al);
(v,kv,e_lst,re_lst,ie_lst,ae_lst,algs,av) = removeSimpleEquations(v,kv, e_lst, re_lst, ie_lst, ae_lst, algs, jacElements);

e = listEquation(e_lst);
re = listEquation(re_lst);
ie = listEquation(ie_lst);
ae = listArray(ae_lst);
al = listArray(algs);
*/
dlow = DAELOW(v,kv,exv,av,e,re,ie,ae,al,ev,eoc);

m = incidenceMatrix(dlow);
mT = transposeMatrix(m);
Debug.fcall("jacdump2", dumpIncidenceMatrix, m);
Debug.fcall("jacdump2", dumpIncidenceMatrixT, mT);
(v1,v2,dlow,m,mT) = matchingAlgorithm(dlow, m, mT, (NO_INDEX_REDUCTION(), EXACT(), KEEP_SIMPLE_EQN()),functionTree);
Debug.fcall("jacdump2", dumpIncidenceMatrix, m);
Debug.fcall("jacdump2", dumpIncidenceMatrixT, mT);
Debug.fcall("jacdump2", dump, dlow);
Debug.fcall("jacdump2", dumpMatching, v1);
(comps1) = strongComponents(m, mT, v1, v2);
Debug.fcall("jacdump2", dumpComponents, comps1);
then (dlow,v1,v2,comps1);
case(_, _, _, _, _) equation
Error.addMessage(Error.INTERNAL_ERROR, {"DAELow.generateLinearMatrix failed"});
then fail();
end matchcontinue;
end generateLinearMatrix;

public function generateSymbolicJacobian
// function: generateSymbolicJacobian
// author: lochel
Expand Down Expand Up @@ -17698,7 +17784,7 @@ algorithm
then ({}, {ALGORITHM(numAlgs, in_, out, source)}, {derivedAlgorithm});

case(currEquation as WHEN_EQUATION(_, _), algorithms, functions, var, ass1, ass2, allVars, _) equation
print("DAELow.derive: WHEN_EQUATION has been removed");
Debug.fcall("jacdump",print,"DAELow.derive: WHEN_EQUATION has been removed");
then ({}, {}, {});

case(currEquation as COMPLEX_EQUATION(_, _, _, _), algorithms, functions, var, ass1, ass2, allVars, _) equation
Expand Down
213 changes: 33 additions & 180 deletions Compiler/SimCode.mo
Expand Up @@ -1514,18 +1514,13 @@ algorithm
matchcontinue (functions,inDAELow2,inIntegerArray3,inIntegerArray4)
local
DAE.DAElist dae;
DAELow.DAELow dlow,dlow2;
DAELow.DAELow dlow,deriveddlow1,deriveddlow2;
Integer[:] ass1,ass2;

DAELow.Variables orderedVars,knownVars;
DAELow.Variables v,kv;
list<DAELow.Var> derivedVariables, varlst,varlst1,varlst2, states, inputvars, outputvars;
list<DAE.ComponentRef> comref_states, comref_inputvars, comref_outputvars;

//new vars for Jacobian or rather for Linearization
DAELow.DAELow deriveddlowAC;
DAELow.DAELow deriveddlowBD;
DAELow.Variables jacOrderedVars;
list<Integer>[:] m,mT;

Integer[:] v1,v2;
list<list<Integer>> comps1;
list<SimEqSystem> JacAEquations;
Expand All @@ -1534,31 +1529,18 @@ algorithm
list<SimEqSystem> JacDEquations;
list<SimVar> JacAVars, JacBVars, JacCVars, JacDVars;

list<tuple<String,Integer>> varTuple;
DAELow.BinTree jacElements;

list<JacobianMatrix> LinearMats;

DAELow.Variables v,kv,exv;
DAELow.AliasVariables av;
DAELow.EquationArray e,re,ie;
DAELow.MultiDimEquation[:] ae;
DAE.Algorithm[:] al;
DAELow.EventInfo ev;
DAELow.ExternalObjectClasses eoc;
list<DAELow.Equation> e_lst,re_lst,ie_lst;
list<DAE.Algorithm> algs;
list<DAELow.MultiDimEquation> ae_lst;

case (functions,dlow,ass1,ass2)
equation
true = RTOpts.debugFlag("linearization");
Debug.fcall("linmodel",print,"Generate Linear Moldel Matrixes\n");
Debug.fcall("linmodel",print,"Generate Linear Model Matrices\n");

// Prepare all need variables
orderedVars = DAELow.daeVars(dlow);
knownVars = DAELow.daeKnVars(dlow);
varlst = DAELow.varList(orderedVars);
varlst1 = DAELow.varList(knownVars);
v = DAELow.daeVars(dlow);
kv = DAELow.daeKnVars(dlow);
varlst = DAELow.varList(v);
varlst1 = DAELow.varList(kv);
varlst2 = listAppend(varlst,varlst1);
varlst = listReverse(varlst);
varlst1 = listReverse(varlst1);
Expand All @@ -1573,173 +1555,44 @@ algorithm

// Create SimCode structure for Jacobian or rather for Linearization

// Differentiate the System w.r.t states for Matrixes A and C
// Differentiate the System w.r.t states for matrices A and C
Debug.fcall("linmodel",print,"Differentiate System w.r.t. states.\n");
(deriveddlowAC as DAELow.DAELOW(v,kv,exv,av,e,re,ie,ae,al,ev,eoc)) = DAELow.generateSymbolicJacobian(dlow, functions, comref_states, ass1,ass2);
deriveddlow1 = DAELow.generateSymbolicJacobian(dlow, functions, comref_states, ass1,ass2);
Debug.fcall("linmodel",print,"Done! Create now Matrixes A and C for linear model.\n");

// prepare index for Matrix A and Variable for simpleEquations
// create Matrix A and variables
(deriveddlow2, v1, v2, comps1) = DAELow.generateLinearMatrix(deriveddlow1,functions,comref_states,comref_states,varlst);
JacAEquations = createEquations(true, false, true, deriveddlow2, v1, v2, comps1, {});
v = DAELow.daeVars(deriveddlow2);
derivedVariables = DAELow.varList(v);
(varTuple) = DAELow.determineIndices(comref_states, comref_states, 0, varlst);
DAELow.printTuple(varTuple);
jacElements = DAELow.emptyBintree;
(derivedVariables,jacElements) = DAELow.changeIndices(derivedVariables, varTuple,jacElements);
v = DAELow.listVar(derivedVariables);

/*
// Remove simple Equtaion and
e_lst = DAELow.equationList(e);
re_lst = DAELow.equationList(re);
ie_lst = DAELow.equationList(ie);
ae_lst = arrayList(ae);
algs = arrayList(al);
(v,kv,e_lst,re_lst,ie_lst,ae_lst,algs,av) = DAELow.removeSimpleEquations(v,kv, e_lst, re_lst, ie_lst, ae_lst, algs, jacElements);
e = DAELow.listEquation(e_lst);
re = DAELow.listEquation(re_lst);
ie = DAELow.listEquation(ie_lst);
ae = listArray(ae_lst);
al = listArray(algs);
deriveddlowAC = DAELow.DAELOW(v,kv,exv,av,e,re,ie,ae,al,ev,eoc);
*/

m = DAELow.incidenceMatrix(deriveddlowAC);
mT = DAELow.transposeMatrix(m);
Debug.fcall("jacdump2", DAELow.dumpIncidenceMatrix, m);
Debug.fcall("jacdump2", DAELow.dumpIncidenceMatrixT, mT);
(v1,v2,deriveddlowAC,m,mT) = DAELow.matchingAlgorithm(deriveddlowAC, m, mT, (DAELow.NO_INDEX_REDUCTION(), DAELow.EXACT(), DAELow.KEEP_SIMPLE_EQN()),functions);
Debug.fcall("jacdump2", DAELow.dumpIncidenceMatrix, m);
Debug.fcall("jacdump2", DAELow.dumpIncidenceMatrixT, mT);
Debug.fcall("jacdump2", DAELow.dump, deriveddlowAC);
Debug.fcall("jacdump2", DAELow.dumpMatching, v1);
(comps1) = DAELow.strongComponents(m, mT, v1, v2);
Debug.fcall("jacdump2", DAELow.dumpComponents, comps1);

JacAEquations = createEquations(true, false, true, deriveddlowAC, v1, v2, comps1, {});
JacAVars = Util.listMap(derivedVariables, dlowvarToSimvar);

jacOrderedVars = DAELow.daeVars(deriveddlowAC);
derivedVariables = DAELow.varList(jacOrderedVars);
(varTuple) = DAELow.determineIndices(comref_outputvars, comref_states, 0, varlst);
DAELow.printTuple(varTuple);
jacElements = DAELow.emptyBintree;
(derivedVariables,jacElements) = DAELow.changeIndices(derivedVariables, varTuple,jacElements);
v = DAELow.listVar(derivedVariables);
/*
e_lst = DAELow.equationList(e);
re_lst = DAELow.equationList(re);
ie_lst = DAELow.equationList(ie);
ae_lst = arrayList(ae);
algs = arrayList(al);
(v,kv,e_lst,re_lst,ie_lst,ae_lst,algs,av) = DAELow.removeSimpleEquations(v,kv, e_lst, re_lst, ie_lst, ae_lst, algs, jacElements);
e = DAELow.listEquation(e_lst);
re = DAELow.listEquation(re_lst);
ie = DAELow.listEquation(ie_lst);
ae = listArray(ae_lst);
al = listArray(algs);
deriveddlowAC = DAELow.DAELOW(v,kv,exv,av,e,re,ie,ae,al,ev,eoc);
*/
m = DAELow.incidenceMatrix(deriveddlowAC);
mT = DAELow.transposeMatrix(m);
Debug.fcall("jacdump2", DAELow.dumpIncidenceMatrix, m);
Debug.fcall("jacdump2", DAELow.dumpIncidenceMatrixT, mT);
(v1,v2,deriveddlowAC,m,mT) = DAELow.matchingAlgorithm(deriveddlowAC, m, mT, (DAELow.NO_INDEX_REDUCTION(), DAELow.EXACT(), DAELow.KEEP_SIMPLE_EQN()),functions);
Debug.fcall("jacdump2", DAELow.dumpIncidenceMatrix, m);
Debug.fcall("jacdump2", DAELow.dumpIncidenceMatrixT, mT);
Debug.fcall("jacdump2", DAELow.dump, deriveddlowAC);
Debug.fcall("jacdump2", DAELow.dumpMatching, v1);
(comps1) = DAELow.strongComponents(m, mT, v1, v2);
Debug.fcall("jacdump2", DAELow.dumpComponents, comps1);

JacCEquations = createEquations(true, false, true, deriveddlowAC, v1, v2, comps1, {});

// create Matrix C and variables
(deriveddlow2, v1, v2, comps1) = DAELow.generateLinearMatrix(deriveddlow1,functions,comref_outputvars,comref_states,varlst);
JacCEquations = createEquations(true, false, true, deriveddlow2, v1, v2, comps1, {});
v = DAELow.daeVars(deriveddlow2);
derivedVariables = DAELow.varList(v);
JacCVars = Util.listMap(derivedVariables, dlowvarToSimvar);

// For Matrix B and D

// Differentiate the System w.r.t states for matrices B and D
Debug.fcall("linmodel",print,"Differentiate System w.r.t. inputs.\n");
(deriveddlowBD as DAELow.DAELOW(v,kv,exv,av,e,re,ie,ae,al,ev,eoc)) = DAELow.generateSymbolicJacobian(dlow, functions, comref_inputvars, ass1,ass2);
deriveddlow1 = DAELow.generateSymbolicJacobian(dlow, functions, comref_inputvars, ass1,ass2);
Debug.fcall("linmodel",print,"Done! Create now Matrixes B and D for linear model.\n");

// create Matrix B and variables
(deriveddlow2, v1, v2, comps1) = DAELow.generateLinearMatrix(deriveddlow1,functions,comref_states,comref_inputvars,varlst);
JacBEquations = createEquations(true, false, true, deriveddlow2, v1, v2, comps1, {});
v = DAELow.daeVars(deriveddlow2);
derivedVariables = DAELow.varList(v);
(varTuple) = DAELow.determineIndices(comref_states, comref_inputvars, 0, varlst);
DAELow.printTuple(varTuple);
jacElements = DAELow.emptyBintree;
(derivedVariables,jacElements) = DAELow.changeIndices(derivedVariables, varTuple,jacElements);
/*v = DAELow.listVar(derivedVariables);
e_lst = DAELow.equationList(e);
re_lst = DAELow.equationList(re);
ie_lst = DAELow.equationList(ie);
ae_lst = arrayList(ae);
algs = arrayList(al);
(v,kv,e_lst,re_lst,ie_lst,ae_lst,algs,av) = DAELow.removeSimpleEquations(v,kv, e_lst, re_lst, ie_lst, ae_lst, algs, jacElements);
e = DAELow.listEquation(e_lst);
re = DAELow.listEquation(re_lst);
ie = DAELow.listEquation(ie_lst);
ae = listArray(ae_lst);
al = listArray(algs);
deriveddlowBD = DAELow.DAELOW(v,kv,exv,av,e,re,ie,ae,al,ev,eoc);
*/
m = DAELow.incidenceMatrix(deriveddlowBD);
mT = DAELow.transposeMatrix(m);
Debug.fcall("jacdump2", DAELow.dumpIncidenceMatrix, m);
Debug.fcall("jacdump2", DAELow.dumpIncidenceMatrixT, mT);
(v1,v2,deriveddlowBD,m,mT) = DAELow.matchingAlgorithm(deriveddlowBD, m, mT, (DAELow.NO_INDEX_REDUCTION(), DAELow.EXACT(), DAELow.KEEP_SIMPLE_EQN()),functions);
Debug.fcall("jacdump2", DAELow.dumpIncidenceMatrix, m);
Debug.fcall("jacdump2", DAELow.dumpIncidenceMatrixT, mT);
Debug.fcall("jacdump2", DAELow.dump, deriveddlowBD);
Debug.fcall("jacdump2", DAELow.dumpMatching, v1);
(comps1) = DAELow.strongComponents(m, mT, v1, v2);
Debug.fcall("jacdump2", DAELow.dumpComponents, comps1);

JacBEquations = createEquations(true, false, true, deriveddlowBD, v1, v2, comps1, {});
JacBVars = Util.listMap(derivedVariables, dlowvarToSimvar);

jacOrderedVars = DAELow.daeVars(deriveddlowBD);
derivedVariables = DAELow.varList(jacOrderedVars);
(varTuple) = DAELow.determineIndices(comref_outputvars, comref_inputvars, 0, varlst);
DAELow.printTuple(varTuple);
jacElements = DAELow.emptyBintree;
(derivedVariables,jacElements) = DAELow.changeIndices(derivedVariables, varTuple,jacElements);

/*
v = DAELow.listVar(derivedVariables);

e_lst = DAELow.equationList(e);
re_lst = DAELow.equationList(re);
ie_lst = DAELow.equationList(ie);
ae_lst = arrayList(ae);
algs = arrayList(al);
(v,kv,e_lst,re_lst,ie_lst,ae_lst,algs,av) = DAELow.removeSimpleEquations(v,kv, e_lst, re_lst, ie_lst, ae_lst, algs, jacElements);
e = DAELow.listEquation(e_lst);
re = DAELow.listEquation(re_lst);
ie = DAELow.listEquation(ie_lst);
ae = listArray(ae_lst);
al = listArray(algs);
deriveddlowBD = DAELow.DAELOW(v,kv,exv,av,e,re,ie,ae,al,ev,eoc);
*/
m = DAELow.incidenceMatrix(deriveddlowBD);
mT = DAELow.transposeMatrix(m);
Debug.fcall("jacdump2", DAELow.dumpIncidenceMatrix, m);
Debug.fcall("jacdump2", DAELow.dumpIncidenceMatrixT, mT);
(v1,v2,deriveddlowBD,m,mT) = DAELow.matchingAlgorithm(deriveddlowBD, m, mT, (DAELow.NO_INDEX_REDUCTION(), DAELow.EXACT(), DAELow.KEEP_SIMPLE_EQN()),functions);
Debug.fcall("jacdump2", DAELow.dumpIncidenceMatrix, m);
Debug.fcall("jacdump2", DAELow.dumpIncidenceMatrixT, mT);
Debug.fcall("jacdump2", DAELow.dump, deriveddlowBD);
Debug.fcall("jacdump2", DAELow.dumpMatching, v1);
(comps1) = DAELow.strongComponents(m, mT, v1, v2);
Debug.fcall("jacdump2", DAELow.dumpComponents, comps1);

JacDEquations = createEquations(true, false, true, deriveddlowBD, v1, v2, comps1, {});
// create Matrix D and variables
(deriveddlow2, v1, v2, comps1) = DAELow.generateLinearMatrix(deriveddlow1,functions,comref_outputvars,comref_inputvars,varlst);
JacDEquations = createEquations(true, false, true, deriveddlow2, v1, v2, comps1, {});
v = DAELow.daeVars(deriveddlow2);
derivedVariables = DAELow.varList(v);
JacDVars = Util.listMap(derivedVariables, dlowvarToSimvar);

LinearMats = {(JacAEquations,JacAVars,"A"),(JacBEquations,JacBVars,"B"),(JacCEquations,JacCVars,"C"),(JacDEquations,JacDVars,"D")};

then
Expand Down

0 comments on commit 4c500ba

Please sign in to comment.