Ticket #5552: bug_demo.2.py

File bug_demo.2.py, 3.1 KB (added by dafred4991@…, 5 years ago)

a programming error was fixed in the previous file (line 95) and another model was chosen (a version of Modelica.Mechanics.MultiBody.Examples.Rotational3DEffects.GyroscopicEffects)

Line 
1import fmpy
2import re
3import matplotlib.pyplot as plt
4import numpy as np
5
6def 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
20def 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: Gyro3d.fmu '''
26fmu_file = 'Gyro3d.fmu'
27
28model_description = fmpy.read_model_description(fmu_file, validate=False)
29unzip_dir = fmpy.extract(fmu_file)
30
31if model_description.coSimulation is not None:
32 modelIdentifier = model_description.coSimulation.modelIdentifier
33else:
34 modelIdentifier = model_description.modelExchange.modelIdentifier
35
36state_names, var_ref_states = getStates(model_description)
37
38''' set up 2 FMU2Slaves from the same .fmu archive '''
39fmus = []
40
41for 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
48for fmu in fmus:
49 fmu.instantiate(loggingOn=False)
50 fmu.setupExperiment(startTime=0)
51
52 fmu.enterInitializationMode()
53 fmu.exitInitializationMode()
54
55time = 0
56n_steps = 1000
57step_size = 1e-3
58
59saved_states_0 = np.zeros([n_steps, len(state_names)])
60saved_states_1 = saved_states_0.copy()
61
62'''=============================================================================
63simulation loop
64============================================================================='''
65
66for 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
84for fmu in fmus:
85 fmu.terminate()
86 fmu.freeInstance()
87
88fig, axes = plt.subplots(saved_states_0.shape[1], 1)
89
90'''=============================================================================
91visualisation
92============================================================================='''
93
94''' determine differences between the saved states of fmu 0 and fmu 1 '''
95differences = saved_states_0[1:] - saved_states_1[:-1]
96
97for state_id, ax in enumerate(axes):
98 ax.plot(differences[:, state_id])
99 ax.set_ylabel(state_names[state_id])
100
101plt.show()