source: trunk/Python/src/pyjmi/examples/if_example_2.py @ 4648

Last change on this file since 4648 was 4648, checked in by Tove Bergdahl, 7 years ago

In this commit:

  • Changed get_item in JMResultBase so that a vector of same length as time is returned, independent of variability. (#2712)
  • Introduced methods JMResultBase.final and JMResultBase.initial returning the final and initial value resp. of a specific variable in the result. Also modified tests and examples so that they use final and initial instead of raw vector accesses.(#2712)
  • Changed some simulation examples using JMU to use FMU instead. (#2553)
  • Added asserts in some examples that did not have any before. (#2732)

Also, made sure that all examples are in one of the test modules.

File size: 1.7 KB
Line 
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
18import os
19
20import numpy as N
21import pylab as p
22
23from pymodelica import compile_fmu
24from pyfmi import load_fmu
25
26def run_demo(with_plots=True):
27    """
28    This example shows how to simulate a system that contains switches. The
29    example model is simple in the sense that no reinitialization of the
30    variables is needed at the event points.
31    """
32
33    curr_dir = os.path.dirname(os.path.abspath(__file__));
34
35    class_name = 'IfExpExamples.IfExpExample2'
36    mofile = curr_dir+'/files/IfExpExamples.mo'
37
38    fmu_name = compile_fmu(class_name, mofile)
39
40    # Load the dynamic library and XML data
41    model = load_fmu(fmu_name)
42
43    # Simulate
44    res = model.simulate(final_time=5.0)
45   
46    # Get results
47    x = res['x']
48    u = res['u']
49    t = res['time']
50   
51    assert N.abs(res.final('x') - 3.5297217)    < 1e-3
52    assert N.abs(res.final('u') - (-0.2836621)) < 1e-3
53
54    if with_plots:
55        fig = p.figure()
56        p.plot(t, x, t, u)
57        p.legend(('x','u'))
58        p.show()
59
60if __name__== "__main__":
61    run_demo()
62
Note: See TracBrowser for help on using the repository browser.