예제 #1
0
        public void RunAusfarmGetOutputs()
        {
            RuntimeEnvironment environment = new RuntimeEnvironment
            {
                AusfarmRevision = "AusFarm-1.4.12",
            };

            RunF4PJob  job    = new RunF4PJob(GetDefaultF4PSimulationSpec(), environment);
            IJobRunner runner = new JobRunnerAsync();

            runner.Run(job, wait: true);

            // Make sure we don't have an error.
            Assert.AreEqual(job.Errors.Count, 0);

            // Make sure we have a daily output table.
            //Assert.AreEqual(job.Outputs.Tables.Count, 3);
            //Assert.AreEqual(job.Outputs.Tables[0].TableName, "Summary");
            //
            //Assert.AreEqual(job.Outputs.Tables[1].TableName, "YieldProphetDaily");
            //Assert.AreEqual(job.Outputs.Tables[1].Rows.Count, 92);
            //double[] biomass = DataTableUtilities.GetColumnAsDoubles(job.Outputs.Tables[1], "biomass");
            //Assert.IsTrue(MathUtilities.Max(biomass) > 20.0); // make sure something is growing.
            //
            //// Make sure we have a depth table.
            //Assert.AreEqual(job.Outputs.Tables[2].TableName, "YieldProphetDepth");
            //Assert.AreEqual(job.Outputs.Tables[2].Rows.Count, 8);
            //double[] sw = DataTableUtilities.GetColumnAsDoubles(job.Outputs.Tables[2], "sw");
            //Assert.IsTrue(MathUtilities.Max(sw) > 0.0); // make sure there are sw values
        }
예제 #2
0
        /// <summary>Main DoWork method for the task thread.</summary>
        private void JobRunnerThread()
        {
            IJobManager nextJob = null;

            while (!cancel)
            {
                try
                {
                    nextJob = GetJobFromServer();

                    if (nextJob != null)
                    {
                        // Run the job.
                        IJobRunner runner = new JobRunnerAsync();
                        runner.Run(nextJob,
                                   wait: true,
                                   numberOfProcessors: Convert.ToInt32(appSettings["MaximumNumberOfCores"]));

                        // Tell the server we've finished the job.
                        UpdateServerForCompletedJob(nextJob);
                    }
                }
                catch (Exception err)
                {
                    WriteToLog(err.Message);
                }
            }
        }
예제 #3
0
        public void EnsureAllToolsRun()
        {
            string           json = ReflectionUtilities.GetResourceAsString("UnitTests.Core.PostSimulationTool.apsimx");
            List <Exception> errors;
            Simulations      sims = FileFormat.ReadFromString <Simulations>(json, out errors);

            Assert.AreEqual(0, errors.Count);

            IModel script = Apsim.Child(Apsim.Find(sims, "Tool2"), "Script");

            Assert.NotNull(script);

            Simulation sim = Apsim.Find(sims, typeof(Simulation)) as Simulation;

            Assert.NotNull(sim);

            bool hasBeenRun = (bool)ReflectionUtilities.GetValueOfFieldOrProperty("HasBeenRun", script);

            Assert.False(hasBeenRun);

            IJobManager jobManager = new RunOrganiser(sims, sim, false);
            IJobRunner  jobRunner  = new JobRunnerAsync();

            jobRunner.Run(jobManager, wait: true);

            hasBeenRun = (bool)ReflectionUtilities.GetValueOfFieldOrProperty("HasBeenRun", script);
            Assert.True(hasBeenRun, "Failure in a post simulation tool prevented another post simulation tool from running.");
        }
예제 #4
0
        public void RunAPSIMXGetOutputs()
        {
            APSIMSpecification simulation = GetDefaultSimulationSpec();

            List <APSIMSpecification> simulations = new List <APSIMSpecification>();

            simulations.Add(simulation);

            RuntimeEnvironment environment = new RuntimeEnvironment
            {
                APSIMxBuildNumber = 2473 // issue number that was resovled.
            };

            RunYPJob   job    = new RunYPJob(simulations, environment);
            IJobRunner runner = new JobRunnerAsync();

            runner.Run(job, wait: true);

            // Make sure we don't have an error.
            Assert.AreEqual(job.Errors.Count, 0);

            // Make sure we have a daily output table.
            Assert.AreEqual(job.Outputs.Tables.Count, 1);
            Assert.AreEqual(job.Outputs.Tables[0].TableName, "Daily");

            double[] biomass = DataTableUtilities.GetColumnAsDoubles(job.Outputs.Tables[0], "Wheat.Aboveground.Wt");
            Assert.IsTrue(MathUtilities.Max(biomass) > 5); // make sure something is growing.
        }
