Skip to content

Commit

Permalink
some code cleanup
Browse files Browse the repository at this point in the history
sin(acos(e)) = cos(asin(e)) = sqrt(1-e^2)
sin(atan(e)) = e/sqrt(1+e^2)
cos(atan(e)) = 1/sqrt(1+e^2)


git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@24637 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Henning Kiel committed Feb 19, 2015
1 parent 1b352cf commit 9c4fd4c
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 66 deletions.
61 changes: 11 additions & 50 deletions Compiler/BackEnd/EvaluateFunctions.mo
Expand Up @@ -152,9 +152,7 @@ algorithm
ExpressionDump.dumpExp(bindExp);
then
varIn;
else
equation
then varIn;
else varIn;
end matchcontinue;
end evaluateParameter;

Expand Down Expand Up @@ -256,8 +254,6 @@ algorithm
then
(eq,(shared,addEqs,idx+1,changed));
else
equation
then
(eqIn,tplIn);
end matchcontinue;
end evalFunctions_findFuncs;
Expand Down Expand Up @@ -452,8 +448,6 @@ algorithm
then
((exp,outputExp,constEqs,funcs,idx,changed));
else
equation
then
((rhsExpIn,lhsExpIn,{},funcsIn,eqIdx,false));
end matchcontinue;
end evaluateConstantFunction;
Expand Down Expand Up @@ -610,8 +604,6 @@ algorithm
then
expLst;
else
equation
then
{};
end match;
end getCrefsForRecord;
Expand Down Expand Up @@ -639,8 +631,7 @@ algorithm
exp = Expression.crefExp(cref);
then exp;
else
equation
then expIn;
expIn;
end matchcontinue;
end scalarRecExpForOneDimRec;

Expand Down Expand Up @@ -699,8 +690,7 @@ algorithm
funcProts = List.map2(protCrefs,generateProtectedElements,allOutputs,lhsExpIn);
varOutputs = listAppend(funcOutputs,funcProts);
then (varOutputs,outputExp,varScalarCrefsInFunc);
case(_,_,_,_,_,_,DAE.LBINARY(exp1=exp1,exp2=exp2))
equation
case(_,_,_,_,_,_,DAE.LBINARY())
then
({},lhsExpIn,{});
case(_,_,_,_,_,_,DAE.TUPLE(PR=expLst))
Expand All @@ -727,10 +717,6 @@ algorithm
varScalarExps = List.map(varScalarExps,scalarRecExpForOneDimRec);
outputExp = if List.hasOneElement(varScalarExps) then List.first(varScalarExps) else DAE.TUPLE(varScalarExps);
then (varOutputs,outputExp,varScalarCrefsInFunc);
case(_,_,_,_,_,_,DAE.LBINARY(exp1=exp1,exp2=exp2))
equation
then
({},lhsExpIn,{});
case(_,_,_,_,_,_,DAE.TUPLE(PR=expLst))
equation
true = List.isEmpty(List.flatten(scalarOutputs));
Expand All @@ -745,10 +731,6 @@ algorithm
funcProts = List.map2(protCrefs,generateProtectedElements,allOutputs,lhsExpIn);
varOutputs = listAppend(funcOutputs,funcProts);
then (varOutputs,outputExp,varScalarCrefsInFunc);
case(_,_,_,_,_,_,DAE.LBINARY(exp1=exp1,exp2=exp2))
equation
then
({},lhsExpIn,{});
case(_,{},{},_,{},_,_)
equation
// only constant scalarOutputs
Expand Down Expand Up @@ -818,9 +800,7 @@ algorithm
constCrefs = List.map(constExps,Expression.expCref);
then
({},constCrefs);
case(_,_,_,_)
equation
then
else
(constScalarCrefs,constComplCrefs);
end matchcontinue;
end buildConstFunctionCrefs;
Expand All @@ -846,7 +826,6 @@ algorithm
list<DAE.Element> rest;
list<DAE.ComponentRef> constCompl, varCompl, varScalar, constScalar, constScalarCrefs;
case({},_,_,_,_,_)
equation
then(constComplexLstIn,varComplexLstIn,constScalarLstIn,varScalarLstIn);
case(elem::rest,_,_,_,_,_)
equation
Expand Down Expand Up @@ -1209,7 +1188,6 @@ algorithm
then
b1;
case({})
equation
then
false;
case(exp::rest)
Expand Down Expand Up @@ -1274,12 +1252,10 @@ algorithm
then
cref2;
case(cref1 as DAE.CREF_IDENT(),_)
equation
then
cref1;
else
then
crefIn;
cref1;
else
crefIn;
end matchcontinue;
end makeIdentCref2;

Expand Down Expand Up @@ -1327,9 +1303,7 @@ algorithm
then
not b;
else
equation
then
true;
true;
end match;
end statementRHSIsNotConst;

Expand Down Expand Up @@ -1829,7 +1803,6 @@ algorithm
then
(stmts,true);
case(DAE.NOELSE(),FUNCINFO())
equation
then
({},true);
end match;
Expand All @@ -1856,8 +1829,7 @@ algorithm
then
repl;
else
then
replIn;
replIn;
end matchcontinue;
end addTplReplacements;

