Skip to content

Commit

Permalink
[BE] fix stateSelect.never
Browse files Browse the repository at this point in the history
  - force dummy selection of stateSelect.never variables
  - change adjacency matrix of states with stateSelect.never
    to SOLVABILITY_SOLVED() in all equations they appear on
    only one side (only for static state selection)
  - additional check for known variables in solvability checks

[fixup] testsuite update
  • Loading branch information
kabdelhak authored and adrpo committed Jul 5, 2019
1 parent ecca413 commit 1d78a97
Show file tree
Hide file tree
Showing 6 changed files with 235 additions and 73 deletions.
1 change: 0 additions & 1 deletion OMCompiler/Compiler/BackEnd/BackendDAECreate.mo
Expand Up @@ -173,7 +173,6 @@ algorithm
));
BackendDAEUtil.checkBackendDAEWithErrorMsg(outBackendDAE);
BackendDAEUtil.checkIncidenceMatrixSolvability(syst, functionTree);

if Flags.isSet(Flags.DUMP_BACKENDDAE_INFO) then
Error.addSourceMessage(Error.BACKENDDAEINFO_LOWER,{String(BackendEquation.equationArraySize(syst.orderedEqs)), String(BackendVariable.varsSize(syst.orderedVars))},AbsynUtil.dummyInfo);
end if;
Expand Down
125 changes: 84 additions & 41 deletions OMCompiler/Compiler/BackEnd/BackendDAEUtil.mo

Large diffs are not rendered by default.

50 changes: 50 additions & 0 deletions OMCompiler/Compiler/BackEnd/BackendVariable.mo
Expand Up @@ -2674,6 +2674,56 @@ algorithm
outMatch := inCrefIndex1.index == inCrefIndex2.index;
end removeVar2;

public function isKnownAndParam
"Returns true if all contained crefs are knownVariables and parameters"
input DAE.Exp inExp;
input BackendDAE.Variables knownVars;
output Boolean outBoolean;
protected
tuple<Boolean,BackendDAE.Variables> tpl = (true,knownVars);
algorithm
(_,(outBoolean,_)) := Expression.traverseExpBottomUp(inExp, isKnownAndParamWork, tpl);
end isKnownAndParam;

protected function isKnownAndParamWork
input output DAE.Exp inExp;
input output tuple<Boolean,BackendDAE.Variables> tpl;
protected
Boolean outBoolean;
BackendDAE.Variables knownVars;
algorithm
(outBoolean,knownVars) := tpl;
tpl := match (inExp, outBoolean)
local
DAE.ComponentRef cr;
case (_,false)
then (false,knownVars);
case (DAE.CREF(componentRef = cr),_)
then (BackendVariable.crefIsParam(cr,knownVars),knownVars);
else (true,knownVars);
end match;
end isKnownAndParamWork;

public function crefIsParam
input DAE.ComponentRef inComponentRef;
input BackendDAE.Variables inVariables;
output Boolean outBool = true;
protected
list<BackendDAE.Var> varlst;
algorithm
try
varlst := getVar(inComponentRef, inVariables);
for var in varlst loop
outBool := isParam(var);
if not outBool then
return;
end if;
end for;
else
outBool := false;
end try;
end crefIsParam;

public function existsVar
"author: PA
Return true if a variable exists in the vector"
Expand Down
74 changes: 63 additions & 11 deletions OMCompiler/Compiler/BackEnd/IndexReduction.mo
Expand Up @@ -1316,7 +1316,7 @@ protected
array<Integer> mapIncRowEqn;
BackendDAE.Variables vars;
DAE.FunctionTree funcs;
Integer numFreeStates,numOrgEqs;
Integer numFreeStates,numNeverStates,numOrgEqs;
algorithm
(so,orgEqnsLst,mapEqnIncRow,mapIncRowEqn,_) := inArg;
if Array.arrayListsEmpty(orgEqnsLst) then
Expand All @@ -1335,9 +1335,11 @@ algorithm
print("########################### STATE SELECTION ###########################\n");
end if;
// geth the number of states without stateSelect.always (free states), if the number of differentiated equations is equal to the number of free states no selection is necessary

numFreeStates := BackendVariable.traverseBackendDAEVars(vars,countStateCandidates,0);
//numNeverStates := BackendVariable.traverseBackendDAEVars(vars,countStateCandidatesWithNever,0);
numOrgEqs := countOrgEqns(orgEqnsLst,0);
//print("got "+intString(numFreeStates)+" free states and "+intString(numOrgEqs)+" orgeqns.\n");
//print("got "+ intString(numNeverStates) + " never states and " + intString(numFreeStates)+" free states and "+intString(numOrgEqs)+" orgeqns.\n");

