Skip to content

Commit

Permalink
- fix bugs for simCodeTarget Cpp
Browse files Browse the repository at this point in the history
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@12705 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Jens Frenkel committed Aug 28, 2012
1 parent 3bf0dfc commit 2f9c749
Show file tree
Hide file tree
Showing 4 changed files with 201 additions and 50 deletions.
19 changes: 13 additions & 6 deletions Compiler/BackEnd/BackendDAEOptimize.mo
Original file line number Diff line number Diff line change
Expand Up @@ -7975,12 +7975,15 @@ algorithm
DAE.ComponentRef cref, x;
String id,str;
String matrixName;
/* replace the subscripts with strings because no all elements of the arrays may be derived, this avoid trouble when generate simulation code */
case(cref, x, (matrixName,true))
equation
cref = ComponentReference.joinCrefs(ComponentReference.makeCrefIdent(BackendDAE.partialDerivativeNamePrefix, ComponentReference.crefType(cref), {}),cref);
cref = ComponentReference.appendStringCref(matrixName, cref);
cref = ComponentReference.joinCrefs(cref, x);
cref = ComponentReference.replaceSubsWithString(cref);
then
ComponentReference.joinCrefs(cref, x);
cref;
case(cref, x, (matrixName,false))
equation
id = ComponentReference.printComponentRefStr(cref) +& BackendDAE.partialDerivativeNamePrefix +& matrixName +& ComponentReference.printComponentRefStr(x);
Expand Down Expand Up @@ -11095,7 +11098,8 @@ algorithm
DAE.ComponentRef cr,cr1;
BackendVarTransform.VariableReplacements repl;
list<BackendDAE.Var> varLst;
BackendDAE.Var var;
BackendDAE.Var var;
DAE.Type ty;
case ({},_,_,_,_,_)
then
(inVarLst,inRepl);
Expand All @@ -11105,12 +11109,15 @@ algorithm
var = BackendVariable.getVarAt(inVars, v);
cr = BackendVariable.varCref(var);
cr = Debug.bcallret1(BackendVariable.isStateVar(var), ComponentReference.crefPrefixDer, cr, cr);
cr1 = ComponentReference.makeCrefQual("$ZERO",DAE.T_REAL_DEFAULT,{},cr);
cr1 = ComponentReference.makeCrefQual("$ZERO",DAE.T_COMPLEX_DEFAULT,{},cr);
cr1 = ComponentReference.replaceSubsWithString(cr1);
varexp = Expression.crefExp(cr1);
repl = BackendVarTransform.addReplacement(inRepl,cr,varexp,NONE());
var = BackendVariable.copyVarNewName(cr1,var);
var = BackendVariable.setVarKind(var, BackendDAE.VARIABLE());
var = BackendVariable.setVarAttributes(var, NONE());
ty = ComponentReference.crefLastType(cr1);
var = BackendDAE.VAR(cr1,BackendDAE.VARIABLE(),DAE.BIDIR(),DAE.NON_PARALLEL(),ty,NONE(),NONE(),{},0,DAE.emptyElementSource,NONE(),NONE(),DAE.NON_CONNECTOR());
//var = BackendVariable.copyVarNewName(cr1,var);
//var = BackendVariable.setVarKind(var, BackendDAE.VARIABLE());
//var = BackendVariable.setVarAttributes(var, NONE());
(varLst,repl) = getZeroVarReplacements1(rest,inVars,ass2,mapIncRowEqn,repl,var::inVarLst);
then
(varLst,repl);
Expand Down
43 changes: 43 additions & 0 deletions Compiler/FrontEnd/ComponentReference.mo
Original file line number Diff line number Diff line change
Expand Up @@ -2631,5 +2631,48 @@ algorithm
end match;
end expandSlice;