예제 #5
0
        /// <summary>Runs the job (.xml file) specified on the command line.</summary>
        /// <returns>True if something was run.</returns>
        private static bool RunJobFromCommandLine(Dictionary <string, string> appSettings)
        {
            if (appSettings.ContainsKey("FileName"))
            {
                string jobFileName = appSettings["FileName"];

                if (File.Exists(jobFileName))
                {
                    appSettings.TryGetValue("APSIMXExecutable", out string executable);

                    string jobXML  = File.ReadAllText(jobFileName);
                    string jobName = Path.GetFileNameWithoutExtension(jobFileName);

                    var environment = new APSIM.Cloud.Shared.RuntimeEnvironment
                    {
                        APSIMRevision  = appSettings["APSIMRevision"],
                        RuntimePackage = appSettings["RuntimePackage"],
                    };

                    RunYPJob job = new RunYPJob(jobXML, environment)
                    {
                        ApsimXExecutable = executable
                    };
                    if (job.Errors.Count == 0)
                    {
                        IJobRunner runner = new JobRunnerAsync();
                        runner.Run(job, wait: true);
                    }

                    if (job.AllFilesZipped != null)
                    {
                        string destZipFileName = Path.ChangeExtension(jobFileName, ".out.zip");
                        using (Stream s = File.Create(destZipFileName))
                        {
                            job.AllFilesZipped.Seek(0, SeekOrigin.Begin);
                            job.AllFilesZipped.CopyTo(s);
                        }
                    }

                    if (job.Errors.Count > 0)
                    {
                        string msg = string.Empty;
                        foreach (string error in job.Errors)
                        {
                            msg += error + Environment.NewLine;
                        }
                        throw new Exception(msg);
                    }
                    return(true);
                }
            }
            return(false);
        }
예제 #6
0
        /// <summary>
        /// Runs an <see cref="IJobManager"/> with all implementations of <see cref="IJobRunner"/>.
        /// </summary>
        /// <param name="jobManager">The job manager to run.</param>
        /// <param name="onSimulationCompleted">Event handler which will be invoked by each job runner after it finishes running.</param>
        private void TestWithAllJobRunners(IJobManager jobManager, EventHandler <AllCompletedArgs> onSimulationCompleted)
        {
            IJobRunner jobRunner = new JobRunnerSync();

            jobRunner.AllJobsCompleted += onSimulationCompleted;
            Assert.DoesNotThrow(() => jobRunner.Run(jobManager, true));

            jobRunner.AllJobsCompleted -= onSimulationCompleted;

            jobRunner = new JobRunnerAsync();
            jobRunner.AllJobsCompleted += onSimulationCompleted;
            Assert.DoesNotThrow(() => jobRunner.Run(jobManager, true));

            jobRunner.AllJobsCompleted -= onSimulationCompleted;

            jobRunner = new JobRunnerMultiProcess(false);
            jobRunner.AllJobsCompleted += onSimulationCompleted;
            Assert.DoesNotThrow(() => jobRunner.Run(jobManager, true));
            jobRunner.AllJobsCompleted -= onSimulationCompleted;
        }
예제 #7
0
        public void RefreshCheckpointNames()
        {
            Simulations sims = Utilities.GetRunnableSim();

            sims.FileName = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString() + ".apsimx");
            sims.Write(sims.FileName);

            Simulation sim     = Apsim.Find(sims, typeof(Simulation)) as Simulation;
            IDataStore storage = Apsim.Find(sims, typeof(IDataStore)) as IDataStore;

            // Record checkpoint names before and after running the simulation,
            // and ensure that they are not the same.
            string[] checkpointNamesBeforeRun = storage.Reader.CheckpointNames.ToArray();

            // Run the simulation
            IJobManager jobManager = new RunOrganiser(sims, sim, false);
            IJobRunner  jobRunner  = new JobRunnerAsync();

            jobRunner.Run(jobManager, wait: true);
            string[] checkpointNamesAfterRun = storage.Reader.CheckpointNames.ToArray();

            Assert.AreNotEqual(checkpointNamesBeforeRun, checkpointNamesAfterRun, "Storage reader failed to update checkpoint names after simulation was run.");
        }
예제 #8
0
        public void RunAPSIMGetError()
        {
            APSIMSpecification simulation = GetDefaultSimulationSpec();

            (simulation.Management[0] as Sow).Cultivar = string.Empty;

            List <APSIMSpecification> simulations = new List <APSIMSpecification>();

            simulations.Add(simulation);

            RuntimeEnvironment environment = new RuntimeEnvironment
            {
                APSIMRevision = "Apsim7.8-R4013"
            };

            RunYPJob   job    = new RunYPJob(simulations, environment);
            IJobRunner runner = new JobRunnerAsync();

            runner.Run(job, wait: true);

            // Make sure we have an error.
            Assert.AreEqual(job.Errors.Count, 1);
            Assert.AreEqual(job.Errors[0], "Cannot sow plant - : Cultivar not specified\r\n     Component name: Paddock.Wheat");
        }
