Skip to content

Commit

Permalink
- fixed wrong variable indices in modelDescription.xml (FMU export fo…
Browse files Browse the repository at this point in the history
…r CPP runtime)

- fixed FMU 1.0 export of cpp runtime
- fixed linker flags for hpcom fmu export (cpp-runtime)
  • Loading branch information
Marcus Walther committed Jul 10, 2015
1 parent 4476180 commit 41e9fb0
Show file tree
Hide file tree
Showing 9 changed files with 250 additions and 222 deletions.
1 change: 1 addition & 0 deletions Compiler/SimCode/HpcOmSimCodeMain.mo
Expand Up @@ -205,6 +205,7 @@ algorithm
varToArrayIndexMapping, varToIndexMapping, crefToSimVarHT, backendMapping ) = simCode;

//print("Number of literals pre: " + intString(listLength(simCodeLiterals)) + "\n");
//print("Number of zero crossings: " + intString(listLength(zeroCrossings)) + "\n");

SOME(SimCode.BACKENDMAPPING(simVarMapping=simVarMapping)) = backendMapping;

Expand Down
20 changes: 7 additions & 13 deletions Compiler/SimCode/SimCodeUtil.mo
Expand Up @@ -10007,10 +10007,12 @@ algorithm
(oCurrentVarIndices, oVarIndex) := match(iVar, iVarToIndexMapping, iCurrentVarIndices)
case(SimCodeVar.SIMVAR(name=name, aliasvar=SimCodeVar.NOALIAS()),_,tmpCurrentVarIndices)
equation
//print("getArrayIdxByVar: Handling common variable\n");
(varIdx,tmpCurrentVarIndices) = getVarToArrayIndexByType(iVarType, tmpCurrentVarIndices);
then (tmpCurrentVarIndices, varIdx);
case(SimCodeVar.SIMVAR(name=name, aliasvar=SimCodeVar.NEGATEDALIAS(varName)),_,_)
equation
//print("getArrayIdxByVar: Handling negated alias variable pointing to " + ComponentReference.printComponentRefStr(varName) + "\n");
if(BaseHashTable.hasKey(varName, iVarToIndexMapping)) then
varIdx::_ = BaseHashTable.get(varName, iVarToIndexMapping);
varIdx = intMul(varIdx,-1);
Expand All @@ -10021,6 +10023,7 @@ algorithm
then (iCurrentVarIndices, varIdx);
case(SimCodeVar.SIMVAR(name=name, aliasvar=SimCodeVar.ALIAS(varName)),_,_)
equation
//print("getArrayIdxByVar: Handling alias variable pointing to " + ComponentReference.printComponentRefStr(varName) + "\n");
if(BaseHashTable.hasKey(varName, iVarToIndexMapping)) then
varIdx::_ = BaseHashTable.get(varName, iVarToIndexMapping);
else
Expand Down Expand Up @@ -10094,6 +10097,9 @@ algorithm
if(BaseHashTable.hasKey(varName, iVarToArrayIndexMapping)) then
((arrayDimensions,varIndices)) := BaseHashTable.get(varName, iVarToArrayIndexMapping);
arraySize := arrayLength(varIndices);

((concreteVarIndex,_,_)) := List.fold(listReverse(arraySubscripts), getUnrolledArrayIndex, (0, 1, listReverse(arrayDimensions)));

for arrayIdx in 0:(arraySize-1) loop
idx := arrayGet(varIndices, arraySize-arrayIdx);
if(intLt(idx, 0)) then
Expand All @@ -10107,19 +10113,7 @@ algorithm
end if;
end if;
end for;
((concreteVarIndex,_,_)) := List.fold(listReverse(arraySubscripts), getUnrolledArrayIndex, (0, 1, listReverse(arrayDimensions)));

// ignore all values that are undefined references and part of the same array
idx := 1;
while intLe (idx, concreteVarIndex) loop
if(intLt(arrayGet(varIndices, idx), 1)) then
concreteVarIndex := concreteVarIndex - 1;
end if;
idx := idx + 1;
end while;

concreteVarIndex := intAbs(arrayGet(varIndices, 1)) - 1 + concreteVarIndex;
oConcreteVarIndex := intString(concreteVarIndex);
oConcreteVarIndex := listGet(tmpVarIndexListNew, concreteVarIndex + 1);
end if;
if(listEmpty(tmpVarIndexListNew)) then
Error.addMessage(Error.INTERNAL_ERROR, {"GetVarIndexListByMapping: No Element for " + ComponentReference.printComponentRefStr(varName) + " found!"});
Expand Down
88 changes: 30 additions & 58 deletions Compiler/Template/CodegenCpp.tpl
Expand Up @@ -2527,12 +2527,6 @@ case var as FUNCTION_PTR(__) then

