Skip to content

Commit

Permalink
[BE] fix array binding vars (#8981)
Browse files Browse the repository at this point in the history
* [BE] fix array binding vars

 - scalarize variables after collecting bindings in initialization
 - allows to correctly detect dependencies and order equations
 - fix for ticket #8772 and #8938

* [testsuite] update testsuite after fix

* [testsuite] cleanup

 - remove bad tests that are sensitive to ordering changes (multi-modal nonlinear systems)

* [testsuite] more updates
  • Loading branch information
kabdelhak committed May 30, 2022
1 parent d31fbb2 commit d51b93f
Show file tree
Hide file tree
Showing 60 changed files with 749 additions and 838 deletions.
31 changes: 31 additions & 0 deletions OMCompiler/Compiler/BackEnd/BackendVariable.mo
Expand Up @@ -4591,5 +4591,36 @@ algorithm
end match;
end varExp2;

public function scalarizeVariables
input output BackendDAE.Variables vars;
protected
list<BackendDAE.Var> var_lst, new_var_lst = {};
algorithm
var_lst := varList(vars);
for var in var_lst loop
new_var_lst := scalarizeVar(var, new_var_lst);
end for;
vars := listVar(listReverse(new_var_lst));
end scalarizeVariables;

public function scalarizeVar
input BackendDAE.Var var;
input output list<BackendDAE.Var> scalar_vars = {};
protected
list<DAE.ComponentRef> scalar_crefs;
BackendDAE.Var scalar_var;
algorithm
if Types.isArray(var.varType) then
scalar_crefs := ComponentReference.expandCref(var.varName, false);
for cref in scalar_crefs loop
scalar_var := BackendVariable.copyVarNewName(cref, var);
scalar_var.varType := ComponentReference.crefTypeFull(cref);
scalar_vars := scalar_var :: scalar_vars;
end for;
else
scalar_vars := var :: scalar_vars;
end if;
end scalarizeVar;

annotation(__OpenModelica_Interface="backend");
end BackendVariable;
6 changes: 6 additions & 0 deletions OMCompiler/Compiler/BackEnd/Initialization.mo
Expand Up @@ -165,6 +165,12 @@ algorithm
((eqns, reeqns)) := BackendVariable.traverseBackendDAEVars(vars, collectInitialBindings, (eqns, reeqns));
execStat("collectInitialBindings (initialization)");

// scalarize variables after collecting bindings
if Flags.isSet(Flags.NF_SCALARIZE) then
vars := BackendVariable.scalarizeVariables(vars);
initVars := BackendVariable.scalarizeVariables(initVars);
end if;

// replace initial(), sample(...), delay(...) and homotopy(...)
useHomotopy := BackendDAEUtil.traverseBackendDAEExpsEqns(eqns, simplifyInitialFunctions, false);
execStat("simplifyInitialFunctions (initialization)");
Expand Down
Expand Up @@ -354,7 +354,7 @@ 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 NLSJac0. For more information set -d=initialization. In OMEdit Tools->Options->Simulation->Show additional information from the initialization process, in OMNotebook call setCommandLineOptions(\"-d=initialization\").
// 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->Show additional information from the initialization process, 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->Show additional information from the initialization process, 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 @@ -43,8 +43,7 @@ if 0<>system("sort -h testModel_info.json.filtered | diff - testModel_info.json.
// true
// ""
// true
// "Warning: There are nonlinear iteration variables with default zero start attribute found in NLSJac4. For more information set -d=initialization. In OMEdit Tools->Options->Simulation->Show additional information from the initialization process, 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->Show additional information from the initialization process, 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->Show additional information from the initialization process, in OMNotebook call setCommandLineOptions(\"-d=initialization\").
// "
// 0
// "OK"
Expand Down
14 changes: 7 additions & 7 deletions testsuite/openmodelica/debugDumps/dumpSparsePatternLin.mos
Expand Up @@ -28,27 +28,27 @@ buildModel(problem3); getErrorString();
// ========================================
// Number of non zero elements: 4
// Independents [or inputs] (2)
// {x[1], x[2]}
// {x[2], x[1]}
// Dependents [or outputs] (2)
// {$res_LSJac0_1, $res_LSJac0_2}
// x[1] affects the following (2) outputs
// {$res_LSJac0_2, $res_LSJac0_1}
// x[2] affects the following (2) outputs
// {$res_LSJac0_2, $res_LSJac0_1}
// x[1] affects the following (2) outputs
// {$res_LSJac0_2, $res_LSJac0_1}
//
// Transposed pattern
// $res_LSJac0_1 affects the following (2) outputs
// {x[1], x[2]}
// {x[2], x[1]}
// $res_LSJac0_2 affects the following (2) outputs
// {x[1], x[2]}
// {x[2], x[1]}
//
// --- Sparsity Coloring ---
// ========================================
// Number of colors: 2
// The following (1) independents belong to one color
// 0: {x[1]}
// 0: {x[2]}
// The following (1) independents belong to one color
// 1: {x[2]}
// 1: {x[1]}
//
// --- SparsityPattern ---
// ========================================
Expand Down
86 changes: 43 additions & 43 deletions testsuite/openmodelica/debugDumps/lateInline.mos
Expand Up @@ -526,8 +526,8 @@ buildModel(testOptdaedump); getErrorString();
//
// Variables (2)
// ========================================
// 1: y:VARIABLE() type: Real
// 2: x:VARIABLE() type: Real
// 1: x:VARIABLE() type: Real
// 2: y:VARIABLE() type: Real
//
//
// Equations (2, 2)
Expand All @@ -554,8 +554,8 @@ buildModel(testOptdaedump); getErrorString();
//
// Variables (2)
// ========================================
// 1: y:VARIABLE() type: Real
// 2: x:VARIABLE() type: Real
// 1: x:VARIABLE() type: Real
// 2: y:VARIABLE() type: Real
//
//
// Equations (2, 2)
Expand All @@ -568,13 +568,13 @@ buildModel(testOptdaedump); getErrorString();
// ========================================
// number of rows: 2
// 1: 2 1
// 2: 2
// 2: 1
//
// Transposed Adjacency Matrix (row: variable)
// ========================================
// number of rows: 2
// 1: 1
// 2: 2 1
// 1: 2 1
// 2: 1
//
// no matching
//
Expand All @@ -594,8 +594,8 @@ buildModel(testOptdaedump); getErrorString();
//
// Variables (2)
// ========================================
// 1: y:VARIABLE() type: Real
// 2: x:VARIABLE() type: Real
// 1: x:VARIABLE() type: Real
// 2: y:VARIABLE() type: Real
//
//
// Equations (2, 2)
Expand All @@ -607,14 +607,14 @@ buildModel(testOptdaedump); getErrorString();
// Matching
// ========================================
// 2 variables and equations
// var 1 is solved in eqn 1
// var 2 is solved in eqn 2
// var 1 is solved in eqn 2
// var 2 is solved in eqn 1
//
//
// StrongComponents
// ========================================
// {2:2}
// {1:1}
// {2:1}
// {1:2}
//
//
//
Expand All @@ -632,8 +632,8 @@ buildModel(testOptdaedump); getErrorString();
//
// Variables (2)
// ========================================
// 1: y:VARIABLE() type: Real
// 2: x:VARIABLE() type: Real
// 1: x:VARIABLE() type: Real
// 2: y:VARIABLE() type: Real
//
//
// Equations (2, 2)
Expand All @@ -645,14 +645,14 @@ buildModel(testOptdaedump); getErrorString();
// Matching
// ========================================
// 2 variables and equations
// var 1 is solved in eqn 1
// var 2 is solved in eqn 2
// var 1 is solved in eqn 2
// var 2 is solved in eqn 1
//
//
// StrongComponents
// ========================================
// {2:2}
// {1:1}
// {2:1}
// {1:2}
//
//
//
Expand All @@ -670,8 +670,8 @@ buildModel(testOptdaedump); getErrorString();
//
// Variables (2)
// ========================================
// 1: y:VARIABLE() type: Real
// 2: x:VARIABLE() type: Real
// 1: x:VARIABLE() type: Real
// 2: y:VARIABLE() type: Real
//
//
// Equations (2, 2)
Expand All @@ -683,14 +683,14 @@ buildModel(testOptdaedump); getErrorString();
// Matching
// ========================================
// 2 variables and equations
// var 1 is solved in eqn 1
// var 2 is solved in eqn 2
// var 1 is solved in eqn 2
// var 2 is solved in eqn 1
//
//
// StrongComponents
// ========================================
// {2:2}
// {1:1}
// {2:1}
// {1:2}
//
//
//
Expand All @@ -708,8 +708,8 @@ buildModel(testOptdaedump); getErrorString();
//
// Variables (2)
// ========================================
// 1: y:VARIABLE() type: Real
// 2: x:VARIABLE() type: Real
// 1: x:VARIABLE() type: Real
// 2: y:VARIABLE() type: Real
//
//
// Equations (2, 2)
Expand All @@ -721,14 +721,14 @@ buildModel(testOptdaedump); getErrorString();
// Matching
// ========================================
// 2 variables and equations
// var 1 is solved in eqn 1
// var 2 is solved in eqn 2
// var 1 is solved in eqn 2
// var 2 is solved in eqn 1
//
//
// StrongComponents
// ========================================
// {2:2}
// {1:1}
// {2:1}
// {1:2}
//
//
//
Expand All @@ -746,8 +746,8 @@ buildModel(testOptdaedump); getErrorString();
//
// Variables (2)
// ========================================
// 1: y:VARIABLE() type: Real
// 2: x:VARIABLE() type: Real
// 1: x:VARIABLE() type: Real
// 2: y:VARIABLE() type: Real
//
//
// Equations (2, 2)
Expand All @@ -759,14 +759,14 @@ buildModel(testOptdaedump); getErrorString();
// Matching
// ========================================
// 2 variables and equations
// var 1 is solved in eqn 1
// var 2 is solved in eqn 2
// var 1 is solved in eqn 2
// var 2 is solved in eqn 1
//
//
// StrongComponents
// ========================================
// {2:2}
// {1:1}
// {2:1}
// {1:2}
//
//
//
Expand All @@ -784,8 +784,8 @@ buildModel(testOptdaedump); getErrorString();
//
// Variables (2)
// ========================================
// 1: y:VARIABLE() type: Real
// 2: x:VARIABLE() type: Real
// 1: x:VARIABLE() type: Real
// 2: y:VARIABLE() type: Real
//
//
// Equations (2, 2)
Expand All @@ -797,14 +797,14 @@ buildModel(testOptdaedump); getErrorString();
// Matching
// ========================================
// 2 variables and equations
// var 1 is solved in eqn 1
// var 2 is solved in eqn 2
// var 1 is solved in eqn 2
// var 2 is solved in eqn 1
//
//
// StrongComponents
// ========================================
// {2:2}
// {1:1}
// {2:1}
// {1:2}
//
//
//
Expand Down

0 comments on commit d51b93f

Please sign in to comment.