Allow termination condition in "minimal" kernel
This commit is contained in:
parent
94b9296d6b
commit
783264fa26
1 changed files with 12 additions and 4 deletions
|
|
@ -161,6 +161,7 @@ class Simulator(object):
|
||||||
|
|
||||||
:param model: the model to simulate
|
:param model: the model to simulate
|
||||||
"""
|
"""
|
||||||
|
self.original_model = model
|
||||||
if isinstance(model, CoupledDEVS):
|
if isinstance(model, CoupledDEVS):
|
||||||
component_set = directConnect(model.component_set)
|
component_set = directConnect(model.component_set)
|
||||||
ids = 0
|
ids = 0
|
||||||
|
|
@ -177,7 +178,7 @@ class Simulator(object):
|
||||||
model.time_next = (model.time_last[0] + model.timeAdvance(), 1)
|
model.time_next = (model.time_last[0] + model.timeAdvance(), 1)
|
||||||
model.model_id = 0
|
model.model_id = 0
|
||||||
self.model = RootDEVS([model])
|
self.model = RootDEVS([model])
|
||||||
self.termination_time = float('inf')
|
self.setTerminationTime(float('inf'))
|
||||||
|
|
||||||
def setTerminationTime(self, time):
|
def setTerminationTime(self, time):
|
||||||
"""
|
"""
|
||||||
|
|
@ -185,7 +186,15 @@ class Simulator(object):
|
||||||
|
|
||||||
:param time: simulation time at which simulation should terminate
|
:param time: simulation time at which simulation should terminate
|
||||||
"""
|
"""
|
||||||
self.termination_time = time
|
self.setTerminationCondition(lambda t, m: time <= t[0])
|
||||||
|
|
||||||
|
def setTerminationCondition(self, function):
|
||||||
|
"""
|
||||||
|
Set the termination condition of the simulation.
|
||||||
|
|
||||||
|
:param function: termination condition to execute, taking the current simulated time and the model, returning a boolean (True to terminate)
|
||||||
|
"""
|
||||||
|
self.termination_function = function
|
||||||
|
|
||||||
def simulate(self):
|
def simulate(self):
|
||||||
"""
|
"""
|
||||||
|
|
@ -193,8 +202,7 @@ class Simulator(object):
|
||||||
"""
|
"""
|
||||||
scheduler = self.model.scheduler
|
scheduler = self.model.scheduler
|
||||||
tn = scheduler.readFirst()
|
tn = scheduler.readFirst()
|
||||||
tt = self.termination_time
|
while not self.termination_function(tn, self.original_model):
|
||||||
while tt > tn[0]:
|
|
||||||
# Generate outputs
|
# Generate outputs
|
||||||
transitioning = defaultdict(int)
|
transitioning = defaultdict(int)
|
||||||
for c in scheduler.getImminent(tn):
|
for c in scheduler.getImminent(tn):
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue