Skip to content

Commit

Permalink
- Merging
Browse files Browse the repository at this point in the history
  • Loading branch information
sjoelund committed Oct 22, 2010
2 parents f94870a + 5c6fce8 commit f42a324
Show file tree
Hide file tree
Showing 8 changed files with 596 additions and 207 deletions.
85 changes: 47 additions & 38 deletions Compiler/DAELow.mo
Expand Up @@ -6131,17 +6131,17 @@ algorithm
then
EQUATION(e1,e2,source);

case (DAE.DEFINE(componentRef = cr1, exp = e2, source = source))
case (DAE.DEFINE(componentRef = cr1, exp = e1, source = source))
equation
e1 = Exp.simplify(DAE.CREF(cr1, DAE.ET_OTHER()));
e2 = Exp.simplify(e2);
e2 = Exp.simplify(e1);
then
EQUATION(e1,e2,source);

case (DAE.INITIALDEFINE(componentRef = cr1, exp = e2, source = source))
case (DAE.INITIALDEFINE(componentRef = cr1, exp = e1, source = source))
equation
e1 = Exp.simplify(DAE.CREF(cr1, DAE.ET_OTHER()));
e2 = Exp.simplify(e2);
e2 = Exp.simplify(e1);
then
EQUATION(e1,e2,source);
end matchcontinue;
Expand Down Expand Up @@ -17675,9 +17675,12 @@ public function generateSymbolicJacobian
input DAELow inDAELow;
input DAE.FunctionTree functions;
input list<DAE.ComponentRef> inVars;
input list<Var> stateVars;
input list<Var> inputVars;
input list<Var> paramVars;
output DAELow outJacobian;
algorithm
outJacobian := matchcontinue(inDAELow, functions, inVars)
outJacobian := matchcontinue(inDAELow, functions, inVars, stateVars, inputVars, paramVars)
local
DAELow daeLow;
DAE.DAElist daeList;
Expand All @@ -17703,7 +17706,7 @@ algorithm
list<DAE.Algorithm> derivedAlgorithms;
list<tuple<Integer, DAE.ComponentRef>> derivedAlgorithmsLookUp;

case(_, _, {}) equation
case(_, _, {}, _, _,_) equation
jacOrderedVars = emptyVars();
jacKnownVars = emptyVars();
jacExternalObjects = emptyVars();
Expand All @@ -17719,15 +17722,12 @@ algorithm
jacobian = DAELOW(jacOrderedVars, jacKnownVars, jacExternalObjects, jacAliasVars, jacOrderedEqs, jacRemovedEqs, jacInitialEqs, jacArrayEqs, jacAlgorithms, jacEventInfo, jacExtObjClasses);
then jacobian;

case(daeLow as DAELOW(orderedVars=orderedVars, knownVars=knownVars, externalObjects=externalObjects, aliasVars=aliasVars, orderedEqs=orderedEqs, removedEqs=removedEqs, initialEqs=initialEqs, arrayEqs=arrayEqs, algorithms=algorithms, eventInfo=eventInfo, extObjClasses=extObjClasses), functions, vars) equation
case(daeLow as DAELOW(orderedVars=orderedVars, knownVars=knownVars, externalObjects=externalObjects, aliasVars=aliasVars, orderedEqs=orderedEqs, removedEqs=removedEqs, initialEqs=initialEqs, arrayEqs=arrayEqs, algorithms=algorithms, eventInfo=eventInfo, extObjClasses=extObjClasses), functions, vars, stateVars, inputVars, paramVars) equation
Debug.fcall("jacdump", print, "\n+++++++++++++++++++++ daeLow-dump: input +++++++++++++++++++++\n");
Debug.fcall("jacdump", dump, daeLow);
Debug.fcall("jacdump", print, "##################### daeLow-dump: input #####################\n\n");

allVars = listAppend(varList(orderedVars), varList(knownVars));
inputVars = Util.listSelect(allVars, isVarOnTopLevelAndInput);
paramVars = Util.listSelect(allVars, isParam);
stateVars = Util.listSelect(allVars, isStateVar);
allVars = listAppend(listAppend(stateVars, inputVars), paramVars);

derivedVariables = generateJacobianVars(varList(orderedVars), vars, stateVars);
(derivedAlgorithms, derivedAlgorithmsLookUp) = deriveAllAlg(arrayList(algorithms), vars, functions, 0);
Expand All @@ -17752,7 +17752,7 @@ algorithm
Debug.fcall("jacdump", print, "##################### daeLow-dump: jacobian #####################\n");
then jacobian;

