Skip to content

Commit

Permalink
Debugged matching algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
Kalashnikovni committed Mar 21, 2022
1 parent 6086577 commit cf960b2
Show file tree
Hide file tree
Showing 29 changed files with 223 additions and 33 deletions.
Binary file modified 3rd-party/sbg/sb-graph-dev.tar.gz
Binary file not shown.
2 changes: 1 addition & 1 deletion causalize/sbg_implementation/test/models_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ TEST_P(ITests, GenerateCode)
const std::string NAME = GetParam();
std::cout << "Testing model: " << NAME << std::endl;
const std::string FLATTER = "../../../bin/causalize ";
const std::string ARGS = "-o ./test_data/ ../../../test/mccprograms/causalize/" + NAME + ".mo";
const std::string ARGS = "-o ./test_data/ ../../../test/mccprograms/" + NAME + ".mo";
const std::string RESULT_FILE = "./test_data/" + NAME + ".passed";
const std::string TEST_CMD = "./test_results.sh " + NAME;
const std::string FLATTER_CMD = FLATTER + ARGS;
Expand Down
22 changes: 11 additions & 11 deletions flatter/test/gt_data/SBGraph1.mo
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,22 @@ model SBGraph1
Real C_p_eff[N];
Real C_p_fl[N];
equation
for i in 1:1:999 loop
C_p_eff[i] = R_p_eff[i+1];
C_p_eff[i] = R_n_eff[i];
end for;
for i in 1:1:999 loop
C_p_fl[i]+R_p_fl[i+1]+R_n_fl[i] = 0;
end for;
G_n_fl = 0;
C_p_eff[1000] = R_n_eff[1000];
C_p_fl[1000]+R_n_fl[1000] = 0;
C_n_eff[1] = S_n_eff;
C_n_eff[1] = G_p_eff;
for i in 1:1:999 loop
C_n_eff[1] = C_n_eff[i+1];
end for;
C_n_fl[1]+S_n_fl+G_p_fl+sum(C_n_fl[2:1:1000]) = 0;
C_p_eff[1000] = R_n_eff[1000];
C_p_fl[1000]+R_n_fl[1000] = 0;
C_n_fl[1]+sum(C_n_fl[2:1:1000])+S_n_fl+G_p_fl = 0;
R_p_eff[1] = S_p_eff;
R_p_fl[1]+S_p_fl = 0;
G_n_fl = 0;
for i in 1:1:999 loop
C_p_eff[i] = R_p_eff[i+1];
C_p_eff[i] = R_n_eff[i];
end for;
for i in 1:1:999 loop
C_p_fl[i]+R_p_fl[i+1]+R_n_fl[i] = 0;
end for;
end SBGraph1;
14 changes: 7 additions & 7 deletions flatter/test/gt_data/SBGraph2.mo
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ model SBGraph2
Real C_p_eff[N];
Real C_p_fl[N];
equation
C_p_eff[1000] = R_n_eff[1000];
C_p_fl[1000]+R_n_fl[1000] = 0;
R_p_eff[1] = S_p_eff;
R_p_fl[1]+S_p_fl = 0;
C_n_eff[1] = G_p_eff;
G_n_fl = 0;
C_n_eff[1] = S_n_eff;
C_n_eff[1] = G_p_eff;
for i in 1:1:999 loop
C_n_eff[1] = C_n_eff[i+1];
end for;
C_n_fl[1]+sum(C_n_fl[2:1:1000])+G_p_fl+S_n_fl = 0;
G_n_fl = 0;
C_n_fl[1]+S_n_fl+G_p_fl+sum(C_n_fl[2:1:1000]) = 0;
R_p_eff[1] = S_p_eff;
R_p_fl[1]+S_p_fl = 0;
C_p_eff[1000] = R_n_eff[1000];
C_p_fl[1000]+R_n_fl[1000] = 0;
for i in 1:1:999 loop
C_p_eff[i] = R_p_eff[i+1];
C_p_eff[i] = R_n_eff[i];
Expand Down
28 changes: 14 additions & 14 deletions flatter/test/gt_data/SBGraph3.mo
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,30 @@ model SBGraph3
Real C_r_eff[N, M];
Real C_r_fl[N, M];
equation
C_d_eff[1000,1] = G_p_eff;
C_d_eff[1000,1] = S_n_eff;
for i in 1:1:1,j in 1:1:999 loop
C_d_eff[1000,1] = C_d_eff[1000,j+1];
for i in 1:1:999,j in 1:1:1000 loop
C_d_eff[i,j] = C_u_eff[i+1,j];
end for;
C_d_fl[1000,1]+sum(C_d_fl[1000:1:1000, 2:1:1000])+G_p_fl+S_n_fl = 0;
C_u_eff[1,1] = S_p_eff;
for i in 1:1:1,j in 1:1:999 loop
C_u_eff[1,1] = C_u_eff[1,j+1];
for i in 1:1:999,j in 1:1:1000 loop
C_d_fl[i,j]+C_u_fl[i+1,j] = 0;
end for;
C_u_fl[1,1]+S_p_fl+sum(C_u_fl[1:1:1, 2:1:1000]) = 0;
G_n_fl = 0;
for i in 1:1:1000,j in 1:1:999 loop
C_l_eff[i,j+1] = C_r_eff[i,j];
end for;
for i in 1:1:1000,j in 1:1:999 loop
C_l_fl[i,j+1]+C_r_fl[i,j] = 0;
end for;
for i in 1:1:999,j in 1:1:1000 loop
C_d_eff[i,j] = C_u_eff[i+1,j];
G_n_fl = 0;
C_u_eff[1,1] = S_p_eff;
for i in 1:1:1,j in 1:1:999 loop
C_u_eff[1,1] = C_u_eff[1,j+1];
end for;
for i in 1:1:999,j in 1:1:1000 loop
C_d_fl[i,j]+C_u_fl[i+1,j] = 0;
C_u_fl[1,1]+sum(C_u_fl[1:1:1, 2:1:1000])+S_p_fl = 0;
C_d_eff[1000,1] = G_p_eff;
C_d_eff[1000,1] = S_n_eff;
for i in 1:1:1,j in 1:1:999 loop
C_d_eff[1000,1] = C_d_eff[1000,j+1];
end for;
C_d_fl[1000,1]+sum(C_d_fl[1000:1:1000, 2:1:1000])+G_p_fl+S_n_fl = 0;
for i in 1:1:1000,j in 1:1:1 loop
C_l_eff[i,1] = C_r_eff[i,1000];
end for;
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
21 changes: 21 additions & 0 deletions test/mccprograms/matching/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
**Matching Algorithm test models**

