Skip to content

Commit

Permalink
[BE] update iteration var warning and dump
Browse files Browse the repository at this point in the history
 - ticket #5602
 - fixup testsuite
  • Loading branch information
AnHeuermann authored and sjoelund committed Aug 13, 2019
1 parent c6fab8d commit 85d9636
Show file tree
Hide file tree
Showing 173 changed files with 375 additions and 304 deletions.
2 changes: 1 addition & 1 deletion OMCompiler/Compiler/BackEnd/BackendEquation.mo
Expand Up @@ -625,7 +625,7 @@ algorithm
cr_lst := BaseHashTable.hashTableKeyList(ht);
end getCrefsFromEquations;

protected function findUnknownCrefs
public function findUnknownCrefs
input output BackendDAE.Equation inEq;
input output tuple<BackendDAE.Variables, BackendDAE.Variables, HashTable.HashTable> extraArgs;
algorithm
Expand Down
8 changes: 4 additions & 4 deletions OMCompiler/Compiler/BackEnd/Initialization.mo
Expand Up @@ -292,10 +292,10 @@ algorithm
end if;

// warn about iteration variables with default zero start attribute
b := warnAboutIterationVariablesWithDefaultZeroStartAttribute(initdae);
if b and (not Flags.isSet(Flags.INITIALIZATION)) then
Error.addMessage(Error.INITIALIZATION_ITERATION_VARIABLES, {msg});
end if;
// b := warnAboutIterationVariablesWithDefaultZeroStartAttribute(initdae);
//if b and (not Flags.isSet(Flags.INITIALIZATION)) then
// Error.addMessage(Error.INITIALIZATION_ITERATION_VARIABLES, {msg});
//end if;

if Flags.isSet(Flags.DUMP_EQNINORDER) and Flags.isSet(Flags.DUMP_INITIAL_SYSTEM) then
BackendDump.dumpEqnsSolved(initdae, "initial system: eqns in order");
Expand Down
232 changes: 231 additions & 1 deletion OMCompiler/Compiler/BackEnd/SymbolicJacobian.mo
Expand Up @@ -2394,6 +2394,29 @@ algorithm
end match;
end getJacobianMatrixbyName;

public function updateJacobianDependencies
input output BackendDAE.Jacobian jacobian;
algorithm
jacobian := match jacobian
local
BackendDAE.Jacobian jac;
BackendDAE.SymbolicJacobian symJac;
BackendDAE.EqSystem syst;
BackendDAE.Shared shared;
String name;
list<BackendDAE.Var> diffVars;
list<BackendDAE.Var> diffedVars;
list<BackendDAE.Var> allDiffedVars;
list<DAE.ComponentRef> dependencies;
case jac as BackendDAE.GENERIC_JACOBIAN()
algorithm
SOME(symJac as (BackendDAE.DAE({syst}, shared),name,diffVars,diffedVars,allDiffedVars,dependencies)) := jac.jacobian;
dependencies := calcJacobianDependencies(symJac);
jac.jacobian := SOME((BackendDAE.DAE({syst}, shared),name,diffVars,diffedVars,allDiffedVars,dependencies));
then jac;
else jacobian;
end match;
end updateJacobianDependencies;

public function calcJacobianDependencies
input BackendDAE.SymbolicJacobian jacobian;
Expand Down Expand Up @@ -2621,6 +2644,7 @@ algorithm
end calculateTearingSetJacobian;

protected function calculateJacobianComponent
"Calculates jacobian matrix for strong components of torn systems and non-linear systems."
input BackendDAE.StrongComponent inComp;
input BackendDAE.Variables inVars;
input BackendDAE.EquationArray inEqns;
Expand Down Expand Up @@ -2719,13 +2743,219 @@ algorithm

// generate generic jacobian backend dae
(jacobian, shared) = getSymbolicJacobian(diffVars, eqns, resVars, oeqns, ovars, inShared, inVars, name, onlySparsePattern);

then (BackendDAE.EQUATIONSYSTEM(residualequations, iterationvarsInts, jacobian, BackendDAE.JAC_GENERIC(), mixedSystem), shared);

case (comp, _, _, _) then (comp, inShared);
end matchcontinue;

// Check if all nonlinear iteration variables have start values
if BackendDAEUtil.isInitializationDAE(inShared) then
checkNonLinDependecies(outComp,inEqns);
end if;
end calculateJacobianComponent;