case(_, _, _) equation
case(_, _, _, _, _,_) equation
Error.addMessage(Error.INTERNAL_ERROR, {"DAELow.generateSymbolicJacobian failed"});
then fail();
end matchcontinue;
Expand Down Expand Up @@ -18182,15 +18182,15 @@ algorithm
e1_ = differentiateWithRespectToX(e1, x, functions, inputVars, paramVars, stateVars);
e2_ = differentiateWithRespectToX(e2, x, functions, inputVars, paramVars, stateVars);
e = DAE.BINARY(DAE.BINARY(e1_, DAE.MUL(et), e2), DAE.ADD(et), DAE.BINARY(e1, DAE.MUL(et), e2_));
//e = Exp.simplify(e);
e = Exp.simplify(e);
then e;

// a / b
case(DAE.BINARY(exp1=e1, operator=DAE.DIV(ty=et), exp2=e2), x, functions, inputVars, paramVars, stateVars) equation
e1_ = differentiateWithRespectToX(e1, x, functions, inputVars, paramVars, stateVars);
e2_ = differentiateWithRespectToX(e2, x, functions, inputVars, paramVars, stateVars);
e = DAE.BINARY(DAE.BINARY(DAE.BINARY(e1_, DAE.MUL(et), e2), DAE.SUB(et), DAE.BINARY(e1, DAE.MUL(et), e2_)), DAE.DIV(et), DAE.BINARY(e2, DAE.MUL(et), e2));
//e = Exp.simplify(e);
e = Exp.simplify(e);
then e;

// a(x)^b
Expand Down Expand Up @@ -18283,7 +18283,7 @@ algorithm
equation
nArgs = listLength(expList1);
(DAE.FUNCTION_DER_MAPPER(derivativeFunction=derFname,conditionRefs=conditions), tp) = Derive.getFunctionMapper(fname, functions);
expList2 = deriveExpListwrtstate(expList1, nArgs, conditions, x, functions);
expList2 = deriveExpListwrtstate(expList1, nArgs, conditions, x, functions, inputVars, paramVars, stateVars);
e1 = partialAnalyticalDifferentiation(expList1, expList2, e, derFname, listLength(expList2));
then e1;

Expand All @@ -18294,7 +18294,7 @@ algorithm
Integer nArgs;
equation
nArgs = listLength(expList1);
expList2 = deriveExpListwrtstate2(expList1, nArgs, x, functions);
expList2 = deriveExpListwrtstate2(expList1, nArgs, x, functions, inputVars, paramVars, stateVars);
e1 = partialNumericalDifferentiation(expList1, expList2, x, e);
then e1;

Expand All @@ -18314,26 +18314,30 @@ protected function deriveExpListwrtstate
input list<tuple<Integer,DAE.derivativeCond>> inConditios;
input DAE.ComponentRef inState;
input DAE.FunctionTree inFunctions;
input list<Var> inInputVars;
input list<Var> inParamVars;
input list<Var> inStateVars;
output list<DAE.Exp> outExpList;
algorithm
outExpList := matchcontinue(inExpList, inLengthExpList, inConditios, inState, inFunctions)
outExpList := matchcontinue(inExpList, inLengthExpList, inConditios, inState, inFunctions, inInputVars, inParamVars, inStateVars)
local
DAE.ComponentRef x;
DAE.Exp curr,r1;
list<DAE.Exp> rest, r2;
DAE.FunctionTree functions;
Integer LengthExpList,n, argnum;
list<tuple<Integer,DAE.derivativeCond>> conditions;
case ({},_, _,_,_) then ({});
case (curr::rest, LengthExpList, conditions, x, functions) equation
list<Var> inputVars, paramVars, stateVars;
case ({},_,_,_,_,_,_,_) then ({});
case (curr::rest, LengthExpList, conditions, x, functions,inputVars, paramVars, stateVars) equation
n = listLength(rest);
argnum = LengthExpList - n;
true = checkcondition(conditions,argnum);
r1 = differentiateWithRespectToX(curr, x, functions, {}, {}, {});
r2 = deriveExpListwrtstate(rest,LengthExpList,conditions, x, functions);
r1 = differentiateWithRespectToX(curr, x, functions, inputVars, paramVars, stateVars);
r2 = deriveExpListwrtstate(rest,LengthExpList,conditions, x, functions,inputVars, paramVars, stateVars);
then (r1::r2);
case (curr::rest, LengthExpList, conditions, x, functions) equation
r2 = deriveExpListwrtstate(rest,LengthExpList,conditions, x, functions);
case (curr::rest, LengthExpList, conditions, x, functions,inputVars, paramVars, stateVars) equation
r2 = deriveExpListwrtstate(rest,LengthExpList,conditions, x, functions,inputVars, paramVars, stateVars);
then r2;
end matchcontinue;
end deriveExpListwrtstate;
Expand All @@ -18343,21 +18347,25 @@ protected function deriveExpListwrtstate2
input Integer inLengthExpList;
input DAE.ComponentRef inState;
input DAE.FunctionTree inFunctions;
input list<Var> inInputVars;
input list<Var> inParamVars;
input list<Var> inStateVars;
output list<DAE.Exp> outExpList;
algorithm
outExpList := matchcontinue(inExpList, inLengthExpList, inState, inFunctions)
outExpList := matchcontinue(inExpList, inLengthExpList, inState, inFunctions, inInputVars, inParamVars, inStateVars)
local
DAE.ComponentRef x;
DAE.Exp curr,r1;
list<DAE.Exp> rest, r2;
DAE.FunctionTree functions;
Integer LengthExpList,n, argnum;
case ({}, _, _, _) then ({});
case (curr::rest, LengthExpList, x, functions) equation
list<Var> inputVars, paramVars, stateVars;
case ({}, _, _, _, _, _, _) then ({});
case (curr::rest, LengthExpList, x, functions, inputVars, paramVars, stateVars) equation
n = listLength(rest);
argnum = LengthExpList - n;
r1 = differentiateWithRespectToX(curr, x, functions, {}, {}, {});
r2 = deriveExpListwrtstate2(rest,LengthExpList, x, functions);
r1 = differentiateWithRespectToX(curr, x, functions, inputVars, paramVars, stateVars);
r2 = deriveExpListwrtstate2(rest,LengthExpList, x, functions, inputVars, paramVars, stateVars);
then (r1::r2);
end matchcontinue;
end deriveExpListwrtstate2;
Expand All @@ -18379,11 +18387,13 @@ algorithm
equality(i = nArgs);
cond = DAE.ZERO_DERIVATIVE();
then false;
/* case((i,cond)::rest,nArgs)
case((i,cond)::rest,nArgs)
local
DAE.Exp e1;
equation
equality(i = nArgs);
cond = DAE.NO_DERIVATIVE();
then false;*/
DAE.NO_DERIVATIVE(_) = cond;
then false;
case((i,cond)::rest,nArgs)
equation
res = checkcondition(rest,nArgs);
Expand Down Expand Up @@ -18636,7 +18646,7 @@ algorithm