public function replaceSubsWithString
input DAE.ComponentRef inCref;
output DAE.ComponentRef outCref;
algorithm
outCref := match(inCref)
local
DAE.Ident ident,ident1;
DAE.Type identType;
list<DAE.Subscript> subscriptLst;
DAE.ComponentRef cr,cr1;
String str;
case DAE.CREF_QUAL(ident=ident,identType=identType,subscriptLst={},componentRef=cr)
equation
cr1 = replaceSubsWithString(cr);
then
DAE.CREF_QUAL(ident,identType,{},cr1);
case DAE.CREF_QUAL(ident=ident,identType=identType,subscriptLst=subscriptLst,componentRef=cr)
equation
identType = Expression.unliftArrayTypeWithSubs(subscriptLst,identType);
cr1 = replaceSubsWithString(cr);
str = ExpressionDump.printListStr(subscriptLst, ExpressionDump.printSubscriptStr, "_");
ident1 = stringAppendList({ident, "_", str, "_"});
then
DAE.CREF_QUAL(ident1,identType,{},cr1);
case DAE.CREF_IDENT(subscriptLst={})
then
inCref;
case DAE.CREF_IDENT(ident=ident,identType=identType,subscriptLst=subscriptLst)
equation
identType = Expression.unliftArrayTypeWithSubs(subscriptLst,identType);
str = ExpressionDump.printListStr(subscriptLst, ExpressionDump.printSubscriptStr, "_");
ident1 = stringAppendList({ident, "_", str, "_"});
then
DAE.CREF_IDENT(ident1,identType,{});
case DAE.CREF_ITER(ident=_)
then
inCref;
case DAE.WILD()
then
inCref;
end match;
end replaceSubsWithString;

end ComponentReference;

