Skip to content

Commit

Permalink
- first version of initialization XML for c++ simulation runtime added
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcus Walther committed Jun 16, 2015
1 parent de1412c commit b2d7e9f
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 9 deletions.
14 changes: 10 additions & 4 deletions Compiler/Template/CodegenCpp.tpl
Expand Up @@ -2,7 +2,7 @@ package CodegenCpp

import interface SimCodeTV;
import CodegenUtil.*;
// SECTION: SIMULATION TARGET, ROOT TEMPLATE
import CodegenCppInit.*;



Expand Down Expand Up @@ -58,6 +58,7 @@ template translateModel(SimCode simCode)
let()= textFile(simulationWriteOutputParameterCppFile(simCode , &extraFuncs , &extraFuncsDecl, "", false),'OMCpp<%fileNamePrefix%>WriteOutputParameter.cpp')
let()= textFile(simulationWriteOutputAliasVarsCppFile(simCode , &extraFuncs , &extraFuncsDecl, "", stateDerVectorName, false),'OMCpp<%fileNamePrefix%>WriteOutputAliasVars.cpp')
let()= textFile(simulationFactoryFile(simCode , &extraFuncs , &extraFuncsDecl, ""),'OMCpp<%fileNamePrefix%>FactoryExport.cpp')
let()= textFile(modelInitXMLFile(simCode, numRealVars, numIntVars, numBoolVars),'OMCpp<%fileNamePrefix%>Init.xml')
let()= textFile(simulationMainRunScript(simCode , &extraFuncs , &extraFuncsDecl, "", "", "", "exec"), '<%fileNamePrefix%><%simulationMainRunScriptSuffix(simCode , &extraFuncs , &extraFuncsDecl, "")%>')
let jac = (jacobianMatrixes |> (mat, _, _, _, _, _, _) =>
(mat |> (eqs,_,_) => algloopfiles(eqs,simCode , &extraFuncs , &extraFuncsDecl, "",contextAlgloopJacobian, stateDerVectorName, false) ;separator="")
Expand Down Expand Up @@ -2137,6 +2138,7 @@ template calcHelperMainfile(SimCode simCode ,Text& extraFuncs,Text& extraFuncsDe
#include <Core/Modelica.h>
#include <Core/System/FactoryExport.h>
#include <Core/DataExchange/SimData.h>
#include <Core/DataExchange/XmlPropertyReader.h>
#include <Core/System/SimVars.h>
#include <Core/System/DiscreteEvents.h>
#include <Core/System/EventHandling.h>
Expand Down Expand Up @@ -2611,7 +2613,7 @@ case "gcc" then

MODELICA_EXTERNAL_LIBS=-lModelicaExternalC -lModelicaStandardTables -L$(LAPACK_LIBS) $(LAPACK_LIBRARIES)
LDSYSTEMFLAGS_COMMON=-L"$(OMHOME)/lib/<%getTriple()%>/omc/cpp" $(BASE_LIB) <%additionalLinkerFlags_GCC%> -Wl,-rpath,"$(OMHOME)/lib/<%getTriple()%>/omc/cpp" <%timeMeasureLink%> -L"$(BOOST_LIBS)" $(BOOST_LIBRARIES) $(LINUX_LIB_DL)
LDSYSTEMFLAGS_DYNAMIC=-lOMCppSystem -lOMCppModelicaUtilities -lOMCppMath -lOMCppExtensionUtilities -lOMCppOMCFactory $(LDSYSTEMFLAGS_COMMON)
LDSYSTEMFLAGS_DYNAMIC=-lOMCppSystem -lOMCppModelicaUtilities -lOMCppMath -lOMCppExtensionUtilities -lOMCppOMCFactory -lOMCppDataExchange $(LDSYSTEMFLAGS_COMMON)
LDSYSTEMFLAGS_STATIC=<%staticLibs%> $(LDSYSTEMFLAGS_COMMON)
<%if(Flags.isSet(Flags.RUNTIME_STATIC_LINKING)) then 'LDSYSTEMFLAGS=$(LDSYSTEMFLAGS_STATIC)' else 'LDSYSTEMFLAGS=$(LDSYSTEMFLAGS_DYNAMIC)'%>

Expand Down Expand Up @@ -5067,10 +5069,14 @@ case SIMCODE(modelInfo = MODELINFO(__)) then


initializeMemory();

initializeFreeVariables();
IPropertyReader *reader = new XmlPropertyReader("OMCpp<%fileNamePrefix%>Init.xml");
reader->readInitialValues(_sim_vars);
//initializeFreeVariables();
initializeStringParameterVars();
initializeStringAliasVars();
initializeBoundVariables();
saveAll();
delete reader;
}

void <%lastIdentOfPath(modelInfo.name)%>Initialize::initializeMemory()
Expand Down
151 changes: 151 additions & 0 deletions Compiler/Template/CodegenCppInit.tpl
@@ -0,0 +1,151 @@
package CodegenCppInit

import interface SimCodeTV;
import CodegenUtil.*;

template modelInitXMLFile(SimCode simCode, String numRealVars, String numIntVars, String numBoolVars)
::=
match simCode
case SIMCODE(modelInfo = MODELINFO(__)) then
let variables = modelVariablesXML(modelInfo, varToArrayIndexMapping, '<%numRealVars%> - 1', '<%numIntVars%> - 1', '<%numBoolVars%> - 1')
let algLoops = (listAppend(allEquations,initialEquations) |> eq => algLoopXML(eq, simCode, varToArrayIndexMapping, '<%numRealVars%> - 1') ;separator="\n")
let jacobianMatrixes = jacobianMatrixesXML(simCode.jacobianMatrixes)
<<
<?xml version="1.0" encoding="UTF8"?>
<ModelDescription modelName="<%dotPath(modelInfo.name)%>">
<ModelVariables>
<%variables%>
</ModelVariables>
<AlgLoops>
<%algLoops%>
</AlgLoops>
<Jacobian>
<%jacobianMatrixes%>
</Jacobian>
</ModelDescription>
>>
end modelInitXMLFile;

template modelVariablesXML(ModelInfo modelInfo, HashTableCrIListArray.HashTable varToArrayIndexMapping, String indexForUndefinedReferencesReal, String indexForUndefinedReferencesInt, String indexForUndefinedReferencesBool)
"Generates the xml code for the variable defintions."
::=
match modelInfo
case MODELINFO(vars=SIMVARS(__),varInfo=VARINFO(numAlgVars= numAlgVars, numDiscreteReal = numDiscreteReal, numOptimizeConstraints = numOptimizeConstraints)) then
<<
<%vars.stateVars |> var => scalarVariableXML(var, varToArrayIndexMapping, indexForUndefinedReferencesReal) ;separator="\n";empty%>
<%vars.derivativeVars |> var => scalarVariableXML(var, varToArrayIndexMapping, indexForUndefinedReferencesReal) ;separator="\n";empty%>
<%vars.algVars |> var => scalarVariableXML(var, varToArrayIndexMapping, indexForUndefinedReferencesReal) ;separator="\n";empty%>
<%vars.discreteAlgVars |> var => scalarVariableXML(var, varToArrayIndexMapping, indexForUndefinedReferencesReal) ;separator="\n";empty%>
<%/*vars.realOptimizeConstraintsVars
|> var hasindex i0 => scalarVariableXML(var,varToArrayIndexMapping, indexForUndefinedReferencesReal) ;separator="\n";empty*/%>
<%/*vars.realOptimizeFinalConstraintsVars
|> var => scalarVariableXML(var,varToArrayIndexMapping, indexForUndefinedReferencesReal) ;separator="\n";empty*/%>
<%vars.paramVars |> var => scalarVariableXML(var,varToArrayIndexMapping, indexForUndefinedReferencesReal) ;separator="\n";empty%>
<%vars.aliasVars |> var => scalarVariableXML(var,varToArrayIndexMapping, indexForUndefinedReferencesReal) ;separator="\n";empty%>

<%vars.intAlgVars |> var => scalarVariableXML(var,varToArrayIndexMapping, indexForUndefinedReferencesInt) ;separator="\n";empty%>
<%vars.intParamVars |> var => scalarVariableXML(var,varToArrayIndexMapping, indexForUndefinedReferencesInt) ;separator="\n";empty%>
<%vars.intAliasVars |> var => scalarVariableXML(var,varToArrayIndexMapping, indexForUndefinedReferencesInt) ;separator="\n";empty%>

<%vars.boolAlgVars |> var => scalarVariableXML(var,varToArrayIndexMapping, indexForUndefinedReferencesBool) ;separator="\n";empty%>
<%vars.boolParamVars |> var => scalarVariableXML(var,varToArrayIndexMapping, indexForUndefinedReferencesBool) ;separator="\n";empty%>
<%vars.boolAliasVars |> var => scalarVariableXML(var,varToArrayIndexMapping, indexForUndefinedReferencesBool) ;separator="\n";empty%>
>>
/*
<%vars.stringAlgVars |> var hasindex i0 => ScalarVariable(var,i0,"sAlg") ;separator="\n";empty%>
<%vars.stringParamVars |> var hasindex i0 => ScalarVariable(var,i0,"sPar") ;separator="\n";empty%>
<%vars.stringAliasVars |> var hasindex i0 => ScalarVariable(var,i0,"sAli") ;separator="\n";empty%>
*/
end modelVariablesXML;

template scalarVariableXML(SimVar simVar, HashTableCrIListArray.HashTable varToArrayIndexMapping, String indexForUndefinedReferences)
"Generates code for ScalarVariable file for FMU target."
::=
match simVar
case SIMVAR(__) then
<<
<ScalarVariable <%scalarVariableAttributeXML(simVar, varToArrayIndexMapping, indexForUndefinedReferences)%>>
<%ScalarVariableType(unit, displayUnit, minValue, maxValue, initialValue, nominalValue, isFixed, type_)%>
</ScalarVariable>
>>
end scalarVariableXML;

template scalarVariableAttributeXML(SimVar simVar, HashTableCrIListArray.HashTable varToArrayIndexMapping, String indexForUndefinedReferences)
"Generates code for ScalarVariable Attribute file for FMU target."
::=
match simVar
case SIMVAR(source = SOURCE(info = info)) then
let valueReference = SimCodeUtil.getVarIndexListByMapping(varToArrayIndexMapping,name,indexForUndefinedReferences)
let variability = getVariablity(varKind)
let description = if comment then 'description = "<%Util.escapeModelicaStringToXmlString(comment)%>"'
<<
name = "<%Util.escapeModelicaStringToXmlString(crefStrNoUnderscore(name))%>" valueReference = "<%valueReference%>" <%description%> variability = "<%variability%>" isProtected = "<%isProtected%>"
>>
end scalarVariableAttributeXML;

template algLoopXML(SimEqSystem eqs, SimCode simCode, HashTableCrIListArray.HashTable varToArrayIndexMapping, String indexForUndefinedReferences)
::=
<<
<%
match(eqs)
case(SES_LINEAR(lSystem = ls as LINEARSYSTEM(__))) then
<<
<Linear eqIdx="<%ls.index%>" sparse="true" size="<%listLength(ls.vars)%>">
<Vars>
<%ls.vars |> v as SIMVAR(__) => '<Var type="double" index="<%SimCodeUtil.getVarIndexListByMapping(varToArrayIndexMapping,v.name,indexForUndefinedReferences)%>" />' ;separator="\n"%>
</Vars>
</Linear>
>>
case(SES_NONLINEAR(nlSystem = nls as NONLINEARSYSTEM(__))) then
<<
<NonLinear eqIdx="<%nls.index%>" size="<%listLength(nls.crefs)%>">
<Vars>
<%nls.crefs |> name => '<Var type="double" index="<%SimCodeUtil.getVarIndexListByMapping(varToArrayIndexMapping,name,indexForUndefinedReferences)%>" />' ;separator="\n"%>
</Vars>
<NominalVars>
<!-- Maybe Expressions here -->
</NominalVars>
</NonLinear>
>>
else
''
%>
>>
end algLoopXML;

template jacobianMatrixesXML(list<JacobianMatrix> JacobianMatrixes)
::=
let jacMats = (JacobianMatrixes |> (mat, vars, name, (sparsepattern,_), colorList, maxColor, jacIndex) =>
jacobianMatrixXML(jacIndex, mat, vars, name, sparsepattern, colorList, maxColor)
;separator="\n";empty)
<<
<%jacMats%>
>>
end jacobianMatrixesXML;

template jacobianMatrixXML(Integer indexJacobian, list<JacobianColumn> jacobianColumn, list<SimVar> seedVars, String matrixName, list<tuple<Integer,list<Integer>>> sparsepattern, list<list<Integer>> colorList, Integer maxColor)
::=
let indexColumn = (jacobianColumn |> (eqs,vars,indxColumn) => indxColumn; separator="\n")
let jacvals = (sparsepattern |> (index,indexes) hasindex index0 =>
'<Column>
<%(indexes |> i_index hasindex index1 =>
(
match indexColumn case "1" then '<Entry indexX="<%index%>" indexY="0" valueIndex="0"/>'
else '<Entry indexX="<%index%>" indexY="<%i_index%>" valueIndex="<%i_index%>"/>'
);separator="\n"
)%>
</Column>'
;separator="\n"
)
<<
<Matrix name="<%matrixName%>">
<Column>
<%jacvals%>
</Column>
</Matrix>
>>
end jacobianMatrixXML;


annotation(__OpenModelica_Interface="backend");
end CodegenCppInit;
11 changes: 8 additions & 3 deletions Compiler/Template/Makefile.common
@@ -1,6 +1,6 @@
.PHONY : all

GENERATED_FILES=AbsynDumpTpl.mo CodegenUtil.mo CodegenC.mo CodegenFMUCommon.mo CodegenFMU.mo CodegenFMU1.mo CodegenFMU2.mo CodegenCSharp.mo CodegenQSS.mo CodegenCpp.mo CodegenCppHpcom.mo CodegenFMUCpp.mo CodegenModelica.mo DAEDumpTpl.mo ExpressionDumpTpl.mo GraphvizDump.mo GraphMLDumpTpl.mo NFInstDumpTpl.mo SimCodeDump.mo Unparsing.mo SCodeDumpTpl.mo CodegenAdevs.mo CodegenSparseFMI.mo CodegenXML.mo CodegenJava.mo CodegenJS.mo TplCodegen.mo TaskSystemDump.mo GenerateAPIFunctionsTpl.mo VisualXMLTpl.mo
GENERATED_FILES=AbsynDumpTpl.mo CodegenUtil.mo CodegenC.mo CodegenFMUCommon.mo CodegenFMU.mo CodegenFMU1.mo CodegenFMU2.mo CodegenCSharp.mo CodegenQSS.mo CodegenCpp.mo CodegenCppHpcom.mo CodegenFMUCpp.mo CodegenCppInit.mo CodegenModelica.mo DAEDumpTpl.mo ExpressionDumpTpl.mo GraphvizDump.mo GraphMLDumpTpl.mo NFInstDumpTpl.mo SimCodeDump.mo Unparsing.mo SCodeDumpTpl.mo CodegenAdevs.mo CodegenSparseFMI.mo CodegenXML.mo CodegenJava.mo CodegenJS.mo TplCodegen.mo TaskSystemDump.mo GenerateAPIFunctionsTpl.mo VisualXMLTpl.mo

all : $(GENERATED_FILES)

Expand Down Expand Up @@ -82,13 +82,13 @@ CodegenQSS.mo : CodegenQSS.tpl SimCodeTV.mo CodegenC.tpl CodegenUtil.tpl
$(OMC) $< > $@.log || (cat $@.log && false)
@echo " "

CodegenCpp.mo : CodegenCpp.tpl SimCodeTV.mo
CodegenCpp.mo : CodegenCpp.tpl SimCodeTV.mo CodegenUtil.tpl CodegenCppInit.tpl
@echo " ** CodegenCpp template compilation ** "
$(OMC) $< > $@.log || (cat $@.log && false)
@echo " "

CodegenCppHpcom.mo : CodegenCppHpcom.tpl SimCodeTV.mo CodegenCpp.tpl CodegenUtil.tpl
@echo " ** CodegenCppHpcom.mo template compilation ** "
@echo " ** CodegenCppHpcom template compilation ** "
$(OMC) $< > $@.log || (cat $@.log && false)
@echo " "

Expand All @@ -97,6 +97,11 @@ CodegenFMUCpp.mo : CodegenFMUCpp.tpl SimCodeTV.mo CodegenC.tpl CodegenUtil.tpl C
$(OMC) $< > $@.log || (cat $@.log && false)
@echo " "

CodegenCppInit.mo : CodegenCppInit.tpl SimCodeTV.mo CodegenUtil.tpl
@echo " ** CodegenCppInit template compilation ** "
$(OMC) $< > $@.log || (cat $@.log && false)
@echo " "

ExpressionDumpTpl.mo : ExpressionDumpTpl.tpl ExpressionDumpTV.mo DAEDumpTpl.tpl
@echo " ** ExpressionDumpTpl template compilation ** "
$(OMC) $< > $@.log || (cat $@.log && false)
Expand Down
1 change: 1 addition & 0 deletions Compiler/boot/LoadCompilerSources.mos
Expand Up @@ -205,6 +205,7 @@ if true then /* Suppress output */
"../Template/CodegenFMU2.mo",
"../Template/CodegenFMUCommon.mo",
"../Template/CodegenFMUCpp.mo",
"../Template/CodegenCppInit.mo",
"../Template/CodegenCSharp.mo",
"../Template/CodegenJava.mo",
"../Template/CodegenJS.mo",
Expand Down
6 changes: 4 additions & 2 deletions SimulationRuntime/cpp/Core/DataExchange/CMakeLists.txt
Expand Up @@ -2,7 +2,7 @@ cmake_minimum_required (VERSION 2.8.6)

project(${DataExchangeName})

add_library(${DataExchangeName} SHARED SimData.cpp FactoryExport.cpp )
add_library(${DataExchangeName} SHARED SimData.cpp FactoryExport.cpp XmlPropertyReader.cpp)

IF(NOT BOOST_STATIC_LINKING)
target_link_libraries (${DataExchangeName} ${Boost_LIBRARIES})
Expand All @@ -11,7 +11,7 @@ ENDIF(NOT BOOST_STATIC_LINKING)
include_directories(${SUNDIALS_INCLUDE_DIR}/cvodes ${SUNDIALS_INCLUDE_DIR}/nvector ${SUNDIALS_INCLUDE_DIR}/sundials ${SUNDIALS_INCLUDE_DIR})
add_precompiled_header(${DataExchangeName} Include/Core/Modelica.h)

add_library(${DataExchangeName}_static STATIC SimData.cpp )
add_library(${DataExchangeName}_static STATIC SimData.cpp XmlPropertyReader.cpp)

IF(NOT BOOST_STATIC_LINKING)
target_link_libraries (${DataExchangeName}_static ${Boost_LIBRARIES})
Expand All @@ -29,6 +29,8 @@ install (FILES ${CMAKE_SOURCE_DIR}/Include/Core/DataExchange/IHistory.h
${CMAKE_SOURCE_DIR}/Include/Core/DataExchange/SimData.h
${CMAKE_SOURCE_DIR}/Include/Core/DataExchange/Writer.h
${CMAKE_SOURCE_DIR}/Include/Core/DataExchange/SimDouble.h
${CMAKE_SOURCE_DIR}/Include/Core/DataExchange/XmlPropertyReader.h
${CMAKE_SOURCE_DIR}/Include/Core/DataExchange/IPropertyReader.h
DESTINATION include/omc/cpp/Core/DataExchange)


Expand Down
1 change: 1 addition & 0 deletions SimulationRuntime/cpp/Include/Core/Modelica.h
Expand Up @@ -125,6 +125,7 @@ typedef uBlas::compressed_matrix<double, uBlas::row_major, 0, uBlas::unbounded_a
#include <Core/Math/ArrayOperations.h>
#include <Core/Math/ArraySlice.h>
#include <Core/Math/Utility.h>
#include <Core/DataExchange/IPropertyReader.h>
#include <Core/DataExchange/Writer.h>
#include <Core/DataExchange/Policies/TextfileWriter.h>
#include <Core/DataExchange/Policies/MatfileWriter.h>
Expand Down

0 comments on commit b2d7e9f

Please sign in to comment.