Esempio n. 1
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.");
        }
Esempio n. 2
0
        /// <summary>
        /// Generates .apsimx files for each child model under a given model.
        /// Returns false if errors were encountered, or true otherwise.
        /// </summary>
        /// <param name="model">Model to generate .apsimx files for.</param>
        /// <param name="path">
        /// Path which the files will be saved to.
        /// If null, the user will be prompted to choose a directory.
        /// </param>
        public bool GenerateApsimXFiles(IModel model, string path = null)
        {
            if (string.IsNullOrEmpty(path))
            {
                IFileDialog fileChooser = new FileDialog()
                {
                    Prompt = "Select a directory to save model files to.",
                    Action = FileDialog.FileActionType.SelectFolder
                };
                path = fileChooser.GetFile();
                if (!string.IsNullOrEmpty(path))
                {
                    MainPresenter.ShowMessage("Generating simulation files: ", Simulation.MessageType.Information);

                    RunOrganiser organiser = new RunOrganiser(ApsimXFile, model, false);
                    var          errors    = organiser.GenerateApsimXFiles(path, (int percent) =>
                    {
                        MainPresenter.ShowProgress(percent, false);
                    });

                    if (errors == null || errors.Count == 0)
                    {
                        MainPresenter.ShowMessage("Successfully generated .apsimx files under " + path + ".", Simulation.MessageType.Information);
                        return(true);
                    }
                    else
                    {
                        MainPresenter.ShowError(errors);
                        return(false);
                    }
                }
            }
            return(true);
        }
Esempio n. 3
0
        /// <summary>
        /// Clean up at the end of a set of runs. Stops the job manager, timers, etc.
        /// </summary>
        private void Stop()
        {
            timer.Stop();
            stopwatch.Stop();
            jobRunner.Stop();

            IsRunning  = false;
            jobManager = null;
            jobRunner  = null;
        }
Esempio n. 4
0
        /// <summary>
        /// Clean up at the end of a set of runs. Stops the job manager, timers, etc.
        /// </summary>
        private void Stop()
        {
            this.explorerPresenter.MainPresenter.RemoveStopHandler(OnStopSimulation);
            timer.Stop();
            stopwatch.Stop();
            jobRunner.Stop();

            IsRunning  = false;
            jobManager = null;
            jobRunner  = null;
        }
Esempio n. 5
0
 public void RunPostSimulationModels(object sender, EventArgs e)
 {
     try
     {
         // Run all child model post processors.
         RunOrganiser.RunPostSimulationTools(explorerPresenter.ApsimXFile, storage);
         this.explorerPresenter.MainPresenter.ShowMessage("Post processing models have successfully completed", Simulation.MessageType.Information);
     }
     catch (Exception err)
     {
         explorerPresenter.MainPresenter.ShowError(err);
     }
 }
Esempio n. 6
0
        /// <summary>Constructor</summary>
        /// <param name="model">The model the user has selected to run</param>
        /// <param name="presenter">The explorer presenter.</param>
        /// <param name="multiProcess">Use the multi-process runner?</param>
        /// <param name="storage">A storage writer where all data should be stored</param>
        public RunCommand(IModel model, ExplorerPresenter presenter, bool multiProcess, IStorageWriter storage)
        {
            this.jobName           = model.Name;
            this.explorerPresenter = presenter;

            jobManager = Runner.ForSimulations(explorerPresenter.ApsimXFile, model, false);

            if (multiProcess)
            {
                jobRunner = new JobRunnerMultiProcess(storage);
            }
            else
            {
                jobRunner = new JobRunnerAsync();
            }
            jobRunner.JobCompleted     += OnJobCompleded;
            jobRunner.AllJobsCompleted += OnAllJobsCompleted;
        }
Esempio n. 7
0
        /// <summary>Constructor</summary>
        /// <param name="model">The model the user has selected to run</param>
        /// <param name="presenter">The explorer presenter.</param>
        /// <param name="multiProcess">Use the multi-process runner?</param>
        /// <param name="storage">A storage writer where all data should be stored</param>
        public RunCommand(IModel model, ExplorerPresenter presenter, bool multiProcess)
        {
            this.jobName           = model.Name;
            this.explorerPresenter = presenter;
            this.explorerPresenter.MainPresenter.AddStopHandler(OnStopSimulation);
            jobManager = Runner.ForSimulations(explorerPresenter.ApsimXFile, model, true);

            if (multiProcess)
            {
                jobRunner = new JobRunnerMultiProcess(false);
            }
            else
            {
                jobRunner = new JobRunnerAsync();
            }
            jobRunner.JobCompleted     += OnJobCompleded;
            jobRunner.AllJobsCompleted += OnAllJobsCompleted;
        }
Esempio n. 8
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.");
        }