Skip to content

Commit 24b8a35

Browse files
author
Willi Braun
committed
- bugfix in differentiation rule for abs()
- added some improvments in the c_runtime - adjust the SimCodeC.tpl for the c_runtime - adjust some testcase and moved some from msl221 examples to failed testcase, because they not simulate correctly anyway. git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@7988 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
1 parent 6494fa9 commit 24b8a35

File tree

12 files changed

+418
-233
lines changed

12 files changed

+418
-233
lines changed

Compiler/BackEnd/BackendDAECreate.mo

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2370,12 +2370,12 @@ algorithm
23702370
ilst = Util.listIntRange3(istart,istop,istep);
23712371
explst = Util.listMap(ilst,Expression.makeIntegerExp);
23722372
then
2373-
explst;
2373+
explst;
23742374
case (_,_)
23752375
equation
2376-
print("BackendDAECreate.extendRange failed \n");
2376+
Debug.fprint("failtrace","BackendDAECreate.extendRange failed. Maybe some ZeroCrossing are not supported\n");
23772377
then
2378-
fail();
2378+
({});
23792379
end matchcontinue;
23802380
end extendRange;
23812381

@@ -2386,10 +2386,10 @@ protected function expInt "returns the int value of an expression"
23862386
algorithm
23872387
i := match(exp,inKnVariables)
23882388
local
2389-
Integer i2;
2389+
Integer i,i1,i2;
23902390
DAE.ComponentRef cr;
23912391
BackendDAE.Variables knv;
2392-
DAE.Exp e;
2392+
DAE.Exp e,e1,e2;
23932393
case (DAE.ICONST(integer = i2),_) then i2;
23942394
case (DAE.ENUM_LITERAL(index = i2),_) then i2;
23952395
case (DAE.CREF(componentRef=cr),knv)
@@ -2398,6 +2398,18 @@ algorithm
23982398
i2 = expInt(e,knv);
23992399
then
24002400
i2;
2401+
case (DAE.BINARY(exp1 = e1,operator=DAE.ADD(DAE.ET_INT()),exp2 = e2),knv)
2402+
equation
2403+
i1 = expInt(e1,knv);
2404+
i2 = expInt(e1,knv);
2405+
i = i1 + i2;
2406+
then i;
2407+
case (DAE.BINARY(exp1 = e1,operator=DAE.SUB(DAE.ET_INT()),exp2 = e2),knv)
2408+
equation
2409+
i1 = expInt(e1,knv);
2410+
i2 = expInt(e2,knv);
2411+
i = i1 - i2;
2412+
then i;
24012413
end match;
24022414
end expInt;
24032415

Compiler/BackEnd/BackendDAEOptimize.mo

Lines changed: 54 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1986,11 +1986,11 @@ algorithm
19861986
list<tuple<String,Integer>> varTuple;
19871987
array<list<Integer>> m,mT;
19881988

1989-
BackendDAE.Variables v,kv,exv;
1990-
BackendDAE.AliasVariables av;
1991-
BackendDAE.EquationArray e,re,ie;
1992-
array<BackendDAE.MultiDimEquation> ae;
1993-
array<DAE.Algorithm> al;
1989+
BackendDAE.Variables v,kv,exv,vN,kvN;
1990+
BackendDAE.AliasVariables av,avN;
1991+
BackendDAE.EquationArray e,re,ie,eN,reN,ieN;
1992+
array<BackendDAE.MultiDimEquation> ae,aeN;
1993+
array<DAE.Algorithm> al,alN;
19941994
BackendDAE.EventInfo ev;
19951995
BackendDAE.ExternalObjectClasses eoc;
19961996
list<BackendDAE.Equation> e_lst,re_lst,ie_lst;
@@ -2000,6 +2000,8 @@ algorithm
20002000
list<String> s;
20012001
String str;
20022002

2003+
Integer elimLevel;
2004+
20032005
case(dlow as BackendDAE.DAE(v,kv,exv,av,e,re,ie,ae,al,ev,eoc),_,{},_,_)
20042006
equation
20052007
v = BackendDAEUtil.listVar({});
@@ -2014,24 +2016,30 @@ algorithm
20142016
// prepare index for Matrix and variables for simpleEquations
20152017
derivedVariables = BackendDAEUtil.varList(v);
20162018
(varTuple) = determineIndices(eqvars, diffvars, 0, varlst);
2017-
BackendDump.printTuple(varTuple);
2019+
//BackendDump.printTuple(varTuple);
20182020
jacElements = BackendDAE.emptyBintree;
20192021
(derivedVariables,jacElements) = changeIndices(derivedVariables, varTuple, jacElements);
20202022
v = BackendDAEUtil.listVar(derivedVariables);
20212023

