Skip to content
This repository was archived by the owner on May 18, 2019. It is now read-only.

Commit 6d2338d

Browse files
Willi BraunOpenModelica-Hudson
authored andcommitted
improve variable generation for jacobians
- use varKind to distinguish result, tmp and seed vars - added matrixName to SimVar to remove the jacobian defines in future Belonging to [master]: - #1890 - OpenModelica/OpenModelica-testsuite#737
1 parent 024a75d commit 6d2338d

File tree

5 files changed

+123
-114
lines changed

5 files changed

+123
-114
lines changed

Compiler/SimCode/SimCodeUtil.mo

Lines changed: 69 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -3120,7 +3120,7 @@ algorithm
31203120

31213121
case(DAE.CREF(cr, ty)::rest) equation
31223122
slst = List.map(dims, intString);
3123-
var = SimCodeVar.SIMVAR(cr, BackendDAE.VARIABLE(), "", "", "", 0, NONE(), NONE(), NONE(), NONE(), false, ty, false, SOME(name), SimCodeVar.NOALIAS(), DAE.emptyElementSource, SimCodeVar.NONECAUS(), NONE(), slst, false, true, false, NONE());
3123+
var = SimCodeVar.SIMVAR(cr, BackendDAE.VARIABLE(), "", "", "", 0, NONE(), NONE(), NONE(), NONE(), false, ty, false, SOME(name), SimCodeVar.NOALIAS(), DAE.emptyElementSource, SimCodeVar.NONECAUS(), NONE(), slst, false, true, false, NONE(), NONE());
31243124
tempvars = createTempVarsforCrefs(rest, {var});
31253125
then List.append_reverse(tempvars, itempvars);
31263126
end match;
@@ -3156,7 +3156,7 @@ algorithm
31563156
arrayCref = ComponentReference.getArrayCref(cr);
31573157
inst_dims = ComponentReference.crefDims(cr);
31583158
numArrayElement = List.map(inst_dims, ExpressionDump.dimensionString);
3159-
var = SimCodeVar.SIMVAR(cr, BackendDAE.VARIABLE(), "", "", "", 0, NONE(), NONE(), NONE(), NONE(), false, ty, false, arrayCref, SimCodeVar.NOALIAS(), DAE.emptyElementSource, SimCodeVar.NONECAUS(), NONE(), numArrayElement, false, true, false, NONE());
3159+
var = SimCodeVar.SIMVAR(cr, BackendDAE.VARIABLE(), "", "", "", 0, NONE(), NONE(), NONE(), NONE(), false, ty, false, arrayCref, SimCodeVar.NOALIAS(), DAE.emptyElementSource, SimCodeVar.NONECAUS(), NONE(), numArrayElement, false, true, false, NONE(), NONE());
31603160
then createTempVarsforCrefs(rest, var::itempvars);
31613161
end match;
31623162
end createTempVarsforCrefs;
@@ -3199,13 +3199,13 @@ algorithm
31993199
arraycref := ComponentReference.crefStripSubs(cr);
32003200
ty := ComponentReference.crefTypeFull(cr);
32013201
var := SimCodeVar.SIMVAR(cr, BackendDAE.VARIABLE(), "", "", "", 0, NONE(), NONE(), NONE(), NONE(), false,
3202-
ty, false, SOME(arraycref), SimCodeVar.NOALIAS(), DAE.emptyElementSource, SimCodeVar.NONECAUS(), NONE(), {}, false, true, false, NONE());
3202+
ty, false, SOME(arraycref), SimCodeVar.NOALIAS(), DAE.emptyElementSource, SimCodeVar.NONECAUS(), NONE(), {}, false, true, false, NONE(), NONE());
32033203

