Exemplo n.º 1
0
def get_modified_params(sp, part_name, trait_name, trait_arr, param_name, VPD):
    params_arr = np.zeros(
        (len(trait_arr), 3)
    )  # allow for three slots to fill up to three params (e.g., Emax, sw, sst)
    if trait_name == 'R': plant = sp
    for i, trait_val in enumerate(trait_arr):
        print i, trait_val
        if trait_name != 'R':
            modified_plant = initialize_modified_plant(sp, soil_type,
                                                       part_name, trait_name,
                                                       trait_val)
        if param_name == 'Es':
            func = modified_plant.get_Es_params
            params_arr[i, :] = func(VPD, s)
        elif param_name == 'sigma':
            func = modified_plant.get_sigma
            params_arr[i, :] = func(VPD, s)
        elif param_name == 'Cgain' or param_name == 'Hrisk':
            if trait_name == 'R':
                modified_plant = plant
                gam, eta, k, sw, sst, sCrit = modified_plant.get_derived_params(
                    VPD, s, alpha, n, Ks, sfc, plc=plc)
                ps, assm = simulate_ps_t(n_trajectories, tRun, dt, s0, lam,
                                         gam, eta, k, sw, sst, s1, Amax,
                                         trait_val)
            else:
                gam, eta, k, sw, sst, sCrit = modified_plant.get_derived_params(
                    VPD, s, alpha, n, Ks, sfc, plc=plc)
                ps, assm = simulate_ps_t(n_trajectories, tRun, dt, s0, lam,
                                         gam, eta, k, sw, sst, s1, Amax, Rmax)
            params_arr[i, 0] = get_psCrit(ps, sCrit)
            params_arr[i, 1] = get_relGnet(assm, Rmax)
    return params_arr
def get_iso_aniso_performance(sp, VPD, tmax_arr, iso_xf, aniso_xf, plc=0.8):
    plant = initialize_plant(sp, traits, soil_type)

    # defining what it means to be iso or aniso
    Pg12_ref = plant.get_Pg12()
    Pg12_iso = iso_xf * Pg12_ref
    Pg12_aniso = aniso_xf * Pg12_ref
    plant_iso = initialize_modified_plant(sp, soil_type, 'canopy_dict',
                                          'c_leaf',
                                          np.log(0.12) / (Pg12_iso))
    plant_aniso = initialize_modified_plant(sp, soil_type, 'canopy_dict',
                                            'c_leaf',
                                            np.log(0.12) / (Pg12_aniso))

    Aref_iso = plant_iso.canopy.gmax_canopy(0.5)
    Avpd_iso = plant_iso.canopy.gmax_canopy(VPD)
    Aref_aniso = plant_aniso.canopy.gmax_canopy(0.5)
    Avpd_aniso = plant_aniso.canopy.gmax_canopy(VPD)

    metrics_iso = np.zeros((len(tmax_arr), 2))
    metrics_aniso = np.zeros_like(metrics_iso)
    print 'start iso_aniso_performance, loop length:', len(tmax_arr)
    for i, tmax in enumerate(tmax_arr):
        print i,
        tRun = np.arange(0, tmax + dt, dt)
        ## isohydric performance
        gam, eta, k, sw, sst, sCrit = plant_iso.get_derived_params(
            VPD, s, alpha, n, Ks, sfc, plc)
        Amax = plant.canopy.Amax
        R = 0.1 * Amax
        ps, assm = simulate_ps_t(n_trajectories, tRun, dt, s0, lam, gam, eta,
                                 k, sw, sst, s1, Amax, R)
        metrics_iso[i, 0] = get_psCrit(ps, sCrit)
        metrics_iso[i, 1] = get_relGnet(assm, Amax, R) * (Avpd_iso / Aref_iso)
        #         for k in range(len(ps[:,0])):
        #             plt.plot(tRun, ps[k], lw=0.5, color='gray')
        #         plt.hlines(sw, 0, tmax); plt.hlines(sst, 0, tmax); plt.hlines(sCrit, 0, tmax, 'red')
        #         plt.show()
        ## anisohydric performance
        gam, eta, k, sw, sst, sCrit = plant_aniso.get_derived_params(
            VPD, s, alpha, n, Ks, sfc, plc)
        Amax = plant.canopy.Amax
        R = 0.1 * Amax
        ps, assm = simulate_ps_t(n_trajectories, tRun, dt, s0, lam, gam, eta,
                                 k, sw, sst, s1, Amax, R)
        metrics_aniso[i, 0] = get_psCrit(ps, sCrit)
        metrics_aniso[i, 1] = get_relGnet(assm, Amax,
                                          R) * (Avpd_aniso / Aref_aniso)