protected function checkNonLinDependecies
"Check if all non-linear iteartion variables of given non-linear equation
system have a start value and throw warning if not. Only start values for
those have an influence on solver iteration."
input BackendDAE.StrongComponent inComp;
input BackendDAE.EquationArray inEqns;
protected
String name, msg;
Boolean existNonLin;
algorithm
if Flags.isSet(Flags.INITIALIZATION) then
// Dump full information.
_ := match (inComp)
local
BackendDAE.Jacobian jac;
list<Integer> resIndices, eqnIndices = {};
BackendDAE.InnerEquations innerEquations;
Boolean linear;
String str;
// Case non-linear teared equation system
case (BackendDAE.TORNSYSTEM(strictTearingSet=BackendDAE.TEARINGSET(jac=jac, residualequations=resIndices, innerEquations=innerEquations), linear=false))
algorithm
for eq in innerEquations loop
eqnIndices := match eq
local
Integer idx;
case BackendDAE.INNEREQUATION(eqn = idx) then idx::eqnIndices;
case BackendDAE.INNEREQUATIONCONSTRAINTS(eqn = idx) then idx::eqnIndices;
else eqnIndices;
end match;
end for;
eqnIndices := listAppend(resIndices,eqnIndices);
printNonLinIterVarsAndEqs(jac,eqnIndices,inEqns);
then "";

// Case non-linear non-teared equation system
case (BackendDAE.EQUATIONSYSTEM(eqns=eqnIndices, jac=jac, jacType=BackendDAE.JAC_NONLINEAR()))
algorithm
printNonLinIterVarsAndEqs(jac,eqnIndices,inEqns);
then "";

// ToDo: Check if jacType=BackendDAE.JAC_GENERIC is needed
//case BackendDAE.EQUATIONSYSTEM(jac=jac, jacType=BackendDAE.JAC_GENERIC())
else "";
end match;
else
// Only error message.
(existNonLin, name) := match (inComp)
local
BackendDAE.Jacobian jac;
Boolean linear;
String str;
// Case non-linear teared equation system
case (BackendDAE.TORNSYSTEM(strictTearingSet=BackendDAE.TEARINGSET(jac=jac), linear=false))
then existNonLinIterVars(jac);

// Case non-linear non-teared equation system
case (BackendDAE.EQUATIONSYSTEM(jac=jac, jacType=BackendDAE.JAC_NONLINEAR()))
then existNonLinIterVars(jac);

// ToDo: Check if jacType=BackendDAE.JAC_GENERIC is needed
//case BackendDAE.EQUATIONSYSTEM(jac=jac, jacType=BackendDAE.JAC_GENERIC())
else (false,"");
end match;
if existNonLin then
msg := System.gettext("For more information set -d=initialization. In OMEdit Tools->Options->Simulation->OMCFlags, in OMNotebook call setCommandLineOptions(\"-d=initialization\")");
Error.addMessage(Error.INITIALIZATION_ITERATION_VARIABLES, {name, msg});
end if;
end if;
end checkNonLinDependecies;

protected function existNonLinIterVars
"Helper function for checkNonLinDependecies. Returns true if any non-linear
iteration variables without start value are contained in given jacobian."
input BackendDAE.Jacobian jacobian_in;
output Boolean existNonLin;
output String jacName;
algorithm
(existNonLin, jacName) := match (jacobian_in)
local
list<BackendDAE.Var> diffVars, residualVars, allDiffedVars;
list<DAE.ComponentRef> dependentVarsCref;
DAE.ComponentRef varCref;
BackendDAE.Var var;
String name;
Boolean exist=false;
case BackendDAE.GENERIC_JACOBIAN(SOME((_,name,diffVars,residualVars,allDiffedVars,dependentVarsCref))) algorithm
// Search for non-linear variables without start value
for varCref in dependentVarsCref loop
for var in diffVars loop
if ComponentReference.crefEqual(varCref, var.varName) then
if (not BackendVariable.varHasStartValue(var)) then
exist:= true;
break;
end if;
end if;
end for;
if exist then
break;
end if;
end for;
then (exist, name);

// ToDo
// case BackendDAE.FULL_JACOBIAN() algorithm
else (false, "");
end match;
end existNonLinIterVars;

protected function printNonLinIterVarsAndEqs
"Helper function for checkNonLinDependecies. Prints relevant information regarding
start attributes of non linear iteration variables."
input BackendDAE.Jacobian jacobian;
input list<Integer> eqnIndices;
input BackendDAE.EquationArray inEqns;
protected
BackendDAE.EqSystem syst;
BackendDAE.Shared shared;
list<BackendDAE.Equation> nonLinEqns = {};
Integer idx = 1;
list<BackendDAE.Var> diffVars, residualVars, allDiffedVars, nonLin = {}, nonLinStart = {}, lin = {};
list<DAE.ComponentRef> dependentVarsCref;
DAE.ComponentRef varCref;
BackendDAE.Var var;
String name;
algorithm
BackendDAE.GENERIC_JACOBIAN(SOME((BackendDAE.DAE({syst}, shared),name,diffVars,residualVars,allDiffedVars,dependentVarsCref))) := jacobian;