32043204
/* The rest don't need to be marked i.e. we have 'NONE()'. Just create simvars. */
32053205
ttmpvars := {var};
32063206
for cr in crlst loop
32073207
ty := ComponentReference.crefTypeFull(cr);
3208-
var := SimCodeVar.SIMVAR(cr, BackendDAE.VARIABLE(), "", "", "", 0, NONE(), NONE(), NONE(), NONE(), false, ty, false, NONE(), SimCodeVar.NOALIAS(), DAE.emptyElementSource, SimCodeVar.NONECAUS(), NONE(), {}, false, true, false, NONE());
3208+
var := SimCodeVar.SIMVAR(cr, BackendDAE.VARIABLE(), "", "", "", 0, NONE(), NONE(), NONE(), NONE(), false, ty, false, NONE(), SimCodeVar.NOALIAS(), DAE.emptyElementSource, SimCodeVar.NONECAUS(), NONE(), {}, false, true, false, NONE(), NONE());
32093209
ttmpvars := var::ttmpvars;
32103210
end for;
32113211
ttmpvars := Dangerous.listReverseInPlace(ttmpvars);
@@ -4435,13 +4435,18 @@ algorithm
44354435
residualVars = BackendVariable.listVar1(residualVarsLst);
44364436
independentVars = BackendVariable.listVar1(independentVarsLst);
44374437

4438+
// get cse and other aux vars > columnVars
44384439
((allVars, _)) = BackendVariable.traverseBackendDAEVars(syst.orderedVars, getFurtherVars , ({}, x));
44394440
systvars = BackendVariable.listVar1(allVars);
44404441
((columnVars, _)) = BackendVariable.traverseBackendDAEVars(systvars, traversingdlowvarToSimvar, ({}, emptyVars));
4441-
columnVars = createAllDiffedSimVars(dependentVarsLst, x, residualVars, 0, name, columnVars);
4442+
columnVars = List.map1(columnVars, setSimVarKind, BackendDAE.JAC_DIFF_VAR());
4443+
columnVars = List.map1(columnVars, setSimVarMatrixName, SOME(name));
4444+
columnVars = rewriteIndex(columnVars, 0);
4445+
4446+
columnVars = createAllDiffedSimVars(dependentVarsLst, x, residualVars, 0, listLength(columnVars), name, columnVars);
44424447
columnVars = listReverse(columnVars);
44434448

4444-
if Flags.isSet(Flags.JAC_DUMP2) then
4449+
if Flags.isSet(Flags.JAC_DUMP2) then
44454450
print("\n---+++ all column variables +++---\n");
44464451
print(Tpl.tplString(SimCodeDump.dumpVarsShort, columnVars));
44474452
print("analytical Jacobians -> create all SimCode vars for Matrix " + name + " time: " + realString(clock()) + "\n");
@@ -4469,6 +4474,8 @@ algorithm
44694474

44704475
// create seed vars
44714476
seedVars = replaceSeedVarsName(seedVars, name);
4477+
seedVars = List.map1(seedVars, setSimVarKind, BackendDAE.SEED_VAR());
4478+
seedVars = List.map1(seedVars, setSimVarMatrixName, SOME(name));
44724479

44734480
if Flags.isSet(Flags.JAC_DUMP2) then
44744481
print("analytical Jacobians -> transformed to SimCode for Matrix " + name + " time: " + realString(clock()) + "\n");
@@ -4659,6 +4666,8 @@ algorithm
46594666

46604667
// create seed vars
46614668
seedVars = replaceSeedVarsName(seedVars, name);
4669+
seedVars = List.map1(seedVars, setSimVarKind, BackendDAE.SEED_VAR());
4670+
seedVars = List.map1(seedVars, setSimVarMatrixName, SOME(name));
46624671

46634672
tmpJac = SimCode.JAC_MATRIX({SimCode.JAC_COLUMN({},{},nRows)}, seedVars, name, sparseInts, sparseIntsT, coloring, maxColor, -1, 0);
46644673
linearModelMatrices = tmpJac::inJacobianMatrixes;
@@ -4685,10 +4694,13 @@ algorithm
46854694
dummyVar = ("dummyVar" + name);
46864695
x = DAE.CREF_IDENT(dummyVar, DAE.T_REAL_DEFAULT, {});
46874696

