Пример #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="x_1"></param>
        /// <param name="x_2"></param>
        /// <param name="x_3"></param>
        /// <param name="x_4"></param>
        /// <param name="x_5"></param>
        /// <param name="layer"></param>
        /// <param name="canopy"></param>
        /// <returns></returns>
        public static double CalcAssimilation(SunlitShadedCanopy s, double x_1, double x_2, double x_3, double x_4, double x_5, double x_6, double x_7, double p, double q, int layer, LeafCanopy canopy)
        {
            double a, b, c, d;

            double g_m  = s.gm_CO2T[layer];
            double g_bs = 1;
            double α    = 0;
            double R_d  = s.RdT[layer];
            double γ_   = s.G_[layer];
            double O_m  = s.Om[layer];
            double R_m  = s.Rm[layer];

            double C_a = canopy.Ca;
            double g_s = s.GsCO2[layer];
            double g_b = s.GbCO2[layer];


            a = -1 * (p * g_bs + p * x_4 * x_6 + α / (0.047 * g_bs) * g_bs * x_2 * x_7 * R_d + α / (0.047 * g_bs) * g_bs * x_1 * x_7 * γ_ -
                      q * g_bs * R_d + q * g_bs * x_1 + g_bs * O_m * x_2 + g_bs * x_3 - x_6 * R_m - q * x_4 * x_6 * R_d - x_6 * R_d + q * x_1 * x_4 * x_6 +
                      x_1 * x_6 + x_5 * x_6);
            d = -α / (0.047 * g_bs) * g_bs * x_2 * x_7 + q * g_bs + q * x_4 * x_6 + x_6;
            b = -d * (p * g_bs * R_d - p * g_bs * x_1 + p * x_4 * x_6 * R_d - p * x_1 * x_4 * x_6 + g_bs * x_2 * O_m * R_d + g_bs * x_3 * R_d +
                      g_bs * x_1 * γ_ * O_m - x_6 * R_m * R_d + x_1 * x_6 * R_m + x_5 * x_6 * R_d - x_1 * x_5 * x_6);
            c = -a;

            return(s.SolveQuadratic(a, b, c, d)); //Eq (A55)
        }
Пример #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="canopy"></param>
        /// <param name="layer"></param>
        /// <param name="mode"></param>
        /// <returns></returns>
        public static double CalcAc(SunlitShadedCanopy s, LeafCanopy canopy, int layer, TranspirationMode mode, double p, double q)
        {
            double x_1, x_2, x_3, x_4, x_5, x_6, x_7;

            x_1 = s.VcMaxT[layer];
            x_2 = s.Kc[layer] / s.Ko[layer];
            x_3 = s.Kc[layer];
            x_4 = 0;
            x_5 = 0;
            x_6 = 1;
            x_7 = 1;

            if (s.type == SSType.AC1)
            {
                x_4 = s.VpMaxT[layer] / (s.Cm__[layer] + s.Kp[layer]); //Delta Eq (A56)
                                                                       //x_5 = 0;
            }
            else if (s.type == SSType.AC2)
            {
                x_5 = s.Vpr[layer];
            }

            s.X_4 = x_4;
            s.X_5 = x_5;

            return(CalcAssimilation(s, x_1, x_2, x_3, x_4, x_5, x_6, x_7, p, q, layer, canopy));
        }
Пример #3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="x_1"></param>
        /// <param name="x_2"></param>
        /// <param name="x_3"></param>
        /// <param name="x_4"></param>
        /// <param name="x_5"></param>
        /// <param name="layer"></param>
        /// <param name="canopy"></param>
        /// <returns></returns>
        public static double CalcAssimilation(SunlitShadedCanopy s, int layer, LeafCanopy canopy)
        {
            s.m = s.Rm[layer];
            s.t = s.G_[layer];
            s.b = 0.1 / canopy.Constant;
            s.j = s.Gbs[layer];
            s.e = s.Om[layer];
            s.R = s.RdT[layer];

            return(s.CalcAssimilation());
        }
Пример #4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="x_1"></param>
        /// <param name="x_2"></param>
        /// <param name="x_3"></param>
        /// <param name="x_4"></param>
        /// <param name="x_5"></param>
        /// <param name="layer"></param>
        /// <param name="canopy"></param>
        /// <returns></returns>
        public static double CalcAssimilation(SunlitShadedCanopy s, int layer, LeafCanopy canopy)
        {
            s.m = s.Rm[layer];
            s.t = s.G_[layer];
            s.b = 0;
            s.j = 1;
            s.e = s.Om[layer];
            s.R = s.RdT[layer];

            return(s.CalcAssimilation()); //Eq (A55)
        }
