Skip to content
This repository has been archived by the owner on May 18, 2019. It is now read-only.

[FMI] add initial implementation for intialUnknowns #1938

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Compiler/BackEnd/BackendDAEUtil.mo
Original file line number Diff line number Diff line change
Expand Up @@ -8139,6 +8139,16 @@ algorithm
varLst := List.flatten(List.map(globalKnownVars::List.map(eqs, BackendVariable.daeVars), BackendVariable.varList));
end getAllVarLst;

public function getOrderedVarsLst "retrieve all orderedVars variables of the dae by collecting them from each equation system"
input BackendDAE.BackendDAE dae;
output list<BackendDAE.Var> varLst;
protected
BackendDAE.EqSystems eqs;
algorithm
BackendDAE.DAE(eqs=eqs) := dae;
varLst := List.flatten(List.map(List.map(eqs, BackendVariable.daeVars), BackendVariable.varList));
end getOrderedVarsLst;

public function isClockedSyst
input BackendDAE.EqSystem inSyst;
output Boolean out;
Expand Down
2 changes: 1 addition & 1 deletion Compiler/BackEnd/SymbolicJacobian.mo
Original file line number Diff line number Diff line change
Expand Up @@ -1061,7 +1061,7 @@ algorithm
end matchcontinue;
end checkLinearSystem;

protected function generateSparsePattern "author: wbraun
public function generateSparsePattern "author: wbraun
Function generated for a given set of variables and
equations the sparsity pattern and a coloring of Jacobian matrix A^(NxM).
col: N = size(diffVars)
Expand Down
66 changes: 57 additions & 9 deletions Compiler/SimCode/SimCodeUtil.mo
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ algorithm

// collect fmi partial derivative
if FMI.isFMIVersion20(FMUVersion) then
(SymbolicJacsFMI, modelStructure, modelInfo, SymbolicJacsTemp, uniqueEqIndex) := createFMIModelStructure(inFMIDer, modelInfo, uniqueEqIndex);
(SymbolicJacsFMI, modelStructure, modelInfo, SymbolicJacsTemp, uniqueEqIndex) := createFMIModelStructure(inFMIDer, modelInfo, uniqueEqIndex, inInitDAE);
SymbolicJacsNLS := listAppend(SymbolicJacsTemp, SymbolicJacsNLS);
end if;
// collect symbolic jacobians in linear loops of the overall jacobians
Expand Down Expand Up @@ -12686,6 +12686,7 @@ public function createFMIModelStructure
input BackendDAE.SymbolicJacobians inSymjacs;
input SimCode.ModelInfo inModelInfo;
input Integer inUniqueEqIndex;
input BackendDAE.BackendDAE inInitDAE;
output list<SimCode.JacobianMatrix> symJacFMI = {};
output Option<SimCode.FmiModelStructure> outFmiModelStructure;
output SimCode.ModelInfo outModelInfo = inModelInfo;
Expand All @@ -12709,29 +12710,31 @@ protected
list<SimCodeVar.SimVar> tempvars;
SimCodeVar.SimVars vars;
SimCode.HashTableCrefToSimVar crefSimVarHT;
SimCode.FmiInitialUnknowns fmiInitUnknowns;
constant Boolean debug = false;
algorithm
try
//print("Start creating createFMIModelStructure\n");
// combine the transposed sparse pattern of matrix A and B
// to obtain dependencies for the derivativesq
if debug then print("Start creating createFMIModelStructure\n"); end if;
// get the sparsity pattern of the matrix FMIDER
// to obtain dependencies for the derivatives and outputs
SOME((optcontPartDer, spPattern as (_, spTA, (diffCrefsA, diffedCrefsA),_), spColors)) := SymbolicJacobian.getJacobianMatrixbyName(inSymjacs, "FMIDER");

crefSimVarHT := createCrefToSimVarHT(inModelInfo);
//print("-- Got matrixes\n");
if debug then print("-- create Hashtable\n"); end if;

(spTA, derdiffCrefsA) := translateSparsePatterCref2DerCref(spTA, crefSimVarHT, {}, {});
//print("-- translateSparsePatterCref2DerCref matrixes AB\n");

