Skip to content

Commit

Permalink
added getJacobian matrix method for an Jacobain matrix index to cpp r…
Browse files Browse the repository at this point in the history
…untime
  • Loading branch information
niklwors authored and OpenModelica-Hudson committed Jul 9, 2015
1 parent 6327e13 commit d53f8f5
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Compiler/BackEnd/Tearing.mo
Expand Up @@ -248,7 +248,7 @@ algorithm
end if;
false = Flags.getConfigBool(Flags.DISABLE_LINEAR_TEARING);
// TODO: Remove when cpp runtime ready for doLinearTearing
false = stringEqual(Config.simCodeTarget(), "Cpp");
//false = stringEqual(Config.simCodeTarget(), "Cpp");
if Flags.isSet(Flags.TEARING_DUMP) or Flags.isSet(Flags.TEARING_DUMPVERBOSE) then
print("Flag 'doLinearTearing' is set\n\n");
end if;
Expand Down
59 changes: 55 additions & 4 deletions Compiler/Template/CodegenCpp.tpl
Expand Up @@ -51,7 +51,7 @@ template translateModel(SimCode simCode)
let()= textFile(simulationStateSelectionCppFile(simCode , &extraFuncs , &extraFuncsDecl, "", stateDerVectorName, false), 'OMCpp<%fileNamePrefix%>StateSelection.cpp')
let()= textFile(simulationStateSelectionHeaderFile(simCode , &extraFuncs , &extraFuncsDecl, ""),'OMCpp<%fileNamePrefix%>StateSelection.h')
let()= textFile(simulationExtensionHeaderFile(simCode , &extraFuncs , &extraFuncsDecl, ""),'OMCpp<%fileNamePrefix%>Extension.h')
let()= textFile(simulationExtensionCppFile(simCode , &extraFuncs , &extraFuncsDecl, ""),'OMCpp<%fileNamePrefix%>Extension.cpp')
let()= textFile(simulationExtensionCppFile(simCode , &extraFuncs , &extraFuncsDecl, "", stateDerVectorName, false),'OMCpp<%fileNamePrefix%>Extension.cpp')
let()= textFile(simulationWriteOutputHeaderFile(simCode , &extraFuncs , &extraFuncsDecl, ""),'OMCpp<%fileNamePrefix%>WriteOutput.h')
let()= textFile(simulationWriteOutputCppFile(simCode , &extraFuncs , &extraFuncsDecl, "", stateDerVectorName, false),'OMCpp<%fileNamePrefix%>WriteOutput.cpp')
let()= textFile(simulationWriteOutputAlgVarsCppFile(simCode , &extraFuncs , &extraFuncsDecl, "", stateDerVectorName, false),'OMCpp<%fileNamePrefix%>WriteOutputAlgVars.cpp')
Expand Down Expand Up @@ -416,6 +416,7 @@ case SIMCODE(modelInfo=MODELINFO(vars = vars as SIMVARS(__))) then
virtual IHistory* getHistory();
/// Provide Jacobian
virtual void getJacobian(SparseMatrix& matrix);
virtual void getJacobian(SparseMatrix& matrix,unsigned int index);
virtual void getStateSetJacobian(unsigned int index,SparseMatrix& matrix);
/// Called to handle all events occured at same time
virtual bool handleSystemEvents(bool* events);
Expand Down Expand Up @@ -649,7 +650,7 @@ match simCode
case SIMCODE(modelInfo = MODELINFO(__)) then
let initialjacMats = (jacobianMatrixes |> (mat, vars, name, (sparsepattern,_), colorList, _, jacIndex) =>
initialAnalyticJacobians(jacIndex, mat, vars, name, sparsepattern, colorList,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace)
;separator="";empty)
;separator="/*test <%jacIndex%>*/";empty)
<<

<% (jacobianMatrixes |> (mat, _, _, _, _, _, _) hasindex index0 =>
Expand Down Expand Up @@ -949,11 +950,15 @@ case modelInfo as MODELINFO(vars=SIMVARS(__)) then
>>
end simulationWriteOutputAliasVarsCppFile;

template simulationExtensionCppFile(SimCode simCode ,Text& extraFuncs,Text& extraFuncsDecl,Text extraFuncsNamespace)
template simulationExtensionCppFile(SimCode simCode ,Text& extraFuncs,Text& extraFuncsDecl,Text extraFuncsNamespace, Text stateDerVectorName /*=__zDot*/, Boolean useFlatArrayNotation)
"Generates code for main cpp file for simulation target."
::=
match simCode
case SIMCODE(modelInfo = MODELINFO(vars=SIMVARS(__))) then
case SIMCODE(modelInfo = MODELINFO(__)) then

let getJacobianForIndexMethods = (jacobianMatrixes |> (mat, _,name, _,colorList, _, jacIndex) =>
generateJacobianForIndex (simCode,mat,colorList,jacIndex, name, &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation)
;separator="\n\n";empty)
let classname = lastIdentOfPath(modelInfo.name)
<<

Expand Down Expand Up @@ -995,6 +1000,17 @@ case SIMCODE(modelInfo = MODELINFO(vars=SIMVARS(__))) then

}