2022-
// Remove simple Equtaion and
2024+
// Remove simple Equtaion and
2025+
elimLevel = RTOpts.eliminationLevel();
2026+
RTOpts.setEliminationLevel(2) "Full elimination";
20232027
e_lst = BackendDAEUtil.equationList(e);
20242028
re_lst = BackendDAEUtil.equationList(re);
20252029
ie_lst = BackendDAEUtil.equationList(ie);
20262030
ae_lst = arrayList(ae);
20272031
algs = arrayList(al);
2028-
(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);
2029-
e = BackendDAEUtil.listEquation(e_lst);
2030-
re = BackendDAEUtil.listEquation(re_lst);
2031-
ie = BackendDAEUtil.listEquation(ie_lst);
2032-
ae = listArray(ae_lst);
2033-
al = listArray(algs);
2034-
dlow = BackendDAE.DAE(v,kv,exv,av,e,re,ie,ae,al,ev,eoc);
2032+
(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);
2033+
(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);
2034+
(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);
2035+
(vN,kvN,e_lst,re_lst,ie_lst,ae_lst,algs,avN) = removeSimpleEquations(v,kv, e_lst, re_lst, ie_lst, ae_lst, algs, jacElements);
2036+
eN = BackendDAEUtil.listEquation(e_lst);
2037+
reN = BackendDAEUtil.listEquation(re_lst);
2038+
ieN = BackendDAEUtil.listEquation(ie_lst);
2039+
aeN = listArray(ae_lst);
2040+
alN = listArray(algs);
2041+
RTOpts.setEliminationLevel(elimLevel);
2042+
dlow = BackendDAE.DAE(vN,kvN,exv,avN,eN,reN,ieN,aeN,alN,ev,eoc);
20352043

20362044
// figure out new matching and the strong components
20372045
m = BackendDAEUtil.incidenceMatrix(dlow, BackendDAE.NORMAL());
@@ -2056,7 +2064,7 @@ algorithm
20562064
v4 = arrayCreate(listLength(comps1),0);
20572065
v4 = markArray(v3,comps1,v4);
20582066
(comps1,_) = splitBlocks2(comps1,v4,1);
2059-
2067+
20602068
Debug.fcall("jacdump2", BackendDump.dumpComponents, comps1);
20612069

20622070
then (dlow,v1,v2,comps1);
@@ -2578,17 +2586,19 @@ algorithm
25782586
true = BackendVariable.isStateVar(v1);
25792587
cref = ComponentReference.crefPrefixDer(cref);
25802588
id = ComponentReference.printComponentRefStr(cref) +& BackendDAE.partialDerivativeNamePrefix +& ComponentReference.printComponentRefStr(x);
2589+
id = Util.stringReplaceChar(id, ",", "$K");
25812590
id = Util.stringReplaceChar(id, ".", "$P");
2582-
id = Util.stringReplaceChar(id, "[", "$pL");
2583-
id = Util.stringReplaceChar(id, "]", "$pR");
2591+
id = Util.stringReplaceChar(id, "[", "$lB");
2592+
id = Util.stringReplaceChar(id, "]", "$rB");
25842593
then ComponentReference.makeCrefIdent(id, DAE.ET_REAL(), {});
25852594

25862595
// d(no state)/d(x)
25872596
case(cref, x, _) equation
25882597
id = ComponentReference.printComponentRefStr(cref) +& BackendDAE.partialDerivativeNamePrefix +& ComponentReference.printComponentRefStr(x);
2598+
id = Util.stringReplaceChar(id, ",", "$K");
25892599
id = Util.stringReplaceChar(id, ".", "$P");
2590-
id = Util.stringReplaceChar(id, "[", "$pL");
2591-
id = Util.stringReplaceChar(id, "]", "$pR");
2600+
id = Util.stringReplaceChar(id, "[", "$lB");
2601+
id = Util.stringReplaceChar(id, "]", "$rB");
25922602
then ComponentReference.makeCrefIdent(id, DAE.ET_REAL(), {});
25932603

25942604
case(cref, _, _)
@@ -2623,7 +2633,7 @@ algorithm
26232633
Absyn.Path fname,derFname;
26242634

