Skip to content

Commit

Permalink
Fix variability of alias variables (#496)
Browse files Browse the repository at this point in the history
  • Loading branch information
arun3688 authored and lochel committed Nov 8, 2019
1 parent e658cdc commit a7c5e31
Show file tree
Hide file tree
Showing 18 changed files with 4,142 additions and 3,676 deletions.
1 change: 1 addition & 0 deletions .CI/compliance-newinst.failures
Expand Up @@ -209,3 +209,4 @@ ModelicaCompliance.Scoping.Visibility.ModifyProtectedClass
ModelicaCompliance.Scoping.Visibility.ModifyProtectedComp
ModelicaCompliance.Scoping.Visibility.RedeclareProtectedClass
ModelicaCompliance.Scoping.Visibility.RedeclareProtectedComp
ModelicaCompliance.Components.Variability.ConstantNoBinding
3 changes: 2 additions & 1 deletion .CI/compliance.failures
Expand Up @@ -200,11 +200,12 @@ ModelicaCompliance.Scoping.Visibility.AccessProtectedClassComp
ModelicaCompliance.Scoping.Visibility.AccessProtectedComp
ModelicaCompliance.Scoping.Visibility.AccessProtectedCompClass
ModelicaCompliance.Scoping.Visibility.AccessProtectedCompComp
ModelicaCompliance.Scoping.Visibility.EnclosingAccessProtectedComp
ModelicaCompliance.Scoping.Visibility.ModifyProtectedClass
ModelicaCompliance.Scoping.Visibility.ModifyProtectedComp
ModelicaCompliance.Scoping.Visibility.ProtectedMultiClass
ModelicaCompliance.Scoping.Visibility.ProtectedMultiComp
ModelicaCompliance.Scoping.Visibility.RedeclareInheritedProtectedComp
ModelicaCompliance.Scoping.Visibility.RedeclareProtectedClass
ModelicaCompliance.Scoping.Visibility.RedeclareProtectedComp
ModelicaCompliance.Components.Variability.ConstantNoBinding
ModelicaCompliance.Connections.Restrictions.ConnectMismatchConstant
5 changes: 3 additions & 2 deletions .gitignore
Expand Up @@ -10,13 +10,14 @@
/config.sub
/configure
/install-sh
/issues/
/make.log
/Makefile
autom4te.cache/
build/
NewDocumentation/
OMEncryption/
OMPublicKey/
OMPython/
OMSetup/
OpenModelicaSetup/
OMEncryption/
OMPublicKey/
8 changes: 8 additions & 0 deletions OMCompiler/Compiler/BackEnd/BackendVariable.mo
Expand Up @@ -1191,6 +1191,14 @@ algorithm
end match;
end isParam;

public function isParamOrConstant
"Return true if variable is parameter or constant"
input BackendDAE.Var invar;
output Boolean outbool=false;
algorithm
outbool := isParam(invar) or isConst(invar);
end isParamOrConstant;

public function isIntParam
"Return true if variable is a parameter and integer."
input BackendDAE.Var inVar;
Expand Down
78 changes: 78 additions & 0 deletions OMCompiler/Compiler/BackEnd/RemoveSimpleEquations.mo
Expand Up @@ -165,6 +165,7 @@ algorithm

outDAE := fixAliasVars(outDAE) "workaround for #3323";
outDAE := fixAliasAndKnownVarsCausal(inDAE, outDAE);
outDAE := fixAliasVarsVariablity(outDAE) "workaround for #5673";
else
// This case performs "remove simple equations" on an acausal system.
outDAE := match(Flags.getConfigString(Flags.REMOVE_SIMPLE_EQUATIONS))
Expand All @@ -177,6 +178,7 @@ algorithm

outDAE := fixAliasVars(outDAE) "workaround for #3323";
outDAE := fixKnownVars(outDAE);
outDAE := fixAliasVarsVariablity(outDAE) "workaround for #5673";
end if;
end removeSimpleEquations;

Expand All @@ -192,6 +194,82 @@ algorithm
end if;
end removeVerySimpleEquations;

public function fixAliasVarsVariablity
" This is a workaround for #5673
This module traverses all alias variables and double-checks if the referenced alias variables variablitly
is parameter or not and changes variablilty of alias variable to BackendDAE.PARAM()."
input BackendDAE.BackendDAE inDAE;
output BackendDAE.BackendDAE outDAE;
protected
BackendDAE.Variables aliasVars, systvars, globalKnownVars;
DAE.Exp binding;
list<DAE.ComponentRef> crefs;
Boolean paramOrConst, const;
BackendDAE.Shared shared;
list<BackendDAE.Var> referencevar, tempreferencevar, knownVarList={}, aliasVarList={};
BackendDAE.Var tempvar;
algorithm
aliasVars := BackendDAEUtil.getAliasVars(inDAE);
systvars := BackendVariable.listVar(BackendVariable.equationSystemsVarsLst(inDAE.eqs));

//BackendDump.dumpVariables(aliasVars, "aliasVariable-Actual");
for var in BackendVariable.varList(aliasVars) loop
binding := BackendVariable.varBindExp(var);
crefs := Expression.getAllCrefs(binding);

// look up for the reference variable variablity
// check in shared.globalKnownVars if not found in orderedVars
referencevar := {};
for cr in crefs loop
tempreferencevar := getVarsHelper(cr, systvars); // first check in EqSystem.OrderedVars
if listEmpty(tempreferencevar) then
tempreferencevar := getVarsHelper(cr,inDAE.shared.globalKnownVars);
end if;
referencevar := listAppend(referencevar,tempreferencevar);
end for;

// check list of referencevariable either PARAM() or CONST()
if listEmpty(referencevar) then
paramOrConst := false;
const := false;
else
paramOrConst := List.mapAllValueBool(referencevar, BackendVariable.isParamOrConstant, true);
const := List.mapAllValueBool(referencevar, BackendVariable.isConst, true);
end if;

// remove the variable from aliasVarList and add it to knownVarList after changing it to PARAM()/CONST() and fixed=true
if const then
tempvar := BackendVariable.setVarKind(var,BackendDAE.CONST());
knownVarList := BackendVariable.setVarFixed(tempvar, true) :: knownVarList;
elseif paramOrConst then
tempvar := BackendVariable.setVarKind(var,BackendDAE.PARAM());
knownVarList := BackendVariable.setVarFixed(tempvar, true) :: knownVarList;
else
aliasVarList := var :: aliasVarList;
end if;
end for;

//BackendDump.dumpVarList(aliasVarList, "AfterChangingParameters");
// add the parameter dependent alias vars to Global known Vars after removing from aliasVarList
globalKnownVars := BackendVariable.mergeVariables(inDAE.shared.globalKnownVars, BackendVariable.listVar(knownVarList));

outDAE := BackendDAEUtil.setAliasVars(inDAE, BackendVariable.listVar(aliasVarList));
outDAE := BackendDAEUtil.setDAEGlobalKnownVars(outDAE, globalKnownVars);
end fixAliasVarsVariablity;

protected function getVarsHelper
input DAE.ComponentRef cr;
input BackendDAE.Variables vars;
output list<BackendDAE.Var> outVars;
algorithm
try
(outVars, _) := BackendVariable.getVar(cr, vars);
else
// guard against failures
outVars := {};
end try;
end getVarsHelper;

protected function fixAliasVars "author: lochel
This is a workaround for #3323
TODO: Remove this once removeSimpleEquations is implemented properly.
Expand Down
3 changes: 2 additions & 1 deletion testsuite/openmodelica/cppruntime/Makefile
Expand Up @@ -23,7 +23,8 @@ testMatrixIO.mos \
testVectorizedBlocks.mos \
testVectorizedPowerSystem.mos \
testVectorizedSolarSystem.mos \
trapezoidTest.mos
trapezoidTest.mos \
negatedParameter.mos

FAILINGTESTFILES= \
ClockInterval.mos \
Expand Down

0 comments on commit a7c5e31

Please sign in to comment.