Skip to content

Commit

Permalink
- try to solve single assigment statements
Browse files Browse the repository at this point in the history
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@14534 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Jens Frenkel committed Dec 24, 2012
1 parent 0afa2d8 commit 35f5380
Showing 1 changed file with 65 additions and 1 deletion.
66 changes: 65 additions & 1 deletion Compiler/BackEnd/SimCodeUtil.mo
Original file line number Diff line number Diff line change
Expand Up @@ -2384,7 +2384,20 @@ algorithm
DAE.ALGORITHM_STMTS(algStatements) = BackendDAEUtil.collateAlgorithm(alg, NONE());
then
({SimCode.SES_ALGORITHM(iuniqueEqIndex,algStatements)},iuniqueEqIndex+1,itempvars);


// inverse Algorithm for single variable.
case (_, _, BackendDAE.EQSYSTEM(orderedVars = vars, orderedEqs = eqns),_, _, false, _,_,_)
equation
BackendDAE.ALGORITHM(alg=alg,source=source) = BackendDAEUtil.equationNth(eqns, eqNum-1);
varOutput::{} = CheckModel.algorithmOutputs(alg);
v = BackendVariable.getVarAt(vars,varNum);
// We need to solve an inverse problem of an algorithm section.
false = ComponentReference.crefEqualNoStringCompare(BackendVariable.varCref(v),varOutput);
DAE.ALGORITHM_STMTS(algStatements) = BackendDAEUtil.collateAlgorithm(alg, NONE());
algStatements = solveAlgorithmInverse(algStatements,{v});
then
({SimCode.SES_ALGORITHM(iuniqueEqIndex,algStatements)},iuniqueEqIndex+1,itempvars);

// inverse Algorithm for single variable.
case (_, _, BackendDAE.EQSYSTEM(orderedVars = vars, orderedEqs = eqns),_, _, false, _,_,_)
equation
Expand Down Expand Up @@ -2415,6 +2428,18 @@ algorithm

// inverse Algorithm for single variable.
case (_, _, BackendDAE.EQSYSTEM(orderedVars = vars, orderedEqs = eqns),_, _, true, _,_,_)
equation
BackendDAE.ALGORITHM(alg=alg,source=source) = BackendDAEUtil.equationNth(eqns, eqNum-1);
varOutput::{} = CheckModel.algorithmOutputs(alg);
v = BackendVariable.getVarAt(vars,varNum);
// We need to solve an inverse problem of an algorithm section.
false = ComponentReference.crefEqualNoStringCompare(BackendVariable.varCref(v),varOutput);
DAE.ALGORITHM_STMTS(algStatements) = BackendDAEUtil.collateAlgorithm(alg, NONE());
algStatements = solveAlgorithmInverse(algStatements,{v});
then
({SimCode.SES_ALGORITHM(iuniqueEqIndex,algStatements)},iuniqueEqIndex+1,itempvars);
// inverse Algorithm for single variable.
case (_, _, BackendDAE.EQSYSTEM(orderedVars = vars, orderedEqs = eqns),_, _, true, _,_,_)
equation
BackendDAE.ALGORITHM(alg=alg,source=source) = BackendDAEUtil.equationNth(eqns, eqNum-1);
varOutput::{} = CheckModel.algorithmOutputs(alg);
Expand All @@ -2429,6 +2454,34 @@ algorithm
end matchcontinue;
end createEquation;

protected function solveAlgorithmInverse
input list<DAE.Statement> inStmts;
input list<BackendDAE.Var> solveFor;
output list<DAE.Statement> outStmts;
algorithm
outStmts := matchcontinue (inStmts, solveFor)
local
DAE.ComponentRef cr;
DAE.Exp e1,e2,varexp,exp;
DAE.ElementSource source;
list<DAE.Statement> asserts;
BackendDAE.Var v;
DAE.Type tp;

// Algorithm for single variable.
case (DAE.STMT_ASSIGN(exp1=e1,exp=e2,source=source)::{}, ( v as BackendDAE.VAR(varName=cr))::{})
equation
varexp = Expression.crefExp(cr);
varexp = Debug.bcallret1(BackendVariable.isStateVar(v), Expression.expDer, varexp, varexp);
(exp,asserts) = ExpressionSolve.solve(e1, e2, varexp);
cr = Debug.bcallret1(BackendVariable.isStateVar(v), ComponentReference.crefPrefixDer, cr, cr);
source = DAEUtil.addSymbolicTransformationSolve(true, source, cr, e1, e2, exp, asserts);
tp = Expression.typeof(varexp);
then
{DAE.STMT_ASSIGN(tp,varexp,exp,source)};
end matchcontinue;
end solveAlgorithmInverse;

protected function createSampleEquations
" function: createSampleEquations
author: wbraun
Expand Down Expand Up @@ -6707,6 +6760,17 @@ algorithm
then
({SimCode.SES_ALGORITHM(iuniqueEqIndex,algStatements)},iuniqueEqIndex+1);

// inverse Algorithm for single variable.
case (BackendDAE.ALGORITHM(alg=alg)::_,_,false,_)
equation
solvedVars = List.map(vars,BackendVariable.varCref);
algOutVars = CheckModel.algorithmOutputs(alg);
// We need to solve an inverse problem of an algorithm section.
DAE.ALGORITHM_STMTS(algStatements) = BackendDAEUtil.collateAlgorithm(alg, NONE());
algStatements = solveAlgorithmInverse(algStatements,vars);
then
({SimCode.SES_ALGORITHM(iuniqueEqIndex,algStatements)},iuniqueEqIndex+1);

// Error message, inverse algorithms not supported yet
case (BackendDAE.ALGORITHM(alg=alg,source=source)::_,_,_,_)
equation
Expand Down

0 comments on commit 35f5380

Please sign in to comment.