Skip to content

Commit 4164f59

Browse files
committed
- Retain elementsource for linear system of eq's
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@8941 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
1 parent ba57249 commit 4164f59

File tree

5 files changed

+81
-23
lines changed

5 files changed

+81
-23
lines changed

Compiler/BackEnd/SimCode.mo

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4774,6 +4774,8 @@ algorithm
47744774
list<Real> rhsVals,solvedVals;
47754775
BackendDAE.ExternalObjectClasses eoc;
47764776
BackendDAE.AliasVariables ave;
4777+
list<DAE.ElementSource> sources;
4778+
list<DAE.ComponentRef> names;
47774779

47784780
// A single array equation
47794781
case (mixedEvent,genDiscrete,skipDiscInAlgorithm,dae,optJac,jac_tp,block_,helpVarInfo)
@@ -4799,14 +4801,16 @@ algorithm
47994801
eqn_size = BackendDAEUtil.equationSize(eqn);
48004802
((simVars,_)) = BackendVariable.traverseBackendDAEVars(v,traversingdlowvarToSimvar,({},kv));
48014803
simVars = listReverse(simVars);
4802-
((_,_,_,beqs)) = BackendEquation.traverseBackendDAEEqns(eqn,dlowEqToExp,(v,arrayEqs,{},{}));
4804+
((_,_,_,beqs,sources)) = BackendEquation.traverseBackendDAEEqns(eqn,dlowEqToExp,(v,arrayEqs,{},{},{}));
48034805
beqs = listReverse(beqs);
48044806
rhsVals = ValuesUtil.valueReals(Util.listMap(beqs,Ceval.cevalSimple));
48054807
jacVals = BackendDAEOptimize.evaluateConstantJacobian(listLength(simVars),jac);
48064808
(solvedVals,linInfo) = System.solveLinearSystem(jacVals,rhsVals);
4807-
checkLinearSystem(linInfo,simVars,jacVals,rhsVals);
4809+
names = Util.listMap(simVars,varName);
4810+
checkLinearSystem(linInfo,names,jacVals,rhsVals);
48084811
// TODO: Move these to known vars :/ This is done in the wrong phase of the compiler... Also, if done as an optimization module, we can optimize more!
4809-
equations_ = Util.listThreadMap(simVars,solvedVals,generateSolvedEquation);
4812+
sources = Util.listMap1(sources, DAEUtil.addSymbolicTransformation, DAE.LINEAR_SOLVED(names,jacVals,solvedVals,rhsVals));
4813+
equations_ = Util.listThread3Map(simVars,solvedVals,sources,generateSolvedEquation);
48104814
then
48114815
equations_;
48124816

@@ -4840,7 +4844,7 @@ algorithm
48404844
equation
48414845
((simVars,_)) = BackendVariable.traverseBackendDAEVars(v,traversingdlowvarToSimvar,({},kv));
48424846
simVars = listReverse(simVars);
4843-
((_,_,_,beqs)) = BackendEquation.traverseBackendDAEEqns(eqn,dlowEqToExp,(v,arrayEqs,{},{}));
4847+
((_,_,_,beqs,_)) = BackendEquation.traverseBackendDAEEqns(eqn,dlowEqToExp,(v,arrayEqs,{},{},{}));
48444848
beqs = listReverse(beqs);
48454849
simJac = Util.listMap1(jac, jacToSimjac, v);
48464850
index = Util.listFirst(block_); // use first equation nr as index
@@ -4878,7 +4882,7 @@ end createOdeSystem2;
48784882