Expand Down Expand Up @@ -1908,7 +1880,6 @@ algorithm
list<DAE.Statement> stmtLst1,stmtLst2;
list<list<DAE.Statement>> stmtLstLst;
case(DAE.STMT_ASSIGN(exp1=exp),_)
equation
then
exp::expsIn;
case(DAE.STMT_TUPLE_ASSIGN(expExpLst=expLst),_)
Expand Down Expand Up @@ -1970,7 +1941,6 @@ algorithm
end if;
then fail();
case(DAE.STMT_NORETCALL(),_)
equation
then expsIn;
case(DAE.STMT_RETURN(),_)
equation
Expand Down Expand Up @@ -2072,9 +2042,7 @@ algorithm
then
stmtsLst;
else
equation
then
stmtLstsIn;
stmtLstsIn;
end match;
end getDAEelseStatemntLsts;

Expand Down Expand Up @@ -2149,7 +2117,6 @@ algorithm
local
DAE.Type t;
case(DAE.CREF(ty=t))
equation
then
t;
else
Expand Down Expand Up @@ -2289,8 +2256,6 @@ algorithm
then
{};
else
equation
then
{};
end matchcontinue;
end getScalarsForComplexVar;
Expand Down Expand Up @@ -2537,8 +2502,7 @@ algorithm
then
(({stmtNew},addStmts),FUNCINFO(repl,funcTree,idx));
else
equation
then(({stmtIn},{}),infoIn);
(({stmtIn},{}),infoIn);
end matchcontinue;
end predictIfOutput;

Expand Down Expand Up @@ -2569,7 +2533,6 @@ algorithm
list<DAE.Exp> lhsLst,rhsLst,constExps,varExps;
list<DAE.Statement> rest;
case({},_)
equation
then
replIn;
case(DAE.STMT_ASSIGN(exp1=lhs,exp=rhs)::rest,_)
Expand Down Expand Up @@ -2666,11 +2629,9 @@ algorithm
then
DAE.ELSEIF(exp,stmts,els);
case(stmts::_,DAE.ELSE())
equation
then
DAE.ELSE(stmts);
case(_::_,DAE.NOELSE())
equation
then
DAE.NOELSE();
end match;
Expand Down
3 changes: 1 addition & 2 deletions Compiler/FrontEnd/Algorithm.mo
Expand Up @@ -320,10 +320,9 @@ public function makeSimpleAssingment
output DAE.Statement outStmt;
protected
DAE.Exp e1, e2;
DAE.ComponentRef cr;
DAE.Type tp;
algorithm
(e1 as DAE.CREF(cr, tp), e2) := inTpl;
(e1 as DAE.CREF(ty=tp), e2) := inTpl;
outStmt := DAE.STMT_ASSIGN(tp, e1, e2, source);
end makeSimpleAssingment;

Expand Down
33 changes: 22 additions & 11 deletions Compiler/FrontEnd/ExpressionSimplify.mo
Expand Up @@ -403,6 +403,18 @@ algorithm
then e;
case (DAE.CALL(path=Absyn.IDENT("atan"),expLst={DAE.CALL(path=Absyn.IDENT("tan"),expLst={e})}))
then e;
// sin(acos(e)) = sqrt(1-e^2)
case (DAE.CALL(path=Absyn.IDENT("sin"),expLst={DAE.CALL(path=Absyn.IDENT("acos"),expLst={e})}))
then Expression.makePureBuiltinCall("sqrt",{DAE.BINARY(DAE.RCONST(1),DAE.SUB(DAE.T_REAL_DEFAULT),DAE.BINARY(e,DAE.MUL(DAE.T_REAL_DEFAULT),e))},DAE.T_REAL_DEFAULT);
// cos(asin(e)) = sqrt(1-e^2)
case (DAE.CALL(path=Absyn.IDENT("cos"),expLst={DAE.CALL(path=Absyn.IDENT("asin"),expLst={e})}))
then Expression.makePureBuiltinCall("sqrt",{DAE.BINARY(DAE.RCONST(1),DAE.SUB(DAE.T_REAL_DEFAULT),DAE.BINARY(e,DAE.MUL(DAE.T_REAL_DEFAULT),e))},DAE.T_REAL_DEFAULT);
// sin(atan(e)) = e/sqrt(1+e^2)
case (DAE.CALL(path=Absyn.IDENT("sin"),expLst={DAE.CALL(path=Absyn.IDENT("atan"),expLst={e})}))
then DAE.BINARY(e,DAE.DIV(DAE.T_REAL_DEFAULT),Expression.makePureBuiltinCall("sqrt",{DAE.BINARY(DAE.RCONST(1),DAE.ADD(DAE.T_REAL_DEFAULT),DAE.BINARY(e,DAE.MUL(DAE.T_REAL_DEFAULT),e))},DAE.T_REAL_DEFAULT));
// cos(atan(e)) = 1/sqrt(1+e^2)
case (DAE.CALL(path=Absyn.IDENT("cos"),expLst={DAE.CALL(path=Absyn.IDENT("atan"),expLst={e})}))
then DAE.BINARY(DAE.RCONST(1),DAE.DIV(DAE.T_REAL_DEFAULT),Expression.makePureBuiltinCall("sqrt",{DAE.BINARY(DAE.RCONST(1),DAE.ADD(DAE.T_REAL_DEFAULT),DAE.BINARY(e,DAE.MUL(DAE.T_REAL_DEFAULT),e))},DAE.T_REAL_DEFAULT));
// atan2(y,0) = sign(y)*pi/2
case (DAE.CALL(path=Absyn.IDENT("atan2"),expLst={e1,e2}))
equation
Expand Down Expand Up @@ -1739,7 +1751,7 @@ algorithm
then
DAE.UNARY(op,e1);