#         for k in range(len(ps[:,0])):
#             plt.plot(tRun, ps[k], lw=0.5, color='gray')
#         plt.hlines(sw, 0, tmax); plt.hlines(sst, 0, tmax); plt.hlines(sCrit, 0, tmax, 'red')
#         plt.show()
    return metrics_iso, metrics_aniso
Exemplo n.º 3
0
def plot_trajectories(plant, tmax, VPD, newfig=True, nonlinear=True):
    if newfig:
        plt.figure(figsize=(6, 8))
    tRun = np.arange(0, tmax + dt, dt)
    gam, eta, k, sw, sst, sCrit = plant.get_derived_params(VPD,
                                                           s,
                                                           alpha,
                                                           n,
                                                           Ks,
                                                           sfc,
                                                           plc=0.50)
    Amax = plant.canopy.Amax
    R = plant.canopy.R()

    if nonlinear:
        ps, assm = simulate_ps_t_nonlinearized(n_trajectories, tRun, dt, s0,
                                               plant, VPD, lam, alpha)
    else:
        ps, assm = simulate_ps_t(n_trajectories, tRun, dt, s0, lam, gam, eta,
                                 k, sw, sst, s1, Amax, R)

    ps_mean = np.mean(ps, axis=0)
    assm_cumsum = np.cumsum(assm, axis=1) / (
        (Amax - R) * np.shape(assm)[1] * dt)

    plt.figure(figsize=(5, 5))
    plt.subplot(211)
    plt.title(str(get_psCrit(ps, sCrit)))
    for i in range(len(ps)):
        plt.plot(tRun, ps[i], lw=0.2, color='darkgray')
        plt.plot(tRun[ps[i] <= sCrit],
                 ps[i][ps[i] <= sCrit],
                 lw=1.5,
                 color='k')

    plt.plot(tRun, np.ones(len(tRun)) * sst, color='red', ls=':')
    plt.plot(tRun, np.ones(len(tRun)) * sCrit, color='red', ls='--')
    plt.ylabel('Soil moisture')
    plt.subplot(212)
    plt.title(str(get_relGnet(assm, Amax, R, tmax)))
    for i in range(len(ps)):
        plt.plot(tRun, assm_cumsum[i], lw=0.2, color='darkgray')
    plt.plot(tRun, np.mean(assm_cumsum, axis=0), lw=1.5, color='k')
    plt.ylabel('Normalized net assimilation')
    plt.xlabel('Days')
    plt.tight_layout()

    plt.figure(figsize=(6, 8))
    fluxes, _ = plant.get_fluxes(VPD, ps_mean)
    plt.subplot(3, 1, 1)
    plt.title('Mean soil moisture')
    plt.plot(tRun, ps_mean, lw=1.0, color='gray')
    plt.hlines(sCrit, 0, tmax, lw=1.0, color='red')
    plt.subplot(3, 1, 2)
    plt.title('canopy conductance, m3/d')
    plt.plot(tRun, plant.canopy.g_canopy(fluxes[:, 2]), lw=1.0, color='green')
    plt.subplot(3, 1, 3)
    plt.title('xylem conductance, m3/d')
    plt.plot(tRun, plant.stem.g_stem(fluxes[:, 1]), lw=1.0, color='blue')
