Skip to content

Commit e7045ec

Browse files
ptaeuberOpenModelica-Hudson
authored andcommitted
Use nominal value as start value for iteration vars
If there is no start value but a nominal value, print a warning and use the nominal value rather than 0.0
1 parent ddfec0e commit e7045ec

File tree

2 files changed

+36
-21
lines changed

2 files changed

+36
-21
lines changed

Compiler/BackEnd/Initialization.mo

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ algorithm
248248
end if;
249249

250250
// warn about iteration variables with default zero start attribute
251-
b := warnAboutIterationVariablesWithDefaultZeroStartAttribute(initdae);
251+
(initdae, b) := warnAboutIterationVariablesWithDefaultZeroStartAttribute(initdae);
252252
if b and (not Flags.isSet(Flags.INITIALIZATION)) then
253253
Error.addMessage(Error.INITIALIZATION_ITERATION_VARIABLES, {msg});
254254
end if;
@@ -624,39 +624,39 @@ end collectPreVariablesTraverseExp2;
624624

625625
protected function warnAboutIterationVariablesWithDefaultZeroStartAttribute "author: lochel
626626
This function ... read the function name."
627-
input BackendDAE.BackendDAE inDAE;
627+
input output BackendDAE.BackendDAE inDAE;
628628
output Boolean outWarning;
629+
protected
630+
list<BackendDAE.EqSystem> eqs;
629631
algorithm
630-
outWarning := warnAboutIterationVariablesWithDefaultZeroStartAttribute0(inDAE.eqs, Flags.isSet(Flags.INITIALIZATION));
632+
(eqs, outWarning) := warnAboutIterationVariablesWithDefaultZeroStartAttribute0(inDAE.eqs, Flags.isSet(Flags.INITIALIZATION));
633+
inDAE.eqs := eqs;
631634
end warnAboutIterationVariablesWithDefaultZeroStartAttribute;
632635

633636
protected function warnAboutIterationVariablesWithDefaultZeroStartAttribute0 "author: lochel"
634-
input list<BackendDAE.EqSystem> inEqs;
637+
input output list<BackendDAE.EqSystem> inEqs;
635638
input Boolean inShowWarnings;
636639
output Boolean outWarning = false;
637640
protected
638641
Boolean warn;
642+
BackendDAE.Variables orderedVars;
639643
algorithm
640644
for eqs in inEqs loop
641-
warn := warnAboutIterationVariablesWithDefaultZeroStartAttribute1(eqs, inShowWarnings);
645+
(orderedVars, warn) := warnAboutIterationVariablesWithDefaultZeroStartAttribute1(eqs, inShowWarnings);
646+
eqs.orderedVars := orderedVars;
642647
outWarning := outWarning or warn;
643-
644-
// If we found an iteration variable with default zero start attribute but
645-
// -d=initialization wasn't given, we don't need to continue searching.
646-
if warn and not inShowWarnings then
647-
return;
648-
end if;
649648
end for;
650649
end warnAboutIterationVariablesWithDefaultZeroStartAttribute0;
651650

652651
protected function warnAboutIterationVariablesWithDefaultZeroStartAttribute1 "author: lochel"
653652
input BackendDAE.EqSystem inEqSystem;
654653
input Boolean inShowWarnings;
654+
output BackendDAE.Variables orderedVars = inEqSystem.orderedVars;
655655
output Boolean outWarning = false "True if any warnings were printed.";
656656
protected
657657
BackendDAE.StrongComponents comps;
658658
list<Integer> vlst = {};
659-
list<BackendDAE.Var> vars;
659+
list<BackendDAE.Var> vars, vars1={}, vars2={};
660660
String err;
661661
algorithm
662662
BackendDAE.EQSYSTEM(matching=BackendDAE.MATCHING(comps=comps)) := inEqSystem;
@@ -681,19 +681,34 @@ algorithm
681681
// Filter out the variables that are missing start values.
682682
vars := List.map1r(vlst, BackendVariable.getVarAt, inEqSystem.orderedVars);
683683
//vars := list(BackendVariable.getVarAt(inEqSystem.orderedVars, idx) for idx in vlst);
684-
vars := list(v for v guard(not BackendVariable.varHasStartValue(v)) in vars);
685-
//vars := List.filterOnTrue(vars, BackendVariable.varHasStartValue);
684+
685+
for v in vars loop
686+
if not BackendVariable.varHasStartValue(v) then
687+
// If the variable has a nominal value, use it as start value
688+
if BackendVariable.varHasNominalValue(v) then
689+
v := BackendVariable.setVarStartValue(v, DAE.RCONST(BackendVariable.varNominal(v)));
690+
orderedVars := BackendVariable.addVar(v, inEqSystem.orderedVars);
691+
vars2 := v::vars2;
692+
else
693+
vars1 := v::vars1;
694+
end if;
695+
end if;
696+
end for;
686697

687698
// Print a warning if we found any variables with missing start values.
688-
if not listEmpty(vars) then
699+
if not listEmpty(vars1) then
700+
outWarning := true;
701+
702+
if inShowWarnings then
703+
Error.addCompilerWarning("Iteration variables with default zero start attribute in " + err + warnAboutVars2(vars1));
704+
end if;
705+
end if;
706+
707+
if not listEmpty(vars2) then
689708
outWarning := true;
690709

691710
if inShowWarnings then
692-
Error.addCompilerWarning("Iteration variables with default zero start attribute in " + err + warnAboutVars2(vars));
693-
else
694-
// If -d=initialization wasn't given we don't need to continue searching
695-
// once we've found one.
696-
return;
711+
Error.addCompilerWarning("Iteration variables with nominal value as start attribute in " + err + warnAboutVars2(vars2));
697712
end if;
698713
end if;
699714
end if;

Compiler/Util/Error.mo

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -730,7 +730,7 @@ public constant Message INITIALIZATION_NOT_FULLY_SPECIFIED = MESSAGE(496, TRANSL
730730
public constant Message INITIALIZATION_OVER_SPECIFIED = MESSAGE(497, TRANSLATION(), WARNING(),
731731
Util.gettext("The initial conditions are over specified. %s."));
732732
public constant Message INITIALIZATION_ITERATION_VARIABLES = MESSAGE(498, TRANSLATION(), WARNING(),
733-
Util.gettext("There are iteration variables with default zero start attribute. %s."));
733+
Util.gettext("There are iteration variables with default zero start attribute or nominal value as start attribute. %s."));
734734
public constant Message UNBOUND_PARAMETER_WITH_START_VALUE_WARNING = MESSAGE(499, TRANSLATION(), WARNING(),
735735
Util.gettext("Parameter %s has no value, and is fixed during initialization (fixed=true), using available start value (start=%s) as default value."));
736736
public constant Message UNBOUND_PARAMETER_WARNING = MESSAGE(500, TRANSLATION(), WARNING(),

0 commit comments

Comments
 (0)