예제 #9
0
        public void RunAPSIMGetOutputs()
        {
            APSIMSpecification simulation = GetDefaultSimulationSpec();

            List <APSIMSpecification> simulations = new List <APSIMSpecification>();

            simulations.Add(simulation);

            RuntimeEnvironment environment = new RuntimeEnvironment
            {
                APSIMRevision = "Apsim7.8-R4013"
            };

            RunYPJob   job    = new RunYPJob(simulations, environment);
            IJobRunner runner = new JobRunnerAsync();

            runner.Run(job, wait: true);

            // Make sure we don't have an error.
            Assert.AreEqual(job.Errors.Count, 0);

            // Make sure we have a daily output table.
            Assert.AreEqual(job.Outputs.Tables.Count, 3);
            Assert.AreEqual(job.Outputs.Tables[0].TableName, "Summary");

            Assert.AreEqual(job.Outputs.Tables[1].TableName, "YieldProphetDaily");
            Assert.AreEqual(job.Outputs.Tables[1].Rows.Count, 92);
            double[] biomass = DataTableUtilities.GetColumnAsDoubles(job.Outputs.Tables[1], "biomass");
            Assert.IsTrue(MathUtilities.Max(biomass) > 20.0); // make sure something is growing.

            // Make sure we have a depth table.
            Assert.AreEqual(job.Outputs.Tables[2].TableName, "YieldProphetDepth");
            Assert.AreEqual(job.Outputs.Tables[2].Rows.Count, 8);
            double[] sw = DataTableUtilities.GetColumnAsDoubles(job.Outputs.Tables[2], "sw");
            Assert.IsTrue(MathUtilities.Max(sw) > 0.0); // make sure there are sw values
        }
예제 #10
0
        /// <summary>Runs the job (.xml file) specified on the command line.</summary>
        /// <returns>True if something was run.</returns>
        private static bool RunJobFromCommandLine(Dictionary <string, string> appSettings)
        {
            if (appSettings.ContainsKey("FileName"))
            {
                string jobFileName = appSettings["FileName"];

                if (File.Exists(jobFileName))
                {
                    var ypEnvironment = new APSIM.Cloud.Shared.RuntimeEnvironment
                    {
                        APSIMRevision  = appSettings["APSIMRevision"],
                        RuntimePackage = appSettings["RuntimePackage"],
                    };

                    if (appSettings.ContainsKey("UpdateFile"))
                    {
                        YieldProphetOld.UpdateFile(jobFileName);
                        return(true);
                    }
                    else if (appSettings.ContainsKey("ConvertToAPSIM"))
                    {
                        string   jobXML = File.ReadAllText(jobFileName);
                        RunYPJob job    = new RunYPJob(jobXML, ypEnvironment, createSims: false);
                        AllocConsole();
                        Console.WriteLine(job.WorkingDirectory);
                        Console.WriteLine();
                        if (job.Errors != null)
                        {
                            string msg = null;
                            foreach (string error in job.Errors)
                            {
                                msg += error + Environment.NewLine;
                            }
                            throw new Exception(msg);
                        }
                        return(true);
                    }
                    else
                    {
                        appSettings.TryGetValue("APSIMXExecutable", out string executable);

                        string jobXML  = File.ReadAllText(jobFileName);
                        string jobName = Path.GetFileNameWithoutExtension(jobFileName);

                        IYPJob job = null;
                        if (jobXML.Contains("Farm4Prophet"))
                        {
                            var environment = new APSIM.Cloud.Shared.RuntimeEnvironment
                            {
                                AusfarmRevision = appSettings["AusfarmRevision"],
                            };
                            job = new RunF4PJob(jobXML, environment);
                        }
                        else
                        {
                            job = new RunYPJob(jobXML, ypEnvironment)
                            {
                                ApsimXExecutable = executable
                            };
                        }
                        if (job.Errors == null || job.Errors.Count == 0)
                        {
                            IJobRunner runner = new JobRunnerAsync();
                            runner.Run(job as IJobManager, wait: true);
                        }

                        if (job.AllFilesZipped != null)
                        {
                            string destZipFileName = Path.ChangeExtension(jobFileName, ".out.zip");
                            using (Stream s = File.Create(destZipFileName))
                            {
                                job.AllFilesZipped.Seek(0, SeekOrigin.Begin);
                                job.AllFilesZipped.CopyTo(s);
                            }
                        }

                        if (job.Errors != null && job.Errors.Count > 0)
                        {
                            string msg = string.Empty;
                            foreach (string error in job.Errors)
                            {
                                msg += error + Environment.NewLine;
                            }
                            throw new Exception(msg);
                        }
                        return(true);
                    }
                }
            }
            return(false);
        }