// select dummy states
(osyst,oshared,oHt,oSetIndex) := selectStates(numFreeStates,numOrgEqs,inSystem,inShared,so,orgEqnsLst,mapEqnIncRow,mapIncRowEqn,iHt,iSetIndex);
Expand All @@ -1350,14 +1352,14 @@ algorithm
end dynamicStateSelectionWork;

protected function countStateCandidates
"author Frenkel TUD 2013-01
"author: Frenkel TUD 2013-01
count the number of states in variables"
input BackendDAE.Var inVar;
input Integer inCount;
output BackendDAE.Var outVar;
output BackendDAE.Var outVar = inVar;
output Integer outCount;
algorithm
(outVar,outCount) := match inVar
outCount := match inVar
local
Integer diffcount,statecount;
Boolean b;
Expand All @@ -1366,27 +1368,65 @@ algorithm
// do not count states with stateSelect.always
b = varStateSelectAlways(inVar);
statecount = if not b then inCount+1 else inCount;
then (inVar, statecount);
then statecount;

case BackendDAE.VAR(varKind=BackendDAE.STATE(derName=SOME(_)))
equation
// do not count states with stateSelect.always, but ignore only higest state
b = varStateSelectAlways(inVar);
statecount = if b then inCount+1 else inCount;
then (inVar, statecount);
then statecount;

case BackendDAE.VAR(varKind=BackendDAE.STATE(index=diffcount,derName=NONE()))
equation
statecount = diffcount + inCount;
// do not count states with stateSelect.always, but ignore only higest state
b = varStateSelectAlways(inVar);
statecount = if b then statecount-1 else statecount;
then (inVar, statecount);
then statecount;

else (inVar,inCount);
else inCount;
end match;
end countStateCandidates;

protected function countStateCandidatesWithNever
"author: kabdelhak FHB 2019-07
count the number of states with stateSelect.never"
input BackendDAE.Var inVar;
input Integer inCount;
output BackendDAE.Var outVar = inVar;
output Integer outCount;
algorithm
outCount := match inVar
local
Integer diffcount,statecount;
Boolean b;
case BackendDAE.VAR(varKind=BackendDAE.STATE(index=1))
equation
// do only count states with stateSelect.never
b = varStateSelectNever(inVar);
statecount = if b then inCount+1 else inCount;
then statecount;

case BackendDAE.VAR(varKind=BackendDAE.STATE(derName=SOME(_)))
equation
// do only count states with stateSelect.never, but ignore only higest state
b = varStateSelectNever(inVar);
statecount = if not b then inCount+1 else inCount;
then statecount;

case BackendDAE.VAR(varKind=BackendDAE.STATE(index=diffcount,derName=NONE()))
equation
statecount = diffcount + inCount;
// do only count states with stateSelect.never, but ignore only higest state
b = varStateSelectNever(inVar);
statecount = if not b then statecount-1 else statecount;
then statecount;

else inCount;
end match;
end countStateCandidatesWithNever;

protected function countOrgEqns
"author: Frenkel TUD 2012-06
return the number of orgens."
Expand Down Expand Up @@ -2012,7 +2052,7 @@ algorithm
hovvars = BackendVariable.listVar1(statecandidates);
eqns1 = BackendEquation.listEquation(eqnslst);
syst = BackendDAEUtil.createEqSystem(hovvars, eqns1);
(me,meT,_,_) = BackendDAEUtil.getAdjacencyMatrixEnhancedScalar(syst,inShared,false);
(me,meT,_,_) = BackendDAEUtil.getAdjacencyMatrixEnhancedScalar(syst,inShared,false,true);
m1 = incidenceMatrixfromEnhanced2(me,hovvars);
mT1 = AdjacencyMatrix.transposeAdjacencyMatrix(m1,nfreeStates);
// BackendDump.printEqSystem(syst);
Expand Down Expand Up @@ -2073,7 +2113,7 @@ algorithm
vars = BackendVariable.addVars(BackendVariable.varList(hovvars), vars);
syst = BackendDAEUtil.createEqSystem(vars, eqns);
// get advanced incidence Matrix
(me,meT,mapEqnIncRow,mapIncRowEqn) = BackendDAEUtil.getAdjacencyMatrixEnhancedScalar(syst,inShared,false);
(me,meT,mapEqnIncRow,mapIncRowEqn) = BackendDAEUtil.getAdjacencyMatrixEnhancedScalar(syst,inShared,false,true);
if Flags.isSet(Flags.BLT_DUMP) then
BackendDump.dumpAdjacencyMatrixEnhanced(me);
print("\n");
Expand Down Expand Up @@ -3180,6 +3220,18 @@ algorithm
end match;
end varStateSelectAlways;