Пример #5
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="canopy"></param>
        /// <param name="layer"></param>
        /// <param name="mode"></param>
        /// <returns></returns>
        public static double CalcAc(SunlitShadedCanopy s, LeafCanopy canopy, int layer, TranspirationMode mode, double p, double q)
        {
            double x_1, x_2, x_3, x_4, x_5, x_6, x_7;

            x_1 = s.VcMaxT[layer];
            x_2 = 0;
            x_3 = s.Kc[layer] * (1 + s.Oc[layer] / s.Ko[layer]);
            x_4 = 0;
            x_5 = 0;
            x_6 = 0;
            x_7 = 0;

            return(CalcAssimilation(s, x_1, x_2, x_3, x_4, x_5, x_6, x_7, p, q, layer, canopy));
        }
Пример #6
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="canopy"></param>
        /// <param name="layer"></param>
        /// <param name="mode"></param>
        /// <returns></returns>
        public static double CalcAj(SunlitShadedCanopy s, LeafCanopy canopy, int layer, TranspirationMode mode, double p, double q)
        {
            double x_1, x_2, x_3, x_4, x_5, x_6, x_7;

            x_1 = s.J[layer] / 4;
            x_2 = 2 * s.G_[layer];
            x_3 = 0;
            x_4 = 0;
            x_5 = 0;
            x_6 = 0;
            x_7 = 0;

            return(CalcAssimilation(s, x_1, x_2, x_3, x_4, x_5, x_6, x_7, p, q, layer, canopy));;
        }
Пример #7
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="canopy"></param>
        /// <param name="layer"></param>
        /// <param name="mode"></param>
        /// <returns></returns>
        public static double CalcAj(SunlitShadedCanopy s, LeafCanopy canopy, int layer, TranspirationMode mode)
        {
            s.x_1 = (1 - canopy.CPath.X) * canopy.z * s.J[layer] / 3.0;
            s.x_2 = 7.0 / 3.0 * s.G_[layer];
            s.x_3 = 0;
            s.x_4 = 0;
            s.x_5 = canopy.CPath.X * canopy.z * s.J[layer] / canopy.CPath.Phi;
            s.x_6 = 0;
            s.x_7 = s.Cc[layer] * (1 - canopy.CPath.X) * canopy.z * s.J[layer] / (3 * s.Cc[layer] + 7 * s.G_[layer] * s.Oc[layer]);
            s.x_8 = 1;
            s.x_9 = 1;

            return(CalcAssimilation(s, layer, canopy));;
        }
Пример #8
0
        public static double CalcAc2(SunlitShadedCanopy s, LeafCanopy canopy, int layer, TranspirationMode mode)
        {
            s.x_1 = s.VcMaxT[layer];
            s.x_2 = s.Kc[layer] / s.Ko[layer];
            s.x_3 = s.Kc[layer];
            s.x_4 = 0;
            s.x_5 = s.Vpr[layer];
            s.x_6 = 0;
            s.x_7 = s.Cc[layer] * s.VcMaxT[layer] / (s.Cc[layer] + s.Kc[layer] * (1 + s.Oc[layer] / s.Ko[layer]));;
            s.x_8 = 1;
            s.x_9 = 1;

            return(CalcAssimilation(s, layer, canopy));
        }
Пример #9
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="canopy"></param>
        /// <param name="layer"></param>
        /// <param name="mode"></param>
        /// <returns></returns>
        public static double CalcAc1(SunlitShadedCanopy s, LeafCanopy canopy, int layer, TranspirationMode mode)
        {
            s.x_1 = s.VcMaxT[layer];
            s.x_2 = s.Kc[layer] / s.Ko[layer];
            s.x_3 = s.Kc[layer];
            s.x_4 = s.VpMaxT[layer] / (s.Cm__[layer] + s.Kp[layer]);
            s.x_5 = 0;
            s.x_6 = 1;
            s.x_7 = 0;
            s.x_8 = 1;
            s.x_9 = 1;

            return(CalcAssimilation(s, layer, canopy));
        }