4697+
// get cse and other aux vars > columnVars
46884698
emptyVars = BackendVariable.emptyVars();
46894699
((allVars, _)) = BackendVariable.traverseBackendDAEVars(syst.orderedVars, getFurtherVars , ({}, x));
46904700
systvars = BackendVariable.listVar1(allVars);
46914701
((otherColumnVars, _)) = BackendVariable.traverseBackendDAEVars(systvars, traversingdlowvarToSimvar, ({}, emptyVars));
4702+
otherColumnVars = List.map1(otherColumnVars, setSimVarKind, BackendDAE.JAC_DIFF_VAR());
4703+
otherColumnVars = rewriteIndex(otherColumnVars, 0);
46924704

46934705
//sort variable for index
46944706
empty = BackendVariable.listVar1(alldiffedVars);
@@ -4698,7 +4710,13 @@ algorithm
46984710
(_, (_, alldiffedVars)) = List.mapFoldTuple(columnVars, sortBackVarWithSimVarsOrder, (empty, {}));
46994711
alldiffedVars = listReverse(alldiffedVars);
47004712
vars = BackendVariable.listVar1(diffedVars);
4701-
columnVars = createAllDiffedSimVars(alldiffedVars, x, vars, 0, name, otherColumnVars);
4713+
4714+
columnVars = createAllDiffedSimVars(alldiffedVars, x, vars, 0, listLength(otherColumnVars), name, otherColumnVars);
4715+
if Flags.isSet(Flags.JAC_DUMP2) then
4716+
print("\n---+++ second columnVars +++---\n");
4717+
print(Tpl.tplString(SimCodeDump.dumpVarsShort, columnVars));
4718+
print("analytical Jacobians -> create column variables for matrix " + name + " time: " + realString(clock()) + "\n");
4719+
end if;
47024720

47034721
if Flags.isSet(Flags.JAC_DUMP2) then
47044722
print("analytical Jacobians -> create all SimCode vars for Matrix " + name + " time: " + realString(clock()) + "\n");
@@ -4748,6 +4766,8 @@ algorithm
47484766

47494767
// create seed vars
47504768
seedVars = replaceSeedVarsName(seedVars, name);
4769+
seedVars = List.map1(seedVars, setSimVarKind, BackendDAE.SEED_VAR());
4770+
seedVars = List.map1(seedVars, setSimVarMatrixName, SOME(name));
47514771

47524772
tmpJac = SimCode.JAC_MATRIX({SimCode.JAC_COLUMN(columnEquations, columnVars, nRows)}, seedVars, name, sparseInts, sparseIntsT, coloring, maxColor, -1, 0);
47534773
linearModelMatrices = tmpJac::inJacobianMatrixes;
@@ -4810,64 +4830,55 @@ protected function createAllDiffedSimVars "author: wbraun"
48104830
input list<BackendDAE.Var> inVars;
48114831
input DAE.ComponentRef inCref;
48124832
input BackendDAE.Variables inAllVars;
4813-
input Integer inIndex;
4833+
input Integer inResIndex;
4834+
input Integer inTmpIndex;
48144835
input String inMatrixName;
48154836
input list<SimCodeVar.SimVar> iVars;
48164837
output list<SimCodeVar.SimVar> outVars;
48174838
algorithm
4818-
outVars := match(inVars, inCref, inAllVars, inIndex, inMatrixName, iVars)
4839+
outVars := match(inVars, inCref, inAllVars, inResIndex, inTmpIndex, inMatrixName, iVars)
48194840
local
4820-
BackendDAE.Var v1;
4821-
SimCodeVar.SimVar r1;
4841+
BackendDAE.Var v, v1;
4842+
SimCodeVar.SimVar simVar;
48224843
DAE.ComponentRef currVar, cref, derivedCref;
48234844
list<BackendDAE.Var> restVar;
48244845
Option<DAE.VariableAttributes> dae_var_attr;
48254846
Boolean isProtected;
48264847
Boolean hideResult = false;
4827-
Integer index;
4848+
Integer resIndex, tmpIndex;
48284849
BackendDAE.VarKind varkind;
48294850

