diff --git a/3rd-party/sbg/sb-graph-dev.tar.gz b/3rd-party/sbg/sb-graph-dev.tar.gz index 6ca47a4..61c7b48 100644 Binary files a/3rd-party/sbg/sb-graph-dev.tar.gz and b/3rd-party/sbg/sb-graph-dev.tar.gz differ diff --git a/causalize/sbg_implementation/test/models_test.cpp b/causalize/sbg_implementation/test/models_test.cpp index b682824..6baffd3 100644 --- a/causalize/sbg_implementation/test/models_test.cpp +++ b/causalize/sbg_implementation/test/models_test.cpp @@ -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; diff --git a/flatter/test/gt_data/SBGraph1.mo b/flatter/test/gt_data/SBGraph1.mo index ef10314..99e6645 100644 --- a/flatter/test/gt_data/SBGraph1.mo +++ b/flatter/test/gt_data/SBGraph1.mo @@ -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; diff --git a/flatter/test/gt_data/SBGraph2.mo b/flatter/test/gt_data/SBGraph2.mo index a5a4a49..b123acc 100644 --- a/flatter/test/gt_data/SBGraph2.mo +++ b/flatter/test/gt_data/SBGraph2.mo @@ -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]; diff --git a/flatter/test/gt_data/SBGraph3.mo b/flatter/test/gt_data/SBGraph3.mo index ebe6743..1b2641b 100644 --- a/flatter/test/gt_data/SBGraph3.mo +++ b/flatter/test/gt_data/SBGraph3.mo @@ -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; diff --git a/test/mccprograms/causalize/advection.mo b/test/mccprograms/advection.mo similarity index 100% rename from test/mccprograms/causalize/advection.mo rename to test/mccprograms/advection.mo diff --git a/test/mccprograms/causalize/advection2D.mo b/test/mccprograms/advection2D.mo similarity index 100% rename from test/mccprograms/causalize/advection2D.mo rename to test/mccprograms/advection2D.mo diff --git a/test/mccprograms/causalize/airconds.mo b/test/mccprograms/airconds.mo similarity index 100% rename from test/mccprograms/causalize/airconds.mo rename to test/mccprograms/airconds.mo diff --git a/test/mccprograms/causalize/aircont.mo b/test/mccprograms/aircont.mo similarity index 100% rename from test/mccprograms/causalize/aircont.mo rename to test/mccprograms/aircont.mo diff --git a/test/mccprograms/causalize/bad_advection.mo b/test/mccprograms/bad_advection.mo similarity index 100% rename from test/mccprograms/causalize/bad_advection.mo rename to test/mccprograms/bad_advection.mo diff --git a/test/mccprograms/causalize/bball_downstairs.mo b/test/mccprograms/bball_downstairs.mo similarity index 100% rename from test/mccprograms/causalize/bball_downstairs.mo rename to test/mccprograms/bball_downstairs.mo diff --git a/test/mccprograms/causalize/buck.mo b/test/mccprograms/buck.mo similarity index 100% rename from test/mccprograms/causalize/buck.mo rename to test/mccprograms/buck.mo diff --git a/test/mccprograms/matching/README.md b/test/mccprograms/matching/README.md new file mode 100644 index 0000000..9f20345 --- /dev/null +++ b/test/mccprograms/matching/README.md @@ -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 ` diff --git a/test/mccprograms/matching/Test1.mo b/test/mccprograms/matching/Test1.mo new file mode 100644 index 0000000..d627d40 --- /dev/null +++ b/test/mccprograms/matching/Test1.mo @@ -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; diff --git a/test/mccprograms/matching/Test2.mo b/test/mccprograms/matching/Test2.mo new file mode 100644 index 0000000..b070f0d --- /dev/null +++ b/test/mccprograms/matching/Test2.mo @@ -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; diff --git a/test/mccprograms/matching/Test3.mo b/test/mccprograms/matching/Test3.mo new file mode 100644 index 0000000..6eef262 --- /dev/null +++ b/test/mccprograms/matching/Test3.mo @@ -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; diff --git a/test/mccprograms/matching/Test4.mo b/test/mccprograms/matching/Test4.mo new file mode 100644 index 0000000..eb1a399 --- /dev/null +++ b/test/mccprograms/matching/Test4.mo @@ -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; diff --git a/test/mccprograms/matching/results1.txt b/test/mccprograms/matching/results1.txt new file mode 100644 index 0000000..a40c0da --- /dev/null +++ b/test/mccprograms/matching/results1.txt @@ -0,0 +1,4 @@ +Generated matching: +{[1:1:100], [301:1:400], [501:1:600]} + +>>> Matched all unknowns diff --git a/test/mccprograms/matching/results2.txt b/test/mccprograms/matching/results2.txt new file mode 100644 index 0000000..3bacf8a --- /dev/null +++ b/test/mccprograms/matching/results2.txt @@ -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 diff --git a/test/mccprograms/matching/results3.txt b/test/mccprograms/matching/results3.txt new file mode 100644 index 0000000..9fdf05e --- /dev/null +++ b/test/mccprograms/matching/results3.txt @@ -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 diff --git a/test/mccprograms/matching/results4.txt b/test/mccprograms/matching/results4.txt new file mode 100644 index 0000000..39f68ff --- /dev/null +++ b/test/mccprograms/matching/results4.txt @@ -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 diff --git a/test/mccprograms/causalize/matching1.mo b/test/mccprograms/matching1.mo similarity index 100% rename from test/mccprograms/causalize/matching1.mo rename to test/mccprograms/matching1.mo diff --git a/test/mccprograms/causalize/matching2.mo b/test/mccprograms/matching2.mo similarity index 100% rename from test/mccprograms/causalize/matching2.mo rename to test/mccprograms/matching2.mo diff --git a/test/mccprograms/causalize/matching3.mo b/test/mccprograms/matching3.mo similarity index 100% rename from test/mccprograms/causalize/matching3.mo rename to test/mccprograms/matching3.mo diff --git a/test/mccprograms/causalize/matching4.mo b/test/mccprograms/matching4.mo similarity index 100% rename from test/mccprograms/causalize/matching4.mo rename to test/mccprograms/matching4.mo diff --git a/test/mccprograms/causalize/matching5.mo b/test/mccprograms/matching5.mo similarity index 100% rename from test/mccprograms/causalize/matching5.mo rename to test/mccprograms/matching5.mo diff --git a/test/mccprograms/causalize/matching6.mo b/test/mccprograms/matching6.mo similarity index 100% rename from test/mccprograms/causalize/matching6.mo rename to test/mccprograms/matching6.mo diff --git a/test/mccprograms/causalize/matching7.mo b/test/mccprograms/matching7.mo similarity index 100% rename from test/mccprograms/causalize/matching7.mo rename to test/mccprograms/matching7.mo diff --git a/test/mccprograms/causalize/rlc_loop.mo b/test/mccprograms/rlc_loop.mo similarity index 100% rename from test/mccprograms/causalize/rlc_loop.mo rename to test/mccprograms/rlc_loop.mo