Ejemplo n.º 1
0
def params(N, M, T, k, Tref, T_pk, B_U, B_R, Ma, Ea_U, Ea_R, Ea_D):
    #Uptake
    u1 = np.zeros([M, M])
    u_temp = st.temp_growth(
        k, T, Tref, T_pk, N, B_U, Ma, Ea_U,
        Ea_D)  # uptake rates and maintenance are temp-dependant
    np.fill_diagonal(u1,
                     u_temp[0:M])  # fill in temp dependant uptake on diagonals
    u2 = np.zeros([M, M])
    u3 = np.zeros([M, M])
    if N == M:
        U = u1
    elif M == N / 2:
        np.fill_diagonal(
            u2, u_temp[M:M * 2])  # fill in temp dependant uptake on diagonals
        U = np.concatenate((u1, u2), axis=0)
    else:
        np.fill_diagonal(
            u2, u_temp[M:M * 2])  # fill in temp dependant uptake on diagonals
        np.fill_diagonal(u3, u_temp[M + 1:M * 3])
        U = np.concatenate((u1, u2, u3), axis=0)

    # Respiration
    ar_rm = st.temp_resp(
        k, T, Tref, T_pk, N, B_R, Ma, Ea_R,
        Ea_D)  # find how varies with temperature (ar = arrhenius)
    # #Rm = sc.full([N], (0.3))
    R = ar_rm

    ### Calc  R as function  of U/Rm rule for competitors. Then need to back-calc Ea.

    # R_2 = np.array([])
    # if M == N:
    #     R = st.temp_resp(k, T, Tref,T_pk, N, B_r, Ma, Ea_R, Ea_D) # find how varies with temperature (ar = arrhenius)
    # elif M == N/2:
    #     R_1 = st.temp_resp(k, T, Tref,T_pk, N, B_r, Ma, Ea_R, Ea_D)[0:M] # find how varies with temperature (ar = arrhenius)
    #     for g in range(0, M):
    #         #print(g)
    #         R_1x = range(0,M) # upper diag row
    #         R_1y = range(0,M) #np.where(np.diag(U)) # upper diag column
    #         R_2x = np.array(np.where(np.diag(U[M:N]))) + M # lower diag row
    #         R_2y = range(0,M) # lower diag column
    #         R_i = U[R_2x[0,g], R_2y[g]] * R_1[g] * Meta_ratio[g]/U[R_1x[g], R_1y[g]]
    #         #print(R_i)
    #         R_2 = np.append(R_2, R_i)
    #         #print(R_2)
    #     R = np.concatenate((R_1, R_2))

    # Excretion
    l = np.zeros([M, M])
    for i in range(M - 1):
        l[i, i + 1] = 0.4
    l[M - 1, 0] = 0.4

    # External resource input
    p = np.concatenate(
        (np.array([1]), np.repeat(1, M - 1)))  #np.repeat(1, M) #np.ones(M) #
    return U, R, l, p
Ejemplo n.º 2
0
def params(N, M, T, k, Tref, T_pk, B_U, B_R, Ma, Ea_U, Ea_R, Ea_D, lf):
    '''
    Returning matrices and vectors of uptake, respiration and excretion.
    '''
    # Uptake
    # Give random uptake for each resources, sum up to the total uptake of the bacteria
    U_sum = st.temp_growth(k, T, Tref, T_pk, N, B_U, Ma, Ea_U, Ea_D)
    diri = np.transpose(np.random.dirichlet(np.ones(M), N))
    U = np.transpose(diri * U_sum)

    # Respiration
    R = st.temp_resp(k, T, Tref, T_pk, N, B_R, Ma, Ea_R,
                     Ea_D)  # find how varies with temperature (ar = arrhenius)

    # Excretion
    # SUMMING UP TO 0.4
    l_raw = np.array(
        [[np.random.normal(1 / (i), 0.005) * lf for i in range(M, 0, -1)]
         for i in range(1, M + 1)])
    l = [[l_raw[j, i] if j >= i else 0 for j in range(M)] for i in range(M)]

    return U, R, l
