def test_jacobian(self): C_0, C_1 = symbols('C_0 C_1') state_vars = [C_0, C_1] t = Symbol('t') # linear compartmental matrix # constant input # The result is the compartmental matrix input_fluxes = {0: 50, 1: 60} internal_fluxes = {} output_fluxes = {0: C_0, 1: 6 * C_1} rm = SmoothReservoirModel(state_vars, t, input_fluxes, output_fluxes, internal_fluxes) self.assertEqual(rm.jacobian, -diag(1, 6)) # linear compartmental matrix # but 'linear' state dependent input I=M*C+I0 # The result is J=B+M sv = Matrix(state_vars) B = -diag(1, 6) M = Matrix([[1, 2], [3, 4]]) I0 = Matrix(2, 1, [50, 60]) I = M * sv + I0 rm = SmoothReservoirModel.from_B_u(sv, t, -diag(1, 6), I) self.assertEqual(rm.jacobian, B + M) # non linear compartmental matrix # constant input # The result is NOT the compartmental matrix B state_vars = [C_0] output_fluxes = {0: C_0**3} #input_fluxes = {0:50,1:60} input_fluxes = {} rm = SmoothReservoirModel(state_vars, t, input_fluxes, output_fluxes, internal_fluxes) J = rm.jacobian CM = Matrix([-3 * C_0**2]) # the next line breaks self.assertEqual(J, CM) # non linear compartmental matrix # with (linear) external function in input state_vars = [C_0] output_fluxes = {0: C_0**3} f_expr = Function('f')(t) def f_func(t_val): return t_val func_set = {f_expr: f_func} input_fluxes = {0: C_0 * f_expr} #input_fluxes = {0:50,1:60} internal_fluxes = {} rm = SmoothReservoirModel(state_vars, t, input_fluxes, output_fluxes, internal_fluxes) J = rm.jacobian CM = Matrix([-3 * C_0**2 + f_expr]) self.assertEqual(J, CM)
def test_is_state_dependent(self): x, y, t = symbols("x y t") X = Matrix([x, y]) u = Matrix([0, 0]) srm = SmoothReservoirModel.from_B_u(X, t, Matrix([[-1, 0], [0, -1]]), u) self.assertFalse(srm.is_state_dependent(u)) u = Matrix([x, 0]) srm = SmoothReservoirModel.from_B_u(X, t, Matrix([[-1, 0], [0, -1]]), u) self.assertTrue(srm.is_state_dependent(u))
def test_input_flux_type(self): C_0, C_1, C_2 = symbols('C_0 C_1 C_2') state_vector = [C_0, C_1, C_2] time_symbol = Symbol('t') input_fluxes = {0: C_0 + 5, 1: 0, 2: C_0**2 + C_1} output_fluxes = {} internal_fluxes = {} rm = SmoothReservoirModel(state_vector, time_symbol, input_fluxes, output_fluxes, internal_fluxes) self.assertEqual(rm._input_flux_type(0), 'linear')
def setUp(self): x, y, t = symbols("x y t") state_vector = Matrix([x, y]) B = Matrix([[-1, 1.5], [0.5, -2]]) u = Matrix(2, 1, [9, 1]) srm = SmoothReservoirModel.from_B_u(state_vector, t, B, u) start_values = np.array([10, 40]) self.start_values = start_values self.t_0 = 0 self.t_max = 10 self.ntmo = 10 self.fac = 2 self.times = np.linspace(self.t_0, self.t_max, self.ntmo + 1) self.smr = SmoothModelRun(srm, {}, start_values, self.times) alpha = 0.5 self.decay_rate = 1.0 self.start_values_14C = alpha * self.start_values def Fa_func(t): return alpha self.Fa_func = Fa_func self.smr_14C = SmoothModelRun_14C(self.smr, self.start_values_14C, self.Fa_func, self.decay_rate)
def setUp(self): x, t = symbols('x t') B = Matrix(1, 1, [-1]) u = Matrix(1, 1, [1]) state_vector = Matrix(1, 1, [x]) srm = SmoothReservoirModel.from_B_u(state_vector, t, B, u) parameter_dict = {} start_values = np.array([1]) times = np.arange(0, 6, 1) smr = SmoothModelRun(srm, parameter_dict, start_values, times) self.alpha = 1 start_values_14C = start_values * self.alpha decay_rate = 1.0 def Fa_func(t): return self.alpha self.smr_14C = SmoothModelRun_14C(smr, start_values_14C, Fa_func, decay_rate) dmr_from_pwc = DMR.from_SmoothModelRun(smr) fake_net_Us = DMR.from_SmoothModelRun(self.smr_14C).net_Us # we cannot infer net_Us_14C coming from data, hence we use the # net_Us from the 14C model coming from the Smooth 14C model # this is of no use in pracitical situations tough since once # we have smr, we can use smr_14C immediately instead of going # through DMRs self.dmr_from_pwc_14C = DiscreteModelRun_14C(dmr_from_pwc, start_values_14C, fake_net_Us, decay_rate)
def test_Bu_matrices_to_fluxes_and_back(self): # f = u + xi*B*C t,C_1, C_2, C_3, k_1, k_2, k_3, a_12, a_13, a_21, a_23, a_31, a_32, u_1, u_2, u_3, gamma, xi \ = symbols('t,C_1 C_2 C_3 k_1 k_2 k_3 a_12 a_13 a_21 a_23 a_31 a_32 u_1 u_2 u_3 gamma xi') C = Matrix(3, 1, [C_1, C_2, C_3]) u = Matrix(3, 1, [u_1, u_2, u_3]) B = gamma * Matrix([[-k_1, a_12, a_13], [a_21, -k_2, a_23], [a_31, a_32, -k_3]]) rm = SmoothReservoirModel.from_B_u(C, t, B, u) self.assertEqual(rm.input_fluxes, {0: u_1, 1: u_2, 2: u_3}) for key, val in rm.output_fluxes.items(): with self.subTest(): ref_val = { 0: C_1 * (-a_21 * gamma - a_31 * gamma + k_1 * gamma), 1: C_2 * (-a_12 * gamma - a_32 * gamma + k_2 * gamma), 2: C_3 * (-a_13 * gamma - a_23 * gamma + k_3 * gamma) }[key] self.assertEqual(simplify(val - ref_val), 0) self.assertEqual( rm.internal_fluxes, { (0, 1): gamma * a_21 * C_1, (0, 2): gamma * a_31 * C_1, (1, 0): gamma * a_12 * C_2, (1, 2): gamma * a_32 * C_2, (2, 0): gamma * a_13 * C_3, (2, 1): gamma * a_23 * C_3 }) ## test backward conversion to compartmental matrix B2 = rm.compartmental_matrix u2 = rm.external_inputs self.assertEqual(simplify(u - u2), zeros(*u.shape)) self.assertEqual(simplify(B - B2), zeros(*B.shape))
def setUp(self): C_1, C_2, k = symbols('C_1 C_2 k') B = Matrix([[-2, 0], [k, -2]]) u = Matrix(2, 1, [1, 1]) state_vector = Matrix(2, 1, [C_1, C_2]) time_symbol = Symbol('t') srm = SmoothReservoirModel.from_B_u(state_vector, time_symbol, B, u) parameter_dict = {k: 1} start_values = np.array([1.0 / 3.0, 4.0 / 9.0]) times = np.linspace(0, 5, 6) smr = SmoothModelRun(srm, parameter_dict, start_values, times) # smr.initialize_state_transition_operator_cache(lru_maxsize=None) self.alpha = 0.5 # self.alpha = ALPHA_14C start_values_14C = smr.start_values * self.alpha def Fa_func_14C(t): return self.alpha self.smr_14C = SmoothModelRun_14C( smr, start_values_14C, Fa_func_14C, 1.0 # or use DECAY_RATE_14C_DAILY )
def test_figure(self): C_0, C_1 = symbols('C_0 C_1') state_vector = [C_0, C_1] time_symbol = Symbol('t') input_fluxes = {0: 3 * C_0} output_fluxes = {1: 2 * C_0} internal_fluxes = {(0, 1): 5 * C_0 * C_1, (1, 0): 4 * C_0} rm = SmoothReservoirModel(state_vector, time_symbol, input_fluxes, output_fluxes, internal_fluxes) fig = rm.figure() fig.savefig("normal_reservoir_model_plot.pdf") fig = rm.figure(thumbnail=True) fig.savefig("thumbnail_reservoir_model_plot.pdf") fig = rm.figure(logo=True) fig.savefig("logo_reservoir_model_plot.pdf")
def test_free_symbols(self): t,C_1, C_2, C_3, k_1, k_2, k_3, a_12, a_13, a_21, a_23, a_31, a_32, u_1, u_2, u_3, gamma, xi \ = symbols('t,C_1 C_2 C_3 k_1 k_2 k_3 a_12 a_13 a_21 a_23 a_31 a_32 u_1 u_2 u_3 gamma xi') C = Matrix(3, 1, [C_1, C_2, C_3]) u = Matrix(3, 1, [u_1, u_2, u_3]) B = gamma * Matrix([[-k_1, a_12, a_13], [a_21, -k_2, a_23], [a_31, a_32, -k_3]]) rm = SmoothReservoirModel.from_B_u(C, t, B, u) rm_p1 = rm.subs({ k_1: 4, a_21: 1, a_31: 2, k_2: 6, a_12: 2, a_32: 3, k_3: 9, a_13: 4, a_23: 4, u_1: 1, u_2: 1, u_3: 1, gamma: 1, xi: 0.5 })
def setUp(self): self.symbol_names = ("C_0", "C_1", "t", "k_01", "k_10", "k_0o") for n in self.symbol_names: var(n) k_1o = Function("k_1o") self.state_variables = [C_0, C_1] # order is important # input to pool 0 # input to pool 1 self.input_fluxes_by_symbol = {C_0: sin(t) + 2, C_1: cos(t) + 2} self.inputs = hr.to_int_keys_1(self.input_fluxes_by_symbol, self.state_variables) #self.inputs = {0: sin(t) + 2, 1: cos(t) + 2} self.out_fluxes_by_symbol = { C_0: k_0o * C_0**3, # output from pool 0 C_1: k_1o(t) * C_1**3, # output from pool 0 } self.outputs = hr.to_int_keys_1(self.out_fluxes_by_symbol, self.state_variables) #self.outputs = { # 0: k_0o * C_0 ** 3, # output from pool 0 # 1: k_1o(t) * C_1 ** 3, # output from pool 0 #} self.internal_fluxes_by_symbol = { (C_0, C_1): k_01 * C_0 * C_1**2, # flux from pool 0 to pool 1 (C_1, C_0): k_10 * C_0 * C_1, # flux from pool 1 to pool 0 } self.internal_fluxes = hr.to_int_keys_2(self.internal_fluxes_by_symbol, self.state_variables) self.time_symbol = t self.srm = SmoothReservoirModel(self.state_variables, self.time_symbol, self.inputs, self.outputs, self.internal_fluxes)
def test_age_moment_system(self): x, y, t = symbols("x y t") state_vector = Matrix([x, y]) B = Matrix([[-1, 0], [0, -2]]) u = Matrix(2, 1, [9, 1]) srm = SmoothReservoirModel.from_B_u(state_vector, t, B, u) max_order = 1 extended_state, extended_rhs = srm.age_moment_system(max_order) x_moment_1, y_moment_1 = symbols('x_moment_1 y_moment_1') self.assertEqual(extended_state, Matrix([[x], [y], [x_moment_1], [y_moment_1]])) self.assertEqual( extended_rhs, Matrix([[-x + 9], [-2 * y + 1], [1 - 9 * x_moment_1 / x], [1 - y_moment_1 / y]])) max_order = 2 extended_state, extended_rhs = srm.age_moment_system(max_order) x_moment_1, y_moment_1, x_moment_2, y_moment_2 = symbols( 'x_moment_1 y_moment_1 x_moment_2 y_moment_2') self.assertEqual( extended_state, Matrix([[x], [y], [x_moment_1], [y_moment_1], [x_moment_2], [y_moment_2]])) self.assertEqual( extended_rhs, Matrix([[-x + 9], [-2 * y + 1], [1 - 9 * x_moment_1 / x], [1 - y_moment_1 / y], [2 * x_moment_1 - 9 * x_moment_2 / x], [2 * y_moment_1 - y_moment_2 / y]]))
def test_xi_T_N_u_representation(self): u_0, u_1, C_0, C_1, gamma = symbols('u_0 u_1 C_0 C_1 gamma') state_vector = [C_0, C_1] time_symbol = Symbol('t') input_fluxes = {0: u_0, 1: u_1} output_fluxes = {1: 3 * gamma * C_0 * C_1} internal_fluxes = { (0, 1): gamma * 3 * 5 * C_0 * C_1, (1, 0): gamma * 3 * 4 * C_0 } rm = SmoothReservoirModel(state_vector, time_symbol, input_fluxes, output_fluxes, internal_fluxes) xi, T, N, C, u = rm.xi_T_N_u_representation() self.assertEqual(u, Matrix([u_0, u_1])) self.assertEqual(xi, 3 * gamma) self.assertEqual(T, Matrix([[-1, 4 / (C_1 + 4)], [1, -1]])) self.assertEqual(N, Matrix([[5 * C_1, 0], [0, C_0 * (C_1 + 4) / C_1]]))
def test_state_transition_operator_2d(self): # two-dimensional case 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([5, 3]) times = np.linspace(0, 1, 11) smr = SmoothModelRun(srm, {}, start_values, times) x = np.array([1, 3]) Phix = smr._state_transition_operator(1, 0, x) self.assertEqual(Phix.shape, (2, )) # test t < t_0 with self.assertRaises(Exception): Phix = smr._state_transition_operator(0, 1, x) # test if operator works correctly also late in time C = Symbol('C') state_vector = [C] time_symbol = Symbol('t') # are inputs really ignored in the computation of Phi? input_fluxes = {0: 1} output_fluxes = {0: C} internal_fluxes = {} srm = SmoothReservoirModel(state_vector, time_symbol, input_fluxes, output_fluxes, internal_fluxes) start_values = np.array([5]) times = np.linspace(0, 100, 101) smr = SmoothModelRun(srm, {}, start_values, times) x = np.array([1]) Phix = smr._state_transition_operator(91, 89, x) self.assertTrue(abs(Phix - np.exp(-2)) < 1e-03)
def test_port_controlled_Hamiltonian_representation(self): # for the test the two pool microbial model is used S, B, t, ux = symbols('S B,t,ux') V, ADD, k_B, k_SD, r, A_f, K_m, k_S = symbols( 'V,ADD,k_B,k_SD,r,A_f,K_m,k_S') state_vector = [S, B] time_symbol = Symbol('t') df = k_S * A_f * S / (S + K_m) * B output_fluxes = {0: (1 - r) * df} internal_fluxes = {(0, 1): r * df, (1, 0): k_B * B} input_fluxes = {0: ADD} rm = SmoothReservoirModel(state_vector, time_symbol, input_fluxes, output_fluxes, internal_fluxes) J, Q, C, u = rm.port_controlled_Hamiltonian_representation() self.assertEqual( J, Matrix([[0, -r * df + k_B * B], [r * df - k_B * B, 0]])) self.assertEqual(zeros(2), simplify(Q - Matrix([[(1 - r) * df, 0], [0, 0]]))) self.assertEqual(u, Matrix([ADD, 0]))
def test_function_expressions(self): C_0, C_1, C_2 = symbols('C_0 C_1 C_2') k = symbols('k') t = Symbol('t') f = Function('f') state_vector = [C_0, C_1, C_2] input_fluxes = {C_0: C_0 + 5 * f(4), C_1: 0, C_2: C_0**2 + C_1 * f(t)} output_fluxes = {C_0: k * C_0} internal_fluxes = {(C_0, C_1): k * C_0} rm = SmoothReservoirModel.from_state_variable_indexed_fluxes( state_vector, t, input_fluxes, output_fluxes, internal_fluxes) self.assertEqual(rm.function_expressions, {f(t), f(4)}) # now test with no function expressions input_fluxes = {C_0: C_0 + 5, C_1: 0, C_2: C_0**2 + C_1} rm = SmoothReservoirModel.from_state_variable_indexed_fluxes( state_vector, t, input_fluxes, output_fluxes, internal_fluxes) self.assertEqual(rm.function_expressions, set())
def setUp(self): x, y, t, k = symbols("x y t k") u_1 = Function('u_1')(x, t) state_vector = Matrix([x, y]) B = Matrix([[-1, 1.5], [k, -2]]) u = Matrix(2, 1, [u_1, 1]) srm = SmoothReservoirModel.from_B_u(state_vector, t, B, u) decay_symbol = symbols('lamda') Fa = Function('Fa')(t) self.srm_14C = SmoothReservoirModel_14C(srm, decay_symbol, Fa)
def test_JordanNormalForm(self): C_0, C_1 = symbols('C_0 C_1') k, k_0, k_1 = symbols('k,k_0,k_1') time_symbol = symbols('t') #create a Model from a compartmental_matrix in jordan form #P,J=m.jordan_form() #m,P,J B = Matrix([[-k, 0], [1, -k]]) u = Matrix([[0], [0]]) rm2 = SmoothReservoirModel.from_B_u(Matrix([C_0, C_1]), time_symbol, B, u)
def setUp(self): x, y, t, k = symbols("x y t k") u_1 = Function('u_1')(x, t) state_vector = Matrix([x, y]) B = Matrix([[-1, 1.5], [k, -2]]) u = Matrix(2, 1, [u_1, 1]) self.srm = SmoothReservoirModel.from_B_u(state_vector, t, B, u) start_values = np.array([10, 40]) t_0 = 0 t_max = 10 times = np.linspace(t_0, t_max, 11) disc_times = [5] parameter_dicts = [{k: 1}, {k: 0.5}] func_dicts = [{u_1: lambda x_14C, t: 9}, {u_1: lambda x_14C, t: 3}] pwc_mr = PWCModelRun(self.srm, parameter_dicts, start_values, times, disc_times, func_dicts) self.alpha = 0.5 start_values_14C = start_values * self.alpha def Fa_func(t): return self.alpha decay_rate = 1.0 self.pwc_mr_14C = PWCModelRun_14C(pwc_mr, start_values_14C, Fa_func, decay_rate) timess = [ np.linspace(t_0, disc_times[0], 6), np.linspace(disc_times[0], t_max, 6) ] smrs_14C = [] tmp_start_values = start_values tmp_start_values_14C = start_values_14C for i in range(len(disc_times) + 1): smr = SmoothModelRun(self.srm, parameter_dict=parameter_dicts[i], start_values=tmp_start_values, times=timess[i], func_set=func_dicts[i]) tmp_start_values = smr.solve()[-1] smrs_14C.append( SmoothModelRun_14C(smr, tmp_start_values_14C, Fa_func, decay_rate)) tmp_start_values_14C = smrs_14C[i].solve()[-1] self.smrs_14C = smrs_14C
def smooth_reservoir_model_from_input_tuple_and_matrix( u: InputTuple, B: CompartmentalMatrix, time_symbol: TimeSymbol, state_variable_tuple: StateVariableTuple, ) -> SmoothReservoirModel: return SmoothReservoirModel.from_B_u( state_vector=ImmutableMatrix(state_variable_tuple), time_symbol=time_symbol, B=B, u=ImmutableMatrix(u), )
def test_phi_cache_vals(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 = 2000 # the old way relies on the interpolation and goes wild for # small nt... 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) nr_pools = srm.nr_pools # smr.initialize_state_transition_operator_cache_2b(size=3) cache = smr._compute_state_transition_operator_cache(size=2) 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 ind, phi in enumerate(cache.values): tmin = cache.keys[ind] tmax = cache.keys[ind + 1] for x in bvs: with self.subTest(): phi_x_old = smrl._state_transition_operator_for_linear_systems( tmax, tmin, x) # noqa: E501 phi_x_mat = np.matmul(phi, x).reshape(nr_pools, ) self.assertTrue( np.allclose(phi_x_old, phi_x_mat, rtol=1e-2))
def test_is_linear(self): C_0, C_1 = symbols('C_0 C_1') state_vector = [C_0, C_1] time_symbol = Symbol('t') # test all fluxes linear internal_fluxes = {(0, 1): 5 * C_0, (1, 0): 4 * C_1} input_fluxes = {} output_fluxes = {} rm = SmoothReservoirModel(state_vector, time_symbol, input_fluxes, output_fluxes, internal_fluxes) self.assertEqual(rm.is_linear, True) # test nonlinear internal flux input_fluxes = {} output_fluxes = {} internal_fluxes = {(0, 1): 5 * C_0, (1, 0): 4 * C_1**2} rm = SmoothReservoirModel(state_vector, time_symbol, input_fluxes, output_fluxes, internal_fluxes) self.assertEqual(rm.is_linear, False) # test nonlinear output flux input_fluxes = {} output_fluxes = {0: C_0 + 5, 1: C_1 / C_0} internal_fluxes = {(0, 1): 5 * C_0, (1, 0): 4 * C_1} rm = SmoothReservoirModel(state_vector, time_symbol, input_fluxes, output_fluxes, internal_fluxes) self.assertEqual(rm.is_linear, False) # test state dependent input flux that is linear though output_fluxes = {} input_fluxes = {0: C_0 + 5, 1: 0} internal_fluxes = {(0, 1): 5 * C_0, (1, 0): 4 * C_1} rm = SmoothReservoirModel(state_vector, time_symbol, input_fluxes, output_fluxes, internal_fluxes) self.assertEqual(rm.is_linear, True) # test state dependent input flux with external functions # (to be replaced later by numeric approximations of data) # external functions of state variables are always considered # to be nonlinear # It they are not it is easy to rewrite them as a product... output_fluxes = {} u_0_expr = Function('u_0')(C_0, C_1, time_symbol) input_fluxes = {0: u_0_expr, 1: 0} internal_fluxes = {(0, 1): 5 * C_0, (1, 0): 4 * C_1} rm = SmoothReservoirModel(state_vector, time_symbol, input_fluxes, output_fluxes, internal_fluxes) self.assertEqual(rm.is_linear, False) # this time with a linear symbolic function # (that does not depend on the state) output_fluxes = {} u_0_expr = Function('u_0')(time_symbol) input_fluxes = {0: u_0_expr, 1: 0} internal_fluxes = {(0, 1): 5 * C_0, (1, 0): 4 * C_1} rm = SmoothReservoirModel(state_vector, time_symbol, input_fluxes, output_fluxes, internal_fluxes) self.assertEqual(rm.is_linear, True)
def setUp(self): x, y, t, k = symbols("x y t k") u_1 = Function('u_1')(x, t) state_vector = Matrix([x, y]) B = Matrix([[-1, 1.5], [k/(t+1), -2]]) u = Matrix(2, 1, [u_1, 1]) self.srm = SmoothReservoirModel.from_B_u( state_vector, t, B, u ) start_values = np.array([10, 40]) t_0 = 0 t_max = 10 times = np.linspace(t_0, t_max, 11) disc_times = [5] parameter_dicts = [{k: 1}, {k: 0.5*t}] func_dicts = [{u_1: lambda x, t: 9}, {u_1: lambda x, t: 3*t}] self.pwc_mr = PWCModelRun( self.srm, parameter_dicts, start_values, times, disc_times, func_dicts ) timess = [ np.linspace(t_0, disc_times[0], 6), np.linspace(disc_times[0], t_max, 6) ] smrs = [] tmp_start_values = start_values for i in range(len(disc_times)+1): smrs.append( SmoothModelRun( self.srm, parameter_dict=parameter_dicts[i], start_values=tmp_start_values, times=timess[i], func_set=func_dicts[i] ) ) tmp_start_values = smrs[i].solve()[-1] self.smrs = smrs
def test_free_symbols(self): C_0, C_1, C_2 = symbols('C_0 C_1 C_2') k = symbols('k') t = Symbol('t') f = Function('f') state_vector = [C_0, C_1, C_2] input_fluxes = {C_0: C_0 + 5 * f(4), C_1: 0, C_2: C_0**2 + C_1 * f(t)} output_fluxes = {C_0: k * C_0} internal_fluxes = {(C_0, C_1): k * C_0} rm = SmoothReservoirModel.from_state_variable_indexed_fluxes( state_vector, t, input_fluxes, output_fluxes, internal_fluxes) self.assertEqual(rm.free_symbols, {k, t, C_0, C_1})
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 smooth_reservoir_model_from_fluxes( in_fluxes: InFluxesBySymbol, out_fluxes: OutFluxesBySymbol, internal_fluxes: InternalFluxesBySymbol, time_symbol: TimeSymbol, state_variable_tuple: StateVariableTuple, ) -> SmoothReservoirModel: return SmoothReservoirModel.from_state_variable_indexed_fluxes( state_vector=list(state_variable_tuple), time_symbol=time_symbol, input_fluxes=in_fluxes, output_fluxes=out_fluxes, internal_fluxes=internal_fluxes, )
def smr_2d(nc): # two-dimensional 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([5, 3]) times = np.linspace(0, 1, 100) smr = SmoothModelRun(srm, {}, start_values, times) smr.build_state_transition_operator_cache(nc) return deepcopy(smr)
def smr_1d(nc): #one-dimensional C = Symbol('C') state_vector = [C] time_symbol = Symbol('t') input_fluxes = {} output_fluxes = {0: C} internal_fluxes = {} srm = SmoothReservoirModel(state_vector, time_symbol, input_fluxes, output_fluxes, internal_fluxes) start_values = np.array([5]) times = np.linspace(0, 1, 6) smr = SmoothModelRun(srm, {}, start_values, times) smr.build_state_transition_operator_cache(nc) return deepcopy(smr)
def setUp(self): x, y, t = symbols("x y t") state_vector = Matrix([x, y]) B = Matrix([[-1, 1.5], [0.5, -2]]) u = Matrix(2, 1, [9, 1]) srm = SmoothReservoirModel.from_B_u(state_vector, t, B, u) start_values = np.array([10, 40]) self.start_values = start_values self.t_0 = 0 self.t_max = 10 self.ntmo = 10 self.fac = 2 self.times = np.linspace(self.t_0, self.t_max, self.ntmo + 1) self.smr = SmoothModelRun(srm, {}, start_values, self.times)
def test_from_symbolic_fluxes(self): # we allow the fluxes also be indexed by the state variables # rather that their position in the statevector C_0, C_1, C_2 = symbols('C_0 C_1 C_2') k = symbols('k') state_vector = [C_0, C_1, C_2] time_symbol = Symbol('t') input_fluxes = {C_0: C_0 + 5, C_1: 0, C_2: C_0**2 + C_1} output_fluxes = {C_0: k * C_0} internal_fluxes = {(C_0, C_1): k * C_0} rm = SmoothReservoirModel.from_state_variable_indexed_fluxes( state_vector, time_symbol, input_fluxes, output_fluxes, internal_fluxes) self.assertEqual(rm.input_fluxes, {0: C_0 + 5, 1: 0, 2: C_0**2 + C_1}) self.assertEqual(rm.output_fluxes, {0: k * C_0}) self.assertEqual(rm.internal_fluxes, {(0, 1): k * C_0})
def setUp(self): x, y, t, k = symbols("x y t k") u_1 = Function('u_1')(x, t) state_vector = Matrix([x, y]) B = Matrix([[-1, 1.5], [k / (t + 1), -2]]) u = Matrix(2, 1, [u_1, 1]) self.srm = SmoothReservoirModel.from_B_u(state_vector, t, B, u) start_values = np.array([10, 40]) t_0 = 0 t_max = 10 times = np.linspace(t_0, t_max, 11) parameter_dict = {k: 1} func_dict = {u_1: lambda x, t: 9} self.smr = SmoothModelRun(self.srm, parameter_dict, start_values, times, func_dict)