Skip to content

Commit

Permalink
[NB] update balance initialization (#11870)
Browse files Browse the repository at this point in the history
* [NB] update balance initialization

 - do not set "fixed" attribute to true here! the variable might need to be fixed again for lambda=0
 - slightly adapt the resolve singularities interface
 - add debugging output options

* [NB] update -d=initialization

 - print equations created by balance initialization

* [testsuite] small output update
  • Loading branch information
kabdelhak committed Jan 24, 2024
1 parent 6707167 commit 5ccfd22
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 9 deletions.
4 changes: 4 additions & 0 deletions OMCompiler/Compiler/NBackEnd/Modules/1_Main/NBAdjacency.mo
Expand Up @@ -102,6 +102,10 @@ public
end for;
end toString;

function empty
output Mapping mapping = MAPPING(arrayCreate(0, 0), arrayCreate(0, 0),arrayCreate(0, (0,0)),arrayCreate(0, (0,0)));
end empty;

function create
input EquationPointers eqns;
input VariablePointers vars;
Expand Down
4 changes: 2 additions & 2 deletions OMCompiler/Compiler/NBackEnd/Modules/1_Main/NBMatching.mo
Expand Up @@ -150,13 +150,13 @@ public
changed := match systemType
case NBSystem.SystemType.INI algorithm
// ####### BALANCE INITIALIZATION #######
(vars, eqns, varData, eqData, funcTree, changed) := ResolveSingularities.balanceInitialization(vars, eqns, varData, eqData, funcTree, mapping, matrixType, matching);
(vars, eqns, varData, eqData, funcTree, changed) := ResolveSingularities.balanceInitialization(vars, eqns, varData, eqData, funcTree, adj, matching, mapping, matrixType);
then changed;

else algorithm
// ####### INDEX REDUCTION ######
// for now no index reduction
(vars, eqns, varData, eqData, funcTree, changed) := ResolveSingularities.noIndexReduction(vars, eqns, varData, eqData, funcTree, mapping, matrixType, matching);
(vars, eqns, varData, eqData, funcTree, changed) := ResolveSingularities.noIndexReduction(vars, eqns, varData, eqData, funcTree, adj, matching, mapping, matrixType);
then changed;
end match;

Expand Down
Expand Up @@ -99,7 +99,6 @@ public
- add new variables in correct arrays
"
extends Module.resolveSingularitiesInterface;
input list<list<Integer>> marked_eqns_lst;
protected
UnorderedSet<Integer> marked_eqns_set;
list<Integer> marked_eqns;
Expand All @@ -115,6 +114,7 @@ public
EquationPointers constraint_ptrs;
Adjacency.Matrix set_adj;
Matching set_matching;
list<list<Integer>> marked_eqns_lst = {}; // todo: fill!

Boolean debug = false;
algorithm
Expand Down Expand Up @@ -260,7 +260,6 @@ public
function noIndexReduction
"fails if the system has unmatched variables"
extends Module.resolveSingularitiesInterface;
input Matching matching;
protected
list<Slice<VariablePointer>> unmatched_vars, matched_vars;
list<Slice<EquationPointer>> unmatched_eqns, matched_eqns;
Expand Down Expand Up @@ -299,7 +298,6 @@ public

function balanceInitialization
extends Module.resolveSingularitiesInterface;
input Matching matching;
protected
list<Slice<VariablePointer>> unmatched_vars;
list<Slice<EquationPointer>> unmatched_eqns;
Expand All @@ -309,6 +307,7 @@ public
Pointer<list<Pointer<Variable>>> ptr_start_vars = Pointer.create({});
Pointer<list<Pointer<BEquation.Equation>>> ptr_start_eqns = Pointer.create({});
Pointer<Integer> idx;
String error_msg;
algorithm
(_, unmatched_vars, _, unmatched_eqns) := Matching.getMatches(matching, mapping_opt, variables, equations);
if Flags.isSet(Flags.INITIALIZATION) then
Expand Down Expand Up @@ -347,7 +346,7 @@ public
else
// create start equations for everything else
// var = $START.var ($PRE.d = $START.d for previous vars)
var_ptr := BVariable.setFixed(var_ptr);
// DO NOT SET VARIABLE TO FIXED! we might have to fix it again for Lambda=0 system
Initialization.createStartEquationSlice(var, ptr_start_vars, ptr_start_eqns, idx);
end if;
else
Expand All @@ -365,10 +364,29 @@ public

// add new equations to system pointer arrays
equations := EquationPointers.addList(start_eqns, equations);

if Flags.isSet(Flags.INITIALIZATION) then
print(List.toString(start_eqns, function Equation.pointerToString(str = ""),
StringUtil.headline_4("Created Start Equations for balancing the Initialization (" + intString(listLength(start_eqns)) + "):"), "\t", "\n\t", "", false) + "\n\n");
end if;
else
Error.addMessage(Error.INTERNAL_ERROR,{getInstanceName()
error_msg := getInstanceName()
+ " failed because following non-fixable variables could not be solved:\n"
+ List.toString(failed_vars, BVariable.pointerToString, "", "\t", ", ", "\n", true)});
+ List.toString(failed_vars, BVariable.pointerToString, "", "\t", ", ", "\n", true);
if Flags.isSet(Flags.INITIALIZATION) then
error_msg := error_msg + "\nFollowing equations were created by fixing variables:\n"
+ List.toString(Pointer.access(ptr_start_eqns), function Equation.pointerToString(str = "\t"), "", "", "\n", "\n", true);
else
error_msg := error_msg + "\nUse -d=initialization for more debug output.";
end if;
if Flags.isSet(Flags.BLT_DUMP) then
error_msg := error_msg + "\n" + VariablePointers.toString(variables, "All") + EquationPointers.toString(equations, "All")
+ Adjacency.Mapping.toString(Util.getOptionOrDefault(mapping_opt, Adjacency.Mapping.empty()))
+ Adjacency.Matrix.toString(adj) + "\n" + Matching.toString(matching);
else
error_msg := error_msg + "\nUse -d=bltdump for more verbose debug output.";
end if;
Error.addMessage(Error.INTERNAL_ERROR,{error_msg});
fail();
end if;
else
Expand Down
2 changes: 2 additions & 0 deletions OMCompiler/Compiler/NBackEnd/Modules/NBModule.mo
Expand Up @@ -136,6 +136,8 @@ public
input output VarData varData;
input output EqData eqData;
input output FunctionTree funcTree;
input Adjacency.Matrix adj;
input Matching matching;
input Option<Adjacency.Mapping> mapping_opt;
input Adjacency.MatrixType matrixType;
output Boolean changed;
Expand Down
Expand Up @@ -100,7 +100,7 @@ simulate(simple_der_for); getErrorString();
//
// Seed Variables (1/11)
// ***********************
// (1) [STAT] (11) Real[11] x (fixed = {true for $i1 in 1:11})
// (1) [STAT] (11) Real[11] x
//
// ==============================
// Sparsity Pattern (nnz: 10)
Expand Down
Expand Up @@ -39,6 +39,10 @@ simulate(underdetermined_init); getErrorString();
// (0) Unmatched equations:
// **************************
//
// Created Start Equations for balancing the Initialization (1):
// ***************************************************************
// [SCAL] (1) y = $START.y ($RES_SIM_6)
//
// Not underdetermined.
// Not overdetermined.
// (0) Unmatched variables:
Expand Down
Expand Up @@ -74,6 +74,24 @@ simulate(hybridSys); getErrorString();
// (0) Unmatched equations:
// **************************
//
// Created Start Equations for balancing the Initialization (15):
// ****************************************************************
// [SCAL] (1) $PRE.x_Start = $START.x_Start ($RES_SIM_41)
// [SCAL] (1) $PRE.x_Loop1 = $START.x_Loop1 ($RES_SIM_42)
// [SCAL] (1) $PRE.x_Loop2 = $START.x_Loop2 ($RES_SIM_43)
// [SCAL] (1) $PRE.x_Loop3 = $START.x_Loop3 ($RES_SIM_44)
// [SCAL] (1) $PRE.x_End = $START.x_End ($RES_SIM_45)
// [SCAL] (1) $PRE.phase_Start = $START.phase_Start ($RES_SIM_46)
// [SCAL] (1) $PRE.phase_Loop3 = $START.phase_Loop3 ($RES_SIM_47)
// [SCAL] (1) $PRE.phase_Loop1 = $START.phase_Loop1 ($RES_SIM_48)
// [SCAL] (1) $PRE.phase_Loop2 = $START.phase_Loop2 ($RES_SIM_49)
// [SCAL] (1) x1 = $START.x1 ($RES_SIM_50)
// [SCAL] (1) x_End = $PRE.x_End ($RES_SIM_51)
// [SCAL] (1) x_Loop3 = $PRE.x_Loop3 ($RES_SIM_52)
// [SCAL] (1) x_Loop2 = $PRE.x_Loop2 ($RES_SIM_53)
// [SCAL] (1) x_Loop1 = $PRE.x_Loop1 ($RES_SIM_54)
// [SCAL] (1) x_Start = $PRE.x_Start ($RES_SIM_55)
//
// Not underdetermined.
// Not overdetermined.
// (0) Unmatched variables:
Expand Down

0 comments on commit 5ccfd22

Please sign in to comment.