26252635
list<DAE.Exp> expList1, expList2;
2626-
Boolean tuple_, builtin;
2636+
Boolean tuple_, builtin,b;
26272637
DAE.InlineType inlineType;
26282638
list<BackendDAE.Var> inputVars, paramVars, stateVars, knownVars;
26292639
String str;
@@ -2697,7 +2707,11 @@ algorithm
26972707
case(DAE.CREF(componentRef=cref, ty=et), x, functions, inputVars, paramVars, stateVars, knownVars) equation
26982708
cref_ = differentiateVarWithRespectToX(cref, x, stateVars);
26992709
then DAE.CREF(cref_, et);
2700-
2710+
2711+
// a < b
2712+
case(DAE.RELATION(exp1=e1), x, functions, inputVars, paramVars, stateVars, knownVars)
2713+
then DAE.RCONST(0.0);
2714+
27012715
// a + b
27022716
case(DAE.BINARY(exp1=e1, operator=DAE.ADD(ty=et), exp2=e2), x, functions, inputVars, paramVars, stateVars, knownVars) equation
27032717
e1_ = differentiateWithRespectToX(e1, x, functions, inputVars, paramVars, stateVars, knownVars);
@@ -2749,6 +2763,24 @@ algorithm
27492763
e = ExpressionSimplify.simplify(z7);
27502764
then e;
27512765

2766+
case (DAE.ARRAY(ty = et,scalar = b,array = expList1), x, functions, inputVars, paramVars, stateVars, knownVars)
2767+
equation
2768+
expList2 = Util.listMap6(expList1, differentiateWithRespectToX, x, functions, inputVars, paramVars, stateVars, knownVars);
2769+
then
2770+
DAE.ARRAY(et,b,expList2);
2771+
2772+
case (DAE.TUPLE(PR = expList1), x, functions, inputVars, paramVars, stateVars, knownVars)
2773+
equation
2774+
expList2 = Util.listMap6(expList1, differentiateWithRespectToX, x, functions, inputVars, paramVars, stateVars, knownVars);
2775+
then
2776+
DAE.TUPLE(expList2);
2777+
2778+
case (DAE.ASUB(exp = e,sub = expList1), x, functions, inputVars, paramVars, stateVars, knownVars)
2779+
equation
2780+
e1_ = differentiateWithRespectToX(e, x, functions, inputVars, paramVars, stateVars, knownVars);
2781+
then
2782+
e1_;
2783+
27522784
// der(x)
27532785
case (DAE.CALL(path=fname, expLst={e1}), x, functions, inputVars, paramVars, stateVars, knownVars)
27542786
equation
@@ -2809,7 +2841,7 @@ algorithm
28092841
case (DAE.CALL(path=Absyn.IDENT("abs"), expLst={e1}), x, functions, inputVars, paramVars, stateVars, knownVars)
28102842
equation
28112843
e1_ = differentiateWithRespectToX(e1, x, functions, inputVars, paramVars, stateVars, knownVars);
2812-
then DAE.IFEXP(DAE.RELATION(e1_,DAE.GREATER(DAE.ET_REAL()),DAE.RCONST(0.0),-1,NONE()), e1_, DAE.UNARY(DAE.UMINUS(DAE.ET_REAL()),e1_));
2844+
then DAE.IFEXP(DAE.RELATION(e1,DAE.GREATER(DAE.ET_REAL()),DAE.RCONST(0.0),-1,NONE()), e1_, DAE.UNARY(DAE.UMINUS(DAE.ET_REAL()),e1_));
28132845

28142846
// differentiate if-expressions
28152847
case (DAE.IFEXP(expCond=e, expThen=e1, expElse=e2), x, functions, inputVars, paramVars, stateVars, knownVars)

Compiler/BackEnd/Derive.mo

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,7 @@ algorithm
475475
equation
476476
e1_1 = differentiateExpTime(exp, (timevars,functions));
477477
then
478-
DAE.IFEXP(DAE.RELATION(e1_1,DAE.GREATER(DAE.ET_REAL()),DAE.RCONST(0.0),-1,NONE()), e1_1, DAE.UNARY(DAE.UMINUS(DAE.ET_REAL()),e1_1));
478+
DAE.IFEXP(DAE.RELATION(exp,DAE.GREATER(DAE.ET_REAL()),DAE.RCONST(0.0),-1,NONE()), e1_1, DAE.UNARY(DAE.UMINUS(DAE.ET_REAL()),e1_1));
479479

480480
case (e0 as DAE.BINARY(exp1 = e1,operator = DAE.POW(tp),exp2 = (e2 as DAE.RCONST(_))),(timevars,functions)) /* ax^(a-1) */
481481
equation

