Skip to content

Commit 02cf91c

Browse files
authored
Merge pull request #451 from GREENRAT-K405/dev
Add option to see realtime simulation
2 parents f07c437 + c28d6b0 commit 02cf91c

7 files changed

Lines changed: 532 additions & 68 deletions

File tree

demo/controller_RT.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import numpy as np
2+
from ast import literal_eval
3+
import concore
4+
5+
try:
6+
ysp = literal_eval(open("ysp.txt").read())
7+
except:
8+
ysp = 3.0
9+
10+
def controller(ym):
11+
if ym[0] < ysp:
12+
return 1.01 * ym
13+
else:
14+
return 0.9 * ym
15+
16+
#//main//
17+
concore.default_maxtime(150) ##maps to-- for i in range(0,150):
18+
concore.delay = 0.02
19+
20+
#//initial values-- transforms to string including the simtime as the 0th entry in the list//
21+
# u = np.array([[0.0]])
22+
# ym = np.array([[0.0]])
23+
init_simtime_u = "[0.0, 0.0]"
24+
init_simtime_ym = "[0.0, 0.0]"
25+
26+
u = np.array([concore.initval(init_simtime_u)]).T
27+
while(concore.simtime<concore.maxtime):
28+
while concore.unchanged():
29+
ym = concore.read(1,"ym",init_simtime_ym)
30+
ym = np.array([ym]).T
31+
#####
32+
u = controller(ym)
33+
#####
34+
print(str(concore.simtime) + ". u="+str(u) + "ym="+str(ym));
35+
concore.write(1,"u",list(u.T[0]),delta=0)
36+
37+
print("retry="+str(concore.retrycount))

demo/plotym_RT.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import concore
2+
import logging
3+
import numpy as np
4+
import matplotlib.pyplot as plt
5+
import time
6+
logging.info("plot ym")
7+
8+
concore.delay = 0.02
9+
concore.default_maxtime(150)
10+
init_simtime_u = "[0.0, 0.0]"
11+
init_simtime_ym = "[0.0, 0.0]"
12+
ymt = []
13+
ym = concore.initval(init_simtime_ym)
14+
15+
# 1. Fetch 'realtime' parameter passed from terminal (defaults to False)
16+
realtime = concore.tryparam('realtime', False)
17+
18+
# DEBUG: Check if the parameter was successfully caught
19+
logging.info(f"--- Realtime mode is set to: {realtime} ---")
20+
21+
# 2. Set up interactive plot before the loop if realtime is True
22+
if realtime:
23+
plt.ion() # Turn on interactive mode
24+
fig, ax1 = plt.subplots(1, 1)
25+
line1, = ax1.plot([], [])
26+
27+
ax1.set_ylabel('ym')
28+
ax1.legend(['ym'], loc=0)
29+
ax1.set_xlabel('Cycles')
30+
plt.show(block=False) # Ensure it does not block the script
31+
32+
while(concore.simtime<concore.maxtime):
33+
while concore.unchanged():
34+
ym = concore.read(1,"ym",init_simtime_ym)
35+
concore.write(1,"ym",ym)
36+
logging.debug(f" ym={ym}")
37+
ymt.append(np.array(ym).T)
38+
39+
# 3. Update the plot iteratively during the simulation
40+
if realtime:
41+
ym1 = [x[0].item() for x in ymt]
42+
x_data = range(len(ym1))
43+
44+
line1.set_data(x_data, ym1)
45+
46+
ax1.relim()
47+
ax1.autoscale_view()
48+
49+
# Force the GUI to draw the new data and flush UI events
50+
fig.canvas.draw()
51+
fig.canvas.flush_events()
52+
53+
logging.info(f"retry={concore.retrycount}")
54+
55+
#################
56+
57+
# 4. Finalize plotting
58+
if realtime:
59+
plt.ioff() # Turn off interactive mode so the plot stays open at the end
60+
plt.savefig("ym.pdf")
61+
plt.show()
62+
else:
63+
ym1 = [x[0].item() for x in ymt]
64+
65+
Nsim = len(ym1)
66+
plt.figure()
67+
plt.subplot(111)
68+
plt.plot(range(Nsim), ym1)
69+
plt.ylabel('ym')
70+
plt.legend(['ym'], loc=0)
71+
plt.xlabel('Cycles')
72+
plt.savefig("ym.pdf")
73+
plt.show()

demo/pm_RT.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import concore
2+
import numpy as np
3+
import tensorflow as tf
4+
5+
#//pm function//
6+
def pm(u,oldstate):
7+
newresult = u + 0.01
8+
newstate = 0.5*oldstate + 0.5*newresult
9+
return newstate,newresult
10+
11+
pmstate = tf.convert_to_tensor(np.array([[4.0]]))
12+
u = tf.convert_to_tensor(np.array([[0.0]]))
13+
14+
#//main//
15+
16+
concore.default_maxtime(150) ##maps to-- for i in range(0,150):
17+
concore.delay = 0.02
18+
19+
#//initial values-- transforms to string including the simtime as the 0th entry in the list//
20+
# u = np.array([[0.0]])
21+
# ym = np.array([[0.0]])
22+
init_simtime_u = "[0.0, 0.0]"
23+
init_simtime_ym = "[0.0, 0.0]"
24+
25+
ym = np.array([concore.initval(init_simtime_ym)]).T
26+
while(concore.simtime<concore.maxtime):
27+
while concore.unchanged():
28+
u = concore.read(1,"u",init_simtime_u)
29+
30+
### Added lines
31+
u = tf.convert_to_tensor(np.array(u))
32+
pmstate,ym = pm(pmstate,u)
33+
ym = np.array(np.array(ym))
34+
####
35+
36+
print(str(concore.simtime) + ". u="+str(u) + "ym="+str(ym));
37+
concore.write(1,"ym",list(ym.T[0]),delta=1)
38+
39+
print("retry="+str(concore.retrycount))
40+
41+
42+
#// main-- to begin with//
43+
#for i in range(0,150):
44+
# ym = pm(u)
45+
# print('u='+repr(u)+' ym='+repr(ym))