Пример #10
0
        // No CalcAc2 in C3

        /// <summary>
        ///
        /// </summary>
        /// <param name="canopy"></param>
        /// <param name="layer"></param>
        /// <param name="mode"></param>
        /// <returns></returns>
        public static double CalcAj(SunlitShadedCanopy s, LeafCanopy canopy, int layer, TranspirationMode mode)
        {
            s.x_1 = s.J[layer] / 4;
            s.x_2 = 2 * s.G_[layer];
            s.x_3 = 0;
            s.x_4 = 0;
            s.x_5 = 0;
            s.x_6 = 0;
            s.x_7 = 0;
            s.x_8 = 0;
            s.x_9 = 0;

            return(CalcAssimilation(s, layer, canopy));;
        }
Пример #11
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="canopy"></param>
        /// <param name="layer"></param>
        /// <param name="mode"></param>
        /// <returns></returns>
        public static double CalcAj(SunlitShadedCanopy s, LeafCanopy canopy, int layer, TranspirationMode mode, double p, double q)
        {
            double x_1, x_2, x_3, x_4, x_5, x_6, x_7;

            x_1 = (1 - canopy.CPath.X) * s.J[layer] / 3.0;
            x_2 = 7.0 / 3.0 * s.G_[layer];
            x_3 = 0;
            x_4 = 0;
            x_5 = canopy.CPath.X * s.J[layer] / canopy.CPath.Phi;
            x_6 = 1;
            x_7 = 1;

            s.X_4 = x_4;
            s.X_5 = x_5;

            return(CalcAssimilation(s, x_1, x_2, x_3, x_4, x_5, x_6, x_7, p, q, layer, canopy));;
        }
