Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Corrected stimulus preparation and update for cosimulator.py #672

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions tvb_contrib/tvb/contrib/cosimulation/cosimulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ class CoSimulator(Simulator):
good_cosim_update_values_shape = (0, 0, 0, 0)
cosim_history = None # type: CosimHistory
_cosimulation_flag = False
_current_stimulus = None
_compute_requirements = True
number_of_cosim_monitors = 0
_cosim_monitors_noncoupling_indices = []
Expand Down Expand Up @@ -171,6 +172,7 @@ def configure(self, full_configure=True):
else:
self._cosimulation_flag = False
self.synchronization_n_step = 0
self._current_stimulus = self._prepare_stimulus()

def _loop_update_cosim_history(self, step, state):
"""
Expand Down Expand Up @@ -210,6 +212,12 @@ def _update_cosim_history(self, current_steps, cosim_updates):
raise NumericalInstability("There are missing values for continue the simulation")
super(CoSimulator,self)._loop_update_history(step, state)

def _loop_update_stimulus(self, step, stimulus):
"""Update stimulus values for current time step."""
# TODO: Think about how to have the same behavior as for the Simulator for continuous simulation!
if self.stimulus is not None:
stimulus[self.model.stvar, :, :] = self.stimulus(step-1).reshape((1, -1, 1))

def __call__(self, simulation_length=None, random_state=None, n_steps=None,
cosim_updates=None, recompute_requirements=False):
"""
Expand Down Expand Up @@ -274,15 +282,16 @@ def __call__(self, simulation_length=None, random_state=None, n_steps=None,
self.integrator.set_random_state(random_state)

local_coupling = self._prepare_local_coupling()
stimulus = self._prepare_stimulus()
state = self.current_state
if self._current_stimulus is None:
self._current_stimulus = self._prepare_stimulus()
start_step = self.current_step + 1
node_coupling = self._loop_compute_node_coupling(start_step)

# integration loop
for step in range(start_step, start_step + n_steps):
self._loop_update_stimulus(step, stimulus)
state = self.integrate_next_step(state, self.model, node_coupling, local_coupling, stimulus)
self._loop_update_stimulus(step, self._current_stimulus)
state = self.integrate_next_step(state, self.model, node_coupling, local_coupling, self._current_stimulus)
state_output = self._loop_update_cosim_history(step, state)
node_coupling = self._loop_compute_node_coupling(step + 1)
output = self._loop_monitor_output(step-self.synchronization_n_step, state_output, node_coupling)
Expand Down