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
def test_respiration(self): # The respiration is a function of the Matrix B = A*K # here we test that this leads to the same result as yuanyuan's original version # which computes it directly from the parameters def respiration_from_params(pa, X): lig_leaf = pa.lig_leaf f41 = pa.f_leaf2metlit f42 = pa.f_root2metlit f51 = 1 - f41 f52 = 1 - f42 f63 = 1 f74 = 0.45 f75 = 0.45 * (1 - lig_leaf) f85 = 0.7 * lig_leaf f86 = 0.4 * (1 - pa.lig_wood) f96 = 0.7 * pa.lig_wood f87 = (0.85 - 0.68 * (pa.clay + pa.silt)) * (0.997 - 0.032 * pa.clay) f97 = (0.85 - 0.68 * (pa.clay + pa.silt)) * (0.003 + 0.032 * pa.clay) f98 = 0.45 * (0.003 + 0.009 * pa.clay) temp = [ pa.k_leaf, pa.k_root, pa.k_wood, pa.k_metlit, pa.k_metlit / (5.75 * np.exp(-3 * pa.lig_leaf)), pa.k_metlit / 20.6, pa.k_mic, pa.k_slowsom, pa.k_passsom ] K = np.zeros(81).reshape([9, 9]) for i in range(0, 9): K[i][i] = temp[i] co2_rate = np.array([ 0, 0, 0, (1 - f74) * K[3, 3], (1 - f75 - f85) * K[4, 4], (1 - f86 - f96) * K[5, 5], (1 - f87 - f97) * K[6, 6], (1 - f98) * K[7, 7], K[8, 8] ]) #.reshape(9,1) return np.sum(co2_rate * X.reshape(1, 9)) Xnt = self.x_init X = np.array(Xnt).reshape(9, 1) orh = respiration_from_params(self.mpa, X) B_func = make_compartmental_matrix_func(self.mpa) B = B_func(0, self.x_init) nrh = respiration_from_compartmental_matrix(B, X) self.assertTrue(np.allclose(orh - nrh, np.zeros((9, 1))))