Skip to content

Commit

Permalink
- added support of analytical jacobians for non-linear solver(hybrid…
Browse files Browse the repository at this point in the history
…-method).

   - for now they can be activated by debug flag "+d=NLSanalyticJacobian".
 - added some new tests for non-linear solvers-


git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@14434 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Willi Braun committed Dec 18, 2012
1 parent 71032ce commit 6d103d0
Show file tree
Hide file tree
Showing 14 changed files with 840 additions and 380 deletions.
40 changes: 23 additions & 17 deletions Compiler/BackEnd/BackendDAEOptimize.mo
Expand Up @@ -4513,12 +4513,14 @@ algorithm
adjSizeT = arrayLength(adjMatrixT);

// Debug dumping
/*
Debug.fcall(Flags.JAC_DUMP2, BackendDump.dumpFullMatching, bdaeMatching);
Debug.fcall(Flags.JAC_DUMP2,BackendDump.printVarList,BackendVariable.varList(varswithDiffs));
Debug.fcall(Flags.JAC_DUMP2,BackendDump.printEquationList,BackendEquation.equationList(orderedEqns));
Debug.fcall(Flags.JAC_DUMP2,BackendDump.dumpIncidenceMatrix,adjMatrix);
Debug.fcall(Flags.JAC_DUMP2,BackendDump.dumpIncidenceMatrixT,adjMatrixT);
Debug.fcall(Flags.JAC_DUMP2,BackendDump.dumpComponents, comps);
*/

// get indexes of diffed vars (rows)
diffedVars = BackendVariable.listVar1(indiffedVars);
Expand Down Expand Up @@ -4568,7 +4570,7 @@ algorithm
sparsetuple = List.threadTuple(diffCompRefs, translated);


Debug.fcall(Flags.JAC_DUMP,print,"analytical Jacobians[SPARSE] -> build sparse graph.");
Debug.fcall(Flags.JAC_DUMP,print,"analytical Jacobians[SPARSE] -> build sparse graph.\n");
// build up a graph of pattern
nodesList = List.intRange2(1,adjSize);
sparseGraph = Graph.buildGraph(nodesList,createBipartiteGraph,sparseArray);
Expand All @@ -4578,7 +4580,7 @@ algorithm
Debug.fcall(Flags.JAC_DUMP2,print,"transposed sparse graph: \n");
Debug.fcall(Flags.JAC_DUMP2,Graph.printGraphInt,sparseGraphT);

Debug.fcall(Flags.JAC_DUMP,print,"analytical Jacobians[SPARSE] -> builded graph for coloring.");
Debug.fcall(Flags.JAC_DUMP,print,"analytical Jacobians[SPARSE] -> builded graph for coloring.\n");
// color sparse bipartite graph
forbiddenColor = arrayCreate(adjSize,NONE());
colored = arrayCreate(adjSize,0);
Expand Down Expand Up @@ -5047,7 +5049,7 @@ algorithm
end matchcontinue;
end generateImplicitInitialEquations;

protected function convertInitialResidualsIntoInitialEquations "function convertInitialResidualsIntoInitialEquations
public function convertInitialResidualsIntoInitialEquations "function convertInitialResidualsIntoInitialEquations
author: lochel
This function converts initial residuals into initial equations of the following form:
e.g.: 0 = a+b -> $res1 = a+b"
Expand Down Expand Up @@ -5222,7 +5224,6 @@ algorithm
BackendVariable.listVar1(parameters), //
BackendVariable.listVar1(outputs), //
orderedVarList, //
(orderedVarCrefList, knownVarCrefList), //
"G"); // name
then (jacobian, sparsityPattern, DAE);

Expand Down Expand Up @@ -5485,7 +5486,7 @@ algorithm
paramvars = List.select(knvarlst, BackendVariable.isParam);

Debug.fcall(Flags.JAC_DUMP, print, "analytical Jacobians -> prepared vars for symbolic matrix A time: " +& realString(clock()) +& "\n");
(outJacobian, outSparsePattern, outSparseColoring) = createJacobian(backendDAE2,states,BackendVariable.listVar1(states),BackendVariable.listVar1(inputvars),BackendVariable.listVar1(paramvars),BackendVariable.listVar1(states),varlst,(comref_vars,comref_knvars),"A");
(outJacobian, outSparsePattern, outSparseColoring) = createJacobian(backendDAE2,states,BackendVariable.listVar1(states),BackendVariable.listVar1(inputvars),BackendVariable.listVar1(paramvars),BackendVariable.listVar1(states),varlst,"A");

then
(outJacobian, outSparsePattern, outSparseColoring);
Expand Down Expand Up @@ -5548,25 +5549,25 @@ algorithm
outputvarsarr = BackendVariable.listVar1(outputvars);

