Skip to content

Commit

Permalink
- fix bugs for runtime cpp
Browse files Browse the repository at this point in the history
- skip warning from initialisation about self generated variables 

git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@12710 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Jens Frenkel committed Aug 28, 2012
1 parent be5aa99 commit e67d806
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 28 deletions.
94 changes: 76 additions & 18 deletions Compiler/Template/CodegenCpp.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ case SIMCODE(modelInfo=MODELINFO(__)) then
#pragma once
#include "Math/Implementation/ArrayOperations.h"
#include "Math/Implementation/Functions.h"
#include "LibrariesConfig.h"
#include "Math/Implementation/Utility.h"
#include "LibrariesConfig.h"
/*****************************************************************************
*
* Simulation code for FunctionCall functions generated by the OpenModelica Compiler.
Expand Down Expand Up @@ -1213,11 +1214,11 @@ match var
case var as VARIABLE(__) then
match value
case SOME(CREF(componentRef = cr)) then
'copy_<%expTypeShort(var.ty)%>_array_data(&<%contextCref(cr,contextFunction,simCode)%>, &<%outStruct%>.targTest9<%i%>);<%\n%>'
'<%contextCref(cr,contextFunction,simCode)%> = <%outStruct%>.targTest9<%i%><%\n%>'
case SOME(arr as ARRAY(__)) then
let arrayExp = '<%daeExp(arr, contextFunction, &varInits /*BUFC*/, &varDecls /*BUFD*/,simCode)%>'
<<
copy_<%expTypeShort(var.ty)%>_array_data(&<%arrayExp%>, &<%lhsVarName%>);<%\n%>
<%lhsVarName%> = <%arrayExp%>;<%\n%>
>>
end varDefaultValue;
Expand Down Expand Up @@ -2963,12 +2964,9 @@ case SIMCODE(modelInfo = MODELINFO(varInfo = vi as VARINFO(__), vars = vars as S
end simulationInitFile;
template initVals(list<SimVar> varsLst,SimCode simCode) ::=
varsLst |> SIMVAR(numArrayElement={}) =>
varsLst |> SIMVAR(numArrayElement={},initialValue=SOME(v)) =>
<<
,<%cref(name)%>(<%match initialValue
case SOME(v) then initVal(v)
else "0"
%>)
,<%cref(name)%>(<%initVal(v)%>)
>>
;separator="\n"
end initVals;
Expand Down Expand Up @@ -4683,11 +4681,11 @@ template daeExpCall(Exp call, Context context, Text &preExp /*BUFP*/,
case CALL(path=IDENT(name="cross"), expLst={v1, v2},attr=CALL_ATTR(ty=ty as T_ARRAY(dims=dims))) then
let var1 = daeExp(v1, context, &preExp /*BUFC*/, &varDecls /*BUFD*/,simCode)
let var2 = daeExp(v2, context, &preExp /*BUFC*/, &varDecls /*BUFD*/,simCode)
let type = match ty case T_ARRAY(ty=T_INTEGER(__)) then 'multi_array<int,<%listLength(dims)%>>'
case T_ARRAY(ty=T_ENUMERATION(__)) then 'multi_array<int,<%listLength(dims)%>>'
else 'multi_array<double,<%listLength(dims)%>>'
let tvar = tempDecl(type, &varDecls /*BUFD*/)
let &preExp += '<%tvar%> = cross_array(<%var1%>,<%var2%>);<%\n%>'
let type = match ty case T_ARRAY(ty=T_INTEGER(__)) then 'int'
case T_ARRAY(ty=T_ENUMERATION(__)) then 'int'
else 'double'
let tvar = tempDecl('multi_array<<%type%>,<%listLength(dims)%>>', &varDecls /*BUFD*/)
let &preExp += '<%tvar%> = cross_array<<%type%>>(<%var1%>,<%var2%>);<%\n%>'
'<%tvar%>'

case CALL(path=IDENT(name="identity"), expLst={A}) then
Expand Down Expand Up @@ -4951,7 +4949,7 @@ template daeExpBinary(Operator it, Exp exp1, Exp exp2, Context context, Text &pr
let type = match ty case T_ARRAY(ty=T_INTEGER(__)) then 'int>'
case T_ARRAY(ty=T_ENUMERATION(__)) then 'int'
else 'double'
'dot_array(<%e1%>, <%e2%>)'
'dot_array<<%type%>>(<%e1%>, <%e2%>)'
case DIV_SCALAR_ARRAY(__) then "daeExpBinary:ERR DIV_SCALAR_ARRAY not supported"
case POW_ARRAY_SCALAR(__) then "daeExpBinary:ERR POW_ARRAY_SCALAR not supported"
case POW_SCALAR_ARRAY(__) then "daeExpBinary:ERR POW_SCALAR_ARRAY not supported"
Expand Down Expand Up @@ -4994,7 +4992,10 @@ case UNARY(__) then
match operator
case UMINUS(__) then '(-<%e%>)'
case UMINUS_ARR(ty=T_ARRAY(ty=T_REAL(__))) then
let &preExp += 'usub_real_array(&<%e%>);<%\n%>'
let &preExp += 'usub_array<double,<%listLength(ty.dims)%>>(<%e%>);<%\n%>'
'<%e%>'
case UMINUS_ARR(ty=T_ARRAY(ty=T_INTEGER(__))) then
let &preExp += 'usub_array<int,<%listLength(ty.dims)%>>(<%e%>);<%\n%>'
'<%e%>'
case UMINUS_ARR(__) then 'unary minus for non-real arrays not implemented'
else "daeExpUnary:ERR"
Expand Down Expand Up @@ -5380,7 +5381,7 @@ template daeExpIf(Exp cond, Exp then_, Exp else_, Context context, Text &preExp,
end daeExpIf;

template expTypeFromExpArrayIf(Exp exp) ::=
expTypeFromExp(exp)
expTypeFlag(typeof(exp),6)
end expTypeFromExpArrayIf;

template expTypeFromExp(Exp it) ::=
Expand All @@ -5397,7 +5398,7 @@ template expTypeFromExp(Exp it) ::=
case RELATION(__) then "bool" //TODO: a HACK, it was expTypeFromOp(operator)
case IFEXP(__) then expTypeFromExp(expThen)
case CALL(attr=CALL_ATTR(__)) then expTypeShort(attr.ty)
case ARRAY(__)
case ARRAY(__)
case MATRIX(__)
case RANGE(__)
case CAST(__)
Expand Down Expand Up @@ -6703,7 +6704,7 @@ template algStatement(DAE.Statement stmt, Context context, Text &varDecls,SimCod
let res = match stmt
case s as STMT_ASSIGN(exp1=PATTERN(__)) then "STMT_ASSIGN Pattern not supported yet"
case s as STMT_ASSIGN(__) then algStmtAssign(s, context, &varDecls /*BUFD*/,simCode)
case s as STMT_ASSIGN_ARR(__) then "STMT ASSIGN ARR"
case s as STMT_ASSIGN_ARR(__) then algStmtAssignArr(s, context, &varDecls /*BUFD*/,simCode)
case s as STMT_TUPLE_ASSIGN(__) then algStmtTupleAssign(s, context, &varDecls /*BUFD*/,simCode)
case s as STMT_IF(__) then algStmtIf(s, context, &varDecls /*BUFD*/,simCode)
case s as STMT_FOR(__) then algStmtFor(s, context, &varDecls /*BUFD*/,simCode)
Expand Down Expand Up @@ -7103,4 +7104,61 @@ case RANGE(__) then
}
>>
end algStmtForRange_impl;


template algStmtAssignArr(DAE.Statement stmt, Context context,
Text &varDecls /*BUFP*/,SimCode simCode)
"Generates an array assigment algorithm statement."
::=
match stmt
case STMT_ASSIGN_ARR(exp=RANGE(__), componentRef=cr, type_=t) then
<<
STMT_ASSIGN_ARR RANGE
fillArrayFromRange(t,exp,cr,context,&varDecls)
>>
case STMT_ASSIGN_ARR(exp=e as CALL(__), componentRef=cr, type_=t) then
let &preExp = buffer "" /*BUFD*/
let expPart = daeExp(e, context, &preExp /*BUFC*/, &varDecls /*BUFD*/, simCode)
let ispec = indexSpecFromCref(cr, context, &preExp /*BUFC*/, &varDecls /*BUFD*/,simCode)
if ispec then
<<
STMT_ASSIGN_ARR CALL ispec
<%preExp%>
indexedAssign(t, expPart, cr, ispec, context, &varDecls)
>>
else
<<
STMT_ASSIGN_ARR CALL
<%preExp%>
copyArrayDataAndFreeMemAfterCall(t, expPart, cr, context)
>>
case STMT_ASSIGN_ARR(exp=e, componentRef=cr, type_=t) then
let &preExp = buffer "" /*BUFD*/
let expPart = daeExp(e, context, &preExp /*BUFC*/, &varDecls /*BUFD*/, simCode)
let ispec = indexSpecFromCref(cr, context, &preExp /*BUFC*/, &varDecls /*BUFD*/,simCode)
if ispec then
<<
STMT_ASSIGN_ARR cr ispec
<%preExp%>
indexedAssign(t, expPart, cr, ispec, context, &varDecls)
>>
else
<<
<%preExp%>
<%arrayCrefStr(cr)%> = <%expPart%>;
>>
end algStmtAssignArr;

template indexSpecFromCref(ComponentRef cr, Context context, Text &preExp /*BUFP*/,
Text &varDecls /*BUFP*/,SimCode simCode)
"Helper to algStmtAssignArr.
Currently works only for CREF_IDENT."
::=
match cr
case CREF_IDENT(subscriptLst=subs as (_ :: _)) then
daeExpCrefRhsIndexSpec(subs, context, &preExp /*BUFC*/, &varDecls /*BUFD*/,simCode)
end indexSpecFromCref;



end CodegenCpp;
8 changes: 6 additions & 2 deletions SimulationRuntime/c/simulation/solver/initialization.c
Original file line number Diff line number Diff line change
Expand Up @@ -588,8 +588,12 @@ static int importStartValues(DATA *data, const char* pInitFile, double initTime)
omc_matlab4_val(&(mData->realVarsData[i].attribute.start), &reader, pVar, initTime);
DEBUG_INFO_AL2(LOG_INIT, "| %s(start=%g)", mData->realVarsData[i].info.name, mData->realVarsData[i].attribute.start);
}
else
WARNING1("unable to import real variable %s from given file", mData->realVarsData[i].info.name);
else {
/* skipp warnings about self generated variables */
if ((stringcmp(mData->realVarsData[i].info.name,"$ZERO.") != 0) || DEBUG_FLAG(LOG_INIT) ||
(stringcmp(mData->realVarsData[i].info.name,"$pDER.") != 0))
WARNING1("unable to import real variable %s from given file", mData->realVarsData[i].info.name);
}
}

DEBUG_INFO(LOG_INIT, "import real parameters");
Expand Down
25 changes: 17 additions & 8 deletions SimulationRuntime/cpp/Source/Math/Implementation/ArrayOperations.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,12 @@ boost::multi_array< T, dims > add_array( boost::multi_array_ref< T, dims > a ,
/**
scalar product of two arrays (a,b type as template parameter)
*/
//template < typename T >
double dot_array( boost::multi_array_ref< double, 1 > a , boost::multi_array_ref< double, 1 > b )
template < typename T >
T dot_array( boost::multi_array_ref< T, 1 > a , boost::multi_array_ref< T, 1 > b )
{
double tmp = 0;
boost::multi_array< double, 1 >::const_iterator j = b.begin();
for ( boost::multi_array< double, 1 >::iterator i = a.begin(); i != a.end(); i++, j++ )
T tmp = 0;
typename boost::multi_array< T, 1 >::const_iterator j = b.begin();
for ( typename boost::multi_array< T, 1 >::iterator i = a.begin(); i != a.end(); i++, j++ )
tmp += (*i) * (*j);

return tmp;
Expand All @@ -140,16 +140,25 @@ double dot_array( boost::multi_array_ref< double, 1 > a , boost::multi_array_re
/**
cross product of two arrays (a,b type as template parameter)
*/
//template < typename T >
boost::multi_array< double, 1 > cross_array( boost::multi_array_ref< double, 1 > a , boost::multi_array_ref< double, 1 > b )
template < typename T >
boost::multi_array< T, 1 > cross_array( boost::multi_array_ref< T, 1 > a , boost::multi_array_ref< T, 1 > b )
{
boost::multi_array<double, 1> res(boost::extents[3]);
typename boost::multi_array<T, 1> res(boost::extents[3]);
res[1] = (a[2] * b[3]) - (a[3] * b[2]);
res[2] = (a[3] * b[1]) - (a[1] * b[3]);
res[3] = (a[1] * b[2]) - (a[2] * b[1]);
return res;
};

/**
- array (a)
*/
template < typename T, size_t dims >
void usub_array(boost::multi_array_ref< T, dims > &a)
{
for ( typename boost::multi_array< T, dims >::iterator i = a.begin(); i != a.end(); i++)
(*i) = -(*i);
}

/**
Applies array operation F (*,/) on array
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ add_library(${MathName} SHARED ArrayOperations.cpp )
install (TARGETS ${MathName} DESTINATION lib/omc)
install (FILES Functions.h
ArrayOperations.h
Utility.h
DESTINATION include/omc/cpp/Math/Implementation)
install (FILES ../Interfaces/ILapack.h
DESTINATION include/omc/cpp/Math/Interfaces)
Expand Down
51 changes: 51 additions & 0 deletions SimulationRuntime/cpp/Source/Math/Implementation/Utility.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* This file is part of OpenModelica.
*
* Copyright (c) 1998-CurrentYear, Linköping University,
* Department of Computer and Information Science,
* SE-58183 Linköping, Sweden.
*
* All rights reserved.
*
* THIS PROGRAM IS PROVIDED UNDER THE TERMS OF GPL VERSION 3
* AND THIS OSMC PUBLIC LICENSE (OSMC-PL).
* ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES RECIPIENT'S
* ACCEPTANCE OF THE OSMC PUBLIC LICENSE.
*
* The OpenModelica software and the Open Source Modelica
* Consortium (OSMC) Public License (OSMC-PL) are obtained
* from Linköping University, either from the above address,
* from the URLs: http://www.ida.liu.se/projects/OpenModelica or
* http://www.openmodelica.org, and in the OpenModelica distribution.
* GNU version 3 is obtained from: http://www.gnu.org/copyleft/gpl.html.
*
* This program is distributed WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH
* IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS
* OF OSMC-PL.
*
* See the full OSMC Public License conditions for more details.
*
*/


#ifndef UTILITY_H
#define UTILITY_H

#include <math.h>


/* fmod in math.h does not work in the same way as mod defined by modelica, so
* we need to define our own mod. */
static inline double modelica_mod_double(double x, double y)
{
return (x - (floor(x/y) * y));
}

static inline int modelica_mod_integer(int x, int y)
{
return x % y;
}

#endif

0 comments on commit e67d806

Please sign in to comment.