48794883
protected function checkLinearSystem
48804884
input Integer info;
4881-
input list<SimVar> vars;
4885+
input list<DAE.ComponentRef> vars;
48824886
input list<list<Real>> jac;
48834887
input list<Real> rhs;
48844888
algorithm
@@ -4889,9 +4893,9 @@ algorithm
48894893
case (info,vars,jac,rhs)
48904894
equation
48914895
true = info > 0;
4892-
varname = varName(listGet(vars,info));
4896+
varname = ComponentReference.printComponentRefStr(listGet(vars,info));
48934897
infoStr = intString(info);
4894-
varnames = Util.stringDelimitList(Util.listMap(vars,varName)," ;\n ");
4898+
varnames = Util.stringDelimitList(Util.listMap(vars,ComponentReference.printComponentRefStr)," ;\n ");
48954899
rhsStr = Util.stringDelimitList(Util.listMap(rhs, realString)," ;\n ");
48964900
jacStr = Util.stringDelimitList(Util.listMap1(Util.listListMap(jac,realString),Util.stringDelimitList," , ")," ;\n ");
48974901
syst = stringAppendList({"\n[\n ", jacStr, "\n]\n *\n[\n ",varnames,"\n]\n =\n[\n ",rhsStr,"\n]"});
@@ -4900,7 +4904,7 @@ algorithm
49004904
case (info,vars,jac,rhs)
49014905
equation
49024906
true = info < 0;
4903-
varnames = Util.stringDelimitList(Util.listMap(vars,varName)," ; ");
4907+
varnames = Util.stringDelimitList(Util.listMap(vars,ComponentReference.printComponentRefStr)," ;\n ");
49044908
rhsStr = Util.stringDelimitList(Util.listMap(rhs, realString)," ; ");
49054909
jacStr = Util.stringDelimitList(Util.listMap1(Util.listListMap(jac,realString),Util.stringDelimitList," , ")," ; ");
49064910
syst = stringAppendList({"[", jacStr, "] * [",varnames,"] = [",rhsStr,"]"});
@@ -4912,12 +4916,13 @@ end checkLinearSystem;
49124916
protected function generateSolvedEquation
49134917
input SimVar var;
49144918
input Real val;
4919+
input DAE.ElementSource source;
49154920
output SimEqSystem eq;
49164921
protected
49174922
DAE.ComponentRef name;
49184923
algorithm
49194924
SIMVAR(name=name) := var;
4920-
eq := SES_SIMPLE_ASSIGN(name,DAE.RCONST(val),DAE.emptyElementSource);
4925+
eq := SES_SIMPLE_ASSIGN(name,DAE.RCONST(val),source);
49214926
end generateSolvedEquation;
49224927

49234928
protected function replaceDerOpInEquationList
@@ -5285,7 +5290,7 @@ algorithm
52855290
SOME(jac) = BackendDAEUtil.calculateJacobian(v, eqn, ae, m, mT,false);
52865291
((simVars,_)) = BackendVariable.traverseBackendDAEVars(v,traversingdlowvarToSimvar,({},kv));
52875292
simVars = listReverse(simVars);
5288-
((_,_,_,beqs)) = BackendEquation.traverseBackendDAEEqns(eqn,dlowEqToExp,(v,ae,{},{}));
5293+
((_,_,_,beqs,_)) = BackendEquation.traverseBackendDAEEqns(eqn,dlowEqToExp,(v,ae,{},{},{}));
52895294
beqs = listReverse(beqs);
52905295
simJac = Util.listMap1(jac, jacToSimjac, v);
52915296
index = Util.listFirst(block_); // use first equation nr as index
@@ -5317,8 +5322,8 @@ algorithm
53175322
end jacToSimjac;
53185323

