From 866beed61f24ce181784781518606e1993212ead Mon Sep 17 00:00:00 2001 From: GREENRAT-K405 Date: Mon, 23 Feb 2026 02:17:04 +0530 Subject: [PATCH 1/2] add option to see realtime simulation --- tools/plotu.py | 96 +++++++++++++++++++++++++++++++--------------- tools/plotym.py | 74 +++++++++++++++++++++++++---------- tools/plotymlag.py | 67 ++++++++++++++++++++++++-------- 3 files changed, 169 insertions(+), 68 deletions(-) diff --git a/tools/plotu.py b/tools/plotu.py index 73edc6ec..25a8ab55 100644 --- a/tools/plotu.py +++ b/tools/plotu.py @@ -12,48 +12,82 @@ ut = [] ymt = [] u = concore.initval(init_simtime_u) + +# --- Real-time plotting setup --- +realtime = concore.tryparam('realtime', False) +if realtime: + plt.ion() + fig, axs = plt.subplots(3, 2, figsize=(8, 6)) + lines = [ax.plot([], [])[0] for ax in axs.flat] + ylabels = ['Pw1 (s)', 'Pf1 (Hz)', 'Pw2 (s)', 'Pf2 (Hz)', 'Pw3 (s)', 'Pf3 (Hz)'] + + for ax, ylab in zip(axs.flat, ylabels): + ax.set_ylabel(ylab) + axs[2, 0].set_xlabel('Cycles') + axs[2, 1].set_xlabel('Cycles') + plt.tight_layout() + plt.show(block=False) +# -------------------------------- + while(concore.simtime= 6: + for i in range(6): + lines[i].set_data(range(len(ut)), [x[i].item() for x in ut]) + axs.flat[i].relim() + axs.flat[i].autoscale_view() + fig.canvas.draw() + fig.canvas.flush_events() + # ----------------------------- + logging.info(f"retry={concore.retrycount}") ################# -# plot inputs and outputs -u1 = [x[0].item() for x in ut] -u2 = [x[1].item() for x in ut] -u3 = [x[2].item() for x in ut] -u4 = [x[3].item() for x in ut] -u5 = [x[4].item() for x in ut] -u6 = [x[5].item() for x in ut] - -Nsim = len(u1) -plt.figure() -plt.subplot(321) -plt.plot(range(Nsim), u1) -plt.ylabel('Pw1 (s)') -plt.subplot(322) -plt.plot(range(Nsim), u2) -plt.ylabel('Pf1 (Hz)') -plt.subplot(323) -plt.plot(range(Nsim), u3) -plt.xlabel('Cycles') -plt.ylabel('Pw2 (s)') -plt.subplot(324) -plt.plot(range(Nsim), u4) -plt.ylabel('Pf2 (Hz)') -plt.subplot(325) -plt.plot(range(Nsim), u5) -plt.ylabel('Pw3 (s)') -plt.subplot(326) -plt.plot(range(Nsim), u6) -plt.xlabel('Cycles') -plt.ylabel('Pf3 (Hz)') +# Finalize rendering +if realtime: + plt.ioff() +else: + # Original static plotting logic + u1 = [x[0].item() for x in ut] + u2 = [x[1].item() for x in ut] + u3 = [x[2].item() for x in ut] + u4 = [x[3].item() for x in ut] + u5 = [x[4].item() for x in ut] + u6 = [x[5].item() for x in ut] + + Nsim = len(u1) + plt.figure() + plt.subplot(321) + plt.plot(range(Nsim), u1) + plt.ylabel('Pw1 (s)') + plt.subplot(322) + plt.plot(range(Nsim), u2) + plt.ylabel('Pf1 (Hz)') + plt.subplot(323) + plt.plot(range(Nsim), u3) + plt.xlabel('Cycles') + plt.ylabel('Pw2 (s)') + plt.subplot(324) + plt.plot(range(Nsim), u4) + plt.ylabel('Pf2 (Hz)') + plt.subplot(325) + plt.plot(range(Nsim), u5) + plt.ylabel('Pw3 (s)') + plt.subplot(326) + plt.plot(range(Nsim), u6) + plt.xlabel('Cycles') + plt.ylabel('Pf3 (Hz)') + plt.tight_layout() + +# Save and show for both modes plt.savefig("stim.pdf") -plt.tight_layout() plt.show() diff --git a/tools/plotym.py b/tools/plotym.py index cd9bfae2..fdeac954 100644 --- a/tools/plotym.py +++ b/tools/plotym.py @@ -5,37 +5,69 @@ import time logging.info("plot ym") -concore.delay = 0.005 +concore.delay = 0.02 concore.default_maxtime(150) -init_simtime_u = "[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]" -init_simtime_ym = "[0.0, 0.0, 0.0]" -ut = [] +init_simtime_u = "[0.0, 0.0]" +init_simtime_ym = "[0.0, 0.0]" ymt = [] ym = concore.initval(init_simtime_ym) + +# 1. Fetch 'realtime' parameter passed from terminal (defaults to False) +realtime = concore.tryparam('realtime', False) + +# DEBUG: Check if the parameter was successfully caught +logging.info(f"--- Realtime mode is set to: {realtime} ---") + +# 2. Set up interactive plot before the loop if realtime is True +if realtime: + plt.ion() # Turn on interactive mode + fig, ax1 = plt.subplots(1, 1) + line1, = ax1.plot([], []) + + ax1.set_ylabel('ym') + ax1.legend(['ym'], loc=0) + ax1.set_xlabel('Cycles') + plt.show(block=False) # Ensure it does not block the script + while(concore.simtime 0 and len(ymt[-1]) >= 2: + for i in range(2): + lines[i].set_data(range(len(ymt)), [x[i].item() for x in ymt]) + axs[i].relim() + axs[i].autoscale_view() + fig.canvas.draw() + fig.canvas.flush_events() + # ----------------------------- + logging.info(f"retry={concore.retrycount}") ################# # plot inputs and outputs -ym1 = [x[0].item() for x in ymt] -ym2 = [x[1].item() for x in ymt] -Nsim = len(ym1) - -plt.figure() -plt.subplot(211) -plt.plot(range(Nsim), ym1) -plt.ylabel('MAP (mmHg)') -plt.legend(['MAP'], loc=0) -plt.subplot(212) -plt.plot(range(Nsim), ym2) -plt.xlabel('Cycles '+str(concore.params)) -plt.ylabel('HR (bpm)') -plt.legend(['HR'], loc=0) +if realtime: + plt.ioff() +else: + # Original static plotting logic + ym1 = [x[0].item() for x in ymt] + ym2 = [x[1].item() for x in ymt] + Nsim = len(ym1) + + plt.figure() + plt.subplot(211) + plt.plot(range(Nsim), ym1) + plt.ylabel('MAP (mmHg)') + plt.legend(['MAP'], loc=0) + plt.subplot(212) + plt.plot(range(Nsim), ym2) + plt.xlabel('Cycles '+str(concore.params)) + plt.ylabel('HR (bpm)') + plt.legend(['HR'], loc=0) + plt.tight_layout() + plt.savefig("hrmap.pdf") -plt.show() +plt.show() \ No newline at end of file From c28d6b0a68691e891d25c3fe9852f5bad0348068 Mon Sep 17 00:00:00 2001 From: GREENRAT-K405 Date: Mon, 23 Feb 2026 02:47:52 +0530 Subject: [PATCH 2/2] add sample for realtime simulation --- demo/controller_RT.py | 37 ++++++++ demo/plotym_RT.py | 73 +++++++++++++++ demo/pm_RT.py | 45 +++++++++ demo/sample_RT.graphml | 208 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 363 insertions(+) create mode 100644 demo/controller_RT.py create mode 100644 demo/plotym_RT.py create mode 100644 demo/pm_RT.py create mode 100644 demo/sample_RT.graphml diff --git a/demo/controller_RT.py b/demo/controller_RT.py new file mode 100644 index 00000000..5516fbcc --- /dev/null +++ b/demo/controller_RT.py @@ -0,0 +1,37 @@ +import numpy as np +from ast import literal_eval +import concore + +try: + ysp = literal_eval(open("ysp.txt").read()) +except: + ysp = 3.0 + +def controller(ym): + if ym[0] < ysp: + return 1.01 * ym + else: + return 0.9 * ym + +#//main// +concore.default_maxtime(150) ##maps to-- for i in range(0,150): +concore.delay = 0.02 + +#//initial values-- transforms to string including the simtime as the 0th entry in the list// +# u = np.array([[0.0]]) +# ym = np.array([[0.0]]) +init_simtime_u = "[0.0, 0.0]" +init_simtime_ym = "[0.0, 0.0]" + +u = np.array([concore.initval(init_simtime_u)]).T +while(concore.simtime + + + + + + + + + + + pm:pm_RT.py + + + + + + + + + + + controller:controller_RT.py + + + + + + + + + + + plotym:plotym_RT.py + + + + + + + + + + edge1 + + + + + + + + + + + + edge2 + + + + + + + + + + + + edge3 + + + + + + + + 1771794664219 + + DEL_NODE + WyI5N2M2OTRlZS0zNTAzLTRlZDctOWZhZS0xOGQ3YjgwNzA5ZmYiXQ== + + + ADD_NODE + WyJwbTpwbV9SVC5weSIseyJ3aWR0aCI6MTE4LCJoZWlnaHQiOjYwLCJzaGFwZSI6InJlY3RhbmdsZSIsIm9wYWNpdHkiOjEsImJhY2tncm91bmRDb2xvciI6IiNmZmNjMDAiLCJib3JkZXJDb2xvciI6IiMwMDAiLCJib3JkZXJXaWR0aCI6MX0sIm9yZGluIix7IngiOjExMCwieSI6MTEwfSx7fSwiOTdjNjk0ZWUtMzUwMy00ZWQ3LTlmYWUtMThkN2I4MDcwOWZmIl0= + + a95ae0297e5f0e63bfa65a29c8d14d92 + + + 1771794687823 + + DEL_NODE + WyIxYmUwMDJkYi0wZmMyLTRjM2ItOGNhMS01NGU0YmJiM2ZiY2IiXQ== + + + ADD_NODE + WyJjb250cm9sbGVyOmNvbnRyb2xsZXJfUlQucHkiLHsid2lkdGgiOjIwNiwiaGVpZ2h0Ijo2MCwic2hhcGUiOiJyZWN0YW5nbGUiLCJvcGFjaXR5IjoxLCJiYWNrZ3JvdW5kQ29sb3IiOiIjZmZjYzAwIiwiYm9yZGVyQ29sb3IiOiIjMDAwIiwiYm9yZGVyV2lkdGgiOjF9LCJvcmRpbiIseyJ4Ijo2NzAsInkiOjkwfSx7fSwiMWJlMDAyZGItMGZjMi00YzNiLThjYTEtNTRlNGJiYjNmYmNiIl0= + + 81165b0ec333e71a4ee794f887493323 + + + 1771794690698 + + SET_POS + WyIxYmUwMDJkYi0wZmMyLTRjM2ItOGNhMS01NGU0YmJiM2ZiY2IiLHsieCI6MTEwLCJ5IjoxMTB9LHsieCI6NDkwLCJ5IjoxMTB9XQ== + + + SET_POS + WyIxYmUwMDJkYi0wZmMyLTRjM2ItOGNhMS01NGU0YmJiM2ZiY2IiLHsieCI6NDkwLCJ5IjoxMTB9LHsieCI6MTEwLCJ5IjoxMTB9XQ== + + 2137de444092ba020db5d395a39a3b79 + + + 1771794720693 + + DEL_NODE + WyIyN2M1ZDUyNC1iZGM5LTQ4Y2QtOWFhNi00Y2IxODk0Y2I1ZDciXQ== + + + ADD_NODE + WyJwbG90eW06cGxvdHltX1JULnB5Iix7IndpZHRoIjoxOTYsImhlaWdodCI6NjAsInNoYXBlIjoicmVjdGFuZ2xlIiwib3BhY2l0eSI6MSwiYmFja2dyb3VuZENvbG9yIjoiI2ZmY2MwMCIsImJvcmRlckNvbG9yIjoiIzAwMCIsImJvcmRlcldpZHRoIjoxfSwib3JkaW4iLHsieCI6MzcwLCJ5Ijo0MzB9LHt9LCIyN2M1ZDUyNC1iZGM5LTQ4Y2QtOWFhNi00Y2IxODk0Y2I1ZDciXQ== + + 64c4ee3fac24cbd01e9cf06ea57dd8c2 + + + 1771794723359 + + SET_POS + WyIyN2M1ZDUyNC1iZGM5LTQ4Y2QtOWFhNi00Y2IxODk0Y2I1ZDciLHsieCI6MTEwLCJ5IjoxMTB9LHsieCI6MzcwLCJ5IjozNTB9XQ== + + + SET_POS + WyIyN2M1ZDUyNC1iZGM5LTQ4Y2QtOWFhNi00Y2IxODk0Y2I1ZDciLHsieCI6MzcwLCJ5IjozNTB9LHsieCI6MTEwLCJ5IjoxMTB9XQ== + + b92c36146ebe446c0907df865f83794d + + + 1771794740309 + + DEL_EDGE + WyJkNGRkZDA2Mi00NTUyLTRkNTktOWIzYy0wYzA0ZGU2MGQxOTciXQ== + + + ADD_EDGE + W3sic291cmNlSUQiOiIxYmUwMDJkYi0wZmMyLTRjM2ItOGNhMS01NGU0YmJiM2ZiY2IiLCJ0YXJnZXRJRCI6Ijk3YzY5NGVlLTM1MDMtNGVkNy05ZmFlLTE4ZDdiODA3MDlmZiIsImxhYmVsIjoiZWRnZTEiLCJzdHlsZSI6eyJ0aGlja25lc3MiOjIsImJhY2tncm91bmRDb2xvciI6IiM1NTUiLCJzaGFwZSI6InNvbGlkIn0sImlkIjoiZDRkZGQwNjItNDU1Mi00ZDU5LTliM2MtMGMwNGRlNjBkMTk3In1d + + 8b3c683a137808fb40dc2921313f8115 + + + 1771794751288 + + SET_POS + WyIxYmUwMDJkYi0wZmMyLTRjM2ItOGNhMS01NGU0YmJiM2ZiY2IiLHsieCI6NDkwLCJ5IjoxMTB9LHsieCI6NjMwLCJ5Ijo5MH1d + + + SET_POS + WyIxYmUwMDJkYi0wZmMyLTRjM2ItOGNhMS01NGU0YmJiM2ZiY2IiLHsieCI6NjMwLCJ5Ijo5MH0seyJ4Ijo0OTAsInkiOjExMH1d + + 0ce00744b96d9ae529645d9297b56821 + + + 1771794765726 + + DEL_EDGE + WyIwOGU0NTRhNS1mYjAwLTQ2MDEtOWMxNC00NDA4NGIyMzFiNTciXQ== + + + ADD_EDGE + W3sic291cmNlSUQiOiI5N2M2OTRlZS0zNTAzLTRlZDctOWZhZS0xOGQ3YjgwNzA5ZmYiLCJ0YXJnZXRJRCI6IjI3YzVkNTI0LWJkYzktNDhjZC05YWE2LTRjYjE4OTRjYjVkNyIsImxhYmVsIjoiZWRnZTIiLCJzdHlsZSI6eyJ0aGlja25lc3MiOjIsImJhY2tncm91bmRDb2xvciI6IiM1NTUiLCJzaGFwZSI6InNvbGlkIn0sImlkIjoiMDhlNDU0YTUtZmIwMC00NjAxLTljMTQtNDQwODRiMjMxYjU3In1d + + 901af9eaa14d4bf0fc1ba715fc5ac3d2 + + + 1771794782565 + + DEL_EDGE + WyI3ZmM3YjQxNy01NDBkLTRhMDgtYTc4Ni0wNDkwYjgyMDg4YWUiXQ== + + + ADD_EDGE + W3sic291cmNlSUQiOiIyN2M1ZDUyNC1iZGM5LTQ4Y2QtOWFhNi00Y2IxODk0Y2I1ZDciLCJ0YXJnZXRJRCI6IjFiZTAwMmRiLTBmYzItNGMzYi04Y2ExLTU0ZTRiYmIzZmJjYiIsImxhYmVsIjoiZWRnZTMiLCJzdHlsZSI6eyJ0aGlja25lc3MiOjIsImJhY2tncm91bmRDb2xvciI6IiM1NTUiLCJzaGFwZSI6InNvbGlkIn0sImlkIjoiN2ZjN2I0MTctNTQwZC00YTA4LWE3ODYtMDQ5MGI4MjA4OGFlIn1d + + e157ed01dca772c8461787c1053794ce + + + 1771794788695 + + SET_POS + WyIyN2M1ZDUyNC1iZGM5LTQ4Y2QtOWFhNi00Y2IxODk0Y2I1ZDciLHsieCI6MzcwLCJ5IjozNTB9LHsieCI6MzcwLCJ5Ijo0MzB9XQ== + + + SET_POS + WyIyN2M1ZDUyNC1iZGM5LTQ4Y2QtOWFhNi00Y2IxODk0Y2I1ZDciLHsieCI6MzcwLCJ5Ijo0MzB9LHsieCI6MzcwLCJ5IjozNTB9XQ== + + aebd758397fec30aac0b59fe643a0da2 + + + 1771794795323 + + SET_POS + WyIxYmUwMDJkYi0wZmMyLTRjM2ItOGNhMS01NGU0YmJiM2ZiY2IiLHsieCI6NjMwLCJ5Ijo5MH0seyJ4Ijo2NzAsInkiOjkwfV0= + + + SET_POS + WyIxYmUwMDJkYi0wZmMyLTRjM2ItOGNhMS01NGU0YmJiM2ZiY2IiLHsieCI6NjcwLCJ5Ijo5MH0seyJ4Ijo2MzAsInkiOjkwfV0= + + 20778f5ad2b1c7bd577f09da007bc0e0 + + + \ No newline at end of file