1+
2+ # This code is designed to read data from a pickle file and plot various solutions for a pendulum system.
3+ # It's likely part of a verification process for "Testing solution outputs to manually display graphs of pendulum #813".
4+ # The script handles the extraction and processing of the pendulum system's state (q, qdot) and control (tau) variables.
5+ # These variables are visualized for two degrees of freedom across a specified time interval.
6+ # Matplotlib is used for plotting, with adjustments made for layout to prevent overlapping of the subplots.
7+
8+
9+ import pickle
10+ import matplotlib .pyplot as plt
11+ import matplotlib
12+ import numpy as np
13+
14+ # Set the backend for Matplotlib to 'Qt5Agg'
15+ matplotlib .use ('Qt5Agg' ) # Use 'Qt5Agg' for PyQt5 compatibility, 'Qt6Agg' if using PyQt6
16+
17+ # Load data from the pickle file
18+ with open ("pendulum.pkl" , "rb" ) as file :
19+ data = pickle .load (file )
20+
21+ # Extract and process q, qdot, and tau solutions from the data
22+ # Extracting the first and last elements of each solution for two degrees of freedom (DOF)
23+ q_sol_1 = [point [0 ][0 ] for point in data ["q_sol" ]]
24+ q_sol_2 = [point [- 1 ][- 1 ] for point in data ["q_sol" ]]
25+ qdot_sol_1 = [point [0 ][0 ] for point in data ["qdot_sol" ]]
26+ qdot_sol_2 = [point [- 1 ][- 1 ] for point in data ["qdot_sol" ]]
27+ tau_sol_1 = [point [0 ][0 ] for point in data ["tau_sol" ]]
28+ tau_sol_2 = [point [- 1 ][- 1 ] for point in data ["tau_sol" ]]
29+
30+ # Append the last value to tau solutions to ensure equal length with the time array
31+ tau_sol_1 .append (tau_sol_1 [- 1 ])
32+ tau_sol_2 .append (tau_sol_2 [- 1 ])
33+
34+ # Extract time data and convert each DM (Differential Matrix) to a NumPy array
35+ time = data ["time" ]
36+ numpy_array = np .concatenate ([np .array (t .full ()).flatten ()[:- 1 ] if i < len (time ) - 1 else np .array (t .full ()).flatten () for i , t in enumerate (time )])
37+
38+ # Create a time array between 0 and 1 with 400 intervals (total 401 points)
39+ Time = np .linspace (0 , 1 , 401 )
40+
41+ # Create a figure and a set of subplots
42+ # 3 rows for q, qdot, tau and 2 columns for each DOF
43+ fig , axs = plt .subplots (3 , 2 , figsize = (10 , 15 ))
44+
45+ # Plotting q solutions for both DOFs
46+ axs [0 , 0 ].plot (Time , q_sol_1 )
47+ axs [0 , 0 ].set_title ("q Solution for first DOF" )
48+ axs [0 , 0 ].set_ylabel ("q" )
49+ axs [0 , 0 ].set_xlabel ("Time" )
50+ axs [0 , 0 ].grid (True )
51+
52+ axs [0 , 1 ].plot (Time , q_sol_2 )
53+ axs [0 , 1 ].set_title ("q Solution for second DOF" )
54+ axs [0 , 1 ].set_ylabel ("q" )
55+ axs [0 , 1 ].set_xlabel ("Time" )
56+ axs [0 , 1 ].grid (True )
57+
58+ axs [1 , 0 ].plot (Time , qdot_sol_1 )
59+ axs [1 , 0 ].set_title ("qdot Solution for first DOF" )
60+ axs [1 , 0 ].set_ylabel ("qdot" )
61+ axs [1 , 0 ].set_xlabel ("Time" )
62+ axs [1 , 0 ].grid (True )
63+
64+ axs [1 , 1 ].plot (Time , qdot_sol_2 )
65+ axs [1 , 1 ].set_title ("qdot Solution for second DOF" )
66+ axs [1 , 1 ].set_ylabel ("qdot" )
67+ axs [1 , 1 ].set_xlabel ("Time" )
68+ axs [1 , 1 ].grid (True )
69+
70+ axs [2 , 0 ].plot (Time , tau_sol_1 )
71+ axs [2 , 0 ].set_title ("Tau Solution for first DOF" )
72+ axs [2 , 0 ].set_ylabel ("Tau" )
73+ axs [2 , 0 ].set_xlabel ("Time" )
74+ axs [2 , 0 ].grid (True )
75+
76+ axs [2 , 1 ].plot (Time , tau_sol_2 )
77+ axs [2 , 1 ].set_title ("Tau Solution for second DOF" )
78+ axs [2 , 1 ].set_ylabel ("Tau" )
79+ axs [2 , 1 ].set_xlabel ("Time" )
80+ axs [2 , 1 ].grid (True )
81+
82+ plt .tight_layout ()
83+ plt .subplots_adjust (left = None , bottom = None , right = None , top = None , wspace = None , hspace = 0.3 )
84+ plt .savefig ("pendulum_graph.jpg" )
85+
86+ # Display the plot
87+ plt .show ()
0 commit comments