Skip to content

Commit a18a6b3

Browse files
committed
Add tests for simulate() edge cases
1 parent b221ab3 commit a18a6b3

1 file changed

Lines changed: 37 additions & 6 deletions

File tree

tests/test_ModelicaSystem.py

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -315,32 +315,43 @@ def test_simulate_inputs(self):
315315
model_file.write_text("""
316316
model M_input
317317
Real x(start=0, fixed=true);
318-
input Real u;
318+
input Real u1;
319+
input Real u2;
319320
output Real y;
320321
equation
321-
der(x) = u;
322+
der(x) = u1 + u2;
322323
y = x;
323324
end M_input;
324325
""")
325326
mod = OMPython.ModelicaSystem(model_file.as_posix(), "M_input", raiseerrors=True)
326327

327328
mod.setSimulationOptions("stopTime=1.0")
328329

330+
# integrate zero (no setInputs call) - it should default to None -> 0
331+
assert mod.getInputs() == {
332+
"u1": None,
333+
"u2": None,
334+
}
335+
mod.simulate()
336+
y = mod.getSolutions("y")[0]
337+
assert np.isclose(y[-1], 0.0)
338+
329339
# integrate a constant
330-
mod.setInputs("u=2.5")
340+
mod.setInputs("u1=2.5")
331341
assert mod.getInputs() == {
332-
"u": [
342+
"u1": [
333343
(0.0, 2.5),
334344
(1.0, 2.5),
335345
],
346+
"u2": None,
336347
}
337348
mod.simulate()
338349
y = mod.getSolutions("y")[0]
339350
assert np.isclose(y[-1], 2.5)
340351

341352
# now let's integrate the sum of two ramps
342-
mod.setInputs("u=[(0.0, 0.0), (0.5, 2), (1.0, 0)]")
343-
assert mod.getInputs("u") == [[
353+
mod.setInputs("u1=[(0.0, 0.0), (0.5, 2), (1.0, 0)]")
354+
assert mod.getInputs("u1") == [[
344355
(0.0, 0.0),
345356
(0.5, 2.0),
346357
(1.0, 0.0),
@@ -349,6 +360,26 @@ def test_simulate_inputs(self):
349360
y = mod.getSolutions("y")[0]
350361
assert np.isclose(y[-1], 1.0)
351362

363+
# let's try some edge cases
364+
mod.setInputs("u1=[(-0.5, 0.0), (1.0, 1)]")
365+
# unmatched startTime
366+
with self.assertRaises(OMPython.ModelicaSystemError):
367+
mod.simulate()
368+
# unmatched stopTime
369+
mod.setInputs("u1=[(0.0, 0.0), (0.5, 1)]")
370+
with self.assertRaises(OMPython.ModelicaSystemError):
371+
mod.simulate()
372+
373+
# Let's use both inputs, but each one with different number of of
374+
# samples. This has an effect when generating the csv file.
375+
mod.setInputs([
376+
"u1=[(0.0, 0), (1.0, 1)]",
377+
"u2=[(0.0, 0), (0.25, 0.5), (0.5, 1.0), (1.0, 0)]",
378+
])
379+
mod.simulate()
380+
y = mod.getSolutions("y")[0]
381+
assert np.isclose(y[-1], 1.0)
382+
352383

353384
if __name__ == '__main__':
354385
unittest.main()

0 commit comments

Comments
 (0)