@@ -118,6 +118,36 @@ def MPC(x0, u, y, Pd, X):
118118concore .delay = 0.02
119119init_simtime_u = "[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]"
120120init_simtime_ym = "[0.0, 0.0, 0.0]"
121+
122+ # --- LIVE PLOT SETUP ---
123+ if GENERATE_PLOT == 1 :
124+ plt .ion () # Enable interactive mode
125+
126+ # 1. Setup hrmap figure (Outputs & Setpoints)
127+ fig_ym , (ax_map , ax_hr ) = plt .subplots (2 , 1 )
128+ line_map_m , = ax_map .plot ([], [], label = 'MAPm' )
129+ line_map_sp , = ax_map .plot ([], [], label = 'MAPsp' )
130+ ax_map .set_ylabel ('MAP (mmHg)' )
131+ ax_map .legend (loc = 0 )
132+
133+ line_hr_m , = ax_hr .plot ([], [], label = 'HRm' )
134+ line_hr_sp , = ax_hr .plot ([], [], label = 'HRsp' )
135+ ax_hr .set_xlabel ('Cycles' )
136+ ax_hr .set_ylabel ('HR (bpm)' )
137+ ax_hr .legend (loc = 0 )
138+
139+ # 2. Setup stim figure (Inputs)
140+ fig_u , axs_u = plt .subplots (3 , 2 )
141+ lines_u = []
142+ labels_u = ['Pw1 (s)' , 'Pf1 (Hz)' , 'Pw2 (s)' , 'Pf2 (Hz)' , 'Pw3 (s)' , 'Pf3 (Hz)' ]
143+ for i , ax in enumerate (axs_u .flat ):
144+ line , = ax .plot ([], [])
145+ ax .set_ylabel (labels_u [i ])
146+ if i >= 4 : ax .set_xlabel ('Cycles' )
147+ lines_u .append ((ax , line ))
148+ fig_u .tight_layout ()
149+ # ------------------------
150+
121151u = np .array ([concore .initval (init_simtime_u )]).T
122152wallclock1 = time .perf_counter ()
123153while (concore .simtime < concore .maxtime ):
@@ -129,8 +159,34 @@ def MPC(x0, u, y, Pd, X):
129159 ymt .append (ym )
130160 u , xc , Pd = MPC (xc , u , ym , Pd , X )
131161 #################
162+
163+ # --- LIVE UPDATE ---
164+ if GENERATE_PLOT == 1 :
165+ Nsim = len (ymt )
166+ xdata = range (Nsim )
167+
168+ # Update hrmap figure
169+ line_map_m .set_data (xdata , [x [0 ].item () for x in ymt ])
170+ line_map_sp .set_data (xdata , np .tile (X ['ysp' ][0 ], Nsim ))
171+ line_hr_m .set_data (xdata , [x [1 ].item () for x in ymt ])
172+ line_hr_sp .set_data (xdata , np .tile (X ['ysp' ][1 ], Nsim ))
173+
174+ for ax in (ax_map , ax_hr ):
175+ ax .relim ()
176+ ax .autoscale_view ()
177+
178+ # Update stim figure
179+ for i , (ax , line ) in enumerate (lines_u ):
180+ line .set_data (xdata , [x [i ].item () for x in ut ])
181+ ax .relim ()
182+ ax .autoscale_view ()
183+
184+ plt .pause (0.001 ) # Render updates
185+ # -------------------
186+
132187 print ("ym=" + str (ym )+ " u=" + str (u ));
133188 concore .write (1 ,"u" ,list (u .T [0 ]));
189+
134190wallclock2 = time .perf_counter ()
135191#concore.write(1,"u",init_simtime_u)
136192print ("retry=" + str (concore .retrycount ))
@@ -139,55 +195,8 @@ def MPC(x0, u, y, Pd, X):
139195if GENERATE_PLOT == 0 :
140196 quit ()
141197
142- # plot inputs and outputs
143- ym1 = [x [0 ].item () for x in ymt ]
144- ym2 = [x [1 ].item () for x in ymt ]
145- u1 = [x [0 ].item () for x in ut ]
146- u2 = [x [1 ].item () for x in ut ]
147- u3 = [x [2 ].item () for x in ut ]
148- u4 = [x [3 ].item () for x in ut ]
149- u5 = [x [4 ].item () for x in ut ]
150- u6 = [x [5 ].item () for x in ut ]
151-
152- Nsim = len (ym1 )
153- plt .figure ()
154- plt .subplot (211 )
155- plt .plot (range (Nsim ), ym1 )
156- plt .plot (range (Nsim ), np .tile (X ['ysp' ][0 ], Nsim ))
157- plt .ylabel ('MAP (mmHg)' )
158- plt .legend (['MAPm' , 'MAPsp' ], loc = 0 )
159- plt .subplot (212 )
160- plt .plot (range (Nsim ), ym2 )
161- plt .plot (range (Nsim ), np .tile (X ['ysp' ][1 ], Nsim ))
162- plt .xlabel ('Cycles' )
163- plt .ylabel ('HR (bpm)' )
164- plt .legend (['HRm' , 'HRsp' ], loc = 0 )
165- plt .savefig ("hrmap.pdf" )
166- #plt.tight_layout()
167-
168- plt .figure ()
169- plt .subplot (321 )
170- plt .plot (range (Nsim ), u1 )
171- plt .ylabel ('Pw1 (s)' )
172- plt .subplot (322 )
173- plt .plot (range (Nsim ), u2 )
174- plt .ylabel ('Pf1 (Hz)' )
175- plt .subplot (323 )
176- plt .plot (range (Nsim ), u3 )
177- plt .xlabel ('Cycles' )
178- plt .ylabel ('Pw2 (s)' )
179- plt .subplot (324 )
180- plt .plot (range (Nsim ), u4 )
181- plt .ylabel ('Pf2 (Hz)' )
182- plt .subplot (325 )
183- plt .plot (range (Nsim ), u5 )
184- plt .ylabel ('Pw3 (s)' )
185- plt .subplot (326 )
186- plt .plot (range (Nsim ), u6 )
187- plt .xlabel ('Cycles' )
188- plt .ylabel ('Pf3 (Hz)' )
189- plt .savefig ("stim.pdf" )
190- plt .tight_layout ()
191- plt .show ()
192-
193-
198+ # --- FINAL SAVE & CLEANUP ---
199+ plt .ioff ()
200+ fig_ym .savefig ("hrmap.pdf" )
201+ fig_u .savefig ("stim.pdf" )
202+ plt .show () # Keep plots open at the end
0 commit comments