case (e) then e;
else inExp;

end matchcontinue;
end simplify2;
Expand Down Expand Up @@ -2348,7 +2360,7 @@ algorithm
res;

// return e
case(e) then e;
else inExp;

end matchcontinue;
end simplifyBinarySortConstants;
Expand Down Expand Up @@ -2404,7 +2416,7 @@ algorithm
then
res;

case (e) then e;
else inExp;

end matchcontinue;
end simplifyBinaryCoeff;
Expand Down Expand Up @@ -2798,7 +2810,7 @@ algorithm
then
((e1,2.0));

case (e) then ((e,1.0));
else ((inExp,1.0));

end matchcontinue;
end simplifyBinaryMulCoeff2;
Expand Down Expand Up @@ -3230,13 +3242,12 @@ protected function simplifyAsubOperator
output Operator outOperator;
algorithm
outOperator:=
matchcontinue (inExp1,inOperator2,inOperator3)
local Operator sop,aop;
case (DAE.ARRAY(),_,aop) then aop;
case (DAE.MATRIX(),_,aop) then aop;
case (DAE.RANGE(),_,aop) then aop;
case (_,sop,_) then sop;
end matchcontinue;
match (inExp1)
case (DAE.ARRAY()) then inOperator3;
case (DAE.MATRIX()) then inOperator3;
case (DAE.RANGE()) then inOperator3;
else inOperator2;
end match;
end simplifyAsubOperator;

protected function simplifyAsubSlicing
Expand Down
4 changes: 2 additions & 2 deletions Compiler/FrontEnd/InnerOuter.mo
Expand Up @@ -679,7 +679,7 @@ algorithm
case(_,_,_,_,_,true,_,_,_,_,_,_,_) then inSets;

// if it was not added, add it (first component found: cr1)
case(cache,env,ih,pre, Connect.SETS(sets, sc, cl, oc),false,_,io1,_,_,io2,_,_)
case(cache,env,ih,_, Connect.SETS(sets, sc, cl, oc),false,_,io1,_,_,io2,_,_)
equation
(cache,DAE.ATTR(connectorType = ct,variability=vt1),t1,_,_,_,_,_,_) = Lookup.lookupVar(cache,env,cr1);
pre = Prefix.NOPRE();
Expand All @@ -697,7 +697,7 @@ algorithm
Connect.SETS(sets, sc, cl, oc);

// if it was not added, add it (first component found: cr2)
case(cache,env,ih,pre, Connect.SETS(sets, sc, cl, oc),false,_,io1,_,_,io2,_,_)
case(cache,env,ih,_, Connect.SETS(sets, sc, cl, oc),false,_,io1,_,_,io2,_,_)
equation
pre = Prefix.NOPRE();
(cache,DAE.ATTR(connectorType = ct,variability=vt2),t2,_,_,_,_,_,_) = Lookup.lookupVar(cache,env,cr2);
Expand Down
1 change: 0 additions & 1 deletion Compiler/Template/GenerateAPIFunctionsTpl.tpl
Expand Up @@ -211,7 +211,6 @@ end structToString;

template getQtInterfaceHeader(String name, String prefix, list<DAE.FuncArg> args, DAE.Type res, String className, Boolean addStructs)
::=
let &toStringBuffer = buffer ""
let inTypes = args |> arg as FUNCARG(__) => '<%getQtType(arg.ty)%> <%arg.name%>' ; separator=", "
let outType = match res
case T_TUPLE(__) then
Expand Down
22 changes: 22 additions & 0 deletions SimulationRuntime/c/simulation/solver/linearSolverUmfpack.c
Expand Up @@ -425,6 +425,17 @@ int solveSingularSystem(LINEAR_SYSTEM_DATA* systemData)
else
{
infoStreamPrint(LOG_LS_V, 0, "error: system is not solvable*");
/* free all used memory */
free(Up);
free(Ui);
free(Ux);

free(Q);
free(Rs);

free(b);
free(y);
free(z);
return -1;
}
}
Expand Down Expand Up @@ -466,6 +477,17 @@ int solveSingularSystem(LINEAR_SYSTEM_DATA* systemData)
else
{
infoStreamPrint(LOG_LS_V, 0, "error: system is not solvable");
/* free all used memory */
free(Up);
free(Ui);
free(Ux);

free(Q);
free(Rs);

free(b);
free(y);
free(z);
return -1;
}

Expand Down

0 comments on commit 9c4fd4c

Please sign in to comment.