Compiler/BackEnd/SimCode.mo

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3349,9 +3349,9 @@ algorithm
33493349
= getEquationAndSolvedVar(eqNum, eqns, vars, ass2);
33503350
eqStr = BackendDump.equationStr(eqn);
33513351
varexp = Expression.crefExp(cr);
3352-
(exp_,asserts) = ExpressionSolve.solveLin(e1, e2, varexp);
3352+
(exp_,_) = ExpressionSolve.solveLin(e1, e2, varexp);
33533353
then
3354-
{SES_ALGORITHM(asserts),SES_SIMPLE_ASSIGN(cr, exp_)};
3354+
{SES_SIMPLE_ASSIGN(cr, exp_)};
33553355

33563356
// when eq without else
33573357
case (eqNum,
@@ -3474,7 +3474,32 @@ algorithm
34743474
message = stringAppendList({"Inverse Algorithm needs to be solved for in ",algStr,". This is not implemented yet.\n"});
34753475
Error.addMessage(Error.INTERNAL_ERROR,{message});
34763476
then fail();
3477-
end matchcontinue;
3477+
// Algorithm for single variable.
3478+
case (eqNum, BackendDAE.DAE(orderedVars=vars,orderedEqs=eqns,algorithms=algs), ass1, ass2, helpVarInfo, true, false)
3479+
equation
3480+
(BackendDAE.ALGORITHM(indx,algInputs,DAE.CREF(varOutput,_)::_,source),v) = getEquationAndSolvedVar(eqNum, eqns, vars, ass2);
3481+
// The output variable of the algorithm must be the variable solved
3482+
// for, otherwise we need to solve an inverse problem of an algorithm
3483+
// section.
3484+
true = ComponentReference.crefEqualNoStringCompare(BackendVariable.varCref(v),varOutput);
3485+
alg = algs[indx + 1];
3486+
algStr = DAEDump.dumpAlgorithmsStr({DAE.ALGORITHM(alg,source)});
3487+
DAE.ALGORITHM_STMTS(algStatements) = BackendDAEUtil.collateAlgorithm(alg, NONE());
3488+
then
3489+
{SES_ALGORITHM(algStatements)};
3490+
3491+
// inverse Algorithm for single variable.
3492+
case (eqNum, BackendDAE.DAE(orderedVars = vars, orderedEqs = eqns,algorithms=algs),ass1,ass2, helpVarInfo, true, skipDiscInAlgorithm)
3493+
equation
3494+
(BackendDAE.ALGORITHM(indx,algInputs,DAE.CREF(varOutput,_)::_,source),v) = getEquationAndSolvedVar(eqNum, eqns, vars, ass2);
3495+
// We need to solve an inverse problem of an algorithm section.
3496+
false = ComponentReference.crefEqualNoStringCompare(BackendVariable.varCref(v),varOutput);
3497+
alg = algs[indx + 1];
3498+
algStr = DAEDump.dumpAlgorithmsStr({DAE.ALGORITHM(alg,source)});
3499+
message = stringAppendList({"Inverse Algorithm needs to be solved for in ",algStr,". This is not implemented yet.\n"});
3500+
Error.addMessage(Error.INTERNAL_ERROR,{message});
3501+
then fail();
3502+
end matchcontinue;
34783503
end createEquation;
34793504

34803505
protected function createElseWhenEquation

Compiler/susan_codegen/SimCode/SimCodeC.tpl

Lines changed: 17 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -573,16 +573,19 @@ template functionInitializeDataStruc()
573573
returnData->statesDerivativesFilterOutput = (modelica_boolean*) malloc(sizeof(modelica_boolean)*returnData->nStates);
574574
returnData->statesDerivatives_old = (double*) malloc(sizeof(double)*returnData->nStates);
575575
returnData->statesDerivatives_old2 = (double*) malloc(sizeof(double)*returnData->nStates);
576-
assert(returnData->statesDerivatives&&returnData->statesDerivatives_old&&returnData->statesDerivatives_old2);
576+
returnData->statesDerivativesBackup = (double*) malloc(sizeof(double)*returnData->nStates);
577+
assert(returnData->statesDerivatives&&returnData->statesDerivatives_old&&returnData->statesDerivatives_old2&&returnData->statesDerivativesBackup);
577578
memset(returnData->statesDerivatives,0,sizeof(double)*returnData->nStates);
578579
memset(returnData->statesDerivativesFilterOutput,0,sizeof(modelica_boolean)*returnData->nStates);
579580
memset(returnData->statesDerivatives_old,0,sizeof(double)*returnData->nStates);
580581
memset(returnData->statesDerivatives_old2,0,sizeof(double)*returnData->nStates);
582+
memset(returnData->statesDerivativesBackup,0,sizeof(double)*returnData->nStates);
581583
} else {
582584
returnData->statesDerivatives = 0;
583585
returnData->statesDerivativesFilterOutput = 0;
584586
returnData->statesDerivatives_old = 0;
585587
returnData->statesDerivatives_old2 = 0;
588+
returnData->statesDerivativesBackup = 0;
586589
}
587590