// Differentiate the System w.r.t states for matrices A
(linearModelMatrix, sparsePattern, sparseColoring) = createJacobian(backendDAE2,states,statesarr,inputvarsarr,paramvarsarr,statesarr,varlst,(comref_vars,comref_knvars),"A");
(linearModelMatrix, sparsePattern, sparseColoring) = createJacobian(backendDAE2,states,statesarr,inputvarsarr,paramvarsarr,statesarr,varlst,"A");
linearModelMatrices = {(SOME(linearModelMatrix),sparsePattern,sparseColoring)};
Debug.fcall(Flags.JAC_DUMP, print, "analytical Jacobians -> generated system for matrix A time: " +& realString(clock()) +& "\n");


// Differentiate the System w.r.t inputs for matrices B
(linearModelMatrix, sparsePattern, sparseColoring) = createJacobian(backendDAE2,inputvars2,statesarr,inputvarsarr,paramvarsarr,statesarr,varlst,(comref_vars,comref_knvars),"B");
(linearModelMatrix, sparsePattern, sparseColoring) = createJacobian(backendDAE2,inputvars2,statesarr,inputvarsarr,paramvarsarr,statesarr,varlst,"B");
linearModelMatrices = listAppend(linearModelMatrices,{(SOME(linearModelMatrix),sparsePattern,sparseColoring)});
Debug.fcall(Flags.JAC_DUMP, print, "analytical Jacobians -> generated system for matrix B time: " +& realString(clock()) +& "\n");


// Differentiate the System w.r.t states for matrices C
(linearModelMatrix, sparsePattern, sparseColoring) = createJacobian(backendDAE2,states,statesarr,inputvarsarr,paramvarsarr,outputvarsarr,varlst,(comref_vars,comref_knvars),"C");
(linearModelMatrix, sparsePattern, sparseColoring) = createJacobian(backendDAE2,states,statesarr,inputvarsarr,paramvarsarr,outputvarsarr,varlst,"C");
linearModelMatrices = listAppend(linearModelMatrices,{(SOME(linearModelMatrix),sparsePattern,sparseColoring)});
Debug.fcall(Flags.JAC_DUMP, print, "analytical Jacobians -> generated system for matrix C time: " +& realString(clock()) +& "\n");


// Differentiate the System w.r.t inputs for matrices D
(linearModelMatrix, sparsePattern, sparseColoring) = createJacobian(backendDAE2,inputvars2,statesarr,inputvarsarr,paramvarsarr,outputvarsarr,varlst,(comref_vars,comref_knvars),"D");
(linearModelMatrix, sparsePattern, sparseColoring) = createJacobian(backendDAE2,inputvars2,statesarr,inputvarsarr,paramvarsarr,outputvarsarr,varlst,"D");
linearModelMatrices = listAppend(linearModelMatrices,{(SOME(linearModelMatrix),sparsePattern,sparseColoring)});
Debug.fcall(Flags.JAC_DUMP, print, "analytical Jacobians -> generated system for matrix D time: " +& realString(clock()) +& "\n");

Expand All @@ -5590,18 +5591,17 @@ public function createJacobian "function createJacobian
input BackendDAE.Variables inParameterVars;
input BackendDAE.Variables inDifferentiatedVars;
input list<BackendDAE.Var> inVars;
input tuple<list<DAE.ComponentRef>,list<DAE.ComponentRef>> inOrigTuple;
input String inName;
output BackendDAE.SymbolicJacobian outJacobian;
output BackendDAE.SparsePattern outSparsePattern;
output BackendDAE.SparseColoring outSparseColoring;
algorithm
(outJacobian, outSparsePattern, outSparseColoring) :=
matchcontinue (inBackendDAE,inDiffVars,inStateVars,inInputVars,inParameterVars,inDifferentiatedVars,inVars,inOrigTuple,inName)
matchcontinue (inBackendDAE,inDiffVars,inStateVars,inInputVars,inParameterVars,inDifferentiatedVars,inVars,inName)
local
BackendDAE.BackendDAE backendDAE;

list<DAE.ComponentRef> inOrigVars, inOrigKnVars, comref_vars, comref_seedVars, comref_differentiatedVars;
list<DAE.ComponentRef> comref_vars, comref_seedVars, comref_differentiatedVars;

BackendDAE.Shared shared;
BackendDAE.Variables knvars, knvars1;
Expand All @@ -5611,7 +5611,7 @@ algorithm
BackendDAE.SparsePattern sparsepattern;
BackendDAE.SparseColoring colsColors;

case (_,_,_,_,_,_,_,(inOrigVars,inOrigKnVars),_)
case (_,_,_,_,_,_,_,_)
equation