4830-
case({}, _, _, _, _, _) then listReverse(iVars);
4851+
case({}, _, _, _, _, _, _) then listReverse(iVars);
48314852

4832-
case(BackendDAE.VAR(varName=currVar, varKind=varkind, values = dae_var_attr)::restVar, cref, _, index, _, _) algorithm
4853+
case((v as BackendDAE.VAR(varName=currVar, varKind=varkind, values = dae_var_attr))::restVar, cref, _, resIndex, tmpIndex, _, _) algorithm
48334854
try
48344855
BackendVariable.getVarSingle(currVar, inAllVars);
4835-
r1 := match (varkind)
4836-
case BackendDAE.STATE()
4837-
equation
4838-
currVar = ComponentReference.crefPrefixDer(currVar);
4839-
derivedCref = Differentiate.createDifferentiatedCrefName(currVar, cref, inMatrixName);
4840-
isProtected = getProtected(dae_var_attr);
4841-
index = index + 1;
4842-
then
4843-
SimCodeVar.SIMVAR(derivedCref, BackendDAE.STATE_DER(), "", "", "", inIndex, NONE(), NONE(), NONE(), NONE(), false, DAE.T_REAL_DEFAULT, false, NONE(), SimCodeVar.NOALIAS(), DAE.emptyElementSource, SimCodeVar.NONECAUS(), NONE(), {}, false, isProtected, hideResult, NONE());
4844-
else
4845-
equation
4846-
derivedCref = Differentiate.createDifferentiatedCrefName(currVar, cref, inMatrixName);
4847-
isProtected = getProtected(dae_var_attr);
4848-
index = index + 1;
4849-
then
4850-
SimCodeVar.SIMVAR(derivedCref, BackendDAE.STATE_DER(), "", "", "", inIndex, NONE(), NONE(), NONE(), NONE(), false, DAE.T_REAL_DEFAULT, false, NONE(), SimCodeVar.NOALIAS(), DAE.emptyElementSource, SimCodeVar.NONECAUS(), NONE(), {}, false, isProtected, hideResult, NONE());
4856+
currVar := match (varkind)
4857+
case BackendDAE.STATE() then ComponentReference.crefPrefixDer(currVar);
4858+
else currVar;
48514859
end match;
4860+
derivedCref := Differentiate.createDifferentiatedCrefName(currVar, cref, inMatrixName);
4861+
v1 := BackendVariable.copyVarNewName(derivedCref, v);
4862+
v1 := BackendVariable.setVarKind(v1, BackendDAE.JAC_VAR());
4863+
simVar := dlowvarToSimvar(v1, NONE(), inAllVars);
4864+
simVar.index := resIndex;
4865+
resIndex := resIndex + 1;
48524866
else
4853-
r1 := match (varkind)
4854-
case BackendDAE.STATE()
4855-
equation
4856-
currVar = ComponentReference.crefPrefixDer(currVar);
4857-
derivedCref = Differentiate.createDifferentiatedCrefName(currVar, cref, inMatrixName);
4858-
isProtected = getProtected(dae_var_attr);
4859-
then
4860-
SimCodeVar.SIMVAR(derivedCref, BackendDAE.VARIABLE(), "", "", "", -1, NONE(), NONE(), NONE(), NONE(), false, DAE.T_REAL_DEFAULT, false, NONE(), SimCodeVar.NOALIAS(), DAE.emptyElementSource, SimCodeVar.NONECAUS(), NONE(), {}, false, isProtected, hideResult, NONE());
4861-
else
4862-
equation
4863-
derivedCref = Differentiate.createDifferentiatedCrefName(currVar, cref, inMatrixName);
4864-
isProtected = getProtected(dae_var_attr);
4865-
then
4866-
SimCodeVar.SIMVAR(derivedCref, BackendDAE.VARIABLE(), "", "", "", -1, NONE(), NONE(), NONE(), NONE(), false, DAE.T_REAL_DEFAULT, false, NONE(), SimCodeVar.NOALIAS(), DAE.emptyElementSource, SimCodeVar.NONECAUS(), NONE(), {}, false, isProtected, hideResult, NONE());
4867+
currVar := match (varkind)
4868+
case BackendDAE.STATE() then ComponentReference.crefPrefixDer(currVar);
4869+
else currVar;
48674870
end match;
4871+
derivedCref := Differentiate.createDifferentiatedCrefName(currVar, cref, inMatrixName);
4872+
v1 := BackendVariable.copyVarNewName(derivedCref, v);
4873+
v1 := BackendVariable.setVarKind(v1, BackendDAE.JAC_DIFF_VAR());
4874+
simVar := dlowvarToSimvar(v1, NONE(), inAllVars);
4875+
simVar.index := tmpIndex;
4876+
tmpIndex := tmpIndex + 1;
48684877
end try;
4878+
simVar.matrixName := SOME(inMatrixName);
48694879
then
4870-
createAllDiffedSimVars(restVar, cref, inAllVars, index, inMatrixName, r1::iVars);
4880+
createAllDiffedSimVars(restVar, cref, inAllVars, resIndex, tmpIndex, inMatrixName, simVar::iVars);
4881+
48714882
else
48724883
equation
48734884
Error.addInternalError("function createAllDiffedSimVars failed", sourceInfo());
@@ -4905,7 +4916,6 @@ algorithm
49054916
outVars := listAppend(tmp, outVars);
49064917
end for;
49074918
end for;
4908-
outVars := List.map1(outVars, setSimVarKind, BackendDAE.JAC_VAR());
49094919
end collectAllJacobianVars;
49104920

