def test_phi_2d_linear(self): C_0, C_1 = symbols('C_0 C_1') state_vector = [C_0, C_1] time_symbol = Symbol('t') input_fluxes = {} output_fluxes = {0: C_0, 1: C_1} internal_fluxes = {} srm = SmoothReservoirModel(state_vector, time_symbol, input_fluxes, output_fluxes, internal_fluxes) start_values = np.array([1, 2]) t_0 = 0 t_max = 4 nt = 200 times = np.linspace(t_0, t_max, nt) smr = SmoothModelRun(srm, {}, start_values, times) smr.initialize_state_transition_operator_cache(lru_maxsize=None, size=2) nr_pools = srm.nr_pools def baseVector(i): e_i = np.zeros((nr_pools, 1)) e_i[i] = 1 return e_i bvs = [baseVector(i) for i in range(nr_pools)] for s in np.linspace(t_0, t_max, 5): for t in np.linspace(s, t_max, 5): phi_ref = np.eye(2) * np.exp(-(t - s)) # test the matrix valued results with self.subTest(): self.assertTrue( np.allclose(smr.Phi(t, s), phi_ref, rtol=1e-2)) # test the vectored valued results for x in bvs: for phi_x in [ smr._state_transition_operator(t, s, x), smr._state_transition_operator_for_linear_systems( t, s, x) ]: with self.subTest(): self.assertTrue( np.allclose(phi_x, np.matmul(phi_ref, x).reshape(nr_pools, ), rtol=1e-2))
def test_phi_2d_non_linear(self): k_0_val = 1 k_1_val = 2 x0_0 = np.float(0.5) x0_1 = np.float(1.5) x_0, x_1, k_0, k_1, t, u = symbols("x_0 x_1 k_0 k_1 t u") inputs = {0: u, 1: u * t} outputs = {0: k_0 * x_0**2, 1: k_1 * x_1} internal_fluxes = {} svec = Matrix([x_0, x_1]) srm = SmoothReservoirModel(state_vector=svec, time_symbol=t, input_fluxes=inputs, output_fluxes=outputs, internal_fluxes=internal_fluxes) t_0 = 0 t_max = 4 nt = 20000 times = np.linspace(t_0, t_max, nt) parameter_dict = {k_0: k_0_val, k_1: k_1_val, u: 1} func_dict = {} # make it a column vector for later use start_x = np.array([x0_0, x0_1]) # create the model run smr = SmoothModelRun( model=srm, parameter_dict=parameter_dict, start_values=start_x, times=times, func_set=func_dict, ) smr.initialize_state_transition_operator_cache(lru_maxsize=None, size=4) nr_pools = srm.nr_pools def baseVector(i): e_i = np.zeros((nr_pools, 1)) e_i[i] = 1 return e_i bvs = [baseVector(i) for i in range(nr_pools)] smrl = smr.linearize_old() for s in np.linspace(t_0, t_max, 5): for t in np.linspace(s, t_max, 5): for x in bvs: with self.subTest(): self.assertTrue( np.allclose( smr._state_transition_operator(t, s, x), smrl. _state_transition_operator_for_linear_systems( t, s, x), # noqa: E501 rtol=1.5 * 1e-2)) for t in np.linspace(t_0, t_max, 6): for phi_mat in [smr.Phi(t, t_0), smrl.Phi(t, t_0)]: for x in bvs: with self.subTest(): self.assertTrue( np.allclose(np.matmul(phi_mat, x).reshape(nr_pools, ), smr._state_transition_operator( t, t_0, x), rtol=1.5 * 1e-2))