Skip to content
This repository has been archived by the owner on May 18, 2019. It is now read-only.

Commit

Permalink
Correct handling of parameters
Browse files Browse the repository at this point in the history
This commit corrects the improper and inconsistent handling
of parameters by implementing what is listed below:

- Only evaluate structural parameters and parameters with
  annotation(Evaluate=true) during compile time by default.
  Final parameters and protected parameters are not evaluated
  any more.
  (see ticket:3211, ticket:4027)

- Many different preOpt modules are merged to one preOpt module
  "evaluateParameters" with different flags instead
  -> To calculate additional parameters the following flags are introduced:
     --evaluateFinalParameters=<true|false> (default: false)
     --evaluateProtectedParameters=<true|false> (default: false)

  -> To activate/deactivate the replacement of the calculated parameters
     in the DAE use:
     --replaceEvaluatedParameters=<true|false> (default: true)

- Mark structural parameters deciding the if-branch final
  (see ticket ticket:4053)

- Structural parameters in instances are detected correctly
  (see ticket:4059)

- Propagate the final attribute to parameters structural parameters
  depend on
  (see ticket:4052)

- Mark parameters final if used for calculation of other parameters,
  because otherwise it leads to inconsistency when changing the value
  after compilation.
  Also replace the evaluated expressions because they are already evaluated
  and not changeable anymore.
  (see ticket:4027, ticket:3211)

- Make final, structural and protected parameters unchangeable
  -> Write final parameters to _08bnd.c
  -> Make use of the isValueChangeable-flag
  (see ticket:4060, ticket:3211, ticket:4027)
  • Loading branch information
ptaeuber authored and OpenModelica-Hudson committed Oct 20, 2016
1 parent c2ed655 commit d75f648
Show file tree
Hide file tree
Showing 9 changed files with 501 additions and 680 deletions.
18 changes: 9 additions & 9 deletions Compiler/BackEnd/BackendDAEUtil.mo
Original file line number Diff line number Diff line change
Expand Up @@ -4978,6 +4978,14 @@ algorithm
(e,_) = Expression.traverseExpBottomUp(e, replaceVartraverser, vars);
then (e, vars);

case (DAE.CREF(componentRef=cr),vars)
equation
(v::_,_) = BackendVariable.getVar(cr,vars);
true = BackendVariable.varFixed(v);
e = BackendVariable.varBindExpStartValue(v);
(e,_) = Expression.traverseExpBottomUp(e, replaceVartraverser, vars);
then (e, vars);

else (inExp,inVars);

end matchcontinue;
Expand Down Expand Up @@ -7252,14 +7260,7 @@ protected function allPreOptimizationModules
(UnitCheck.unitChecking, "unitChecking"),
(DynamicOptimization.createDynamicOptimization,"createDynamicOptimization"),
(BackendInline.normalInlineFunction, "normalInlineFunction"),
(EvaluateParameter.evaluateEvaluateParameters, "evaluateEvaluateParameters"),
(EvaluateParameter.evaluateFinalEvaluateParameters, "evaluateFinalEvaluateParameters"),
(EvaluateParameter.evaluateFinalParameters, "evaluateFinalParameters"),
(EvaluateParameter.evaluateReplaceFinalEvaluateParameters, "evaluateReplaceFinalEvaluateParameters"),
(EvaluateParameter.evaluateReplaceFinalParameters, "evaluateReplaceFinalParameters"),
(EvaluateParameter.evaluateAllParameters, "evaluateAllParameters"),
(EvaluateParameter.evaluateReplaceEvaluateParameters, "evaluateReplaceEvaluateParameters"),
(EvaluateParameter.evaluateReplaceProtectedFinalEvaluateParameters, "evaluateReplaceProtectedFinalEvaluateParameters"),
(EvaluateParameter.evaluateParameters, "evaluateParameters"),
(RemoveSimpleEquations.removeVerySimpleEquations, "removeVerySimpleEquations"),
(StateMachineFeatures.stateMachineElab, "stateMachineElab"),
(BackendDAEOptimize.simplifyIfEquations, "simplifyIfEquations"),
Expand Down Expand Up @@ -7402,7 +7403,6 @@ algorithm

enabledModules := deprecatedDebugFlag(Flags.SORT_EQNS_AND_VARS, enabledModules, "sortEqnsVars", "preOptModules+");
enabledModules := deprecatedDebugFlag(Flags.RESOLVE_LOOPS, enabledModules, "resolveLoops", "preOptModules+");
enabledModules := deprecatedDebugFlag(Flags.EVAL_ALL_PARAMS, enabledModules, "evaluateAllParameters", "preOptModules+");
enabledModules := deprecatedDebugFlag(Flags.ADD_DER_ALIASES, enabledModules, "introduceDerAlias", "preOptModules+");
if Config.acceptOptimicaGrammar() or Flags.getConfigBool(Flags.GENERATE_DYN_OPTIMIZATION_PROBLEM) then
enabledModules := "inputDerivativesForDynOpt"::enabledModules;
Expand Down
7 changes: 7 additions & 0 deletions Compiler/BackEnd/BackendVariable.mo
Original file line number Diff line number Diff line change
Expand Up @@ -1302,6 +1302,13 @@ algorithm
select := isFinalVar(inVar) or hasVarEvaluateAnnotation(inVar);
end hasVarEvaluateAnnotationOrFinal;

