1 | import fmpy
|
---|
2 | import re
|
---|
3 | import matplotlib.pyplot as plt
|
---|
4 | import numpy as np
|
---|
5 |
|
---|
6 | def getStates(model_description):
|
---|
7 | ''' extracts the continuous state names and their variables references from
|
---|
8 | a model description '''
|
---|
9 |
|
---|
10 | derivative_names = [
|
---|
11 | der.variable.name for der in model_description.derivatives]
|
---|
12 |
|
---|
13 | names = [
|
---|
14 | re.search(r'der\((.*)\)', n).group(1) for n in derivative_names]
|
---|
15 |
|
---|
16 | var_refs = get_var_refs(names, model_description)
|
---|
17 |
|
---|
18 | return (names, var_refs)
|
---|
19 |
|
---|
20 | def get_var_refs(signal_names,
|
---|
21 | model_description):
|
---|
22 | return [var.valueReference for var in model_description.modelVariables
|
---|
23 | if var.name in signal_names]
|
---|
24 |
|
---|
25 | ''' example model: Modelica.Blocks.Examples.PID_Controller '''
|
---|
26 | fmu_file = 'Modelica.Blocks.Examples.PID_Controller.fmu'
|
---|
27 |
|
---|
28 | model_description = fmpy.read_model_description(fmu_file, validate=False)
|
---|
29 | unzip_dir = fmpy.extract(fmu_file)
|
---|
30 |
|
---|
31 | if model_description.coSimulation is not None:
|
---|
32 | modelIdentifier = model_description.coSimulation.modelIdentifier
|
---|
33 | else:
|
---|
34 | modelIdentifier = model_description.modelExchange.modelIdentifier
|
---|
35 |
|
---|
36 | state_names, var_ref_states = getStates(model_description)
|
---|
37 |
|
---|
38 | ''' set up 2 FMU2Slaves from the same .fmu archive '''
|
---|
39 | fmus = []
|
---|
40 |
|
---|
41 | for i in range(2):
|
---|
42 | fmus.append(fmpy.fmi2.FMU2Slave(guid=model_description.guid,
|
---|
43 | unzipDirectory=unzip_dir,
|
---|
44 | modelIdentifier=modelIdentifier,
|
---|
45 | instanceName='i1')
|
---|
46 | )
|
---|
47 |
|
---|
48 | for fmu in fmus:
|
---|
49 | fmu.instantiate(loggingOn=False)
|
---|
50 | fmu.setupExperiment(startTime=0)
|
---|
51 |
|
---|
52 | fmu.enterInitializationMode()
|
---|
53 | fmu.exitInitializationMode()
|
---|
54 |
|
---|
55 | time = 0
|
---|
56 | n_steps = 1000
|
---|
57 | step_size = 1e-3
|
---|
58 |
|
---|
59 | saved_states_0 = np.zeros([n_steps, len(state_names)])
|
---|
60 | saved_states_1 = saved_states_0.copy()
|
---|
61 |
|
---|
62 | '''=============================================================================
|
---|
63 | simulation loop
|
---|
64 | ============================================================================='''
|
---|
65 |
|
---|
66 | for step_id in range(saved_states_0.shape[0]):
|
---|
67 | ''' save the state of fmu 0 '''
|
---|
68 | saved_states_0[step_id, :] = np.array(fmus[0].getReal(var_ref_states))
|
---|
69 |
|
---|
70 | fmus[0].doStep(currentCommunicationPoint=time,
|
---|
71 | communicationStepSize=step_size)
|
---|
72 |
|
---|
73 | ''' set fmu 1 state to the PREVIOUS state of fmu 0 '''
|
---|
74 | fmus[1].setReal(var_ref_states, saved_states_0[step_id, :])
|
---|
75 |
|
---|
76 | fmus[1].doStep(currentCommunicationPoint=time,
|
---|
77 | communicationStepSize=step_size)
|
---|
78 |
|
---|
79 | ''' save the state of fmu 1 '''
|
---|
80 | saved_states_1[step_id, :] = np.array(fmus[1].getReal(var_ref_states))
|
---|
81 |
|
---|
82 | time += step_size
|
---|
83 |
|
---|
84 | for fmu in fmus:
|
---|
85 | fmu.terminate()
|
---|
86 | fmu.freeInstance()
|
---|
87 |
|
---|
88 | fig, axes = plt.subplots(saved_states_0.shape[1], 1)
|
---|
89 |
|
---|
90 | '''=============================================================================
|
---|
91 | visualisation
|
---|
92 | ============================================================================='''
|
---|
93 |
|
---|
94 | ''' determine differences between the saved states of fmu 0 and fmu 1 '''
|
---|
95 | differences = saved_states_0 - saved_states_1
|
---|
96 |
|
---|
97 | for state_id, ax in enumerate(axes):
|
---|
98 | ax.plot(differences[:, state_id])
|
---|
99 | ax.set_ylabel(state_names[state_id])
|
---|
100 |
|
---|
101 | plt.show()
|
---|