// Get non-linear variables without start value
for varCref in dependentVarsCref loop
for var in diffVars loop
if ComponentReference.crefEqual(varCref, var.varName) then
if (not BackendVariable.varHasStartValue(var)) then
nonLin := var::nonLin;
else
nonLinStart := var::nonLinStart;
end if;
end if;
end for;
end for;
if not listEmpty(nonLin) then
BackendDump.dumpVarList(nonLin, "Nonlinear iteration variables with default zero start attribute in " + name + ".");
end if;
if not listEmpty(nonLinStart) then
BackendDump.dumpVarList(nonLinStart, "Nonlinear iteration variables with predefined start attribute in " + name + ".");
end if;

// Get linear variables with start value, but ignore discrete vars
for var in allDiffedVars loop
if (BackendVariable.varHasStartValue(var) and not BackendVariable.isVarDiscrete(var) ) then
lin := var::lin;
end if;
end for;
if not listEmpty(lin) then
BackendDump.dumpVarList(lin, "Linear iteration variables with predefined start attributes that are unrelevant in " + name + ".");
end if;

// Get nonlinear equations
for i in eqnIndices loop
nonLinEqns := addNonlinearEquation(ExpandableArray.get(i,inEqns),ExpandableArray.get(idx,syst.orderedEqs),syst.orderedVars, shared.globalKnownVars,nonLinEqns);
//BackendDump.dumpEquationList({ExpandableArray.get(i,inEqns)}, "check eq");
//BackendDump.dumpEquationList({ExpandableArray.get(idx,syst.orderedEqs)}, "diffed eq");
idx := idx + 1;
end for;
if not listEmpty(nonLinEqns) then
BackendDump.dumpEquationList(nonLinEqns, "Relevant nonlinear equations in " + name + ".");
end if;

if not (listEmpty(nonLin) and listEmpty(nonLinStart) and listEmpty(lin)) then
print("Info: Only non-linear iteration variables in non-linear eqation systems require start values. " +
"All other start values have no influence on convergence and are ignored. " +
"Use \"-d=dumpLoops\" to show all loops. In OMEdit Tools->Options->Simulation->OMCFlags, in "+
"OMNotebook call setCommandLineOptions(\"-d=dumpLoops\")\n\n");
end if;
// ToDo
// BackendDAE.FULL_JACOBIAN()
end printNonLinIterVarsAndEqs;

protected function addNonlinearEquation
"Checks an equation for nonlinearity regarding input vars and adds
it to a list if the check succeeds. "
input BackendDAE.Equation originalEqn;
input BackendDAE.Equation diffedEqn;
input BackendDAE.Variables vars;
input BackendDAE.Variables knownVars;
input output list<BackendDAE.Equation> nonlinearEquationLst;
protected
Integer eqnIndex;
HashTable.HashTable ht;
algorithm
try
ht := HashTable.emptyHashTable();
(_, (_, _, ht)) := BackendEquation.findUnknownCrefs(diffedEqn,(vars, knownVars, ht));

if not listEmpty(BaseHashTable.hashTableKeyList(ht)) then
nonlinearEquationLst := originalEqn::nonlinearEquationLst;
end if;
else
Error.addMessage(Error.INTERNAL_ERROR,{"SymbolicJacobian.addNonlinearEquation failed"});
end try;
end addNonlinearEquation;