end paramInit3;







template simulationMainDLLib(SimCode simCode ,Text& extraFuncs,Text& extraFuncsDecl,Text extraFuncsNamespace)
::=
match simCode
Expand Down Expand Up @@ -3156,8 +3150,8 @@ match simCode

//Number of equations
<%dimension1(simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace)%>
_dimZeroFunc = <%zerocrosslength(simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace)%>;
_dimTimeEvent = <%timeeventlength(simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace)%>;
_dimZeroFunc = <%zeroCrossLength(simCode)%>;
_dimTimeEvent = <%timeEventLength(simCode)%>;
//Number of residues
_event_handling= boost::shared_ptr<EventHandling>(new EventHandling());
<%if Flags.isSet(Flags.WRITE_TO_BUFFER) then
Expand Down Expand Up @@ -13071,28 +13065,6 @@ case SIMCODE(__) then
>>
end helpvarlength;

template zerocrosslength(SimCode simCode ,Text& extraFuncs,Text& extraFuncsDecl,Text extraFuncsNamespace)
::=
match simCode
case SIMCODE(modelInfo = MODELINFO(varInfo = vi as VARINFO(__))) then
let size = listLength(zeroCrossings)
<<
<%intSub(listLength(zeroCrossings), vi.numTimeEvents)%>
>>
end zerocrosslength;


template timeeventlength(SimCode simCode ,Text& extraFuncs,Text& extraFuncsDecl,Text extraFuncsNamespace)
::=
match simCode
case SIMCODE(modelInfo = MODELINFO(varInfo = vi as VARINFO(__))) then

<<
<%vi.numTimeEvents%>
>>
end timeeventlength;