Пример #12
0
        //---------------------------------------------------------------------------------------------------------
        /// <summary>
        ///
        /// </summary>
        /// <param name="s"></param>
        /// <param name="PM"></param>
        /// <param name="useAirTemp"></param>
        /// <param name="layer"></param>
        /// <param name="leafTemperature"></param>
        /// <param name="mode"></param>
        /// <param name="maxHourlyT"></param>
        /// <param name="Tfraction"></param>
        /// <returns></returns>
        public static bool CalcPhotosynthesis(this SunlitShadedCanopy s, PhotosynthesisModel PM, bool useAirTemp, int layer, double leafTemperature,
                                              TranspirationMode mode, double maxHourlyT, double Tfraction)
        {
            double p, q;

            LeafCanopy canopy = PM.Canopy;

            s.Oi[layer] = canopy.OxygenPartialPressure;

            s.Om[layer] = canopy.OxygenPartialPressure;

            s.Oc[layer] = s.Oi[layer];

            s.LeafTemp__[layer] = leafTemperature;

            if (useAirTemp)
            {
                s.LeafTemp__[layer] = PM.EnvModel.GetTemp(PM.Time);
            }

            s.CalcConductanceResistance(PM, canopy);

            s.VcMaxT[layer] = TemperatureFunction.Val2(s.LeafTemp__[layer], s.VcMax25[layer], canopy.CPath.VcTMin);
            s.RdT[layer]    = TemperatureFunction.Val2(s.LeafTemp__[layer], s.Rd25[layer], canopy.CPath.RdTMin);
            s.JMaxT[layer]  = TemperatureFunction.Val(s.LeafTemp__[layer], s.JMax25[layer], canopy.CPath.JMaxC, canopy.CPath.JTMax, canopy.CPath.JTMin, canopy.CPath.JTOpt, canopy.CPath.JBeta);

            canopy.Ja = (1 - canopy.F) / 2;

            s.J[layer] = (canopy.Ja * s.AbsorbedIrradiance[layer] + s.JMaxT[layer] - Math.Pow(Math.Pow(canopy.Ja * s.AbsorbedIrradiance[layer] + s.JMaxT[layer], 2) -
                                                                                              4 * canopy.Theta * s.JMaxT[layer] * canopy.Ja * s.AbsorbedIrradiance[layer], 0.5)) / (2 * canopy.Theta);

            s.Kc[layer]   = TemperatureFunction.Val2(s.LeafTemp__[layer], canopy.CPath.KcP25, canopy.CPath.KcTMin);
            s.Ko[layer]   = TemperatureFunction.Val2(s.LeafTemp__[layer], canopy.CPath.KoP25, canopy.CPath.KoTMin);
            s.VcVo[layer] = TemperatureFunction.Val2(s.LeafTemp__[layer], canopy.CPath.VcMax_VoMaxP25, canopy.CPath.VcMax_VoMaxTMin);


            s.ScO[layer] = s.Ko[layer] / s.Kc[layer] * s.VcVo[layer];

            s.G_[layer] = 0.5 / s.ScO[layer];

            s.r_[layer] = s.G_[layer] * s.Oc[layer];

            canopy.Sco = s.ScO[layer];

            s.gm_CO2T[layer] = s.LAIS[layer] * TemperatureFunction.Val(s.LeafTemp__[layer], canopy.Gm25[layer], canopy.CPath.GmC, canopy.CPath.GmTMax, canopy.CPath.GmTMin, canopy.CPath.GmTOpt, canopy.CPath.GmBeta);

            if (mode == TranspirationMode.unlimited)
            {
                s.Ci[layer] = canopy.CPath.CiCaRatio * canopy.Ca;

                p = s.Ci[layer];

                q = 1 / s.gm_CO2T[layer];

                //Caculate A's
                if (s.type == SSType.AC1)
                {
                    s.A[layer] = CalcAc(s, canopy, layer, TranspirationMode.unlimited, p, q);
                }
                else if (s.type == SSType.AJ)
                {
                    s.A[layer] = CalcAj(s, canopy, layer, TranspirationMode.unlimited, p, q);
                }

                if (s.A[layer] < 0 || double.IsNaN(s.A[layer]))
                {
                    s.A[layer] = 0;
                }

                if (PM.conductanceModel == PhotosynthesisModel.ConductanceModel.DETAILED)
                {
                    s.Ci[layer] = canopy.Ca - s.A[layer] / s.Gb_CO2[layer] - s.A[layer] / s.gs_CO2[layer];
                }

                s.Cc[layer] = s.Ci[layer] - s.A[layer] / s.gm_CO2T[layer];

                if (s.Cc[layer] < 0 || double.IsNaN(s.Cc[layer]))
                {
                    s.Cc[layer] = 0;
                }

                s.CiCaRatio[layer] = s.Ci[layer] / canopy.Ca;

                s.DoWaterInteraction(PM, canopy, mode);
            }

            else if (mode == TranspirationMode.limited)
            {
                s.WaterUse[layer] = maxHourlyT * Tfraction;

                s.Elambda[layer] = s.WaterUse[layer] / (0.001 * 3600) * canopy.Lambda / 1000;

                s.DoWaterInteraction(PM, canopy, mode);

                s.gm_CO2T[layer] = s.LAIS[layer] * TemperatureFunction.Val(s.LeafTemp__[layer], canopy.Gm25[layer], canopy.CPath.GmC, canopy.CPath.GmTMax, canopy.CPath.GmTMin, canopy.CPath.GmTOpt, canopy.CPath.GmBeta);

                double Gt = 1 / (1 / s.GbCO2[layer] + 1 / s.GsCO2[layer]);

                p = canopy.Ca - s.WaterUseMolsSecond[layer] * canopy.Ca / (Gt + s.WaterUseMolsSecond[layer] / 2);
                q = 1 / (Gt + s.WaterUseMolsSecond[layer] / 2) + 1 / s.gm_CO2T[layer];

                //Caculate A's
                if (s.type == SSType.AC1)
                {
                    s.A[layer] = CalcAc(s, canopy, layer, TranspirationMode.limited, p, q);
                }
                else if (s.type == SSType.AJ)
                {
                    s.A[layer] = CalcAj(s, canopy, layer, TranspirationMode.limited, p, q);
                }

                s.Cb[layer] = canopy.Ca - s.A[layer] / s.GbCO2[layer];

                s.Ci[layer] = ((Gt - s.WaterUseMolsSecond[layer] / 2) * canopy.Ca - s.A[layer]) / (Gt + s.WaterUseMolsSecond[layer] / 2);
            }

            double airTemp = PM.EnvModel.GetTemp(PM.Time);

            if (useAirTemp)
            {
                s.LeafTemp[layer] = PM.EnvModel.GetTemp(PM.Time);
            }

            double diffTemp = s.LeafTemp__[layer] - s.LeafTemp[layer];


            s.LeafTemp[layer] = (s.LeafTemp[layer] + s.LeafTemp__[layer]) / 2;

            if ((Math.Abs(diffTemp) > s.leafTempTolerance) || double.IsNaN(s.LeafTemp[layer]))
            {
                return(false);
            }
            return(true);
        }