// collect all variable
varsA := getSimVars2Crefs(diffedCrefsA, crefSimVarHT);
varsB := getSimVars2Crefs(derdiffCrefsA, crefSimVarHT);
varsA := listAppend(varsA, varsB);
varsB := getSimVars2Crefs(diffCrefsA, crefSimVarHT);
varsA := listAppend(varsA, varsB);
//print("-- created vars for AB\n");
if debug then print("-- Translate the crefs to simcode cref to obtain the right index\n"); end if;
sparseInts := sortSparsePattern(varsA, spTA, true);
//print("-- sorted vars for AB\n");

derivatives := translateSparsePatterInts2FMIUnknown(sparseInts, {});
if debug then print("-- Sorted and transformed to dependency info\n"); end if;

(derivatives, outputs) := List.split(derivatives, inModelInfo.varInfo.numStateVars);

Expand All @@ -12745,19 +12748,24 @@ algorithm
else
contPartSimDer := NONE();
end if;
if debug then print("-- FMI directional derivatives created\n"); end if;

//TODO: create DiscreteStates with dependencies, like derivatives
clockedStates := List.filterOnTrue(inModelInfo.vars.algVars, isClockedStateSimVar);
discreteStates := List.map1(clockedStates, createFmiUnknownFromSimVar, 2*listLength(derivatives)+1);

// create initial unknonw dependencies
fmiInitUnknowns := getFMIInitialDep(inInitDAE, crefSimVarHT);
if debug then print("-- FMI initial unknown created\n"); end if;

outFmiModelStructure :=
SOME(
SimCode.FMIMODELSTRUCTURE(
SimCode.FMIOUTPUTS(outputs),
SimCode.FMIDERIVATIVES(derivatives),
contPartSimDer,
SimCode.FMIDISCRETESTATES(discreteStates),
SimCode.FMIINITIALUNKNOWNS({})));
fmiInitUnknowns));
else
Error.addInternalError("SimCodeUtil.createFMIModelStructure failed", sourceInfo());
fail();
Expand Down Expand Up @@ -12866,6 +12874,46 @@ algorithm
end match;
end mergeSparsePatter;

protected function getFMIInitialDep
input BackendDAE.BackendDAE initDAE;
input SimCode.HashTableCrefToSimVar crefSimVarHT;
output SimCode.FmiInitialUnknowns fmiInitUnknowns;
protected
list<BackendDAE.Var> initUnknowns, states;
list<DAE.ComponentRef> diffCrefs, diffedCrefs, derdiffCrefs;
list<tuple<DAE.ComponentRef, list<DAE.ComponentRef>>> spT;
list<tuple<Integer, list<Integer>>> sparseInts;
list<SimCode.FmiUnknown> unknowns;
list<SimCodeVar.SimVar> vars1, vars2;
BackendDAE.BackendDAE tmpBDAE;
algorithm
try
//prepare initialUnknows
tmpBDAE := BackendDAEOptimize.collapseIndependentBlocks(initDAE);
tmpBDAE := BackendDAEUtil.transformBackendDAE(tmpBDAE, SOME((BackendDAE.NO_INDEX_REDUCTION(), BackendDAE.EXACT())), NONE(), NONE());

// TODO: filter the initUnknows for variables
// with causality = "output" and causality = "calculatedParameter"
// and all continuous-time states and all state derivatives
initUnknowns := BackendDAEUtil.getOrderedVarsLst(tmpBDAE);

((_, spT, (diffCrefs, diffedCrefs),_),_) := SymbolicJacobian.generateSparsePattern(tmpBDAE, initUnknowns, initUnknowns);

// collect all variable
vars1 := getSimVars2Crefs(diffedCrefs, crefSimVarHT);
vars2 := getSimVars2Crefs(diffCrefs, crefSimVarHT);
vars1 := listAppend(vars1, vars2);

sparseInts := sortSparsePattern(vars1, spT, true);
unknowns := translateSparsePatterInts2FMIUnknown(sparseInts, {});

fmiInitUnknowns := SimCode.FMIINITIALUNKNOWNS(unknowns);
else
fmiInitUnknowns := SimCode.FMIINITIALUNKNOWNS({});
end try;
end getFMIInitialDep;


public function getStateSimVarIndexFromIndex
input list<SimCodeVar.SimVar> inStateVars;
input Integer inIndex;
Expand Down