diffedVars = BackendVariable.varList(inDifferentiatedVars);
Expand Down Expand Up @@ -5704,7 +5704,7 @@ algorithm
"removeSimpleEquations",
"collapseIndependentBlocks"}));
_ = Flags.set(Flags.EXEC_STAT, b);
Debug.fcall(Flags.JAC_DUMP2, BackendDump.bltdump, ("jacdump2",backendDAE2));
//Debug.fcall(Flags.JAC_DUMP2, BackendDump.bltdump, ("jacdump2",backendDAE2));
then backendDAE2;
else
equation
Expand Down Expand Up @@ -6231,7 +6231,7 @@ algorithm
case(cref, x, (matrixName,false))
equation
id = ComponentReference.printComponentRefStr(cref) +& BackendDAE.partialDerivativeNamePrefix +& matrixName +& "$P" +& ComponentReference.printComponentRefStr(x);
id = Util.stringReplaceChar(id, ",", "$K");
id = Util.stringReplaceChar(id, ",", "$c");
id = Util.stringReplaceChar(id, ".", "$P");
id = Util.stringReplaceChar(id, "[", "$lB");
id = Util.stringReplaceChar(id, "]", "$rB");
Expand Down Expand Up @@ -7868,8 +7868,14 @@ public function tearingSystemNew "function tearingSystem
input BackendDAE.BackendDAE inDAE;
output BackendDAE.BackendDAE outDAE;
algorithm
false := Flags.getConfigBool(Flags.NO_TEARING);
(outDAE,_) := BackendDAEUtil.mapEqSystemAndFold(inDAE,tearingSystemNew0,false);
outDAE := matchcontinue(inDAE)
case(_)
equation
false = Flags.getConfigBool(Flags.NO_TEARING);
(outDAE,_) = BackendDAEUtil.mapEqSystemAndFold(inDAE,tearingSystemNew0,false);
then outDAE;
case(_) then inDAE;
end matchcontinue;
end tearingSystemNew;

protected function tearingSystemNew0 "function tearingSystem0
Expand Down
15 changes: 15 additions & 0 deletions Compiler/BackEnd/BackendEquation.mo
Expand Up @@ -1733,10 +1733,25 @@ algorithm
list<list<DAE.Subscript>> subslst;
String errorMessage;
Boolean diffed;
Real r;

case (BackendDAE.EQUATION(exp=DAE.TUPLE(explst), scalar=e2, source=source)) equation
((_, eqns)) = List.fold2(explst, equationTupleToScalarResidualForm, e2, source, (1, {}));
then eqns;

// workaround, should changed to DAE.RCONST(0.0)
// when new rml is availiable
case (BackendDAE.EQUATION(exp=e1 as DAE.RCONST(r), scalar=e2, source=source, differentiated=diffed)) equation
true = realEq(r, 0.0);
eqns ={BackendDAE.RESIDUAL_EQUATION(e2, source, diffed)};
then eqns;

// workaround, should changed to DAE.RCONST(0.0)
// when new rml is availiable
case (BackendDAE.EQUATION(exp=e1, scalar=e2 as DAE.RCONST(r), source=source, differentiated=diffed)) equation
true = realEq(r, 0.0);
eqns ={BackendDAE.RESIDUAL_EQUATION(e1, source, diffed)};
then eqns;

case (BackendDAE.EQUATION(exp=e1, scalar=e2, source=source, differentiated=diffed)) equation
exp = Expression.expSub(e1, e2);
Expand Down
3 changes: 1 addition & 2 deletions Compiler/BackEnd/BackendVariable.mo
Expand Up @@ -2606,7 +2606,7 @@ public function deleteCrefs "function deleteCrefs
input BackendDAE.Variables vars;
output BackendDAE.Variables vars_1;
algorithm
vars_1 := List.fold(varlst, deleteVar, vars);
vars_1 := List.fold(varlst, removeCref, vars);
end deleteCrefs;

public function deleteVars "function deleteVars
Expand Down Expand Up @@ -3189,7 +3189,6 @@ algorithm
end matchcontinue;
end expandVars;


public function getVarAt
"function: getVarAt
author: PA
Expand Down
3 changes: 2 additions & 1 deletion Compiler/BackEnd/SimCode.mo
Expand Up @@ -64,7 +64,7 @@ type ExtAlias = tuple<DAE.ComponentRef, DAE.ComponentRef>;
type HelpVarInfo = tuple<Integer, DAE.Exp, Integer>; // helpvarindex, expression, whenclause index
type SampleCondition = tuple<DAE.Exp,Integer>; // helpvarindex, expression,
type JacobianColumn = tuple<list<SimEqSystem>, list<SimVar>, String>; // column equations, column vars, column length
type JacobianMatrix = tuple<list<JacobianColumn>, // column
type JacobianMatrix = tuple<list<JacobianColumn>, // column
list<SimVar>, // seed vars
String, // matrix name
tuple<list<tuple<DAE.ComponentRef,list<DAE.ComponentRef>>>,tuple<list<SimVar>,list<SimVar>>>, // sparse pattern
Expand Down Expand Up @@ -421,6 +421,7 @@ uniontype SimEqSystem
list<SimEqSystem> eqs;
list<DAE.ComponentRef> crefs;
Integer indexNonLinear;
Option<JacobianMatrix> jacobianMatrix;
end SES_NONLINEAR;
record SES_MIXED
Integer index;
Expand Down

0 comments on commit 6d103d0

Please sign in to comment.