void <%classname%>Extension::getJacobian(SparseMatrix& matrix,unsigned int index)
{
switch (index)
{
<%getJacobianForIndexMethods%>
}
}




void <%classname%>Extension::getStateSetJacobian(unsigned int index,SparseMatrix& matrix)
{
switch (index)
Expand Down Expand Up @@ -1117,6 +1133,41 @@ case SIMCODE(modelInfo = MODELINFO(vars=SIMVARS(__))) then
end simulationExtensionCppFile;


template generateJacobianForIndex(SimCode simCode, list<JacobianColumn> jacobianColumn, list<list<Integer>> colorList,Integer indexJacobian, String matrixName,Text& extraFuncs,Text& extraFuncsDecl,Text extraFuncsNamespace, Text stateDerVectorName /*=__zDot*/, Boolean useFlatArrayNotation)
"Generates Matrixes for Linear Model."
::=
match simCode
case SIMCODE(modelInfo = MODELINFO(__)) then

let classname = lastIdentOfPath(modelInfo.name)
match jacobianColumn
case {} then
<<
>>
case _ then
match colorList
case {} then
<<

>>
case _ then

<<

case <%indexJacobian%>:
{
get<%matrixName%>Jacobian(matrix);
break;
}
>>

/*
(match indexColumn case "1" then ' _<%matrixName%>jacobian(<%crefWithoutIndexOperator(cref)%>$pDER<%matrixName%>$indexdiff,0) = _<%matrixName%>jac_y(0); //1 <%cref(cref)%>'
else ' _<%matrixName%>jacobian(<%index0%>,<%crefWithoutIndexOperator(cref)%>$pDER<%matrixName%>$indexdiff) = _<%matrixName%>jac_y(<%crefWithoutIndexOperator(cref)%>$pDER<%matrixName%>$indexdiff);//2 <%cref(cref)%>'

*/
end generateJacobianForIndex;

template functionDimStateSets(list<StateSet> stateSets,SimCode simCode ,Text& extraFuncs,Text& extraFuncsDecl,Text extraFuncsNamespace)
"Generates functions in simulation file to initialize the stateset data."
::=
Expand Down
2 changes: 1 addition & 1 deletion Compiler/Template/CodegenCppHpcom.tpl
Expand Up @@ -73,7 +73,7 @@ template translateModel(SimCode simCode)
let() = textFile(simulationStateSelectionCppFile(simCode, &extraFuncs, &extraFuncsDecl, "", stateDerVectorName, false), 'OMCpp<%fileNamePrefix%>StateSelection.cpp')
let() = textFile(simulationStateSelectionHeaderFile(simCode, &extraFuncs, &extraFuncsDecl, ""), 'OMCpp<%fileNamePrefix%>StateSelection.h')
let() = textFile(simulationExtensionHeaderFile(simCode, &extraFuncs, &extraFuncsDecl, ""), 'OMCpp<%fileNamePrefix%>Extension.h')
let() = textFile(simulationExtensionCppFile(simCode, &extraFuncs, &extraFuncsDecl, ""), 'OMCpp<%fileNamePrefix%>Extension.cpp')
let()= textFile(simulationExtensionCppFile(simCode , &extraFuncs , &extraFuncsDecl, "", stateDerVectorName, false),'OMCpp<%fileNamePrefix%>Extension.cpp')
let() = textFile(simulationWriteOutputHeaderFile(simCode, &extraFuncs, &extraFuncsDecl, ""), 'OMCpp<%fileNamePrefix%>WriteOutput.h')
let() = textFile(simulationWriteOutputCppFile(simCode, &extraFuncs, &extraFuncsDecl, "", stateDerVectorName, false), 'OMCpp<%fileNamePrefix%>WriteOutput.cpp')
let() = textFile(simulationWriteOutputAlgVarsCppFile(simCode, &extraFuncs, &extraFuncsDecl, "", stateDerVectorName, false), 'OMCpp<%fileNamePrefix%>WriteOutputAlgVars.cpp')
Expand Down
1 change: 1 addition & 0 deletions SimulationRuntime/cpp/Include/Core/System/IMixedSystem.h
Expand Up @@ -27,6 +27,7 @@ class IMixedSystem
virtual ~IMixedSystem() {};
/// Provide Jacobian
virtual void getJacobian(SparseMatrix& matrix) = 0;
virtual void getJacobian(SparseMatrix& matrix,unsigned int index) = 0;
virtual void getStateSetJacobian(unsigned int index, SparseMatrix& matrix) = 0;
/// Called to handle all events occured at same time
virtual bool handleSystemEvents(bool* events) = 0;
Expand Down

0 comments on commit d53f8f5

Please sign in to comment.