49114921
protected function collectAllSeedVars
@@ -4929,6 +4939,13 @@ algorithm
49294939
simVar.varKind := varKind;
49304940
end setSimVarKind;
49314941

4942+
protected function setSimVarMatrixName
4943+
input output SimCodeVar.SimVar simVar;
4944+
input Option<String> optName;
4945+
algorithm
4946+
simVar.matrixName := optName;
4947+
end setSimVarMatrixName;
4948+
49324949
protected function makeTmpRealSimCodeVar
49334950
input DAE.ComponentRef inName;
49344951
input BackendDAE.VarKind inVarKind;
@@ -4937,7 +4954,7 @@ algorithm
49374954
outSimVar := SimCodeVar.SIMVAR(inName, inVarKind, "", "", "", -1 /* use -1 to get an error in simulation if something failed */,
49384955
NONE(), NONE(), NONE(), NONE(), false, DAE.T_REAL_DEFAULT,
49394956
false, NONE(), SimCodeVar.NOALIAS(), DAE.emptyElementSource,
4940-
SimCodeVar.NONECAUS(), NONE(), {}, false, false, false, NONE());
4957+
SimCodeVar.NONECAUS(), NONE(), {}, false, false, false, NONE(), NONE());
49414958
end makeTmpRealSimCodeVar;
49424959

49434960
protected function sortSparsePattern
@@ -8874,7 +8891,7 @@ algorithm
88748891
and isFixed;
88758892
then
88768893
SimCodeVar.SIMVAR(cr, kind, commentStr, unit, displayUnit, -1 /* use -1 to get an error in simulation if something failed */,
8877-
minValue, maxValue, initVal, nomVal, isFixed, type_, isDiscrete, arrayCref, aliasvar, source, caus, NONE(), numArrayElement, isValueChangeable, isProtected, hideResult, NONE());
8894+
minValue, maxValue, initVal, nomVal, isFixed, type_, isDiscrete, arrayCref, aliasvar, source, caus, NONE(), numArrayElement, isValueChangeable, isProtected, hideResult, NONE(), NONE());
88788895

