1
1
import dpsimpy
2
+ import math
3
+ from threading import Thread
2
4
import os
3
5
4
6
from villas .controller .components .simulator import Simulator
@@ -8,7 +10,9 @@ class DPsimSimulator(Simulator):
8
10
9
11
def __init__ (self , ** args ):
10
12
self .sim = None
11
-
13
+ self .thread = Thread (target = self .sim_loop ,args = [self ])
14
+ self .count = 0
15
+ self .current = 0
12
16
super ().__init__ (** args )
13
17
14
18
@property
@@ -50,6 +54,7 @@ def load_cim(self, fp):
50
54
self .sim .set_solver (solver )
51
55
self .sim .set_time_step (timestep )
52
56
self .sim .set_final_time (duration )
57
+ self .count = math .trunc (duration / timestep )
53
58
logger = dpsimpy .Logger (name )
54
59
for node in system .nodes :
55
60
logger .log_attribute (node .name ()+ '.V' , 'v' , node )
@@ -67,39 +72,38 @@ def start(self, payload):
67
72
try :
68
73
self .change_state ('starting' )
69
74
self .logger .info ('Starting simulation...' )
70
-
71
75
self .logger .info (self .sim )
72
76
if self .sim .start () is None :
73
77
self .change_state ('running' )
78
+ self .thread .start ()
74
79
else :
75
80
self .change_to_error ('failed to start simulation' )
76
81
self .logger .warn ('Attempt to start simulator failed.'
77
82
'State is %s' , self ._state )
78
83
79
- self .upload_results ()
80
- self .change_state ('stopping' )
81
84
except Exception as e :
82
85
self .logger .warn ('Attempted to start non-stopped simulator.'
83
86
'State is %s' , self ._state )
84
87
85
88
def reset (self ,payload ):
86
89
self .sim = None
87
- self .params = None
88
- self .model = None
89
- self .results = None
90
+ self .current = 0
90
91
try :
91
92
self .change_state ('resetting' )
92
93
except Exception as e :
93
94
self .change_state ('error' )
94
95
else :
95
96
self .change_state ('idle' )
96
97
97
- def stop (self , payload ):
98
+ def stop (self ):
98
99
if self ._state == 'running' :
99
100
self .logger .info ('Stopping simulation...' )
100
101
101
102
if self .sim and self .sim .stop () is None :
102
103
self .change_state ('stopped' )
104
+ self .sim = None
105
+ self .current = 0
106
+ self .upload_results ()
103
107
self .logger .warn ('State changed to ' + self ._state )
104
108
else :
105
109
self .change_state ('unknown' )
@@ -112,13 +116,13 @@ def stop(self, payload):
112
116
def pause (self , payload ):
113
117
if self ._state == 'running' :
114
118
self .logger .info ('Pausing simulation...' )
115
-
116
119
self ._state = 'pausing'
117
120
118
121
try :
119
122
if self .sim and self .sim .pause () is None :
120
123
self .change_state ('paused' )
121
124
self .logger .warn ('State changed to ' + self ._state )
125
+ self .thread = Thread (target = self .sim_loop ,args = [self ])
122
126
else :
123
127
self .logger .warn ('Attempted to pause simulator failed.' )
124
128
self .change_state ('unknown' )
@@ -135,7 +139,6 @@ def pause(self, payload):
135
139
def resume (self , payload ):
136
140
if self ._state == 'paused' :
137
141
self .logger .info ('Resuming simulation...' )
138
-
139
142
self ._state = 'resuming'
140
143
141
144
try :
@@ -154,3 +157,14 @@ def resume(self, payload):
154
157
else :
155
158
self .logger .warn ('Attempted to resume non-paused simulator.'
156
159
'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