Exemplo n.º 4
0
def plot_trait_dependence(n_trajectories, tRun, sInit, lam, gam, eta, k, s1,
                          Amax, R):
    # use the range for juniper in sandy loam
    sst_arr = np.linspace(0.25, 0.45, 5)
    sw_arr = np.linspace(0.15, 0.22, 5)
    psCrit_grid = np.zeros((len(sst_arr), len(sw_arr)))
    Gnet_grid = np.zeros_like(psCrit_grid)
    tmax = np.max(tRun)
    for i, _ in enumerate(sst_arr):
        print i
        for j, _ in enumerate(sw_arr):
            print j,
            sw, sst = sw_arr[j], sst_arr[i]
            sCrit = sw + 0.1 * (sst - sw)
            sim_params = (lam, gam, eta, k, sw, sst, s1, Amax, R)
            ps_samples, assm_samples = simulate_ps_t(n_trajectories, tRun,
                                                     sInit, *sim_params)
            psCrit_grid[i, j] = len(ps_samples[ps_samples < sCrit]) / float(
                n_trajectories * len(tRun))
            Gnet_grid[i, j] = np.mean(np.sum(assm_samples, axis=1)) / (
                (Amax - R) * tmax)
    SW, SST = np.meshgrid(sw_arr, sst_arr)
    try:
        plt.figure(figsize=(4.8, 4.5))
        CS = plt.contour(SW, SST, Gnet_grid - psCrit_grid)
        plt.clabel(CS, fontsize=10, inline=1)
        plt.xlabel('sw')
        plt.ylabel('sst')
    except ValueError:
        print "Zeros in grid."
        pass

    try:
        plt.figure(figsize=(4.8, 4.5))
        CS = plt.contour(SW, SST, psCrit_grid)
        plt.clabel(CS, fontsize=10, inline=1)
        plt.xlabel('sw')
        plt.ylabel('sst')
        print psCrit_grid
    except ValueError:
        print "Zeros in grid."
        pass

    try:
        plt.figure(figsize=(4.8, 4.5))
        CS = plt.contour(SW, SST, Gnet_grid)
        plt.clabel(CS, fontsize=10, inline=1)
        plt.xlabel('sw')
        plt.ylabel('sst')
        print Gnet_grid
    except ValueError:
        print "Zeros in grid."
        pass
Exemplo n.º 5
0
def evaluate_model(args):
    param_values, VPD, tmax = args
    Y = np.zeros((len(param_values), 2))
    tRun = np.arange(0, tmax + dt, dt)
    for i, p_vals in enumerate(param_values):
        plant_traits = p_vals[:-1]
        R = p_vals[-1]
        generic_plant = initialize_generic_plant(var_names, plant_traits,
                                                 soil_type)
        gam, eta, k, sw, sst, sCrit = generic_plant.get_derived_params(VPD,
                                                                       s,
                                                                       alpha,
                                                                       n,
                                                                       Ks,
                                                                       sfc,
                                                                       plc=plc)
        ps, assm = simulate_ps_t(n_trajectories, tRun, dt, s0, lam, gam, eta,
                                 k, sw, sst, s1, Amax, R)
        Y[i, 0] = get_psCrit(ps, sCrit)
        Y[i, 1] = get_relGnet(assm, R)
        print i, Y[i]
    return Y
def simulate_intensity_duration(plant, int_range, dur_range):
    Aref = plant.canopy.gmax_canopy(1.0)
    psCrit_grid = np.zeros((len(int_range), len(dur_range)))
    Gnet_grid = np.zeros_like(psCrit_grid)
    for i, vpd in enumerate(int_range):  # on y-axis of contour
        print i
        for j, tmax in enumerate(dur_range):  # on x-axis of contour
            print j,
            # simulation params
            tRun = np.arange(0, tmax + dt, dt)
            # environmental, units in meters
            Avpd = plant.canopy.gmax_canopy(vpd)
            gam, eta, k, sw, sst, sCrit = plant.get_derived_params(
                vpd, s, alpha, n, Ks, sfc)
            Amax = plant.canopy.Amax
            R = 0.1 * Amax
            ps, assm = simulate_ps_t(n_trajectories, tRun, dt, s0, lam, gam,
                                     eta, k, sw, sst, s1, Amax, R)
            # probability of being below sCrit over season
            psCrit_grid[i, j] = get_psCrit(ps, sCrit)
            # normalized assm with respect to max net assimilation
            Gnet_grid[i, j] = get_relGnet(assm, Amax, R) * (Avpd / Aref)
    return psCrit_grid, Gnet_grid
Exemplo n.º 7
0
    params = pickle.load(handle)
count1 = []
count2 = []
print 'total number of params:', len(params)
for i, p_vals in enumerate(params):
    print i
    plant_traits = p_vals[:-1]; R = p_vals[-1]
    generic_plant = initialize_generic_plant(var_names, plant_traits, soil_type)
