Source code for smew.organic_carbon

# SPDX-License-Identifier: AGPL-3.0-only
# -*- coding: utf-8 -*-
"""
Created on Mon Dec 16 12:19:42 2019
"""
import numpy as np
from numba import njit
import smew
from smew.errors import ErrorCode, _float_text, raise_for_error


[docs] def respiration(ADD, SOC_in, CO2_air_in, ratio_aut_het, soil, s, v, k_v, Zr, temp_soil,dt,conv_mol, tau_OC=None): """Calculate respiration, reporting failures after compiled arrays are freed.""" s, v, temp_soil = (np.ascontiguousarray(a, dtype=np.float64) for a in (s, v, temp_soil)) result, error_code, error_message = _respiration( ADD, SOC_in, CO2_air_in, ratio_aut_het, soil, s, v, k_v, Zr, temp_soil, dt, conv_mol, tau_OC, ) raise_for_error(error_code, error_message) return result
@njit(nogil=True, error_model="numpy") def _respiration(ADD, SOC_in, CO2_air_in, ratio_aut_het, soil, s, v, k_v, Zr, temp_soil, dt, conv_mol, tau_OC=None): """Return (results or None, error_code, error_message) for valid inputs. Arrays must be nonempty, matching 1D float64 arrays; soil must be supported. """ # Preallocating the variables f_s = np.zeros(len(s)) f_T = np.zeros(len(s)) DEC = np.zeros(len(s)) SOC = np.zeros(len(s)) #constants MM_Mg, MM_Ca, MM_Na, MM_K, MM_Si, MM_C, MM_Anions, MM_Al = smew.MM(conv_mol) s_h, s_w, s_i, s_fc, b, K_s, n = smew.soil_const(soil) r = 0.7 # [-]: Fraction of carbon that goes into respiration CO2_atm = smew.CO2_atm(conv_mol) # [mol-conv/l] D_0 = smew.D_0() #free-air diffusion [m2/d] D = D_0*(1-s)**(10/3)*n**(4/3) #Mill-Quirk (1961) #moisture impact on decomposition for i in range(0, len(s)): if s[i]<=s_h: f_s[i] = 0 elif s[i]>s_h and s[i]<=s_w: f_s[i] = (s[i]-s_h)/(s_w-s_h) elif s[i]>s_w and s[i]<=s_i: f_s[i] = 1 elif s[i]>s_i and s[i]<=1: f_s[i] = (1-s[i])/(1-s_i) # temperature impact on decomposition; decomposition stops below 0 degC temp_active = np.maximum(temp_soil, 0.0) if np.any(temp_active > 0.0): f_T = temp_active / np.mean(temp_active[temp_active > 0.0]) #CO2 gas-diffusion baricenter if Zr <= 0.3: Z_CO2 = Zr/2 else: Z_CO2 = 0.15 # input data: SOC_in and either tau_OC or CO2_air_in # missing data (None) estimated via qs-state approximation if SOC_in is not None: SOC[0] = SOC_in # [gOC/m3] if tau_OC is not None: k_dec = 1/tau_OC # [1/d] #Fs_in = k_dec/MM_C*(r*Zr*f_s[0]*f_T[0]*SOC[0]*(1 + ratio_aut_het * v / k_v)) # [mol-conv/m2] (resp_het + resp_aut = Fs) elif CO2_air_in is not None: Fs_in = (D[0]*1000/(Z_CO2))*(CO2_air_in - CO2_atm) # [mol-conv/m2] activity0 = f_s[0] * f_T[0] * (1.0 + ratio_aut_het * v[0] / k_v) if activity0 <= 0.0: return None, ErrorCode.RESPIRATION_INITIAL_ACTIVITY.value, ( 'respiration.initial_conditions: Cannot estimate k_dec from CO2_air_in ' 'when the initial soil is frozen or biologically inactive. ' 'Provide tau_OC or start from an unfrozen active timestep.; step=0, time_days=0' + ', activity=' + _float_text(activity0) + ', s=' + _float_text(s[0]) + ', temp_soil=' + _float_text(temp_soil[0]) + ', SOC=' + _float_text(SOC[0]) ) k_dec = MM_C * Fs_in / (r * Zr * activity0 * SOC[0]) # mean decomposition activity f_dec = f_T * f_s mean_f_dec = np.mean(f_dec) if mean_f_dec <= 0.0: return None, ErrorCode.RESPIRATION_MEAN_ACTIVITY.value, ( 'respiration.steady_state: Mean decomposition activity is zero. ' 'Cannot estimate ADD or SOC from steady-state balance.' + '; mean_activity=' + _float_text(mean_f_dec) + '; mean_f_T=' + _float_text(np.mean(f_T)) + '; temp_soil_min=' + _float_text(np.min(temp_soil)) + '; temp_soil_max=' + _float_text(np.max(temp_soil)) + '; mean_f_s=' + _float_text(np.mean(f_s)) + '; soil=' + soil + '; s_h=' + _float_text(s_h) + '; s_w=' + _float_text(s_w) + '; s_i=' + _float_text(s_i) + '; s_min=' + _float_text(np.min(s)) + '; s_max=' + _float_text(np.max(s)) ) # ADD estimate for qs-equilibrium (in absence of data) # Use a numeric local rather than reassigning optional ADD, which Numba cannot type. if ADD is None and SOC[0] is not None: add_rate = r * Zr * k_dec * mean_f_dec * SOC[0] # [gOC/(m2*d)] else: add_rate = ADD # SOC estimate for qs-equilibrium (in absence of data) if SOC_in is None and ADD is not None: SOC[0] = (ADD / Zr) / (r * k_dec * mean_f_dec) # [gOC/m3] # OC equation DEC[0] = k_dec*f_T[0]*f_s[0]*SOC[0] for i in range(1, len(s)): SOC[i] = SOC[i-1]+(add_rate/Zr-r*DEC[i-1])*dt DEC[i] = k_dec*f_T[i]*f_s[i]*SOC[i] # [gOC/(m3*d)] #CO2 respiration r_het = r*DEC*Zr/MM_C #mol-conv/ m2 d r_aut = ratio_aut_het*r_het*v/k_v #if this changes, the initial equilibrium condition above must be changed return (SOC, r_het, r_aut, D), 0, "" respiration_numba = _respiration