template dimZeroFunc(SimCode simCode ,Text& extraFuncs,Text& extraFuncsDecl,Text extraFuncsNamespace)
::=
Expand Down Expand Up @@ -13826,36 +13798,36 @@ template getCondition(list<ZeroCrossing> zeroCrossings,list<SimWhenClause> whenC
::=
let &varDecls = buffer "" /*BUFD*/
let zeroCrossingsCode = checkConditions1(zeroCrossings, &varDecls /*BUFD*/, simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation)
match zeroCrossings
case {} then
match simCode
case SIMCODE(modelInfo = MODELINFO(__)) then
<<
bool <%lastIdentOfPath(modelInfo.name)%>::getCondition(unsigned int index)
{
return false;
}
>>
end match
else
match simCode
case SIMCODE(modelInfo = MODELINFO(__)) then
<<
bool <%lastIdentOfPath(modelInfo.name)%>::getCondition(unsigned int index)
{
<%varDecls%>
switch(index)
{
<%zeroCrossingsCode%>
default:
match zeroCrossings
case {} then
match simCode
case SIMCODE(modelInfo = MODELINFO(__)) then
<<
bool <%lastIdentOfPath(modelInfo.name)%>::getCondition(unsigned int index)
{
string error =string("Wrong condition index ") + boost::lexical_cast<string>(index);
throw ModelicaSimulationError(EVENT_HANDLING,error);
return false;
}
};
}
>>
end match
>>
end match
else
match simCode
case SIMCODE(modelInfo = MODELINFO(__)) then
<<
bool <%lastIdentOfPath(modelInfo.name)%>::getCondition(unsigned int index)
{
<%varDecls%>
switch(index)
{
<%zeroCrossingsCode%>
default:
{
string error =string("Wrong condition index ") + boost::lexical_cast<string>(index);
throw ModelicaSimulationError(EVENT_HANDLING,error);
}
};
}
>>
end match
end getCondition;

template checkConditions1(list<ZeroCrossing> zeroCrossings, Text &varDecls /*BUFP*/,SimCode simCode ,Text& extraFuncs,Text& extraFuncsDecl,Text extraFuncsNamespace, Text stateDerVectorName /*=__zDot*/, Boolean useFlatArrayNotation)
Expand Down
22 changes: 15 additions & 7 deletions Compiler/Template/CodegenCppHpcom.tpl
Expand Up @@ -1855,27 +1855,35 @@ template simulationMainRunScript(SimCode simCode, Text& extraFuncs, Text& extraF
CodegenCpp.simulationMainRunScript(simCode, extraFuncs, extraFuncsDecl, extraFuncsNamespace, preRunCommandLinux, preRunCommandWindows, execCommandLinux)
end simulationMainRunScript;
template simulationMakefile(String target, SimCode simCode, Text& extraFuncs, Text& extraFuncsDecl, Text extraFuncsNamespace)
"Adds specific compiler flags for HPCOM mode to simulation makefile."
template getAdditionalMakefileFlags(Text& additionalLinkerFlags_GCC, Text& additionalLinkerFlags_MSVC, Text& additionalCFlags_GCC, Text& additionalCFlags_MSVC)
::=
let type = getConfigString(HPCOM_CODE)
let &additionalCFlags_GCC = buffer ""
let &additionalCFlags_GCC += if stringEq(type,"openmp") then " -fopenmp" else ""
let &additionalCFlags_GCC += if stringEq(type,"tbb") then ' -I"$(INTEL_TBB_INCLUDE)"' else ""
let &additionalCFlags_MSVC = buffer ""
let &additionalCFlags_MSVC += if stringEq(type,"openmp") then "/openmp" else ""
let &additionalLinkerFlags_GCC = buffer ""
let &additionalLinkerFlags_GCC += if stringEq(type,"tbb") then " $(INTEL_TBB_LIBRARIES) " else ""
let &additionalLinkerFlags_GCC += if stringEq(type,"openmp") then " -fopenmp" else ""
<<
>>
end getAdditionalMakefileFlags;
template simulationMakefile(String target, SimCode simCode, Text& extraFuncs, Text& extraFuncsDecl, Text extraFuncsNamespace)
"Adds specific compiler flags for HPCOM mode to simulation makefile."
::=
let &additionalCFlags_GCC = buffer ""
let &additionalCFlags_MSVC = buffer ""
let &additionalLinkerFlags_GCC = buffer ""
let &additionalLinkerFlags_MSVC = buffer ""
CodegenCpp.simulationMakefile(target, simCode, extraFuncs ,extraFuncsDecl, extraFuncsNamespace, additionalLinkerFlags_GCC,
<<
<%getAdditionalMakefileFlags(additionalCFlags_GCC, additionalCFlags_MSVC, additionalLinkerFlags_GCC, additionalLinkerFlags_MSVC)%>
<%CodegenCpp.simulationMakefile(target, simCode, extraFuncs ,extraFuncsDecl, extraFuncsNamespace, additionalLinkerFlags_GCC,
additionalCFlags_MSVC, additionalCFlags_GCC,
additionalLinkerFlags_MSVC, Flags.isSet(Flags.USEMPI))
additionalLinkerFlags_MSVC, Flags.isSet(Flags.USEMPI))%>
>>
end simulationMakefile;
template numPreVarsHpcom(ModelInfo modelInfo, Option<MemoryMap> hpcOmMemoryOpt)
Expand Down
73 changes: 65 additions & 8 deletions Compiler/Template/CodegenCppInit.tpl
Expand Up @@ -16,7 +16,7 @@ template modelInitXMLFile(SimCode simCode, String numRealVars, String numIntVars
let algLoops = (listAppend(allEquations,initialEquations) |> eq => algLoopXML(eq, simCode, varToArrayIndexMapping, '<%numRealVars%> - 1') ;separator="\n")
let jacobianMatrixes = jacobianMatrixesXML(simCode.jacobianMatrixes)
let descriptionTag = if generateFMUModelDescription then "fmiModelDescription" else "ModelDescription"
let fmiDescriptionAttributes = if generateFMUModelDescription then fmuDescriptionAttributes(simCode, FMUVersion, FMUType, FMUGuid) else 'modelName="<%dotPath(modelInfo.name)%>"'
let fmiDescriptionAttributes = if generateFMUModelDescription then fmiDescriptionAttributes(simCode, FMUVersion, FMUType, FMUGuid) else 'modelName="<%dotPath(modelInfo.name)%>"'
let fmiTypeDefinitions = if generateFMUModelDescription then CodegenFMUCommon.fmiTypeDefinitions(modelInfo, FMUVersion)
let fmiDefaultExperiment = if generateFMUModelDescription then CodegenFMUCommon.DefaultExperiment(simulationSettingsOpt)
<<
Expand All @@ -42,11 +42,40 @@ template modelInitXMLFile(SimCode simCode, String numRealVars, String numIntVars
>>
end modelInitXMLFile;

template fmuDescriptionAttributes(SimCode simCode, String FMUVersion, String FMUType, String FMUGuid)
template fmiDescriptionAttributes(SimCode simCode, String FMUVersion, String FMUType, String FMUGuid)
::=
if isFMIVersion20(FMUVersion) then CodegenFMU2.fmiModelDescriptionAttributes(simCode,FMUGuid)
else CodegenFMU1.fmiModelDescriptionAttributes(simCode,FMUGuid)
end fmuDescriptionAttributes;
else fmiModelDescriptionAttributes(simCode,FMUGuid)
end fmiDescriptionAttributes;

template fmiModelDescriptionAttributes(SimCode simCode, String guid)
"Generates code for ModelDescription file for FMU target."
::=
match simCode
case SIMCODE(modelInfo = MODELINFO(varInfo = vi as VARINFO(__), vars = SIMVARS(stateVars = listStates))) then
let fmiVersion = '1.0'
let modelName = dotPath(modelInfo.name)
let modelIdentifier = System.stringReplace(fileNamePrefix,".", "_")
let description = ''
let author = ''
let version= ''
let generationTool= 'OpenModelica Compiler <%getVersionNr()%>'
let generationDateAndTime = CodegenFMUCommon.xsdateTime(getCurrentDateTime())
let variableNamingConvention = 'structured'
let numberOfContinuousStates = vi.numStateVars
let numberOfEventIndicators = eventIndicatorsLength(simCode)
<<
fmiVersion="<%fmiVersion%>"
modelName="<%modelName%>"
modelIdentifier="<%modelIdentifier%>"
guid="{<%guid%>}"
generationTool="<%generationTool%>"
generationDateAndTime="<%generationDateAndTime%>"
variableNamingConvention="<%variableNamingConvention%>"
numberOfContinuousStates="<%numberOfContinuousStates%>"
numberOfEventIndicators="<%numberOfEventIndicators%>"
>>
end fmiModelDescriptionAttributes;

template modelVariablesXML(ModelInfo modelInfo, HashTableCrIListArray.HashTable varToArrayIndexMapping, String indexForUndefinedReferencesReal, String indexForUndefinedReferencesInt, String indexForUndefinedReferencesBool, Boolean generateFMUModelDescription)
"Generates the xml code for the variable defintions."
Expand Down Expand Up @@ -100,7 +129,7 @@ template scalarVariableAttributeXML(SimVar simVar, HashTableCrIListArray.HashTab
match simVar
case SIMVAR(source = SOURCE(info = info)) then
let valueReference = SimCodeUtil.getVarIndexByMapping(varToArrayIndexMapping,name,indexForUndefinedReferences)
let alias = getAliasVar(aliasvar)
let alias = getAliasAttribute(aliasvar)
let causalityAtt = CodegenFMUCommon.getCausality(causality)
let variability = getVariablity(varKind)
let description = if comment then 'description="<%Util.escapeModelicaStringToXmlString(comment)%>"'
Expand All @@ -110,15 +139,15 @@ template scalarVariableAttributeXML(SimVar simVar, HashTableCrIListArray.HashTab
>>
end scalarVariableAttributeXML;

template getAliasVar(AliasVariable aliasvar)
template getAliasAttribute(AliasVariable aliasvar)
"Returns the alias Attribute of ScalarVariable."
::=
match aliasvar
case NOALIAS(__) then "noAlias"
case ALIAS(__) then "alias"
case NEGATEDALIAS(__) then "negatedAlias"
else "noAlias"
end getAliasVar;
else "undefinedAliasType"
end getAliasAttribute;

template algLoopXML(SimEqSystem eqs, SimCode simCode, HashTableCrIListArray.HashTable varToArrayIndexMapping, String indexForUndefinedReferences)
::=
Expand Down Expand Up @@ -183,6 +212,34 @@ template jacobianMatrixXML(Integer indexJacobian, list<JacobianColumn> jacobianC
>>
end jacobianMatrixXML;

template zeroCrossLength(SimCode simCode)
::=
match simCode
case SIMCODE(modelInfo = MODELINFO(varInfo = vi as VARINFO(__))) then
let size = listLength(zeroCrossings)
<<
<%intSub(listLength(zeroCrossings), vi.numTimeEvents)%>
>>
end zeroCrossLength;

template timeEventLength(SimCode simCode)
::=
match simCode
case SIMCODE(modelInfo = MODELINFO(varInfo = vi as VARINFO(__))) then
<<
<%vi.numTimeEvents%>
>>
end timeEventLength;

template eventIndicatorsLength(SimCode simCode)
::=
match simCode
case SIMCODE(modelInfo = MODELINFO(varInfo = vi as VARINFO(__))) then
let size = listLength(zeroCrossings)
<<
<%listLength(zeroCrossings)%>
>>
end eventIndicatorsLength;

annotation(__OpenModelica_Interface="backend");
end CodegenCppInit;

0 comments on commit 41e9fb0

Please sign in to comment.