case ({}, states, _, _) then {};
case (curr::rest, states, actInd, allVars) equation
(str, actInd) = determineIndices2(states, curr, actInd, allVars);
(str, actInd) = determineIndices2(curr, states, actInd, allVars);
erg = determineIndices(rest, states, actInd, allVars);
str = listAppend(str, erg);
then str;
Expand All @@ -18645,28 +18655,27 @@ end determineIndices;

protected function determineIndices2
// function: determineIndices2
input list<DAE.ComponentRef> inStates;
input DAE.ComponentRef inDStates;
//input Boolean inSearchForStates;
input list<DAE.ComponentRef> inStates;
input Integer actInd;
input list<Var> inAllVars;
output list<tuple<String,Integer>> outTuple;
output Integer outActInd;
algorithm
(outTuple,outActInd) := matchcontinue(inStates, inDStates, actInd, inAllVars)
(outTuple,outActInd) := matchcontinue(inDStates, inStates, actInd, inAllVars)
local
tuple<String,Integer> str;
list<tuple<String,Integer>> erg;
list<DAE.ComponentRef> rest;
DAE.ComponentRef new, curr, dState;
list<Var> allVars;
//String debug1;Integer debug2;
case ({}, dState, actInd, allVars) then ({}, actInd);
case (curr::rest, dState, actInd, allVars) equation
case (dState, {}, actInd, allVars) then ({}, actInd);
case (dState,curr::rest, actInd, allVars) equation
new = differentiateVarWithRespectToX(dState,curr,allVars);
str = (Exp.printComponentRefStr(new) ,actInd);
actInd = actInd+1;
(erg, actInd) = determineIndices2(rest, dState, actInd, allVars);
(erg, actInd) = determineIndices2(dState, rest, actInd, allVars);
then (str::erg, actInd);
case (_,_, _, _) equation
Error.addMessage(Error.INTERNAL_ERROR, {"DAELow.determineIndices2() failed"});
Expand Down
7 changes: 7 additions & 0 deletions Compiler/Derive.mo
Expand Up @@ -1250,6 +1250,13 @@ algorithm
then
DAE.UNARY(op,e_1);

// abs(x)
case (DAE.CALL(path=fname, expLst={exp},tuple_ = b,builtin = c,ty=tp,inlineType=inl),tv,differentiateIfExp) equation
Builtin.isAbs(fname);
true = Exp.expContains(exp, DAE.CREF(tv,DAE.ET_REAL()));
exp_1 = differentiateExp(exp, tv,differentiateIfExp);
then DAE.IFEXP(DAE.RELATION(exp_1,DAE.GREATER(DAE.ET_REAL()),DAE.RCONST(0.0)), exp_1, DAE.UNARY(DAE.UMINUS(DAE.ET_REAL()),exp_1));

