Skip to content

Commit

Permalink
Added simple dpsim simulator reset
Browse files Browse the repository at this point in the history
Signed-off-by: SystemsPurge <[email protected]>
  • Loading branch information
SystemsPurge committed Nov 7, 2024
1 parent 412be7b commit 0ec6070
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
2 changes: 1 addition & 1 deletion villas/controller/components/simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def start(self, payload):
self.sim_workdir = os.path.join(self.workdir, 'simulation',
str(self.simuuid))

self.sim_logdir = self.sim_workdir + '/logs/'
self.sim_logdir = self.sim_workdir + '/Logs/'
self.logger.info('Simulation working directory: %s' % self.sim_workdir)

try:
Expand Down
22 changes: 19 additions & 3 deletions villas/controller/components/simulators/dpsim.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ def headers(self):

def load_cim(self, fp):
if fp is not None:
reader = dpsimpy.CIMReader(fp)
name = self.params.get("name",str(os.urandom(6).hex()))
reader = dpsimpy.CIMReader(name)
files = list(map(lambda x: f'{fp}/{x}',os.listdir(fp)))
freq = self.params.get("system-freq",50)
duration = self.params.get("duration",10)
Expand All @@ -43,13 +44,13 @@ def load_cim(self, fp):
solver = dpsimpy.Solver.NRP

system = reader.loadCIM(freq, files, domain, dpsimpy.PhaseType.Single, dpsimpy.GeneratorType.PVNode)
self.sim = dpsimpy.Simulation(fp)
self.sim = dpsimpy.Simulation(name)
self.sim.set_system(system)
self.sim.set_domain(domain)
self.sim.set_solver(solver)
self.sim.set_time_step(timestep)
self.sim.set_final_time(duration)
logger = dpsimpy.Logger(fp)
logger = dpsimpy.Logger(name)
for node in system.nodes:
logger.log_attribute(node.name()+'.V', 'v', node)
self.sim.add_logger(logger)
Expand All @@ -74,10 +75,25 @@ def start(self, payload):
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
try:
self.change_state('resetting')
except Exception as e:
self.change_state('error')
else:
self.change_state('idle')

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

0 comments on commit 0ec6070

Please sign in to comment.