示例#1
0
    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 = {}
        output_fluxes = {}
        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("reservoir_model_plot.pdf")
示例#2
0
    def test_linearize_piecewise(self):
        # Atmosphere, Terrestrial Carbon and Surface layer
        C_A, C_T, C_S = symbols('C_A C_T C_S')

        # equilibrium contents
        A_e, T_e, S_e = symbols('A_e T_e S_e')

        # equilibrium fluxes
        F_0, F_1, F_2 = symbols('F_0 F_1 F_2')

        # nonlinear coefficients
        alpha, beta = symbols('alpha beta')

        # external flux from surface layer to deep ocean
        F_ex = F_0 * C_S / S_e

        # fossil fuel inputs
        u_A = symbols('u_A')

        #########################################

        state_vector = Matrix([C_A, C_T, C_S])
        time_symbol = symbols('tau')

        input_fluxes = {0: u_A, 1: 0, 2: F_0}
        output_fluxes = {
            0: Piecewise((1, time_symbol < 0), (0, True)),
            1: 0,
            2: F_0 * C_S / S_e
        }
        internal_fluxes = {
            (0, 1): F_2 * (C_A / A_e)**alpha,  # A --> T
            (0, 2): F_1 * C_A / A_e,  # A --> S
            (1, 0): F_2 * C_T / T_e,  # T --> A
            (2, 0): F_1 * (C_S / S_e)**beta
        }  # S --> A
        nonlinear_srm = SmoothReservoirModel(state_vector, time_symbol,
                                             input_fluxes, output_fluxes,
                                             internal_fluxes)

        A_eq, T_eq, S_eq = (700.0, 3000.0, 1000.0)
        par_dict = {
            A_e: A_eq,
            T_e: T_eq,
            S_e: S_eq,  # equilibrium contents in Pg
            F_0: 45.0,
            F_1: 100.0,
            F_2: 60.0,  # equilibrium fluxes in PgC/yr
            alpha: 0.2,
            beta: 10.0
        }  # nonlinear coefficients

        # fossil fuel inputs
        par_dict[u_A] = 0

        # initialize model run
        times = np.linspace(0, 10, 101)
        start_values = np.array([A_eq, T_eq, S_eq])
        nonlinear_smr = SmoothModelRun(nonlinear_srm, par_dict, start_values,
                                       times)
def minimal(symbs):
    x,t,k=symbs 
    inputs={0:1}
    outputs={0:-x*k}
    internal_fluxes={}
    mod=SmoothReservoirModel([x],t,inputs,outputs,internal_fluxes)
    return(mod)
示例#4
0
    def test_age_moment_system(self):
        x, y, t = symbols("x y t")
        state_vector = Matrix([x, y])
        A = Matrix([[-1, 0], [0, -2]])
        u = Matrix(2, 1, [9, 1])
        srm = SmoothReservoirModel.from_A_u(state_vector, t, A, 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]]))
示例#5
0
    def test_Au_matrices_to_fluxes_and_back(self):
        # f = u + xi*A*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])
        A = gamma * Matrix([[-k_1, a_12, a_13], [a_21, -k_2, a_23],
                            [a_31, a_32, -k_3]])
        rm = SmoothReservoirModel.from_A_u(C, t, A, u)
        self.assertEqual(rm.input_fluxes, {0: u_1, 1: u_2, 2: u_3})
        self.assertEqual(
            rm.output_fluxes, {
                0: gamma * (k_1 - a_21 - a_31) * C_1,
                1: gamma * (k_2 - a_12 - a_32) * C_2,
                2: gamma * (k_3 - a_13 - a_23) * C_3
            })

        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
        A2 = rm.compartmental_matrix
        u2 = rm.external_inputs
        self.assertEqual(u, u2)
        self.assertEqual(A, A2)
def emanuel(symbs):
    u_1, u_3, x_1, x_2, x_3, x_4, x_5, t, F_1, F_2, F_3, F_4, F_5, F_21, F_41, F_42, F_52, F_43, F_53, F_54 = symbs
    x = Matrix(5,1,[x_1, x_2, x_3, x_4, x_5])
    u = (1+sin(t*pi*2)/2)*Matrix(5, 1, [u_1, 0, u_3, 0, 0])
    
    A  = (1+cos(t*pi*2))*Matrix([[-F_1,        0,       0,       0,        0],
                                 [F_21,     -F_2,       0,       0,        0],
                                 [   0,        0,    -F_3,       0,        0],
                                 [F_41,     F_42,    F_43,    -F_4,        0],
                                 [   0,     F_52,    F_53,    F_54,     -F_5]])
    srm = SmoothReservoirModel.from_A_u(x,t,A,u)
    return srm
示例#7
0
    def test_mean_age_system(self):
        x, y, t = symbols("x y t")
        state_vector = Matrix([x, y])
        A = Matrix([[-1, 0], [0, -2]])
        u = Matrix(2, 1, [9, 1])
        srm = SmoothReservoirModel.from_A_u(state_vector, t, A, u)

        extended_state, extended_rhs = srm.mean_age_system
        x_meanage, y_meanage = symbols('x_meanage y_meanage')
        self.assertEqual(extended_state,
                         Matrix([[x], [y], [x_meanage], [y_meanage]]))
        self.assertEqual(
            extended_rhs,
            Matrix([[-x + 9], [-2 * y + 1], [1 - 9 * x_meanage / x],
                    [1 - y_meanage / y]]))