Пример #13
0
        //---------------------------------------------------------------------------------------------------------
        /// <summary>
        ///
        /// </summary>
        /// <param name="PM"></param>
        /// <param name="useAirTemp"></param>
        /// <param name="layer"></param>
        /// <param name="leafTemperature"></param>
        /// <param name="cm"></param>
        /// <param name="mode"></param>
        /// <param name="maxHourlyT"></param>
        /// <param name="Tfraction"></param>
        /// <returns></returns>
        public static bool CalcPhotosynthesis(this SunlitShadedCanopy s, PhotosynthesisModel PM, bool useAirTemp, int layer, double leafTemperature, double cm, double cc, double oc,
                                              TranspirationMode mode, double maxHourlyT, double Tfraction)
        {
            LeafCanopy canopy = PM.Canopy;

            s.LeafTemp__[layer] = leafTemperature;
            if (useAirTemp)
            {
                s.LeafTemp__[layer] = PM.EnvModel.GetTemp(PM.Time);
            }

            s.CalcConductanceResistance(PM, canopy);

            s.Cm__[layer] = cm;
            s.Cc[layer]   = cc;
            s.Oc[layer]   = oc;

            s.VcMaxT[layer] = TemperatureFunction.Val2(s.LeafTemp__[layer], s.VcMax25[layer], canopy.CPath.VcTEa);
            s.RdT[layer]    = TemperatureFunction.Val2(s.LeafTemp__[layer], s.Rd25[layer], canopy.CPath.RdTEa);
            s.JMaxT[layer]  = TemperatureFunction.Val(s.LeafTemp__[layer], s.JMax25[layer], canopy.CPath.JMaxC, canopy.CPath.JTMax, canopy.CPath.JTMin, canopy.CPath.JTOpt, canopy.CPath.JBeta);
            s.VpMaxT[layer] = TemperatureFunction.Val2(s.LeafTemp__[layer], s.VpMax25[layer], canopy.CPath.VpMaxTEa);
            s.GmT[layer]    = TemperatureFunction.Val(s.LeafTemp__[layer], s.Gm25[layer], canopy.CPath.GmC, canopy.CPath.GmTMax, canopy.CPath.GmTMin, canopy.CPath.GmTOpt, canopy.CPath.GmBeta);
            s.Vpr[layer]    = canopy.Vpr_l * s.LAIS[layer];

            canopy.Ja = (1 - canopy.F) / 2;

            s.J[layer] = (canopy.Ja * s.AbsorbedIrradiance[layer] + s.JMaxT[layer] - Math.Pow(Math.Pow(canopy.Ja * s.AbsorbedIrradiance[layer] + s.JMaxT[layer], 2) -
                                                                                              4 * canopy.Theta * s.JMaxT[layer] * canopy.Ja * s.AbsorbedIrradiance[layer], 0.5)) / (2 * canopy.Theta);

            s.Kc[layer]   = TemperatureFunction.Val2(s.LeafTemp__[layer], canopy.CPath.KcP25, canopy.CPath.KcTEa);
            s.Ko[layer]   = TemperatureFunction.Val2(s.LeafTemp__[layer], canopy.CPath.KoP25, canopy.CPath.KoTEa);
            s.VcVo[layer] = TemperatureFunction.Val2(s.LeafTemp__[layer], canopy.CPath.VcMax_VoMaxP25, canopy.CPath.VcMax_VoMaxTEa);

            s.Oi[layer] = canopy.OxygenPartialPressure;
            s.Om[layer] = canopy.OxygenPartialPressure;

            s.ScO[layer] = s.Ko[layer] / s.Kc[layer] * s.VcVo[layer];
            s.G_[layer]  = 0.5 / s.ScO[layer];
            canopy.Sco   = s.ScO[layer];

            s.K_[layer]  = s.Kc[layer] * (1 + canopy.OxygenPartialPressure / s.Ko[layer]);
            s.Kp[layer]  = TemperatureFunction.Val2(s.LeafTemp__[layer], canopy.CPath.KpP25, canopy.CPath.KpTEa);
            s.Gbs[layer] = canopy.Gbs_CO2 * s.LAIS[layer];

            if (mode == TranspirationMode.unlimited)
            {
                s.Rm[layer]  = s.RdT[layer] * 0.5;
                s.Gbs[layer] = canopy.Gbs_CO2 * s.LAIS[layer];
                s.Ci[layer]  = canopy.CPath.CiCaRatio * canopy.Ca;

                s.p = s.Ci[layer];
                s.q = 1 / s.GmT[layer];

                //Caculate A's
                if (s.type == SSType.Ac1)
                {
                    s.A[layer] = CalcAc1(s, canopy, layer, TranspirationMode.unlimited);
                }
                else if (s.type == SSType.Ac2)
                {
                    s.A[layer] = CalcAc2(s, canopy, layer, TranspirationMode.unlimited);
                }
                else if (s.type == SSType.Aj)
                {
                    s.A[layer] = CalcAj(s, canopy, layer, TranspirationMode.unlimited);
                }

                s.DoWaterInteraction(PM, canopy, mode);
            }
            else if (mode == TranspirationMode.limited)
            {
                s.WaterUse[layer] = maxHourlyT * Tfraction;
                s.DoWaterInteraction(PM, canopy, mode);

                double Gt = 1 / (1 / s.GbCO2[layer] + 1 / s.GsCO2[layer]);

                s.p = canopy.Ca - s.WaterUseMolsSecond[layer] * canopy.Ca / (Gt + s.WaterUseMolsSecond[layer] / 2);
                s.q = 1 / (Gt + s.WaterUseMolsSecond[layer] / 2) + 1 / s.GmT[layer];

                //Caculate A's
                if (s.type == SSType.Ac1)
                {
                    s.A[layer] = CalcAc1(s, canopy, layer, TranspirationMode.limited);
                }
                else if (s.type == SSType.Ac2)
                {
                    s.A[layer] = CalcAc2(s, canopy, layer, TranspirationMode.limited);
                }
                else if (s.type == SSType.Aj)
                {
                    s.A[layer] = CalcAj(s, canopy, layer, TranspirationMode.limited);
                }

                s.Ci[layer] = ((Gt - s.WaterUseMolsSecond[0] / 2) * canopy.Ca - s.A[layer]) / (Gt + s.WaterUseMolsSecond[0] / 2);
            }

            s.Oc[layer]       = canopy.Alpha * s.A[layer] / (canopy.Constant * s.Gbs[layer]) + s.Om[layer];
            s.Cc[layer]       = (s.Ci[layer] - s.A[layer] / s.GmT[layer]) + (((s.Ci[layer] - s.A[layer] / s.GmT[layer]) * s.x_4 + s.x_5) - s.x_6 * s.A[layer] - s.m - s.x_7) * s.x_8 / s.Gbs[layer];
            s.Cm[layer]       = s.Ci[layer] - s.A[layer] / s.GmT[layer];
            s.LeafTemp[layer] = (s.LeafTemp[layer] + s.LeafTemp__[layer]) / 2;

            // Test if A is sensible
            if (double.IsNaN(s.A[layer]) || s.A[layer] <= 0.0)
            {
                return(true);
            }
            // Test if WaterUse is sensible
            else if (double.IsNaN(s.WaterUse[layer]) || s.WaterUse[layer] <= 0)
            {
                return(true);
            }
            // If both are sensible, return false (no errors fonud)
            else
            {
                return(false);
            }
        }
