def _setup_sim(self, **kwargs): """ Create a simulation interface to Assimulo using CVODE, given a problem definition """ # Create Assimulo interface sim = CVode(self.prob) sim.discr = 'BDF' sim.maxord = 5 # Setup some default arguments for the ODE solver, or override # if available. This is very hackish, but it's fine for now while # the number of anticipated tuning knobs is small. if 'maxh' in kwargs: sim.maxh = kwargs['maxh'] else: sim.maxh = np.min([0.1, self.output_dt]) if 'minh' in kwargs: sim.minh = kwargs['minh'] # else: sim.minh = 0.001 if "iter" in kwargs: sim.iter = kwargs['iter'] else: sim.iter = 'Newton' if "linear_solver" in kwargs: sim.linear_solver = kwargs['linear_solver'] if "max_steps" in kwargs: # DIFFERENT NAME!!!! sim.maxsteps = kwargs['max_steps'] else: sim.maxsteps = 1000 if "time_limit" in kwargs: sim.time_limit = kwargs['time_limit'] sim.report_continuously = True else: sim.time_limit = 0.0 # Don't save the [t_-, t_+] around events sim.store_event_points = False # Setup tolerances nr = self.args[0] sim.rtol = state_rtol sim.atol = state_atol + [1e-12] * nr if not self.console: sim.verbosity = 50 else: sim.verbosity = 40 # sim.report_continuously = False # Save the Assimulo interface return sim
def _setup_sim(self, **kwargs): """ Create a simulation interface to Assimulo using CVODE, given a problem definition """ # Create Assimulo interface sim = CVode(self.prob) sim.discr = 'BDF' sim.maxord = 5 # Setup some default arguments for the ODE solver, or override # if available. This is very hackish, but it's fine for now while # the number of anticipated tuning knobs is small. if 'maxh' in kwargs: sim.maxh = kwargs['maxh'] else: sim.maxh = np.min([0.1, self.output_dt]) if 'minh' in kwargs: sim.minh = kwargs['minh'] # else: sim.minh = 0.001 if "iter" in kwargs: sim.iter = kwargs['iter'] else: sim.iter = 'Newton' if "linear_solver" in kwargs: sim.linear_solver = kwargs['linear_solver'] if "max_steps" in kwargs: # DIFFERENT NAME!!!! sim.maxsteps = kwargs['max_steps'] else: sim.maxsteps = 1000 if "time_limit" in kwargs: sim.time_limit = kwargs['time_limit'] sim.report_continuously = True else: sim.time_limit = 0.0 # Don't save the [t_-, t_+] around events sim.store_event_points = False # Setup tolerances nr = self.args[0] sim.rtol = state_rtol sim.atol = state_atol + [1e-12]*nr if not self.console: sim.verbosity = 50 else: sim.verbosity = 40 # sim.report_continuously = False # Save the Assimulo interface return sim
def simulate(self, t:numpy.ndarray, parameters:dict=None, verbosity:int=30, reset_afterwards:bool=False, suppress_stdout:bool=True) -> List[TimeSeries]: """ Runs a forward simulation for the fully specified model and its observation functions (if specified). Arguments --------- t : numpy.ndarray or float The time points for integration. In case a single time point is provided, the solver will treat this as final integration time and chooses the intermediate steps on its own. Keyword arguments ----------------- parameters : dict In case a simulation for specific parameter values is wanted. Default is None. verbosity : int Prints solver statistics (quiet = 50, whisper = 40, normal = 30, loud = 20, scream = 10). Default is 30. reset_afterwards : bool After simulation, reset the Simulator instance to its state directly after instantiation. Useful, if parameters argument is used. suppress_stdout : bool No printouts of integrator warnings, which are directed to stdout by the assimulo package. Set to False for model debugging purposes. Default is True. Returns ------- simulations : List[TimeSeries] The collection of simulations results as `ModelState` objects and `Observation` objects (if at least one `ObservationFunction` has been specified) Raises ------ ValueError Not all parameters have values. """ if parameters is not None: self.set_parameters(parameters) # Create the solver from problem instance exp_sim = CVode(self.bioprocess_model) exp_sim.verbosity = verbosity # QUIET = 50 WHISPER = 40 NORMAL = 30 LOUD = 20 SCREAM = 10 exp_sim.store_event_points = False exp_sim.discr = 'BDF' exp_sim.stablimit = True if self.integrator_kwargs is not None: for key in list(self.integrator_kwargs.keys()): exec(f'exp_sim.{key} = self.integrator_kwargs["{key}"]') # Do the actual forward simulation _t = numpy.array(t, dtype=numpy.float64).flatten() if len(_t) == 1: tfinal = float(_t) ncp_list = None elif len(_t) > 1: ncp_list = _t tfinal = numpy.max(_t) try: if suppress_stdout: f = io.StringIO() with redirect_stdout(f): t, y = exp_sim.simulate(tfinal=tfinal, ncp_list=ncp_list) else: t, y = exp_sim.simulate(tfinal=tfinal, ncp_list=ncp_list) except CVodeError as e: print(f'CVodeError occured with flag {e.value}. CVodeError message was: {e}.') raise e # clean double entry artifacts due to event detection unq, unq_idx = numpy.unique(t, return_index=True) # build model prediction dictionary model_predictions = [ ModelState( name=_name, timepoints=numpy.array(t)[unq_idx], values=numpy.array(_y)[unq_idx], replicate_id=self.replicate_id, ) for _name, _y in zip(self.bioprocess_model.states, y.T) ] if self.observer is not None: observations = self.observer.get_observations(model_predictions) else: observations = [] if reset_afterwards: self.reset() simulations = [] simulations.extend(model_predictions) simulations.extend(observations) return simulations