Ejemplo n.º 1
0
    public void OnProcess()
    {
        //set up data table
        int NumLayers = 0;

        AllRootSystems.Rows.Clear();
        foreach (Paddock p in paddock.ChildPaddocks)
        {
            foreach (Component c in p.Crops)
            {
                string PlantStatus;
                if (!c.Get("plant_status", out PlantStatus))
                {
                    throw new Exception("Could not find plant_status for crop :" + c.Name);
                }

                if (PlantStatus != "out")                        //if crop is not in ground, we don't care about it
                {
                    if (c.GetObject("RootSystem", ref RootData)) //crop has a RootData structre
                    {
                        Dictionary <string, double> SWStrength = CalcSWSourceStrength(RootData);
                        foreach (RootSystemZoneType zone in RootData.Zone) //add each zone to the table
                        {
                            AllRootSystems.Rows.Add(zone.ZoneName, c.Name, RootData.SWDemand, SWStrength, zone, true);
                        }
                        NumLayers = RootData.Zone[0].kl.Length;
                    }
                    else //crop does not have RootData structure, so make one.
                    {
                        Dictionary <string, double> SWStrength = new Dictionary <string, double>();
                        RootData                  = new RootSystemType();
                        RootData.Zone             = new RootSystemZoneType[1];
                        RootData.Zone[0]          = new RootSystemZoneType();
                        RootData.Zone[0].ZoneName = p.Name;
                        RootData.Zone[0].ZoneArea = 1;
                        if (!c.Get("sw_demand", out RootData.SWDemand))
                        {
                            throw new Exception("Could not get sw_demand for crop " + c.Name);
                        }
                        if (!c.Get("root_depth", out RootData.Zone[0].RootDepth))
                        {
                            throw new Exception("Could not get root_depth for crop " + c.Name);
                        }
                        if (!c.Get("ll", out RootData.Zone[0].ll))
                        {
                            throw new Exception("Could not get ll for crop " + c.Name);
                        }
                        if (!c.Get("kl", out RootData.Zone[0].kl))
                        {
                            throw new Exception("Could not get kl for crop " + c.Name);
                        }
                        SoilWat = (Component)p.LinkByType("SoilWat");
                        SWStrength.Add(p.Name, 1);
                        if (!SoilWat.Get("dlayer", out RootData.Zone[0].dlayer))
                        {
                            throw new Exception("Could not get dlayer for paddock " + p.Name);
                        }
                        AllRootSystems.Rows.Add(RootData.Zone[0].ZoneName, c.Name, RootData.SWDemand, SWStrength, RootData.Zone[0], true);
                        NumLayers = RootData.Zone[0].kl.Length;
                    }
                }
            }
        }
        //use LINQ to extract the paddocks for processing
        IEnumerable <string> paddockNames = AllRootSystems.AsEnumerable().Select <DataRow, string>(name => (string)name.ItemArray[0]).Distinct();

        //do water allocation for each paddock
        foreach (string PaddockName in paddockNames)
        {
            IEnumerable <DataRow> RootZones = AllRootSystems.AsEnumerable().Where(row => row.ItemArray[0].Equals(PaddockName));
            Paddock   p          = (Paddock)paddock.LinkByName(PaddockName);
            Component fieldProps = (Component)p.LinkByName("FieldProps");
            double    fieldArea;
            if (fieldProps == null || !fieldProps.Get("fieldArea", out fieldArea))
            {
                throw new Exception("Could not find FieldProps component in field " + PaddockName);
            }

            Component Soil = (Component)p.LinkByType("SoilWat");
            double[]  SWDep;
            double[]  dlayer;
            Soil.Get("dlayer", out dlayer);
            Soil.Get("sw_dep", out SWDep);
            double[] CropSWDemand = new double[RootZones.Count()];
            for (int i = 0; i < RootZones.Count(); i++) //get demand for all crops in paddock using relative SW strength
            {
                Dictionary <string, double> PaddockSWDemands = (Dictionary <string, double>)RootZones.ToArray()[i].ItemArray[3];
                CropSWDemand[i] = PaddockSWDemands[p.Name] * (double)RootZones.ToArray()[i].ItemArray[2];
            }
            double[,] RelKLStrength      = CalcRelKLStrength(RootZones, CropSWDemand);
            double[,] RelSWLayerStrength = CalcRelSWLayerStrength(RootZones, SWDep, NumLayers);
            double[,] SWSupply           = CalcSWSupply(RootZones, SWDep, NumLayers);

            double[,] LayerUptake = new double[RootZones.Count(), NumLayers];
            double[] LastCropSWDemand;
            double[,] LastSWSupply;

            int count = 0;
            do
            {
                count++;
                LastCropSWDemand = CropSWDemand;
                LastSWSupply     = SWSupply;

                for (int i = 0; i < RootZones.Count(); i++) //get as much water as possible for the layer using relative kl strengths
                {
                    RootSystemZoneType Zone = (RootSystemZoneType)RootZones.ToArray()[i].ItemArray[4];
                    for (int j = 0; j < NumLayers; j++)
                    {
                        if (MathUtility.Sum(CropSWDemand) < MathUtility.Sum(SWSupply))
                        {
                            LayerUptake[i, j] = CropSWDemand[i] * RelSWLayerStrength[i, j];
                        }
                        else
                        {
                            LayerUptake[i, j] = SWSupply[i, j] * RelKLStrength[j, i] * RootProportion(j, Zone.RootDepth, dlayer);
                        }

                        if (LayerUptake[i, j] < 0)
                        {
                            throw new Exception("Layer uptake should not be negative");
                        }
                    }
                }

                DenseMatrix Uptake = DenseMatrix.OfArray(LayerUptake);
                Paddock     CurrentPaddock;
                Component   CurrentCrop;
                for (int i = 0; i < RootZones.Count(); i++) //subtract taken water from the supply and demand
                {
                    CurrentPaddock   = (Paddock)p.LinkByName((string)RootZones.ToArray()[i].ItemArray[0]);
                    CurrentCrop      = (Component)CurrentPaddock.LinkByName((string)RootZones.ToArray()[i].ItemArray[1]);
                    CropSWDemand[i] -= Uptake.Row(i).Sum();
                    if (CurrentCrop != null && CurrentCrop.Name.ToLower().Equals("maize"))
                    {
                        CurrentCrop.Set("arb_water_uptake", Uptake.Row(i).ToArray());
                    }
                    for (int j = 0; j < NumLayers; j++)
                    {
                        SWSupply[i, j] -= LayerUptake[i, j];
                    }
                }

                //subtract from soil water
                for (int j = 0; j < Uptake.ColumnCount; j++)
                {
                    SWDep[j] -= Uptake.Column(j).Sum() / fieldArea;
                }

                Soil.Set("sw_dep", SWDep);
            } while (MathUtility.Sum(LastCropSWDemand) != MathUtility.Sum(CropSWDemand) && MathUtility.Sum(LastSWSupply) != MathUtility.Sum(SWSupply));
        }
    }