Example #1
0
        public void TestSoilWithNullProperties()
        {
            string      json = ReflectionUtilities.GetResourceAsString("UnitTests.Resources.NullSample.apsimx");
            Simulations file = FileFormat.ReadFromString <Simulations>(json, out List <Exception> fileErrors);

            if (fileErrors != null && fileErrors.Count > 0)
            {
                throw fileErrors[0];
            }

            // This simulation needs a weather node, but using a legit
            // met component will just slow down the test.
            IModel sim     = file.FindInScope <Simulation>();
            Model  weather = new MockWeather();

            sim.Children.Add(weather);
            weather.Parent = sim;

            // Run the file.
            var runner = new Runner(file);
            List <Exception> errors = runner.Run();

            if (errors != null && errors.Count > 0)
            {
                throw errors[0];
            }
        }
Example #2
0
        public static void TestReportingOnModelEvents()
        {
            string      json = ReflectionUtilities.GetResourceAsString("UnitTests.Report.ReportOnEvents.apsimx");
            Simulations file = FileFormat.ReadFromString <Simulations>(json, out List <Exception> fileErrors);

            if (fileErrors != null && fileErrors.Count > 0)
            {
                throw fileErrors[0];
            }

            // This simulation needs a weather node, but using a legit
            // met component will just slow down the test.
            IModel sim     = file.FindInScope <Simulation>();
            Model  weather = new MockWeather();

            sim.Children.Add(weather);
            weather.Parent = sim;

            // Run the file.
            var Runner = new Runner(file);

            Runner.Run();

            // Check that the report reported on the correct dates.
            var           storage    = file.FindInScope <IDataStore>();
            List <string> fieldNames = new List <string>()
            {
                "doy"
            };

            DataTable data = storage.Reader.GetData("ReportOnFertilisation", fieldNames: fieldNames);

            double[] values   = DataTableUtilities.GetColumnAsDoubles(data, "doy");
            double[] expected = new double[] { 1, 32, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 364 };
            Assert.AreEqual(expected, values);

            data   = storage.Reader.GetData("ReportOnIrrigation", fieldNames: fieldNames);
            values = DataTableUtilities.GetColumnAsDoubles(data, "doy");
            // There is one less irrigation event, as the manager script doesn't irrigate.
            expected = new double[] { 1, 32, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335 };
            Assert.AreEqual(expected, values);
        }
        public void EnsureModelOverrideWork()
        {
            var sim = new Simulation()
            {
                Name     = "BaseSimulation",
                Children = new List <Model>()
                {
                    new MockWeather()
                    {
                        Name      = "Weather",
                        MaxT      = 1,
                        StartDate = DateTime.MinValue
                    },
                }
            };

            Apsim.ParentAllChildren(sim);

            var replacementWeather = new MockWeather()
            {
                Name      = "Weather2",
                MaxT      = 2,
                StartDate = DateTime.MinValue
            };

            var simulationDescription = new SimulationDescription(sim, "CustomName");

            simulationDescription.AddOverride(new ModelReplacement("Weather", replacementWeather));

            var newSim = simulationDescription.ToSimulation();

            Assert.AreEqual(newSim.Name, "CustomName");

            var weather = newSim.Children[0] as MockWeather;

            Assert.AreEqual(weather.MaxT, 2);

            // The name of the new model should be the same as the original model.
            Assert.AreEqual(weather.Name, "Weather");
        }
