Skip to content

Commit c310fe4

Browse files
author
Marcus Walther
committed
- fix for CPP runtime
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@21166 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
1 parent 270650f commit c310fe4

File tree

1 file changed

+143
-26
lines changed

1 file changed

+143
-26
lines changed

Compiler/Template/CodegenCpp.tpl

Lines changed: 143 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1478,7 +1478,6 @@ case SIMCODE(modelInfo = MODELINFO(__)) then
14781478
match eq
14791479
case SES_LINEAR(__)
14801480
case SES_NONLINEAR(__) then
1481-
14821481
<<
14831482
#include "Modelica.h"
14841483
#include "ModelicaDefine.h"
@@ -1494,23 +1493,40 @@ match eq
14941493
,_system(system)
14951494
,__z(z)
14961495
,__zDot(zDot)
1496+
,__A(0)
1497+
,__Asparse(0)
1498+
<%alocateLinearSystemConstructor(eq)%>
14971499
,_conditions(conditions)
14981500
,_event_handling(event_handling)
1499-
<%alocateLinearSystem(eq)%>
1500-
1501+
,_useSparseFormat(false)
15011502
{
1502-
15031503
<%initAlgloopDimension(eq,varDecls)%>
15041504
15051505
}
15061506
15071507
<%modelname%>Algloop<%index%>::~<%modelname%>Algloop<%index%>()
15081508
{
1509+
if(__Asparse != 0)
1510+
delete __Asparse;
1511+
if(__A != 0)
1512+
delete __A;
1513+
}
1514+
1515+
bool <%modelname%>Algloop<%index%>::getUseSparseFormat()
1516+
{
1517+
return _useSparseFormat;
1518+
}
15091519
1520+
void <%modelname%>Algloop<%index%>::setUseSparseFormat(bool value)
1521+
{
1522+
_useSparseFormat = value;
15101523
}
1524+
15111525
<%algloopRHSCode(simCode,eq)%>
15121526
<%if Flags.isSet(Flags.WRITE_TO_BUFFER) then algloopResiduals(simCode,eq)%>
15131527
<%initAlgloop(simCode,eq,context)%>
1528+
<%initAlgloopTemplate(simCode,eq,context)%>
1529+
<%updateAlgloop(simCode,eq,context)%>
15141530
<%upateAlgloopNonLinear(simCode,eq,context)%>
15151531
<%upateAlgloopLinear(simCode,eq,context)%>
15161532
<%AlgloopDefaultImplementationCode(simCode,eq,context)%>
@@ -1521,6 +1537,47 @@ match eq
15211537
>>
15221538
end algloopCppFile;
15231539
1540+
template updateAlgloop(SimCode simCode,SimEqSystem eqn,Context context)
1541+
::=
1542+
match simCode
1543+
case SIMCODE(modelInfo = MODELINFO(__)) then
1544+
let modelname = lastIdentOfPath(modelInfo.name)
1545+
match eqn
1546+
case eq as SES_NONLINEAR(__) then
1547+
<<
1548+
void <%modelname%>Algloop<%index%>::evaluate()
1549+
{
1550+
if(_useSparseFormat)
1551+
evaluate(__Asparse);
1552+
else
1553+
evaluate(__A);
1554+
}
1555+
>>
1556+
case eq as SES_LINEAR(__) then
1557+
<<
1558+
void <%modelname%>Algloop<%index%>::evaluate()
1559+
{
1560+
if(_useSparseFormat)
1561+
{
1562+
if(__Asparse == 0)
1563+
{
1564+
//sometimes initialize was not called before
1565+
<%alocateLinearSystem(eq)%>
1566+
}
1567+
evaluate(__Asparse);
1568+
}
1569+
else
1570+
{
1571+
if(__A == 0)
1572+
{
1573+
//sometimes initialize was not called before
1574+
<%alocateLinearSystem(eq)%>
1575+
}
1576+
evaluate(__A);
1577+
}
1578+
}
1579+
>>
1580+
end updateAlgloop;
15241581
15251582
template upateAlgloopNonLinear( SimCode simCode,SimEqSystem eqn,Context context)
15261583
"Generates functions in simulation file."
@@ -1552,7 +1609,8 @@ case SIMCODE(modelInfo = MODELINFO(__)) then
15521609
15531610
15541611
<<
1555-
void <%modelname%>Algloop<%index%>::evaluate()
1612+
template <typename T>
1613+
void <%modelname%>Algloop<%index%>::evaluate(T *__A)
15561614
{
15571615
<%varDecls%>
15581616
@@ -1607,7 +1665,7 @@ case SIMCODE(modelInfo = MODELINFO(__)) then
16071665
let Amatrix=
16081666
(simJac |> (row, col, eq as SES_RESIDUAL(__)) =>
16091667
let expPart = daeExp(eq.exp, context, &preExp /*BUFC*/, &varDecls /*BUFD*/,simCode)
1610-
'<%preExp%>__A[<%row%>][<%col%>]=<%expPart%>;'
1668+
'<%preExp%>(*__A)[<%row%>][<%col%>]=<%expPart%>;'
16111669
;separator="\n")
16121670
16131671
@@ -1620,7 +1678,8 @@ case SIMCODE(modelInfo = MODELINFO(__)) then
16201678
16211679
16221680
<<
1623-
void <%modelname%>Algloop<%index%>::evaluate()
1681+
template <typename T>
1682+
void <%modelname%>Algloop<%index%>::evaluate(T* __A)
16241683
{
16251684
<%varDecls%>
16261685
<%Amatrix%>
@@ -3006,6 +3065,40 @@ end functionInitialEquations;
30063065
template initAlgloop(SimCode simCode,SimEqSystem eq,Context context)
30073066
::=
30083067
match simCode
3068+
case SIMCODE(modelInfo = MODELINFO(__)) then
3069+
let modelname = lastIdentOfPath(modelInfo.name)
3070+
3071+
match eq
3072+
case SES_NONLINEAR(__) then
3073+
<<
3074+
void <%modelname%>Algloop<%index%>::initialize()
3075+
{
3076+
3077+
if(_useSparseFormat)
3078+
<%modelname%>Algloop<%index%>::initialize(__Asparse);
3079+
else
3080+
<%modelname%>Algloop<%index%>::initialize(__A);
3081+
}
3082+
>>
3083+
case SES_LINEAR(__) then
3084+
<<
3085+
void <%modelname%>Algloop<%index%>::initialize()
3086+
{
3087+
<%alocateLinearSystem(eq)%>
3088+
if(_useSparseFormat)
3089+
<%modelname%>Algloop<%index%>::initialize(__Asparse);
3090+
else
3091+
{
3092+
fill_array(*__A,0.0);
3093+
<%modelname%>Algloop<%index%>::initialize(__A);
3094+
}
3095+
}
3096+
>>
3097+
end initAlgloop;
3098+
3099+
template initAlgloopTemplate(SimCode simCode,SimEqSystem eq,Context context)
3100+
::=
3101+
match simCode
30093102
case SIMCODE(modelInfo = MODELINFO(__)) then
30103103
let modelname = lastIdentOfPath(modelInfo.name)
30113104
let &varDecls = buffer ""
@@ -3015,10 +3108,10 @@ case SIMCODE(modelInfo = MODELINFO(__)) then
30153108
match eq
30163109
case SES_NONLINEAR(__) then
30173110
<<
3018-
void <%modelname%>Algloop<%index%>::initialize()
3111+
template <typename T>
3112+
void <%modelname%>Algloop<%index%>::initialize(T *__A)
30193113
{
3020-
3021-
<%initAlgloopEquation(eq,varDecls,simCode,context)%>
3114+
<%initAlgloopEquation(eq,varDecls,simCode,context)%>
30223115
AlgLoopDefaultImplementation::initialize();
30233116
30243117
// Update the equations once before start of simulation
@@ -3027,16 +3120,15 @@ case SIMCODE(modelInfo = MODELINFO(__)) then
30273120
>>
30283121
case SES_LINEAR(__) then
30293122
<<
3030-
void <%modelname%>Algloop<%index%>::initialize()
3123+
template <typename T>
3124+
void <%modelname%>Algloop<%index%>::initialize(T *__A)
30313125
{
3032-
3033-
<%initAlgloopEquation(eq,varDecls,simCode,context)%>
3126+
<%initAlgloopEquation(eq,varDecls,simCode,context)%>
30343127
// Update the equations once before start of simulation
30353128
evaluate();
30363129
}
30373130
>>
3038-
3039-
end initAlgloop;
3131+
end initAlgloopTemplate;
30403132
30413133
30423134
template getAMatrixCode(SimCode simCode,SimEqSystem eq)
@@ -3060,7 +3152,7 @@ case SIMCODE(modelInfo = MODELINFO(__)) then
30603152
<<
30613153
void <%modelname%>Algloop<%index%>::getSystemMatrix(double* A_matrix)
30623154
{
3063-
memcpy(A_matrix,__A.data(),_dimAEq*_dimAEq*sizeof(double));
3155+
memcpy(A_matrix,__A->data(),_dimAEq*_dimAEq*sizeof(double));
30643156
}
30653157
>>
30663158
@@ -3111,7 +3203,7 @@ match eq
31113203
31123204
void <%modelname%>Algloop<%index%>::getRHS(double* vars)
31133205
{
3114-
ublas::matrix<double> A=toMatrix(_dimAEq,_dimAEq,__A.data());
3206+
ublas::matrix<double> A=toMatrix(_dimAEq,_dimAEq,__A->data());
31153207
double* doubleUnknowns = new double[_dimAEq];
31163208
getReal(doubleUnknowns);
31173209
ublas::vector<double> x=toVector(_dimAEq,doubleUnknowns);
@@ -3216,7 +3308,7 @@ case SES_NONLINEAR(__) then
32163308
let Amatrix=
32173309
(simJac |> (row, col, eq as SES_RESIDUAL(__)) =>
32183310
let expPart = daeExp(eq.exp, context, &preExp /*BUFC*/, &varDecls /*BUFD*/,simCode)
3219-
'<%preExp%>__A[<%row%>][<%col%>]=<%expPart%>;'
3311+
'<%preExp%>(*__A)[<%row%>][<%col%>]=<%expPart%>;'
32203312
;separator="\n")
32213313
32223314
let bvector = (beqs |> exp hasindex i0 =>
@@ -3398,7 +3490,6 @@ case SES_NONLINEAR(__) then
33983490
<<
33993491
// Number of unknowns/equations according to type (0: double, 1: int, 2: bool)
34003492
_dimAEq = <%size%>;
3401-
fill_array(__A,0.0);
34023493
fill_array(__b,0.0);
34033494
>>
34043495
@@ -3408,14 +3499,26 @@ template alocateLinearSystem(SimEqSystem eq)
34083499
"Generates a non linear equation system."
34093500
::=
34103501
match eq
3502+
case SES_LINEAR(__) then
3503+
let size = listLength(vars)
3504+
<<
3505+
if(_useSparseFormat)
3506+
__Asparse = 0;
3507+
else
3508+
__A = new boost::multi_array<double,2>(boost::extents[<%size%>][<%size%>],boost::fortran_storage_order());
3509+
>>
3510+
end alocateLinearSystem;
3511+
3512+
template alocateLinearSystemConstructor(SimEqSystem eq)
3513+
"Generates a non linear equation system."
3514+
::=
3515+
match eq
34113516
case SES_LINEAR(__) then
34123517
let size = listLength(vars)
34133518
<<
3414-
,__A(boost::extents[<%size%>][<%size%>],boost::fortran_storage_order())
34153519
,__b(boost::extents[<%size%>])
34163520
>>
3417-
3418-
end alocateLinearSystem;
3521+
end alocateLinearSystemConstructor;
34193522
34203523
template Update(SimCode simCode)
34213524
::=
@@ -3764,21 +3867,34 @@ case SIMCODE(modelInfo = MODELINFO(__)) then
37643867
37653868
<%generateAlgloopMethodDeclarationCode(simCode)%>
37663869
3870+
bool getUseSparseFormat();
3871+
void setUseSparseFormat(bool value);
3872+
3873+
protected:
3874+
template <typename T>
3875+
void evaluate(T* __A);
3876+
37673877
private:
37683878
Functions _functions;
37693879
//states
37703880
double* __z;
37713881
//state derivatives
37723882
double* __zDot;
37733883
// A matrix
3774-
boost::multi_array<double,2> __A;
3884+
boost::multi_array<double,2> *__A; //dense
3885+
3886+
typedef double** sparseType;
3887+
sparseType *__Asparse; //sparse
3888+
37753889
//b vector
37763890
boost::multi_array<double,1> __b;
37773891
bool* _conditions;
37783892
37793893
EventHandling& _event_handling;
37803894
37813895
<%systemname%>* _system;
3896+
3897+
bool _useSparseFormat;
37823898
};
37833899
>>
37843900
end generateAlgloopClassDeclarationCode;
@@ -4095,6 +4211,10 @@ case SIMCODE(modelInfo = MODELINFO(__)) then
40954211
virtual int getDimRHS() const ;
40964212
/// (Re-) initialize the system of equations
40974213
virtual void initialize();
4214+
4215+
template <typename T>
4216+
void initialize(T *__A);
4217+
40984218
/// Provide variables with given index to the system
40994219
virtual void getReal(double* vars) ;
41004220
/// Provide variables with given index to the system
@@ -5474,9 +5594,6 @@ template getAliasVarName(AliasVariable aliasvar, SimCode simCode,Context context
54745594
else 'noAlias'
54755595
end getAliasVarName;
54765596
5477-
5478-
5479-
54805597
//template for write variables for each time step
54815598
template writeoutput2(ModelInfo modelInfo,SimCode simCode)
54825599

0 commit comments

Comments
 (0)