Skip to content

Commit

Permalink
- start to rewrite handling of sample-calls
Browse files Browse the repository at this point in the history
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@14881 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
lochel committed Jan 22, 2013
1 parent 0007cdb commit c423888
Show file tree
Hide file tree
Showing 16 changed files with 172 additions and 122 deletions.
15 changes: 12 additions & 3 deletions Compiler/BackEnd/BackendDAE.mo
Expand Up @@ -228,15 +228,24 @@ end WhenClause;
public
uniontype ZeroCrossing "- Zero Crossing"
record ZERO_CROSSING
.DAE.Exp relation_ "function" ;
list<Integer> occurEquLst "list of equations where the function occurs" ;
list<Integer> occurWhenLst "list of when clauses where the function occurs" ;
.DAE.Exp relation_ "function" ;
list<Integer> occurEquLst "list of equations where the function occurs" ;
list<Integer> occurWhenLst "list of when clauses where the function occurs" ;
end ZERO_CROSSING;
end ZeroCrossing;

public
uniontype SampleLookup
record SAMPLE_LOOKUP
Integer nSamples "total number of different sample calls" ;
list<tuple<Integer, .DAE.Exp, .DAE.Exp>> lookup "sample arguments (index, start, interval)" ;
end SAMPLE_LOOKUP;
end SampleLookup;

