1 | #!/usr/bin/env python |
---|
2 | # -*- coding: utf-8 -*- |
---|
3 | |
---|
4 | # Copyright (C) 2010 Modelon AB |
---|
5 | # |
---|
6 | # This program is free software: you can redistribute it and/or modify |
---|
7 | # it under the terms of the GNU General Public License as published by |
---|
8 | # the Free Software Foundation, version 3 of the License. |
---|
9 | # |
---|
10 | # This program is distributed in the hope that it will be useful, |
---|
11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
13 | # GNU General Public License for more details. |
---|
14 | # |
---|
15 | # You should have received a copy of the GNU General Public License |
---|
16 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
17 | """Tests for the pyfmi.simulation.assimulo module.""" |
---|
18 | import logging |
---|
19 | import nose |
---|
20 | import os |
---|
21 | import numpy as N |
---|
22 | import pylab as P |
---|
23 | from scipy.io.matlab.mio import loadmat |
---|
24 | |
---|
25 | from pymodelica.compiler import compile_fmu |
---|
26 | from pyfmi.fmi import FMUModel, load_fmu, FMUException, TimeLimitExceeded |
---|
27 | from pyfmi.common.io import ResultDymolaTextual |
---|
28 | from tests_jmodelica import testattr, get_files_path |
---|
29 | from pyfmi.common import python3_flag |
---|
30 | |
---|
31 | try: |
---|
32 | from pyfmi.simulation.assimulo_interface import FMIODE |
---|
33 | from pyfmi.simulation.assimulo_interface import write_data |
---|
34 | from pyfmi.common.core import TrajectoryLinearInterpolation |
---|
35 | from assimulo.solvers import CVode |
---|
36 | from assimulo.solvers import IDA |
---|
37 | except (NameError, ImportError): |
---|
38 | logging.warning('Could not load Assimulo module. Check pyfmi.check_packages()') |
---|
39 | |
---|
40 | path_to_fmus = os.path.join(get_files_path(), 'FMUs') |
---|
41 | path_to_fmus_me1 = os.path.join(path_to_fmus,"ME1.0") |
---|
42 | path_to_fmus_cs1 = os.path.join(path_to_fmus,"CS1.0") |
---|
43 | path_to_mos = os.path.join(get_files_path(), 'Modelica') |
---|
44 | |
---|
45 | |
---|
46 | def input_linear(t): |
---|
47 | if t < 0.5: |
---|
48 | return t |
---|
49 | elif t < 1.0: |
---|
50 | return 0.5 |
---|
51 | elif t < 1.5: |
---|
52 | return t-0.5 |
---|
53 | elif t < 2.0: |
---|
54 | return 2.5-t |
---|
55 | elif t < 2.5: |
---|
56 | return 0.5 |
---|
57 | else: |
---|
58 | return 3.0-t |
---|
59 | |
---|
60 | input_object = (["u"],input_linear) |
---|
61 | |
---|
62 | class AssertWithMessage: |
---|
63 | def __init__(self): |
---|
64 | self.message = "" |
---|
65 | def assert_equal_msg(self, actual, desired): |
---|
66 | err_msg = "\nACTUAL: {}\nDESIRED: {}".format(actual, desired) |
---|
67 | assert actual == desired, err_msg |
---|
68 | |
---|
69 | class Test_When(AssertWithMessage): |
---|
70 | @classmethod |
---|
71 | def setUpClass(cls): |
---|
72 | file_name = os.path.join(get_files_path(), 'Modelica', 'WhenTests.mo') |
---|
73 | |
---|
74 | compile_fmu("WhenTests.WhenTest5", file_name) |
---|
75 | |
---|
76 | @testattr(stddist_full = True) |
---|
77 | def test_sequence_of_pre(self): |
---|
78 | model = load_fmu("WhenTests_WhenTest5.fmu") |
---|
79 | |
---|
80 | res = model.simulate(final_time=3.5) |
---|
81 | |
---|
82 | self.assert_equal_msg(res.final("nextTime"), 4.0) |
---|
83 | self.assert_equal_msg(res.final("nextTime2"), 3.0) |
---|
84 | self.assert_equal_msg(res.final("nextTime3"), 8.0) |
---|
85 | |
---|
86 | class Test_Sparse_Linear_Block(AssertWithMessage): |
---|
87 | @classmethod |
---|
88 | def setUpClass(cls): |
---|
89 | |
---|
90 | _cc_name = compile_fmu("Modelica.Mechanics.Rotational.Examples.CoupledClutches", |
---|
91 | version=1.0) |
---|
92 | _cc_name_sparse = compile_fmu("Modelica.Mechanics.Rotational.Examples.CoupledClutches", |
---|
93 | version=1.0, |
---|
94 | compiler_options={"generate_sparse_block_jacobian_threshold": 0}, |
---|
95 | compile_to=_cc_name[:-4]+"_sparse"+_cc_name[-4:]) |
---|
96 | |
---|
97 | @testattr(stddist_base = True) |
---|
98 | def test_cc(self): |
---|
99 | model = load_fmu("Modelica_Mechanics_Rotational_Examples_CoupledClutches.fmu") |
---|
100 | |
---|
101 | res1 = model.simulate() |
---|
102 | |
---|
103 | model = load_fmu("Modelica_Mechanics_Rotational_Examples_CoupledClutches_sparse.fmu") |
---|
104 | |
---|
105 | res2 = model.simulate() |
---|
106 | |
---|
107 | #Assert that sparse handling has no impact on the number of steps |
---|
108 | self.assert_equal_msg(res2.solver.statistics["nsteps"], res1.solver.statistics["nsteps"]) |
---|
109 | N.testing.assert_almost_equal(res1.final("J1.w"), res2.final("J1.w"), 3) |
---|
110 | |
---|
111 | @testattr(stddist_base = True) |
---|
112 | def test_multiple_sparse_systems(self): |
---|
113 | file_name = os.path.join(get_files_path(), 'Modelica', 'Linear.mo') |
---|
114 | |
---|
115 | fmu_name = compile_fmu("LinearTest.TwoTornSystems1", file_name, version=1.0, |
---|
116 | compiler_options={"generate_sparse_block_jacobian_threshold": 0}) |
---|
117 | |
---|
118 | class Test_Time_Events_FMU10(AssertWithMessage): |
---|
119 | @classmethod |
---|
120 | def setUpClass(cls): |
---|
121 | """ |
---|
122 | Compile the test model. |
---|
123 | """ |
---|
124 | file_name = os.path.join(get_files_path(), 'Modelica', 'TimeEvents.mo') |
---|
125 | |
---|
126 | compile_fmu("TimeEvents.Basic1", file_name, compiler_options={"relational_time_events":True}, compiler_log_level="debug:log.txt", version="1.0") |
---|
127 | compile_fmu("TimeEvents.Basic2", file_name, compiler_options={"relational_time_events":True}, version="1.0") |
---|
128 | compile_fmu("TimeEvents.Basic3", file_name, compiler_options={"relational_time_events":True}, version="1.0") |
---|
129 | compile_fmu("TimeEvents.Basic4", file_name, compiler_options={"relational_time_events":True}, version="1.0") |
---|
130 | |
---|
131 | compile_fmu("TimeEvents.Advanced1", file_name, compiler_options={"relational_time_events":True}, version="1.0") |
---|
132 | compile_fmu("TimeEvents.Advanced2", file_name, compiler_options={"relational_time_events":True}, version="1.0") |
---|
133 | compile_fmu("TimeEvents.Advanced3", file_name, compiler_options={"relational_time_events":True}, version="1.0") |
---|
134 | compile_fmu("TimeEvents.Advanced4", file_name, compiler_options={"relational_time_events":True}, version="1.0") |
---|
135 | |
---|
136 | compile_fmu("TimeEvents.Mixed1", file_name, compiler_options={"relational_time_events":True}, version="1.0") |
---|
137 | compile_fmu("TimeEvents.TestSampling1", file_name, version="1.0") |
---|
138 | compile_fmu("TimeEvents.TestSampling2", file_name, version="1.0") |
---|
139 | compile_fmu("TimeEvents.TestSampling3", file_name, version="1.0") |
---|
140 | compile_fmu("TimeEvents.TestSampling4", file_name, version="1.0") |
---|
141 | compile_fmu("TimeEvents.TestSampling5", file_name, version="1.0") |
---|
142 | compile_fmu("TimeEvents.TestSampling6", file_name, version="1.0") |
---|
143 | compile_fmu("TimeEvents.TestSampling7", file_name, version="1.0") |
---|
144 | compile_fmu("TimeEvents.TestSampling8", file_name, version="1.0") |
---|
145 | compile_fmu("TimeEvents.TestSampling9", file_name, version="1.0") |
---|
146 | compile_fmu("TimeEvents.StateEventAfterTimeEvent", file_name, version="1.0") |
---|
147 | |
---|
148 | @testattr(stddist_full = True) |
---|
149 | def test_time_event_basic_1(self): |
---|
150 | model = load_fmu("TimeEvents_Basic1.fmu") |
---|
151 | model.initialize() |
---|
152 | ev = model.get_event_info() |
---|
153 | print(ev.nextEventTime) |
---|
154 | self.assert_equal_msg(ev.nextEventTime, 1) |
---|
155 | |
---|
156 | @testattr(stddist_full = True) |
---|
157 | def test_time_event_basic_2(self): |
---|
158 | model = load_fmu("TimeEvents_Basic2.fmu") |
---|
159 | model.initialize() |
---|
160 | ev = model.get_event_info() |
---|
161 | print(ev.nextEventTime) |
---|
162 | self.assert_equal_msg(ev.nextEventTime, 2) |
---|
163 | self.assert_equal_msg(ev.nextEventTime, model.get("p")) |
---|
164 | |
---|
165 | @testattr(stddist_full = True) |
---|
166 | def test_time_event_basic_3(self): |
---|
167 | model = load_fmu("TimeEvents_Basic3.fmu") |
---|
168 | model.initialize() |
---|
169 | ev = model.get_event_info() |
---|
170 | print(ev.nextEventTime) |
---|
171 | self.assert_equal_msg(ev.nextEventTime, 1.5) |
---|
172 | |
---|
173 | @testattr(stddist_full = True) |
---|
174 | def test_time_event_basic_4(self): |
---|
175 | model = load_fmu("TimeEvents_Basic4.fmu") |
---|
176 | |
---|
177 | model.initialize() |
---|
178 | ev = model.get_event_info() |
---|
179 | self.assert_equal_msg(ev.upcomingTimeEvent, False) |
---|
180 | self.assert_equal_msg(model.get("x"), 2) |
---|
181 | |
---|
182 | model.reset() |
---|
183 | model.time = 1 |
---|
184 | model.initialize() |
---|
185 | |
---|
186 | self.assert_equal_msg(ev.upcomingTimeEvent, False) |
---|
187 | self.assert_equal_msg(model.get("x"), 1) |
---|
188 | |
---|
189 | @testattr(stddist_base = True) |
---|
190 | def test_time_event_advanced1(self): |
---|
191 | model = load_fmu("TimeEvents_Advanced1.fmu") |
---|
192 | model.initialize() |
---|
193 | ev = model.get_event_info() |
---|
194 | print(ev.nextEventTime) |
---|
195 | self.assert_equal_msg(ev.nextEventTime, 0.5) |
---|
196 | |
---|
197 | model.simulate(options={"initialize":False}) |
---|
198 | |
---|
199 | print("i (should be 2): ", model.get("i")) |
---|
200 | self.assert_equal_msg(model.get("i"), 2) |
---|
201 | |
---|
202 | @testattr(stddist_base = True) |
---|
203 | def test_time_event_advanced2(self): |
---|
204 | model = load_fmu("TimeEvents_Advanced2.fmu") |
---|
205 | model.initialize() |
---|
206 | ev = model.get_event_info() |
---|
207 | print(ev.nextEventTime) |
---|
208 | self.assert_equal_msg(ev.nextEventTime, 0.5) |
---|
209 | |
---|
210 | model.simulate(options={"initialize":False}) |
---|
211 | |
---|
212 | print("i (should be 2): ", model.get("i")) |
---|
213 | self.assert_equal_msg(model.get("i"), 2) |
---|
214 | |
---|
215 | @testattr(stddist_base = True) |
---|
216 | def test_time_event_advanced3(self): |
---|
217 | model = load_fmu("TimeEvents_Advanced3.fmu") |
---|
218 | model.initialize() |
---|
219 | ev = model.get_event_info() |
---|
220 | print(ev.nextEventTime) |
---|
221 | self.assert_equal_msg(ev.nextEventTime, 0.5) |
---|
222 | |
---|
223 | model.simulate(options={"initialize":False}) |
---|
224 | |
---|
225 | print("i (should be 2): ", model.get("i")) |
---|
226 | print("j (should be 1): ", model.get("j")) |
---|
227 | self.assert_equal_msg(model.get("i"), 2) |
---|
228 | self.assert_equal_msg(model.get("j"), 1) |
---|
229 | |
---|
230 | @testattr(stddist_base = True) |
---|
231 | def test_time_event_advanced4(self): |
---|
232 | model = load_fmu("TimeEvents_Advanced4.fmu") |
---|
233 | model.initialize() |
---|
234 | ev = model.get_event_info() |
---|
235 | print(ev.nextEventTime) |
---|
236 | self.assert_equal_msg(ev.nextEventTime, 0.5) |
---|
237 | |
---|
238 | model.simulate(options={"initialize":False}) |
---|
239 | |
---|
240 | print("i (should be 1): ", model.get("i")) |
---|
241 | print("j (should be 1): ", model.get("j")) |
---|
242 | self.assert_equal_msg(model.get("i"), 1) |
---|
243 | self.assert_equal_msg(model.get("j"), 1) |
---|
244 | |
---|
245 | @testattr(stddist_full = True) |
---|
246 | def test_time_event_mixed1(self): |
---|
247 | model = load_fmu("TimeEvents_Mixed1.fmu") |
---|
248 | model.initialize() |
---|
249 | ev = model.get_event_info() |
---|
250 | print(ev.nextEventTime) |
---|
251 | self.assert_equal_msg(ev.nextEventTime , 1.5) |
---|
252 | |
---|
253 | res = model.simulate(final_time=4, options={"initialize":False}) |
---|
254 | |
---|
255 | print("x: ", res["x"]) |
---|
256 | print("dx: ", res["der(x)"]) |
---|
257 | |
---|
258 | self.assert_equal_msg(res.solver.statistics["ntimeevents"], 2) |
---|
259 | self.assert_equal_msg(res.solver.statistics["nstateevents"], 2) |
---|
260 | |
---|
261 | """ """ |
---|
262 | """ Sampling tests. """ |
---|
263 | """ """ |
---|
264 | |
---|
265 | """ Basic test using only interval. """ |
---|
266 | |
---|
267 | @testattr(sample = True) |
---|
268 | def test_time_event_sampling1(self): |
---|
269 | model = load_fmu("TimeEvents_TestSampling1.fmu") |
---|
270 | model.initialize() |
---|
271 | res = model.simulate(0, 1e3, options={"initialize":False}) |
---|
272 | self.assert_equal_msg(res.solver.statistics["ntimeevents"], 1e4) |
---|
273 | |
---|
274 | """ Only small interval. """ |
---|
275 | |
---|
276 | @testattr(sample = True) |
---|
277 | def test_time_event_sampling2(self): |
---|
278 | model = load_fmu("TimeEvents_TestSampling2.fmu") |
---|
279 | model.initialize() |
---|
280 | res = model.simulate(0,1e-6, options={"initialize":False}) |
---|
281 | self.assert_equal_msg(res.solver.statistics["ntimeevents"], 1e4) |
---|
282 | |
---|
283 | """ Only big interval. """ |
---|
284 | |
---|
285 | @testattr(sample = True) |
---|
286 | def test_time_event_sampling3(self): |
---|
287 | model = load_fmu("TimeEvents_TestSampling3.fmu") |
---|
288 | model.initialize() |
---|
289 | res = model.simulate(0,1e64, options={"initialize":False}) |
---|
290 | self.assert_equal_msg(res.solver.statistics["ntimeevents"], 1e4) |
---|
291 | |
---|
292 | """ Basic test using offset. """ |
---|
293 | |
---|
294 | @testattr(sample = True) |
---|
295 | def test_time_event_sampling4(self): |
---|
296 | model = load_fmu("TimeEvents_TestSampling4.fmu") |
---|
297 | model.initialize() |
---|
298 | res = model.simulate(0,2e-6, options={"initialize":False}) |
---|
299 | self.assert_equal_msg(res.solver.statistics["ntimeevents"], (1e4)+1) |
---|
300 | |
---|
301 | """ Big interval, small offset. """ |
---|
302 | |
---|
303 | @testattr(sample = True) |
---|
304 | def test_time_event_sampling5(self): |
---|
305 | model = load_fmu("TimeEvents_TestSampling5.fmu") |
---|
306 | model.initialize() |
---|
307 | res = model.simulate(0,1e64, options={"initialize":False}) |
---|
308 | self.assert_equal_msg(res.solver.statistics["ntimeevents"], 1e4) |
---|
309 | |
---|
310 | """ Big interval and offset. """ |
---|
311 | |
---|
312 | @testattr(sample = True) |
---|
313 | def test_time_event_sampling6(self): |
---|
314 | model = load_fmu("TimeEvents_TestSampling6.fmu") |
---|
315 | model.initialize() |
---|
316 | res = model.simulate(0,1e64, options={"initialize":False}) |
---|
317 | self.assert_equal_msg(res.solver.statistics["ntimeevents"], 1e4) |
---|
318 | |
---|
319 | @testattr(sample = True) |
---|
320 | def test_time_event_sampling7(self): |
---|
321 | model = load_fmu("TimeEvents_TestSampling7.fmu") |
---|
322 | model.initialize() |
---|
323 | res = model.simulate(0,1e5, options={"initialize":False}) |
---|
324 | self.assert_equal_msg(res.solver.statistics["ntimeevents"], 1e4) |
---|
325 | |
---|
326 | """ Test 8 verifies that sampling raises an exception when a too small step is required. """ |
---|
327 | |
---|
328 | @testattr(sample = True) |
---|
329 | def test_time_event_sampling8(self): |
---|
330 | model = load_fmu("TimeEvents_TestSampling8.fmu") |
---|
331 | nose.tools.assert_raises(model.initialize) |
---|
332 | |
---|
333 | """ Same interval and offset. """ |
---|
334 | |
---|
335 | @testattr(sample = True) |
---|
336 | def test_time_event_sampling9(self): |
---|
337 | model = load_fmu("TimeEvents_TestSampling9.fmu") |
---|
338 | model.initialize() |
---|
339 | res = model.simulate(0,1, options={"initialize":False}) |
---|
340 | self.assert_equal_msg(res.solver.statistics["ntimeevents"], 10) |
---|
341 | |
---|
342 | @testattr(stddist_base = True) |
---|
343 | def test_time_event_state_event_after_time_event(self): |
---|
344 | model = load_fmu("TimeEvents_StateEventAfterTimeEvent.fmu") |
---|
345 | opts = model.simulate_options() |
---|
346 | opts["solver"] = "CVode" |
---|
347 | opts["CVode_options"]["rtol"] = 1e-4 |
---|
348 | res = model.simulate(0,1, options=opts) |
---|
349 | N.testing.assert_almost_equal(model.get("s"), 2.8) |
---|
350 | self.assert_equal_msg(res.solver.statistics["ntimeevents"], 2) |
---|
351 | |
---|
352 | class Test_Time_Events_FMU20: |
---|
353 | @classmethod |
---|
354 | def setUpClass(cls): |
---|
355 | """ |
---|
356 | Compile the test model. |
---|
357 | """ |
---|
358 | file_name = os.path.join(get_files_path(), 'Modelica', 'TimeEvents.mo') |
---|
359 | |
---|
360 | compile_fmu("TimeEvents.Basic1", file_name, compiler_options={"relational_time_events":True}, compiler_log_level="debug:log.txt", version="2.0") |
---|
361 | compile_fmu("TimeEvents.Basic2", file_name, compiler_options={"relational_time_events":True}, version="2.0") |
---|
362 | compile_fmu("TimeEvents.Basic3", file_name, compiler_options={"relational_time_events":True}, version="2.0") |
---|
363 | compile_fmu("TimeEvents.Basic4", file_name, compiler_options={"relational_time_events":True}, version="2.0") |
---|
364 | |
---|
365 | compile_fmu("TimeEvents.Advanced1", file_name, compiler_options={"relational_time_events":True}, version="2.0") |
---|
366 | compile_fmu("TimeEvents.Advanced2", file_name, compiler_options={"relational_time_events":True}, version="2.0") |
---|
367 | compile_fmu("TimeEvents.Advanced3", file_name, compiler_options={"relational_time_events":True}, version="2.0") |
---|
368 | compile_fmu("TimeEvents.Advanced4", file_name, compiler_options={"relational_time_events":True}, version="2.0") |
---|
369 | |
---|
370 | compile_fmu("TimeEvents.Mixed1", file_name, compiler_options={"relational_time_events":True}, version="2.0") |
---|
371 | compile_fmu("TimeEvents.TestSampling1", file_name, version="2.0") |
---|
372 | compile_fmu("TimeEvents.TestSampling2", file_name, version="2.0") |
---|
373 | compile_fmu("TimeEvents.TestSampling3", file_name, version="2.0") |
---|
374 | compile_fmu("TimeEvents.TestSampling4", file_name, version="2.0") |
---|
375 | compile_fmu("TimeEvents.TestSampling5", file_name, version="2.0") |
---|
376 | compile_fmu("TimeEvents.TestSampling6", file_name, version="2.0") |
---|
377 | compile_fmu("TimeEvents.TestSampling7", file_name, version="2.0") |
---|
378 | compile_fmu("TimeEvents.TestSampling8", file_name, version="2.0") |
---|
379 | compile_fmu("TimeEvents.TestSampling9", file_name, version="2.0") |
---|
380 | compile_fmu("TimeEvents.StateEventAfterTimeEvent", file_name, version="2.0") |
---|
381 | |
---|
382 | @testattr(stddist_full = True) |
---|
383 | def test_time_event_basic_1(self): |
---|
384 | model = load_fmu("TimeEvents_Basic1.fmu") |
---|
385 | model.initialize() |
---|
386 | model.event_update() |
---|
387 | model.enter_continuous_time_mode() |
---|
388 | ev = model.get_event_info() |
---|
389 | print(ev.nextEventTime) |
---|
390 | assert ev.nextEventTime == 1 |
---|
391 | |
---|
392 | @testattr(stddist_full = True) |
---|
393 | def test_time_event_basic_2(self): |
---|
394 | model = load_fmu("TimeEvents_Basic2.fmu") |
---|
395 | model.initialize() |
---|
396 | model.event_update() |
---|
397 | model.enter_continuous_time_mode() |
---|
398 | ev = model.get_event_info() |
---|
399 | print(ev.nextEventTime) |
---|
400 | assert ev.nextEventTime == 2 |
---|
401 | assert ev.nextEventTime == model.get("p") |
---|
402 | |
---|
403 | @testattr(stddist_full = True) |
---|
404 | def test_time_event_basic_3(self): |
---|
405 | model = load_fmu("TimeEvents_Basic3.fmu") |
---|
406 | model.initialize() |
---|
407 | model.event_update() |
---|
408 | model.enter_continuous_time_mode() |
---|
409 | ev = model.get_event_info() |
---|
410 | print(ev.nextEventTime) |
---|
411 | assert ev.nextEventTime == 1.5 |
---|
412 | |
---|
413 | @testattr(stddist_full = True) |
---|
414 | def test_time_event_basic_4(self): |
---|
415 | model = load_fmu("TimeEvents_Basic4.fmu") |
---|
416 | |
---|
417 | model.initialize() |
---|
418 | model.event_update() |
---|
419 | model.enter_continuous_time_mode() |
---|
420 | ev = model.get_event_info() |
---|
421 | assert ev.nextEventTimeDefined == False |
---|
422 | assert model.get("x")== 2 |
---|
423 | |
---|
424 | model.reset() |
---|
425 | model.time = 1 |
---|
426 | model.initialize() |
---|
427 | model.event_update() |
---|
428 | model.enter_continuous_time_mode() |
---|
429 | |
---|
430 | assert ev.nextEventTimeDefined == False |
---|
431 | assert model.get("x") == 1 |
---|
432 | |
---|
433 | @testattr(stddist_base = True) |
---|
434 | def test_time_event_advanced1(self): |
---|
435 | model = load_fmu("TimeEvents_Advanced1.fmu") |
---|
436 | model.initialize() |
---|
437 | model.event_update() |
---|
438 | model.enter_continuous_time_mode() |
---|
439 | ev = model.get_event_info() |
---|
440 | print(ev.nextEventTime) |
---|
441 | assert ev.nextEventTime == 0.5 |
---|
442 | |
---|
443 | model.simulate(options={"initialize":False}) |
---|
444 | |
---|
445 | print("i (should be 2): ", model.get("i")) |
---|
446 | assert model.get("i") == 2 |
---|
447 | |
---|
448 | @testattr(stddist_base = True) |
---|
449 | def test_time_event_advanced2(self): |
---|
450 | model = load_fmu("TimeEvents_Advanced2.fmu") |
---|
451 | model.initialize() |
---|
452 | model.event_update() |
---|
453 | model.enter_continuous_time_mode() |
---|
454 | ev = model.get_event_info() |
---|
455 | print(ev.nextEventTime) |
---|
456 | assert ev.nextEventTime == 0.5 |
---|
457 | |
---|
458 | model.simulate(options={"initialize":False}) |
---|
459 | |
---|
460 | print("i (should be 2): ", model.get("i")) |
---|
461 | assert model.get("i") == 2 |
---|
462 | |
---|
463 | @testattr(stddist_base = True) |
---|
464 | def test_time_event_advanced3(self): |
---|
465 | model = load_fmu("TimeEvents_Advanced3.fmu") |
---|
466 | model.initialize() |
---|
467 | model.event_update() |
---|
468 | model.enter_continuous_time_mode() |
---|
469 | ev = model.get_event_info() |
---|
470 | print(ev.nextEventTime) |
---|
471 | assert ev.nextEventTime == 0.5 |
---|
472 | |
---|
473 | model.simulate(options={"initialize":False}) |
---|
474 | |
---|
475 | print("i (should be 2): ", model.get("i")) |
---|
476 | print("j (should be 1): ", model.get("j")) |
---|
477 | assert model.get("i") == 2 |
---|
478 | assert model.get("j") == 1 |
---|
479 | |
---|
480 | @testattr(stddist_base = True) |
---|
481 | def test_time_event_advanced4(self): |
---|
482 | model = load_fmu("TimeEvents_Advanced4.fmu") |
---|
483 | model.initialize() |
---|
484 | model.event_update() |
---|
485 | model.enter_continuous_time_mode() |
---|
486 | ev = model.get_event_info() |
---|
487 | assert ev.nextEventTime == 0.5 |
---|
488 | |
---|
489 | model.simulate(options={"initialize":False}) |
---|
490 | |
---|
491 | print("i (should be 1): ", model.get("i")) |
---|
492 | print("j (should be 1): ", model.get("j")) |
---|
493 | assert model.get("i") == 1 |
---|
494 | assert model.get("j") == 1 |
---|
495 | |
---|
496 | @testattr(stddist_full = True) |
---|
497 | def test_time_event_mixed1(self): |
---|
498 | model = load_fmu("TimeEvents_Mixed1.fmu") |
---|
499 | model.initialize() |
---|
500 | model.event_update() |
---|
501 | model.enter_continuous_time_mode() |
---|
502 | ev = model.get_event_info() |
---|
503 | print(ev.nextEventTime) |
---|
504 | assert ev.nextEventTime == 1.5 |
---|
505 | |
---|
506 | res = model.simulate(final_time=4, options={"initialize":False}) |
---|
507 | |
---|
508 | print("x: ", res["x"]) |
---|
509 | print("dx: ", res["der(x)"]) |
---|
510 | |
---|
511 | assert res.solver.statistics["ntimeevents"] == 2 |
---|
512 | assert res.solver.statistics["nstateevents"] == 2 |
---|
513 | |
---|
514 | """ """ |
---|
515 | """ Sampling tests. """ |
---|
516 | """ """ |
---|
517 | |
---|
518 | """ Basic test using only interval. """ |
---|
519 | |
---|
520 | @testattr(sample = True) |
---|
521 | def test_time_event_sampling1(self): |
---|
522 | model = load_fmu("TimeEvents_TestSampling1.fmu") |
---|
523 | model.initialize() |
---|
524 | res = model.simulate(0, 1e3, options={"initialize":False}); |
---|
525 | assert res.solver.statistics["ntimeevents"] == 1e4 |
---|
526 | |
---|
527 | """ Only small interval. """ |
---|
528 | |
---|
529 | @testattr(sample = True) |
---|
530 | def test_time_event_sampling2(self): |
---|
531 | model = load_fmu("TimeEvents_TestSampling2.fmu") |
---|
532 | model.initialize() |
---|
533 | res = model.simulate(0,1e-6, options={"initialize":False}); |
---|
534 | assert res.solver.statistics["ntimeevents"] == 1e4 |
---|
535 | |
---|
536 | """ Only big interval. """ |
---|
537 | |
---|
538 | @testattr(sample = True) |
---|
539 | def test_time_event_sampling3(self): |
---|
540 | model = load_fmu("TimeEvents_TestSampling3.fmu") |
---|
541 | model.initialize() |
---|
542 | res = model.simulate(0,1e64, options={"initialize":False}); |
---|
543 | assert res.solver.statistics["ntimeevents"] == 1e4 |
---|
544 | |
---|
545 | """ Basic test using offset. """ |
---|
546 | |
---|
547 | @testattr(sample = True) |
---|
548 | def test_time_event_sampling4(self): |
---|
549 | model = load_fmu("TimeEvents_TestSampling4.fmu") |
---|
550 | model.initialize() |
---|
551 | res = model.simulate(0,2e-6, options={"initialize":False}); |
---|
552 | assert res.solver.statistics["ntimeevents"] == (1e4)+1 |
---|
553 | |
---|
554 | """ Big interval, small offset. """ |
---|
555 | |
---|
556 | @testattr(sample = True) |
---|
557 | def test_time_event_sampling5(self): |
---|
558 | model = load_fmu("TimeEvents_TestSampling5.fmu") |
---|
559 | model.initialize() |
---|
560 | res = model.simulate(0,1e64, options={"initialize":False}); |
---|
561 | assert res.solver.statistics["ntimeevents"] == 1e4 |
---|
562 | |
---|
563 | """ Big interval and offset. """ |
---|
564 | |
---|
565 | @testattr(sample = True) |
---|
566 | def test_time_event_sampling6(self): |
---|
567 | model = load_fmu("TimeEvents_TestSampling6.fmu") |
---|
568 | model.initialize() |
---|
569 | res = model.simulate(0,1e64, options={"initialize":False}); |
---|
570 | assert res.solver.statistics["ntimeevents"] == 1e4 |
---|
571 | |
---|
572 | @testattr(sample = True) |
---|
573 | def test_time_event_sampling7(self): |
---|
574 | model = load_fmu("TimeEvents_TestSampling7.fmu") |
---|
575 | model.initialize() |
---|
576 | res = model.simulate(0,1e5, options={"initialize":False}); |
---|
577 | assert res.solver.statistics["ntimeevents"] == 1e4 |
---|
578 | |
---|
579 | """ Test 8 verifies that sampling raises an exception when a too small step is required. """ |
---|
580 | |
---|
581 | @testattr(sample = True) |
---|
582 | def test_time_event_sampling8(self): |
---|
583 | model = load_fmu("TimeEvents_TestSampling8.fmu") |
---|
584 | nose.tools.assert_raises(model.initialize) |
---|
585 | |
---|
586 | """ Same interval and offset. """ |
---|
587 | |
---|
588 | @testattr(sample = True) |
---|
589 | def test_time_event_sampling9(self): |
---|
590 | model = load_fmu("TimeEvents_TestSampling9.fmu") |
---|
591 | model.initialize() |
---|
592 | res = model.simulate(0,1, options={"initialize":False}); |
---|
593 | assert res.solver.statistics["ntimeevents"] == 10 |
---|
594 | |
---|
595 | @testattr(stddist_base = True) |
---|
596 | def test_time_event_state_event_after_time_event(self): |
---|
597 | model = load_fmu("TimeEvents_StateEventAfterTimeEvent.fmu") |
---|
598 | opts = model.simulate_options() |
---|
599 | opts["solver"] = "CVode" |
---|
600 | opts["CVode_options"]["rtol"] = 1e-4 |
---|
601 | res = model.simulate(0,1, options=opts); |
---|
602 | N.testing.assert_almost_equal(model.get("s"), 2.8) |
---|
603 | assert res.solver.statistics["ntimeevents"] == 2 |
---|
604 | |
---|
605 | class Test_DynamicStates: |
---|
606 | @classmethod |
---|
607 | def setUpClass(cls): |
---|
608 | """ |
---|
609 | Compile the test model. |
---|
610 | """ |
---|
611 | file_name = os.path.join(get_files_path(), 'Modelica', 'RevoluteConstraint.mo') |
---|
612 | |
---|
613 | compile_fmu("StrippedRevoluteConstraint", file_name) |
---|
614 | |
---|
615 | @testattr(stddist_full = True) |
---|
616 | def test_no_switch_of_states(self): |
---|
617 | model = load_fmu("StrippedRevoluteConstraint.fmu") |
---|
618 | |
---|
619 | res = model.simulate(final_time=10) |
---|
620 | |
---|
621 | #No step events triggered |
---|
622 | assert res.solver.statistics["nstepevents"] == 0 |
---|
623 | |
---|
624 | var = res["freeMotionScalarInit.angle_2"] |
---|
625 | |
---|
626 | N.testing.assert_almost_equal(abs(max(var)), 0.00, 2) |
---|
627 | N.testing.assert_almost_equal(abs(min(var)), 2.54, 2) |
---|
628 | |
---|
629 | class Test_Events: |
---|
630 | @classmethod |
---|
631 | def setUpClass(cls): |
---|
632 | """ |
---|
633 | Compile the test model. |
---|
634 | """ |
---|
635 | file_name = os.path.join(get_files_path(), 'Modelica', 'EventIter.mo') |
---|
636 | |
---|
637 | compile_fmu("EventIter.EventInfiniteIteration1", file_name) |
---|
638 | compile_fmu("EventIter.EventInfiniteIteration2", file_name) |
---|
639 | compile_fmu("EventIter.EventInfiniteIteration3", file_name) |
---|
640 | compile_fmu("EventIter.EnhancedEventIteration1", file_name) |
---|
641 | compile_fmu("EventIter.EnhancedEventIteration2", file_name) |
---|
642 | compile_fmu("EventIter.EnhancedEventIteration3", file_name) |
---|
643 | compile_fmu("EventIter.SingularSystem1", file_name) |
---|
644 | compile_fmu("EventIter.InitialPhasing1", file_name) |
---|
645 | compile_fmu("EventIter.EventIterDiscreteReals", file_name) |
---|
646 | compile_fmu("EventIter.EventAfterTimeEvent", file_name) |
---|
647 | |
---|
648 | @testattr(stddist_full = True) |
---|
649 | def test_reinit_after_two_time_events(self): |
---|
650 | model = load_fmu("EventIter_EventAfterTimeEvent.fmu") |
---|
651 | |
---|
652 | res = model.simulate() |
---|
653 | |
---|
654 | N.testing.assert_almost_equal(res.final("s"), -1.0) |
---|
655 | |
---|
656 | @testattr(stddist_full = True) |
---|
657 | def test_event_infinite_iteration_1(self): |
---|
658 | model = load_fmu("EventIter_EventInfiniteIteration1.fmu") |
---|
659 | nose.tools.assert_raises(FMUException, model.initialize) |
---|
660 | |
---|
661 | @testattr(stddist_full = True) |
---|
662 | def test_event_infinite_iteration_2(self): |
---|
663 | model = load_fmu("EventIter_EventInfiniteIteration2.fmu") |
---|
664 | nose.tools.assert_raises(FMUException, model.initialize) |
---|
665 | |
---|
666 | @testattr(stddist_full = True) |
---|
667 | def test_event_infinite_iteration_3(self): |
---|
668 | model = load_fmu("EventIter_EventInfiniteIteration3.fmu") |
---|
669 | nose.tools.assert_raises(FMUException, model.simulate) |
---|
670 | |
---|
671 | @testattr(stddist_full = True) |
---|
672 | def test_singular_system_event_1(self): |
---|
673 | model = load_fmu("EventIter_SingularSystem1.fmu") |
---|
674 | |
---|
675 | #Check that we can initialize without error! |
---|
676 | model.initialize() |
---|
677 | |
---|
678 | N.testing.assert_almost_equal(model.get("mode"), 0.0) |
---|
679 | N.testing.assert_almost_equal(model.get("sa"), 0.0) |
---|
680 | |
---|
681 | @testattr(stddist_base = True) |
---|
682 | def test_enhanced_event_iteration_1(self): |
---|
683 | model = load_fmu("EventIter_EnhancedEventIteration1.fmu") |
---|
684 | res = model.simulate() |
---|
685 | |
---|
686 | N.testing.assert_almost_equal(res["x[1]"][-1], 0) |
---|
687 | N.testing.assert_almost_equal(res["x[2]"][-1], 0) |
---|
688 | N.testing.assert_almost_equal(res["x[3]"][-1], 0) |
---|
689 | N.testing.assert_almost_equal(res["x[4]"][-1], -0.406) |
---|
690 | N.testing.assert_almost_equal(res["x[5]"][-1], -0.406) |
---|
691 | N.testing.assert_almost_equal(res["x[6]"][-1], -0.406) |
---|
692 | N.testing.assert_almost_equal(res["x[7]"][-1], 0.94) |
---|
693 | |
---|
694 | @testattr(stddist_base = True) |
---|
695 | def test_enhanced_event_iteration_2(self): |
---|
696 | model = load_fmu("EventIter_EnhancedEventIteration2.fmu") |
---|
697 | res = model.simulate(final_time=2.0) |
---|
698 | |
---|
699 | N.testing.assert_almost_equal(res["y"][0], 1.0) |
---|
700 | N.testing.assert_almost_equal(res["w"][0], 0.0) |
---|
701 | N.testing.assert_almost_equal(res["x"][-1], 2.0) |
---|
702 | N.testing.assert_almost_equal(res["y"][-1],1.58385,4) |
---|
703 | N.testing.assert_almost_equal(res["z"][-1], 0.0) |
---|
704 | N.testing.assert_almost_equal(res["w"][-1], 1.0) |
---|
705 | |
---|
706 | @testattr(stddist_base = True) |
---|
707 | def test_enhanced_event_iteration_3(self): |
---|
708 | model = load_fmu("EventIter_EnhancedEventIteration3.fmu") |
---|
709 | model.initialize(tolerance=1e-1) |
---|
710 | |
---|
711 | N.testing.assert_almost_equal(model.get("x"), -1e-6) |
---|
712 | |
---|
713 | @testattr(stddist_full = True) |
---|
714 | def test_initial_phasing_1(self): |
---|
715 | model = load_fmu("EventIter_InitialPhasing1.fmu") |
---|
716 | res = model.simulate(final_time=0.1) |
---|
717 | N.testing.assert_almost_equal(res["b1"][0], 0.0) |
---|
718 | N.testing.assert_almost_equal(res["b2"][0], 1.0) |
---|
719 | |
---|
720 | @testattr(stddist_base=True) |
---|
721 | def test_discrete_real_event_iteration(self): |
---|
722 | model = load_fmu("EventIter_EventIterDiscreteReals.fmu") |
---|
723 | res = model.simulate(final_time=1.0) |
---|
724 | N.testing.assert_almost_equal(res["T1"][0], 0.0) |
---|
725 | N.testing.assert_almost_equal(res["start"][0], 1.0) |
---|
726 | N.testing.assert_almost_equal(res["T2"][0], 0.0) |
---|
727 | |
---|
728 | class Test_Relations: |
---|
729 | @classmethod |
---|
730 | def setUpClass(cls): |
---|
731 | """ |
---|
732 | Compile the test model. |
---|
733 | """ |
---|
734 | file_name = os.path.join(get_files_path(), 'Modelica', 'RelationTests.mo') |
---|
735 | |
---|
736 | compile_fmu("RelationTests.RelationLE", file_name) |
---|
737 | compile_fmu("RelationTests.RelationGE", file_name) |
---|
738 | compile_fmu("RelationTests.RelationLEInv", file_name) |
---|
739 | compile_fmu("RelationTests.RelationGEInv", file_name) |
---|
740 | compile_fmu("RelationTests.RelationLEInit", file_name) |
---|
741 | compile_fmu("RelationTests.RelationGEInit", file_name) |
---|
742 | compile_fmu("RelationTests.TestRelationalOp1", file_name) |
---|
743 | |
---|
744 | @testattr(stddist_full = True) |
---|
745 | def test_relation_le(self): |
---|
746 | model = load_fmu("RelationTests_RelationLE.fmu") |
---|
747 | opts = model.simulate_options() |
---|
748 | opts["CVode_options"]["maxh"] = 0.001 |
---|
749 | res = model.simulate(final_time=3.5, input=input_object,options=opts) |
---|
750 | |
---|
751 | N.testing.assert_almost_equal(N.interp(0.75,res["time"],res["y"]),0.5,places=2) |
---|
752 | N.testing.assert_almost_equal(N.interp(2.25,res["time"],res["y"]),0.5,places=2) |
---|
753 | nose.tools.assert_not_almost_equal(N.interp(0.25,res["time"],res["y"]),0.5,places=2) |
---|
754 | nose.tools.assert_not_almost_equal(N.interp(2.75,res["time"],res["y"]),0.5,places=2) |
---|
755 | nose.tools.assert_not_almost_equal(N.interp(0.75,res["time"],res["x"]),0.5,places=2) |
---|
756 | nose.tools.assert_not_almost_equal(N.interp(2.25,res["time"],res["x"]),0.5,places=2) |
---|
757 | N.testing.assert_almost_equal(N.interp(1.5,res["time"],res["y"]),0.5,places=2) |
---|
758 | |
---|
759 | @testattr(stddist_full = True) |
---|
760 | def test_relation_leinv(self): |
---|
761 | model = load_fmu("RelationTests_RelationLEInv.fmu") |
---|
762 | opts = model.simulate_options() |
---|
763 | opts["CVode_options"]["maxh"] = 0.001 |
---|
764 | res = model.simulate(final_time=3.5, input=input_object,options=opts) |
---|
765 | |
---|
766 | N.testing.assert_almost_equal(N.interp(0.75,res["time"],res["y"]),0.5,places=2) |
---|
767 | N.testing.assert_almost_equal(N.interp(2.25,res["time"],res["y"]),0.5,places=2) |
---|
768 | nose.tools.assert_not_almost_equal(N.interp(0.25,res["time"],res["y"]),0.5,places=2) |
---|
769 | nose.tools.assert_not_almost_equal(N.interp(2.75,res["time"],res["y"]),0.5,places=2) |
---|
770 | nose.tools.assert_not_almost_equal(N.interp(0.75,res["time"],res["x"]),0.5,places=2) |
---|
771 | nose.tools.assert_not_almost_equal(N.interp(2.25,res["time"],res["x"]),0.5,places=2) |
---|
772 | N.testing.assert_almost_equal(N.interp(1.5,res["time"],res["y"]),0.5,places=2) |
---|
773 | |
---|
774 | @testattr(stddist_full = True) |
---|
775 | def test_relation_ge(self): |
---|
776 | model = load_fmu("RelationTests_RelationGE.fmu") |
---|
777 | opts = model.simulate_options() |
---|
778 | opts["CVode_options"]["maxh"] = 0.001 |
---|
779 | res = model.simulate(final_time=3.5, input=input_object,options=opts) |
---|
780 | |
---|
781 | N.testing.assert_almost_equal(N.interp(0.75,res["time"],res["y"]),0.5,places=2) |
---|
782 | N.testing.assert_almost_equal(N.interp(2.25,res["time"],res["y"]),0.5,places=2) |
---|
783 | nose.tools.assert_not_almost_equal(N.interp(0.25,res["time"],res["y"]),0.5,places=2) |
---|
784 | nose.tools.assert_not_almost_equal(N.interp(2.75,res["time"],res["y"]),0.5,places=2) |
---|
785 | nose.tools.assert_not_almost_equal(N.interp(0.75,res["time"],res["x"]),0.5,places=2) |
---|
786 | nose.tools.assert_not_almost_equal(N.interp(2.25,res["time"],res["x"]),0.5,places=2) |
---|
787 | N.testing.assert_almost_equal(N.interp(1.5,res["time"],res["y"]),0.5,places=2) |
---|
788 | |
---|
789 | @testattr(stddist_full = True) |
---|
790 | def test_relation_geinv(self): |
---|
791 | model = load_fmu("RelationTests_RelationGEInv.fmu") |
---|
792 | opts = model.simulate_options() |
---|
793 | opts["CVode_options"]["maxh"] = 0.001 |
---|
794 | res = model.simulate(final_time=3.5, input=input_object,options=opts) |
---|
795 | |
---|
796 | N.testing.assert_almost_equal(N.interp(0.75,res["time"],res["y"]),0.5,places=2) |
---|
797 | N.testing.assert_almost_equal(N.interp(2.25,res["time"],res["y"]),0.5,places=2) |
---|
798 | nose.tools.assert_not_almost_equal(N.interp(0.25,res["time"],res["y"]),0.5,places=2) |
---|
799 | nose.tools.assert_not_almost_equal(N.interp(2.75,res["time"],res["y"]),0.5,places=2) |
---|
800 | nose.tools.assert_not_almost_equal(N.interp(0.75,res["time"],res["x"]),0.5,places=2) |
---|
801 | nose.tools.assert_not_almost_equal(N.interp(2.25,res["time"],res["x"]),0.5,places=2) |
---|
802 | N.testing.assert_almost_equal(N.interp(1.5,res["time"],res["y"]),0.5,places=2) |
---|
803 | |
---|
804 | @testattr(stddist_full = True) |
---|
805 | def test_relation_leinit(self): |
---|
806 | model = load_fmu("RelationTests_RelationLEInit.fmu") |
---|
807 | |
---|
808 | res = model.simulate(final_time=0.1) |
---|
809 | |
---|
810 | N.testing.assert_almost_equal(res.initial("x"),1.0,places=3) |
---|
811 | N.testing.assert_almost_equal(res.initial("y"),0.0,places=3) |
---|
812 | |
---|
813 | @testattr(stddist_full = True) |
---|
814 | def test_relation_geinit(self): |
---|
815 | model = load_fmu("RelationTests_RelationGEInit.fmu") |
---|
816 | |
---|
817 | res = model.simulate(final_time=0.1) |
---|
818 | |
---|
819 | N.testing.assert_almost_equal(res.initial("x"),0.0,places=3) |
---|
820 | N.testing.assert_almost_equal(res.initial("y"),1.0,places=3) |
---|
821 | |
---|
822 | @testattr(stddist_full = True) |
---|
823 | def test_relation_op_1(self): |
---|
824 | model = load_fmu("RelationTests_TestRelationalOp1.fmu") |
---|
825 | |
---|
826 | res = model.simulate(final_time=10) |
---|
827 | |
---|
828 | N.testing.assert_almost_equal(N.interp(3.00,res["time"],res["der(v1)"]),1.0,places=3) |
---|
829 | N.testing.assert_almost_equal(N.interp(3.40,res["time"],res["der(v1)"]),0.0,places=3) |
---|
830 | N.testing.assert_almost_equal(N.interp(8.00,res["time"],res["der(v1)"]),0.0,places=3) |
---|
831 | N.testing.assert_almost_equal(N.interp(8.25,res["time"],res["der(v1)"]),1.0,places=3) |
---|
832 | N.testing.assert_almost_equal(N.interp(4.00,res["time"],res["der(v2)"]),1.0,places=3) |
---|
833 | N.testing.assert_almost_equal(N.interp(4.20,res["time"],res["der(v2)"]),0.0,places=3) |
---|
834 | N.testing.assert_almost_equal(N.interp(7.00,res["time"],res["der(v2)"]),0.0,places=3) |
---|
835 | N.testing.assert_almost_equal(N.interp(7.20,res["time"],res["der(v2)"]),1.0,places=3) |
---|
836 | |
---|
837 | class Test_NonLinear_Systems: |
---|
838 | |
---|
839 | @classmethod |
---|
840 | def setUpClass(cls): |
---|
841 | """ |
---|
842 | Compile the test model. |
---|
843 | """ |
---|
844 | file_name = os.path.join(get_files_path(), 'Modelica', 'NonLinear.mo') |
---|
845 | |
---|
846 | compile_fmu("NonLinear.NominalStart1", file_name) |
---|
847 | compile_fmu("NonLinear.NominalStart2", file_name) |
---|
848 | compile_fmu("NonLinear.NominalStart3", file_name) |
---|
849 | compile_fmu("NonLinear.NominalStart4", file_name) |
---|
850 | compile_fmu("NonLinear.NominalStart5", file_name) |
---|
851 | compile_fmu("NonLinear.NominalStart6", file_name) |
---|
852 | compile_fmu("NonLinear.DoubleRoot1", file_name) |
---|
853 | compile_fmu("NonLinear.NonLinear3", file_name) |
---|
854 | compile_fmu("NonLinear.NonLinear4", file_name) |
---|
855 | compile_fmu("NonLinear.ResidualHeuristicScaling1", file_name) |
---|
856 | compile_fmu("NonLinear.EventIteration1", file_name) |
---|
857 | compile_fmu("NonLinear.NonLinear6", file_name) |
---|
858 | compile_fmu("NonLinear.NonLinear7", file_name) |
---|
859 | compile_fmu("NonLinear.RealTimeSolver1", file_name, compile_to="RT_init.fmu", version=1.0, compiler_options={"init_nonlinear_solver":"realtime"}) |
---|
860 | compile_fmu("NonLinear.RealTimeSolver1", file_name, compile_to="RT_ode.fmu", version=1.0, compiler_options={"nonlinear_solver":"realtime"}) |
---|
861 | |
---|
862 | @testattr(stddist_base= True) |
---|
863 | def test_realtime_solver_init(self): |
---|
864 | model = load_fmu("RT_init.fmu", log_level=4) |
---|
865 | model.set("_log_level", 4) |
---|
866 | |
---|
867 | model.initialize() |
---|
868 | |
---|
869 | from pyjmi.log import parser |
---|
870 | |
---|
871 | log = parser.parse_jmi_log("RT_init_log.txt") |
---|
872 | assert len(log.find("RealtimeConvergence")) > 0 |
---|
873 | |
---|
874 | @testattr(stddist_base= True) |
---|
875 | def test_realtime_solver(self): |
---|
876 | model = load_fmu("RT_ode.fmu", log_level=4) |
---|
877 | model.set("_log_level", 4) |
---|
878 | |
---|
879 | model.initialize() |
---|
880 | |
---|
881 | from pyjmi.log import parser |
---|
882 | |
---|
883 | log = parser.parse_jmi_log("RT_ode_log.txt") |
---|
884 | assert len(log.find("RealtimeConvergence")) == 3 #Three invocations during initialization |
---|
885 | |
---|
886 | model.simulate(options={"initialize":False}) |
---|
887 | |
---|
888 | log = parser.parse_jmi_log("RT_ode_log.txt") |
---|
889 | assert len(log.find("RealtimeConvergence")) > 3 |
---|
890 | |
---|
891 | @testattr(windows_base = True) |
---|
892 | def test_Brent_AD(self): |
---|
893 | |
---|
894 | model = load_fmu(os.path.join(get_files_path(), "FMUs/ME2.0", "NonLinear_NonLinear5.fmu"), log_level=6) |
---|
895 | model.set("_log_level", 8) |
---|
896 | |
---|
897 | model.initialize() |
---|
898 | |
---|
899 | def get_history(filename): |
---|
900 | import re |
---|
901 | data = [[],[]] |
---|
902 | with open(filename, 'r') as f: |
---|
903 | line = f.readline() |
---|
904 | while line: |
---|
905 | if line.find("Iteration variable") != -1: |
---|
906 | iv = re.search('<value name="ivs">(.*)</value>, Function', line).group(1).strip() |
---|
907 | df = re.search('<value name="df">(.*)</value>, Delta', line).group(1).strip() |
---|
908 | data[0].append(float(iv)) |
---|
909 | data[1].append(float(df)) |
---|
910 | line = f.readline() |
---|
911 | return data[0], data[1] |
---|
912 | |
---|
913 | ivs,df = get_history("NonLinear_NonLinear5_log.txt") |
---|
914 | |
---|
915 | fprime = lambda y: -200*y; |
---|
916 | |
---|
917 | for i,iv in enumerate(ivs): |
---|
918 | N.testing.assert_almost_equal(fprime(iv) ,df[i], places=12) |
---|
919 | |
---|
920 | @testattr(stddist_base = True) |
---|
921 | def test_Brent_double_root1(self): |
---|
922 | def run_model(init): |
---|
923 | model = load_fmu("NonLinear_DoubleRoot1.fmu") |
---|
924 | model.set("_use_Brent_in_1d", True) |
---|
925 | model.set("p", init) |
---|
926 | model.initialize() |
---|
927 | return model.get("x") |
---|
928 | |
---|
929 | sol_pos = N.sqrt(1e-7)+1.5 |
---|
930 | sol_neg =-N.sqrt(1e-7)+1.5 |
---|
931 | |
---|
932 | N.testing.assert_almost_equal(run_model(sol_pos+1e-16) ,sol_pos) |
---|
933 | N.testing.assert_almost_equal(run_model(sol_pos-1e-16) ,sol_pos) |
---|
934 | N.testing.assert_almost_equal(run_model(sol_pos+1e-14) ,sol_pos) |
---|
935 | N.testing.assert_almost_equal(run_model(sol_pos-1e-14) ,sol_pos) |
---|
936 | |
---|
937 | N.testing.assert_almost_equal(run_model(sol_neg+1e-16) ,sol_neg) |
---|
938 | N.testing.assert_almost_equal(run_model(sol_neg-1e-16) ,sol_neg) |
---|
939 | N.testing.assert_almost_equal(run_model(sol_neg+1e-14) ,sol_neg) |
---|
940 | N.testing.assert_almost_equal(run_model(sol_neg-1e-14) ,sol_neg) |
---|
941 | |
---|
942 | @testattr(stddist_base = True) |
---|
943 | def test_Brent_close_to_root(self): |
---|
944 | model = load_fmu("NonLinear_NonLinear3.fmu") |
---|
945 | model.set("_use_Brent_in_1d", True) |
---|
946 | |
---|
947 | model.set("i",-9.9760004108556469E-03) |
---|
948 | model.initialize() |
---|
949 | |
---|
950 | N.testing.assert_almost_equal(model.get("i"),-9.9760004108556469E-03) |
---|
951 | |
---|
952 | @testattr(stddist_base = True) |
---|
953 | def test_Brent_close_to_root(self): |
---|
954 | model = load_fmu("NonLinear_NonLinear4.fmu") |
---|
955 | model.set("_use_Brent_in_1d", True) |
---|
956 | |
---|
957 | scale = model.get("scale") |
---|
958 | i = -9.9760004108556469E-03*scale |
---|
959 | model.set("i_start",i) |
---|
960 | model.initialize() |
---|
961 | N.testing.assert_almost_equal(model.get("i"),i) |
---|
962 | |
---|
963 | model.reset() |
---|
964 | |
---|
965 | model.set("i_start", i+i*1e-15) |
---|
966 | model.initialize() |
---|
967 | N.testing.assert_almost_equal(model.get("i"),i) |
---|
968 | |
---|
969 | model.reset() |
---|
970 | |
---|
971 | model.set("i_start", i-i*1e-15) |
---|
972 | model.initialize() |
---|
973 | N.testing.assert_almost_equal(model.get("i"),i) |
---|
974 | |
---|
975 | |
---|
976 | |
---|
977 | @testattr(stddist_base = True) |
---|
978 | def test_nominals_fallback_1(self): |
---|
979 | model = load_fmu("NonLinear_NominalStart1.fmu") |
---|
980 | model.set("_nle_solver_use_nominals_as_fallback", True) |
---|
981 | model.initialize() |
---|
982 | |
---|
983 | @testattr(stddist_base = True) |
---|
984 | def test_nominals_fallback_2(self): |
---|
985 | model = load_fmu("NonLinear_NominalStart1.fmu") |
---|
986 | model.set("_nle_solver_use_nominals_as_fallback", False) |
---|
987 | nose.tools.assert_raises(FMUException, model.initialize) |
---|
988 | |
---|
989 | @testattr(stddist_base = True) |
---|
990 | def test_nominals_fallback_3(self): |
---|
991 | model = load_fmu("NonLinear_NominalStart2.fmu") |
---|
992 | model.set("_nle_solver_use_nominals_as_fallback", True) |
---|
993 | nose.tools.assert_raises(FMUException, model.initialize) |
---|
994 | |
---|
995 | @testattr(stddist_base = True) |
---|
996 | def test_nominals_fallback_4(self): |
---|
997 | model = load_fmu("NonLinear_NominalStart4.fmu") |
---|
998 | model.set("_use_Brent_in_1d", False) |
---|
999 | model.set("_nle_solver_use_nominals_as_fallback", True) |
---|
1000 | nose.tools.assert_raises(FMUException, model.initialize) |
---|
1001 | |
---|
1002 | @testattr(stddist_base = True) |
---|
1003 | def test_nominals_fallback_5(self): |
---|
1004 | model = load_fmu("NonLinear_NominalStart5.fmu") |
---|
1005 | model.set("_use_Brent_in_1d", False) |
---|
1006 | model.set("_nle_solver_use_nominals_as_fallback", True) |
---|
1007 | nose.tools.assert_raises(FMUException, model.initialize) |
---|
1008 | |
---|
1009 | @testattr(stddist_base = True) |
---|
1010 | def test_nominals_fallback_6(self): |
---|
1011 | model = load_fmu("NonLinear_NominalStart3.fmu") |
---|
1012 | model.set("_use_Brent_in_1d", False) |
---|
1013 | model.set("_nle_solver_use_nominals_as_fallback", True) |
---|
1014 | model.set('_nle_jacobian_update_mode', 0) |
---|
1015 | model.initialize() |
---|
1016 | |
---|
1017 | N.testing.assert_almost_equal(model.get("x") ,2.76929235) |
---|
1018 | |
---|
1019 | @testattr(stddist_base = True) |
---|
1020 | def test_nominals_fallback_7(self): |
---|
1021 | model = load_fmu("NonLinear_NominalStart6.fmu") |
---|
1022 | model.set("_nle_solver_use_nominals_as_fallback", True) |
---|
1023 | model.initialize() |
---|
1024 | |
---|
1025 | N.testing.assert_almost_equal(model.get("x"), 0.680716920494911) |
---|
1026 | N.testing.assert_almost_equal(model.get("y"), 0.0) |
---|
1027 | |
---|
1028 | @testattr(stddist_base = True) |
---|
1029 | def test_residual_scaling_heuristics(self): |
---|
1030 | model = load_fmu("NonLinear_ResidualHeuristicScaling1.fmu") |
---|
1031 | model.set("_use_Brent_in_1d", False) |
---|
1032 | model.initialize() |
---|
1033 | N.testing.assert_almost_equal(model.get('state_a_p'), 17.78200351) |
---|
1034 | |
---|
1035 | @testattr(stddist_base = True) |
---|
1036 | def test_event_iternation_inf_check_warmup(self): |
---|
1037 | # Test where event inf check cannot only look at the switches but need |
---|
1038 | # to look at the iteration variables too. Generally this is needed for |
---|
1039 | # models with systems that have many solutions and bad start values. In |
---|
1040 | # this model the variable iter_var_1 will go from 300 to ca 874 and then |
---|
1041 | # if it don't get stuck in inf check it will go to 872.98062403 |
---|
1042 | model = load_fmu("NonLinear_EventIteration1.fmu") |
---|
1043 | model.initialize() |
---|
1044 | N.testing.assert_almost_equal(model.get('iter_var_1'), 872.98062403) |
---|
1045 | |
---|
1046 | @testattr(stddist_base = True) |
---|
1047 | def test_fixed_false_start_attribute(self): |
---|
1048 | model = load_fmu("NonLinear_NonLinear6.fmu") |
---|
1049 | model.initialize() |
---|
1050 | N.testing.assert_almost_equal(model.get('x'), 1.0) |
---|
1051 | N.testing.assert_almost_equal(model.get('z'), -1.0) |
---|
1052 | |
---|
1053 | @testattr(stddist_base = True) |
---|
1054 | def test_fixed_false_start_attribute_brent(self): |
---|
1055 | model = load_fmu("NonLinear_NonLinear7.fmu") |
---|
1056 | model.initialize() |
---|
1057 | N.testing.assert_almost_equal(model.get('x'), 1.0) |
---|
1058 | N.testing.assert_almost_equal(model.get('z'), 1.0) |
---|
1059 | |
---|
1060 | class Test_Singular_Systems: |
---|
1061 | |
---|
1062 | @classmethod |
---|
1063 | def setUpClass(cls): |
---|
1064 | """ |
---|
1065 | Compile the test model. |
---|
1066 | """ |
---|
1067 | file_name = os.path.join(get_files_path(), 'Modelica', 'Singular.mo') |
---|
1068 | |
---|
1069 | compile_fmu("Singular.LinearInf", file_name) |
---|
1070 | compile_fmu("Singular.Linear2", file_name) |
---|
1071 | compile_fmu("Singular.LinearEvent1", file_name) |
---|
1072 | compile_fmu("Singular.LinearEvent2", file_name) |
---|
1073 | compile_fmu("Singular.NonLinear1", file_name) |
---|
1074 | compile_fmu("Singular.NonLinear4", file_name) |
---|
1075 | compile_fmu("Singular.NonLinear5", file_name) |
---|
1076 | compile_fmu("Singular.NoMinimumNormSolution", file_name) |
---|
1077 | compile_fmu("Singular.ZeroColumnJacobian", file_name) |
---|
1078 | compile_fmu("Singular.ZeroColumnJacobian2", file_name) |
---|
1079 | compile_fmu("Singular.LinearEvent3", file_name) |
---|
1080 | |
---|
1081 | @testattr(stddist_base = True) |
---|
1082 | def test_linear_event_1(self): |
---|
1083 | model = load_fmu("Singular_LinearEvent1.fmu", log_level=3) |
---|
1084 | model.set("_log_level", 3) |
---|
1085 | |
---|
1086 | res = model.simulate(final_time=2) |
---|
1087 | N.testing.assert_almost_equal(res.final('y') ,1.000000000) |
---|
1088 | |
---|
1089 | @testattr(stddist_base = True) |
---|
1090 | def test_linear_event_2(self): |
---|
1091 | model = load_fmu("Singular_LinearEvent2.fmu", log_level=3) |
---|
1092 | model.set("_log_level", 3) |
---|
1093 | |
---|
1094 | res = model.simulate(final_time=4) |
---|
1095 | N.testing.assert_almost_equal(res.final('y') ,1.000000000) |
---|
1096 | N.testing.assert_almost_equal(res.final('w') ,2.000000000) |
---|
1097 | |
---|
1098 | @testattr(stddist_base = True) |
---|
1099 | def test_linear_event_3(self): |
---|
1100 | model = load_fmu("Singular_LinearEvent3.fmu", log_level=3) |
---|
1101 | model.set("_log_level", 3) |
---|
1102 | |
---|
1103 | opts = model.simulate_options() |
---|
1104 | opts["ncp"] = 7 |
---|
1105 | opts["CVode_options"]["maxh"] = 0.49 |
---|
1106 | |
---|
1107 | res = model.simulate(final_time=1, options=opts) |
---|
1108 | x = res["x"] |
---|
1109 | for i in range(4): |
---|
1110 | N.testing.assert_almost_equal(x[i] ,0.000000000) |
---|
1111 | for i in range(4,8): |
---|
1112 | N.testing.assert_almost_equal(x[i] ,0.0100000) |
---|
1113 | |
---|
1114 | @testattr(stddist_base = True) |
---|
1115 | def test_linear_inf_1(self): |
---|
1116 | |
---|
1117 | model = load_fmu("Singular_LinearInf.fmu", log_level=6) |
---|
1118 | model.set("_log_level", 6) |
---|
1119 | |
---|
1120 | model.set("a22", N.inf) |
---|
1121 | nose.tools.assert_raises(FMUException, model.initialize) |
---|
1122 | |
---|
1123 | @testattr(stddist_base = True) |
---|
1124 | def test_linear_inf_2(self): |
---|
1125 | |
---|
1126 | model = load_fmu("Singular_LinearInf.fmu", log_level=6) |
---|
1127 | model.set("_log_level", 6) |
---|
1128 | |
---|
1129 | model.set("a33", 0) |
---|
1130 | model.set("a22", N.inf) |
---|
1131 | nose.tools.assert_raises(FMUException, model.initialize) |
---|
1132 | |
---|
1133 | @testattr(stddist_base = True) |
---|
1134 | def test_linear_inf_3(self): |
---|
1135 | |
---|
1136 | model = load_fmu("Singular_LinearInf.fmu", log_level=6) |
---|
1137 | model.set("_log_level", 6) |
---|
1138 | |
---|
1139 | model.set("b[1]", N.inf) |
---|
1140 | nose.tools.assert_raises(FMUException, model.initialize) |
---|
1141 | |
---|
1142 | @testattr(stddist_base = True) |
---|
1143 | def test_nonlinear_1(self): |
---|
1144 | |
---|
1145 | model = load_fmu("Singular_NonLinear1.fmu") |
---|
1146 | model.set("a33", 0) |
---|
1147 | model.set("b[3]", 0) |
---|
1148 | |
---|
1149 | model.initialize() |
---|
1150 | |
---|
1151 | N.testing.assert_almost_equal(model.get("x") ,1.000000000) |
---|
1152 | N.testing.assert_almost_equal(model.get("y") ,2.000000000) |
---|
1153 | N.testing.assert_almost_equal(model.get("z") ,0.000000000) |
---|
1154 | |
---|
1155 | @testattr(stddist_base = True) |
---|
1156 | def test_nonlinear_2(self): |
---|
1157 | |
---|
1158 | model = load_fmu("Singular_NonLinear1.fmu") |
---|
1159 | model.set("a33", 0) |
---|
1160 | model.set("b[3]", 3) |
---|
1161 | |
---|
1162 | nose.tools.assert_raises(FMUException, model.initialize) |
---|
1163 | |
---|
1164 | @testattr(stddist_base = True) |
---|
1165 | def test_nonlinear_3(self): |
---|
1166 | |
---|
1167 | model = load_fmu("Singular_NonLinear1.fmu") |
---|
1168 | model.set("a33", 0) |
---|
1169 | model.set("a22", 1e-5) |
---|
1170 | model.set("b[3]", 0) |
---|
1171 | |
---|
1172 | model.initialize() |
---|
1173 | |
---|
1174 | N.testing.assert_almost_equal(model.get("x") ,1.000000000) |
---|
1175 | N.testing.assert_almost_equal(model.get("y") ,200000.0000) |
---|
1176 | N.testing.assert_almost_equal(model.get("z") ,0.000000000) |
---|
1177 | |
---|
1178 | @testattr(stddist_base = True) |
---|
1179 | def test_nonlinear_4(self): |
---|
1180 | |
---|
1181 | model = load_fmu("Singular_NonLinear1.fmu") |
---|
1182 | model.set("a33", 0) |
---|
1183 | model.set("a22", 1e10) |
---|
1184 | model.set("b[3]", 0) |
---|
1185 | |
---|
1186 | model.initialize() |
---|
1187 | |
---|
1188 | N.testing.assert_almost_equal(model.get("x") ,1.000000000) |
---|
1189 | N.testing.assert_almost_equal(model.get("y") ,2e-10) |
---|
1190 | N.testing.assert_almost_equal(model.get("z") ,0.000000000) |
---|
1191 | |
---|
1192 | @testattr(stddist_base = True) |
---|
1193 | def test_nonlinear_6(self): |
---|
1194 | |
---|
1195 | model = load_fmu("Singular_NonLinear5.fmu") |
---|
1196 | |
---|
1197 | model.initialize() |
---|
1198 | |
---|
1199 | N.testing.assert_almost_equal(model.get("x") ,5) |
---|
1200 | N.testing.assert_almost_equal(model.get("y") ,0) |
---|
1201 | N.testing.assert_almost_equal(model.get("z") ,0.000000000) |
---|
1202 | |
---|
1203 | @testattr(stddist_base = True) |
---|
1204 | def test_nonlinear_5(self): |
---|
1205 | model = load_fmu("Singular_NonLinear4.fmu") |
---|
1206 | |
---|
1207 | model.set("b[3]", 0) |
---|
1208 | model.set("a31", 1) |
---|
1209 | model.set("a32", -0.5) |
---|
1210 | |
---|
1211 | res = model.simulate() |
---|
1212 | |
---|
1213 | N.testing.assert_almost_equal(res["z"][0] ,0.000000000) |
---|
1214 | N.testing.assert_almost_equal(res["z"][-1] ,-1.000000000) |
---|
1215 | |
---|
1216 | @testattr(stddist_base = True) |
---|
1217 | def test_no_valid_minimum_norm_sol(self): |
---|
1218 | model = load_fmu("Singular_NoMinimumNormSolution.fmu", log_level=3) |
---|
1219 | model.set("_log_level", 3) |
---|
1220 | model.set_log_level(3) |
---|
1221 | nose.tools.assert_raises(FMUException, model.initialize) |
---|
1222 | |
---|
1223 | @testattr(stddist_base = True) |
---|
1224 | def test_zero_column_jacobian(self): |
---|
1225 | model = load_fmu("Singular_ZeroColumnJacobian.fmu"); |
---|
1226 | res = model.simulate(); |
---|
1227 | N.testing.assert_almost_equal(res["x"][0], -1) |
---|
1228 | |
---|
1229 | @testattr(stddist_base = True) |
---|
1230 | def test_zero_column_jacobian2(self): |
---|
1231 | model = load_fmu("Singular_ZeroColumnJacobian2.fmu"); |
---|
1232 | res = model.simulate(); |
---|
1233 | N.testing.assert_almost_equal(res["x"][0], -1) |
---|
1234 | |
---|
1235 | class Test_FMI_ODE_CS_2: |
---|
1236 | @classmethod |
---|
1237 | def setUpClass(cls): |
---|
1238 | """ |
---|
1239 | Compile the test model. |
---|
1240 | """ |
---|
1241 | file_name = os.path.join(get_files_path(), 'Modelica', 'noState.mo') |
---|
1242 | file_name_in = os.path.join(get_files_path(), 'Modelica', 'InputTests.mo') |
---|
1243 | file_name_linear = os.path.join(get_files_path(), 'Modelica', 'Linear.mo') |
---|
1244 | file_name_time_event = os.path.join(get_files_path(), 'Modelica', 'TimeEvents.mo') |
---|
1245 | |
---|
1246 | _in3_name = compile_fmu("LinearTest.Linear1", file_name_linear, version=2.0, target="cs") |
---|
1247 | _t1_name = compile_fmu("TimeEvents.Basic5", file_name_time_event, version=2.0, target="cs") |
---|
1248 | _t1_name = compile_fmu("TimeEvents.Advanced5", file_name_time_event, version=2.0, target="cs") |
---|
1249 | compile_fmu("Inputs.PlantDiscreteInputs", file_name_in, version=2.0, target="cs") |
---|
1250 | |
---|
1251 | @testattr(stddist_base = True) |
---|
1252 | def test_changing_discrete_inputs(self): |
---|
1253 | model = load_fmu("Inputs_PlantDiscreteInputs.fmu") |
---|
1254 | opts = model.simulate_options() |
---|
1255 | |
---|
1256 | def step(time): |
---|
1257 | "A step function that goes from 1 to 0 at time=0.28" |
---|
1258 | return time < 0.28 |
---|
1259 | |
---|
1260 | input = ('onSwitch', step) |
---|
1261 | res = model.simulate(final_time=0.8, options=opts, input=input) |
---|
1262 | N.testing.assert_almost_equal(res.final("T"), 11.22494, 2) |
---|
1263 | |
---|
1264 | @testattr(stddist_base = True) |
---|
1265 | def test_changing_discrete_inputs_many_times(self): |
---|
1266 | model = load_fmu("Inputs_PlantDiscreteInputs.fmu") |
---|
1267 | opts = model.simulate_options() |
---|
1268 | |
---|
1269 | def step(time): |
---|
1270 | """ |
---|
1271 | A step function that goes from 1 to 0 at time=0.28 |
---|
1272 | and from 0 to 1 at time=0.55 |
---|
1273 | """ |
---|
1274 | return time < 0.28 or time > 0.55 |
---|
1275 | |
---|
1276 | input = ('onSwitch', step) |
---|
1277 | res = model.simulate(final_time=0.8, options=opts, input=input) |
---|
1278 | N.testing.assert_almost_equal(res.final("T"), 20.67587, 2) |
---|
1279 | |
---|
1280 | @testattr(stddist_full = True) |
---|
1281 | def test_updated_values_in_result(self): |
---|
1282 | model = load_fmu("LinearTest_Linear1.fmu") |
---|
1283 | opts = model.simulate_options() |
---|
1284 | |
---|
1285 | res = model.simulate(final_time=1,options=opts) |
---|
1286 | |
---|
1287 | for i in range(len(res["der(x)"])): |
---|
1288 | assert res["der(x)"][i] == 0.0 |
---|
1289 | |
---|
1290 | @testattr(stddist_full = True) |
---|
1291 | def test_time_event_basic_5(self): |
---|
1292 | model = load_fmu("TimeEvents_Basic5.fmu") |
---|
1293 | opts = model.simulate_options() |
---|
1294 | |
---|
1295 | res = model.simulate(final_time=1,options=opts) |
---|
1296 | |
---|
1297 | assert res["der(x)"][-1] == -1.0 |
---|
1298 | |
---|
1299 | @testattr(stddist_base = True) |
---|
1300 | def test_time_event_at_do_step_end(self): |
---|
1301 | model = load_fmu("TimeEvents_Advanced5.fmu") |
---|
1302 | opts = model.simulate_options() |
---|
1303 | opts["ncp"] = 100 |
---|
1304 | res = model.simulate(final_time=1,options=opts) |
---|
1305 | |
---|
1306 | N.testing.assert_almost_equal(res.final("x"), 3.89, 2) |
---|
1307 | |
---|
1308 | class Test_FMI_ODE_CS: |
---|
1309 | @classmethod |
---|
1310 | def setUpClass(cls): |
---|
1311 | """ |
---|
1312 | Compile the test model. |
---|
1313 | """ |
---|
1314 | file_name = os.path.join(get_files_path(), 'Modelica', 'noState.mo') |
---|
1315 | file_name_in = os.path.join(get_files_path(), 'Modelica', 'InputTests.mo') |
---|
1316 | file_name_linear = os.path.join(get_files_path(), 'Modelica', 'Linear.mo') |
---|
1317 | file_name_time_event = os.path.join(get_files_path(), 'Modelica', 'TimeEvents.mo') |
---|
1318 | |
---|
1319 | _in3_name = compile_fmu("LinearTest.Linear1", file_name_linear, target="cs", version=1.0) |
---|
1320 | _t1_name = compile_fmu("TimeEvents.Advanced5", file_name_time_event, target="cs", version=1.0) |
---|
1321 | _cc_name = compile_fmu("Modelica.Mechanics.Rotational.Examples.CoupledClutches", target="cs", version=1.0) |
---|
1322 | |
---|
1323 | @testattr(stddist_full = True) |
---|
1324 | def test_time_out(self): |
---|
1325 | model = load_fmu("Modelica_Mechanics_Rotational_Examples_CoupledClutches.fmu") |
---|
1326 | |
---|
1327 | res = model.simulate() #Verify that it works to simulate |
---|
1328 | model.reset() |
---|
1329 | |
---|
1330 | opts = model.simulate_options() |
---|
1331 | opts["time_limit"] = 0.001 |
---|
1332 | |
---|
1333 | nose.tools.assert_raises(TimeLimitExceeded, model.simulate, options=opts) |
---|
1334 | model.reset() |
---|
1335 | |
---|
1336 | opts["time_limit"] = 10 |
---|
1337 | res = model.simulate() #Verify that it works with a high time out |
---|
1338 | |
---|
1339 | |
---|
1340 | @testattr(stddist_base = True) |
---|
1341 | def test_time_event_at_do_step_end(self): |
---|
1342 | model = load_fmu("TimeEvents_Advanced5.fmu") |
---|
1343 | opts = model.simulate_options() |
---|
1344 | opts["ncp"] = 100 |
---|
1345 | res = model.simulate(final_time=1,options=opts) |
---|
1346 | |
---|
1347 | N.testing.assert_almost_equal(res.final("x"), 3.89, 2) |
---|
1348 | |
---|
1349 | @testattr(stddist_full = True) |
---|
1350 | def test_updated_values_in_result(self): |
---|
1351 | model = load_fmu("LinearTest_Linear1.fmu") |
---|
1352 | opts = model.simulate_options() |
---|
1353 | |
---|
1354 | res = model.simulate(final_time=1,options=opts) |
---|
1355 | |
---|
1356 | for i in range(len(res["der(x)"])): |
---|
1357 | assert res["der(x)"][i] == 0.0 |
---|
1358 | |
---|
1359 | |
---|
1360 | class Test_FMI_ODE_2: |
---|
1361 | """ |
---|
1362 | This class tests pyfmi.simulation.assimulo.FMIODE and together |
---|
1363 | with Assimulo. Requires that Assimulo is installed. |
---|
1364 | """ |
---|
1365 | |
---|
1366 | @classmethod |
---|
1367 | def setUpClass(cls): |
---|
1368 | """ |
---|
1369 | Compile the test model. |
---|
1370 | """ |
---|
1371 | file_name = os.path.join(get_files_path(), 'Modelica', 'noState.mo') |
---|
1372 | file_name_in = os.path.join(get_files_path(), 'Modelica', 'InputTests.mo') |
---|
1373 | file_name_linear = os.path.join(get_files_path(), 'Modelica', 'Linear.mo') |
---|
1374 | |
---|
1375 | _ex1_name = compile_fmu("NoState.Example1", file_name, version=2.0) |
---|
1376 | _ex2_name = compile_fmu("NoState.Example2", file_name, version=2.0) |
---|
1377 | _in1_name = compile_fmu("Inputs.SimpleInput", file_name_in, version=2.0) |
---|
1378 | _in_disc_name = compile_fmu("Inputs.PlantDiscreteInputs", file_name_in, version=2.0) |
---|
1379 | _cc_name = compile_fmu("Modelica.Mechanics.Rotational.Examples.CoupledClutches", version=2.0) |
---|
1380 | #_in3_name = compile_fmu("LinearTest.Linear1", file_name_linear) |
---|
1381 | |
---|
1382 | @testattr(stddist_full = True) |
---|
1383 | def test_discrete_input(self): |
---|
1384 | model = load_fmu("Inputs_PlantDiscreteInputs.fmu") |
---|
1385 | |
---|
1386 | input_object = (["Tenv","onSwitch"], N.array([[0.0, 0.0, 0.0], [1.0, 1.0, 1.0]])) |
---|
1387 | |
---|
1388 | res = model.simulate(input=input_object) |
---|
1389 | |
---|
1390 | N.testing.assert_almost_equal(res.final('onSwitch') ,1.000000000) |
---|
1391 | |
---|
1392 | model.reset() |
---|
1393 | |
---|
1394 | input_object = (["Tenv","onSwitch"], N.array([[0.0, 0.0], [1.0, 1.0]])) |
---|
1395 | |
---|
1396 | nose.tools.assert_raises(FMUException, model.simulate, input=input_object) |
---|
1397 | |
---|
1398 | |
---|
1399 | @testattr(stddist_full = True) |
---|
1400 | def test_simple_input(self): |
---|
1401 | model = load_fmu("Inputs_SimpleInput.fmu") |
---|
1402 | |
---|
1403 | input_object = ("u", N.array([[0.0, 0.0], [1.0, 1.0]])) |
---|
1404 | |
---|
1405 | res = model.simulate(input=input_object) |
---|
1406 | |
---|
1407 | N.testing.assert_almost_equal(res.final('y') ,1.000000000) |
---|
1408 | N.testing.assert_almost_equal(res.final('u') ,1.000000000) |
---|
1409 | |
---|
1410 | model.reset() |
---|
1411 | |
---|
1412 | input_object = ("u", N.array([[0.0, 0.0], [1.0, 1.0]])) |
---|
1413 | |
---|
1414 | res = model.simulate(input=input_object) |
---|
1415 | |
---|
1416 | N.testing.assert_almost_equal(res.final('y') ,1.000000000) |
---|
1417 | N.testing.assert_almost_equal(res.final('u') ,1.000000000) |
---|
1418 | |
---|
1419 | @testattr(stddist_full = True) |
---|
1420 | def test_input_values_not_input(self): |
---|
1421 | model = load_fmu("Inputs_SimpleInput.fmu") |
---|
1422 | |
---|
1423 | input_object = ("y", N.array([[0.0, 0.0], [1.0, 1.0]])) |
---|
1424 | |
---|
1425 | nose.tools.assert_raises(FMUException, model.simulate, input=input_object) |
---|
1426 | |
---|
1427 | @testattr(stddist_base = True) |
---|
1428 | def test_cc_with_radau(self): |
---|
1429 | model = load_fmu("Modelica_Mechanics_Rotational_Examples_CoupledClutches.fmu") |
---|
1430 | opts = model.simulate_options() |
---|
1431 | opts["solver"] = "Radau5ODE" |
---|
1432 | |
---|
1433 | res = model.simulate(final_time=1.5,options=opts) |
---|
1434 | |
---|
1435 | assert (N.abs(res.final("J1.w") - 3.2450903041811698)) < 1e-3 |
---|
1436 | |
---|
1437 | @testattr(stddist_base = True) |
---|
1438 | def test_cc_with_sparse(self): |
---|
1439 | |
---|
1440 | model = load_fmu("Modelica_Mechanics_Rotational_Examples_CoupledClutches.fmu") |
---|
1441 | opts = model.simulate_options() |
---|
1442 | opts["solver"] = "CVode" |
---|
1443 | opts["with_jacobian"] = True |
---|
1444 | opts["CVode_options"]["rtol"] = 1e-7 |
---|
1445 | opts["CVode_options"]["linear_solver"] = "SPARSE" |
---|
1446 | |
---|
1447 | res = model.simulate(final_time=1.5,options=opts) |
---|
1448 | |
---|
1449 | assert (N.abs(res.final("J1.w") - 3.2450903041811698)) < 1e-4 |
---|
1450 | |
---|
1451 | @testattr(stddist_base = True) |
---|
1452 | def test_with_jacobian(self): |
---|
1453 | |
---|
1454 | model = load_fmu("Modelica_Mechanics_Rotational_Examples_CoupledClutches.fmu") |
---|
1455 | opts = model.simulate_options() |
---|
1456 | opts["CVode_options"]["rtol"] = 1e-7 |
---|
1457 | assert opts["with_jacobian"] == "Default" |
---|
1458 | |
---|
1459 | res = model.simulate(final_time=1.5,options=opts) |
---|
1460 | print(res.final("J1.w")) |
---|
1461 | assert (N.abs(res.final("J1.w") - 3.2450903041811698)) < 1e-4 |
---|
1462 | assert res.solver.statistics["nfcnjacs"] > 0 |
---|
1463 | |
---|
1464 | opts["with_jacobian"] = True |
---|
1465 | model.reset() |
---|
1466 | |
---|
1467 | res = model.simulate(final_time=1.5,options=opts) |
---|
1468 | print(res.final("J1.w")) |
---|
1469 | assert (N.abs(res.final("J1.w") - 3.2450903041811698)) < 1e-4 |
---|
1470 | assert res.solver.statistics["nfcnjacs"] == 0 |
---|
1471 | |
---|
1472 | opts["CVode_options"]["usejac"] = False |
---|
1473 | model.reset() |
---|
1474 | |
---|
1475 | res = model.simulate(final_time=1.5,options=opts) |
---|
1476 | print(res.final("J1.w")) |
---|
1477 | assert (N.abs(res.final("J1.w") - 3.2450903041811698)) < 1e-4 |
---|
1478 | assert res.solver.statistics["nfcnjacs"] > 0 |
---|
1479 | |
---|
1480 | opts["with_jacobian"] = False |
---|
1481 | model.reset() |
---|
1482 | |
---|
1483 | res = model.simulate(final_time=1.5,options=opts) |
---|
1484 | print(res.final("J1.w")) |
---|
1485 | assert (N.abs(res.final("J1.w") - 3.2450903041811698)) < 1e-4 |
---|
1486 | assert res.solver.statistics["nfcnjacs"] > 0 |
---|
1487 | |
---|
1488 | @testattr(stddist_full = True) |
---|
1489 | def test_no_state1(self): |
---|
1490 | """ |
---|
1491 | Tests simulation when there is no state in the model (Example1). |
---|
1492 | """ |
---|
1493 | model = load_fmu("NoState_Example1.fmu") |
---|
1494 | |
---|
1495 | res = model.simulate(final_time=10) |
---|
1496 | |
---|
1497 | N.testing.assert_almost_equal(res.initial('x') ,1.000000000) |
---|
1498 | N.testing.assert_almost_equal(res.final('x'),-2.000000000) |
---|
1499 | N.testing.assert_almost_equal(res.initial('y') ,-1.000000000) |
---|
1500 | N.testing.assert_almost_equal(res.final('y'),-1.000000000) |
---|
1501 | N.testing.assert_almost_equal(res.initial('z') ,1.000000000) |
---|
1502 | N.testing.assert_almost_equal(res.final('z'),4.000000000) |
---|
1503 | |
---|
1504 | @testattr(stddist_full = True) |
---|
1505 | def test_no_state2(self): |
---|
1506 | """ |
---|
1507 | Tests simulation when there is no state in the model (Example2). |
---|
1508 | """ |
---|
1509 | model = load_fmu("NoState_Example2.fmu") |
---|
1510 | |
---|
1511 | res = model.simulate(final_time=10) |
---|
1512 | |
---|
1513 | N.testing.assert_almost_equal(res.initial('x') ,-1.000000000) |
---|
1514 | N.testing.assert_almost_equal(res.final('x'),-1.000000000) |
---|
1515 | |
---|
1516 | @testattr(stddist_full = True) |
---|
1517 | def test_no_state1_radau(self): |
---|
1518 | """ |
---|
1519 | Tests simulation when there is no state in the model (Example1). |
---|
1520 | """ |
---|
1521 | model = load_fmu("NoState_Example1.fmu") |
---|
1522 | |
---|
1523 | res = model.simulate(final_time=10, options={"solver": "Radau5ODE"}) |
---|
1524 | |
---|
1525 | N.testing.assert_almost_equal(res.initial('x') ,1.000000000) |
---|
1526 | N.testing.assert_almost_equal(res.final('x'),-2.000000000) |
---|
1527 | N.testing.assert_almost_equal(res.initial('y') ,-1.000000000) |
---|
1528 | N.testing.assert_almost_equal(res.final('y'),-1.000000000) |
---|
1529 | N.testing.assert_almost_equal(res.initial('z') ,1.000000000) |
---|
1530 | N.testing.assert_almost_equal(res.final('z'),4.000000000) |
---|
1531 | |
---|
1532 | class Test_FMI_ODE: |
---|
1533 | """ |
---|
1534 | This class tests pyfmi.simulation.assimulo.FMIODE and together |
---|
1535 | with Assimulo. Requires that Assimulo is installed. |
---|
1536 | """ |
---|
1537 | |
---|
1538 | @classmethod |
---|
1539 | def setUpClass(cls): |
---|
1540 | """ |
---|
1541 | Compile the test model. |
---|
1542 | """ |
---|
1543 | file_name = os.path.join(get_files_path(), 'Modelica', 'noState.mo') |
---|
1544 | file_name_in = os.path.join(get_files_path(), 'Modelica', 'InputTests.mo') |
---|
1545 | file_name_linear = os.path.join(get_files_path(), 'Modelica', 'Linear.mo') |
---|
1546 | |
---|
1547 | _ex1_name = compile_fmu("NoState.Example1", file_name, version=1.0) |
---|
1548 | _ex2_name = compile_fmu("NoState.Example2", file_name, version=1.0) |
---|
1549 | _in1_name = compile_fmu("Inputs.SimpleInput", file_name_in, version=1.0) |
---|
1550 | _in3_name = compile_fmu("Inputs.SimpleInput3", file_name_in, version=1.0) |
---|
1551 | _cc_name = compile_fmu("Modelica.Mechanics.Rotational.Examples.CoupledClutches", version=1.0) |
---|
1552 | _in3_name = compile_fmu("LinearTest.Linear1", file_name_linear, version=1.0) |
---|
1553 | |
---|
1554 | def setUp(self): |
---|
1555 | """ |
---|
1556 | Load the test model. |
---|
1557 | """ |
---|
1558 | self._bounce = load_fmu('bouncingBall.fmu',path_to_fmus_me1) |
---|
1559 | self._dq = load_fmu('dq.fmu',path_to_fmus_me1) |
---|
1560 | self._bounce.initialize() |
---|
1561 | self._dq.initialize() |
---|
1562 | self._bounceSim = FMIODE(self._bounce) |
---|
1563 | self._dqSim = FMIODE(self._dq) |
---|
1564 | |
---|
1565 | @testattr(stddist_full = True) |
---|
1566 | def test_max_log_file_size(self): |
---|
1567 | model = load_fmu("Modelica_Mechanics_Rotational_Examples_CoupledClutches.fmu", log_level=5) |
---|
1568 | model.set("_log_level", 5) |
---|
1569 | model.set_max_log_size(10) |
---|
1570 | |
---|
1571 | model.simulate() |
---|
1572 | |
---|
1573 | with open("Modelica_Mechanics_Rotational_Examples_CoupledClutches_log.txt") as f: |
---|
1574 | for line in f: |
---|
1575 | pass |
---|
1576 | assert "The log file has reached its maximum size" in line |
---|
1577 | |
---|
1578 | @testattr(stddist_full = True) |
---|
1579 | def test_updated_values_in_result(self): |
---|
1580 | model = load_fmu("LinearTest_Linear1.fmu") |
---|
1581 | opts = model.simulate_options() |
---|
1582 | opts["solver"] = "CVode" |
---|
1583 | |
---|
1584 | res = model.simulate(final_time=1,options=opts) |
---|
1585 | |
---|
1586 | for i in range(len(res["der(x)"])): |
---|
1587 | assert res["der(x)"][i] == 0.0 |
---|
1588 | |
---|
1589 | @testattr(stddist_base = True) |
---|
1590 | def test_cc_with_cvode(self): |
---|
1591 | model = load_fmu("Modelica_Mechanics_Rotational_Examples_CoupledClutches.fmu") |
---|
1592 | opts = model.simulate_options() |
---|
1593 | opts["solver"] = "CVode" |
---|
1594 | opts["CVode_options"]["rtol"] = 1e-7 |
---|
1595 | |
---|
1596 | res = model.simulate(final_time=1.5,options=opts) |
---|
1597 | |
---|
1598 | assert (N.abs(res.final("J1.w") - 3.2450903041811698)) < 1e-4 |
---|
1599 | |
---|
1600 | @testattr(stddist_full = True) |
---|
1601 | def test_reset_internal_variables(self): |
---|
1602 | model = load_fmu("Inputs_SimpleInput.fmu") |
---|
1603 | |
---|
1604 | model.initialize() |
---|
1605 | |
---|
1606 | model.set("u",2) |
---|
1607 | model.time = 1 |
---|
1608 | assert model.get("u") == 2.0 |
---|
1609 | |
---|
1610 | model.time = 0.5 |
---|
1611 | assert model.get("u") == 2.0 |
---|
1612 | |
---|
1613 | @testattr(stddist_full = True) |
---|
1614 | def test_reset_internal_variables2(self): |
---|
1615 | model = load_fmu("Inputs_SimpleInput3.fmu") |
---|
1616 | |
---|
1617 | model.initialize() |
---|
1618 | |
---|
1619 | model.set("p",2) |
---|
1620 | model.time = 1 |
---|
1621 | assert model.get("p") == 2.0 |
---|
1622 | |
---|
1623 | model.time = 0.5 |
---|
1624 | assert model.get("p") == 2.0 |
---|
1625 | |
---|
1626 | @testattr(stddist_base = True) |
---|
1627 | def test_cc_with_radau(self): |
---|
1628 | model = load_fmu("Modelica_Mechanics_Rotational_Examples_CoupledClutches.fmu") |
---|
1629 | opts = model.simulate_options() |
---|
1630 | opts["solver"] = "Radau5ODE" |
---|
1631 | |
---|
1632 | res = model.simulate(final_time=1.5,options=opts) |
---|
1633 | |
---|
1634 | assert (N.abs(res.final("J1.w") - 3.2450903041811698)) < 1e-3 |
---|
1635 | |
---|
1636 | @testattr(stddist_base = True) |
---|
1637 | def test_cc_with_dopri(self): |
---|
1638 | model = load_fmu("Modelica_Mechanics_Rotational_Examples_CoupledClutches.fmu") |
---|
1639 | opts = model.simulate_options() |
---|
1640 | opts["solver"] = "Dopri5" |
---|
1641 | |
---|
1642 | res = model.simulate(final_time=1.5,options=opts) |
---|
1643 | |
---|
1644 | assert (N.abs(res.final("J1.w") - 3.2450903041811698)) < 1e-3 |
---|
1645 | |
---|
1646 | @testattr(stddist_base = True) |
---|
1647 | def test_cc_with_lsodar(self): |
---|
1648 | model = load_fmu("Modelica_Mechanics_Rotational_Examples_CoupledClutches.fmu") |
---|
1649 | opts = model.simulate_options() |
---|
1650 | opts["solver"] = "LSODAR" |
---|
1651 | opts["LSODAR_options"]["rtol"] = 1e-6 |
---|
1652 | |
---|
1653 | res = model.simulate(final_time=1.5,options=opts) |
---|
1654 | |
---|
1655 | assert (N.abs(res.final("J1.w") - 3.2450903041811698)) < 1e-3 |
---|
1656 | |
---|
1657 | @testattr(stddist_base = True) |
---|
1658 | def test_cc_with_rodas(self): |
---|
1659 | model = load_fmu("Modelica_Mechanics_Rotational_Examples_CoupledClutches.fmu") |
---|
1660 | opts = model.simulate_options() |
---|
1661 | opts["solver"] = "RodasODE" |
---|
1662 | opts["RodasODE_options"]["rtol"] = 1e-6 |
---|
1663 | |
---|
1664 | res = model.simulate(final_time=1.5,options=opts) |
---|
1665 | |
---|
1666 | assert (N.abs(res.final("J1.w") - 3.2450903041811698)) < 1e-3 |
---|
1667 | |
---|
1668 | @testattr(stddist_base = True) |
---|
1669 | def test_cc_with_impliciteuler(self): |
---|
1670 | model = load_fmu("Modelica_Mechanics_Rotational_Examples_CoupledClutches.fmu") |
---|
1671 | opts = model.simulate_options() |
---|
1672 | opts["solver"] = "ImplicitEuler" |
---|
1673 | opts["ImplicitEuler_options"]["rtol"] = 1e-8 |
---|
1674 | opts["ImplicitEuler_options"]["atol"] = 1e-8 |
---|
1675 | opts["ImplicitEuler_options"]["h"] = 0.001 |
---|
1676 | |
---|
1677 | res = model.simulate(final_time=1.5,options=opts) |
---|
1678 | assert (N.abs(res.final("J1.w") - 3.2450903041811698)) < 1e-2 |
---|
1679 | |
---|
1680 | @testattr(stddist_base = True) |
---|
1681 | def test_cc_with_expliciteuler(self): |
---|
1682 | model = load_fmu("Modelica_Mechanics_Rotational_Examples_CoupledClutches.fmu") |
---|
1683 | opts = model.simulate_options() |
---|
1684 | opts["solver"] = "ExplicitEuler" |
---|
1685 | opts["ExplicitEuler_options"]["h"] = 0.001 |
---|
1686 | |
---|
1687 | res = model.simulate(final_time=1.5,options=opts) |
---|
1688 | assert (N.abs(res.final("J1.w") - 3.2450903041811698)) < 1e-2 |
---|
1689 | |
---|
1690 | @testattr(stddist_full = True) |
---|
1691 | def test_no_state1(self): |
---|
1692 | """ |
---|
1693 | Tests simulation when there is no state in the model (Example1). |
---|
1694 | """ |
---|
1695 | model = load_fmu("NoState_Example1.fmu") |
---|
1696 | |
---|
1697 | res = model.simulate(final_time=10) |
---|
1698 | |
---|
1699 | N.testing.assert_almost_equal(res.initial('x') ,1.000000000) |
---|
1700 | N.testing.assert_almost_equal(res.final('x'),-2.000000000) |
---|
1701 | N.testing.assert_almost_equal(res.initial('y') ,-1.000000000) |
---|
1702 | N.testing.assert_almost_equal(res.final('y'),-1.000000000) |
---|
1703 | N.testing.assert_almost_equal(res.initial('z') ,1.000000000) |
---|
1704 | N.testing.assert_almost_equal(res.final('z'),4.000000000) |
---|
1705 | |
---|
1706 | @testattr(stddist_full = True) |
---|
1707 | def test_no_state2(self): |
---|
1708 | """ |
---|
1709 | Tests simulation when there is no state in the model (Example2). |
---|
1710 | """ |
---|
1711 | model = load_fmu("NoState_Example2.fmu") |
---|
1712 | |
---|
1713 | res = model.simulate(final_time=10) |
---|
1714 | |
---|
1715 | N.testing.assert_almost_equal(res.initial('x') ,-1.000000000) |
---|
1716 | N.testing.assert_almost_equal(res.final('x'),-1.000000000) |
---|
1717 | |
---|
1718 | @testattr(stddist_full = True) |
---|
1719 | def test_result_enumeration_2(self): |
---|
1720 | file_name = os.path.join(get_files_path(), 'Modelica', 'Enumerations.mo') |
---|
1721 | |
---|
1722 | enum_name = compile_fmu("Enumerations.Enumeration2", file_name) |
---|
1723 | model = load_fmu(enum_name) |
---|
1724 | |
---|
1725 | opts = model.simulate_options() |
---|
1726 | |
---|
1727 | res = model.simulate(options=opts) |
---|
1728 | assert res["one"][0] == 1 |
---|
1729 | assert res["one"][-1] == 3 |
---|
1730 | assert res["two"][0] == 2 |
---|
1731 | assert res["two"][-1] == 2 |
---|
1732 | assert res["three"][0] == 3 |
---|
1733 | assert res["three"][-1] == 3 |
---|
1734 | |
---|
1735 | model.reset() |
---|
1736 | opts["result_handling"] = "memory" |
---|
1737 | |
---|
1738 | res = model.simulate(options=opts) |
---|
1739 | |
---|
1740 | assert res["one"][0] == 1 |
---|
1741 | assert res["one"][-1] == 3 |
---|
1742 | assert res["two"][0] == 2 |
---|
1743 | assert res["two"][-1] == 2 |
---|
1744 | assert res["three"][0] == 3 |
---|
1745 | assert res["three"][-1] == 3 |
---|
1746 | |
---|
1747 | from pyfmi.common.io import ResultHandlerCSV |
---|
1748 | model.reset() |
---|
1749 | opts["result_handling"] = "custom" |
---|
1750 | opts["result_handler"] = ResultHandlerCSV(model) |
---|
1751 | |
---|
1752 | res = model.simulate(options=opts) |
---|
1753 | |
---|
1754 | assert res["one"][0] == 1 |
---|
1755 | assert res["one"][-1] == 3 |
---|
1756 | assert res["two"][0] == 2 |
---|
1757 | assert res["two"][-1] == 2 |
---|
1758 | assert res["three"][0] == 3 |
---|
1759 | assert res["three"][-1] == 3 |
---|
1760 | |
---|
1761 | @testattr(stddist_full = True) |
---|
1762 | def test_init(self): |
---|
1763 | """ |
---|
1764 | This tests the functionality of the method init. |
---|
1765 | """ |
---|
1766 | assert self._bounceSim._f_nbr == 2 |
---|
1767 | assert self._bounceSim._g_nbr == 1 |
---|
1768 | assert self._bounceSim.state_events == self._bounceSim.g |
---|
1769 | assert self._bounceSim.y0[0] == 1.0 |
---|
1770 | assert self._bounceSim.y0[1] == 0.0 |
---|
1771 | assert self._dqSim._f_nbr == 1 |
---|
1772 | assert self._dqSim._g_nbr == 0 |
---|
1773 | try: |
---|
1774 | self._dqSim.state_events |
---|
1775 | raise FMUException('') |
---|
1776 | except AttributeError: |
---|
1777 | pass |
---|
1778 | |
---|
1779 | #sol = self._bounceSim._sol_real |
---|
1780 | |
---|
1781 | #N.testing.assert_almost_equal(sol[0][0],1.000000000) |
---|
1782 | #N.testing.assert_almost_equal(sol[0][1],0.000000000) |
---|
1783 | #N.testing.assert_almost_equal(sol[0][2],0.000000000) |
---|
1784 | #N.testing.assert_almost_equal(sol[0][3],-9.81000000) |
---|
1785 | |
---|
1786 | @testattr(stddist_full = True) |
---|
1787 | def test_f(self): |
---|
1788 | """ |
---|
1789 | This tests the functionality of the rhs. |
---|
1790 | """ |
---|
1791 | t = 1.0 |
---|
1792 | y = N.array([1.0,1.0]) |
---|
1793 | |
---|
1794 | rhs = self._bounceSim.rhs(t,y) |
---|
1795 | |
---|
1796 | N.testing.assert_almost_equal(rhs[0],1.00000000) |
---|
1797 | N.testing.assert_almost_equal(rhs[1],-9.8100000) |
---|
1798 | |
---|
1799 | |
---|
1800 | @testattr(stddist_full = True) |
---|
1801 | def test_g(self): |
---|
1802 | """ |
---|
1803 | This tests the functionality of the event indicators. |
---|
1804 | """ |
---|
1805 | t = 1.0 |
---|
1806 | y = N.array([1.0,1.0]) |
---|
1807 | |
---|
1808 | event = self._bounceSim.g(t,y,None) |
---|
1809 | |
---|
1810 | N.testing.assert_almost_equal(event[0],1.00000000) |
---|
1811 | |
---|
1812 | y = N.array([0.5,1.0]) |
---|
1813 | event = self._bounceSim.g(t,y,None) |
---|
1814 | |
---|
1815 | N.testing.assert_almost_equal(event[0],0.50000000) |
---|
1816 | |
---|
1817 | |
---|
1818 | @testattr(stddist_full = True) |
---|
1819 | def test_t(self): |
---|
1820 | """ |
---|
1821 | This tests the functionality of the time events. |
---|
1822 | """ |
---|
1823 | t = 1.0 |
---|
1824 | y = N.array([1.0,1.0]) |
---|
1825 | |
---|
1826 | time = self._bounceSim.t(t,y,None) |
---|
1827 | |
---|
1828 | assert time == None |
---|
1829 | #Further testing of the time event function is needed. |
---|
1830 | |
---|
1831 | |
---|
1832 | @testattr(stddist_full = True) |
---|
1833 | def test_handle_event(self): |
---|
1834 | """ |
---|
1835 | This tests the functionality of the method handle_event. |
---|
1836 | """ |
---|
1837 | y = N.array([1.,1.]) |
---|
1838 | self._bounceSim._model.continuous_states = y |
---|
1839 | solver = lambda x:1 |
---|
1840 | solver.rtol = 1.e-4 |
---|
1841 | solver.t = 1.0 |
---|
1842 | solver.y = y |
---|
1843 | solver.y_sol = [y] |
---|
1844 | solver.report_continuously = False |
---|
1845 | |
---|
1846 | self._bounceSim.initialize(solver) |
---|
1847 | self._bounceSim.handle_event(solver, None) |
---|
1848 | |
---|
1849 | N.testing.assert_almost_equal(solver.y[0],1.00000000) |
---|
1850 | N.testing.assert_almost_equal(solver.y[1],-0.70000000) |
---|
1851 | |
---|
1852 | #Further testing of the handle_event function is needed. |
---|
1853 | |
---|
1854 | @testattr(stddist_full = True) |
---|
1855 | def test_completed_step(self): |
---|
1856 | """ |
---|
1857 | This tests the functionality of the method completed_step. |
---|
1858 | """ |
---|
1859 | y = N.array([1.,1.]) |
---|
1860 | solver = lambda x:1 |
---|
1861 | solver.t = 1.0 |
---|
1862 | solver.y = y |
---|
1863 | assert self._bounceSim.step_events(solver) == 0 |
---|
1864 | #Further testing of the completed step function is needed. |
---|
1865 | |
---|
1866 | @testattr(windows_base = True) |
---|
1867 | def test_simulation_completed_step_cvode(self): |
---|
1868 | """ |
---|
1869 | This tests a simulation of a Pendulum with dynamic state selection. |
---|
1870 | """ |
---|
1871 | model = load_fmu('Pendulum_0Dynamic.fmu', path_to_fmus_me1) |
---|
1872 | |
---|
1873 | opts = model.simulate_options() |
---|
1874 | opts["CVode_options"]["atol"] = 1e-8 |
---|
1875 | opts["CVode_options"]["rtol"] = 1e-8 |
---|
1876 | res = model.simulate(final_time=10, options=opts) |
---|
1877 | |
---|
1878 | N.testing.assert_almost_equal(res.initial('x'), 1.000000, 4) |
---|
1879 | N.testing.assert_almost_equal(res.initial('y'), 0.000000, 4) |
---|
1880 | N.testing.assert_almost_equal(res.final('x'), 0.27510283167449501, 4) |
---|
1881 | N.testing.assert_almost_equal(res.final('y'), -0.96141480746068897, 4) |
---|
1882 | |
---|
1883 | @testattr(windows_base = True) |
---|
1884 | def test_simulation_completed_step_radau(self): |
---|
1885 | model = load_fmu('Pendulum_0Dynamic.fmu', path_to_fmus_me1) |
---|
1886 | |
---|
1887 | opts = model.simulate_options() |
---|
1888 | opts["solver"] = "Radau5ODE" |
---|
1889 | res = model.simulate(final_time=10, options=opts) |
---|
1890 | |
---|
1891 | assert N.abs(res.final('y')+0.96069759894208395) < 1e-2 |
---|
1892 | assert N.abs(res.final('x')-0.27759705219420999) < 1e-1 |
---|
1893 | |
---|
1894 | @testattr(windows_base = True) |
---|
1895 | def test_simulation_completed_step_dopri(self): |
---|
1896 | model = load_fmu('Pendulum_0Dynamic.fmu', path_to_fmus_me1) |
---|
1897 | |
---|
1898 | opts = model.simulate_options() |
---|
1899 | opts["solver"] = "Dopri5" |
---|
1900 | res = model.simulate(final_time=10, options=opts) |
---|
1901 | |
---|
1902 | assert N.abs(res.final('y')+0.95766129067717698) < 1e-1 |
---|
1903 | assert N.abs(res.final('x')-0.28789729477457998) < 1e-1 |
---|
1904 | |
---|
1905 | @testattr(windows_base = True) |
---|
1906 | def test_simulation_completed_step_rodas(self): |
---|
1907 | model = load_fmu('Pendulum_0Dynamic.fmu', path_to_fmus_me1) |
---|
1908 | |
---|
1909 | opts = model.simulate_options() |
---|
1910 | opts["solver"] = "RodasODE" |
---|
1911 | res = model.simulate(final_time=10, options=opts) |
---|
1912 | |
---|
1913 | assert N.abs(res.final('y')+0.96104146428710602) < 1e-1 |
---|
1914 | assert N.abs(res.final('x')-0.27640424005592701) < 1e-1 |
---|
1915 | |
---|
1916 | @testattr(windows_base = True) |
---|
1917 | def test_simulation_completed_step_lsodar(self): |
---|
1918 | model = load_fmu('Pendulum_0Dynamic.fmu', path_to_fmus_me1) |
---|
1919 | |
---|
1920 | opts = model.simulate_options() |
---|
1921 | opts["solver"] = "LSODAR" |
---|
1922 | res = model.simulate(final_time=10, options=opts) |
---|
1923 | |
---|
1924 | assert N.abs(res.final('y')+0.96311062033198303) < 1e-1 |
---|
1925 | assert N.abs(res.final('x')-0.26910580261997902) < 1e-1 |
---|
1926 | |
---|
1927 | @testattr(windows_base = True) |
---|
1928 | def test_terminate_simulation(self): |
---|
1929 | """ |
---|
1930 | This tests a simulation with an event of terminate simulation. |
---|
1931 | """ |
---|
1932 | # Robot.fmu will not load with Python 3 |
---|
1933 | if python3_flag: |
---|
1934 | model = load_fmu('RobotPy3.fmu', path_to_fmus_me1) |
---|
1935 | else: |
---|
1936 | model = load_fmu('Robot.fmu', path_to_fmus_me1) |
---|
1937 | |
---|
1938 | res = model.simulate(final_time=2.0) |
---|
1939 | solver = res.solver |
---|
1940 | |
---|
1941 | N.testing.assert_almost_equal(solver.t, 1.856045, places=3) |
---|
1942 | |
---|
1943 | @testattr(windows_full = True) |
---|
1944 | def test_typeDefinitions_simulation(self): |
---|
1945 | """ |
---|
1946 | This tests a FMU with typeDefinitions including StringType and BooleanType |
---|
1947 | """ |
---|
1948 | model = load_fmu('Robot3d_0MultiBody.fmu', path_to_fmus_me1) |
---|
1949 | |
---|
1950 | res = model.simulate(final_time=2.0) |
---|
1951 | solver = res.solver |
---|
1952 | |
---|
1953 | N.testing.assert_almost_equal(solver.t, 1.856045, places=3) |
---|
1954 | |
---|
1955 | @testattr(noncompliantfmi = True) |
---|
1956 | def test_assert_raises_sensitivity_parameters(self): |
---|
1957 | """ |
---|
1958 | This tests that an exception is raised if a sensitivity calculation |
---|
1959 | is to be perfomed and the parameters are not contained in the model. |
---|
1960 | """ |
---|
1961 | fmu_name = compile_fmu('EventIter.EventMiddleIter', os.path.join(path_to_mos,'EventIter.mo')) |
---|
1962 | |
---|
1963 | model = load_fmu(fmu_name) |
---|
1964 | opts = model.simulate_options() |
---|
1965 | opts["sensitivities"] = ["hej", "hopp"] |
---|
1966 | |
---|
1967 | nose.tools.assert_raises(FMUException,model.simulate,0,1,(),'AssimuloFMIAlg',opts) |
---|
1968 | |
---|
1969 | @testattr(windows_full = True) |
---|
1970 | def test_assert_raises_sensitivity_without_jmodelica(self): |
---|
1971 | model = load_fmu("CoupledClutches_Mod_Generation_Tool.fmu", path_to_fmus_me1) |
---|
1972 | opts = model.simulate_options() |
---|
1973 | opts["sensitivities"] = ["J1.w"] |
---|
1974 | |
---|
1975 | nose.tools.assert_raises(FMUException,model.simulate,0,1,(),'AssimuloFMIAlg',opts) |
---|
1976 | |
---|
1977 | @testattr(stddist_full = True) |
---|
1978 | def test_event_iteration(self): |
---|
1979 | """ |
---|
1980 | This tests FMUs with event iteration (JModelica.org). |
---|
1981 | """ |
---|
1982 | fmu_name = compile_fmu('EventIter.EventMiddleIter', os.path.join(path_to_mos,'EventIter.mo')) |
---|
1983 | |
---|
1984 | model = load_fmu(fmu_name) |
---|
1985 | |
---|
1986 | sim_res = model.simulate(final_time=10) |
---|
1987 | |
---|
1988 | N.testing.assert_almost_equal(sim_res.initial('x'), 2.00000, 4) |
---|
1989 | N.testing.assert_almost_equal(sim_res.final('x'), 10.000000, 4) |
---|
1990 | N.testing.assert_almost_equal(sim_res.final('y'), 3.0000000, 4) |
---|
1991 | N.testing.assert_almost_equal(sim_res.final('z'), 2.0000000, 4) |
---|
1992 | |
---|
1993 | fmu_name = compile_fmu('EventIter.EventStartIter', os.path.join(path_to_mos,'EventIter.mo')) |
---|
1994 | |
---|
1995 | model = load_fmu(fmu_name) |
---|
1996 | |
---|
1997 | sim_res = model.simulate(final_time=10) |
---|
1998 | |
---|
1999 | N.testing.assert_almost_equal(sim_res.initial('x'), 1.00000, 4) |
---|
2000 | N.testing.assert_almost_equal(sim_res.initial('y'), -1.00000, 4) |
---|
2001 | N.testing.assert_almost_equal(sim_res.initial('z'), 1.00000, 4) |
---|
2002 | N.testing.assert_almost_equal(sim_res.final('x'), -2.000000, 4) |
---|
2003 | N.testing.assert_almost_equal(sim_res.final('y'), -1.0000000, 4) |
---|
2004 | N.testing.assert_almost_equal(sim_res.final('z'), 4.0000000, 4) |
---|
2005 | |
---|
2006 | @testattr(stddist_base = True) |
---|
2007 | def test_changed_starttime(self): |
---|
2008 | """ |
---|
2009 | This tests a simulation with different start time. |
---|
2010 | """ |
---|
2011 | bounce = load_fmu('bouncingBall.fmu', path_to_fmus_me1) |
---|
2012 | #bounce.initialize() |
---|
2013 | opts = bounce.simulate_options() |
---|
2014 | opts["CVode_options"]["rtol"] = 1e-4 |
---|
2015 | opts["CVode_options"]["atol"] = 1e-6 |
---|
2016 | res = bounce.simulate(start_time=2.,final_time=5.,options=opts) |
---|
2017 | |
---|
2018 | N.testing.assert_almost_equal(res.initial('h'),1.000000,5) |
---|
2019 | N.testing.assert_almost_equal(res.final('h'),-0.98048862,4) |
---|
2020 | N.testing.assert_almost_equal(res.final('time'),5.000000,5) |
---|
2021 | |
---|
2022 | |
---|
2023 | @testattr(stddist_base = True) |
---|
2024 | def test_basic_simulation(self): |
---|
2025 | """ |
---|
2026 | This tests the basic simulation and writing. |
---|
2027 | """ |
---|
2028 | #Writing continuous |
---|
2029 | bounce = load_fmu('bouncingBall.fmu', path_to_fmus_me1) |
---|
2030 | #bounce.initialize() |
---|
2031 | opts = bounce.simulate_options() |
---|
2032 | opts["CVode_options"]["rtol"] = 1e-4 |
---|
2033 | opts["CVode_options"]["atol"] = 1e-6 |
---|
2034 | res = bounce.simulate(final_time=3., options=opts) |
---|
2035 | |
---|
2036 | N.testing.assert_almost_equal(res.initial('h'),1.000000,5) |
---|
2037 | N.testing.assert_almost_equal(res.final('h'),-0.9804523,5) |
---|
2038 | N.testing.assert_almost_equal(res.final('time'),3.000000,5) |
---|
2039 | |
---|
2040 | #Writing after |
---|
2041 | bounce = load_fmu('bouncingBall.fmu', path_to_fmus_me1) |
---|
2042 | bounce.initialize() |
---|
2043 | opt = bounce.simulate_options() |
---|
2044 | opt['initialize']=False |
---|
2045 | opt["CVode_options"]["rtol"] = 1e-4 |
---|
2046 | opt["CVode_options"]["atol"] = 1e-6 |
---|
2047 | res = bounce.simulate(final_time=3., options=opt) |
---|
2048 | |
---|
2049 | N.testing.assert_almost_equal(res.initial('h'),1.000000,5) |
---|
2050 | N.testing.assert_almost_equal(res.final('h'),-0.9804523,5) |
---|
2051 | N.testing.assert_almost_equal(res.final('time'),3.000000,5) |
---|
2052 | |
---|
2053 | #Test with predefined FMUModel |
---|
2054 | model = load_fmu(os.path.join(path_to_fmus_me1,'bouncingBall.fmu')) |
---|
2055 | #model.initialize() |
---|
2056 | res = model.simulate(final_time=3.,options=opts) |
---|
2057 | |
---|
2058 | N.testing.assert_almost_equal(res.initial('h'),1.000000,5) |
---|
2059 | N.testing.assert_almost_equal(res.final('h'),-0.9804523,5) |
---|
2060 | N.testing.assert_almost_equal(res.final('time'),3.000000,5) |
---|
2061 | |
---|
2062 | |
---|
2063 | @testattr(stddist_base = True) |
---|
2064 | def test_default_simulation(self): |
---|
2065 | """ |
---|
2066 | This test the default values of the simulation using simulate. |
---|
2067 | """ |
---|
2068 | #Writing continuous |
---|
2069 | bounce = load_fmu('bouncingBall.fmu', path_to_fmus_me1) |
---|
2070 | opts = bounce.simulate_options() |
---|
2071 | opts["CVode_options"]["rtol"] = 1e-4 |
---|
2072 | opts["CVode_options"]["atol"] = 1e-6 |
---|
2073 | res = bounce.simulate(final_time=3., options=opts) |
---|
2074 | |
---|
2075 | N.testing.assert_almost_equal(res.solver.rtol, 1e-4, 6) |
---|
2076 | assert res.solver.iter == 'Newton' |
---|
2077 | |
---|
2078 | N.testing.assert_almost_equal(res.initial('h'),1.000000,5) |
---|
2079 | N.testing.assert_almost_equal(res.final('h'),-0.9804523,5) |
---|
2080 | N.testing.assert_almost_equal(res.final('time'),3.000000,5) |
---|
2081 | |
---|
2082 | #Writing continuous |
---|
2083 | bounce = load_fmu('bouncingBall.fmu', path_to_fmus_me1) |
---|
2084 | #bounce.initialize(options={'initialize':False}) |
---|
2085 | res = bounce.simulate(final_time=3., |
---|
2086 | options={'initialize':True,'CVode_options':{'iter':'FixedPoint','rtol':1e-6,'atol':1e-6}}) |
---|
2087 | |
---|
2088 | N.testing.assert_almost_equal(res.solver.rtol, 0.00000100, 7) |
---|
2089 | assert res.solver.iter == 'FixedPoint' |
---|
2090 | |
---|
2091 | N.testing.assert_almost_equal(res.initial('h'),1.000000,5) |
---|
2092 | N.testing.assert_almost_equal(res.final('h'),-0.98018113,5) |
---|
2093 | N.testing.assert_almost_equal(res.final('time'),3.000000,5) |
---|
2094 | |
---|
2095 | @testattr(stddist_base = True) |
---|
2096 | def test_reset(self): |
---|
2097 | """ |
---|
2098 | Test resetting an FMU. (Multiple instances is NOT supported on Dymola |
---|
2099 | FMUs) |
---|
2100 | """ |
---|
2101 | #Writing continuous |
---|
2102 | bounce = load_fmu('bouncingBall.fmu', path_to_fmus_me1) |
---|
2103 | opts = bounce.simulate_options() |
---|
2104 | opts["CVode_options"]["rtol"] = 1e-4 |
---|
2105 | opts["CVode_options"]["atol"] = 1e-6 |
---|
2106 | #bounce.initialize() |
---|
2107 | res = bounce.simulate(final_time=3., options=opts) |
---|
2108 | |
---|
2109 | N.testing.assert_almost_equal(res.initial('h'),1.000000,5) |
---|
2110 | N.testing.assert_almost_equal(res.final('h'),-0.9804523,5) |
---|
2111 | |
---|
2112 | bounce.reset() |
---|
2113 | #bounce.initialize() |
---|
2114 | |
---|
2115 | N.testing.assert_almost_equal(bounce.get('h'), 1.00000,5) |
---|
2116 | |
---|
2117 | res = bounce.simulate(final_time=3.,options=opts) |
---|
2118 | |
---|
2119 | N.testing.assert_almost_equal(res.initial('h'),1.000000,5) |
---|
2120 | N.testing.assert_almost_equal(res.final('h'),-0.9804523,5) |
---|
2121 | |
---|