Skip to content

Commit

Permalink
Fix side-effect in getSymbolicJacobian
Browse files Browse the repository at this point in the history
  • Loading branch information
lochel authored and OpenModelica-Hudson committed Apr 16, 2017
1 parent 2892979 commit fa0a689
Showing 1 changed file with 93 additions and 108 deletions.
201 changes: 93 additions & 108 deletions Compiler/BackEnd/SymbolicJacobian.mo
Expand Up @@ -2619,124 +2619,109 @@ protected function getSymbolicJacobian "author: wbraun
input Boolean inOnlySparsePattern;
output BackendDAE.Jacobian outJacobian;
output BackendDAE.Shared outShared;
protected
BackendDAE.BackendDAE backendDAE;
BackendDAE.EquationArray eqns;
BackendDAE.ExtraInfo einfo;
BackendDAE.Shared shared;
BackendDAE.SparseColoring sparseColoring;
BackendDAE.SparsePattern sparsePattern;
BackendDAE.Variables dependentVars, globalKnownVars;
DAE.FunctionTree funcs;
FCore.Cache cache;
FCore.Graph graph;
list<BackendDAE.Var> knvarLst1, knvarLst2, independentVarsLst, dependentVarsLst, otherVarsLst;
list<DAE.ComponentRef> independentComRefs, dependentVarsComRefs, otherVarsLstComRefs;
Option<BackendDAE.SymbolicJacobian> symJacBDAE;
algorithm
(outJacobian, outShared) := matchcontinue(inDiffVars, inResEquations, inResVars, inotherEquations, inotherVars, inShared, inAllVars, inName)
local
FCore.Cache cache;
FCore.Graph graph;
BackendDAE.BackendDAE backendDAE, jacBackendDAE;

BackendDAE.Variables emptyVars, dependentVars, independentVars, globalKnownVars, allvars;
BackendDAE.EquationArray emptyEqns, eqns;
list<BackendDAE.Var> knvarLst1, knvarLst2, independentVarsLst, dependentVarsLst, otherVarsLst;
list<BackendDAE.Equation> residual_eqnlst;
list<DAE.ComponentRef> independentComRefs, dependentVarsComRefs, otherVarsLstComRefs;

DAE.ComponentRef x;
Option<BackendDAE.SymbolicJacobian> symJacBDAE;
BackendDAE.SparsePattern sparsePattern;
BackendDAE.SparseColoring sparseColoring;

BackendDAE.EqSystem syst;
BackendDAE.Shared shared;
BackendDAE.StrongComponents comps;
BackendDAE.ExtraInfo einfo;

DAE.FunctionTree funcs;

case(_, _, _, _, _, _, _, _)
equation
globalKnownVars = BackendDAEUtil.getGlobalKnownVarsFromShared(inShared);
funcs = BackendDAEUtil.getFunctions(inShared);
einfo = BackendDAEUtil.getExtraInfo(inShared);

if Flags.isSet(Flags.JAC_DUMP2) then
print("---+++ create analytical jacobian +++---");
print("\n---+++ independent variables +++---\n");
BackendDump.printVariables(inDiffVars);
print("\n---+++ equation system +++---\n");
BackendDump.printEquationArray(inResEquations);
end if;

independentVarsLst = BackendVariable.varList(inDiffVars);
independentComRefs = List.map(independentVarsLst, BackendVariable.varCref);

otherVarsLst = BackendVariable.varList(inotherVars);
otherVarsLstComRefs = List.map(otherVarsLst, BackendVariable.varCref);

if Flags.isSet(Flags.JAC_DUMP2) then
print("\n---+++ known variables +++---\n");
BackendDump.printVariables(globalKnownVars);
end if;

// dependentVarsLst = listReverse(dependentVarsLst);
dependentVars = BackendVariable.mergeVariables(inResVars, inotherVars);
eqns = BackendEquation.merge(inResEquations, inotherEquations);

if Flags.isSet(Flags.JAC_DUMP2) then
print("\n---+++ created backend system +++---\n");
print("\n---+++ vars +++---\n");
BackendDump.printVariables(dependentVars);
print("\n---+++ equations +++---\n");
BackendDump.printEquationArray(eqns);
end if;

// create known variables
knvarLst1 = BackendEquation.equationsVars(eqns, globalKnownVars);
knvarLst2 = BackendEquation.equationsVars(eqns, inAllVars);
// Create a list of known variables true *only* for this shared system
globalKnownVars = BackendVariable.listVar2(knvarLst1,knvarLst2);
// Remove inputs for the jacobian
globalKnownVars = BackendVariable.removeCrefs(independentComRefs, globalKnownVars);
globalKnownVars = BackendVariable.removeCrefs(otherVarsLstComRefs, globalKnownVars);

if Flags.isSet(Flags.JAC_DUMP2) then
print("\n---+++ known variables +++---\n");
BackendDump.printVariables(globalKnownVars);
end if;
try
globalKnownVars := BackendDAEUtil.getGlobalKnownVarsFromShared(inShared);
funcs := BackendDAEUtil.getFunctions(inShared);
einfo := BackendDAEUtil.getExtraInfo(inShared);

if Flags.isSet(Flags.JAC_DUMP2) then
print("---+++ create analytical jacobian +++---");
print("\n---+++ independent variables +++---\n");
BackendDump.printVariables(inDiffVars);
print("\n---+++ equation system +++---\n");
BackendDump.printEquationArray(inResEquations);
end if;

