Skip to content
This repository was archived by the owner on Apr 3, 2019. It is now read-only.

Commit 5f1fe9f

Browse files
ptaeuberOpenModelica-Hudson
authored andcommitted
Add adaptive homotopy tests
Belonging to [master]: - OpenModelica/OMCompiler#2090 - #809
1 parent c513fa3 commit 5f1fe9f

7 files changed

Lines changed: 332 additions & 6 deletions

File tree

simulation/modelica/initialization/Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ fullRobot.mos \
3333
gaspropreties.mos \
3434
homotopy1.mos \
3535
homotopy2.mos \
36+
homotopy3.mos \
37+
homotopy4.mos \
38+
homotopy5.mos \
3639
initial_equation.mos \
3740
parameters.mos \
3841
parameterWithoutBinding.mos \

simulation/modelica/initialization/homotopy1.mos

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@ package initializationTests
1515
Real y; // -3
1616

1717
equation
18-
homotopy(x^2, x) = 9;
19-
homotopy(y^2, -y) = 9;
18+
homotopy(x^2 - 9, x-9) = 0;
19+
homotopy(y^2 - 9, -y-9) = 0; // works (continuously differentiable homotopy path)
20+
// homotopy(-y^2 + 9, y+9) = 0; // works (continuously differentiable homotopy path)
21+
// homotopy(y^2 - 9, y+9) = 0; // does not work (homotopy path is not continuously differentiable)
2022
end homotopy1;
2123
end initializationTests;
2224
"); getErrorString();
@@ -37,7 +39,7 @@ res := OpenModelica.Scripting.readSimulationResult("initializationTests.homotopy
3739
// record SimulationResult
3840
// resultFile = "initializationTests.homotopy1_res.mat",
3941
// simulationOptions = "startTime = 0.0, stopTime = 0.0, numberOfIntervals = 500, tolerance = 1e-06, method = 'dassl', fileNamePrefix = 'initializationTests.homotopy1', options = '', outputFormat = 'mat', variableFilter = '.*', cflags = '', simflags = '-homotopyOnFirstTry'",
40-
// messages = "LOG_SUCCESS | info | The initialization finished successfully with homotopy method.
42+
// messages = "LOG_SUCCESS | info | The initialization finished successfully with 8 homotopy steps.
4143
// LOG_SUCCESS | info | The simulation finished successfully.
4244
// "
4345
// end SimulationResult;
@@ -50,7 +52,7 @@ res := OpenModelica.Scripting.readSimulationResult("initializationTests.homotopy
5052
// record SimulationResult
5153
// resultFile = "initializationTests.homotopy1_res.mat",
5254
// simulationOptions = "startTime = 0.0, stopTime = 0.0, numberOfIntervals = 500, tolerance = 1e-06, method = 'dassl', fileNamePrefix = 'initializationTests.homotopy1', options = '', outputFormat = 'mat', variableFilter = '.*', cflags = '', simflags = '-homotopyOnFirstTry'",
53-
// messages = "LOG_SUCCESS | info | The initialization finished successfully with homotopy method.
55+
// messages = "LOG_SUCCESS | info | The initialization finished successfully with 4 homotopy steps.
5456
// LOG_SUCCESS | info | The simulation finished successfully.
5557
// "
5658
// end SimulationResult;

simulation/modelica/initialization/homotopy2.mos

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ res := OpenModelica.Scripting.readSimulationResult("initializationTests.homotopy
198198
// record SimulationResult
199199
// resultFile = "initializationTests.homotopy2_res.mat",
200200
// simulationOptions = "startTime = 0.0, stopTime = 0.0, numberOfIntervals = 500, tolerance = 1e-06, method = 'dassl', fileNamePrefix = 'initializationTests.homotopy2', options = '', outputFormat = 'mat', variableFilter = '.*', cflags = '', simflags = '-homotopyOnFirstTry'",
201-
// messages = "LOG_SUCCESS | info | The initialization finished successfully with homotopy method.
201+
// messages = "LOG_SUCCESS | info | The initialization finished successfully with 4 homotopy steps.
202202
// LOG_SUCCESS | info | The simulation finished successfully.
203203
// "
204204
// end SimulationResult;
@@ -398,7 +398,7 @@ res := OpenModelica.Scripting.readSimulationResult("initializationTests.homotopy
398398
// record SimulationResult
399399
// resultFile = "initializationTests.homotopy2_res.mat",
400400
// simulationOptions = "startTime = 0.0, stopTime = 0.0, numberOfIntervals = 500, tolerance = 1e-06, method = 'dassl', fileNamePrefix = 'initializationTests.homotopy2', options = '', outputFormat = 'mat', variableFilter = '.*', cflags = '', simflags = '-homotopyOnFirstTry'",
401-
// messages = "LOG_SUCCESS | info | The initialization finished successfully with homotopy method.
401+
// messages = "LOG_SUCCESS | info | The initialization finished successfully with 4 homotopy steps.
402402
// LOG_SUCCESS | info | The simulation finished successfully.
403403
// "
404404
// end SimulationResult;
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// name: homotopy3
2+
// keywords: initialization, homotopy
3+
// status: correct
4+
// cflags:
5+
// teardown_command: rm -rf initializationTests.homotopy3* _initializationTests.homotopy3* output.log
6+
//
7+
// case for homotopy
8+
//
9+
10+
loadString("
11+
within ;
12+
package initializationTests
13+
record rec
14+
Real n1;
15+
Real n2[2];
16+
Real n3;
17+
end rec;
18+
19+
function f1
20+
input rec r;
21+
output Real res;
22+
algorithm
23+
res := r.n1 * r.n2[1] / r.n3;
24+
end f1;
25+
26+
model homotopy3
27+
Real x;
28+
Real y;
29+
parameter Real p1 = 1;
30+
parameter Real p2 = 2;
31+
parameter Real p3 = 3;
32+
equation
33+
x = homotopy(f1(rec(p1,{p2,p2},p3))+y, f1(rec(y,{p2,p2},p3)))*y;
34+
x + y = time;
35+
end homotopy3;
36+
end initializationTests;
37+
"); getErrorString();
38+
39+
setCommandLineOptions("--homotopyApproach=adaptiveLocal"); getErrorString();
40+
simulate(initializationTests.homotopy3, startTime=0.0, stopTime=0.0); getErrorString();
41+
res := OpenModelica.Scripting.readSimulationResult("initializationTests.homotopy3_res.mat", {time, x}, 2); getErrorString();
42+
43+
// Result:
44+
// true
45+
// ""
46+
// true
47+
// ""
48+
// record SimulationResult
49+
// resultFile = "initializationTests.homotopy3_res.mat",
50+
// simulationOptions = "startTime = 0.0, stopTime = 0.0, numberOfIntervals = 500, tolerance = 1e-06, method = 'dassl', fileNamePrefix = 'initializationTests.homotopy3', options = '', outputFormat = 'mat', variableFilter = '.*', cflags = '', simflags = ''",
51+
// messages = "LOG_SUCCESS | info | The initialization finished successfully with 4 homotopy steps.
52+
// LOG_SUCCESS | info | The simulation finished successfully.
53+
// "
54+
// end SimulationResult;
55+
// "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").
56+
// "
57+
// {{0.0,0.0},{0.0,0.0}}
58+
// ""
59+
// endResult
Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
// name: homotopy4
2+
// keywords: initialization, homotopy
3+
// status: correct
4+
// cflags:
5+
// teardown_command: rm -rf initializationTests.homotopy4* _initializationTests.homotopy4* output.log
6+
//
7+
// case for homotopy
8+
//
9+
10+
loadString("
11+
within ;
12+
package initializationTests
13+
model homotopy4
14+
Real x,y,z,x1,y1,z1,x2,y2,z2,x3,y3,z3,a,b,c,d,e,f;
15+
// Real x,y,z,x1,y1,z1,x2,y2,z2 annotation(tearingSelect=always),x3,y3,z3,a,b,c,d,e,f;
16+
parameter Real p1 = 10;
17+
18+
equation
19+
// a = sin(p1* time + 1);
20+
a = sin(p1* time + 3.1415/2);
21+
22+
x + 5 = z + a;
23+
-y + 10 = z + p1;
24+
x - y + 9 = z;
25+
26+
b = sin(a*x);
27+
28+
// ********** Beginning of homotopy iteration loop
29+
c = homotopy(b^2, b);
30+
31+
x1 + 5 = z1 + c;
32+
-y1 + 10 = z1 + p1;
33+
x1 - y1 + 9 = z1;
34+
35+
d = x1 + y1 + z1;
36+
37+
homotopy(x2^2, x2) + 5 = z2 + 5;
38+
homotopy(y2^2, -d) + 10 = z2 + p1;
39+
x2 + y2 + 9 = z2;
40+
41+
e = sin(d*x2);
42+
43+
x3 + 5 = z3 + e;
44+
-y3 + 10 = z3*y3 + p1;
45+
x3 - y3 + 9 = z3;
46+
// ********** End of homotopy iteration loop
47+
48+
f = x + y + z + a + b + c + d + e + x1 + y1 + z1 + x2 + y2 + z2 + x3 + y3 + z3;
49+
end homotopy4;
50+
end initializationTests;
51+
"); getErrorString();
52+
53+
sflags:="-homotopyOnFirstTry -homTauStart=0.4";
54+
55+
// setCommandLineOptions("--homotopyApproach=equidistantGlobal"); getErrorString();
56+
// setCommandLineOptions("--homotopyApproach=equidistantGlobal --noTearingForComponent=4"); getErrorString();
57+
// setCommandLineOptions("--homotopyApproach=adaptiveGlobal"); getErrorString();
58+
setCommandLineOptions("--homotopyApproach=adaptiveGlobal --noTearingForComponent=4"); getErrorString();
59+
// setCommandLineOptions("--homotopyApproach=adaptiveGlobal --noTearingForComponent=4 -d=-NLSanalyticJacobian"); getErrorString();
60+
// setCommandLineOptions("--homotopyApproach=eqidistantLocal"); getErrorString();
61+
// setCommandLineOptions("--homotopyApproach=eqidistantLocal --noTearingForComponent=4"); getErrorString();
62+
// setCommandLineOptions("--homotopyApproach=adaptiveLocal"); getErrorString();
63+
// setCommandLineOptions("--homotopyApproach=adaptiveLocal --noTearingForComponent=4"); getErrorString();
64+
simulate(initializationTests.homotopy4, startTime=0.0, stopTime=0.0, simflags=sflags); getErrorString();
65+
res := OpenModelica.Scripting.readSimulationResult("initializationTests.homotopy4_res.mat", {time, x,y,z,a,b,c,d,e,x1,y1,z1,x2,y2,z2,x3,y3,z3,f}, 2); getErrorString();
66+
67+
print("x=");
68+
val(x, 0.0);
69+
70+
print("\ny=");
71+
val(y, 0.0);
72+
73+
print("\nz=");
74+
val(z, 0.0);
75+
76+
print("\nx1=");
77+
val(x1, 0.0);
78+
79+
print("\ny1=");
80+
val(y1, 0.0);
81+
82+
print("\nz1=");
83+
val(z1, 0.0);
84+
85+
print("\nx2=");
86+
val(x2, 0.0);
87+
88+
print("\ny2=");
89+
val(y2, 0.0);
90+
91+
print("\nz2=");
92+
val(z2, 0.0);
93+
94+
print("\nx3=");
95+
val(x3, 0.0);
96+
97+
print("\ny3=");
98+
val(y3, 0.0);
99+
100+
print("\nz3=");
101+
val(z3, 0.0);
102+
103+
print("\na=");
104+
val(a, 0.0);
105+
106+
print("\nb=");
107+
val(b, 0.0);
108+
109+
print("\nc=");
110+
val(c, 0.0);
111+
112+
print("\nd=");
113+
val(d, 0.0);
114+
115+
print("\ne=");
116+
val(e, 0.0);
117+
118+
print("\nf=");
119+
val(f, 0.0);
120+
121+
// Result:
122+
// true
123+
// ""
124+
// "-homotopyOnFirstTry -homTauStart=0.4"
125+
// true
126+
// ""
127+
// record SimulationResult
128+
// resultFile = "initializationTests.homotopy4_res.mat",
129+
// simulationOptions = "startTime = 0.0, stopTime = 0.0, numberOfIntervals = 500, tolerance = 1e-06, method = 'dassl', fileNamePrefix = 'initializationTests.homotopy4', options = '', outputFormat = 'mat', variableFilter = '.*', cflags = '', simflags = '-homotopyOnFirstTry -homTauStart=0.4'",
130+
// messages = "stdout | info | homotopy parameter homTauStart changed to 0.400000
131+
// LOG_SUCCESS | info | The initialization finished successfully with 480 homotopy steps.
132+
// LOG_SUCCESS | info | The simulation finished successfully.
133+
// "
134+
// end SimulationResult;
135+
// "Notification: Tearing is skipped for strong component 4 because of activated compiler flag 'noTearingForComponent=4'.
136+
// 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").
137+
// "
138+
// {{0.0,0.0},{-9.0,-9.0},{4.999999998926914,4.999999998926914},{-4.999999998926914,-4.999999998926914},{0.999999998926914,0.999999998926914},{-0.412118494041246,-0.412118494041246},{0.1698416531308245,0.1698416531308245},{-9.0,-9.0},{-0.956375928404503,-0.956375928404503},{-9.0,-9.0},{4.169841653130826,4.169841653130826},{-4.169841653130826,-4.169841653130826},{3.0,3.0},{-3.0,-3.0},{9.0,9.0},{-6.956375928404503,-6.956375928404503},{3.043624071595497,3.043624071595497},{-1.0,-1.0},{-23.11140462719703,-23.11140462719703}}
139+
// ""
140+
// x=
141+
// -9.0
142+
//
143+
// y=
144+
// 4.999999998926914
145+
//
146+
// z=
147+
// -4.999999998926914
148+
//
149+
// x1=
150+
// -9.0
151+
//
152+
// y1=
153+
// 4.169841653130826
154+
//
155+
// z1=
156+
// -4.169841653130826
157+
//
158+
// x2=
159+
// 3.0
160+
//
161+
// y2=
162+
// -3.0
163+
//
164+
// z2=
165+
// 9.0
166+
//
167+
// x3=
168+
// -6.956375928404503
169+
//
170+
// y3=
171+
// 3.043624071595497
172+
//
173+
// z3=
174+
// -1.0
175+
//
176+
// a=
177+
// 0.999999998926914
178+
//
179+
// b=
180+
// -0.412118494041246
181+
//
182+
// c=
183+
// 0.1698416531308245
184+
//
185+
// d=
186+
// -9.0
187+
//
188+
// e=
189+
// -0.956375928404503
190+
//
191+
// f=
192+
// -23.11140462719703
193+
// endResult
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// name: homotopy5
2+
// keywords: initialization, homotopy
3+
// status: correct
4+
// cflags:
5+
// teardown_command: rm -rf initializationTests.homotopy5* _initializationTests.homotopy5* output.log
6+
//
7+
// case for homotopy
8+
//
9+
10+
loadString("
11+
within ;
12+
package initializationTests
13+
model homotopy5
14+
Real x (start=-0.5);
15+
Real y (start=-0.5);
16+
parameter Real pi = 3.1415;
17+
equation
18+
0 = homotopy(2*x - 4 + sin(2*pi*x), x+0.5);
19+
0 = homotopy(2*y - 4 + sin(2*pi*y), 2*y - 4 + sin(2*pi*y) - (2*(-0.5) - 4 + sin(2*pi*(-0.5))));
20+
end homotopy5;
21+
end initializationTests;
22+
"); getErrorString();
23+
24+
sflags:="-homotopyOnFirstTry";
25+
26+
setCommandLineOptions("--homotopyApproach=adaptiveLocal"); getErrorString();
27+
simulate(initializationTests.homotopy5, startTime=0.0, stopTime=0.0, simflags=sflags); getErrorString();
28+
res := OpenModelica.Scripting.readSimulationResult("initializationTests.homotopy5_res.mat", {time, x}, 2); getErrorString();
29+
30+
print("x=");
31+
val(x, 0.0);
32+
33+
print("y=");
34+
val(x, 0.0);
35+
36+
// Result:
37+
// true
38+
// ""
39+
// "-homotopyOnFirstTry"
40+
// true
41+
// ""
42+
// record SimulationResult
43+
// resultFile = "initializationTests.homotopy5_res.mat",
44+
// simulationOptions = "startTime = 0.0, stopTime = 0.0, numberOfIntervals = 500, tolerance = 1e-06, method = 'dassl', fileNamePrefix = 'initializationTests.homotopy5', options = '', outputFormat = 'mat', variableFilter = '.*', cflags = '', simflags = '-homotopyOnFirstTry'",
45+
// messages = "LOG_SUCCESS | info | The initialization finished successfully with 28 homotopy steps.
46+
// LOG_SUCCESS | info | The simulation finished successfully.
47+
// "
48+
// end SimulationResult;
49+
// "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").
50+
// "
51+
// {{0.0,0.0},{2.000044743976706,2.000044743976706}}
52+
// ""
53+
// x=
54+
// 2.000044743976706
55+
// y=
56+
// 2.000044743976706
57+
// endResult

simulation/modelica/nonlinear_system/bug_2841.mos

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@ simulate(nonlinearSolverTests.bug_2841, stopTime=0.0, simflags="-lv=LOG_INIT,LOG
5656
// | | | | | 100000 1e-05 1
5757
// LOG_NLS_V | info | x0 [2-dim]
5858
// | | | | | 50000 5e-06
59+
// LOG_NLS_V | info | indRow: [2-dim]
60+
// | | | | | 0 1
61+
// LOG_NLS_V | info | indCol: [3-dim]
62+
// | | | | | 1 0 2
63+
// LOG_NLS_V | info | vector x (solution): [3-dim]
64+
// | | | | | -2.5510381 -0.51701271 1
5965
// LOG_NLS_V | info | regular initial point!!!
6066
// LOG_NLS_V | info | ******************************************************
6167
// LOG_NLS_V | info | NEWTON SOLVER STARTED! equation number: 7
@@ -274,6 +280,12 @@ simulate(nonlinearSolverTests.bug_2841, stopTime=0.0, simflags="-lv=LOG_INIT,LOG
274280
// | | | | | 100000 1e-05 1
275281
// LOG_NLS_V | info | x0 [2-dim]
276282
// | | | | | -77390.689 3.3118158e-06
283+
// LOG_NLS_V | info | indRow: [2-dim]
284+
// | | | | | 0 1
285+
// LOG_NLS_V | info | indCol: [3-dim]
286+
// | | | | | 1 0 2
287+
// LOG_NLS_V | info | vector x (solution): [3-dim]
288+
// | | | | | 2.9880129e-17 6.2675771e-17 1
277289
// LOG_NLS_V | info | regular initial point!!!
278290
// LOG_NLS_V | info | ******************************************************
279291
// LOG_NLS_V | info | NEWTON SOLVER STARTED! equation number: 18

0 commit comments

Comments
 (0)