protected function varStateSelectNever
"author: kabdelhak FHB 2019-07
return true if var is StateSelect.never else false"
input BackendDAE.Var v;
output Boolean b;
algorithm
b := match(v)
case BackendDAE.VAR(varKind=BackendDAE.STATE(),values = SOME(DAE.VAR_ATTR_REAL(stateSelectOption = SOME(DAE.NEVER())))) then true;
else false;
end match;
end varStateSelectNever;

protected function incidenceMatrixfromEnhanced2
"author: Frenkel TUD 2012-11
converts an AdjacencyMatrixEnhanced into a IncidenceMatrix"
Expand Down
34 changes: 34 additions & 0 deletions OMCompiler/Compiler/Util/List.mo
Expand Up @@ -3968,6 +3968,40 @@ algorithm
end for;
end fold5;

public function fold6<T, FT, ArgT1, ArgT2, ArgT3, ArgT4, ArgT5, ArgT6>
"Takes a list and a function operating on list elements having an extra
argument that is 'updated', thus returned from the function, and six constant
arguments that is not updated. fold will call the function for each element in
a sequence, updating the start value."
input list<T> inList;
input FoldFunc inFoldFunc;
input ArgT1 inExtraArg1;
input ArgT2 inExtraArg2;
input ArgT3 inExtraArg3;
input ArgT4 inExtraArg4;
input ArgT5 inExtraArg5;
input ArgT6 inExtraArg6;
input FT inStartValue;
output FT outResult = inStartValue;

partial function FoldFunc
input T inElement;
input ArgT1 inArg1;
input ArgT2 inArg2;
input ArgT3 inArg3;
input ArgT4 inArg4;
input ArgT5 inArg5;
input ArgT6 inArg6;
input FT inFoldArg;
output FT outFoldArg;
end FoldFunc;
algorithm
for e in inList loop
outResult := inFoldFunc(e, inExtraArg1, inExtraArg2, inExtraArg3,
inExtraArg4, inExtraArg5, inExtraArg6, outResult);
end for;
end fold6;

public function mapFold<TI, TO, FT>
"Takes a list, an extra argument and a function. The function will be applied
to each element in the list, and the extra argument will be passed to the
Expand Down
Expand Up @@ -200,26 +200,10 @@ getEnvironmentVar("REFERENCEFILES")+"/ThermoSysPro/ThermoSysPro.Examples.SimpleE
// resultFile = "ThermoSysPro.Examples.SimpleExamples.TestDynamicWaterHeating_res.mat",
// simulationOptions = "startTime = 0.0, stopTime = 1000.0, numberOfIntervals = 500, tolerance = 0.0001, method = 'dassl', fileNamePrefix = 'ThermoSysPro.Examples.SimpleExamples.TestDynamicWaterHeating', options = '', outputFormat = 'mat', variableFilter = '.*', cflags = '', simflags = ''",
// messages = "LOG_SUCCESS | info | The initialization finished successfully without homotopy method.
// stdout | warning | While solving non-linear system an assertion failed at time 524.286.
// | | | | | The non-linear solver tries to solve the problem that could take some time.
// | | | | | It could help to provide better start-values for the iteration variables.
// | | | | | For more information simulate with -lv LOG_NLS_V
// stdout | warning | While solving non-linear system an assertion failed at time 458.75.
// | | | | | The non-linear solver tries to solve the problem that could take some time.
// | | | | | It could help to provide better start-values for the iteration variables.
// | | | | | For more information simulate with -lv LOG_NLS_V
// stdout | warning | While solving non-linear system an assertion failed at time 425.982.
// | | | | | The non-linear solver tries to solve the problem that could take some time.
// | | | | | It could help to provide better start-values for the iteration variables.
// | | | | | For more information simulate with -lv LOG_NLS_V
// stdout | warning | While solving non-linear system an assertion failed at time 409.598.
// | | | | | The non-linear solver tries to solve the problem that could take some time.
// | | | | | It could help to provide better start-values for the iteration variables.
// | | | | | For more information simulate with -lv LOG_NLS_V
// stdout | warning | While solving non-linear system an assertion failed at time 681.741.
// | | | | | The non-linear solver tries to solve the problem that could take some time.
// | | | | | It could help to provide better start-values for the iteration variables.
// | | | | | For more information simulate with -lv LOG_NLS_V
//stdout | warning | While solving non-linear system an assertion failed at time 683.009.
//| | | | | The non-linear solver tries to solve the problem that could take some time.
//| | | | | It could help to provide better start-values for the iteration variables.
//| | | | | For more information simulate with -lv LOG_NLS_V
// LOG_SUCCESS | info | The simulation finished successfully.
// "
// end SimulationResult;
Expand Down

0 comments on commit 1d78a97

Please sign in to comment.