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

add fmiinitial dependency list in fmiexport #2888

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 @@ -8287,6 +8287,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 @@ -1139,7 +1139,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
89 changes: 83 additions & 6 deletions Compiler/SimCode/SimCodeUtil.mo
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,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);
if debug then execStat("simCode: create FMI model structure"); end if;
end if;
Expand Down Expand Up @@ -11947,6 +11947,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 @@ -11970,16 +11971,19 @@ protected
list<SimCodeVar.SimVar> tempvars;
SimCodeVar.SimVars vars;
SimCode.HashTableCrefToSimVar crefSimVarHT;
list<Integer> intLst;
list<Integer> intLst, derivativesLst;
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
// 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");

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

Expand All @@ -11995,8 +11999,8 @@ algorithm
allUnknowns := translateSparsePatterInts2FMIUnknown(sparseInts, {});

// get derivatives pattern
intLst := list(getVariableIndex(v) for v in inModelInfo.vars.derivativeVars);
derivatives := list(fmiUnknown for fmiUnknown guard(Util.boolOrList(list(isFmiUnknown(i, fmiUnknown) for i in intLst))) in allUnknowns);
derivativesLst := list(getVariableIndex(v) for v in inModelInfo.vars.derivativeVars);
derivatives := list(fmiUnknown for fmiUnknown guard(Util.boolOrList(list(isFmiUnknown(i, fmiUnknown) for i in derivativesLst))) in allUnknowns);

// get output pattern
varsA := List.filterOnTrue(inModelInfo.vars.algVars, isOutputSimVar);
Expand All @@ -12021,6 +12025,11 @@ algorithm
else
contPartSimDer := NONE();
end if;
if debug then print("-- FMI directional derivatives created\n"); end if;

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

outFmiModelStructure :=
SOME(
Expand All @@ -12029,7 +12038,7 @@ algorithm
SimCode.FMIDERIVATIVES(derivatives),
contPartSimDer,
SimCode.FMIDISCRETESTATES(discreteStates),
SimCode.FMIINITIALUNKNOWNS({})));
fmiInitUnknowns));
else
// create empty model structure
try
Expand Down Expand Up @@ -12181,6 +12190,74 @@ algorithm
end match;
end mergeSparsePatter;

protected function getFMIInitialDep
input BackendDAE.BackendDAE initDAE;
input SimCode.HashTableCrefToSimVar crefSimVarHT;
input list<Integer> derivativesLst;
output SimCode.FmiInitialUnknowns fmiInitUnknowns;
protected
list<BackendDAE.Var> initUnknowns, states, tmpinitUnknowns;
list<DAE.ComponentRef> diffCrefs, diffedCrefs, derdiffCrefs, tmpOutputCref;
list<tuple<DAE.ComponentRef, list<DAE.ComponentRef>>> spT;
list<tuple<Integer, list<Integer>>> sparseInts;
list<SimCode.FmiUnknown> unknowns;
list<SimCodeVar.SimVar> vars1, vars2, outputvars;
BackendDAE.BackendDAE tmpBDAE;
list<Integer> intLst;
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);

// get the output patterns with causality = "output" and intial = "calculated"
(tmpinitUnknowns, _) := List.extractOnTrue(initUnknowns,isVarOutputandNotfixed);
tmpOutputCref:= List.filterMap(tmpinitUnknowns,getVarCref);
outputvars := getSimVars2Crefs(tmpOutputCref, crefSimVarHT);
intLst := list(getVariableIndex(v) for v in outputvars);

((_, 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, {});

// filter the initial unknowns with output and derivativelist index
unknowns := list(fmiUnknown for fmiUnknown guard(Util.boolOrList(list(isFmiUnknown(i, fmiUnknown) for i in listAppend(intLst,derivativesLst)))) in unknowns);

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

public function getVarCref
input BackendDAE.Var inVar;
output DAE.ComponentRef outComponentRef;
algorithm
outComponentRef := BackendVariable.varCref(inVar);
end getVarCref;

public function isVarOutputandNotfixed
input BackendDAE.Var inVar;
output Boolean outVar=false;
protected
Boolean direction;
algorithm
if (BackendVariable.isOutputVar(inVar) and not BackendVariable.varFixed(inVar)) then
outVar := true;
end if;
end isVarOutputandNotfixed;

public function getStateSimVarIndexFromIndex
input list<SimCodeVar.SimVar> inStateVars;
input Integer inIndex;
Expand Down
14 changes: 8 additions & 6 deletions Compiler/Template/CodegenFMUCommon.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ match simVar
let clockIndex = getClockIndex(simVar, simCode)
let previous = match varKind case CLOCKED_STATE(__) then '<%getVariableIndex(cref2simvar(previousName, simCode))%>'
let caus = getCausality2(causality, varKind, isValueChangeable)
let initial = getInitialType2(variability, caus, initialValue)
let initial = getInitialType2(variability, caus, initialValue, isFixed)
<<
name="<%System.stringReplace(crefStrNoUnderscore(name),"$", "_D_")%>"
valueReference="<%valueReference%>"
Expand Down Expand Up @@ -540,7 +540,7 @@ match simCode
else ""
end getNumberOfEventIndicators;

template getInitialType2(String variability, String causality, Option<DAE.Exp> initialValue)
template getInitialType2(String variability, String causality, Option<DAE.Exp> initialValue, Boolean isFixed)
"Returns the Initial Attribute of ScalarVariable."
::=
match variability
Expand All @@ -561,8 +561,10 @@ match variability
match causality
case "output"
case "local" then
match initialValue
case SOME(exp) then "exact"
//match initialValue
//case SOME(exp) then "exact"
match isFixed
case true then "exact"
else "calculated"
else ""
else ""
Expand Down Expand Up @@ -599,7 +601,7 @@ template StartString2(SimVar simvar)
match simvar
case SIMVAR(aliasvar = SimCodeVar.ALIAS(__)) then
''
case SIMVAR(initialValue = initialValue, varKind = varKind, causality = causality, type_ = type_, isValueChangeable = isValueChangeable) then
case SIMVAR(initialValue = initialValue, varKind = varKind, causality = causality, type_ = type_, isValueChangeable = isValueChangeable, isFixed = isFixed) then
match initialValue
case SOME(e as ICONST(__)) then ' start="<%initValXml(e)%>"'
case SOME(e as RCONST(__)) then ' start="<%initValXml(e)%>"'
Expand All @@ -609,7 +611,7 @@ case SIMVAR(initialValue = initialValue, varKind = varKind, causality = causalit
else
let variability = getVariability2(varKind, type_)
let caus = getCausality2(causality, varKind, isValueChangeable)
let initial = getInitialType2(variability, caus, initialValue)
let initial = getInitialType2(variability, caus, initialValue, isFixed)
if boolOr(stringEq(initial, "exact"), boolOr(stringEq(initial, "approx"), stringEq(caus, "input"))) then ' start="<%initDefaultValXml(type_)%>"'
else ''
end StartString2;
Expand Down