Пример #1
0
def make_daily_iterator_sym(
        V_init,
        mpa,
        func_dict
    ):
        from bgc_md2.models.cable_yuanyuan.source import mvs 
        B_func, u_func = make_B_u_funcs(mvs,mpa,func_dict)  
        
        def f(it,V):
            X = V[0:9]
            co2 = V[9]
            b = u_func(it,X)
            B = B_func(it,X)
            X_new = X + b + B@X

            # we also compute the respired co2 in every (daily) timestep
            # and use this part of the solution later to sum up the monthly amount
            co2_new = -np.sum(B @ X) # fixme add computer for respirattion
            
            V_new = np.concatenate((X_new,np.array([co2_new]).reshape(1,1)), axis=0)
            return V_new
    
        return TimeStepIterator2(
                initial_values=V_init,
                f=f#,
                #max_it=max(day_indices)+1
        )
Пример #2
0
def make_daily_iterator_sym(mvs, V_init: StartVector, par_dict, func_dict):
    B_func, u_func = make_B_u_funcs_2(mvs, par_dict, func_dict)
    sv = mvs.get_StateVariableTuple()
    n = len(sv)
    # build an array in the correct order of the StateVariables which in our case is already correct
    # since V_init was built automatically but in case somebody handcrafted it and changed
    # the order later in the symbolic formulation....
    V_arr = np.array([V_init.__getattribute__(str(v))
                      for v in sv] + [V_init.rh]).reshape(
                          n + 1, 1)  #reshaping is neccessary for matmux

    def f(it, V):
        X = V[0:n]
        b = u_func(it, X)
        B = B_func(it, X)
        outfluxes = B @ X
        X_new = X + b + outfluxes
        # we also compute the autotrophic and heterotrophic respiration in every (daily) timestep
        #ra= -np.sum(outfluxes[0:3]) # check with  mvs.get_StateVariableTuple()[0:3]
        rh = -(outfluxes[3:n]).reshape(
            (n - 3, )).sum()  # check with  mvs.get_StateVariableTuple()[3:9]
        V_new = np.concatenate((X_new.reshape(n, 1), rh.reshape(1, 1)), axis=0)
        return V_new

    return TimeStepIterator2(
        initial_values=V_arr,
        f=f,
    )
Пример #3
0
def monthly_matrix_simu(mpa, Xnt, npp, ns):
    # here we want to store only monthly values
    # although the computation takes place on a dayly basis
    # Construct b vector for allocation
    b_func = construct_allocation_vector_func(mpa)
    # Now construct B matrix B=A*K
    B_func = construct_matrix_func(mpa)

    def f(it, V):
        X = V[0:9]
        co2 = V[9]
        b = b_func(it, X)
        B = B_func(it, X)
        X_new = X + b * npp[it] + B @ X
        # here we add the co2 up in every step which allows us not to save it
        #co2_new = co2 + respiration_from_compartmental_matrix(B,X) #accumulate respiration
        co2_new = respiration_from_compartmental_matrix(
            B, X)  #accumulate respiration
        V_new = np.concatenate((X_new, np.array([co2_new]).reshape(1, 1)),
                               axis=0)
        return V_new

    X_0 = np.array(Xnt).reshape(9, 1)  #transform the tuple to a matrix

    co2_0 = np.array([0]).reshape(1, 1)

    day_indices = month_2_day_index(ns)
    tsi = TimeStepIterator2(initial_values=np.concatenate((X_0, co2_0),
                                                          axis=0),
                            f=f,
                            max_it=max(day_indices) + 1)

    #values = [ np.transpose(val) for val in tsi if tsi.i in day_indices]
    #values =  [v for i, v in enumerate(tsi) if i in day_indices]
    def g(acc, el):
        xs, co2s, acc_co2 = acc
        i, v = el
        d_pools = v[0:-1, :]
        d_co2 = v[-1:, :]
        acc_co2 += d_co2
        if i in day_indices:
            xs += [d_pools]
            co2s += [acc_co2]
            acc_co2 = np.array([0.0]).reshape(1, 1)

        acc = (xs, co2s, acc_co2)
        return acc

    xs, co2s, _ = reduce(g, enumerate(tsi), ([], [], 0))

    def h(tup):
        x, co2 = tup
        return np.transpose(np.concatenate([x, co2]))

    values_with_monthly_co2 = [v for v in map(h, zip(xs, co2s))]
    RES = np.concatenate(values_with_monthly_co2, axis=0)
    return RES
