Skip to content

Commit

Permalink
Assigning equations to states
Browse files Browse the repository at this point in the history
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@23181 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
bernhard-thiele committed Nov 4, 2014
1 parent 38dc646 commit b0599b4
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 41 deletions.
13 changes: 10 additions & 3 deletions Compiler/BackEnd/HashTableSM.mo
Expand Up @@ -9,7 +9,8 @@ protected import HashSet;
protected import BaseHashSet;
protected import List;
protected import BackendDAE;

protected import BackendDump;
protected import BackendEquation;

public type Key = DAE.ComponentRef;
public type Value = StateMachineFeatures.Mode;
Expand Down Expand Up @@ -69,14 +70,20 @@ protected
String name;
Boolean isInitial;
HashSet.HashSet edges;
BackendDAE.EquationArray eqs;
list<DAE.ComponentRef> crefs;
list<BackendDAE.Equation> eqsList;
list<String> paths;
list<String> eqsDump;
algorithm
StateMachineFeatures.MODE(name=name, isInitial=isInitial, edges=edges) := mode;
StateMachineFeatures.MODE(name=name, isInitial=isInitial, edges=edges, eqs=eqs) := mode;
crefs := BaseHashSet.hashSetList(edges);
paths := List.map(crefs, ComponentReference.printComponentRefStr);
eqsList := BackendEquation.equationList(eqs);
eqsDump := List.map(eqsList, BackendDump.equationString);
s := "MODE(" + stringDelimitList({name,boolString(isInitial)}, ",") + "), "
+ "EDGES(" + stringDelimitList(paths, ", ") +"))";
+ "EDGES(" + stringDelimitList(paths, ", ") +"), "
+ "Equations( "+ stringDelimitList(eqsDump, ";\n\t") +")";
end modeToString;

annotation(__OpenModelica_Interface="backend");
Expand Down
156 changes: 118 additions & 38 deletions Compiler/BackEnd/StateMachineFeatures.mo
Expand Up @@ -55,6 +55,7 @@ protected import Util;
protected import HashSet;
protected import BaseHashSet;
protected import HashTableSM;
protected import Array;