588591
if (returnData->nHelpVars) {
@@ -715,7 +718,7 @@ template functionInitializeDataStruc()
715718
} else {
716719
returnData->initialResiduals = 0;
717720
}
718-
721+
719722
returnData->initFixed = init_fixed;
720723
returnData->modelName = model_name;
721724
returnData->modelFilePrefix = model_fileprefix;
@@ -1653,41 +1656,25 @@ template functionODE_residual()
16531656
int functionODE_residual(double *t, double *x, double *xd, double *delta,
16541657
fortran_integer *ires, double *rpar, fortran_integer *ipar)
16551658
{
1656-
int i;
1657-
double temp_xd[NX];
1658-
double* statesBackup;
1659-
double* statesDerivativesBackup;
16601659
double timeBackup;
1661-
1660+
double* statesBackup;
1661+
16621662
timeBackup = localData->timeValue;
16631663
statesBackup = localData->states;
1664-
statesDerivativesBackup = localData->statesDerivatives;
1665-
1664+
16661665
localData->timeValue = *t;
16671666
localData->states = x;
1668-
localData->statesDerivatives = temp_xd;
1669-
1670-
memcpy(localData->statesDerivatives, statesDerivativesBackup, localData->nStates*sizeof(double));
1671-
16721667
functionODE_new();
16731668
16741669
/* get the difference between the temp_xd(=localData->statesDerivatives)
16751670
and xd(=statesDerivativesBackup) */
1676-
for (i=0; i < localData->nStates; i++) {
1677-
delta[i] = localData->statesDerivatives[i] - statesDerivativesBackup[i];
1671+
for (int i=0; i < localData->nStates; i++) {
1672+
delta[i] = localData->statesDerivatives[i] - xd[i];
16781673
}
1679-
1674+
16801675
localData->states = statesBackup;
1681-
localData->statesDerivatives = statesDerivativesBackup;
16821676
localData->timeValue = timeBackup;
1683-
1684-
if (modelErrorCode) {
1685-
if (ires) {
1686-
*ires = -1;
1687-
}
1688-
modelErrorCode =0;
1689-
}
1690-
1677+
16911678
return 0;
16921679
}
16931680
>>
@@ -1807,37 +1794,23 @@ template functionJac(list<SimEqSystem> JacEquations, list<SimVar> JacVars, Strin
18071794
let Vars_ = (JacVars |> var =>
18081795
defvars(var)
18091796
;separator="\n")
1797+
18101798
let writeJac_ = (JacVars |> var =>
18111799
writejac(var)
18121800
;separator="\n")
18131801
<<
1814-
int functionJac<%MatrixName%>(double *t, double *x, double *xd, double *jac)
1802+
int functionJac<%MatrixName%>( double *jac)
18151803
{
18161804
state mem_state;
18171805
1818-
double* statesBackup;
1819-
double* statesDerivativesBackup;
1820-
double timeBackup;
1821-
1822-
timeBackup = localData->timeValue;
1823-
statesBackup = localData->states;
1824-
statesDerivativesBackup = localData->statesDerivatives;
1825-
localData->timeValue = *t;
1826-
localData->states = x;
1827-
localData->statesDerivatives = xd;
1828-
1829-
<%Vars_%>
1806+
18301807
<%varDecls%>
18311808
18321809
mem_state = get_memory_state();
18331810
<%Equations_%>
18341811
<%writeJac_%>
18351812
restore_memory_state(mem_state);
18361813
1837-
localData->states = statesBackup;
1838-
localData->statesDerivatives = statesDerivativesBackup;
1839-
localData->timeValue = timeBackup;
1840-
18411814
return 0;
18421815
}
18431816

@@ -2777,8 +2750,8 @@ template subscriptToMStr(Subscript subscript)
27772750
let &preExp = buffer ""
27782751
let &varDecls = buffer ""
27792752
match subscript
2780-
case INDEX(__)
2781-
case SLICE(__) then daeExp(exp, contextSimulationNonDiscrete, &preExp /*BUFC*/, &varDecls /*BUFD*/)
2753+
case INDEX(exp=ICONST(integer=i)) then i
2754+
case SLICE(exp=ICONST(integer=i)) then i
27822755
case WHOLEDIM(__) then "WHOLEDIM"
27832756
else "UNKNOWN_SUBSCRIPT"
27842757
end subscriptToMStr;

0 commit comments

Comments
 (0)