Пример #14
0
        //---------------------------------------------------------------------------------------------------------
        /// <summary>
        ///
        /// </summary>
        /// <param name="PM"></param>
        /// <param name="useAirTemp"></param>
        /// <param name="layer"></param>
        /// <param name="leafTemperature"></param>
        /// <param name="cm"></param>
        /// <param name="mode"></param>
        /// <param name="maxHourlyT"></param>
        /// <param name="Tfraction"></param>
        /// <returns></returns>
        public static bool CalcPhotosynthesis(this SunlitShadedCanopy s, PhotosynthesisModel PM, bool useAirTemp, int layer, double leafTemperature, double cm,
                                              TranspirationMode mode, double maxHourlyT, double Tfraction)
        {
            double p, q;

            LeafCanopy canopy = PM.Canopy;

            //leafTemp[layer] = PM.envModel.getTemp(PM.time);
            s.LeafTemp__[layer] = leafTemperature;

            if (useAirTemp)
            {
                s.LeafTemp__[layer] = PM.EnvModel.GetTemp(PM.Time);
            }

            s.Cm__[layer] = cm;

            double vpd = PM.EnvModel.GetVPD(PM.Time);

            s.VcMaxT[layer] = TemperatureFunction.Val2(s.LeafTemp__[layer], s.VcMax25[layer], canopy.CPath.VcTMin);
            s.RdT[layer]    = TemperatureFunction.Val2(s.LeafTemp__[layer], s.Rd25[layer], canopy.CPath.RdTMin);
            s.JMaxT[layer]  = TemperatureFunction.Val(s.LeafTemp__[layer], s.JMax25[layer], canopy.CPath.JMaxC, canopy.CPath.JTMax, canopy.CPath.JTMin, canopy.CPath.JTOpt, canopy.CPath.JBeta);
            s.VpMaxT[layer] = TemperatureFunction.Val2(s.LeafTemp__[layer], s.VpMax25[layer], canopy.CPath.VpMaxTMin);


            s.Vpr[layer] = canopy.Vpr_l * s.LAIS[layer];

            canopy.Ja = (1 - canopy.F) / 2;

            s.J[layer] = (canopy.Ja * s.AbsorbedIrradiance[layer] + s.JMaxT[layer] - Math.Pow(Math.Pow(canopy.Ja * s.AbsorbedIrradiance[layer] + s.JMaxT[layer], 2) -
                                                                                              4 * canopy.Theta * s.JMaxT[layer] * canopy.Ja * s.AbsorbedIrradiance[layer], 0.5)) / (2 * canopy.Theta);

            s.Kc[layer]   = TemperatureFunction.Val2(s.LeafTemp__[layer], canopy.CPath.KcP25, canopy.CPath.KcTMin);
            s.Ko[layer]   = TemperatureFunction.Val2(s.LeafTemp__[layer], canopy.CPath.KoP25, canopy.CPath.KoTMin);
            s.VcVo[layer] = TemperatureFunction.Val2(s.LeafTemp__[layer], canopy.CPath.VcMax_VoMaxP25, canopy.CPath.VcMax_VoMaxTMin);

            s.ScO[layer] = s.Ko[layer] / s.Kc[layer] * s.VcVo[layer];

            s.G_[layer] = 0.5 / s.ScO[layer];

            canopy.Sco = s.ScO[layer]; //For reporting ???

            s.K_[layer] = s.Kc[layer] * (1 + canopy.OxygenPartialPressure / s.Ko[layer]);

            s.Kp[layer] = TemperatureFunction.Val2(s.LeafTemp__[layer], canopy.CPath.KpP25, canopy.CPath.KpTMin);

            s.Gbs[layer] = canopy.Gbs_CO2 * s.LAIS[layer];

            s.Oi[layer] = canopy.OxygenPartialPressure;

            s.Om[layer] = canopy.OxygenPartialPressure;

            if (mode == TranspirationMode.unlimited)
            {
                s.VPD[layer] = PM.EnvModel.GetVPD(PM.Time);

                s.gm_CO2T[layer] = s.LAIS[layer] * TemperatureFunction.Val(s.LeafTemp__[layer], canopy.Gm25[layer], canopy.CPath.GmC, canopy.CPath.GmTMax, canopy.CPath.GmTMin, canopy.CPath.GmTOpt, canopy.CPath.GmBeta);

                s.Rm[layer] = s.RdT[layer] * 0.5;

                s.Gbs[layer] = canopy.Gbs_CO2 * s.LAIS[layer];

                s.Ci[layer] = canopy.CPath.CiCaRatio * canopy.Ca;

                p = s.Ci[layer];

                q = 1 / s.gm_CO2T[layer];

                //Caculate A's
                if (s.type == SSType.AJ)
                {
                    s.A[layer] = CalcAj(s, canopy, layer, TranspirationMode.unlimited, p, q);
                }
                else
                {
                    s.A[layer] = CalcAc(s, canopy, layer, TranspirationMode.unlimited, p, q);
                }

                s.CalcConductanceResistance(PM, canopy);

                s.Cm[layer] = s.Ci[layer] - s.A[layer] / s.gm_CO2T[layer];

                s.XX = s.Cm[layer] * s.X_4 + s.X_5;

                if (s.type == SSType.AC1 || s.type == SSType.AC2)
                {
                    s.Vp[layer] = Math.Min(s.Cm[layer] * s.VpMaxT[layer] / (s.Cm[layer] + s.Kp[layer]), s.Vpr[layer]);
                }
                else if (s.type == SSType.AJ)
                {
                    s.Vp[layer] = canopy.CPath.X * s.J[layer] / 2;
                }

                s.Oc[layer] = canopy.Alpha * s.A[layer] / (0.047 * s.Gbs[layer]) + s.Om[layer];

                s.r_[layer] = s.G_[layer] * s.Oc[layer];

                s.Cc[layer] = s.Cm[layer] + (s.XX - s.A[layer] - s.Rm[layer]) / s.Gbs[layer];

                if (s.Cc[layer] < 0 || double.IsNaN(s.Cc[layer]))
                {
                    s.Cc[layer] = 0;
                }

                s.F[layer] = s.Gbs[layer] * (s.Cc[layer] - s.Cm[layer]) / s.XX;

                s.DoWaterInteraction(PM, canopy, mode);
            }

            else if (mode == TranspirationMode.limited)
            {
                s.WaterUse[layer] = maxHourlyT * Tfraction;

                s.Elambda[layer] = s.WaterUse[layer] / (0.001 * 3600) * canopy.Lambda / 1000;

                double totalAbsorbed = s.AbsorbedIrradiancePAR[layer] + s.AbsorbedIrradianceNIR[layer];
                s.Rn[layer] = totalAbsorbed - 2 * (canopy.Sigma * Math.Pow(273 + s.LeafTemp__[layer], 4) - canopy.Sigma * Math.Pow(273 + PM.EnvModel.GetTemp(PM.Time), 4));

                s.CalcConductanceResistance(PM, canopy);

                s.DoWaterInteraction(PM, canopy, mode);

                s.gm_CO2T[layer] = s.LAIS[layer] * TemperatureFunction.Val(s.LeafTemp__[layer], canopy.Gm25[layer], canopy.CPath.GmC, canopy.CPath.GmTMax, canopy.CPath.GmTMin, canopy.CPath.GmTOpt, canopy.CPath.GmBeta);

                double Gt = 1 / (1 / s.GbCO2[layer] + 1 / s.GsCO2[layer]);

                p = canopy.Ca - s.WaterUseMolsSecond[layer] * canopy.Ca / (Gt + s.WaterUseMolsSecond[layer] / 2);
                q = 1 / (Gt + s.WaterUseMolsSecond[layer] / 2) + 1 / s.gm_CO2T[layer];

                //Caculate A's
                if (s.type == SSType.AJ)
                {
                    s.A[layer] = CalcAj(s, canopy, layer, TranspirationMode.limited, p, q);
                }
                else
                {
                    s.A[layer] = CalcAc(s, canopy, layer, TranspirationMode.limited, p, q);
                }

                s.Cb[layer] = canopy.Ca - s.A[layer] / s.GbCO2[layer];

                s.Ci[layer] = ((Gt - s.WaterUseMolsSecond[0] / 2) * canopy.Ca - s.A[layer]) / (Gt + s.WaterUseMolsSecond[0] / 2);

                s.Cm[layer] = s.Ci[layer] - s.A[layer] / s.gm_CO2T[layer];

                s.XX = s.Cm[layer] * s.X_4 + s.X_5;

                s.Oc[layer] = canopy.Alpha * s.A[layer] / (0.047 * s.Gbs[layer]) + s.Om[layer];

                s.r_[layer] = s.G_[layer] * s.Oc[layer];

                s.Cc[layer] = s.Cm[layer] + (s.XX - s.A[layer] - s.Rm[layer]) / s.Gbs[layer];

                if (s.Cc[layer] < 0 || double.IsNaN(s.Cc[layer]))
                {
                    s.Cc[layer] = 0;
                }

                s.F[layer] = s.Gbs[layer] * (s.Cc[layer] - s.Cm[layer]) / s.XX;
            }

            double airTemp = PM.EnvModel.GetTemp(PM.Time);

            if (useAirTemp)
            {
                s.LeafTemp[layer] = PM.EnvModel.GetTemp(PM.Time);
            }

            double diffCm   = (s.type == SSType.AC1 ? Math.Abs(s.Cm__[layer] - s.Cm[layer]) : 0);
            double diffTemp = s.LeafTemp__[layer] - s.LeafTemp[layer];


            s.LeafTemp[layer] = (s.LeafTemp[layer] + s.LeafTemp__[layer]) / 2;

            s.Cm[layer] = (s.Cm[layer] + s.Cm__[layer]) / 2;

            if ((Math.Abs(diffCm) > s.CmTolerance) ||
                (Math.Abs(diffTemp) > s.leafTempTolerance) ||
                double.IsNaN(s.Cm[layer]) || double.IsNaN(s.LeafTemp[layer]))
            {
                return(false);
            }
            return(true);
        }