88798896
// Start value of states may be changeable
88808897
case ((BackendDAE.VAR(varName = cr,
@@ -8905,7 +8922,7 @@ algorithm
89058922
// print("name: " + ComponentReference.printComponentRefStr(cr) + "indx: " + intString(indx) + "\n");
89068923
then
89078924
SimCodeVar.SIMVAR(cr, kind, commentStr, unit, displayUnit, -1 /* use -1 to get an error in simulation if something failed */,
8908-
minValue, maxValue, initVal, nomVal, isFixed, type_, isDiscrete, arrayCref, aliasvar, source, caus, NONE(), numArrayElement, isValueChangeable, isProtected, hideResult, NONE());
8925+
minValue, maxValue, initVal, nomVal, isFixed, type_, isDiscrete, arrayCref, aliasvar, source, caus, NONE(), numArrayElement, isValueChangeable, isProtected, hideResult, NONE(), NONE());
89098926

89108927
case ((BackendDAE.VAR(varName = cr,
89118928
varKind = kind,
@@ -8935,7 +8952,7 @@ algorithm
89358952
// print("name: " + ComponentReference.printComponentRefStr(cr) + "indx: " + intString(indx) + "\n");
89368953
then
89378954
SimCodeVar.SIMVAR(cr, kind, commentStr, unit, displayUnit, -1 /* use -1 to get an error in simulation if something failed */,
8938-
minValue, maxValue, initVal, nomVal, isFixed, type_, isDiscrete, arrayCref, aliasvar, source, caus, NONE(), numArrayElement, isValueChangeable, isProtected, hideResult, NONE());
8955+
minValue, maxValue, initVal, nomVal, isFixed, type_, isDiscrete, arrayCref, aliasvar, source, caus, NONE(), numArrayElement, isValueChangeable, isProtected, hideResult, NONE(), NONE());
89398956
end match;
89408957
end dlowvarToSimvar;
89418958

@@ -12233,6 +12250,7 @@ author:Waurich TUD 2014-05"
1223312250
output SimCodeVar.SimVar simVarOut = simVarIn;
1223412251
algorithm
1223512252
simVarOut.name := cref;
12253+
simVarOut.arrayCref := ComponentReference.getArrayCref(cref);
1223612254
end replaceSimVarName;
1223712255

1223812256
public function replaceSimVarIndex "updates the index of simVarIn.
@@ -13302,7 +13320,7 @@ algorithm
1330213320
"Template did not find the simulation variable for "+ ComponentReference.printComponentRefStr(cref) + ". ";
1330313321
Error.addInternalError(errstr, sourceInfo());*/
1330413322
then
13305-
SimCodeVar.SIMVAR(badcref, BackendDAE.VARIABLE(), "", "", "", -2, NONE(), NONE(), NONE(), NONE(), false, DAE.T_REAL_DEFAULT, false, NONE(), SimCodeVar.NOALIAS(), DAE.emptyElementSource, SimCodeVar.INTERNAL(), NONE(), {}, false, true, false, NONE());
13323+
SimCodeVar.SIMVAR(badcref, BackendDAE.VARIABLE(), "", "", "", -2, NONE(), NONE(), NONE(), NONE(), false, DAE.T_REAL_DEFAULT, false, NONE(), SimCodeVar.NOALIAS(), DAE.emptyElementSource, SimCodeVar.INTERNAL(), NONE(), {}, false, true, false, NONE(), NONE());
1330613324
end matchcontinue;
1330713325
end cref2simvar;
1330813326

Compiler/SimCode/SimCodeVar.mo

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ public uniontype SimVar "Information about a variable in a Modelica model."
102102
Boolean isProtected;
103103
Boolean hideResult;
104104
Option<array<Integer>> inputIndex;
105+
Option<String> matrixName; // if the varibale is a jacobian var, this is the corresponding matrix
105106
end SIMVAR;
106107
end SimVar;
107108

0 commit comments

Comments
 (0)