#     (ET, P_stem, P_leaf), Psoil = generic_plant.get_fluxes_scalar(VPD, s_sat)
#     if (P_leaf<-3.0) :  ## Criteria 1
#         count1 += 1
#         print i, P_leaf
    
    # Check criteria 2: mean soil moisture, assimilation/stomatal conductance did not decrease during dry down
    gam,eta,k,sw,sst,sCrit=generic_plant.get_derived_params(VPD, s, alpha, n, Ks, sfc, plc=plc)
    ps, assm = simulate_ps_t(n_trajectories, tRun, dt, s0, lam, gam, eta, k, sw, sst, s1, Amax, R)
    
    assm_t = np.mean(assm+R,axis=0)
    s_t = np.mean(ps,axis=0)
    gs = np.zeros(len(s_t))
    for j, si in enumerate(s_t): 
        (ET, P_stem, P_leaf), Psoil = generic_plant.get_fluxes_scalar(VPD, si)
        gs[j] = generic_plant.canopy.g_canopy(P_leaf, VPD)
        
    gs_re = np.reshape(gs[1:], (tmax,10))
    assm_re = np.reshape(assm_t[1:], (tmax,10))
    assm_gs_ratio = np.mean(assm_re, axis=1)/np.mean(gs_re,axis=1)
    if (np.argmax(assm_gs_ratio) < 0.9*len(assm_gs_ratio)) and (assm_gs_ratio[-1]<0.8*(np.max(assm_gs_ratio)-np.min(assm_gs_ratio))):
        count2.append(i)
        print i, np.argmax(assm_gs_ratio)
        plt.plot(assm_gs_ratio); plt.plot(np.mean(assm_re, axis=1)); plt.plot(np.mean(gs_re, axis=1))
Exemplo n.º 8
0
def plot_environment_contours(sInit,
                              lam,
                              gam,
                              eta,
                              k,
                              sw,
                              sst,
                              s1,
                              Amax,
                              R,
                              int_arr,
                              int_option='eta'):
    tmax_arr = np.linspace(30, 180, 10)
    psCrit_grid = np.zeros((len(int_arr), len(tmax_arr)))
    Gnet_grid = np.zeros_like(psCrit_grid)

    for i, intensity in enumerate(int_arr):  # on y-axis of contour
        print i,
        if int_option == 'lam':
            sim_params = (intensity, gam, eta, k, sw, sst, s1, Amax, R)
            DI = (gam * eta) / intensity
        if int_option == 'eta':
            sim_params = (lam, gam, intensity, k, sw, sst, s1, Amax, R)
            DI = (gam * intensity) / lam
        print DI
        for j, tmax in enumerate(tmax_arr):  # on x-axis of contour
            print j,
            tRun = np.arange(0, tmax + dt, dt)
            ps_samples, assm_samples = simulate_ps_t(n_trajectories, tRun, dt,
                                                     sInit, *sim_params)
            # probability of being below sCrit over season
            psCrit_grid[i, j] = len(ps_samples[ps_samples < sCrit]) / float(
                n_trajectories * len(tRun))
            # normalized assm with respect to max net assimilation
            Gnet_grid[i, j] = np.mean(np.sum(assm_samples, axis=1)) / (
                (Amax - R) * tmax)

    TMAX, INT = np.meshgrid(tmax_arr, int_arr)
    try:
        plt.figure(figsize=(4.8, 4.5))
        CS = plt.contour(TMAX, INT, psCrit_grid)
        plt.clabel(CS, fontsize=10, inline=1)
        if int_option == 'lam': plt.gca().invert_yaxis()
    except ValueError:
        print "Zeros in grid."
        pass

    try:
        plt.figure(figsize=(4.8, 4.5))
        CS = plt.contour(TMAX, INT, Gnet_grid)
        plt.clabel(CS, fontsize=10, inline=1)
        if int_option == 'lam': plt.gca().invert_yaxis()
    except ValueError:
        print "Zeros in grid."
        pass

    try:
        plt.figure(figsize=(4.8, 4.5))
        CS = plt.contour(TMAX, INT, Gnet_grid - psCrit_grid)
        plt.clabel(CS, fontsize=10, inline=1)
        if int_option == 'lam': plt.gca().invert_yaxis()
    except ValueError:
        print "Zeros in grid."
        pass
