Esempio n. 1
0
        public void InitialConditionsIsCreated()
        {
            Soil soil = CreateSimpleSoil();

            Utilities.InitialiseModel(soil);

            SoilStandardiser.Standardise(soil);

            var initial  = soil.Children[5] as Sample;
            var analysis = soil.Children[4] as Chemical;

            Assert.AreEqual(soil.FindAllChildren <Sample>().Count(), 1);
            Assert.AreEqual(initial.Name, "Initial");
            Assert.AreEqual(initial.SW, new double[] { 0.1, 0.2 });
            Assert.AreEqual(initial.NO3, new double[] { 29.240000000000002, 2.432 });  // kg/ha
            Assert.AreEqual(initial.NH4, new double[] { 1.4960000000000002, 0.4864 }); // kg/ha
            Assert.AreEqual(initial.OC, new double[] { 2.0, 0.9 });
            Assert.AreEqual(initial.PH, new double[] { 6.4, 6.9 });
            Assert.AreEqual(initial.EC, new double[] { 150, 200 });

            var soilOrganicMatter = soil.Children[3] as Organic;

            Assert.IsNull(soilOrganicMatter.Carbon);

            Assert.NotNull(analysis);
        }
Esempio n. 2
0
        /// <summary>
        /// Convert the simulation decription to a simulation.
        /// path.
        /// </summary>
        public Simulation ToSimulation()
        {
            try
            {
                AddReplacements();

                Simulation newSimulation;
                if (doClone)
                {
                    newSimulation = Apsim.Clone(baseSimulation) as Simulation;

                    // After a binary clone, we need to force all managers to
                    // recompile their scripts. This is to work around an issue
                    // where scripts will change during deserialization. See issue
                    // #4463 and the TestMultipleChildren test inside ReportTests.
                    Apsim.ChildrenRecursively(newSimulation, typeof(Manager)).ForEach(m => m.OnCreated());
                }
                else
                {
                    newSimulation = baseSimulation;
                }

                if (string.IsNullOrWhiteSpace(Name))
                {
                    newSimulation.Name = baseSimulation.Name;
                }
                else
                {
                    newSimulation.Name = Name;
                }

                newSimulation.Parent = null;
                Apsim.ParentAllChildren(newSimulation);
                replacementsToApply.ForEach(r => r.Replace(newSimulation));

                // Give the simulation the descriptors.
                newSimulation.Descriptors = Descriptors;
                newSimulation.Services    = GetServices();

                // Standardise the soil.
                var soils = Apsim.ChildrenRecursively(newSimulation, typeof(Soils.Soil));
                foreach (Soils.Soil soil in soils)
                {
                    SoilStandardiser.Standardise(soil);
                }

                newSimulation.ClearCaches();
                return(newSimulation);
            }
            catch (Exception err)
            {
                var message = "Error in file: " + baseSimulation.FileName + " Simulation: " + Name;
                throw new Exception(message, err);
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Convert the simulation decription to a simulation.
        /// path.
        /// </summary>
        public Simulation ToSimulation()
        {
            try
            {
                AddReplacements();

                Simulation newSimulation;
                if (doClone)
                {
                    newSimulation = Apsim.Clone(baseSimulation) as Simulation;
                }
                else
                {
                    newSimulation = baseSimulation;
                }

                if (string.IsNullOrWhiteSpace(Name))
                {
                    newSimulation.Name = baseSimulation.Name;
                }
                else
                {
                    newSimulation.Name = Name;
                }

                newSimulation.Parent = null;
                Apsim.ParentAllChildren(newSimulation);
                replacementsToApply.ForEach(r => r.Replace(newSimulation));

                // Give the simulation the descriptors.
                newSimulation.Descriptors = Descriptors;
                newSimulation.Services    = GetServices();

                // Standardise the soil.
                var soils = Apsim.ChildrenRecursively(newSimulation, typeof(Soils.Soil));
                foreach (Soils.Soil soil in soils)
                {
                    SoilStandardiser.Standardise(soil);
                }

                newSimulation.ClearCaches();
                return(newSimulation);
            }
            catch (Exception err)
            {
                var message = "Error in file: " + baseSimulation.FileName + " Simulation: " + Name;
                throw new Exception(message, err);
            }
        }
Esempio n. 4
0
        private static Simulation CreateSimulation(string path)
        {
            path = PathUtilities.GetAbsolutePath(path, null);
            Simulations sims = FileFormat.ReadFromFile <Simulations>(path, e => throw e, false);

            foreach (Soil soil in sims.FindAllDescendants <Soil>())
            {
                SoilStandardiser.Standardise(soil);
            }
            DataStore storage = sims.FindDescendant <DataStore>();

            storage.UseInMemoryDB = true;
            Clock clock = sims.FindDescendant <Clock>();

            clock.EndDate = clock.StartDate.AddYears(1);
            return(sims.FindDescendant <Simulation>());
        }
Esempio n. 5
0
        public void LayerStructureIsUsedForMapping()
        {
            var soil = new Soil
            {
                Children = new List <IModel>()
                {
                    new Physical()
                    {
                        Thickness = new double[] { 100, 300, 300 },
                        BD        = new double[] { 1.36, 1.216, 1.24 },
                        AirDry    = new double[] { 0.135, 0.214, 0.261 },
                        LL15      = new double[] { 0.27, 0.267, 0.261 },
                        DUL       = new double[] { 0.365, 0.461, 0.43 },
                        SAT       = new double[] { 0.400, 0.481, 0.45 },

                        Children = new List <IModel>()
                        {
                            new SoilCrop
                            {
                                Name = "WheatSoil",
                                KL   = new double[] { 0.06, 0.060, 0.060 },
                                LL   = new double[] { 0.27, 0.267, 0.261 }
                            }
                        }
                    },
                    new Models.WaterModel.WaterBalance(),
                    new CERESSoilTemperature(),
                    new Organic
                    {
                        Thickness = new double[] { 100, 300 },
                        Carbon    = new double[] { 2, 1 }
                    },
                    new Chemical
                    {
                        Thickness = new double[] { 100, 200 },
                        NO3N      = new double[] { 27, 6 },
                        CL        = new double[] { 38, double.NaN }
                    },
                    new Sample
                    {
                        Thickness = new double[] { 500 },
                        SW        = new double[] { 0.103 },
                        OC        = new double[] { 1.35 },
                        SWUnits   = Sample.SWUnitsEnum.Gravimetric
                    },
                    new Sample
                    {
                        Thickness = new double[] { 1000 },
                        OC        = new double[] { 1.35 },
                        SWUnits   = Sample.SWUnitsEnum.Volumetric
                    },
                    new LayerStructure
                    {
                        Thickness = new double[] { 100, 300 }
                    }
                }
            };

            Utilities.InitialiseModel(soil);

            SoilStandardiser.Standardise(soil);

            var water             = soil.Children[0] as Physical;
            var soilOrganicMatter = soil.Children[3] as Organic;
            var sample            = soil.Children[5] as Sample;

            // Make sure layer structures have been standardised.
            var targetThickness = new double[] { 100, 300 };

            Assert.AreEqual(water.Thickness, targetThickness);
            Assert.AreEqual(soilOrganicMatter.Thickness, targetThickness);
            Assert.AreEqual(sample.Thickness, targetThickness);

            // Make sure sample units are volumetric.
            Assert.AreEqual(sample.SWUnits, Sample.SWUnitsEnum.Volumetric);
        }
Esempio n. 6
0
        /// <summary>
        /// Prepare the simulation for running.
        /// </summary>
        public void Prepare()
        {
            // Remove disabled models.
            RemoveDisabledModels(this);

            // Standardise the soil.
            var soils = FindAllDescendants <Soils.Soil>();

            foreach (Soils.Soil soil in soils)
            {
                SoilStandardiser.Standardise(soil);
            }

            // If this simulation was not created from deserialisation then we need
            // to parent all child models correctly and call OnCreated for each model.
            bool hasBeenDeserialised = Children.Count > 0 && Children[0].Parent == this;

            if (!hasBeenDeserialised)
            {
                // Parent all models.
                this.ParentAllDescendants();

                // Call OnCreated in all models.
                foreach (IModel model in FindAllDescendants().ToList())
                {
                    model.OnCreated();
                }
            }

            // Call OnPreLink in all models.
            // Note the ToList(). This is important because some models can
            // add/remove models from the simulations tree in their OnPreLink()
            // method, and FindAllDescendants() is lazy.
            FindAllDescendants().ToList().ForEach(model => model.OnPreLink());

            if (Services == null || Services.Count < 1)
            {
                var simulations = FindAncestor <Simulations>();
                if (simulations != null)
                {
                    Services = simulations.GetServices();
                }
                else
                {
                    Services = new List <object>();
                    IDataStore storage = this.FindInScope <IDataStore>();
                    if (storage != null)
                    {
                        Services.Add(this.FindInScope <IDataStore>());
                    }
                    Services.Add(new ScriptCompiler());
                }
            }

            var links  = new Links(Services);
            var events = new Events(this);

            try
            {
                // Connect all events.
                events.ConnectEvents();

                // Resolve all links
                links.Resolve(this, true);

                events.Publish("SubscribeToEvents", new object[] { this, EventArgs.Empty });
            }
            catch (Exception err)
            {
                throw new SimulationException("", err, Name, FileName);
            }
        }
Esempio n. 7
0
        public void InitialConditionsIsCreated()
        {
            var soil = new Soil
            {
                Children = new List <IModel>()
                {
                    new Physical()
                    {
                        Thickness = new double[] { 100, 200 },
                        BD        = new double[] { 1.36, 1.216 },
                        AirDry    = new double[] { 0.135, 0.214 },
                        LL15      = new double[] { 0.27, 0.267 },
                        DUL       = new double[] { 0.365, 0.461 },
                        SAT       = new double[] { 0.400, 0.481 },
                    },
                    new Models.WaterModel.WaterBalance(),
                    new CERESSoilTemperature(),
                    new Organic
                    {
                        Thickness = new double[] { 100, 200 },
                        Carbon    = new double[] { 2, 1 },
                        FBiom     = new double[] { 1, 2 }
                    },
                    new Chemical
                    {
                        Thickness = new double[] { 50, 50 },
                        NO3N      = new double[] { 27, 16 },
                        NH4N      = new double[] { 2, double.NaN },
                        PH        = new double[] { 6.8, 6.9 },
                        EC        = new double[] { 100, 200 }
                    },
                    new Sample
                    {
                        Thickness = new double[] { 100, 200 },
                        SW        = new double[] { 0.1, 0.2 },
                        OC        = new double[] { double.NaN, 0.9 },
                        SWUnits   = Sample.SWUnitsEnum.Volumetric
                    },
                    new Sample
                    {
                        Thickness = new double[] { 100, 200 },
                        PH        = new double[] { 6.4, double.NaN },
                    }
                }
            };

            Apsim.InitialiseModel(soil);

            SoilStandardiser.Standardise(soil);

            var initial  = soil.Children[5] as Sample;
            var analysis = soil.Children[4] as Chemical;

            Assert.AreEqual(Apsim.Children(soil, typeof(Sample)).Count, 1);
            Assert.AreEqual(initial.Name, "Initial");
            Assert.AreEqual(initial.SW, new double[] { 0.1, 0.2 });
            Assert.AreEqual(initial.NO3N, new double[] { 29.240000000000002, 2.432 });  // kg/ha
            Assert.AreEqual(initial.NH4N, new double[] { 1.4960000000000002, 0.4864 }); // kg/ha
            Assert.AreEqual(initial.OC, new double[] { 2.0, 0.9 });
            Assert.AreEqual(initial.PH, new double[] { 6.4, 6.9 });
            Assert.AreEqual(initial.EC, new double[] { 150, 200 });

            var soilOrganicMatter = soil.Children[3] as Organic;

            Assert.IsNull(soilOrganicMatter.Carbon);

            Assert.NotNull(analysis);
        }