1 | #!/usr/bin/env python |
---|
2 | # -*- coding: utf-8 -*- |
---|
3 | |
---|
4 | # Copyright (C) 2017 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 | |
---|
18 | """ |
---|
19 | Module containing the tests for the coupled FMI classes. |
---|
20 | """ |
---|
21 | |
---|
22 | import nose |
---|
23 | import os |
---|
24 | import numpy as N |
---|
25 | import sys as S |
---|
26 | |
---|
27 | from tests_jmodelica import testattr, get_files_path |
---|
28 | from pymodelica.compiler import compile_fmu |
---|
29 | from pyfmi import CoupledFMUModelME2 |
---|
30 | from pyfmi import load_fmu |
---|
31 | import pyfmi.fmi as fmi |
---|
32 | |
---|
33 | path_to_fmus = os.path.join(get_files_path(), 'FMUs') |
---|
34 | path_to_mofiles = os.path.join(get_files_path(), 'Modelica') |
---|
35 | |
---|
36 | path_to_fmus_me2 = os.path.join(path_to_fmus,"ME2.0") |
---|
37 | path_to_fmus_cs2 = os.path.join(path_to_fmus,"CS2.0") |
---|
38 | |
---|
39 | |
---|
40 | |
---|
41 | class Test_CoupledFMUModelME2: |
---|
42 | """ |
---|
43 | This class tests pyfmi.fmi_coupled.CoupledFMUModelME2 |
---|
44 | """ |
---|
45 | |
---|
46 | @classmethod |
---|
47 | def setUpClass(cls): |
---|
48 | """ |
---|
49 | Sets up the test class. |
---|
50 | """ |
---|
51 | cls.cc_name = compile_fmu("Modelica.Mechanics.Rotational.Examples.CoupledClutches", version=2.0) |
---|
52 | |
---|
53 | cls.ls_event_full = compile_fmu("LinearStability.FullSystemWithEvents" , os.path.join(path_to_mofiles,"CoupledME.mo"), version=2.0) |
---|
54 | cls.ls_event_sub1 = compile_fmu("LinearStability.SubSystemWithEvents1" , os.path.join(path_to_mofiles,"CoupledME.mo"), version=2.0) |
---|
55 | cls.ls_event_sub2 = compile_fmu("LinearStability.SubSystemWithEvents2" , os.path.join(path_to_mofiles,"CoupledME.mo"), version=2.0) |
---|
56 | |
---|
57 | cls.ls_event_full_v2 = compile_fmu("LinearStability.FullSystemWithEvents_v2" , os.path.join(path_to_mofiles,"CoupledME.mo"), version=2.0) |
---|
58 | cls.ls_event_sub1_v2 = compile_fmu("LinearStability.SubSystemWithEvents1_v2" , os.path.join(path_to_mofiles,"CoupledME.mo"), version=2.0) |
---|
59 | cls.ls_event_sub2_v2 = compile_fmu("LinearStability.SubSystemWithEvents2_v2" , os.path.join(path_to_mofiles,"CoupledME.mo"), version=2.0) |
---|
60 | |
---|
61 | cls.qc_full = compile_fmu("QuarterCar.QuarterCarComplete", os.path.join(path_to_mofiles,"CoupledME.mo"), version="2.0") |
---|
62 | cls.qc_sub1 = compile_fmu("QuarterCar.QuarterCarWithoutFeedThrough1", os.path.join(path_to_mofiles,"CoupledME.mo"),version="2.0") |
---|
63 | cls.qc_sub2 = compile_fmu("QuarterCar.QuarterCarWithoutFeedThrough2", os.path.join(path_to_mofiles,"CoupledME.mo"),version="2.0") |
---|
64 | |
---|
65 | cls.piplant = compile_fmu("PIPlant", os.path.join(path_to_mofiles,"CoupledME.mo"), version="2.0") |
---|
66 | cls.pi = compile_fmu("PI", os.path.join(path_to_mofiles,"CoupledME.mo"), version="2.0") |
---|
67 | cls.plant = compile_fmu("Plant", os.path.join(path_to_mofiles,"CoupledME.mo"), version="2.0") |
---|
68 | |
---|
69 | @testattr(stddist_full = True) |
---|
70 | def test_basic_simulation(self): |
---|
71 | |
---|
72 | model_cc_1 = load_fmu(Test_CoupledFMUModelME2.cc_name) |
---|
73 | model_cc_2 = load_fmu(Test_CoupledFMUModelME2.cc_name) |
---|
74 | |
---|
75 | models = [("First", model_cc_1), ("Second", model_cc_2)] |
---|
76 | connections = [] |
---|
77 | |
---|
78 | coupled = CoupledFMUModelME2(models, connections) |
---|
79 | |
---|
80 | res = coupled.simulate() |
---|
81 | |
---|
82 | nose.tools.assert_almost_equal(res.final("time"),1.5) |
---|
83 | nose.tools.assert_almost_equal(res.final("First.J1.w"),res.final("Second.J1.w")) |
---|
84 | nose.tools.assert_almost_equal(res.final("First.J1.w"), 3.2501079, places=2) |
---|
85 | |
---|
86 | coupled.reset() |
---|
87 | |
---|
88 | res = coupled.simulate() |
---|
89 | |
---|
90 | nose.tools.assert_almost_equal(res.final("time"),1.5) |
---|
91 | nose.tools.assert_almost_equal(res.final("First.J1.w"),res.final("Second.J1.w")) |
---|
92 | nose.tools.assert_almost_equal(res.final("First.J1.w"), 3.2501079, places=2) |
---|
93 | |
---|
94 | @testattr(stddist_full = True) |
---|
95 | def test_get_set_real(self): |
---|
96 | |
---|
97 | model_cc_1 = load_fmu(Test_CoupledFMUModelME2.cc_name) |
---|
98 | model_cc_2 = load_fmu(Test_CoupledFMUModelME2.cc_name) |
---|
99 | |
---|
100 | models = [("First", model_cc_1), ("Second", model_cc_2)] |
---|
101 | connections = [] |
---|
102 | |
---|
103 | coupled = CoupledFMUModelME2(models, connections) |
---|
104 | |
---|
105 | nose.tools.assert_raises(fmi.FMUException, coupled.get, "J1.w") |
---|
106 | |
---|
107 | coupled.set("First.J1.w", 3) |
---|
108 | coupled.set("Second.J1.w", 4) |
---|
109 | |
---|
110 | nose.tools.assert_almost_equal(coupled.get("First.J1.w"),3) |
---|
111 | nose.tools.assert_almost_equal(coupled.get("Second.J1.w"),4) |
---|
112 | |
---|
113 | @testattr(stddist_full = True) |
---|
114 | def test_quarter_car(self): |
---|
115 | |
---|
116 | model = load_fmu(Test_CoupledFMUModelME2.qc_full) |
---|
117 | |
---|
118 | opts = model.simulate_options() |
---|
119 | |
---|
120 | opts["CVode_options"]["atol"] = 1e-8 |
---|
121 | opts["CVode_options"]["rtol"] = 1e-8 |
---|
122 | |
---|
123 | res_full = model.simulate(final_time=1, options=opts) |
---|
124 | |
---|
125 | model_chassi = load_fmu(Test_CoupledFMUModelME2.qc_sub1) |
---|
126 | model_wheel = load_fmu(Test_CoupledFMUModelME2.qc_sub2) |
---|
127 | |
---|
128 | models = [("Chassi", model_chassi), ("Wheel", model_wheel)] |
---|
129 | connections = [(model_chassi,"x_chassi",model_wheel,"x_chassi"), |
---|
130 | (model_chassi,"v_chassi",model_wheel,"v_chassi"), |
---|
131 | (model_wheel,"x_wheel",model_chassi,"x_wheel"), |
---|
132 | (model_wheel,"v_wheel",model_chassi,"v_wheel")] |
---|
133 | |
---|
134 | coupled = CoupledFMUModelME2(models, connections) |
---|
135 | |
---|
136 | res = coupled.simulate(final_time=1) |
---|
137 | |
---|
138 | nose.tools.assert_almost_equal(res.final("Wheel.x_wheel"),res_full.final("x_wheel"), places=4) |
---|
139 | nose.tools.assert_almost_equal(res.final("Chassi.x_chassi"),res_full.final("x_chassi"), places=4) |
---|
140 | nose.tools.assert_almost_equal(res.initial("Wheel.x_wheel"),res_full.initial("x_wheel"), places=4) |
---|
141 | nose.tools.assert_almost_equal(res.initial("Chassi.x_chassi"),res_full.initial("x_chassi"), places=4) |
---|
142 | |
---|
143 | nose.tools.assert_almost_equal(res.final("Wheel.v_wheel"),res_full.final("v_wheel"), places=4) |
---|
144 | nose.tools.assert_almost_equal(res.final("Chassi.v_chassi"),res_full.final("v_chassi"), places=4) |
---|
145 | nose.tools.assert_almost_equal(res.initial("Wheel.v_wheel"),res_full.initial("v_wheel"), places=4) |
---|
146 | nose.tools.assert_almost_equal(res.initial("Chassi.v_chassi"),res_full.initial("v_chassi"), places=4) |
---|
147 | |
---|
148 | @testattr(stddist_full = True) |
---|
149 | def test_linear_example_with_time_event(self): |
---|
150 | |
---|
151 | m_full = load_fmu(Test_CoupledFMUModelME2.ls_event_full) |
---|
152 | |
---|
153 | res_full = m_full.simulate(final_time=0.6) |
---|
154 | |
---|
155 | m_primary = load_fmu(Test_CoupledFMUModelME2.ls_event_sub1) |
---|
156 | m_secondary = load_fmu(Test_CoupledFMUModelME2.ls_event_sub2) |
---|
157 | |
---|
158 | models = [("First", m_primary), ("Second", m_secondary)] |
---|
159 | connections = [(m_primary,"y1",m_secondary,"u2"), |
---|
160 | (m_secondary,"y2",m_primary,"u1")] |
---|
161 | |
---|
162 | master = CoupledFMUModelME2(models, connections=connections) |
---|
163 | |
---|
164 | res = master.simulate(final_time=0.6) |
---|
165 | |
---|
166 | nose.tools.assert_almost_equal(res.final("First.x1"),res_full.final("p1.x1"), places=4) |
---|
167 | nose.tools.assert_almost_equal(res.final("Second.x2"),res_full.final("p2.x2"), places=4) |
---|
168 | nose.tools.assert_almost_equal(res.initial("First.x1"),res_full.initial("p1.x1"), places=4) |
---|
169 | nose.tools.assert_almost_equal(res.initial("Second.x2"),res_full.initial("p2.x2"), places=4) |
---|
170 | |
---|
171 | nose.tools.assert_almost_equal(res.final("First.u1"),res_full.final("p1.u1"), places=4) |
---|
172 | nose.tools.assert_almost_equal(res.final("Second.u2"),res_full.final("p2.u2"), places=4) |
---|
173 | nose.tools.assert_almost_equal(res.initial("First.u1"),res_full.initial("p1.u1"), places=4) |
---|
174 | nose.tools.assert_almost_equal(res.initial("Second.u2"),res_full.initial("p2.u2"), places=4) |
---|
175 | |
---|
176 | @testattr(stddist_full = True) |
---|
177 | def test_linear_example_with_time_event_v2(self): |
---|
178 | |
---|
179 | m_full = load_fmu(Test_CoupledFMUModelME2.ls_event_full_v2) |
---|
180 | |
---|
181 | res_full = m_full.simulate(final_time=0.2) |
---|
182 | |
---|
183 | m_primary = load_fmu(Test_CoupledFMUModelME2.ls_event_sub1_v2) |
---|
184 | m_secondary = load_fmu(Test_CoupledFMUModelME2.ls_event_sub2_v2) |
---|
185 | |
---|
186 | models = [("First", m_primary), ("Second", m_secondary)] |
---|
187 | connections = [(m_primary,"y1",m_secondary,"u2"), |
---|
188 | (m_secondary,"y2",m_primary,"u1")] |
---|
189 | |
---|
190 | master = CoupledFMUModelME2(models, connections=connections) |
---|
191 | |
---|
192 | res = master.simulate(final_time=0.2) |
---|
193 | |
---|
194 | nose.tools.assert_almost_equal(res.final("First.x1"),res_full.final("p1.x1"), places=4) |
---|
195 | nose.tools.assert_almost_equal(res.final("Second.x2"),res_full.final("p2.x2"), places=4) |
---|
196 | nose.tools.assert_almost_equal(res.initial("First.x1"),res_full.initial("p1.x1"), places=4) |
---|
197 | nose.tools.assert_almost_equal(res.initial("Second.x2"),res_full.initial("p2.x2"), places=4) |
---|
198 | |
---|
199 | nose.tools.assert_almost_equal(res.final("First.u1"),res_full.final("p1.u1"), places=4) |
---|
200 | nose.tools.assert_almost_equal(res.final("Second.u2"),res_full.final("p2.u2"), places=4) |
---|
201 | nose.tools.assert_almost_equal(res.initial("First.u1"),res_full.initial("p1.u1"), places=4) |
---|
202 | nose.tools.assert_almost_equal(res.initial("Second.u2"),res_full.initial("p2.u2"), places=4) |
---|
203 | |
---|
204 | |
---|
205 | @testattr(stddist_full = True) |
---|
206 | def test_example_with_events(self): |
---|
207 | |
---|
208 | pi = load_fmu(Test_CoupledFMUModelME2.pi) |
---|
209 | plant = load_fmu(Test_CoupledFMUModelME2.plant) |
---|
210 | piplant = load_fmu(Test_CoupledFMUModelME2.piplant) |
---|
211 | |
---|
212 | res_full = piplant.simulate(final_time=4) |
---|
213 | |
---|
214 | connections = [(pi, "loadTorque", plant, "inputTorque"), |
---|
215 | (plant, "speed", pi, "speed")] |
---|
216 | |
---|
217 | coupled_model = CoupledFMUModelME2([("pi",pi),("plant",plant)], connections) |
---|
218 | |
---|
219 | res = coupled_model.simulate(final_time=4) |
---|
220 | |
---|
221 | nose.tools.assert_almost_equal(res.final("plant.speed"),res_full.final("plant.speed"), places=3) |
---|
222 | nose.tools.assert_almost_equal(res.final("plant.inputTorque"),res_full.final("plant.inputTorque"), places=3) |
---|
223 | nose.tools.assert_almost_equal(res.initial("plant.speed"),res_full.initial("plant.speed"), places=3) |
---|
224 | nose.tools.assert_almost_equal(res.initial("plant.inputTorque"),res_full.initial("plant.inputTorque"), places=3) |
---|
225 | |
---|