Example #4
0
        public void TestEvaporation()
        {
            MockSoil soil = new MockSoil();

            APSIM.Shared.Soils.Soil soilProperties = Setup();
            APSIMReadySoil.Create(soilProperties);

            MockClock clock = new MockClock();

            clock.Today = new DateTime(2015, 6, 1);

            MockWeather weather = new MockWeather();

            weather.MaxT = 30;
            weather.MinT = 10;
            weather.Rain = 100;
            weather.Radn = 25;

            MockSurfaceOrganicMatter surfaceOrganicMatter = new MockSurfaceOrganicMatter();

            surfaceOrganicMatter.Cover = 0.8;

            List <ICanopy> canopies = new List <ICanopy>();

            EvaporationModel evaporation = new EvaporationModel();

            SetLink(soil, "properties", soilProperties);
            SetLink(evaporation, "soil", soil);
            SetLink(evaporation, "clock", clock);
            SetLink(evaporation, "weather", weather);
            SetLink(evaporation, "canopies", canopies);
            SetLink(evaporation, "surfaceOrganicMatter", surfaceOrganicMatter);

            // Empty profile.
            soil.Water = MathUtilities.Multiply(soilProperties.Water.LL15, soilProperties.Water.Thickness);

            evaporation.Calculate();
            Assert.IsTrue(MathUtilities.FloatsAreEqual(evaporation.Es, 3.00359));

            soil.Infiltration = 0;
            evaporation.Calculate();
            Assert.IsTrue(MathUtilities.FloatsAreEqual(evaporation.Es, 2.20072));

            soil.Infiltration = 0;
            evaporation.Calculate();
            Assert.IsTrue(MathUtilities.FloatsAreEqual(evaporation.Es, 1.57064));

            soil.Infiltration = 0;
            evaporation.Calculate();
            Assert.IsTrue(MathUtilities.FloatsAreEqual(evaporation.Es, 0.96006));

            soil.Infiltration = 0;
            evaporation.Calculate();
            Assert.IsTrue(MathUtilities.FloatsAreEqual(evaporation.Es, 0.75946));

            soil.Infiltration = 0;
            evaporation.Calculate();
            Assert.IsTrue(MathUtilities.FloatsAreEqual(evaporation.Es, 0.64851));

            soil.Infiltration = 100;
            evaporation.Calculate();
            Assert.IsTrue(MathUtilities.FloatsAreEqual(evaporation.Es, 3.00359));

            soil.Infiltration = 0;
            evaporation.Calculate();
            Assert.IsTrue(MathUtilities.FloatsAreEqual(evaporation.Es, 2.20072));
        }
Example #5
0
        public void TestRunoff()
        {
            SoilModel soil = new SoilModel();

            APSIM.Shared.Soils.Soil soilProperties = Setup();
            APSIMReadySoil.Create(soilProperties);

            MockWeather weather = new MockWeather();

            weather.Rain = 100;

            MockIrrigation irrigation = new MockIrrigation();

            irrigation.IrrigationApplied = 0;

            MockSurfaceOrganicMatter surfaceOrganicMatter = new MockSurfaceOrganicMatter();

            surfaceOrganicMatter.Cover = 0.1;

            CNReductionForCover reductionForCover = new CNReductionForCover();
            List <ICanopy>      canopies          = new List <ICanopy>();

            CNReductionForTillage reductionForTillage = new CNReductionForTillage();

            RunoffModel runoff = new RunoffModel();

            runoff.CN2Bare = 70;

            // setup links
            SetLink(soil, "properties", soilProperties);
            SetLink(soil, "runoffModel", runoff);
            SetLink(soil, "weather", weather);
            SetLink(soil, "irrigation", irrigation);
            SetLink(runoff, "soil", soil);
            SetLink(runoff, "reductionForCover", reductionForCover);
            SetLink(runoff, "reductionForTillage", reductionForTillage);
            SetLink(reductionForCover, "surfaceOrganicMatter", surfaceOrganicMatter);
            SetLink(reductionForCover, "canopies", canopies);
            SetLink(reductionForTillage, "weather", weather);

            // Empty profile.
            soil.Water = MathUtilities.Multiply(soilProperties.Water.LL15, soilProperties.Water.Thickness);

            // Profile is empty - should be small amount of runoff.
            Assert.IsTrue(MathUtilities.FloatsAreEqual(runoff.Value(), 5.60815));

            // Full profile - should be a lot more runoff.
            soil.Water = MathUtilities.Multiply(soilProperties.Water.DUL, soilProperties.Water.Thickness);
            Assert.IsTrue(MathUtilities.FloatsAreEqual(runoff.Value(), 58.23552));

            // Test CN reduction due to canopy. Tests the Curve Number vs Cover graph.
            // Cover is 10%, reduction is 2.5
            surfaceOrganicMatter.Cover = 0.1;
            Assert.IsTrue(MathUtilities.FloatsAreEqual(reductionForCover.Value(), 2.49999));

            // Cover is 80%, reduction is 20
            surfaceOrganicMatter.Cover = 0.8;
            Assert.IsTrue(MathUtilities.FloatsAreEqual(reductionForCover.Value(), 20.0));

            // Test Runoff vs Rainfall graph i.e. effect of different curve numbers.
            surfaceOrganicMatter.Cover = 0.0;
            runoff.CN2Bare             = 60;
            Assert.IsTrue(MathUtilities.FloatsAreEqual(runoff.Value(), 48.18584));

            runoff.CN2Bare = 75;
            Assert.IsTrue(MathUtilities.FloatsAreEqual(runoff.Value(), 68.16430));

            runoff.CN2Bare = 85;
            Assert.IsTrue(MathUtilities.FloatsAreEqual(runoff.Value(), 81.15006));
        }