113 changes: 94 additions & 19 deletions Compiler/Template/CodegenCpp.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -198,13 +198,13 @@ case SIMCODE(modelInfo = MODELINFO(__)) then
_historyImpl = new HistoryImplType(globalSettings);
<%arrayReindex(modelInfo)%>
//Initialize array elements
//Load Algloopsover library
<%initializeArrayElements(simCode)%>
//Load Algloopsolver library
type_map types;
std::string algsover_name(SYSTEM_LIB);
if(!load_single_library(types, algsover_name))
throw std::invalid_argument("Algsover library could not be loaded");
std::string algsolver_name(SYSTEM_LIB);
if(!load_single_library(types, algsolver_name))
throw std::invalid_argument("Algsolver library could not be loaded");
std::map<std::string, factory<IAlgLoopSolverFactory> >::iterator iter;
std::map<std::string, factory<IAlgLoopSolverFactory> >& factories(types.get());
iter = factories.find("AlgLoopSolverFactory");
Expand Down Expand Up @@ -1869,7 +1869,7 @@ template generateClassDeclarationCode(SimCode simCode)
match simCode
case SIMCODE(modelInfo = MODELINFO(__)) then
<<
class <%lastIdentOfPath(modelInfo.name)%>: public IDAESystem ,public IContinous ,public IEvent ,public ISystemProperties, public ISystemInitialization <%if Flags.isSet(Flags.WRITE_TO_BUFFER) then ',public IReduceDAE '%>,public SystemDefaultImplementation
class <%lastIdentOfPath(modelInfo.name)%>: public IDAESystem, public IContinous, public IEvent, public ISystemProperties, public ISystemInitialization<%if Flags.isSet(Flags.WRITE_TO_BUFFER) then ', public IReduceDAE'%>, public SystemDefaultImplementation
{
<%generatefriendAlgloops(allEquations,simCode)%>
Expand Down Expand Up @@ -2616,7 +2616,7 @@ match simVar
let& dims = buffer "" /*BUFD*/
let varName = arraycref2(name,dims)
let varType = variableType(type_)
match dims case "0" then '<%varType%>& <%varName%>;'
match dims case "0" then '<%varType%> <%varName%>;'
end MemberVariableDefine;
template MemberVariableDefineReference(String type,SimVar simVar, String arrayName,String pre)
Expand Down Expand Up @@ -2973,6 +2973,32 @@ template initVals(list<SimVar> varsLst,SimCode simCode) ::=
;separator="\n"
end initVals;
template initializeArrayElements(SimCode simCode)
::=
match simCode
case SIMCODE(modelInfo = MODELINFO(varInfo = vi as VARINFO(__), vars = vars as SIMVARS(__)))
then
<<
<%initValsArray(vars.constVars,simCode)%>
<%initValsArray(vars.intConstVars,simCode)%>
<%initValsArray(vars.boolConstVars,simCode)%>
<%initValsArray(vars.stringConstVars,simCode)%>
<%initValsArray(vars.paramVars,simCode)%>
<%initValsArray(vars.intParamVars,simCode)%>
<%initValsArray(vars.boolParamVars,simCode)%>
<%initValsArray(vars.stringParamVars,simCode)%>
>>
end initializeArrayElements;
template initValsArray(list<SimVar> varsLst,SimCode simCode) ::=
varsLst |> SIMVAR(numArrayElement=_::_,initialValue=SOME(v)) =>
<<
<%cref(name)%> = <%initVal(v)%>;
>>
;separator="\n"
end initValsArray;
template arrayInit(SimCode simCode)
"Generates the contents of the makefile for the simulation case."
::=
Expand Down Expand Up @@ -4344,7 +4370,7 @@ template daeExpAsub(Exp inExp, Context context, Text &preExp /*BUFP*/,
case ASUB(exp=e, sub=indexes) then
let exp = daeExp(e, context, &preExp /*BUFC*/, &varDecls /*BUFD*/,simCode)
// let typeShort = expTypeFromExpShort(e)
let expIndexes = (indexes |> index => '<%daeExpASubIndex(index, context, &preExp, &varDecls,simCode)%>' ;separator=", ")
let expIndexes = (indexes |> index => '<%daeExpASubIndex(index, context, &preExp, &varDecls,simCode)%>' ;separator="][")
//'<%typeShort%>_get<%match listLength(indexes) case 1 then "" case i then '_<%i%>D'%>(&<%exp%>, <%expIndexes%>)'
'(<%exp%>)[<%expIndexes%>+1]'
case exp then
Expand Down Expand Up @@ -4654,13 +4680,15 @@ template daeExpCall(Exp call, Context context, Text &preExp /*BUFP*/,
let &preExp += 'transpose_alloc_<%arr_tp_str%>(&<%var1%>, &<%tvar%>);<%\n%>'
'<%tvar%>'

case CALL(path=IDENT(name="cross"), expLst={v1, v2}) then
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 arr_tp_str = '<%expTypeFromExpArray(v1)%>'
let tvar = tempDecl(arr_tp_str, &varDecls /*BUFD*/)
let &preExp += 'cross_alloc_<%arr_tp_str%>(&<%var1%>, &<%var2%>, &<%tvar%>);<%\n%>'
'<%tvar%>'
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%>'
'<%tvar%>'

case CALL(path=IDENT(name="identity"), expLst={A}) then
let var1 = daeExp(A, context, &preExp /*BUFC*/, &varDecls /*BUFD*/,simCode)
Expand Down Expand Up @@ -4902,13 +4930,42 @@ template daeExpBinary(Operator it, Exp exp1, Exp exp2, Context context, Text &pr
let &preExp += '<%var1%>=multiply_array<<%type1%>,<%listLength(dims)%>>(<%e1%>, <%e2%>);<%\n%>'
'<%var1%>'
case DIV_ARRAY_SCALAR(ty=T_ARRAY(dims=dims)) then
let type = match ty case T_ARRAY(ty=T_INTEGER(__)) then "integer_array"
case T_ARRAY(ty=T_ENUMERATION(__)) then "integer_array"
else "real_array"
let var = tempDecl(type, &varDecls /*BUFD*/)
let &preExp += 'div_alloc_<%type%>_scalar(&<%e1%>, <%e2%>, &<%var%>);<%\n%>'
let type = match ty case T_ARRAY(ty=T_INTEGER(__)) then 'int'
case T_ARRAY(ty=T_ENUMERATION(__)) then 'int'
else 'double'
let var = tempDecl('multi_array<<%type%>,<%listLength(dims)%>>', &varDecls /*BUFD*/)
//let var = tempDecl1(type,e1,&varDecls /*BUFD*/)
let &preExp += '<%var%>=divide_array<<%type%>,<%listLength(dims)%>>(<%e1%>, <%e2%>);<%\n%>'
'<%var%>'
case _ then "daeExpBinary:ERR"

case UMINUS(__) then "daeExpBinary:ERR UMINUS not supported"
case UMINUS_ARR(__) then "daeExpBinary:ERR UMINUS_ARR not supported"

case ADD_ARR(__) then "daeExpBinary:ERR ADD_ARR not supported"
case SUB_ARR(__) then "daeExpBinary:ERR SUB_ARR not supported"
case MUL_ARR(__) then "daeExpBinary:ERR MUL_ARR not supported"
case DIV_ARR(__) then "daeExpBinary:ERR DIV_ARR not supported"
case ADD_ARRAY_SCALAR(__) then "daeExpBinary:ERR ADD_ARRAY_SCALAR not supported"
case SUB_SCALAR_ARRAY(__) then "daeExpBinary:ERR SUB_SCALAR_ARRAY not supported"
case MUL_SCALAR_PRODUCT(__) then
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%>)'
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"
case POW_ARR(__) then "daeExpBinary:ERR POW_ARR not supported"
case POW_ARR2(__) then "daeExpBinary:ERR POW_ARR2 not supported"
case NOT(__) then "daeExpBinary:ERR NOT not supported"
case LESS(__) then "daeExpBinary:ERR LESS not supported"
case LESSEQ(__) then "daeExpBinary:ERR LESSEQ not supported"
case GREATER(__) then "daeExpBinary:ERR GREATER not supported"
case GREATEREQ(__) then "daeExpBinary:ERR GREATEREQ not supported"
case EQUAL(__) then "daeExpBinary:ERR EQUAL not supported"
case NEQUAL(__) then "daeExpBinary:ERR NEQUAL not supported"
case USERDEFINED(__) then "daeExpBinary:ERR POW_ARR not supported"
case _ then 'daeExpBinary:ERR'
end daeExpBinary;

template tempDecl1(String ty, String exp, Text &varDecls /*BUFP*/)
Expand Down Expand Up @@ -5329,6 +5386,7 @@ end expTypeFromExpArrayIf;
template expTypeFromExp(Exp it) ::=
match it
case ICONST(__) then "int"
case ENUM_LITERAL(__) then "int"
case RCONST(__) then "double"
case SCONST(__) then "string"
case BCONST(__) then "bool"
Expand All @@ -5347,6 +5405,23 @@ template expTypeFromExp(Exp it) ::=
case CODE(__) then expTypeShort(ty)
case ASUB(__) then expTypeFromExp(exp)
case REDUCTION(__) then expTypeFromExp(expr)

case TUPLE(__) then "expTypeFromExp:ERROR TUPLE unsupported"
case TSUB(__) then "expTypeFromExp:ERROR TSUB unsupported"
case SIZE(__) then "expTypeFromExp:ERROR SIZE unsupported"

/* Part of MetaModelica extension. KS */
case LIST(__) then "expTypeFromExp:ERROR LIST unsupported"
case CONS(__) then "expTypeFromExp:ERROR CONS unsupported"
case META_TUPLE(__) then "expTypeFromExp:ERROR META_TUPLE unsupported"
case META_OPTION(__) then "expTypeFromExp:ERROR META_OPTION unsupported"
case METARECORDCALL(__) then "expTypeFromExp:ERROR METARECORDCALL unsupported"
case MATCHEXPRESSION(__) then "expTypeFromExp:ERROR MATCHEXPRESSION unsupported"
case BOX(__) then "expTypeFromExp:ERROR BOX unsupported"
case UNBOX(__) then "expTypeFromExp:ERROR UNBOX unsupported"
case SHARED_LITERAL(__) then expTypeFlag(ty,6)
case PATTERN(__) then "expTypeFromExp:ERROR PATTERN unsupported"

case _ then "expTypeFromExp:ERROR"
end expTypeFromExp;

Expand Down

0 comments on commit 2f9c749

Please sign in to comment.