Skip to content

Commit

Permalink
- Modelica.Mechanics.MultiBody.Examples.Elementary.Pendulum now simul…
Browse files Browse the repository at this point in the history
…ates

- fixes to handle code generation for world.gravityAcceleration function
- code generation for functions now take the functions from the DAE.FunctionTree
  instead of re-instantiate them
- some problems appeared with partial functions (i fixed some of them but more debugging is needed to fix all)

SimCode.mo
- SimCode.createFunctions now uses the functions already present in DAE.FunctionTree
- some more comments
- small fixes (functions can contain multiple definitions if they have a derivative annotations) 

SimCodeC.tpl
- handle enumeration comparison and types 
- replace "." in identifiers with "_" in underscorePath

DAELow.mo
- use basic type equality functions instead of polymorphic one

DAEUtil.mo
- added DAEUtil.getFunctionNames to return the paths of all functions in the DAE
- use basic type equality functions instead of polymorphic one

DAEDump.mo
- DAEDump.unparseDimensions is now public

git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@5766 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
adrpo committed Jul 1, 2010
1 parent 853d59e commit 898bad7
Show file tree
Hide file tree
Showing 6 changed files with 1,017 additions and 870 deletions.
2 changes: 1 addition & 1 deletion Compiler/DAEDump.mo
Expand Up @@ -2425,7 +2425,7 @@ algorithm
end matchcontinue;
end unparseType;

protected function unparseDimensions
public function unparseDimensions
"prints dimensions to a string"
input DAE.InstDims dims;
input Boolean printTypeDimension "use true here when printing components in functions as these are not vectorized! Otherwise, use false";
Expand Down
14 changes: 7 additions & 7 deletions Compiler/DAELow.mo
Expand Up @@ -7294,7 +7294,7 @@ algorithm
indx = getVarUsingName2(cr, indexes);
((v as VAR(varName = cr2))) = vararrayNth(varr, indx);
name_str = Exp.printComponentRefStr(cr2);
equality(name_str = cr);
true = stringEqual(name_str, cr);
indx_1 = indx + 1;
then
(v,indx_1);
Expand Down Expand Up @@ -7389,7 +7389,7 @@ algorithm
list<StringIndex> vs;
case (cr,(STRINGINDEX(str = cr2,index = v) :: _))
equation
equality(cr = cr2);
true = stringEqual(cr, cr2);
then
v;
case (cr,(v :: vs))
Expand Down Expand Up @@ -9967,17 +9967,17 @@ protected function varHasSameLastIdent
Helper funciton to varStateSelectHeuristicPrio3.
Returns true if the variable has the same name (the last identifier)
as the variable name given as second argument."
input Var v;
input DAE.ComponentRef cr;
output Boolean b;
input Var v;
input DAE.ComponentRef cr;
output Boolean b;
algorithm
b := matchcontinue(v,cr)
local DAE.ComponentRef cr2; DAE.Ident id1,id2;
case(VAR(varName=cr2 ),cr )
equation
id1 = Exp.crefLastIdent(cr);
id2 = Exp.crefLastIdent(cr2);
equality(id1 = id2);
true = stringEqual(id1, id2);
then true;
case(_,_) then false;
end matchcontinue;
Expand Down Expand Up @@ -16102,7 +16102,7 @@ algorithm outEqnLst := matchcontinue(inEqn,inFuncs)
complexEqsLst1 = Util.listMap1(complexEqs,extendRecordEqns,funcs);
complexEqs1 = Util.listFlatten(complexEqsLst1);
then
complexEqs1;
complexEqs1;
case(eqn,_) then {eqn};
end matchcontinue;
end extendRecordEqns;
Expand Down
37 changes: 32 additions & 5 deletions Compiler/DAEUtil.mo
Expand Up @@ -4096,6 +4096,35 @@ algorithm
end matchcontinue;
end functionName;

public function getFunctionNames "returns the name of a FUNCTION or RECORD_CONSTRUCTOR"
input list<DAE.Element> elts;
output list<Absyn.Path> name;
algorithm
name:= matchcontinue(elts)
local
list<Absyn.Path> functionPaths;
Absyn.Path name;
list<DAE.Element> rest;
// empty case
case({}) then {};
// function
case(DAE.FUNCTION(path=name)::rest)
equation
functionPaths = getFunctionNames(rest);
then name::functionPaths;
// record constructors
case(DAE.RECORD_CONSTRUCTOR(path=name)::rest)
equation
functionPaths = getFunctionNames(rest);
then name::functionPaths;
// anything else
case(_::rest)
equation
functionPaths = getFunctionNames(rest);
then functionPaths;
end matchcontinue;
end getFunctionNames;