protected function traverserhasEqnNonDiffParts
"function breaks differentiation for
currently not working parts of functions"
Expand Down
2 changes: 1 addition & 1 deletion OMCompiler/Compiler/Util/Error.mo
Expand Up @@ -849,7 +849,7 @@ public constant Message INITIALIZATION_NOT_FULLY_SPECIFIED = MESSAGE(496, TRANSL
public constant Message INITIALIZATION_OVER_SPECIFIED = MESSAGE(497, TRANSLATION(), WARNING(),
Util.gettext("The initial conditions are over specified. %s."));
public constant Message INITIALIZATION_ITERATION_VARIABLES = MESSAGE(498, TRANSLATION(), WARNING(),
Util.gettext("There are iteration variables with default zero start attribute. %s."));
Util.gettext("There are nonlinear iteration variables with default zero start attribute found in %s. %s."));
public constant Message UNBOUND_PARAMETER_WITH_START_VALUE_WARNING = MESSAGE(499, TRANSLATION(), WARNING(),
Util.gettext("Parameter %s has no value, and is fixed during initialization (fixed=true), using available start value (start=%s) as default value."));
public constant Message UNBOUND_PARAMETER_WARNING = MESSAGE(500, TRANSLATION(), WARNING(),
Expand Down
3 changes: 1 addition & 2 deletions testsuite/flattening/modelica/modification/Bug3817.mos
Expand Up @@ -765,6 +765,5 @@ simulate(PowerSystems.Examples.Spot.DrivesAC3ph.SM_ctrlAv); getErrorString();
// LOG_SUCCESS | info | The simulation finished successfully.
// "
// end SimulationResult;
// "Warning: There are iteration variables with default zero start attribute. For more information set -d=initialization. In OMEdit Tools->Options->Simulation->OMCFlags, in OMNotebook call setCommandLineOptions("-d=initialization").
// "
// ""
// endResult
Expand Up @@ -72,7 +72,6 @@ getErrorString();
// "
// end SimulationResult;
// "Warning: The initial conditions are not fully specified. For more information set -d=initialization. In OMEdit Tools->Options->Simulation->OMCFlags, in OMNotebook call setCommandLineOptions("-d=initialization").
// Warning: There are iteration variables with default zero start attribute. For more information set -d=initialization. In OMEdit Tools->Options->Simulation->OMCFlags, in OMNotebook call setCommandLineOptions("-d=initialization").
// "
// {"Files Equal!"}
// "Warning: 'compareSimulationResults' is deprecated. It is recommended to use 'diffSimulationResults' instead.
Expand Down
Expand Up @@ -72,7 +72,6 @@ getErrorString();
// "
// end SimulationResult;
// "Warning: The initial conditions are not fully specified. For more information set -d=initialization. In OMEdit Tools->Options->Simulation->OMCFlags, in OMNotebook call setCommandLineOptions("-d=initialization").
// Warning: There are iteration variables with default zero start attribute. For more information set -d=initialization. In OMEdit Tools->Options->Simulation->OMCFlags, in OMNotebook call setCommandLineOptions("-d=initialization").
// "
// {"Files Equal!"}
// "Warning: 'compareSimulationResults' is deprecated. It is recommended to use 'diffSimulationResults' instead.
Expand Down
Expand Up @@ -78,7 +78,6 @@ getErrorString();
// "
// end SimulationResult;
// "Warning: The initial conditions are not fully specified. For more information set -d=initialization. In OMEdit Tools->Options->Simulation->OMCFlags, in OMNotebook call setCommandLineOptions("-d=initialization").
// Warning: There are iteration variables with default zero start attribute. For more information set -d=initialization. In OMEdit Tools->Options->Simulation->OMCFlags, in OMNotebook call setCommandLineOptions("-d=initialization").
// "
// {"Files Equal!"}
// "Warning: 'compareSimulationResults' is deprecated. It is recommended to use 'diffSimulationResults' instead.
Expand Down
Expand Up @@ -59,8 +59,7 @@ getErrorString();
// LOG_SUCCESS | info | The simulation finished successfully.
// "
// end SimulationResult;
// "Warning: There are iteration variables with default zero start attribute. For more information set -d=initialization. In OMEdit Tools->Options->Simulation->OMCFlags, in OMNotebook call setCommandLineOptions("-d=initialization").
// "
// ""
// {"Files Equal!"}
// "Warning: 'compareSimulationResults' is deprecated. It is recommended to use 'diffSimulationResults' instead.
// "
Expand Down
Expand Up @@ -66,8 +66,7 @@ getErrorString();
// LOG_SUCCESS | info | The simulation finished successfully.
// "
// end SimulationResult;
// "Warning: There are iteration variables with default zero start attribute. For more information set -d=initialization. In OMEdit Tools->Options->Simulation->OMCFlags, in OMNotebook call setCommandLineOptions("-d=initialization").
// "
// ""
// {"Files Equal!"}
// "Warning: 'compareSimulationResults' is deprecated. It is recommended to use 'diffSimulationResults' instead.
// "
Expand Down
Expand Up @@ -425,8 +425,8 @@ getErrorString();
// end SimulationResult;
// "Notification: Following iteration variables are selected by the user for strong component 1 (DAE kind: initialization):
// x2:VARIABLE(min = -1.0 max = 1.0 ) type: Real
// Warning: There are nonlinear iteration variables with default zero start attribute found in NLSJac1. For more information set -d=initialization. In OMEdit Tools->Options->Simulation->OMCFlags, in OMNotebook call setCommandLineOptions("-d=initialization").
// Warning: The initial conditions are not fully specified. For more information set -d=initialization. In OMEdit Tools->Options->Simulation->OMCFlags, in OMNotebook call setCommandLineOptions("-d=initialization").
// Warning: There are iteration variables with default zero start attribute. For more information set -d=initialization. In OMEdit Tools->Options->Simulation->OMCFlags, in OMNotebook call setCommandLineOptions("-d=initialization").
// Notification: Following iteration variables are selected by the user for strong component 2 (DAE kind: simulation):
// x2:VARIABLE(min = -1.0 max = 1.0 ) type: Real
// "
Expand Down
Expand Up @@ -79,7 +79,6 @@ getErrorString();
// "
// end SimulationResult;
// "Warning: The model contains alias variables with conflicting start and/or nominal values. It is recommended to resolve the conflicts, because otherwise the system could be hard to solve. To print the conflicting alias sets and the chosen candidates please use -d=aliasConflicts.
// Warning: There are iteration variables with default zero start attribute. For more information set -d=initialization. In OMEdit Tools->Options->Simulation->OMCFlags, in OMNotebook call setCommandLineOptions("-d=initialization").
// "
// {"Files Equal!"}
// "Warning: 'compareSimulationResults' is deprecated. It is recommended to use 'diffSimulationResults' instead.
Expand Down
Expand Up @@ -42,7 +42,6 @@ getErrorString();
// end SimulationResult;
// true
// "Warning: The model contains alias variables with conflicting start and/or nominal values. It is recommended to resolve the conflicts, because otherwise the system could be hard to solve. To print the conflicting alias sets and the chosen candidates please use -d=aliasConflicts.
// Warning: There are iteration variables with default zero start attribute. For more information set -d=initialization. In OMEdit Tools->Options->Simulation->OMCFlags, in OMNotebook call setCommandLineOptions("-d=initialization").
// "
// record SimulationResult
// resultFile = "drumBoiler.optDrumBoiler_res.mat",
Expand Down Expand Up @@ -111,7 +110,6 @@ getErrorString();
// end SimulationResult;
// "Warning: Deprecated debug flag -d=reduceDynOpt detected. Use --postOptModules+=reduceDynamicOptimization instead.
// Warning: The model contains alias variables with conflicting start and/or nominal values. It is recommended to resolve the conflicts, because otherwise the system could be hard to solve. To print the conflicting alias sets and the chosen candidates please use -d=aliasConflicts.
// Warning: There are iteration variables with default zero start attribute. For more information set -d=initialization. In OMEdit Tools->Options->Simulation->OMCFlags, in OMNotebook call setCommandLineOptions("-d=initialization").
// "
// {"Files Equal!"}
// "Warning: 'compareSimulationResults' is deprecated. It is recommended to use 'diffSimulationResults' instead.
Expand Down
Expand Up @@ -76,7 +76,6 @@ getErrorString();
// end SimulationResult;
// "Warning: Deprecated debug flag -d=reduceDynOpt detected. Use --postOptModules+=reduceDynamicOptimization instead.
// Warning: The model contains alias variables with conflicting start and/or nominal values. It is recommended to resolve the conflicts, because otherwise the system could be hard to solve. To print the conflicting alias sets and the chosen candidates please use -d=aliasConflicts.
// Warning: There are iteration variables with default zero start attribute. For more information set -d=initialization. In OMEdit Tools->Options->Simulation->OMCFlags, in OMNotebook call setCommandLineOptions("-d=initialization").
// "
// {"Files Equal!"}
// "Warning: 'compareSimulationResults' is deprecated. It is recommended to use 'diffSimulationResults' instead.
Expand Down

5 comments on commit 85d9636

@casella
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AnHeuermann, @kabdelhak, this commit triggered 480 regressions in the library testsuite.

177 were recovered in a later commit by @kabdelhak , but there are still 300 models that remain broken, particularly in all the Buildings libraries.

Can you please check and fix this?

Thanks!

@AnHeuermann
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've made a fix which suppresses the error inside this new checkNonLinDependecies function. See PR #406
I made the initial commit but @kabdelhak finalized it and I haven't looked into the commit in detail.
This should work for the moment and we can fix this next week.

@casella
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK. We're not in a hurry, as long as we finalize this in time for the 1.14.0 release.

@casella
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll look at the next Jenkins report

@casella
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, see here

Please sign in to comment.