diff --git a/bluepyopt/ephys/protocols.py b/bluepyopt/ephys/protocols.py index 43752aff..a658eb7f 100644 --- a/bluepyopt/ephys/protocols.py +++ b/bluepyopt/ephys/protocols.py @@ -171,7 +171,10 @@ def _run_func(self, cell_model, param_values, sim=None): self.instantiate(sim=sim, icell=cell_model.icell) try: - sim.run(self.total_duration, cvode_active=self.cvode_active) + sim.run( + self.total_duration, + cvode_active=self.cvode_active, + icell=cell_model.icell) except (RuntimeError, simulators.NrnSimulatorException): logger.debug( 'SweepProtocol: Running of parameter set {%s} generated ' diff --git a/bluepyopt/ephys/simulators.py b/bluepyopt/ephys/simulators.py index 49deeca0..ca71326f 100644 --- a/bluepyopt/ephys/simulators.py +++ b/bluepyopt/ephys/simulators.py @@ -112,12 +112,19 @@ def neuron(self): return neuron + def check_voltage_valid(self, icell, voltage_bound=200.): + """Raises exception if voltage at soma is out of bound""" + + if icell and abs(icell.soma[0](0.5).v) > voltage_bound: + raise NrnSimulatorException('Membrane potential is out of bound') + def run( self, tstop=None, dt=None, cvode_active=None, - random123_globalindex=None): + random123_globalindex=None, + icell=None): """Run protocol""" self.neuron.h.tstop = tstop @@ -163,7 +170,10 @@ def run( rng.Random123_globalindex(random123_globalindex) try: - self.neuron.h.run() + self.neuron.h.finitialize() + while self.neuron.h.t < self.neuron.h.tstop: + self.neuron.h.fadvance() + self.check_voltage_valid(icell) except Exception as e: raise NrnSimulatorException('Neuron simulator error', e)