Skip to content

Commit e1f1cb0

Browse files
committed
Sim concurrent execution
Signed-off-by: SystemsPurge <[email protected]>
1 parent 0ec6070 commit e1f1cb0

File tree

1 file changed

+24
-10
lines changed
  • villas/controller/components/simulators

1 file changed

+24
-10
lines changed

villas/controller/components/simulators/dpsim.py

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import dpsimpy
2+
import math
3+
from threading import Thread
24
import os
35

46
from villas.controller.components.simulator import Simulator
@@ -8,7 +10,9 @@ class DPsimSimulator(Simulator):
810

911
def __init__(self, **args):
1012
self.sim = None
11-
13+
self.thread = Thread(target=self.sim_loop,args=[self])
14+
self.count = 0
15+
self.current = 0
1216
super().__init__(**args)
1317

1418
@property
@@ -50,6 +54,7 @@ def load_cim(self, fp):
5054
self.sim.set_solver(solver)
5155
self.sim.set_time_step(timestep)
5256
self.sim.set_final_time(duration)
57+
self.count = math.trunc(duration/timestep)
5358
logger = dpsimpy.Logger(name)
5459
for node in system.nodes:
5560
logger.log_attribute(node.name()+'.V', 'v', node)
@@ -67,39 +72,38 @@ def start(self, payload):
6772
try:
6873
self.change_state('starting')
6974
self.logger.info('Starting simulation...')
70-
7175
self.logger.info(self.sim)
7276
if self.sim.start() is None:
7377
self.change_state('running')
78+
self.thread.start()
7479
else:
7580
self.change_to_error('failed to start simulation')
7681
self.logger.warn('Attempt to start simulator failed.'
7782
'State is %s', self._state)
7883

79-
self.upload_results()
80-
self.change_state('stopping')
8184
except Exception as e:
8285
self.logger.warn('Attempted to start non-stopped simulator.'
8386
'State is %s', self._state)
8487

8588
def reset(self,payload):
8689
self.sim = None
87-
self.params = None
88-
self.model = None
89-
self.results = None
90+
self.current = 0
9091
try:
9192
self.change_state('resetting')
9293
except Exception as e:
9394
self.change_state('error')
9495
else:
9596
self.change_state('idle')
9697

97-
def stop(self, payload):
98+
def stop(self):
9899
if self._state == 'running':
99100
self.logger.info('Stopping simulation...')
100101

101102
if self.sim and self.sim.stop() is None:
102103
self.change_state('stopped')
104+
self.sim = None
105+
self.current = 0
106+
self.upload_results()
103107
self.logger.warn('State changed to ' + self._state)
104108
else:
105109
self.change_state('unknown')
@@ -112,13 +116,13 @@ def stop(self, payload):
112116
def pause(self, payload):
113117
if self._state == 'running':
114118
self.logger.info('Pausing simulation...')
115-
116119
self._state = 'pausing'
117120

118121
try:
119122
if self.sim and self.sim.pause() is None:
120123
self.change_state('paused')
121124
self.logger.warn('State changed to ' + self._state)
125+
self.thread = Thread(target=self.sim_loop,args=[self])
122126
else:
123127
self.logger.warn('Attempted to pause simulator failed.')
124128
self.change_state('unknown')
@@ -135,7 +139,6 @@ def pause(self, payload):
135139
def resume(self, payload):
136140
if self._state == 'paused':
137141
self.logger.info('Resuming simulation...')
138-
139142
self._state = 'resuming'
140143

141144
try:
@@ -154,3 +157,14 @@ def resume(self, payload):
154157
else:
155158
self.logger.warn('Attempted to resume non-paused simulator.'
156159
'State is %s', self._state)
160+
161+
162+
def sim_loop(self):
163+
while self.current<self.count and self._state == 'running':
164+
self.current+=1
165+
self.sim.next()
166+
167+
if self._state == 'running':
168+
self.stop()
169+
170+

0 commit comments

Comments
 (0)