Ejemplo n.º 3
0
# Temperature params
Tref = 273.15 # Reference temperature Kelvin, 0 degrees C
np.random.seed(0)
pk_U = np.random.normal(25, 3, size = N)
Ma = 1 # Mass
Ea_D = np.repeat(3.5,N) # Deactivation energy - only used if use Sharpe-Schoolfield temp-dependance
k = 0.0000862 # Boltzman constant
T_pk = Tref + pk_U # Peak above Tref, Kelvin
T = 273.15 + 21 # Model temperature
Ea_U = np.round(np.random.normal(1.5, 0.01, N),3)[0:N] # Ea for uptake
Ea_R = Ea_U - 0.8 # Ea for respiration, which should always be lower than Ea_U so 'peaks' later
B_U = (10**(2.84 + (-4.96 * Ea_U))) + 4 # B0 for uptake - ** NB The '+4' term is added so B_U>> B_R, otherwise often the both consumers can die and the resources are consumed
B_R = (10**(1.29 + (-1.25 * Ea_R))) # B0 for respiration

U_sum = st.temp_growth(k, T, Tref, T_pk, N, B_U, Ma, Ea_U, Ea_D)
np.random.seed(0)
diri = np.transpose(np.random.dirichlet(np.ones(M),N))
U = np.transpose(diri*U_sum)
print(U_sum)
print(U)


for i in range(5):
    print(i)