Exemplo n.º 9
0
def plot_metrics_param_dependence(plant, VPD, tmax, params='beta'):
    '''metrics: (both are plotted)
    hrisk: hydraulic risk
    assim: carbon assimilation
    params: 
    alpha (structual/morphological traits), 
    beta (risk aversity traits), 
    kappa (transport efficiency),
    sigma (ratio of differential to predawn)
    
    '''
    # setting up parameter ranges
    shift_range = np.linspace(0.1, 2.0, 10)
    sp = plant.species
    # getting plant traits
    P50 = plant.stem.P50_stem
    c_leaf = plant.canopy.c_leaf
    # Pg12 = 1.0/c_leaf * np.log(0.12)
    A_canopy = plant.canopy.A_canopy
    Zr = plant.soil_root.L_root
    Gs_leaf = plant.canopy.Gs_leaf
    A_root = plant.soil_root.A_root
    Ksat_stem = plant.stem.Ksat_stem
    # constructing range of traits
    P50_arr = shift_range * P50
    Acan_arr = shift_range * A_canopy
    Zr_arr = shift_range * Zr
    Gs_arr = shift_range * Gs_leaf
    R_arr = shift_range * Rmax
    Ksat_arr = shift_range * Ksat_stem
    # set up simulation
    param_ranges = np.array([
        P50_arr, c_leaf / shift_range, Acan_arr, Zr_arr, Gs_arr, R_arr,
        Ksat_arr
    ])
    part_names = np.array([
        'stem_dict', 'canopy_dict', 'canopy_dict', 'root_dict', 'canopy_dict',
        '', 'stem_dict'
    ])
    trait_names = np.array([
        'P50_stem', 'c_leaf', 'A_canopy', 'L_root', 'Gs_leaf', 'R', 'Ksat_stem'
    ])
    colors = np.array(
        ['orange', 'blue', 'green', 'brown', 'purple', 'black', 'red'])
    # select subset under investigation
    if params == 'alpha': ip = [3, 2]
    elif params == 'beta': ip = [0, 1]
    elif params == 'kappa': ip = [4]
    elif params == 'phi': ip = [5]
    elif params == 'sigma': ip = [0, 1, 2, 3, 4, 5]
    plt.figure(figsize=(10, 4))
    for part_name, trait_name, param_range, color in zip(
            part_names[ip], trait_names[ip], param_ranges[ip], colors[ip]):
        print trait_name, (param_range[0], param_range[-1])
        param_arr = np.zeros(len(param_range))
        metric_arr = np.zeros((len(param_range), 2))
        for i, trait_val in enumerate(param_range):
            print i,
            if trait_name == 'R':
                R = trait_val
                modified_plant = plant
            else:
                R = Rmax
                modified_plant = initialize_modified_plant(
                    sp, soil_type, part_name, trait_name, trait_val)
            gam, eta, k, sw, sst, sCrit = modified_plant.get_derived_params(
                VPD, s, alpha, n, Ks, sfc, plc=plc)
            ps, assm = simulate_ps_t(n_trajectories, tRun, dt, s0, lam, gam,
                                     eta, k, sw, sst, s1, Amax, R)
            if params == 'sigma':
                param_arr[i] = modified_plant.get_sigma(
                    VPD,
                    np.mean(ps, axis=0)[::10])
            elif params == 'beta':
                param_arr[i] = modified_plant.get_Pg12(
                ) / modified_plant.stem.P50_stem
            elif params == 'alpha':
                param_arr[i] = modified_plant.canopy.A_canopy / (
                    modified_plant.soil_root.L_root *
                    modified_plant.soil_root.A_root)
            elif params == 'kappa':
                param_arr[i] = modified_plant.canopy.Gs_leaf / (
                    0.001 * modified_plant.stem.Ksat_stem)
            elif params == 'phi':
                param_arr[i] = R / Amax
            # find performance metrics
            metric_arr[i, 0] = get_psCrit(ps, sCrit)
            metric_arr[i, 1] = get_relGnet(assm, R)


#             fluxes, _ = modified_plant.get_fluxes(VPD, np.mean(ps, axis=0)[::10])
#             plt.plot(fluxes[:,0])
#             plt.plot(np.mean(ps, axis=0)[::10], label=i)
#             plt.hlines(modified_plant.get_sCrit(plc), 0, tmax)
        plt.subplot(1, 2, 1)
        plt.plot(param_arr,
                 metric_arr[:, 0],
                 'o',
                 color=color,
                 label=trait_name)
        plt.ylabel('Hydraulic risk')
        plt.xlabel(params)
        plt.subplot(1, 2, 2)
        plt.plot(param_arr,
                 metric_arr[:, 1],
                 'o',
                 color=color,
                 label=trait_name)
        plt.ylabel('Carbon assimilation')
        plt.xlabel(params)
        plt.legend()
        plt.tight_layout()