コード例 #1
0
    public void OnPrepare()
    {
        RootSystem.Zone = new RootSystemZoneType[NumPlots];

        for (int i = 0; i < NumPlots; i++)
        {
            Component fieldProps = (Component)MyPaddock.Parent.ChildPaddocks[i].LinkByName("FieldProps");
            RootSystem.Zone[i] = new RootSystemZoneType();
            if (!fieldProps.Get("fieldArea", out RootSystem.Zone[i].ZoneArea))
            {
                throw new Exception("Could not find FieldProps component in field " + MyPaddock.Parent.ChildPaddocks[i].Name);
            }
            SoilWat = (Component)MyPaddock.LinkByType("SoilWat");
            SoilWat.Get("dlayer", out RootSystem.Zone[i].dlayer);
            RootSystem.Zone[i].ZoneName  = MyPaddock.Parent.ChildPaddocks[i].Name;
            RootSystem.Zone[i].RootDepth = 550;
            RootSystem.Zone[i].kl        = new double[RootSystem.Zone[i].dlayer.Length];
            RootSystem.Zone[i].ll        = new double[RootSystem.Zone[i].dlayer.Length];

            for (int j = 0; j < RootSystem.Zone[i].dlayer.Length; j++)
            {
                RootSystem.Zone[i].kl[j] = 0.02;
                RootSystem.Zone[i].ll[j] = 0.15;
            }
        }
        GetPotSWUptake();
    }
コード例 #2
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));
        }
    }
コード例 #3
0
ファイル: ReportDb.cs プロジェクト: wangyakai01/APSIMClassic
    public void OnInitialised()
    {
        if (Title == null)
        {
            Component Simulation = Paddock.LinkByType("Simulation") as Component;
            if (Simulation != null)
            {
                Simulation.Get("Title", out Title);
            }
        }

        FileName = Title + "-" + My.Name + ".db";
#if __MonoCS__
        Connection = new SqliteConnection("Data Source=" + FileName + ";Version=3;New=False;Compress=True;");
        Connection.Open();
        SqliteCommand sql_cmd = new SqliteCommand(Connection);
#else
        Connection = new SQLiteConnection("Data Source=" + FileName + ";Version=3;New=False;Compress=True;");
        Connection.Open();
        SQLiteCommand sql_cmd = new SQLiteCommand(Connection);
#endif

#if __MonoCS__
        using (SqliteTransaction dbTrans = Connection.BeginTransaction())
#else
        using (SQLiteTransaction dbTrans = Connection.BeginTransaction())
#endif
        {
            // Make sure we have a Simulations table.
            if (!TableNames.Contains("Simulations"))
            {
                sql_cmd             = Connection.CreateCommand();
                sql_cmd.CommandText = "CREATE TABLE Simulations (ID INTEGER PRIMARY KEY ASC, Title TEXT)";;
                sql_cmd.ExecuteNonQuery();
            }
            dbTrans.Commit();
        }

#if __MonoCS__
        using (SqliteTransaction dbTrans = Connection.BeginTransaction())
#else
        using (SQLiteTransaction dbTrans = Connection.BeginTransaction())
#endif
        {
            // Make sure we have a Data table.
            if (!TableNames.Contains("Data"))
            {
                string Cmd = "CREATE TABLE Data (SimulationID INTEGER)";
                sql_cmd             = Connection.CreateCommand();
                sql_cmd.CommandText = Cmd;
                sql_cmd.ExecuteNonQuery();
            }
            dbTrans.Commit();
        }

        // Try and get our SimulationID. If it's not in the Simulations table
        // then add it. The title is used as the simulation name.
        SimulationID = GetSimulationID();
        if (SimulationID == -1)
        {
            sql_cmd.CommandText = "INSERT INTO [Simulations] (Title) VALUES ('" + Title + "')";
            sql_cmd.ExecuteNonQuery();
            SimulationID = GetSimulationID();
        }

        // Remove existing data for this simulation.
        sql_cmd.CommandText = "DELETE FROM [Data] WHERE SimulationID = " + SimulationID.ToString();
        sql_cmd.ExecuteNonQuery();
        SimulationID = GetSimulationID();

        // Give Sqlite some commands to speed up the adding of data.
        sql_cmd             = Connection.CreateCommand();
        sql_cmd.CommandText = "PRAGMA synchronous=OFF";
        sql_cmd.ExecuteNonQuery();

        // subscribe to all events.
        Paddock.Subscribe(Variables.OutputFrequency, OnDoReport);
    }