public function addDaeFunction "add functions present in the element list to the function tree"
input DAE.DAElist dae;
output DAE.DAElist outDae;
Expand Down Expand Up @@ -4393,15 +4422,13 @@ algorithm
end avlTreeAddLst;

public function avlTreeAdd "
Add a tuple (key,value) to the AVL tree.
"
Add a tuple (key,value) to the AVL tree."
input DAE.AvlTree inAvlTree;
input DAE.AvlKey inKey;
input DAE.AvlValue inValue;
output DAE.AvlTree outAvlTree;
algorithm
outAvlTree:=
matchcontinue (inAvlTree,inKey,inValue)
outAvlTree := matchcontinue (inAvlTree,inKey,inValue)
local
DAE.AvlKey key,rkey;
DAE.AvlValue value,rval;
Expand All @@ -4416,7 +4443,7 @@ algorithm
/* Replace this node.*/
case (DAE.AVLTREENODE(value = SOME(DAE.AVLTREEVALUE(rkey,rval)),height=h,left = left,right = right),key,value)
equation
equality(rkey = key);
true = ModUtil.pathEqual(rkey, key);
bt = balance(DAE.AVLTREENODE(SOME(DAE.AVLTREEVALUE(rkey,value)),h,left,right));
then
bt;
Expand Down
85 changes: 70 additions & 15 deletions Compiler/SimCode.mo
Expand Up @@ -654,24 +654,54 @@ algorithm
(libs, functions, outDAELow, outDAE) :=
matchcontinue (inProgram,inDAElist,inDAELow,inPath,inString)
local
list<Absyn.Path> funcpaths;
list<Absyn.Path> funcPaths, funcRefPaths, funcNormalPaths;
list<String> debugpathstrs,libs1,libs2,includes;
String debugpathstr,debugstr,filenameprefix, str;
list<DAE.Element> funcelems,part_func_elems, elements;
list<DAE.Element> funcelems,part_func_elems, elements, funcRefElems, funcNormal;
list<SCode.Class> p;
DAE.DAElist dae;
DAELow.DAELow dlow;
Absyn.Path path;
list<Function> fns;
SimCode sc;
case (p,(dae as DAE.DAE(elementLst = elements)),dlow,path,filenameprefix)
DAE.FunctionTree funcs;

case (p,(dae as DAE.DAE(elementLst = elements, functions = funcs)),dlow,path,filenameprefix)
equation
funcpaths = getCalledFunctions(dae, dlow);
//Debug.fprint("info", "Found called functions: ") "debug" ;
//debugpathstrs = Util.listMap(funcpaths, Absyn.pathString) "debug" ;
//debugpathstr = Util.stringDelimitList(debugpathstrs, ", ") "debug" ;
//Debug.fprintln("info", debugpathstr) "debug" ;
funcelems = generateFunctions2(p, funcpaths);
// get all the used functions from the function tree
funcNormal = Util.listMap(DAEUtil.avlTreeToList(funcs),Util.tuple22);

// get all the function references:
funcRefPaths = getCalledFunctionReferences(dae, dlow);
// get the paths of the normal functions
funcNormalPaths = DAEUtil.getFunctionNames(funcNormal);

// remove all normal functions from the list of function references
funcPaths = Util.listSetDifferenceOnTrue(funcRefPaths, funcNormalPaths, ModUtil.pathEqual);

// adrpo: debugging to see if the union of functions are ok
// debugpathstrs = Util.listMap(funcRefPaths, Absyn.pathString);
// debugpathstr = Util.stringDelimitList(debugpathstrs, ", ");
// print("Refs: " +& debugpathstr +& "\n");
// debugpathstrs = Util.listMap(funcNormalPaths, Absyn.pathString);
// debugpathstr = Util.stringDelimitList(debugpathstrs, ", ");
// print("Normal: " +& debugpathstr +& "\n");
// debugpathstrs = Util.listMap(funcPaths, Absyn.pathString);
// debugpathstr = Util.stringDelimitList(debugpathstrs, ", ");
// print("Ref-Normal: " +& debugpathstr +& "\n");