53195324
protected function dlowEqToExp
5320-
input tuple<BackendDAE.Equation, tuple<BackendDAE.Variables,array<BackendDAE.MultiDimEquation>,list<tuple<Integer,list<list<DAE.Subscript>>>>,list<DAE.Exp>>> inTpl;
5321-
output tuple<BackendDAE.Equation, tuple<BackendDAE.Variables,array<BackendDAE.MultiDimEquation>,list<tuple<Integer,list<list<DAE.Subscript>>>>,list<DAE.Exp>>> outTpl;
5325+
input tuple<BackendDAE.Equation, tuple<BackendDAE.Variables,array<BackendDAE.MultiDimEquation>,list<tuple<Integer,list<list<DAE.Subscript>>>>,list<DAE.Exp>,list<DAE.ElementSource>>> inTpl;
5326+
output tuple<BackendDAE.Equation, tuple<BackendDAE.Variables,array<BackendDAE.MultiDimEquation>,list<tuple<Integer,list<list<DAE.Subscript>>>>,list<DAE.Exp>,list<DAE.ElementSource>>> outTpl;
53225327
algorithm
53235328
outTpl := matchcontinue inTpl
53245329
local
@@ -5333,22 +5338,24 @@ algorithm
53335338
array<BackendDAE.MultiDimEquation> arrayEqs;
53345339
list<tuple<Integer,list<list<DAE.Subscript>>>> entrylst,entrylst1;
53355340
list<DAE.Exp> explst;
5341+
list<DAE.ElementSource> sources;
5342+
DAE.ElementSource source;
53365343

5337-
case ((eqn as BackendDAE.RESIDUAL_EQUATION(exp=e),(v, arrayEqs,entrylst,explst)))
5344+
case ((eqn as BackendDAE.RESIDUAL_EQUATION(exp=e,source=source),(v, arrayEqs,entrylst,explst,sources)))
53385345
equation
53395346
rhs_exp = BackendDAEUtil.getEqnsysRhsExp(e, v);
53405347
(rhs_exp_1,_) = ExpressionSimplify.simplify(rhs_exp);
5341-
then ((eqn,(v, arrayEqs,entrylst,rhs_exp_1::explst)));
5348+
then ((eqn,(v, arrayEqs,entrylst,rhs_exp_1::explst,source::sources)));
53425349

5343-
case ((eqn as BackendDAE.EQUATION(exp=e1, scalar=e2),(v, arrayEqs,entrylst,explst)))
5350+
case ((eqn as BackendDAE.EQUATION(exp=e1, scalar=e2,source=source),(v, arrayEqs,entrylst,explst,sources)))
53445351
equation
53455352
new_exp = Expression.expSub(e1,e2);
53465353
rhs_exp = BackendDAEUtil.getEqnsysRhsExp(new_exp, v);
53475354
rhs_exp_1 = Expression.negate(rhs_exp);
53485355
(rhs_exp_2,_) = ExpressionSimplify.simplify(rhs_exp_1);
5349-
then ((eqn,(v, arrayEqs,entrylst,rhs_exp_2::explst)));
5356+
then ((eqn,(v, arrayEqs,entrylst,rhs_exp_2::explst,source::sources)));
53505357

5351-
case ((eqn as BackendDAE.ARRAY_EQUATION(index=index),(v, arrayEqs,entrylst,explst)))
5358+
case ((eqn as BackendDAE.ARRAY_EQUATION(index=index,source=source),(v, arrayEqs,entrylst,explst,sources)))
53525359
equation
53535360
BackendDAE.MULTIDIM_EQUATION(dimSize=ds,left=e1, right=e2) = arrayEqs[index+1];
53545361
new_exp = Expression.expSub(e1,e2);
@@ -5358,7 +5365,7 @@ algorithm
53585365
rhs_exp = BackendDAEUtil.getEqnsysRhsExp(new_exp1, v);
53595366
rhs_exp_1 = Expression.negate(rhs_exp);
53605367
(rhs_exp_2,_) = ExpressionSimplify.simplify(rhs_exp_1);
5361-
then ((eqn,(v, arrayEqs,entrylst1,rhs_exp_2::explst)));
5368+
then ((eqn,(v, arrayEqs,entrylst1,rhs_exp_2::explst,source::sources)));
53625369

53635370
case ((eqn,_))
53645371
equation
@@ -11414,12 +11421,9 @@ end dumpVarInfo;
1141411421