public
uniontype EventInfo "- EventInfo"
record EVENT_INFO
SampleLookup sampleLookup "stores all information regarding sample-calls" ;
list<WhenClause> whenClauseLst "list of when clauses. The WhenEquation datatype refer to this list by position" ;
list<ZeroCrossing> zeroCrossingLst "list of zero crossing coditions";
list<ZeroCrossing> sampleLst "list of sample as before, used by cpp runtime";
Expand Down
53 changes: 31 additions & 22 deletions Compiler/BackEnd/BackendDAECreate.mo
Expand Up @@ -103,11 +103,12 @@ protected
list<DAE.Element> elems, aliaseqns;
list<BackendDAE.ZeroCrossing> zero_crossings;
DAE.FunctionTree functionTree;
BackendDAE.SampleLookup sampleLookup;
algorithm
System.realtimeTick(CevalScript.RT_CLOCK_EXECSTAT_BACKEND_MODULES);
Debug.execStat("Enter Backend", CevalScript.RT_CLOCK_EXECSTAT_BACKEND_MODULES);
functionTree := Env.getFunctionTree(inCache);
(DAE.DAE(elems), functionTree) := processBuiltinExpressions(lst, functionTree);
(DAE.DAE(elems), functionTree, sampleLookup) := processBuiltinExpressions(lst, functionTree);
vars := BackendVariable.emptyVars();
knvars := BackendVariable.emptyVars();
extVars := BackendVariable.emptyVars();
Expand All @@ -122,7 +123,7 @@ algorithm
ieqnarr := BackendEquation.listEquation(ieqns);
constrarra := listArray(constrs);
clsattrsarra := listArray(clsAttrs);
einfo := BackendDAE.EVENT_INFO(whenclauses_1, {}, {}, {}, 0, 0);
einfo := BackendDAE.EVENT_INFO(sampleLookup, whenclauses_1, {}, {}, {}, 0, 0);
symjacs := {(NONE(), ({}, ({}, {})), {}), (NONE(), ({}, ({}, {})), {}), (NONE(), ({}, ({}, {})), {}), (NONE(), ({}, ({}, {})), {})};
outBackendDAE := BackendDAE.DAE(BackendDAE.EQSYSTEM(vars_1,
eqnarr,
Expand Down Expand Up @@ -431,21 +432,22 @@ protected function processBuiltinExpressions "function processBuiltinExpressions
input DAE.FunctionTree functionTree;
output DAE.DAElist outDAE;
output DAE.FunctionTree outTree;
output BackendDAE.SampleLookup outSampleLookup;
protected
HashTableExpToIndex.HashTable ht;
algorithm
ht := HashTableExpToIndex.emptyHashTable();
(outDAE, outTree, _) := DAEUtil.traverseDAE(inDAE, functionTree, transformBuiltinExpressions, (ht, 1, 1));
(outDAE, outTree, (ht, _, outSampleLookup)) := DAEUtil.traverseDAE(inDAE, functionTree, transformBuiltinExpressions, (ht, 0, BackendDAE.SAMPLE_LOOKUP(0, {})));
end processBuiltinExpressions;

protected function transformBuiltinExpressions "function transformBuiltinExpressions
author: lochel
Helper for processBuiltinExpressions"
input tuple<DAE.Exp, tuple<HashTableExpToIndex.HashTable, Integer, Integer>> itpl;
output tuple<DAE.Exp, tuple<HashTableExpToIndex.HashTable, Integer, Integer>> otpl;
input tuple<DAE.Exp, tuple<HashTableExpToIndex.HashTable, Integer, BackendDAE.SampleLookup>> itpl;
output tuple<DAE.Exp, tuple<HashTableExpToIndex.HashTable, Integer, BackendDAE.SampleLookup>> otpl;
protected
DAE.Exp e;
tuple<HashTableExpToIndex.HashTable, Integer, Integer> i;
tuple<HashTableExpToIndex.HashTable, Integer, BackendDAE.SampleLookup> i;
algorithm
(e, i) := itpl;
otpl := Expression.traverseExp(e, transformBuiltinExpression, i);
Expand All @@ -454,36 +456,41 @@ end transformBuiltinExpressions;
protected function transformBuiltinExpression "function transformBuiltinExpression
author: lochel
Helper for transformBuiltinExpressions"
input tuple<DAE.Exp, tuple<HashTableExpToIndex.HashTable, Integer, Integer>> inTuple;
output tuple<DAE.Exp, tuple<HashTableExpToIndex.HashTable, Integer, Integer>> outTuple;
input tuple<DAE.Exp, tuple<HashTableExpToIndex.HashTable, Integer, BackendDAE.SampleLookup>> inTuple;
output tuple<DAE.Exp, tuple<HashTableExpToIndex.HashTable, Integer, BackendDAE.SampleLookup>> outTuple;
algorithm
outTuple := matchcontinue(inTuple)
local
DAE.Exp e;
DAE.Exp e, start, interval;
list<DAE.Exp> es;
HashTableExpToIndex.HashTable ht;
Integer iDelay, iSample, i;
list<tuple<Integer, DAE.Exp, DAE.Exp>> samples;
BackendDAE.SampleLookup sampleLookup;
DAE.CallAttributes attr;

// delay [already in ht]
case ((e as DAE.CALL(Absyn.IDENT("delay"), es, attr), (ht, iDelay, iSample))) equation
case ((e as DAE.CALL(Absyn.IDENT("delay"), es, attr), (ht, iDelay, sampleLookup))) equation
i = BaseHashTable.get(e, ht);
then ((DAE.CALL(Absyn.IDENT("delay"), DAE.ICONST(i)::es, attr), (ht, iDelay, iSample)));
then ((DAE.CALL(Absyn.IDENT("delay"), DAE.ICONST(i)::es, attr), (ht, iDelay, sampleLookup)));

// delay [not yet in ht]
case ((e as DAE.CALL(Absyn.IDENT("delay"), es, attr), (ht, iDelay, iSample))) equation
ht = BaseHashTable.add((e, iDelay), ht);
then ((DAE.CALL(Absyn.IDENT("delay"), DAE.ICONST(iDelay)::es, attr), (ht, iDelay+1, iSample)));
case ((e as DAE.CALL(Absyn.IDENT("delay"), es, attr), (ht, iDelay, sampleLookup))) equation
ht = BaseHashTable.add((e, iDelay+1), ht);
then ((DAE.CALL(Absyn.IDENT("delay"), DAE.ICONST(iDelay)::es, attr), (ht, iDelay+1, sampleLookup)));

// sample [already in ht]
case ((e as DAE.CALL(Absyn.IDENT("sample"), es, attr), (ht, iDelay, iSample))) equation
case ((e as DAE.CALL(Absyn.IDENT("sample"), es, attr), (ht, iDelay, sampleLookup))) equation
i = BaseHashTable.get(e, ht);
then ((DAE.CALL(Absyn.IDENT("sample"), DAE.ICONST(i)::es, attr), (ht, iDelay, iSample)));
then ((DAE.CALL(Absyn.IDENT("sample"), DAE.ICONST(i)::es, attr), (ht, iDelay, sampleLookup)));

// sample [not yet in ht]
case ((e as DAE.CALL(Absyn.IDENT("sample"), es, attr), (ht, iDelay, iSample))) equation
case ((e as DAE.CALL(Absyn.IDENT("sample"), es as {start, interval}, attr), (ht, iDelay, BackendDAE.SAMPLE_LOOKUP(iSample, samples)))) equation
iSample = iSample+1;
samples = listAppend(samples, {(iSample, start, interval)});
ht = BaseHashTable.add((e, iSample), ht);
then ((DAE.CALL(Absyn.IDENT("sample"), DAE.ICONST(iSample)::es, attr), (ht, iDelay, iSample+1)));
sampleLookup = BackendDAE.SAMPLE_LOOKUP(iSample, samples);
then ((DAE.CALL(Absyn.IDENT("sample"), DAE.ICONST(iSample)::es, attr), (ht, iDelay, sampleLookup)));

else inTuple;
end matchcontinue;
Expand Down Expand Up @@ -3382,6 +3389,7 @@ algorithm
array<DAE.ClassAttributes> clsAttrs;
BackendDAE.EventInfo einfo, einfo1;
BackendDAE.ExternalObjectClasses eoc;
BackendDAE.SampleLookup sampleLookup;
list<BackendDAE.WhenClause> whenclauses, whenclauses1;
list<BackendDAE.Equation> eqs_lst, eqs_lst1;
list<BackendDAE.ZeroCrossing> zero_crossings;
Expand All @@ -3394,7 +3402,7 @@ algorithm
Env.Env env;
BackendDAE.EqSystems systs;
case (BackendDAE.DAE(systs, (BackendDAE.SHARED(knvars, exobj, av, inieqns, remeqns, constrs, clsAttrs,
cache, env, funcs, einfo as BackendDAE.EVENT_INFO(zeroCrossingLst=zero_crossings, relationsLst=relationsLst,
cache, env, funcs, einfo as BackendDAE.EVENT_INFO(sampleLookup=sampleLookup, zeroCrossingLst=zero_crossings, relationsLst=relationsLst,
sampleLst=sampleLst, whenClauseLst=whenclauses, relationsNumber=countRelations,
numberMathEvents=countMathFunctions), eoc, btp, symjacs))), _)
equation
Expand All @@ -3406,7 +3414,7 @@ algorithm
(zero_crossings, eqs_lst1, _, countRelations, countMathFunctions, relationsLst, sampleLst) = findZeroCrossings2(vars, knvars, eqs_lst, 0, {}, 0, countRelations, countMathFunctions, zero_crossings, relationsLst, sampleLst);
inieqns = BackendEquation.listEquation(eqs_lst1);
Debug.fcall(Flags.RELIDX, print, "findZeroCrossings1 sample index: " +& intString(listLength(sampleLst)) +& "\n");
einfo1 = BackendDAE.EVENT_INFO(whenclauses, zero_crossings, sampleLst, relationsLst, countRelations, countMathFunctions);
einfo1 = BackendDAE.EVENT_INFO(sampleLookup, whenclauses, zero_crossings, sampleLst, relationsLst, countRelations, countMathFunctions);
then
BackendDAE.DAE(systs, BackendDAE.SHARED(knvars, exobj, av, inieqns, remeqns, constrs, clsAttrs, cache, env, funcs, einfo1, eoc, btp, symjacs));
end match;
Expand All @@ -3431,6 +3439,7 @@ algorithm
BackendDAE.ExternalObjectClasses eoc;
list<BackendDAE.WhenClause> whenclauses;
list<BackendDAE.Equation> eqs_lst, eqs_lst1;
BackendDAE.SampleLookup sampleLookup;
list<BackendDAE.ZeroCrossing> zero_crossings;
list<BackendDAE.ZeroCrossing> relations, sampleLst;
Integer countRelations;
Expand All @@ -3445,7 +3454,7 @@ algorithm
BackendDAE.StateSets stateSets;
case (BackendDAE.EQSYSTEM(vars, eqns, m, mT, matching, stateSets),
(BackendDAE.SHARED(knvars, exobj, av, inieqns, remeqns, constrs, clsAttrs,
cache, env, funcs, einfo as BackendDAE.EVENT_INFO(zeroCrossingLst=zero_crossings,
cache, env, funcs, einfo as BackendDAE.EVENT_INFO(sampleLookup=sampleLookup, zeroCrossingLst=zero_crossings,
sampleLst=sampleLst, whenClauseLst=whenclauses, relationsLst=relations,
relationsNumber=countRelations, numberMathEvents=countMathFunctions),
eoc, btp, symjacs), allvars))
Expand All @@ -3455,7 +3464,7 @@ algorithm
Debug.fcall(Flags.RELIDX, print, "findZeroCrossings1 number of relations : " +& intString(countRelations) +& "\n");
Debug.fcall(Flags.RELIDX, print, "findZeroCrossings1 sample index: " +& intString(listLength(sampleLst)) +& "\n");
eqns1 = BackendEquation.listEquation(eqs_lst1);
einfo1 = BackendDAE.EVENT_INFO(whenclauses, zero_crossings, sampleLst, relations, countRelations, countMathFunctions);
einfo1 = BackendDAE.EVENT_INFO(sampleLookup, whenclauses, zero_crossings, sampleLst, relations, countRelations, countMathFunctions);
allvars = listAppend(allvars, BackendVariable.varList(vars));
then
(BackendDAE.EQSYSTEM(vars, eqns1, m, mT, matching, stateSets), (BackendDAE.SHARED(knvars, exobj, av, inieqns, remeqns, constrs, clsAttrs, cache, env, funcs, einfo1, eoc, btp, symjacs), allvars));
Expand Down
30 changes: 17 additions & 13 deletions Compiler/BackEnd/BackendDAEOptimize.mo
Expand Up @@ -3184,7 +3184,7 @@ algorithm
emptyEqns = BackendEquation.emptyEqns();
emptyVars = BackendVariable.emptyVars();
eqSystem = BackendDAE.EQSYSTEM(variables,eqArray,NONE(),NONE(),BackendDAE.NO_MATCHING(),{});
shared = BackendDAE.SHARED(knownVars,externalObjects,aliasVars,emptyEqns,removedEqs,constraints,classAttrs,cache,env,functionTree,BackendDAE.EVENT_INFO({},{},{},{},0,0),{},BackendDAE.SIMULATION(),{});
shared = BackendDAE.SHARED(knownVars,externalObjects,aliasVars,emptyEqns,removedEqs,constraints,classAttrs,cache,env,functionTree,BackendDAE.EVENT_INFO(BackendDAE.SAMPLE_LOOKUP(0, {}),{},{},{},{},0,0),{},BackendDAE.SIMULATION(),{});
(m_new,mT_new) = BackendDAEUtil.incidenceMatrix(eqSystem,BackendDAE.NORMAL(),NONE());
match1 = arrayCreate(l,1);
matching = BackendDAE.MATCHING(match1,match1,{});
Expand Down Expand Up @@ -6018,7 +6018,7 @@ algorithm
constrs = listArray({});
clsAttrs = listArray({});
functions = DAEUtil.avlTreeNew();
jacEventInfo = BackendDAE.EVENT_INFO({}, {}, {}, {}, 0, 0);
jacEventInfo = BackendDAE.EVENT_INFO(BackendDAE.SAMPLE_LOOKUP(0, {}), {}, {}, {}, {}, 0, 0);
jacExtObjClasses = {};

jacobian = BackendDAE.DAE({BackendDAE.EQSYSTEM(jacOrderedVars, jacOrderedEqs, NONE(), NONE(), BackendDAE.NO_MATCHING(),{})}, BackendDAE.SHARED(jacKnownVars, jacExternalObjects, jacAliasVars, jacInitialEqs, jacRemovedEqs, constrs, clsAttrs, cache, env, functions, jacEventInfo, jacExtObjClasses,BackendDAE.JACOBIAN(),{}));
Expand Down Expand Up @@ -6059,7 +6059,7 @@ algorithm
constrs = listArray({});
clsAttrs = listArray({});
functions = DAEUtil.avlTreeNew();
jacEventInfo = BackendDAE.EVENT_INFO({}, {}, {}, {}, 0, 0);
jacEventInfo = BackendDAE.EVENT_INFO(BackendDAE.SAMPLE_LOOKUP(0, {}), {}, {}, {}, {}, 0, 0);
jacExtObjClasses = {};

jacobian = BackendDAE.DAE(BackendDAE.EQSYSTEM(jacOrderedVars, jacOrderedEqs, NONE(), NONE(), BackendDAE.NO_MATCHING(),{})::{}, BackendDAE.SHARED(jacKnownVars, jacExternalObjects, jacAliasVars, jacInitialEqs, jacRemovedEqs, constrs, clsAttrs, cache, env, functions, jacEventInfo, jacExtObjClasses, BackendDAE.JACOBIAN(),{}));
Expand Down Expand Up @@ -8100,16 +8100,17 @@ protected function traverseEventInfoExps
output tuple<DAE.Exp, Type_a> outTpl;
end FuncExpType;
protected
BackendDAE.SampleLookup sampleLookup;
list<BackendDAE.WhenClause> whenClauseLst;
list<BackendDAE.ZeroCrossing> zeroCrossingLst,sampleLst,relationsLst;
Integer relationsNumber,numberMathEvents;
algorithm
BackendDAE.EVENT_INFO(whenClauseLst,zeroCrossingLst,sampleLst,relationsLst,relationsNumber,numberMathEvents) := iEventInfo;
BackendDAE.EVENT_INFO(sampleLookup,whenClauseLst,zeroCrossingLst,sampleLst,relationsLst,relationsNumber,numberMathEvents) := iEventInfo;
(whenClauseLst,outTypeA) := traverseWhenClauseExps(whenClauseLst,func,inTypeA,{});
(zeroCrossingLst,outTypeA) := traverseZeroCrossingExps(zeroCrossingLst,func,outTypeA,{});
(sampleLst,outTypeA) := traverseZeroCrossingExps(sampleLst,func,outTypeA,{});
(relationsLst,outTypeA) := traverseZeroCrossingExps(relationsLst,func,outTypeA,{});
oEventInfo := BackendDAE.EVENT_INFO(whenClauseLst,zeroCrossingLst,sampleLst,relationsLst,relationsNumber,numberMathEvents);
oEventInfo := BackendDAE.EVENT_INFO(sampleLookup,whenClauseLst,zeroCrossingLst,sampleLst,relationsLst,relationsNumber,numberMathEvents);
end traverseEventInfoExps;

protected function traverseWhenClauseExps
Expand Down Expand Up @@ -10175,12 +10176,13 @@ protected
BackendDAE.SymbolicJacobians symjacs;
list<BackendDAE.Equation> additionalInitialEquations;

list<BackendDAE.WhenClause> whenClauseLst "List of when clauses. The WhenEquation datatype refer to this list by position" ;
list<BackendDAE.ZeroCrossing> zeroCrossingLst "List of zero crossing coditions";
list<BackendDAE.ZeroCrossing> sampleLst "List of sample as before, used by cpp runtime";
list<BackendDAE.ZeroCrossing> relationsLst "List of zero crossing function as before, used by cpp runtime";
Integer relationsNumber "stores the number of relation in all zero-crossings";
Integer numberMathEvents "stores the number of math function that trigger events e.g. floor, ceil, integer, ...";
BackendDAE.SampleLookup sampleLookup;
list<BackendDAE.WhenClause> whenClauseLst;
list<BackendDAE.ZeroCrossing> zeroCrossingLst;
list<BackendDAE.ZeroCrossing> sampleLst;
list<BackendDAE.ZeroCrossing> relationsLst;
Integer relationsNumber;
Integer numberMathEvents;

Integer index;
HashTableExpToIndex.HashTable ht; // is used to avoid redundant condition-variables
Expand All @@ -10204,7 +10206,8 @@ algorithm
extObjClasses=extObjClasses,
backendDAEType=backendDAEType,
symjacs=symjacs) := shared;
BackendDAE.EVENT_INFO(whenClauseLst=whenClauseLst,
BackendDAE.EVENT_INFO(sampleLookup=sampleLookup,
whenClauseLst=whenClauseLst,
zeroCrossingLst=zeroCrossingLst,
sampleLst=sampleLst,
relationsLst=relationsLst,
Expand All @@ -10226,7 +10229,8 @@ algorithm
systs := listAppend(systs, {BackendDAE.EQSYSTEM(vars_, eqns_, NONE(), NONE(), BackendDAE.NO_MATCHING(), {})});

initialEqs := BackendEquation.addEquations(additionalInitialEquations, initialEqs);
eventInfo := BackendDAE.EVENT_INFO(whenClauseLst,
eventInfo := BackendDAE.EVENT_INFO(sampleLookup,
whenClauseLst,
zeroCrossingLst,
sampleLst,
relationsLst,
Expand Down

0 comments on commit c423888

Please sign in to comment.