// generate functions for the function references
funcRefElems = generateFunctions2(p, funcPaths);

// append the normal functions with the function references
funcelems = listAppend(funcRefElems, funcNormal);

// print ("Detected DAE functions: "+& intString(listLength(funcelems)) +& "\n");

// Debug.fprint("info", "Found called functions: ") "debug" ;
// debugpathstrs = Util.listMap(funcpaths, Absyn.pathString) "debug" ;
// debugpathstr = Util.stringDelimitList(debugpathstrs, ", ") "debug" ;
// Debug.fprintln("info", debugpathstr) "debug" ;

part_func_elems = PartFn.createPartEvalFunctions(funcelems);
(dae, part_func_elems) = PartFn.partEvalDAE(dae, part_func_elems);
Expand Down Expand Up @@ -717,6 +747,31 @@ algorithm
res := removeDuplicatePaths(calledfuncs);
end getCalledFunctions;

public function getCalledFunctionReferences
"Goes through the DAELow structure, finds all function references calls,
and returns them in a list. Removes duplicates."
input DAE.DAElist dae;
input DAELow.DAELow dlow;
output list<Absyn.Path> res;
list<Exp.Exp> explist,fcallexps;
list<Absyn.Path> calledfuncs;
algorithm
res := matchcontinue(dae, dlow)
case (dae, dlow)
equation
false = RTOpts.acceptMetaModelicaGrammar();
then {};
case (dae, dlow)
equation
true = RTOpts.acceptMetaModelicaGrammar();
explist = DAELow.getAllExps(dlow);
fcallexps = getMatchingExpsList(explist, matchFnRefs);
calledfuncs = Util.listMap(fcallexps, getCallPath);
res = removeDuplicatePaths(calledfuncs);
then res;
end matchcontinue;
end getCalledFunctionReferences;

protected function generateExternalObjectIncludes
"Generates the library paths for external objects"
input DAELow.DAELow daelow;
Expand Down Expand Up @@ -838,7 +893,7 @@ algorithm

/* Modelica functions. */
case (DAE.FUNCTION(path = fpath,
functions = {DAE.FUNCTION_DEF(body = daeElts)},
functions = DAE.FUNCTION_DEF(body = daeElts)::_, // might be followed by derivative maps
type_ = tp as (DAE.T_FUNCTION(funcArg=args, funcResultType=restype), _),
partialPrefix=false), rt)
equation
Expand All @@ -855,7 +910,7 @@ algorithm
rt_1);
/* External functions. */
case (DAE.FUNCTION(path = fpath,
functions = {DAE.FUNCTION_EXT(body = daeElts, externalDecl = extdecl)},
functions = DAE.FUNCTION_EXT(body = daeElts, externalDecl = extdecl)::_, // might be followed by derivative maps
type_ = (tp as (DAE.T_FUNCTION(funcArg = args,funcResultType = restype),_))),rt)
equation
DAE.EXTERNALDECL(ident=extfnname, external_=extargs,
Expand Down Expand Up @@ -892,8 +947,9 @@ algorithm
(RECORD_CONSTRUCTOR(name, funArgs, recordDecls),
rt_1);
case (comp,rt)
equation
Error.addMessage(Error.INTERNAL_ERROR, {"SimCode.elaborateFunction failed"});
equation
str = "SimCode.elaborateFunction failed for function: \n" +& DAEDump.dumpFunctionStr(comp);
Error.addMessage(Error.INTERNAL_ERROR, {str});
then
fail();
end matchcontinue;
Expand Down Expand Up @@ -6365,8 +6421,7 @@ protected function generateFunctions3
input list<Absyn.Path> inAbsynPathLst3;
output list<DAE.Element> outDAEElementLst;
algorithm
outDAEElementLst:=
matchcontinue (inProgram1,inAbsynPathLst2,inAbsynPathLst3)
outDAEElementLst := matchcontinue (inProgram1,inAbsynPathLst2,inAbsynPathLst3)
local
list<Absyn.Path> allpaths,subfuncs,allpaths_1,paths_1,paths;
DAE.DAElist fdae,dae,patched_dae;
Expand Down

0 comments on commit 898bad7

Please sign in to comment.