示例#8
0
    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 nonlinear_two_pool(symbs):
    t, k_01, k_10, k_0o, k_1o = symbs
    C_0, C_1 = symbols("C_0 C_1")
    state_variables = [C_0, C_1] # order is important
    inputs = {
        0: sin(t)+2, # input to pool 0
        1: cos(t)+2  # input to pool 1
        }
    outputs={
        0: k_0o*C_0**3, # output from pool 0
        1: k_1o*C_1**3  # output from pool 0
        }
    internal_fluxes={
        (0,1): k_01*C_0*C_1**2, # flux from pool0  to pool 1
        (1,0): k_10*C_0*C_1 # flux from pool1  to pool 0
        }
    time_symbol = t
    srm = SmoothReservoirModel(state_variables, time_symbol,
                               inputs,outputs,internal_fluxes,
                               content_unit="kg C", time_unit="yr")
    return srm
def critics(symbs):
    t, k_01,k_10,k_0o,k_1o = symbs 
    x_0,x_1 = symbols("x_0 x_1")
    state_variables = [x_0, x_1] # order is important
    s1 = 10
    s2 = 20
    t1 = 20
    k = 1
    inputs = {
        0: s1+1/2*(1-tanh(-k*(t-t1))*(s2-s1))
        }
    outputs = {
        0: k_0o*x_0, # output from pool 0
        1: k_1o*x_1*(1+sin(t/2)) #output from pool 0
        }
    internal_fluxes = {
        (0,1):k_01*x_0 # flux from pool0  to pool 1
        }
    time_symbol = t
    srm = SmoothReservoirModel(state_variables, time_symbol, 
                             inputs,outputs, internal_fluxes, 
                             content_unit="kg C", time_unit="yr")
    return srm
示例#11
0
    def test_NTu_matrices_to_fluxes_and_back(self):
        # f = xi*T*N*C + u
        t, C_1, C_2, C_3, gamma, k_1, k_2, k_3, t_12, t_13, t_21, t_23, t_31, t_32, u_1, u_2, u_3, xi \
            = symbols('t C_1 C_2 C_3 gamma k_1 k_2 k_3 t_12 t_13 t_21 t_23 t_31 t_32 u_1 u_2 u_3 xi')
        C = Matrix(3, 1, [C_1, C_2, C_3])
        u = Matrix(3, 1, [u_1, u_2, u_3])
        xi = gamma
        T = Matrix([[-1, t_12, t_13], [t_21, -1, t_23], [t_31, t_32, -1]])
        N = diag(k_1, k_2, k_3)
        A = gamma * T * N

        rm = SmoothReservoirModel.from_A_u(C, t, A, u)

        self.assertEqual(rm.input_fluxes, {0: u_1, 1: u_2, 2: u_3})
        self.assertEqual(
            rm.output_fluxes, {
                0: gamma * k_1 * (1 - t_21 - t_31) * C_1,
                1: gamma * k_2 * (1 - t_12 - t_32) * C_2,
                2: gamma * k_3 * (1 - t_13 - t_23) * C_3
            })
        self.assertEqual(
            rm.internal_fluxes, {
                (0, 1): gamma * t_21 * k_1 * C_1,
                (0, 2): gamma * t_31 * k_1 * C_1,
                (1, 0): gamma * t_12 * k_2 * C_2,
                (1, 2): gamma * t_32 * k_2 * C_2,
                (2, 0): gamma * t_13 * k_3 * C_3,
                (2, 1): gamma * t_23 * k_3 * C_3
            })

        # test backward conversion to matrices
        xi2, T2, N2, C2, u2 = rm.xi_T_N_u_representation
        self.assertEqual(xi, xi2)
        self.assertEqual(u, u2)
        self.assertEqual(T, T2)
        self.assertEqual(N, N2)
示例#12
0
    def test_internal_flux_type(self):
        # test simple cases
        C_0, C_1 = symbols('C_0 C_1')
        state_vector = [C_0, C_1]
        time_symbol = Symbol('t')
        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.internal_flux_type(0, 1), 'linear')
        self.assertEqual(rm.internal_flux_type(1, 0), 'nonlinear')

        # (1,0): 4 is considered to be nonlinear : in A the corresponding entry is 4/C_1
        internal_fluxes = {(0, 1): C_0 + 5, (1, 0): C_1 / C_0}

        rm = SmoothReservoirModel(state_vector, time_symbol, input_fluxes,
                                  output_fluxes, internal_fluxes)

        self.assertEqual(rm.internal_flux_type(0, 1),
                         'no substrate dependence')
        self.assertEqual(rm.internal_flux_type(1, 0), 'nonlinear')
示例#13
0
from sympy import symbols, sin, Symbol, cos

from bgc_md.SmoothReservoirModel import SmoothReservoirModel
from bgc_md.SmoothModelRun import SmoothModelRun

if __name__ == '__main__':

    # two-dimensional model
    C_0, C_1 = symbols('C_0 C_1')
    state_vector = [C_0, C_1]
    t = Symbol('t')
    input_fluxes = {0: 2 + 1 * sin(1 / 4 * t), 1: 3}
    output_fluxes = {0: 1 / 6 * C_0}
    internal_fluxes = {(0, 1): 1 / 4 * C_0, (1, 0): 1 / 4 * C_1}
    srm = SmoothReservoirModel(state_vector, t, input_fluxes, output_fluxes,
                               internal_fluxes)

    # start values, time frame
    start_values = (1, 3)
    times = np.linspace(0, 1000, 1001)
    smr = SmoothModelRun(srm, {}, start_values, times)

    # start ages and age frame
    ages = np.linspace(0, 200, 201)
    start_age_densities = lambda a: np.exp(-a) * np.array(start_values)
    start_mean_ages = (1, 1)

    # plot a figure for comparison
    #fig = plt.figure(figsize=(100,5))
    #stretch = {'x': 1.3, 'y': 1.3, 'z': 1}
    #smr.plot_age_densities(fig, [0, 1, 'system'], start_age_densities, ages, start_mean_ages, stretch)