Skip to content

Commit

Permalink
[BE] fix sparse pattern dae mode symbolic jacobian (#9136)
Browse files Browse the repository at this point in the history
- for dae mode data generate new sparsity pattern
 - for simulation use JAC_A matrix
  • Loading branch information
kabdelhak committed Jun 23, 2022
1 parent de73969 commit 6a5941e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 15 deletions.
8 changes: 3 additions & 5 deletions OMCompiler/Compiler/BackEnd/SymbolicJacobian.mo
Expand Up @@ -223,7 +223,6 @@ protected
BackendDAE.Variables emptyVars = BackendVariable.emptyVars();
Option<BackendDAE.SymbolicJacobian> symjac;
DAE.FunctionTree funcs;
BackendDAE.Jacobian jacobian;
constant Boolean debug = false;
algorithm
try
Expand Down Expand Up @@ -262,11 +261,10 @@ algorithm
daeMode = true);
if debug then execStat(getInstanceName() + "-> generateGenericJacobian "); end if;

jacobian := BackendDAE.GENERIC_JACOBIAN(symjac, sparsePattern, coloredCols);
shared.symjacs := List.set(shared.symjacs, BackendDAE.SymbolicJacobianAIndex, (symjac, sparsePattern, coloredCols));
shared.functionTree := funcs;

if debug then BackendDump.dumpJacobianString(jacobian); end if;
if debug then BackendDump.dumpJacobianString(BackendDAE.GENERIC_JACOBIAN(symjac, sparsePattern, coloredCols)); end if;
else
// only generate sparsity pattern
(sparsePattern, coloredCols) := generateSparsePattern(DAE, inDepVars, depVars);
Expand Down Expand Up @@ -2186,7 +2184,7 @@ algorithm

BackendDAE.Shared shared;
BackendDAE.Variables globalKnownVars, globalKnownVars1;
list<BackendDAE.Var> diffedVars "resVars", seedlst, derseedlst, stateVars, indepVars;
list<BackendDAE.Var> diffedVars "resVars", seedlst, indepVars;

DAE.FunctionTree funcs;

Expand All @@ -2212,7 +2210,7 @@ algorithm
end if;

// Differentiate the eqns system in reducedDAE w.r.t. independents
(backendDAE as BackendDAE.DAE(shared=_), funcs) = generateSymbolicJacobian(reducedDAE, indepVars, inDifferentiatedVars, BackendVariable.listVar1(seedlst), inStateVars, inInputVars, inParameterVars, inName, daeMode);
(backendDAE as BackendDAE.DAE(), funcs) = generateSymbolicJacobian(reducedDAE, indepVars, inDifferentiatedVars, BackendVariable.listVar1(seedlst), inStateVars, inInputVars, inParameterVars, inName, daeMode);
if Flags.isSet(Flags.JAC_DUMP2) then
print("analytical Jacobians -> generated equations for Jacobian " + inName + " time: " + realString(clock()) + "\n");
end if;
Expand Down
19 changes: 9 additions & 10 deletions OMCompiler/Compiler/SimCode/SimCodeMain.mo
Expand Up @@ -1527,14 +1527,14 @@ algorithm
(daeModeSP, uniqueEqIndex, tempVars) := SimCodeUtil.createSymbolicSimulationJacobian(
inJacobian = BackendDAE.GENERIC_JACOBIAN(daeModeJac, daeModeSparsity, daeModeColoring),
iuniqueEqIndex = uniqueEqIndex,
itempvars = tempVars); // tempVars to modelInfo?
itempvars = tempVars);
tmpB := FlagsUtil.set(Flags.NO_START_CALC, true);
modelInfo := SimCodeUtil.createModelInfo(className, p, emptyBDAE, inInitDAE, functions, {}, 0, spatialInfo.maxIndex, fileDir, 0, tempVars);
FlagsUtil.set(Flags.NO_START_CALC, tmpB);
//create hash table
crefToSimVarHT := SimCodeUtil.createCrefToSimVarHT(modelInfo);
(symJacs, uniqueEqIndex) := SimCodeUtil.createSymbolicJacobianssSimCode({}, crefToSimVarHT, uniqueEqIndex, matrixnames, {});
symJacs := Util.getOption(daeModeSP) :: symJacs;
symJacs := listReverse(Util.getOption(daeModeSP) :: symJacs);
else
tmpB := FlagsUtil.set(Flags.NO_START_CALC, true);
modelInfo := SimCodeUtil.createModelInfo(className, p, emptyBDAE, inInitDAE, functions, {}, 0, spatialInfo.maxIndex, fileDir, 0, tempVars);
Expand All @@ -1549,7 +1549,6 @@ algorithm
(symJacs, uniqueEqIndex) := SimCodeUtil.createSymbolicJacobianssSimCode({}, crefToSimVarHT, uniqueEqIndex, matrixnames, {});
end if;


// collect symbolic jacobians in initialization loops of the overall jacobians
SymbolicJacsNLS := {};
(initialEquations, modelInfo, SymbolicJacsTemp) := SimCodeUtil.addAlgebraicLoopsModelInfo(initialEquations, modelInfo);
Expand Down Expand Up @@ -1598,13 +1597,13 @@ algorithm

algebraicStateVars := SimCodeUtil.sortSimVarsAndWriteIndex(algebraicStateVars, crefToSimVarHT);

if not Flags.getConfigBool(Flags.GENERATE_SYMBOLIC_JACOBIAN) then
//create hash table
// only create sparsity pattern
daeModeJacobian := listGet(inBackendDAE.shared.symjacs, BackendDAE.SymbolicJacobianAIndex);
({symDAESparsPattern}, uniqueEqIndex) := SimCodeUtil.createSymbolicJacobianssSimCode({daeModeJacobian}, crefToSimVarHT, uniqueEqIndex, {"daeMode"}, {});
daeModeSP := SOME(symDAESparsPattern);
end if;
// only create sparsity pattern for dae mode data even if it is created with --generateSymbolicJacobian
// the A matrix will be used symbolically
// (also the A matrix sparsity pattern seems to be faulty so we use this one instead)
daeModeJacobian := listGet(inBackendDAE.shared.symjacs, BackendDAE.SymbolicJacobianAIndex);
({symDAESparsPattern}, uniqueEqIndex) := SimCodeUtil.createSymbolicJacobianssSimCode({daeModeJacobian}, crefToSimVarHT, uniqueEqIndex, {"daeMode"}, {});
daeModeSP := SOME(symDAESparsPattern);

daeModeConf := SimCode.ALL_EQUATIONS();
daeModeData := SOME(SimCode.DAEMODEDATA(daeEquations, daeModeSP, residualVars, algebraicStateVars, auxiliaryVars, daeModeConf));

Expand Down

0 comments on commit 6a5941e

Please sign in to comment.