Skip to content

Commit

Permalink
Dae mode symbolic jacobian (#9198)
Browse files Browse the repository at this point in the history
make dae mode symbolic jacobian available
use `--daeMode --generateSymbolicJacobian` as translation flags and `-jacobian=coloredSymbolical` as simflag.
  • Loading branch information
kabdelhak committed Jul 6, 2022
1 parent 460088d commit 1f19bcc
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
9 changes: 6 additions & 3 deletions OMCompiler/Compiler/SimCode/SimCodeMain.mo
Expand Up @@ -1536,7 +1536,6 @@ algorithm
crefToSimVarHT := SimCodeUtil.createCrefToSimVarHT(modelInfo);
(symJacs, uniqueEqIndex) := SimCodeUtil.createSymbolicJacobianssSimCode({}, crefToSimVarHT, uniqueEqIndex, matrixnames, {});
symJacs := listReverse(Util.getOption(daeModeSP) :: symJacs);
jacobianEquations := SimCodeUtil.collectAllJacobianEquations(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 +1548,6 @@ algorithm
matrixnames := {"A", "B", "C", "D", "F"};
end if;
(symJacs, uniqueEqIndex) := SimCodeUtil.createSymbolicJacobianssSimCode({}, crefToSimVarHT, uniqueEqIndex, matrixnames, {});
jacobianEquations := {};
end if;

// collect symbolic jacobians in initialization loops of the overall jacobians
Expand Down Expand Up @@ -1607,11 +1605,16 @@ algorithm
({symDAESparsPattern}, uniqueEqIndex) := SimCodeUtil.createSymbolicJacobianssSimCode({daeModeJacobian}, crefToSimVarHT, uniqueEqIndex, {"daeMode"}, {});
daeModeSP := SOME(symDAESparsPattern);

// copy the sparsity pattern to the A jacobian
if Flags.getConfigBool(Flags.GENERATE_SYMBOLIC_JACOBIAN) then
SymbolicJacs := list(SimCodeUtil.syncDAEandSimJac(symjac, symDAESparsPattern) for symjac in SymbolicJacs);
end if;

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

/* This is a *much* better estimate than the guessed number of equations */
modelInfo := SimCodeUtil.addNumEqns(modelInfo, uniqueEqIndex-listLength(jacobianEquations));
modelInfo := SimCodeUtil.addNumEqns(modelInfo, uniqueEqIndex - listLength(jacobianEquations));

// update hash table
// mahge: This creates a new crefToSimVarHT discarding everything added upto here
Expand Down
25 changes: 25 additions & 0 deletions OMCompiler/Compiler/SimCode/SimCodeUtil.mo
Expand Up @@ -4728,6 +4728,31 @@ algorithm
end matchcontinue;
end createSymbolicSimulationJacobian;

public function syncDAEandSimJac
input SimCode.JacobianMatrix symJac;
input output SimCode.JacobianMatrix daeJac;
algorithm
if symJac.matrixName == "A" then
daeJac.columns := list(updateSimVarIndex(col, daeJac.crefsHT) for col in symJac.columns);
else
daeJac := symJac;
end if;
end syncDAEandSimJac;

public function updateSimVarIndex
input output SimCode.JacobianColumn column;
input Option<HashTableCrefSimVar.HashTable> crefsHT;
algorithm
column := match crefsHT
local
HashTableCrefSimVar.HashTable table;
case SOME(table) algorithm
column.columnVars := list(BaseHashTable.getOrDefault(var.name, table, var) for var in column.columnVars);
then column;
else column;
end match;
end updateSimVarIndex;

protected function getFurtherVars
input BackendDAE.Var v;
input tuple<list<BackendDAE.Var>, DAE.ComponentRef> inTpl;
Expand Down
11 changes: 11 additions & 0 deletions OMCompiler/Compiler/Util/BaseHashTable.mo
Expand Up @@ -320,6 +320,17 @@ algorithm
(_, value) := getValueArray(varr, i);
end get;

public function getOrDefault
"Returns a Value given a Key and a HashTable if it exists,
otherwise returns the default value."
input Key key;
input HashTable hashTable;
input Value default;
output Value value;
algorithm
value := if hasKey(key, hashTable) then get(key, hashTable) else default;
end getOrDefault;

protected function hasKeyIndex
"help function to get and hasKey"
input Key key;
Expand Down

0 comments on commit 1f19bcc

Please sign in to comment.