// prepare vars and equations for BackendDAE
emptyVars = BackendVariable.emptyVars();
cache = FCore.emptyCache();
graph = FGraph.empty();
shared = BackendDAEUtil.createEmptyShared(BackendDAE.ALGEQSYSTEM(), einfo, cache, graph);
shared = BackendDAEUtil.setSharedGlobalKnownVars(shared, globalKnownVars);
shared = BackendDAEUtil.setSharedFunctionTree(shared, funcs);
backendDAE = BackendDAE.DAE({BackendDAEUtil.createEqSystem(dependentVars, eqns)}, shared);
independentVarsLst := BackendVariable.varList(inDiffVars);
independentComRefs := List.map(independentVarsLst, BackendVariable.varCref);

if Flags.isSet(Flags.JAC_DUMP2) then
BackendDump.bltdump("System",backendDAE);
end if;
otherVarsLst := BackendVariable.varList(inotherVars);
otherVarsLstComRefs := List.map(otherVarsLst, BackendVariable.varCref);

backendDAE = BackendDAEUtil.transformBackendDAE(backendDAE, SOME((BackendDAE.NO_INDEX_REDUCTION(), BackendDAE.EXACT())), NONE(), NONE());
BackendDAE.DAE({BackendDAE.EQSYSTEM(orderedVars = dependentVars)}, BackendDAE.SHARED(globalKnownVars = globalKnownVars)) = backendDAE;
if Flags.isSet(Flags.JAC_DUMP2) then
print("\n---+++ known variables +++---\n");
BackendDump.printVariables(globalKnownVars);
end if;

// prepare creation of symbolic jacobian
// create dependent variables
dependentVarsLst = BackendVariable.varList(dependentVars);
// dependentVarsLst = listReverse(dependentVarsLst);
dependentVars := BackendVariable.mergeVariables(inResVars, inotherVars);
eqns := BackendEquation.merge(inResEquations, inotherEquations);

(symJacBDAE, funcs, sparsePattern, sparseColoring) = generateGenericJacobian(backendDAE,
independentVarsLst,
emptyVars,
emptyVars,
globalKnownVars,
inResVars,
dependentVarsLst,
inName,
inOnlySparsePattern);
if Flags.isSet(Flags.JAC_DUMP2) then
print("\n---+++ created backend system +++---\n");
print("\n---+++ vars +++---\n");
BackendDump.printVariables(dependentVars);
print("\n---+++ equations +++---\n");
BackendDump.printEquationArray(eqns);
end if;

shared = BackendDAEUtil.setSharedFunctionTree(inShared, funcs);
// create known variables
knvarLst1 := BackendEquation.equationsVars(eqns, globalKnownVars);
knvarLst2 := BackendEquation.equationsVars(eqns, inAllVars);
// Create a list of known variables true *only* for this shared system
globalKnownVars := BackendVariable.listVar2(knvarLst1,knvarLst2);
// Remove inputs for the jacobian
globalKnownVars := BackendVariable.removeCrefs(independentComRefs, globalKnownVars);
globalKnownVars := BackendVariable.removeCrefs(otherVarsLstComRefs, globalKnownVars);

if Flags.isSet(Flags.JAC_DUMP2) then
print("\n---+++ known variables +++---\n");
BackendDump.printVariables(globalKnownVars);
end if;

then (BackendDAE.GENERIC_JACOBIAN(symJacBDAE, sparsePattern, sparseColoring), shared);
// prepare vars and equations for BackendDAE
cache := FCore.emptyCache();
graph := FGraph.empty();
shared := BackendDAEUtil.createEmptyShared(BackendDAE.ALGEQSYSTEM(), einfo, cache, graph);
shared := BackendDAEUtil.setSharedGlobalKnownVars(shared, globalKnownVars);
shared := BackendDAEUtil.setSharedFunctionTree(shared, funcs);
backendDAE := BackendDAE.DAE({BackendDAEUtil.createEqSystem(dependentVars, eqns)}, shared);

case(_, _, _, _, _, _, _, _)
equation
true = Flags.isSet(Flags.JAC_DUMP);
Error.addInternalError("function getSymbolicJacobian failed", sourceInfo());
then (BackendDAE.EMPTY_JACOBIAN(), inShared);
if Flags.isSet(Flags.JAC_DUMP2) then
BackendDump.bltdump("System",backendDAE);
end if;

else (BackendDAE.EMPTY_JACOBIAN(), inShared);
end matchcontinue;
backendDAE := BackendDAEUtil.transformBackendDAE(backendDAE, SOME((BackendDAE.NO_INDEX_REDUCTION(), BackendDAE.EXACT())), NONE(), NONE());
BackendDAE.DAE({BackendDAE.EQSYSTEM(orderedVars = dependentVars)}, BackendDAE.SHARED(globalKnownVars = globalKnownVars)) := backendDAE;

// prepare creation of symbolic jacobian
// create dependent variables
dependentVarsLst := BackendVariable.varList(dependentVars);

(symJacBDAE, funcs, sparsePattern, sparseColoring) := generateGenericJacobian(backendDAE,
independentVarsLst,
BackendVariable.emptyVars(),
BackendVariable.emptyVars(),
globalKnownVars,
inResVars,
dependentVarsLst,
inName,
inOnlySparsePattern);

outJacobian := BackendDAE.GENERIC_JACOBIAN(symJacBDAE, sparsePattern, sparseColoring);
outShared := BackendDAEUtil.setSharedFunctionTree(inShared, funcs);
else
if Flags.isSet(Flags.JAC_DUMP) then
Error.addInternalError("function getSymbolicJacobian failed", sourceInfo());
end if;
outJacobian := BackendDAE.EMPTY_JACOBIAN();
outShared := inShared;
end try;
end getSymbolicJacobian;

public function hasGenericSymbolicJacobian
Expand Down

0 comments on commit fa0a689

Please sign in to comment.