This folder contains four test Modelica models, where the program constructs the corresponding SB-Graph, and then applies the matching algorithm for SB-Graphs implemented [here](https://github.com/CIFASIS/sb-graph/blob/739ba2969552fa3373137df29dcb045c397e3e93/sbg/sbg_algorithms.cpp#L671). It also contains the expected results for each of them.

The models are the following:

* A simple model with small algebraic loops disconnected from each other `Test1.mo` -> Expected results `results1.txt`
* A model with connections between different indices `Test2.mo` -> Expected results `results2.txt`
* Similar to Test2, but with border conditions changed `Test3.mo` -> Expected results `results3.txt`
* Two copies of Test3 `Test4.mo` -> Expected results `results4.txt`

In order to run the matching algorithm the following steps must be taken:

* Install the ModelicaCC compiler as described [here](https://github.com/CIFASIS/modelicacc#installation)
* From the ModelicaCC root folder run: `./bin/causalize ./test/mccprograms/matching/{TEST_MODEL} > {OUTPUT_MODEL}` where `{TEST_MODEL}` is one of the models defined above and `{OUTPUT_MODEL}` is the result output Modelica model.

**Note:**
The execution will generate a SBG.log file located in the same folder where the command is called, this file contains detailed information about the different steps taken by the matching algorithm
and also information about the generated graph used by the algorithm. Additionally, a dot file with the definition of the graph is created, to generate a pdf file from the dot file, run:

`dot -Grankdir=LR -Tpdf <PATH_TO_DOT_FILE>`
10 changes: 10 additions & 0 deletions test/mccprograms/matching/Test1.mo
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
model Test1
constant Integer N = 100;
Real a[N], b[N], x[N];
equation
for i in 1:N loop
a[i] = 2 * x[i] - b[i];
a[i] = 2 * b[i] - x[i];
der(x[i]) = 1 - a[i];
end for;
end Test1;
17 changes: 17 additions & 0 deletions test/mccprograms/matching/Test2.mo
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
model Test2
//Model with possibly wrong initial matching
constant Integer N = 100;
Real a[N], x[N], b[N];
equation
for i in 2:N-1 loop
der(x[i]) = a[i] - x[i];
a[i + 1] = a[i] + b[i];
b[i] = x[i - 1];
end for;
b[1] = 0;
b[N] = x[N - 1];
der(x[1]) = a[1] - x[1];
der(x[N]) = a[N] - x[N];
a[2] = a[1] + b[1];
a[N] = 1;
end Test2;
16 changes: 16 additions & 0 deletions test/mccprograms/matching/Test3.mo
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
model Test3
constant Integer N = 100;
Real a[N], x[N], b[N];
equation
for i in 2:N-1 loop
der(x[i]) = a[i] - x[i];
a[i + 1] = a[i] + b[i];
b[i] = x[i - 1];
end for;
b[1] = 0;
b[N] = x[N - 1];
der(x[1]) = a[1] - x[1];
der(x[N]) = a[N] - x[N];
a[1] = 1;
a[N] = a[N - 1] + b[N - 1];
end Test3;
110 changes: 110 additions & 0 deletions test/mccprograms/matching/Test4.mo
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
model Test4
constant Integer N = 100000;
Real a1[N], x1[N], b1[N];
Real a2[N], x2[N], b2[N];
/*
Real a3[N], x3[N], b3[N];
Real a4[N], x4[N], b4[N];
Real a5[N], x5[N], b5[N];
Real a6[N], x6[N], b6[N];
Real a7[N], x7[N], b7[N];
Real a8[N], x8[N], b8[N];
*/
equation
for i in 2:N-1 loop
der(x1[i]) = a1[i] - x1[i];
a1[i + 1] = a1[i] + b1[i];
b1[i] = x1[i - 1];
end for;
b1[1] = 0;
b1[N] = x1[N - 1];
der(x1[1]) = a1[1] - x1[1];
der(x1[N]) = a1[N] - x1[N];
a1[1] = 1;
a1[N] = a1[N - 1] + b1[N - 1];

for i in 2:N-1 loop
der(x2[i]) = a2[i] - x2[i];
a2[i + 1] = a2[i] + b2[i];
b2[i] = x2[i - 1];
end for;
b2[1] = 0;
b2[N] = x2[N - 1];
der(x2[1]) = a2[1] - x2[1];
der(x2[N]) = a2[N] - x2[N];
a2[1] = 1;
a2[N] = a2[N - 1] + b2[N - 1];
/*
for i in 2:N-1 loop
der(x3[i]) = a3[i] - x3[i];
a3[i + 1] = a3[i] + b3[i];
b3[i] = x3[i - 1];
end for;
b3[1] = 0;
b3[N] = x3[N - 1];
der(x3[1]) = a3[1] - x3[1];
der(x3[N]) = a3[N] - x3[N];
a3[1] = 1;
a3[N] = a3[N - 1] + b3[N - 1];
for i in 2:N-1 loop
der(x4[i]) = a4[i] - x4[i];
a4[i + 1] = a4[i] + b4[i];
b4[i] = x4[i - 1];
end for;
b4[1] = 0;
b4[N] = x4[N - 1];
der(x4[1]) = a4[1] - x4[1];
der(x4[N]) = a4[N] - x4[N];
a4[1] = 1;
a4[N] = a4[N - 1] + b4[N - 1];
for i in 2:N-1 loop
der(x5[i]) = a5[i] - x5[i];
a5[i + 1] = a5[i] + b5[i];
b5[i] = x5[i - 1];
end for;
b5[1] = 0;
b5[N] = x5[N - 1];
der(x5[1]) = a5[1] - x5[1];
der(x5[N]) = a5[N] - x5[N];
a5[1] = 1;
a5[N] = a5[N - 1] + b5[N - 1];
for i in 2:N-1 loop
der(x6[i]) = a6[i] - x6[i];
a6[i + 1] = a6[i] + b6[i];
b6[i] = x6[i - 1];
end for;
b6[1] = 0;
b6[N] = x6[N - 1];
der(x6[1]) = a6[1] - x6[1];
der(x6[N]) = a6[N] - x6[N];
a6[1] = 1;
a6[N] = a6[N - 1] + b6[N - 1];
for i in 2:N-1 loop
der(x7[i]) = a7[i] - x7[i];
a7[i + 1] = a7[i] + b7[i];
b7[i] = x7[i - 1];
end for;
b7[1] = 0;
b7[N] = x7[N - 1];
der(x7[1]) = a7[1] - x7[1];
der(x7[N]) = a7[N] - x7[N];
a7[1] = 1;
a7[N] = a7[N - 1] + b7[N - 1];
for i in 2:N-1 loop
der(x8[i]) = a8[i] - x8[i];
a8[i + 1] = a8[i] + b8[i];
b8[i] = x8[i - 1];
end for;
b8[1] = 0;
b8[N] = x8[N - 1];
der(x8[1]) = a8[1] - x8[1];
der(x8[N]) = a8[N] - x8[N];
a8[1] = 1;
a8[N] = a8[N - 1] + b8[N - 1];
*/
end Test4;
4 changes: 4 additions & 0 deletions test/mccprograms/matching/results1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Generated matching:
{[1:1:100], [301:1:400], [501:1:600]}

>>> Matched all unknowns
4 changes: 4 additions & 0 deletions test/mccprograms/matching/results2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Generated matching:
{[295:1:391], [99:1:195], [491:1:587], [590:1:590], [595:1:595], [588:1:588], [598:1:598], [392:1:392], [196:1:196], [592:1:592], [589:1:589], [594:1:594]}

>>> Matched all unknowns
4 changes: 4 additions & 0 deletions test/mccprograms/matching/results3.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Generated matching:
{[100:1:195], [297:1:387], [491:1:586], [594:1:594], [589:1:589], [598:1:598], [592:1:592], [196:1:196], [389:1:389], [390:1:390], [296:1:296], [295:1:295], [99:1:99], [388:1:388], [391:1:391], [587:1:587], [588:1:588], [595:1:595], [294:1:294], [590:1:590]}

>>> Matched all unknowns
4 changes: 4 additions & 0 deletions test/mccprograms/matching/results4.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Generated matching:
{[1199992:1:1199992], [1199987:1:1199987], [599994:1:599994], [599989:1:599989], [1199993:1:1199993], [1199986:1:1199986], [599992:1:599992], [1199996:1:1199996], [899993:1:899993], [899995:1:899995], [599987:1:599987], [499991:1:499991], [199996:1:199996], [99999:1:99999], [599998:1:599998], [100000:1:100000], [299997:1:299997], [299996:1:299996], [299995:1:299995], [699997:1:699997], [899994:1:899994], [599988:1:599988], [599995:1:599995], [1199990:1:1199990], [299994:1:299994], [599990:1:599990], [899992:1:899992], [1199988:1:1199988], [499992:1:599986], [100001:1:199995], [899996:1:999989], [299998:1:399991], [1099989:1:1199985], [699998:1:799994]}

>>> Matched all unknowns
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit cf960b2

Please sign in to comment.