def test_wrong_params3(): with pytest.raises( AssertionError, match= "Expecting required_lhs to be a collection of variables or derivatives" ): partial_eval([Eq(Symbol('x'), 2.0)], [3])
def _get_non_linear_state_vars(self): """ Get and store the non_linear state vars """ derivative_equations = \ set(partial_eval(self._derivative_equations, self._model.y_derivatives, keep_multiple_usages=False)) self._non_linear_state_vars = \ get_non_linear_state_vars(derivative_equations, self._model.membrane_voltage_var, self._model.state_vars)
def __init__(self, model, file_name, **kwargs): self._use_data_clamp = kwargs.get('cvode_data_clamp', False) # store if data clamp is needed self._use_analytic_jacobian = kwargs.get('use_analytic_jacobian', False) # store if jacobians are needed super().__init__(model, file_name, **kwargs) self._templates = ['cvode_model.hpp', 'cvode_model.cpp'] if self._use_data_clamp: self._vars_for_template['base_class'] = 'AbstractCvodeCellWithDataClamp' else: self._vars_for_template['base_class'] = 'AbstractCvodeCell' if self._use_data_clamp: self._vars_for_template['model_type'] = 'CvodeCellWithDataClamp' elif self._use_analytic_jacobian: self._vars_for_template['model_type'] = 'AnalyticCvode' else: self._vars_for_template['model_type'] = 'NumericCvode' self._vars_for_template['vector_decl'] = "N_Vector" # indicate how to declare state vars and values if self._use_analytic_jacobian: # get deriv eqs and substitute in all variables other than state vars self._derivative_equations = \ partial_eval(self._derivative_equations, self._model.y_derivatives, keep_multiple_usages=False) self._jacobian_equations, self._jacobian_matrix = get_jacobian(self._state_vars, self._derivative_equations) self._formatted_state_vars = self._update_state_vars() self._vars_for_template['jacobian_equations'], self._vars_for_template['jacobian_entries'] = \ self._print_jacobian() else: self._vars_for_template['jacobian_equations'], self._vars_for_template['jacobian_entries'] = \ [], Matrix()
def test_partial_eval2(fr_model): derivatives_eqs = fr_model.derivative_equations lhs_to_keep = fr_model.y_derivatives assert len(derivatives_eqs) == 163, str(len(derivatives_eqs)) derivatives_eqs = partial_eval(derivatives_eqs, lhs_to_keep, keep_multiple_usages=False) assert len(derivatives_eqs) == 25, str(len(derivatives_eqs)) expected = open( os.path.join(TESTS_FOLDER, 'test_partial_eval_derivatives_eqs2.txt'), 'r').read() assert str(derivatives_eqs) == expected, str(derivatives_eqs)
def _pre_print_hook(self): """ Retreives out linear and non-linear derivatives and the relevant jacobian for it.""" super()._pre_print_hook() # get deriv eqs and substitute in all variables other than state vars derivative_equations = \ partial_eval(self._derivative_equations, self._model.y_derivatives, keep_multiple_usages=False) self._non_linear_state_vars = \ sorted(get_non_linear_state_vars(derivative_equations, self._model.membrane_voltage_var, self._model.state_vars), key=lambda s: get_variable_name(s, s in self._in_interface)) # Pick the formatted equations that are for non-linear derivatives self._non_linear_eqs = self._get_non_linear_eqs() self._jacobian_equations, self._jacobian_matrix = \ get_jacobian(self._non_linear_state_vars, [d for d in derivative_equations if d.lhs.args[0] in self._non_linear_state_vars]) self._linear_deriv_eqs, self._linear_equations, self._vars_in_one_step = \ self._rearrange_linear_derivs()
def _get_derivative_equations(self): """ Get partially evaluated equations defining the derivatives including V (self._model.membrane_voltage_var)""" derivative_equations = partial_eval( super()._get_derivative_equations(), self._model.y_derivatives) self._lookup_tables.calc_lookup_tables(derivative_equations) return derivative_equations
def _get_extended_ionic_vars(self): """ Get the partially evaluated equations defining the ionic derivatives and all dependant equations""" extended_ionic_vars = partial_eval(super()._get_extended_ionic_vars(), self._model.ionic_vars) self._lookup_tables.calc_lookup_tables(extended_ionic_vars) return extended_ionic_vars
def _get_stimulus(self): """ Get the partially evaluated stimulus currents in the model""" return_stim_eqs = super()._get_stimulus() return partial_eval( return_stim_eqs, self._model.stimulus_params | self._model.modifiable_parameters)
def test_wrong_params(): with pytest.raises(TypeError, match="'NoneType' object is not iterable"): partial_eval(None, None)
def test_wrong_params2(): with pytest.raises( AssertionError, match="Expecting equations to be a collection of equations"): partial_eval([1], [2])
def _get_jacobian(self): """Retrieve jacobian matrix""" derivative_eqs_for_jacobian = \ partial_eval(self._derivative_equations, self._model.y_derivatives, keep_multiple_usages=False) return get_jacobian(self._state_vars, derivative_eqs_for_jacobian)