Skip to content

Commit e37a229

Browse files
author
Vitalij Ruge
committed
- CodegenC for Optimica
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@16765 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
1 parent 88b5afd commit e37a229

File tree

6 files changed

+118
-60
lines changed

6 files changed

+118
-60
lines changed

Compiler/BackEnd/SimCode.mo

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,8 @@ uniontype Context
545545
end PARALLEL_FUNCTION_CONTEXT;
546546
record ZEROCROSSINGS_CONTEXT
547547
end ZEROCROSSINGS_CONTEXT;
548+
record OPTIMIZATION
549+
end OPTIMIZATION;
548550
end Context;
549551

550552

@@ -557,6 +559,7 @@ public constant Context contextAlgloop = ALGLOOP_CONTEXT(false);
557559
public constant Context contextOther = OTHER();
558560
public constant Context contextParallelFunction = PARALLEL_FUNCTION_CONTEXT();
559561
public constant Context contextZeroCross = ZEROCROSSINGS_CONTEXT();
562+
public constant Context contextOptimization = OPTIMIZATION();
560563

561564

562565
/****** HashTable ComponentRef -> SimCode.SimVar ******/

Compiler/Template/CodegenC.tpl

Lines changed: 72 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -228,9 +228,9 @@ template simulationFile(SimCode simCode, String guid)
228228
<%functionInitialMixedSystems(initialEquations, inlineEquations, parameterEquations, allEquations, collectAllJacobianEquations(jacobianMatrixes))%>
229229
230230
<%functionAnalyticJacobians(jacobianMatrixes)%>
231-
232-
<%objectiveFunction(classAttributes, simCode)%>
233-
231+
<%if acceptOptimicaGrammar() then optimizationComponents(classAttributes, simCode) else
232+
"int mayer(DATA* data, modelica_real* res){return -1;}
233+
int lagrange(DATA* data, modelica_real* res){return -1;}"%>
234234
<%functionlinearmodel(modelInfo)%>
235235
236236
#ifdef __cplusplus
@@ -7343,6 +7343,19 @@ template daeExpRelationSim(Exp exp, Context context, Text &preExp /*BUFP*/,
73437343
match exp
73447344
case rel as RELATION(__) then
73457345
match context
7346+
case OPTIMIZATION(__) then
7347+
let e1 = daeExp(rel.exp1, context, &preExp /*BUFC*/, &varDecls /*BUFC*/)
7348+
let e2 = daeExp(rel.exp2, context, &preExp /*BUFC*/, &varDecls /*BUFC*/)
7349+
match rel.operator
7350+
case LESS(__) then
7351+
'res[i++] = (<%e1%> - <%e2%>) ;<%\n%>'
7352+
case LESSEQ(__) then
7353+
'res[i++] = (<%e1%> - <%e2%>) ;<%\n%>'
7354+
case GREATER(__) then
7355+
'res[i++] = (<%e2%> - <%e1%>) ;<%\n%>'
7356+
case GREATEREQ(__) then
7357+
'res[i++] = (<%e2%> - <%e1%>) ;<%\n%>'
7358+
end match
73467359
case SIMULATION(__) then
73477360
match rel.optionExpisASUB
73487361
case NONE() then
@@ -9591,14 +9604,14 @@ end endModelicaLine;
95919604
* SECTION: GENERATE OPTIMIZATION IN SIMULATION FILE
95929605
*****************************************************************************/
95939606

9594-
template objectiveFunction( list<DAE.ClassAttributes> classAttributes ,SimCode simCode)
9607+
template optimizationComponents( list<DAE.ClassAttributes> classAttributes ,SimCode simCode)
95959608
"Generates C for Objective Functions."
95969609
::=
9597-
(classAttributes |> classAttribute => classAttributes(classAttribute,simCode); separator="\n")
9610+
(classAttributes |> classAttribute => optimizationComponents1(classAttribute,simCode); separator="\n")
95989611

9599-
end objectiveFunction;
9612+
end optimizationComponents;
96009613

9601-
template classAttributes(ClassAttributes classAttribute, SimCode simCode)
9614+
template optimizationComponents1(ClassAttributes classAttribute, SimCode simCode)
96029615
"Generates C for class attributes of objective function."
96039616
::=
96049617
match classAttribute
@@ -9611,67 +9624,81 @@ template classAttributes(ClassAttributes classAttribute, SimCode simCode)
96119624
let objectiveFunction = match objetiveE
96129625
case SOME(exp) then
96139626
<<
9614-
&res = <%daeExp(exp, contextSimulationNonDiscrete, &preExp /*BUFC*/, &varDecls /*BUFD*/)%>
9627+
*res = <%daeExp(exp, contextSimulationNonDiscrete, &preExp /*BUFC*/, &varDecls /*BUFD*/)%>;
9628+
return 0;
96159629
>>
9630+
96169631
let objectiveIntegrand = match objectiveIntegrandE case SOME(exp) then
96179632
<<
9618-
&res =<%daeExp(exp, contextSimulationNonDiscrete, &preExp1 /*BUFC*/, &varDecls1 /*BUFD*/)%>
9619-
>>
9620-
let constraints = match simCode case SIMCODE(modelInfo = MODELINFO(__)) then constraints(constraints)
9633+
*res= <%daeExp(exp, contextSimulationNonDiscrete, &preExp1 /*BUFC*/, &varDecls1 /*BUFD*/)%>;
9634+
return 0;
9635+
>>
9636+
let listConstraintsLength = match simCode case SIMCODE(modelInfo = MODELINFO(__)) then listLength(constraints)
9637+
9638+
let constraints = match simCode case SIMCODE(modelInfo = MODELINFO(__)) then pathConstraints(constraints)
96219639
<<
9622-
/* objectiveFunction */
9623-
int mayer(DATA* data, modelica_real* res)
9624-
{
9625-
<%varDecls%>
9626-
<%preExp%>
9627-
<%objectiveFunction%>;
9628-
return 0;
9629-
}
9640+
/* objectiveFunction */
96309641

9631-
/* objectiveIntegrand */
9632-
int lagrange(DATA* data, modelica_real* res)
9633-
{
9634-
<%varDecls1%>
9635-
<%preExp1%>
9636-
<%objectiveIntegrand%>;
9637-
return 0;
9638-
}
9639-
9640-
/* constraints */
9641-
/* ToDo
9642-
int pathContrainst(DATA* data, modelica_real* res)
9643-
{
9644-
<%constraints%>
9645-
}
9646-
*/
9642+
int mayer(DATA* data, modelica_real* res)
9643+
{
9644+
<%varDecls%>
9645+
<%preExp%>
9646+
<%objectiveFunction%>
9647+
return -1;
9648+
}
9649+
9650+
/* objectiveIntegrand */
9651+
int lagrange(DATA* data, modelica_real* res)
9652+
{
9653+
<%varDecls1%>
9654+
<%preExp1%>
9655+
<%objectiveIntegrand%>
9656+
return -1;
9657+
}
9658+
9659+
/* constraints */
9660+
int pathConstraints(DATA* data, modelica_real* res, int* N)
9661+
{
9662+
if(*N < 0)
9663+
{
9664+
*N = <%listConstraintsLength%>;
9665+
return 1;
9666+
}
9667+
<%constraints%>
9668+
return 0;
9669+
}
96479670
>>
96489671
else error(sourceInfo(), 'Unknown Constraint List')
9649-
end classAttributes;
9672+
end optimizationComponents1;
96509673

9651-
template constraints( list<DAE.Constraint> constraints)
9674+
template pathConstraints( list<DAE.Constraint> constraints)
96529675
"Generates C for Optimization."
96539676
::=
9654-
(constraints |> constraint => constraint(constraint); separator="\n")
9677+
(constraints |> constraint => pathConstraint(constraint); separator="\n")
96559678

9656-
end constraints;
9679+
end pathConstraints;
96579680

9658-
template constraint(Constraint cons)
9681+
template pathConstraint(Constraint cons)
96599682
"Generates C for List of Constraints."
96609683
::=
96619684
match cons
96629685
case CONSTRAINT_EXPS(__) then
96639686
let &varDecls = buffer "" /*BUFD*/
96649687
let &preExp = buffer "" /*BUFD*/
96659688
let constrain = (constraintLst |> constraint =>
9666-
daeExp(constraint, contextSimulationDiscrete, &preExp /*BUFC*/, &varDecls /*BUFD*/)
9689+
daeExp(constraint, contextOptimization, &preExp /*BUFC*/, &varDecls /*BUFD*/)
96679690
;separator="\n")
96689691
<<
9669-
<%varDecls%>
9670-
<%preExp%>
9671-
<%constrain%>
9692+
else
9693+
{
9694+
int i = 0;
9695+
<%varDecls%>
9696+
<%preExp%>
9697+
<%constrain%>
9698+
}
96729699
>>
96739700
else error(sourceInfo(), 'Unknown Constraint List')
9674-
end constraint;
9701+
end pathConstraint;
96759702

96769703
end CodegenC;
96779704

Compiler/Template/SimCodeTV.mo

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,8 @@ package SimCode
223223
end PARALLEL_FUNCTION_CONTEXT;
224224
record ZEROCROSSINGS_CONTEXT
225225
end ZEROCROSSINGS_CONTEXT;
226+
record OPTIMIZATION
227+
end OPTIMIZATION;
226228
end Context;
227229

228230
uniontype HpcOmParInformation
@@ -556,10 +558,10 @@ package SimCode
556558
constant Context contextFunction;
557559
constant Context contextOther;
558560
constant Context contextAlgloop;
559-
constant Context contextAlgloopInitialisation;
560-
561+
constant Context contextAlgloopInitialisation;
561562
constant Context contextParallelFunction;
562563
constant Context contextZeroCross;
564+
constant Context contextOptimization;
563565
constant list<DAE.Exp> listExpLength1;
564566

565567
end SimCode;
@@ -2477,6 +2479,10 @@ package Config
24772479
function acceptParModelicaGrammar
24782480
output Boolean outBoolean;
24792481
end acceptParModelicaGrammar;
2482+
2483+
function acceptOptimicaGrammar
2484+
output Boolean outBoolean;
2485+
end acceptOptimicaGrammar;
24802486

24812487
function noProc
24822488
output Integer n;

SimulationRuntime/c/simulation/simulation_runtime.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@
8383
*/
8484
#ifndef NO_INTERACTIVE_DEPENDENCY
8585
#include "../../interactive/omi_ServiceInterface.h"
86-
#endif
86+
#endif
8787

8888

8989
using namespace std;
@@ -980,6 +980,7 @@ static void omc_assert_simulation(FILE_INFO info, const char *msg, ...)
980980
longjmp(integratorJmpbuf,1);
981981
break;
982982
case ERROR_EVENTSEARCH:
983+
case ERROR_OPTIMIZE:
983984
/* Ignore asserts for event search, since to find events we need to
984985
* step over in regions, which may trigger asserts.
985986
*/

SimulationRuntime/c/simulation/solver/solver_main.c

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ static int rungekutta_step(DATA* data, SOLVER_INFO* solverInfo);
7373

7474
static int radau_lobatto_step(DATA* data, SOLVER_INFO* solverInfo);
7575

76+
#ifdef WITH_IPOPT
77+
static int ipopt_step(DATA* data, SOLVER_INFO* solverInfo);
78+
#endif
79+
7680
static void writeOutputVars(char* names, DATA* data);
7781

7882
int solver_main_step(DATA* data, SOLVER_INFO* solverInfo)
@@ -89,7 +93,10 @@ int solver_main_step(DATA* data, SOLVER_INFO* solverInfo)
8993
functionODE_inline(data, solverInfo->currentStepSize);
9094
solverInfo->currentTime = data->localData[0]->timeValue;
9195
return 0;
92-
96+
#ifdef WITH_IPOPT
97+
case 5:
98+
return ipopt_step(data, solverInfo);
99+
#endif
93100
#ifdef WITH_SUNDIALS
94101
case 6:
95102
return radau_lobatto_step(data, solverInfo);
@@ -162,13 +169,10 @@ int initializeSolverData(DATA* data, SOLVER_INFO* solverInfo)
162169
for(i = 0; i < inline_work_states_ndims; i++)
163170
work_states[i] = (double*) calloc(data->modelData.nVariablesReal, sizeof(double));
164171
}
165-
#ifdef PATHCONSTRAINTS
172+
#ifdef WITH_IPOPT
166173
else if(solverInfo->solverMethod == 5)
167174
{
168-
int neqns = -1;
169-
/* Allocate work array for optimization*/
170-
pathConstraints(data, NULL, &neqns);
171-
/* allocateDaeIpopt(data,neqns); */
175+
solverInfo->solverData = NULL;
172176
}
173177
#endif
174178
#ifdef WITH_SUNDIALS
@@ -253,6 +257,13 @@ int freeSolverData(DATA* data, SOLVER_INFO* solverInfo)
253257
free(work_states[i]);
254258
free(work_states);
255259
}
260+
#ifdef WITH_IPOPT
261+
else if(solverInfo->solverMethod == 5)
262+
{
263+
/* free work arrays */
264+
/*destroyIpopt(solverInfo);*/
265+
}
266+
#endif
256267
#ifdef WITH_SUNDIALS
257268
else if(solverInfo->solverMethod == 6)
258269
{
@@ -513,7 +524,7 @@ int solver_main(DATA* data, const char* init_initMethod,
513524
case S_DASSLINTERNALNUMJAC:
514525
solverInfo.solverMethod = S_DASSL;
515526
break;
516-
527+
517528
#ifndef WITH_SUNDIALS
518529
case S_RADAU1:
519530
case S_RADAU3:
@@ -524,14 +535,14 @@ int solver_main(DATA* data, const char* init_initMethod,
524535
WARNING(LOG_STDOUT, "Sundial/kinsol is needed but not available. Please choose other solver.");
525536
return 1;
526537
#endif
527-
528-
case S_OPTIMIZATION:
538+
529539
#ifndef WITH_IPOPT
530-
WARNING(LOG_STDOUT, "Ipopt is needed but not available.");
540+
case S_OPTIMIZATION:
541+
WARNING(LOG_STDOUT, "Ipopt is needed but not available.");
542+
return 1;
531543
#endif
532-
WARNING(LOG_STDOUT, "optimization is not supported yet");
533-
return 1;
534544

545+
535546
case S_INLINE_EULER:
536547
if(!_omc_force_solver || strcmp(_omc_force_solver, "inline-euler"))
537548
{
@@ -637,6 +648,15 @@ static int rungekutta_step(DATA* data, SOLVER_INFO* solverInfo)
637648
return 0;
638649
}
639650

651+
/*************************************** Run Ipopt for optimization ***********************************/
652+
#ifdef WITH_IPOPT
653+
static int ipopt_step(DATA* data, SOLVER_INFO* solverInfo)
654+
{
655+
WARNING(LOG_STDOUT, "optimization is not supported yet");
656+
return -1;
657+
}
658+
#endif
659+
640660
#ifdef WITH_SUNDIALS
641661
/*************************************** Radau/Lobatto ***********************************/
642662
int radau_lobatto_step(DATA* data, SOLVER_INFO* solverInfo)

SimulationRuntime/c/util/omc_error.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ enum ERROR_HANDLE
8383
ERROR_INTEGRATOR,
8484
ERROR_NONLINEARSOLVER,
8585
ERROR_EVENTSEARCH,
86+
ERROR_OPTIMIZE,
8687

8788
ERROR_MAX
8889
};

0 commit comments

Comments
 (0)