Skip to content

Commit

Permalink
Fixed bug with evaluating e.g. cos(0)/2 (due to Exp.simplify (MC bug …
Browse files Browse the repository at this point in the history
…656)

git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@2457 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Peter Aronsson committed Aug 8, 2006
1 parent ce2d8ad commit cb5f12f
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 16 deletions.
28 changes: 18 additions & 10 deletions Compiler/DAELow.mo
Expand Up @@ -5911,7 +5911,7 @@ algorithm
print(mlen_str);
print("\n");
m_1 := arrayList(m);
dumpIncidenceMatrix2(m_1);
dumpIncidenceMatrix2(m_1,1);
end dumpIncidenceMatrix;

public function dumpIncidenceMatrixT "function: dumpIncidenceMatrixT
Expand All @@ -5932,7 +5932,7 @@ algorithm
print(mlen_str);
print("\n");
m_1 := arrayList(m);
dumpIncidenceMatrix2(m_1);
dumpIncidenceMatrix2(m_1,1);
end dumpIncidenceMatrixT;

protected function dumpIncidenceMatrix2 "function: dumpIncidenceMatrix2
Expand All @@ -5941,17 +5941,19 @@ protected function dumpIncidenceMatrix2 "function: dumpIncidenceMatrix2
Helper function to dump_incidence_matrix (+_t).
"
input list<list<Integer>> inIntegerLstLst;
input Integer rowIndex;
algorithm
_:=
matchcontinue (inIntegerLstLst)
matchcontinue (inIntegerLstLst,rowIndex)
local
list<Value> row;
list<list<Value>> rows;
case ({}) then ();
case ((row :: rows))
case ({},_) then ();
case ((row :: rows),rowIndex)
equation
print(intString(rowIndex));print(":");
dumpIncidenceRow(row);
dumpIncidenceMatrix2(rows);
dumpIncidenceMatrix2(rows,rowIndex+1);
then
();
end matchcontinue;
Expand All @@ -5965,12 +5967,12 @@ protected function dumpIncidenceRow "function: dumpIncidenceRow
input list<Integer> inIntegerLst;
algorithm
_:=
matchcontinue (inIntegerLst)
matchcontinue (inIntegerLst,rowIndex)
local
String s;
Value x;
list<Value> xs;
case {}
case ({})
equation
print("\n");
then
Expand Down Expand Up @@ -6593,14 +6595,18 @@ algorithm
(dae,m,mt,nv,nf,deqns) = differentiateEqns(dae, m, mt, nv, nf, eqns_1);
(state,stateno) = selectDummyState(states, stateindx, dae, m, mt);
dae = propagateDummyFixedAttribute(dae, eqns_1, state, stateno);
(dummy_der,dae) = newDummyVar(state, dae) "Exp.print_component_ref_str state => statestr &
print \"Choosen dummy state: \" & print statestr & print \"\\n\" &" ;
(dummy_der,dae) = newDummyVar(state, dae) ;
//print("Chosen dummy: ");print(Exp.printComponentRefStr(dummy_der));print("\n");
reqns = eqnsForVarWithStates(mt, stateno);
changedeqns = Util.listUnionP(deqns, reqns, int_eq);
(dae,m,mt) = replaceDummyDer(state, dummy_der, dae, m, mt, changedeqns) "We need to change variables in the differentiated equations and in the
equations having the dummy derivative" ;
dae = makeAlgebraic(dae, state);
(m,mt) = updateIncidenceMatrix(dae, m, mt, changedeqns);
//print("new DAE:");
//dump(dae);
//print("new IM:");
//dumpIncidenceMatrix(m);
then
(dae,m,mt);
case (dae,m,mt,nv,nf,i)
Expand All @@ -6614,6 +6620,8 @@ algorithm
({},_) = statesInEqns(eqns_1, dae, m, mt);
print("no states found in equations:");
printEquations(eqns_1, dae);
print("differentiated equations:");
printEquations(diff_eqns,dae);
print("Variables :");print(Util.stringDelimitList(Util.listMap(DAEEXT.getMarkedVariables(),intString),", "));
print("\n");

Expand Down
4 changes: 2 additions & 2 deletions Compiler/Derive.mo
Expand Up @@ -157,15 +157,15 @@ algorithm
(_,_) = DAELow.getVar(cr, timevars);
then
Exp.CALL(Absyn.IDENT("der"),{e},false,true);
case (Exp.CALL(path = fname,expLst = {e},tuple_ = false,builtin = true),timevars)
case (Exp.CALL(path = fname,expLst = {e}),timevars)
equation
isSin(fname);
e_1 = differentiateExpTime(e, timevars) "der(sin(x)) = der(x)cos(x)" ;
then
Exp.BINARY(e_1,Exp.MUL(Exp.REAL()),
Exp.CALL(Absyn.IDENT("cos"),{e},false,true));

case (Exp.CALL(path = fname,expLst = {e},tuple_ = false,builtin = true),timevars)
case (Exp.CALL(path = fname,expLst = {e}),timevars)
equation
isCos(fname);
e_1 = differentiateExpTime(e, timevars) "der(cos(x)) = -der(x)sin(x)" ;
Expand Down
32 changes: 28 additions & 4 deletions Compiler/Exp.mo
Expand Up @@ -1863,7 +1863,7 @@ end simplifyBinaryMulConstants;
protected function simplifyMul "function: simplifyMul
author: PA
Simplifies expressions like aaababa
Simplifies expressions like a*a*a*b*a*b*a
"
input list<Exp> expl;
output list<Exp> expl_1;
Expand Down Expand Up @@ -2808,6 +2808,7 @@ algorithm
case ({e1,e2})
equation
tp = typeof(e1) "Take type info from e1, ok since type checking already performed." ;
tp = checkIfOther(tp);
then
BINARY(e1,MUL(tp),e2);
case ((BINARY(exp1 = e1,operator = DIV(ty = tp),exp2 = e) :: es))
Expand All @@ -2819,7 +2820,8 @@ algorithm
case ((e1 :: rest))
equation
e2 = makeProduct(rest);
tp = typeof(e2);
tp = typeof(e1);
tp = checkIfOther(tp);
then
BINARY(e1,MUL(tp),e2);
case (lst)
Expand All @@ -2834,6 +2836,18 @@ algorithm
end matchcontinue;
end makeProduct;

protected function checkIfOther "Checks if a type is OTHER and in that case returns REAL instead.
THis is used to make proper transformations in case OTHER is retrieved from subexpression where it should instead be
REAL or INT"
input Type inTp;
output Type outTp;
algorithm
outTp := matchcontinue(inTp)
case (OTHER()) then REAL();
case (inTp) then inTp;
end matchcontinue;
end checkIfOther;

public function makeSum "function: makeSum
Takes a list of expressions an makes a sum expression adding all
Expand Down Expand Up @@ -4704,8 +4718,18 @@ algorithm
s_1 = stringAppend(s, "> ");
then
s_1;
case (SUB(ty = t)) then " - ";
case (MUL(ty = t)) then " * ";
case (SUB(ty = t)) equation
ts = typeString(t);
s = stringAppend(" -<", ts);
s_1 = stringAppend(s, "> ");
then
s_1;
case (MUL(ty = t)) equation
ts = typeString(t);
s = stringAppend(" *<", ts);
s_1 = stringAppend(s, "> ");
then
s_1;
case (DIV(ty = t))
equation
ts = typeString(t);
Expand Down

0 comments on commit cb5f12f

Please sign in to comment.