/*
public
Expand Down Expand Up @@ -132,8 +133,8 @@ protected
list<FlatAutomaton> flatAutomata;
list<Composition> compositions;
list<String> ss;
BackendDAE.EqSystem syst;
BackendDAE.Shared shared;
BackendDAE.EqSystem syst, systNew;
BackendDAE.Shared shared, sharedNew;
algorithm
(syst, shared) := match inDAE
local
Expand All @@ -144,7 +145,6 @@ algorithm
BackendDAE.DAE({syst1}, shared1) = BackendDAEOptimize.collapseIndependentBlocks(inDAE);
then (syst1, shared1);
end match;
outDAE := BackendDAE.DAE({syst}, shared); // dummy assignment as long as elabortion is not implemented

// Identify modes in the system
modes := identifyModes(shared);
Expand Down Expand Up @@ -177,7 +177,10 @@ algorithm
print(stringDelimitList(ss, ",\n") + "\n");

print("SMF-stateMachineElab: annotate modes with additional information\n");
modes := annotateModes(modes, syst, shared);
(modes, systNew, sharedNew) := annotateModes(modes, syst, shared);
BaseHashTable.dumpHashTable(modes);

outDAE := BackendDAE.DAE({syst}, shared); // dummy assignment as long as elabortion is not implemented

end stateMachineElab;

Expand All @@ -188,55 +191,132 @@ Tbd."
input ModeTable modesIn;
input BackendDAE.EqSystem inSyst;
input BackendDAE.Shared inShared;
//output list<BackendDAE.EqSystem> outSysts;
output ModeTable modesOut;
output BackendDAE.EqSystem outSyst;
output BackendDAE.Shared outShared;
protected
BackendDAE.EquationArray removedEqs, orderedEqs;
//BackendDAE.Variables vars;
//BackendDAE.EquationArray eqs;
//BackendDAE.StateSets stateSets;
BackendDAE.EquationArray orderedEqsNew, removedEqsNew;
// Fields EQSYSTEM:
BackendDAE.Variables orderedVars;
BackendDAE.EquationArray orderedEqs;
Option<BackendDAE.IncidenceMatrix> m;
Option<BackendDAE.IncidenceMatrixT> mT;
BackendDAE.Matching matching;
BackendDAE.StateSets stateSets;
BackendDAE.BaseClockPartitionKind partitionKind;
// Fields SHARED:
BackendDAE.Variables knownVars;
BackendDAE.Variables externalObjects;
BackendDAE.Variables aliasVars;
BackendDAE.EquationArray initialEqs;
BackendDAE.EquationArray removedEqs;
list<DAE.Constraint> constraints;
list<DAE.ClassAttributes> classAttrs;
FCore.Cache cache;
FCore.Graph graph;
DAE.FunctionTree functionTree;
BackendDAE.EventInfo eventInfo;
BackendDAE.ExternalObjectClasses extObjClasses;
BackendDAE.BackendDAEType backendDAEType;
BackendDAE.SymbolicJacobians symjacs;
BackendDAE.ExtraInfo info;
// Fields of EQUATION_ARRAY
Integer orderedSize, removedSize;
Integer orderedNumberOfElement, removedNumberOfElement;
Integer orderedArrSize, removedArrSize;
array<Option<BackendDAE.Equation>> orderedEquOptArr, removedEquOptArr;
algorithm
BackendDAE.EQSYSTEM(orderedEqs=orderedEqs) := inSyst;
BackendDAE.SHARED(removedEqs=removedEqs) := inShared;

modesOut := BackendEquation.traverseBackendDAEEqns(orderedEqs, annotateMode, modesIn);
modesOut := BackendEquation.traverseBackendDAEEqns(removedEqs, annotateMode, modesOut);

//BackendDAE.EQSYSTEM(vars, eqs, _, _, _, stateSets, _) := inSyst;
//outSysts := {inSyst};
BackendDAE.EQSYSTEM(orderedVars, orderedEqs, m, mT, matching, stateSets, partitionKind) := inSyst;
BackendDAE.SHARED(knownVars, externalObjects, aliasVars, initialEqs, removedEqs, constraints,
classAttrs, cache, graph, functionTree, eventInfo, extObjClasses,backendDAEType, symjacs, info) := inShared;

BackendDAE.EQUATION_ARRAY(orderedSize, orderedNumberOfElement, orderedArrSize, orderedEquOptArr) := orderedEqs;
BackendDAE.EQUATION_ARRAY(removedSize, removedNumberOfElement, removedArrSize, removedEquOptArr) := removedEqs;
(orderedEquOptArr, modesOut) := Array.mapNoCopy_1(orderedEquOptArr, annotateMode, modesIn);
(removedEquOptArr, modesOut) := Array.mapNoCopy_1(removedEquOptArr, annotateMode, modesOut);

orderedEqsNew := BackendDAE.EQUATION_ARRAY(orderedSize, orderedNumberOfElement, orderedArrSize, orderedEquOptArr);
removedEqsNew := BackendDAE.EQUATION_ARRAY(removedSize, removedNumberOfElement, removedArrSize, removedEquOptArr);

//(orderedEqsNew, modesOut) := BackendEquation.traverseBackendDAEEqnsWithUpdate(orderedEqs, annotateMode, modesIn);
//(removedEqsNew, modesOut) := BackendEquation.traverseBackendDAEEqnsWithUpdate(removedEqs, annotateMode, modesOut);

// A lot of code bloat just for updating fields "orderedEqs" and "removedEqs" in "EQSYSTEM" and "SHARED" ...
outSyst := BackendDAE.EQSYSTEM(orderedVars, orderedEqsNew, m, mT, matching, stateSets, partitionKind);
outShared := BackendDAE.SHARED(knownVars, externalObjects, aliasVars, initialEqs, removedEqsNew, constraints,
classAttrs, cache, graph, functionTree, eventInfo, extObjClasses,backendDAEType, symjacs, info);

// TODO Could use new features https://trac.openmodelica.org/OpenModelica/changeset/23138 for updating...
// What syntax would be even nicer to fight the code bloat?
//outShared := inShared(removedEqs=removedEqsNew);
//outShared := BackendDAE.SHARED(removedEqs=removedEqsNew) <- inShared
//outShared := inShared -> BackendDAE.SHARED(removedEqs=removedEqsNew)
//outShared := inShared /. BackendDAE.SHARED(removedEqs=removedEqsNew)
end annotateModes;

protected function annotateMode "
function annotateMode "
Author: BTH
Helper function to annotateModes
Tbd."
input BackendDAE.Equation inEq;
input ModeTable inA;
output BackendDAE.Equation outEq;
output ModeTable outA;
Tbd"
input tuple<Option<BackendDAE.Equation>, ModeTable> inEqModeTable;
output tuple<Option<BackendDAE.Equation>, ModeTable> outEqModeTable;
protected
DAE.ElementSource source "origin of equation";
Option<DAE.ComponentRef> instanceOpt "the instance(s) this element is part of";
Option<BackendDAE.Equation> eqOpt;
ModeTable modeTable;
Option<DAE.ElementSource> sourceOpt "origin of equation";
Option<DAE.ComponentRef> instanceOpt "the instance(s) this element is part of";
Option<Mode> optMode;
algorithm
(eqOpt, modeTable) := inEqModeTable;
// Just match any possible Equation type and extract the source field.
// TODO More specific handling with error handling (e.g., WHEN_EQUATION not allowed in a state)
source := match (inEq, inA)
sourceOpt := match (eqOpt)
local
DAE.ElementSource source1;
case (BackendDAE.EQUATION(source=source1),_) then source1;
case (BackendDAE.ARRAY_EQUATION(source=source1),_) then source1;
case (BackendDAE.SOLVED_EQUATION(source=source1),_) then source1;
case (BackendDAE.RESIDUAL_EQUATION(source=source1),_) then source1;
case (BackendDAE.ALGORITHM(source=source1),_) then source1;
case (BackendDAE.WHEN_EQUATION(source=source1),_) then source1;
case (BackendDAE.COMPLEX_EQUATION(source=source1),_) then source1;
case (BackendDAE.IF_EQUATION(source=source1),_) then source1;
case SOME((BackendDAE.EQUATION(source=source1))) then SOME(source1);
case SOME((BackendDAE.ARRAY_EQUATION(source=source1))) then SOME(source1);
case SOME((BackendDAE.SOLVED_EQUATION(source=source1))) then SOME(source1);
case SOME((BackendDAE.RESIDUAL_EQUATION(source=source1))) then SOME(source1);
case SOME((BackendDAE.ALGORITHM(source=source1))) then SOME(source1);
case SOME((BackendDAE.WHEN_EQUATION(source=source1))) then SOME(source1);
case SOME((BackendDAE.COMPLEX_EQUATION(source=source1))) then SOME(source1);
case SOME((BackendDAE.IF_EQUATION(source=source1))) then SOME(source1);
else NONE();
end match;

instanceOpt := match (sourceOpt)
local
Option<DAE.ComponentRef> crefOpt;
case SOME(DAE.SOURCE(instanceOpt=crefOpt)) then crefOpt;
else NONE();
end match;

// Is there a mode/state that corresponds to the instance?
optMode := match (instanceOpt)
local
DAE.ComponentRef cref;
case SOME(cref) guard BaseHashTable.hasKey(cref, modeTable)
then SOME(BaseHashTable.get(cref, modeTable));
else NONE();
end match;

outEqModeTable := match (optMode, instanceOpt, eqOpt)
local
DAE.ComponentRef cref;
BackendDAE.Equation eq;
ModeTable modeTableNew;
String name;
Boolean isInitial;
HashSet.HashSet edges;
BackendDAE.EquationArray eqs;
case (SOME(MODE(name,isInitial,edges,eqs)), SOME(cref), SOME(eq))
equation
eqs = BackendEquation.addEquation(eq, eqs);
modeTableNew = BaseHashTable.update((cref, MODE(name,isInitial,edges,eqs)), modeTable);
then (NONE(), modeTableNew);
else (eqOpt, modeTable);
end match;

DAE.SOURCE(instanceOpt=instanceOpt) := source;
// TODO
outEq := inEq; // TODO
outA := inA; // TODO
end annotateMode;


Expand Down

0 comments on commit b0599b4

Please sign in to comment.