/* der(tanh(x)) = der(x) / cosh(x) */
case (DAE.CALL(path = fname,expLst = (exp :: {}),tuple_ = b,builtin = c,ty=tp,inlineType=inl),tv,differentiateIfExp)
local DAE.ExpType tp;
Expand Down
103 changes: 101 additions & 2 deletions Compiler/Exp.mo
Expand Up @@ -1047,6 +1047,13 @@ algorithm
res = boolAnd(b1,b2);
then
res;
case (DAE.IFEXP(_,e1,e2))
equation
b1 = isConst(e1);
b2 = isConst(e2);
res = boolAnd(b1,b2);
then
res;
case (DAE.ARRAY(array = ae))
equation
ab = Util.listMap(ae,isConst);
Expand Down Expand Up @@ -10370,13 +10377,105 @@ algorithm
case (e1,e2,e3)
equation
Debug.fprint("failtrace", "-Exp.solve failed\n");
/*print("solve ");print(printExpStr(e1));print(" = ");print(printExpStr(e2));
print(" w.r.t ");print(printExpStr(e3));print(" failed\n");*/
//print("solve ");print(printExpStr(e1));print(" = ");print(printExpStr(e2));
//print(" w.r.t ");print(printExpStr(e3));print(" failed\n");
then
fail();
end matchcontinue;
end solve;

public function solveLin
"function: solve
Solves an equation consisting of a right hand side (rhs) and a
left hand side (lhs), with respect to the expression given as
third argument, usually a variable."
input Exp inExp1;
input Exp inExp2;
input Exp inExp3;
output Exp outExp;
algorithm
outExp := matchcontinue (inExp1,inExp2,inExp3)
local
Exp crexp,crexp2,rhs,lhs,res,res_1,cr,e1,e2,e3;
ComponentRef cr1,cr2;
/*
case(debuge1,debuge2,debuge3) // FOR DEBBUGING...
local Exp debuge1,debuge2,debuge3;
equation
print("(Exp.mo debugging) To solve: rhs: " +&
printExpStr(debuge1) +& " lhs: " +&
printExpStr(debuge2) +& " with respect to: " +&
printExpStr(debuge3) +& "\n");
then
fail();*/

// special case when already solved, cr1 = rhs, otherwise division by zero when dividing with derivative
case (crexp,rhs,crexp2)
equation
cr1 = crOrDerCr(crexp);
cr2 = crOrDerCr(crexp2);
true = crefEqual(cr1, cr2);
false = expContains(rhs, crexp);
res_1 = simplify1(rhs);
then
res_1;

// special case when already solved, lhs = cr1, otherwise division by zero when dividing with derivative
case (lhs,crexp ,crexp2)
equation
cr1 = crOrDerCr(crexp);
cr2 = crOrDerCr(crexp2);
true = crefEqual(cr1, cr2);
false = expContains(lhs, crexp);
res_1 = simplify1(lhs);
then
res_1;

// solving linear equation system using newton iteration ( converges directly )
case (lhs,rhs,(cr as DAE.CREF(componentRef = _)))
equation
true = hasOnlyFactors(lhs,rhs);
lhs = DAE.BINARY(lhs,DAE.ADD(DAE.ET_REAL()),DAE.RCONST(1.0));
rhs = DAE.BINARY(rhs,DAE.ADD(DAE.ET_REAL()),DAE.RCONST(1.0));
res = solve2(lhs, rhs, cr);
res_1 = simplify1(res);
then
res_1;

// solving linear equation system using newton iteration ( converges directly )
case (lhs,rhs,(cr as DAE.CREF(componentRef = _)))
equation
res = solve2(lhs, rhs, cr);
res_1 = simplify1(res);
then
res_1;

case (lhs,DAE.IFEXP(e1,e2,e3),(cr as DAE.CREF(componentRef = _)))
equation
rhs = solveLin(lhs,e2,cr);
res = solveLin(lhs,e3,cr);
res_1 = simplify1(DAE.IFEXP(e1,rhs,res));
then
res_1;

case (DAE.IFEXP(e1,e2,e3),rhs,(cr as DAE.CREF(componentRef = _)))
equation
lhs = solveLin(rhs,e2,cr);
res = solveLin(rhs,e3,cr);
res_1 = simplify1(DAE.IFEXP(e1,rhs,res));
then
res_1;

case (e1,e2,e3)
equation
Debug.fprint("failtrace", "-Exp.solve failed\n");
//print("solve ");print(printExpStr(e1));print(" = ");print(printExpStr(e2));
//print(" w.r.t ");print(printExpStr(e3));print(" failed\n");
then
fail();
end matchcontinue;
end solveLin;

protected function printExpIfDiff ""
input Exp e1,e2;
output String s;
Expand Down

0 comments on commit f42a324

Please sign in to comment.