Skip to content

Commit

Permalink
- generate objective function in C for optimization
Browse files Browse the repository at this point in the history
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@14853 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Vitalij Ruge committed Jan 21, 2013
1 parent 64fb040 commit 650e9fb
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 0 deletions.
15 changes: 15 additions & 0 deletions Compiler/BackEnd/BackendDump.mo
Expand Up @@ -81,6 +81,7 @@ protected import Util;
// - printEquationArray
// - printEquationList
// - printEquations
// - printClassAttributes
// - printShared
// - printStateSets
// - printVar
Expand Down Expand Up @@ -204,6 +205,20 @@ algorithm
end match;
end printEquationNo;

public function printClassAttributes "function printClassAttributes
This unction print the Optimica ClassAttributes: objetiveE, objetiveE"
input DAE.ClassAttributes optimicaFun;
protected
Option<DAE.Exp> e1,e2;
algorithm
DAE.OPTIMIZATION_ATTRS(objetiveE = e1, objectiveIntegrandE = e2) := optimicaFun;
print("Mayer" +& "\n" +& UNDERLINE +& "\n\n");
print(ExpressionDump.printOptExpStr(e1));
print("Lagrange" +& "\n" +& UNDERLINE +& "\n\n");
print(ExpressionDump.printOptExpStr(e2));
print("\n");
end printClassAttributes;

public function printShared "function printShared
This function dumps the BackendDAE.Shared representaton to stdout."
input BackendDAE.Shared inShared;
Expand Down
87 changes: 87 additions & 0 deletions Compiler/Template/CodegenC.tpl
Expand Up @@ -193,6 +193,7 @@ template simulationFile(SimCode simCode, String guid)
<%functionAssertsforCheck(algorithmAndEquationAsserts)%>
<%functionAnalyticJacobians(jacobianMatrixes)%>
<%objectiveFunction(classAttributes, simCode)%>
<%functionlinearmodel(modelInfo)%>
Expand Down Expand Up @@ -9016,6 +9017,92 @@ template endModelicaLine()
'<% if boolOr(acceptMetaModelicaGrammar(), Flags.isSet(Flags.GEN_DEBUG_SYMBOLS)) then "/*#endModelicaLine*/"%>'
end endModelicaLine;

/*****************************************************************************
* SECTION: GENERATE OPTIMIZATION IN SIMULATION FILE
*****************************************************************************/

template objectiveFunction( list<DAE.ClassAttributes> classAttributes ,SimCode simCode)
"Generates C for Objective Functions."
::=
(classAttributes |> classAttribute => classAttributes(classAttribute,simCode); separator="\n")

end objectiveFunction;

template classAttributes(ClassAttributes classAttribute, SimCode simCode)
"Generates C for class attributes of objective function."
::=
match classAttribute
case OPTIMIZATION_ATTRS(__) then
let &varDecls = buffer "" /*BUFD*/
let &preExp = buffer "" /*BUFD*/
let &varDecls1 = buffer "" /*BUFD*/
let &preExp1 = buffer "" /*BUFD*/

let objectiveFunction = match objetiveE
case SOME(exp) then
<<
&res = <%daeExp(exp, contextSimulationNonDiscrete, &preExp /*BUFC*/, &varDecls /*BUFD*/)%>
>>
let objectiveIntegrand = match objectiveIntegrandE case SOME(exp) then
<<
&res =<%daeExp(exp, contextSimulationNonDiscrete, &preExp1 /*BUFC*/, &varDecls1 /*BUFD*/)%>
>>
let constraints = match simCode case SIMCODE(modelInfo = MODELINFO(__)) then constraints(constraints)
<<
/* objectiveFunction */
int mayer(DATA* data, modelica_real* res)
{
<%varDecls%>
<%preExp%>
<%objectiveFunction%>;
return 0;
}

/* objectiveIntegrand */
int lagrange(DATA* data, modelica_real* res)
{
<%varDecls1%>
<%preExp1%>
<%objectiveIntegrand%>;
return 0;
}

/* constraints */
/* ToDo
int pathContrainst(DATA* data, modelica_real* res)
{
<%constraints%>
}
*/
>>
else error(sourceInfo(), 'Unknown Constraint List')
end classAttributes;

template constraints( list<DAE.Constraint> constraints)
"Generates C for Optimization."
::=
(constraints |> constraint => constraint(constraint); separator="\n")

end constraints;

template constraint(Constraint cons)
"Generates C for List of Constraints."
::=
match cons
case CONSTRAINT_EXPS(__) then
let &varDecls = buffer "" /*BUFD*/
let &preExp = buffer "" /*BUFD*/
let constrain = (constraintLst |> constraint =>
daeExp(constraint, contextSimulationDiscrete, &preExp /*BUFC*/, &varDecls /*BUFD*/)
;separator="\n")
<<
<%varDecls%>
<%preExp%>
<%constrain%>
>>
else error(sourceInfo(), 'Unknown Constraint List')
end constraint;

end CodegenC;

// vim: filetype=susan sw=2 sts=2

0 comments on commit 650e9fb

Please sign in to comment.