Пример #4
0
def make_daily_iterator(V_init, mpa):

    # Construct npp(day)
    # in general this function can depend on the day i and the state_vector X
    # e.g. typically the size fo X.leaf...
    # In this case it only depends on the day i
    def npp_func(day, X):
        return mpa.npp[day_2_month_index(day)]

    # b (b vector for partial allocation)
    beta_wood = 1 - mpa.beta_leaf - mpa.beta_root
    b = np.array([mpa.beta_leaf, mpa.beta_root, beta_wood, 0, 0, 0, 0, 0, 0
                  ], ).reshape(9, 1)
    # Now construct B matrix B=A*K
    B_func = make_compartmental_matrix_func(mpa=mpa, )

    # Build the iterator which is the representation of a dynamical system
    # for looping forward over the results of a difference equation
    # X_{i+1}=f(X_{i},i)
    # This is the discrete counterpart to the initial value problem
    # of the continuous ODE
    # dX/dt = f(X,t) and initial values X(t=0)=X_0

    def f(it, V):
        X = V[0:9]
        co2 = V[9]
        npp = npp_func(it, X)
        B = B_func(it, X)
        X_new = X + npp * b + B @ X

        # we also compute the respired co2 in every (daily) timestep
        # and use this part of the solution later to sum up the monthly amount
        co2_new = respiration_from_compartmental_matrix(B, X)

        V_new = np.concatenate((X_new, np.array([co2_new]).reshape(1, 1)),
                               axis=0)
        return V_new

    #X_0 = np.array(x_init).reshape(9,1) #transform the tuple to a matrix
    #co2_0 = np.array([0]).reshape(1,1)
    #V_0 = np.concatenate((X_0, co2_0), axis=0)

    return TimeStepIterator2(
        initial_values=V_init,
        f=f,
        #max_it=max(day_indices)+1
    )
Пример #5
0
def make_daily_iterator_sym(
        mvs,
        V_init: StartVector,
        par_dict,
        func_dict
    ):
    B_func, u_func = make_B_u_funcs_2(mvs,par_dict,func_dict)  
    sv=mvs.get_StateVariableTuple()
    n=len(sv)
    # build an array in the correct order of the StateVariables which in our case is already correct 
    # since V_init was built automatically but in case somebody handcrafted it and changed
    # the order later in the symbolic formulation....
    V_arr=np.array(
        [V_init.__getattribute__(str(v)) for v in sv]+
        [V_init.ra,V_init.rh]
    ).reshape(n+2,1) #reshaping is neccessary for matmul
    
    def f(it,V):
        X = V[0:n]
        b = u_func(it,X)
        B = B_func(it,X)
        X_new = X + b + B @ X
        # we also compute the autotrophic and heterotrophic respiration in every (daily) timestep
        
        ra = np.sum(
            [
              numOutFluxesBySymbol[k](0,*X_0) 
                for k in [C_leaf,C_wood,C_root] 
                if k in numOutFluxesBySymbol.keys()
            ]
        )
        rh = np.sum(
            [
                numOutFluxesBySymbol[k](0,*X_0) 
                for k in [C_leaf_litter,C_wood_litter,C_root_litter,C_soil_fast,C_soil_slow,C_soil_passive] 
                if k in numOutFluxesBySymbol.keys()
            ]
        )
        V_new = np.concatenate((X_new.reshape(n,1),np.array([ra,rh]).reshape(2,1)), axis=0)
        return V_new
    
    return TimeStepIterator2(
        initial_values=V_arr,
        f=f,
    )