public function hasVarEvaluateAnnotationOrProtected
input BackendDAE.Var inVar;
output Boolean select;
algorithm
select := isProtectedVar(inVar) or hasVarEvaluateAnnotation(inVar);
end hasVarEvaluateAnnotationOrProtected;

public function hasVarEvaluateAnnotationOrFinalOrProtected
input BackendDAE.Var inVar;
output Boolean select;
Expand Down
1,030 changes: 415 additions & 615 deletions Compiler/BackEnd/EvaluateParameter.mo

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions Compiler/FFrontEnd/FCore.mo
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,9 @@ algorithm
case (CACHE(initialGraph,functions,(ht,crs::st),p,program),SCode.PARAM(),_)
then CACHE(initialGraph,functions,(ht,(cr::crs)::st),p,program);

case (CACHE(initialGraph,functions,(ht,{}),p,program),SCode.PARAM(),_)
then CACHE(initialGraph,functions,(ht,{cr}::{}),p,program);

else cache;

end match;
Expand Down
4 changes: 4 additions & 0 deletions Compiler/FrontEnd/InstSection.mo
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ protected import Inst;
protected import InstDAE;
protected import InstFunction;
protected import InstTypes;
protected import InstUtil;
protected import NFInstUtil;
protected import List;
protected import Lookup;
Expand Down Expand Up @@ -396,6 +397,9 @@ algorithm
rest_branches := listRest(rest_branches);
end for;

// Add evaluated parameter condition to the structural parameter list to mark it final later
outCache := InstUtil.popStructuralParameters(outCache,inPrefix);

// A branch was selected, instantiate it.
(outCache, outEnv, outIH, outDae, outSets, outState, outGraph) :=
Inst.instList(outCache, inEnv, inIH, inPrefix, inSets, inState,
Expand Down
1 change: 1 addition & 0 deletions Compiler/FrontEnd/InstUtil.mo
Original file line number Diff line number Diff line change
Expand Up @@ -7749,6 +7749,7 @@ algorithm
equation
ht = prefixAndAddCrefsToHt(cache,ht,pre,crs);
then FCore.CACHE(ie,f,(ht,crss),p,program);
case (FCore.CACHE(ie,f,(ht,{}),p,program),_) then cache;
case (FCore.NO_CACHE(),_) then cache;
end match;
end popStructuralParameters;
Expand Down
20 changes: 12 additions & 8 deletions Compiler/SimCode/SimCodeUtil.mo
Original file line number Diff line number Diff line change
Expand Up @@ -6329,17 +6329,22 @@ algorithm
BackendDump.dumpVarList(inPrimaryParameters, "parameters in order");
end if;

for p in inPrimaryParameters loop
(simEq, outUniqueEqIndex) := makeSolved_SES_SIMPLE_ASSIGN_fromStartValue(p, outUniqueEqIndex);
outParameterEquations := simEq::outParameterEquations;
end for;

// get min/max and nominal asserts
varasserts := {};
for p in inAllPrimaryParameters loop
if BackendVariable.isFinalOrProtectedVar(p) and Expression.isConst(BackendVariable.varBindExpStartValue(p)) then
(simEq, outUniqueEqIndex) := makeSolved_SES_SIMPLE_ASSIGN_fromStartValue(p, outUniqueEqIndex);
outParameterEquations := simEq::outParameterEquations;
end if;
varasserts2 := createVarAsserts(p);
varasserts := List.append_reverse(varasserts2, varasserts);
end for;

for p in inPrimaryParameters loop
(simEq, outUniqueEqIndex) := makeSolved_SES_SIMPLE_ASSIGN_fromStartValue(p, outUniqueEqIndex);
outParameterEquations := simEq::outParameterEquations;
end for;

varasserts := MetaModelica.Dangerous.listReverseInPlace(varasserts);
(simvarasserts, outUniqueEqIndex) := List.mapFold(varasserts, dlowAlgToSimEqSystem, outUniqueEqIndex);

Expand Down Expand Up @@ -8478,9 +8483,8 @@ algorithm
// print("name: " + ComponentReference.printComponentRefStr(cr) + "indx: " + intString(indx) + "\n");
// check if the variable has changeable value
// parameter which has final = true or evaluate annotation are not changeable
isValueChangeable = ((not BackendVariable.hasVarEvaluateAnnotationOrFinal(dlowVar)
and BackendVariable.varHasConstantBindExp(dlowVar))
or not BackendVariable.varHasBindExp(dlowVar))
isValueChangeable = ((not BackendVariable.hasVarEvaluateAnnotationOrFinalOrProtected(dlowVar)
and (BackendVariable.varHasConstantBindExp(dlowVar) or not BackendVariable.varHasBindExp(dlowVar))))
and isFixed;
then
SimCodeVar.SIMVAR(cr, kind, commentStr, unit, displayUnit, -1 /* use -1 to get an error in simulation if something failed */,
Expand Down

0 comments on commit d75f648

Please sign in to comment.