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

Enable algebraic loops in fmi2GetDirectionalDerivative #2287

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions Compiler/SimCode/SimCodeUtil.mo
Original file line number Diff line number Diff line change
Expand Up @@ -1221,6 +1221,9 @@ algorithm
try
column::{} := symjac.columns;
(eqs, modelInfo, tmpSymJacs) := addAlgebraicLoopsModelInfo(column.columnEqns, modelInfo);
// set partition index to number of clocks (max index) for now.
// TODO: use actual clock index to support multirate systems
tmpSymJacs := list(rewriteJacPartIdx(a, modelInfo.nSubClocks) for a in tmpSymJacs);
outSymJacsInSymJacs := listAppend(tmpSymJacs, outSymJacsInSymJacs);
column.columnEqns := eqs;
symjac.columns := {column};
Expand Down Expand Up @@ -12806,6 +12809,7 @@ algorithm
({contSimJac}, outModelInfo, symJacs) := addAlgebraicLoopsModelInfoSymJacs({contSimJac}, inModelInfo);
contPartSimDer := SOME(contSimJac);
// set partition index to number of clocks (max index) for now
// TODO: use actual clock indices to support multirate systems
symJacFMI := {rewriteJacPartIdx(contSimJac, inModelInfo.nSubClocks)};
else
contPartSimDer := NONE();
Expand Down
4 changes: 2 additions & 2 deletions Compiler/Template/CodegenCpp.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ template translateModel(SimCode simCode)
let()= textFile(simulationWriteOutputCppFile(simCode , &extraFuncs , &extraFuncsDecl, "", stateDerVectorName, false),'OMCpp<%fileNamePrefix%>WriteOutput.cpp')
let()= textFile(simulationFactoryFile(simCode , &extraFuncs , &extraFuncsDecl, ""),'OMCpp<%fileNamePrefix%>FactoryExport.cpp')
let()= textFile(simulationMainRunScript(simCode , &extraFuncs , &extraFuncsDecl, "", "", "", "exec"), '<%fileNamePrefix%><%simulationMainRunScriptSuffix(simCode , &extraFuncs , &extraFuncsDecl, "")%>')
let jac = (jacobianMatrixes |> JAC_MATRIX(columns=mat) =>
(mat |> JAC_COLUMN(columnEqns=eqs) => algloopfiles(eqs, simCode, &extraFuncs, &extraFuncsDecl, "", contextAlgloopJacobian, 0, stateDerVectorName, false) ;separator="")
let jac = (jacobianMatrixes |> JAC_MATRIX(columns=mat, partitionIndex=partIdx) =>
(mat |> JAC_COLUMN(columnEqns=eqs) => algloopfiles(eqs, simCode, &extraFuncs, &extraFuncsDecl, "", contextAlgloopJacobian, partIdx, stateDerVectorName, false) ;separator="")
;separator="")
let alg = algloopfiles(listAppend(allEquations, initialEquations), simCode, &extraFuncs, &extraFuncsDecl, "", contextAlgloop, 0, stateDerVectorName, false)
let clk = getSubPartitions(clockedPartitions) |> subPartition hasindex i fromindex 1 =>
Expand Down
8 changes: 5 additions & 3 deletions Compiler/Template/CodegenFMUCommon.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ case SIMCODE(modelInfo = MODELINFO(__)) then
match subPartition
case SUBPARTITION(subClock=SUBCLOCK(factor=RATIONAL(nom=fsub, denom=fsuper), shift=RATIONAL(nom=snom, denom=sres))) then
<<
<Clock><Periodic
<Clock><Inferred
interval="<%realMul(bi, realDiv(intReal(fsub), intReal(fsuper)))%>"
<%if intGt(snom, 0) then 'shiftCounter="'+snom+'"'%>
<%if intGt(sres, 1) then 'resolution="'+sres+'"'%>
Expand All @@ -348,14 +348,16 @@ case SIMCODE(modelInfo = MODELINFO(__)) then
match subPartition
case SUBPARTITION(subClock=SUBCLOCK(factor=RATIONAL(nom=fsub, denom=fsuper), shift=RATIONAL(nom=snom, denom=sres))) then
<<
<Clock><Periodic
<Clock><Inferred
intervalCounter="<%intMul(intMul(bic, fsub), sres)%>"
<%if intGt(snom, 0) then 'shiftCounter="'+intMul(intMul(snom, resi), fsuper)+'"'%>
resolution="<%intMul(intMul(resi, sres), fsuper)%>"
/></Clock>
>>
; separator="\n")
case INFERRED_CLOCK(__) then
case REAL_CLOCK()
case INTEGER_CLOCK()
case INFERRED_CLOCK() then
<<
<Clock><Inferred/></Clock>
>>
Expand Down
23 changes: 5 additions & 18 deletions Compiler/Template/CodegenFMUCpp.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ template fmuModelCppFile(SimCode simCode,Text& extraFuncs,Text& extraFuncsDecl,T
"Generates code for FMU target."
::=
match simCode
case SIMCODE(modelInfo=MODELINFO(vars=SIMVARS(inputVars=inputVars, algVars=algVars, outputVars=outputVars)), modelStructure=modelStructure) then
case SIMCODE(modelInfo=MODELINFO(vars=SIMVARS(inputVars=inputVars, algVars=algVars)), modelStructure=modelStructure) then
let modelName = dotPath(modelInfo.name)
let modelShortName = lastIdentOfPath(modelInfo.name)
let modelLongName = System.stringReplace(modelName, ".", "_")
Expand Down Expand Up @@ -288,7 +288,10 @@ case SIMCODE(modelInfo=MODELINFO(vars=SIMVARS(inputVars=inputVars, algVars=algVa
match var case SIMVAR(name=name, type_=T_REAL()) then
intSub(getVariableIndex(cref2simvar(name, simCode)), 1) ;separator=", "%>};
// value references of real discrete states and outputs
unsigned int <%modelShortName%>FMU::_outputRefs[] = {<%DiscreteStateOutputRefs(modelStructure)%>};
unsigned int <%modelShortName%>FMU::_outputRefs[] = {<%algVars |> var =>
match var case SIMVAR(name=name, type_=T_REAL(), varKind=CLOCKED_STATE())
case SIMVAR(name=name, type_=T_REAL(), causality=OUTPUT()) then
intSub(getVariableIndex(cref2simvar(name, simCode)), 1) ;separator=", "%>};

// constructor
<%modelShortName%>FMU::<%modelShortName%>FMU(IGlobalSettings* globalSettings, shared_ptr<ISimObjects> simObjects)
Expand Down Expand Up @@ -319,22 +322,6 @@ case SIMCODE(modelInfo=MODELINFO(vars=SIMVARS(inputVars=inputVars, algVars=algVa
// <%setExternalFunction(modelInfo)%>
end fmuModelCppFile;

template DiscreteStateOutputRefs(Option<FmiModelStructure> fmiModelStructure)
"Generates list of value references of discrete states and outputs"
::=
match fmiModelStructure
case SOME(modelStructure as FMIMODELSTRUCTURE(__)) then
let discreteStateRefs = match modelStructure.fmiDiscreteStates
case FMIDISCRETESTATES(fmiUnknownsList=fmiUnknownsList) then
'<%fmiUnknownsList |> fmiUnknown => match fmiUnknown
case FMIUNKNOWN(index=index) then intSub(index, 1) ;separator=", "%>'
let outputRefs = match modelStructure.fmiOutputs
case FMIOUTPUTS(fmiUnknownsList=fmiUnknownsList) then
'<%fmiUnknownsList |> fmiUnknown => match fmiUnknown
case FMIUNKNOWN(index=index) then intSub(index, 1) ;separator=", "%>'
'<%discreteStateRefs%><%if intGt(stringLength(discreteStateRefs), 0) then ", "%><%outputRefs%>'
end DiscreteStateOutputRefs;

template ModelDefineData(ModelInfo modelInfo)
"Generates global data in simulation file."
::=
Expand Down