A = np.array([[1,2,2,np.nan,np.nan,np.nan]])
A = np.nan_to_num(A, nan=0)
np.mean(A)
def ass_temp_run(t, N, M, t_n, Tref, Ma, k, ass, x0, t_fin, T_pk, Ea_D, typ):
    result_array = np.empty((0, N + M))  # Array to store data in for plotting

    for i in range(
            20, t_n
    ):  # Run model at multiple temperatures, here set to just run at 20 C
        T = 273.15 + i  # Temperature

        # Set up Ea (activation energy) and B0 (normalisation constant)
        # Based on Tom Smith's observations
        Ea_U = np.round(np.random.normal(1.5, 0.01, 10),
                        3)[0:N]  # Ea for uptake
        Ea_R = Ea_U - 0.8  # Ea for respiration, which should always be lower than Ea_U so 'peaks' later
        B_U = (
            10**(2.84 + (-4.96 * Ea_U))
        ) + 4  # B0 for uptake - ** NB The '+4' term is added so B_U>> B_R, otherwise often the both consumers can die and the resources are consumed
        B_R = (10**(1.29 + (-1.25 * Ea_R)))  # B0 for respiration

        #print('')
        #print('Temperature =',i)

        for j in range(ass):

            #print('')
            #print('Assembly = ', j)
            t = sc.linspace(
                0, t_fin - 1,
                t_fin)  # resetting 't' if steady state not reached (see below)

            #print('Uptake Ea (C1 - C5)' + str(Ea_U[0:M]))
            #print('Uptake Ea (C6 - C10)' + str(Ea_U[M:N]))
            #print('Metabolic ratio between competitors' + str(Meta_ratio))

            # Set up model
            U = par.params(N, M, T, k, Tref, T_pk, B_U, B_R, Ma, Ea_U, Ea_R,
                           Ea_D)[0]  # Uptake
            R = par.params(N, M, T, k, Tref, T_pk, B_U, B_R, Ma, Ea_U, Ea_R,
                           Ea_D)[1]  # Respiration
            l = par.params(N, M, T, k, Tref, T_pk, B_U, B_R, Ma, Ea_U, Ea_R,
                           Ea_D)[2]  # Leakage
            p = par.params(N, M, T, k, Tref, T_pk, B_U, B_R, Ma, Ea_U, Ea_R,
                           Ea_D)[3]  # Resource input
            l_sum = np.sum(l, axis=1)
            pars = (U, R, l, p, l_sum, Ea_U, Ea_R, Ea_D, N, M, T, Tref, B_R,
                    B_U, Ma, k, ass, typ, K)  # Parameters to pass onto model

            # Run model
            pops = odeint(mod.metabolic_model, y0=x0, t=t,
                          args=pars)  # Integrate
            #pops = np.round(pops, 5)

            # Steady state test
            ss_test = np.round(
                abs((pops[t_fin - 1, 0:N]) - (pops[t_fin - 50, 0:N])), 3
            )  # Find difference between last timestep and timestep at t_fin - 50 (i.e. 150 if t_fin = 200)
            while True:
                if np.any(
                        ss_test > 0
                ):  # If there is a difference then that consumer not yet reached steady state
                    t = sc.linspace(
                        0, 99, 100)  # reset t so shorter, only 100 timesteps
                    pops2 = odeint(
                        mod.metabolic_model,
                        y0=pops[t_fin - 1, :],
                        t=t,
                        args=pars
                    )  # re-run model using last timestep concentrations of consumers and resources
                    pops = np.append(
                        pops, pops2, axis=0
                    )  # append results of additional run to orginial run
                    t_fin = t_fin + 100  # adjust timesteps number
                    ss_test = np.round(
                        abs((pops[t_fin - 1, 0:N]) - (pops[t_fin - 50, 0:N])),
                        3)  # Find again if consumers reached steady state now
                elif np.all(ss_test == 0):
                    break  # Once no difference in consumer concentration then stop performing additional model runs
                else:
                    pops = pops  # If at steady state then nothing happens

            t_fin = 100

            pops = np.round(pops, 7)
            # if j == ass-1:
            #     break

            ###Assembly###

            # Find which consumers have gone extinct
            rem_find = pops[
                t_fin - 1,
                0:N]  # Get the consumer concentrations from last timestep
            ext = np.where(
                rem_find < 0.01
            )  # Find which consumers have a concentration < 0.01 g/mL, i.e. are extinct
            rem_find = np.where(
                rem_find < 0.01, 0.1, rem_find
            )  # Replace extinct consumers with a new concentration of 0.1
            x0 = np.concatenate(
                (rem_find, pops[t_fin - 1, N:N + M])
            )  # Join new concentrations for consumers with those of resources

            # Create new consumers

            # New Ea_ and Ea_R
            Ea_tmp_U = np.round(np.random.normal(1.5, 0.01, 10), 3)[0:len(
                ext[0]
            )]  # Ea for uptake cut to length(ext),i.e. the number of extinct consumers
            Ea_U[ext] = Ea_tmp_U  # Replace removed Eas with new Eas
            Ea_R = Ea_U - 0.8

            # New B0
            B_U = 10**(2.84 + (-4.96 * Ea_U)) + 4
            B_R = 10**(1.29 + (-1.25 * Ea_R))

            #print(np.round(Ea_U, 3))

            result_array = np.append(result_array, pops, axis=0)

        x0 = np.concatenate((sc.full([N], (0.1)), sc.full([M], (0.1))))

    #### Plot output ####
    t_plot = sc.linspace(0, len(result_array), len(result_array))
    plt.plot(t_plot,
             result_array[:, 0:N],
             'g-',
             label='Consumers',
             linewidth=0.7)
    plt.plot(t_plot,
             result_array[:, N:N + M],
             'b-',
             label='Resources',
             linewidth=0.7)
    plt.grid
    plt.ylabel('Population density')
    plt.xlabel('Time')
    plt.title('Consumer-Resource population dynamics')
    plt.legend([
        Line2D([0], [0], color='green', lw=2),
        Line2D([0], [0], color='blue', lw=2)
    ], ['Consumer', 'Resources'])
    plt.show()

    U_out = (st.temp_growth(k, T, Tref, T_pk, N, B_U, Ma, Ea_U, Ea_D))

    return result_array, U_out, R