demo/sample_RT.graphml

Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<graphml xmlns="http://graphml.graphdrawing.org/xmlns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns http://www.yworks.com/xml/schema/graphml/1.1/ygraphml.xsd" xmlns:y="http://www.yworks.com/xml/graphml">
3+
<key for="node" id="d6" yfiles.type="nodegraphics"/>
4+
<key for="edge" id="d10" yfiles.type="edgegraphics"/>
5+
<graph edgedefault="directed" id="1771794630414" projectName="sample_RT" authorName="GREENRAT-K405">
6+
<node id="97c694ee-3503-4ed7-9fae-18d7b80709ff">
7+
<data key="d6">
8+
<y:ShapeNode>
9+
<y:Geometry height="60" width="118" x="110" y="110"/>
10+
<y:Fill color="#ffcc00" opacity="1"/>
11+
<y:BorderStyle color="#000" width="1"/>
12+
<y:NodeLabel>pm:pm_RT.py</y:NodeLabel>
13+
<y:Shape type="rectangle"/>
14+
</y:ShapeNode>
15+
</data>
16+
</node>
17+
<node id="1be002db-0fc2-4c3b-8ca1-54e4bbb3fbcb">
18+
<data key="d6">
19+
<y:ShapeNode>
20+
<y:Geometry height="60" width="206" x="670" y="90"/>
21+
<y:Fill color="#ffcc00" opacity="1"/>
22+
<y:BorderStyle color="#000" width="1"/>
23+
<y:NodeLabel>controller:controller_RT.py</y:NodeLabel>
24+
<y:Shape type="rectangle"/>
25+
</y:ShapeNode>
26+
</data>
27+
</node>
28+
<node id="27c5d524-bdc9-48cd-9aa6-4cb1894cb5d7">
29+
<data key="d6">
30+
<y:ShapeNode>
31+
<y:Geometry height="60" width="196" x="370" y="430"/>
32+
<y:Fill color="#ffcc00" opacity="1"/>
33+
<y:BorderStyle color="#000" width="1"/>
34+
<y:NodeLabel>plotym:plotym_RT.py</y:NodeLabel>
35+
<y:Shape type="rectangle"/>
36+
</y:ShapeNode>
37+
</data>
38+
</node>
39+
<edge id="0" source="1be002db-0fc2-4c3b-8ca1-54e4bbb3fbcb" target="97c694ee-3503-4ed7-9fae-18d7b80709ff">
40+
<data key="d10">
41+
<y:GenericEdge configuration="com.yworks.bpmn.Connection">
42+
<y:LineStyle color="#555" width="2" type="solid"/>
43+
<y:Arrows source="none" target="delta"/>
44+
<y:EdgeLabel>edge1</y:EdgeLabel>
45+
<y:Path sx="0.0" sy="0.0" tx="0.0" ty="0.0">
46+
<y:Point x="337.5" y="99.87270471228895"/>
47+
</y:Path>
48+
</y:GenericEdge>
49+
</data>
50+
</edge>
51+
<edge id="1" source="97c694ee-3503-4ed7-9fae-18d7b80709ff" target="27c5d524-bdc9-48cd-9aa6-4cb1894cb5d7">
52+
<data key="d10">
53+
<y:GenericEdge configuration="com.yworks.bpmn.Connection">
54+
<y:LineStyle color="#555" width="2" type="solid"/>
55+
<y:Arrows source="none" target="delta"/>
56+
<y:EdgeLabel>edge2</y:EdgeLabel>
57+
<y:Path sx="0.0" sy="0.0" tx="0.0" ty="0.0">
58+
<y:Point x="252.0133866516665" y="285"/>
59+
</y:Path>
60+
</y:GenericEdge>
61+
</data>
62+
</edge>
63+
<edge id="2" source="27c5d524-bdc9-48cd-9aa6-4cb1894cb5d7" target="1be002db-0fc2-4c3b-8ca1-54e4bbb3fbcb">
64+
<data key="d10">
65+
<y:GenericEdge configuration="com.yworks.bpmn.Connection">
66+
<y:LineStyle color="#555" width="2" type="solid"/>
67+
<y:Arrows source="none" target="delta"/>
68+
<y:EdgeLabel>edge3</y:EdgeLabel>
69+
<y:Path sx="0.0" sy="0.0" tx="0.0" ty="0.0">
70+
<y:Point x="532.9595561001786" y="244.99999999999997"/>
71+
</y:Path>
72+
</y:GenericEdge>
73+
</data>
74+
</edge>
75+
<actionHistory>
76+
<tid>1771794664219</tid>
77+
<inverse>
78+
<actionName>DEL_NODE</actionName>
79+
<parameters>WyI5N2M2OTRlZS0zNTAzLTRlZDctOWZhZS0xOGQ3YjgwNzA5ZmYiXQ==</parameters>
80+
</inverse>
81+
<equivalent>
82+
<actionName>ADD_NODE</actionName>
83+
<parameters>WyJwbTpwbV9SVC5weSIseyJ3aWR0aCI6MTE4LCJoZWlnaHQiOjYwLCJzaGFwZSI6InJlY3RhbmdsZSIsIm9wYWNpdHkiOjEsImJhY2tncm91bmRDb2xvciI6IiNmZmNjMDAiLCJib3JkZXJDb2xvciI6IiMwMDAiLCJib3JkZXJXaWR0aCI6MX0sIm9yZGluIix7IngiOjExMCwieSI6MTEwfSx7fSwiOTdjNjk0ZWUtMzUwMy00ZWQ3LTlmYWUtMThkN2I4MDcwOWZmIl0=</parameters>
84+
</equivalent>
85+
<hash>a95ae0297e5f0e63bfa65a29c8d14d92</hash>
86+
</actionHistory>
87+
<actionHistory>
88+
<tid>1771794687823</tid>
89+
<inverse>
90+
<actionName>DEL_NODE</actionName>
91+
<parameters>WyIxYmUwMDJkYi0wZmMyLTRjM2ItOGNhMS01NGU0YmJiM2ZiY2IiXQ==</parameters>
92+
</inverse>
93+
<equivalent>
94+
<actionName>ADD_NODE</actionName>
95+
<parameters>WyJjb250cm9sbGVyOmNvbnRyb2xsZXJfUlQucHkiLHsid2lkdGgiOjIwNiwiaGVpZ2h0Ijo2MCwic2hhcGUiOiJyZWN0YW5nbGUiLCJvcGFjaXR5IjoxLCJiYWNrZ3JvdW5kQ29sb3IiOiIjZmZjYzAwIiwiYm9yZGVyQ29sb3IiOiIjMDAwIiwiYm9yZGVyV2lkdGgiOjF9LCJvcmRpbiIseyJ4Ijo2NzAsInkiOjkwfSx7fSwiMWJlMDAyZGItMGZjMi00YzNiLThjYTEtNTRlNGJiYjNmYmNiIl0=</parameters>
96+
</equivalent>
97+
<hash>81165b0ec333e71a4ee794f887493323</hash>
98+
</actionHistory>
99+
<actionHistory>
100+
<tid>1771794690698</tid>
101+
<inverse>
102+
<actionName>SET_POS</actionName>
103+
<parameters>WyIxYmUwMDJkYi0wZmMyLTRjM2ItOGNhMS01NGU0YmJiM2ZiY2IiLHsieCI6MTEwLCJ5IjoxMTB9LHsieCI6NDkwLCJ5IjoxMTB9XQ==</parameters>
104+
</inverse>
105+
<equivalent>
106+
<actionName>SET_POS</actionName>
107+
<parameters>WyIxYmUwMDJkYi0wZmMyLTRjM2ItOGNhMS01NGU0YmJiM2ZiY2IiLHsieCI6NDkwLCJ5IjoxMTB9LHsieCI6MTEwLCJ5IjoxMTB9XQ==</parameters>
108+
</equivalent>
109+
<hash>2137de444092ba020db5d395a39a3b79</hash>
110+
</actionHistory>
111+
<actionHistory>
112+
<tid>1771794720693</tid>
113+
<inverse>
114+
<actionName>DEL_NODE</actionName>
115+
<parameters>WyIyN2M1ZDUyNC1iZGM5LTQ4Y2QtOWFhNi00Y2IxODk0Y2I1ZDciXQ==</parameters>
116+
</inverse>
117+
<equivalent>
118+
<actionName>ADD_NODE</actionName>
119+
<parameters>WyJwbG90eW06cGxvdHltX1JULnB5Iix7IndpZHRoIjoxOTYsImhlaWdodCI6NjAsInNoYXBlIjoicmVjdGFuZ2xlIiwib3BhY2l0eSI6MSwiYmFja2dyb3VuZENvbG9yIjoiI2ZmY2MwMCIsImJvcmRlckNvbG9yIjoiIzAwMCIsImJvcmRlcldpZHRoIjoxfSwib3JkaW4iLHsieCI6MzcwLCJ5Ijo0MzB9LHt9LCIyN2M1ZDUyNC1iZGM5LTQ4Y2QtOWFhNi00Y2IxODk0Y2I1ZDciXQ==</parameters>
120+
</equivalent>
121+
<hash>64c4ee3fac24cbd01e9cf06ea57dd8c2</hash>
122+
</actionHistory>
123+
<actionHistory>
124+
<tid>1771794723359</tid>
125+
<inverse>
126+
<actionName>SET_POS</actionName>
127+
<parameters>WyIyN2M1ZDUyNC1iZGM5LTQ4Y2QtOWFhNi00Y2IxODk0Y2I1ZDciLHsieCI6MTEwLCJ5IjoxMTB9LHsieCI6MzcwLCJ5IjozNTB9XQ==</parameters>
128+
</inverse>
129+
<equivalent>
130+
<actionName>SET_POS</actionName>
131+
<parameters>WyIyN2M1ZDUyNC1iZGM5LTQ4Y2QtOWFhNi00Y2IxODk0Y2I1ZDciLHsieCI6MzcwLCJ5IjozNTB9LHsieCI6MTEwLCJ5IjoxMTB9XQ==</parameters>
132+
</equivalent>
133+
<hash>b92c36146ebe446c0907df865f83794d</hash>
134+
</actionHistory>
135+
<actionHistory>
136+
<tid>1771794740309</tid>
137+
<inverse>
138+
<actionName>DEL_EDGE</actionName>
139+
<parameters>WyJkNGRkZDA2Mi00NTUyLTRkNTktOWIzYy0wYzA0ZGU2MGQxOTciXQ==</parameters>
140+
</inverse>
141+
<equivalent>
142+
<actionName>ADD_EDGE</actionName>
143+
<parameters>W3sic291cmNlSUQiOiIxYmUwMDJkYi0wZmMyLTRjM2ItOGNhMS01NGU0YmJiM2ZiY2IiLCJ0YXJnZXRJRCI6Ijk3YzY5NGVlLTM1MDMtNGVkNy05ZmFlLTE4ZDdiODA3MDlmZiIsImxhYmVsIjoiZWRnZTEiLCJzdHlsZSI6eyJ0aGlja25lc3MiOjIsImJhY2tncm91bmRDb2xvciI6IiM1NTUiLCJzaGFwZSI6InNvbGlkIn0sImlkIjoiZDRkZGQwNjItNDU1Mi00ZDU5LTliM2MtMGMwNGRlNjBkMTk3In1d</parameters>
144+
</equivalent>
145+
<hash>8b3c683a137808fb40dc2921313f8115</hash>
146+
</actionHistory>
147+
<actionHistory>
148+
<tid>1771794751288</tid>
149+
<inverse>
150+
<actionName>SET_POS</actionName>
151+
<parameters>WyIxYmUwMDJkYi0wZmMyLTRjM2ItOGNhMS01NGU0YmJiM2ZiY2IiLHsieCI6NDkwLCJ5IjoxMTB9LHsieCI6NjMwLCJ5Ijo5MH1d</parameters>
152+
</inverse>
153+
<equivalent>
154+
<actionName>SET_POS</actionName>
155+
<parameters>WyIxYmUwMDJkYi0wZmMyLTRjM2ItOGNhMS01NGU0YmJiM2ZiY2IiLHsieCI6NjMwLCJ5Ijo5MH0seyJ4Ijo0OTAsInkiOjExMH1d</parameters>
156+
</equivalent>
157+
<hash>0ce00744b96d9ae529645d9297b56821</hash>
158+
</actionHistory>
159+
<actionHistory>
160+
<tid>1771794765726</tid>
161+
<inverse>
162+
<actionName>DEL_EDGE</actionName>
163+
<parameters>WyIwOGU0NTRhNS1mYjAwLTQ2MDEtOWMxNC00NDA4NGIyMzFiNTciXQ==</parameters>
164+
</inverse>
165+
<equivalent>
166+
<actionName>ADD_EDGE</actionName>
167+
<parameters>W3sic291cmNlSUQiOiI5N2M2OTRlZS0zNTAzLTRlZDctOWZhZS0xOGQ3YjgwNzA5ZmYiLCJ0YXJnZXRJRCI6IjI3YzVkNTI0LWJkYzktNDhjZC05YWE2LTRjYjE4OTRjYjVkNyIsImxhYmVsIjoiZWRnZTIiLCJzdHlsZSI6eyJ0aGlja25lc3MiOjIsImJhY2tncm91bmRDb2xvciI6IiM1NTUiLCJzaGFwZSI6InNvbGlkIn0sImlkIjoiMDhlNDU0YTUtZmIwMC00NjAxLTljMTQtNDQwODRiMjMxYjU3In1d</parameters>
168+
</equivalent>
169+
<hash>901af9eaa14d4bf0fc1ba715fc5ac3d2</hash>
170+
</actionHistory>
171+
<actionHistory>
172+
<tid>1771794782565</tid>
173+
<inverse>
174+
<actionName>DEL_EDGE</actionName>
175+
<parameters>WyI3ZmM3YjQxNy01NDBkLTRhMDgtYTc4Ni0wNDkwYjgyMDg4YWUiXQ==</parameters>
176+
</inverse>
177+
<equivalent>
178+
<actionName>ADD_EDGE</actionName>
179+
<parameters>W3sic291cmNlSUQiOiIyN2M1ZDUyNC1iZGM5LTQ4Y2QtOWFhNi00Y2IxODk0Y2I1ZDciLCJ0YXJnZXRJRCI6IjFiZTAwMmRiLTBmYzItNGMzYi04Y2ExLTU0ZTRiYmIzZmJjYiIsImxhYmVsIjoiZWRnZTMiLCJzdHlsZSI6eyJ0aGlja25lc3MiOjIsImJhY2tncm91bmRDb2xvciI6IiM1NTUiLCJzaGFwZSI6InNvbGlkIn0sImlkIjoiN2ZjN2I0MTctNTQwZC00YTA4LWE3ODYtMDQ5MGI4MjA4OGFlIn1d</parameters>
180+
</equivalent>
181+
<hash>e157ed01dca772c8461787c1053794ce</hash>
182+
</actionHistory>
183+
<actionHistory>
184+
<tid>1771794788695</tid>
185+
<inverse>
186+
<actionName>SET_POS</actionName>
187+
<parameters>WyIyN2M1ZDUyNC1iZGM5LTQ4Y2QtOWFhNi00Y2IxODk0Y2I1ZDciLHsieCI6MzcwLCJ5IjozNTB9LHsieCI6MzcwLCJ5Ijo0MzB9XQ==</parameters>
188+
</inverse>
189+
<equivalent>
190+
<actionName>SET_POS</actionName>
191+
<parameters>WyIyN2M1ZDUyNC1iZGM5LTQ4Y2QtOWFhNi00Y2IxODk0Y2I1ZDciLHsieCI6MzcwLCJ5Ijo0MzB9LHsieCI6MzcwLCJ5IjozNTB9XQ==</parameters>
192+
</equivalent>
193+
<hash>aebd758397fec30aac0b59fe643a0da2</hash>
194+
</actionHistory>
195+
<actionHistory>
196+
<tid>1771794795323</tid>
197+
<inverse>
198+
<actionName>SET_POS</actionName>
199+
<parameters>WyIxYmUwMDJkYi0wZmMyLTRjM2ItOGNhMS01NGU0YmJiM2ZiY2IiLHsieCI6NjMwLCJ5Ijo5MH0seyJ4Ijo2NzAsInkiOjkwfV0=</parameters>
200+
</inverse>
201+
<equivalent>
202+
<actionName>SET_POS</actionName>
203+
<parameters>WyIxYmUwMDJkYi0wZmMyLTRjM2ItOGNhMS01NGU0YmJiM2ZiY2IiLHsieCI6NjcwLCJ5Ijo5MH0seyJ4Ijo2MzAsInkiOjkwfV0=</parameters>
204+
</equivalent>
205+
<hash>20778f5ad2b1c7bd577f09da007bc0e0</hash>
206+
</actionHistory>
207+
</graph>
208+
</graphml>

0 commit comments

Comments
 (0)