1141511422
protected function varName
1141611423
input SimVar var;
11417-
output String name;
11418-
protected
11419-
DAE.ComponentRef cr;
11424+
output DAE.ComponentRef name;
1142011425
algorithm
11421-
SIMVAR(name=cr) := var;
11422-
name := ComponentReference.printComponentRefStr(cr);
11426+
SIMVAR(name=name) := var;
1142311427
end varName;
1142411428

1142511429

Compiler/FrontEnd/DAE.mo

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,12 @@ public uniontype SymbolicOperation
121121
ComponentRef cr;
122122
Exp exp;
123123
end SOLVED;
124+
record LINEAR_SOLVED "Solved linear system of equations"
125+
list<ComponentRef> vars;
126+
list<list<Real>> jac;
127+
list<Real> rhs;
128+
list<Real> result;
129+
end LINEAR_SOLVED;
124130
record OP_INLINE
125131
Exp before;
126132
Exp after;

Compiler/Util/Util.mo

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3437,6 +3437,47 @@ algorithm
34373437
end match;
34383438
end listThreadMap;
34393439

3440+
public function listThread3Map "function: listThread3Map
3441+
Takes two lists and a function and threads (interleaves) and maps the elements of the three lists
3442+
creating a new list.
3443+
Example: listThreadMap({1,2},{3,4},{5,6},intAdd3) => {1+3+5, 2+4+6}"
3444+
input list<Type_a> inTypeALst;
3445+
input list<Type_b> inTypeBLst;
3446+
input list<Type_c> inTypeCLst;
3447+
input FuncType fn;
3448+
output list<Type_d> outTypeCLst;
3449+
replaceable type Type_a subtypeof Any;
3450+
replaceable type Type_b subtypeof Any;
3451+
replaceable type Type_c subtypeof Any;
3452+
replaceable type Type_d subtypeof Any;
3453+
partial function FuncType
3454+
input Type_a inTypeA;
3455+
input Type_b inTypeB;
3456+
input Type_c inTypeC;
3457+
output Type_d outTypeC;
3458+
end FuncType;
3459+
algorithm
3460+
outTypeCLst:=
3461+
match (inTypeALst,inTypeBLst,inTypeCLst,fn)
3462+
local
3463+
Type_d fr;
3464+
list<Type_d> res;
3465+
Type_a fa;
3466+
list<Type_a> ra;
3467+
Type_b fb;
3468+
list<Type_b> rb;
3469+
Type_b fc;
3470+
list<Type_b> rc;
3471+
case ({},{},{},_) then {};
3472+
case ((fa :: ra),(fb :: rb),(fc :: rc),fn)
3473+
equation
3474+
fr = fn(fa, fb, fc);
3475+
res = listThread3Map(ra, rb, rc, fn);
3476+
then
3477+
(fr :: res);
3478+
end match;
3479+
end listThread3Map;
3480+
34403481
public function listThreadMap1 "function: listThreadMap
34413482
Takes two lists and a function and threads (interleaves) and maps the elements of the two lists
34423483
creating a new list.

Compiler/susan_codegen/SimCode/SimCodeDump.tpl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ template dumpOperation(SymbolicOperation op, Info info)
152152
<%printExpStr(op.after)%>
153153
>>
154154
case op as SOLVED(__) then '<%\n%> simple equation: <%crefStr(op.cr)%> = <%printExpStr(op.exp)%>'
155+
case op as LINEAR_SOLVED(__) then '<%\n%> simple equation from linear system: ###'
155156
case op as SOLVE(__) then
156157
<<<%\n%>
157158
solve:

Compiler/susan_codegen/SimCode/SimCodeTV.mo

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1263,6 +1263,12 @@ package DAE
12631263
ComponentRef cr;
12641264
Exp exp;
12651265
end SOLVED;
1266+
record LINEAR_SOLVED
1267+
list<ComponentRef> vars;
1268+
list<list<Real>> jac;
1269+
list<Real> rhs;
1270+
list<Real> result;
1271+
end LINEAR_SOLVED;
12661272
record OP_INLINE
12671273
Exp before;
12681274
Exp after;

0 commit comments

Comments
 (0)