Skip to content

Commit

Permalink
Sim concurrent execution
Browse files Browse the repository at this point in the history
Signed-off-by: SystemsPurge <[email protected]>
  • Loading branch information
SystemsPurge committed Nov 11, 2024
1 parent 0ec6070 commit e1f1cb0
Showing 1 changed file with 24 additions and 10 deletions.
34 changes: 24 additions & 10 deletions villas/controller/components/simulators/dpsim.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import dpsimpy
import math
from threading import Thread
import os

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

def __init__(self, **args):
self.sim = None

self.thread = Thread(target=self.sim_loop,args=[self])
self.count = 0
self.current = 0
super().__init__(**args)

@property
Expand Down Expand Up @@ -50,6 +54,7 @@ def load_cim(self, fp):
self.sim.set_solver(solver)
self.sim.set_time_step(timestep)
self.sim.set_final_time(duration)
self.count = math.trunc(duration/timestep)
logger = dpsimpy.Logger(name)
for node in system.nodes:
logger.log_attribute(node.name()+'.V', 'v', node)
Expand All @@ -67,39 +72,38 @@ def start(self, payload):
try:
self.change_state('starting')
self.logger.info('Starting simulation...')

self.logger.info(self.sim)
if self.sim.start() is None:
self.change_state('running')
self.thread.start()
else:
self.change_to_error('failed to start simulation')
self.logger.warn('Attempt to start simulator failed.'
'State is %s', self._state)

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

def reset(self,payload):
self.sim = None
self.params = None
self.model = None
self.results = None
self.current = 0
try:
self.change_state('resetting')
except Exception as e:
self.change_state('error')
else:
self.change_state('idle')

def stop(self, payload):
def stop(self):
if self._state == 'running':
self.logger.info('Stopping simulation...')

if self.sim and self.sim.stop() is None:
self.change_state('stopped')
self.sim = None
self.current = 0
self.upload_results()
self.logger.warn('State changed to ' + self._state)
else:
self.change_state('unknown')
Expand All @@ -112,13 +116,13 @@ def stop(self, payload):
def pause(self, payload):
if self._state == 'running':
self.logger.info('Pausing simulation...')

self._state = 'pausing'

try:
if self.sim and self.sim.pause() is None:
self.change_state('paused')
self.logger.warn('State changed to ' + self._state)
self.thread = Thread(target=self.sim_loop,args=[self])
else:
self.logger.warn('Attempted to pause simulator failed.')
self.change_state('unknown')
Expand All @@ -135,7 +139,6 @@ def pause(self, payload):
def resume(self, payload):
if self._state == 'paused':
self.logger.info('Resuming simulation...')

self._state = 'resuming'

try:
Expand All @@ -154,3 +157,14 @@ def resume(self, payload):
else:
self.logger.warn('Attempted to resume non-paused simulator.'
'State is %s', self._state)


def sim_loop(self):
while self.current<self.count and self._state == 'running':
self.current+=1
self.sim.next()

if self._state == 'running':
self.stop()


0 comments on commit e1f1cb0

Please sign in to comment.