예제 #1
0
        /// <summary>Called to start the job.</summary>
        /// <param name="jobManager">The job manager running this job.</param>
        /// <param name="workerThread">The thread this job is running on.</param>
        public void Run(JobManager jobManager, BackgroundWorker workerThread)
        {
            // IF we are going to run all simulations, we can delete all tables in the DataStore. This
            // will clean up order of columns in the tables and removed unused ones.
            // Otherwise just remove the unwanted simulations from the DataStore.
            DataStore store = Apsim.Child(simulations, typeof(DataStore)) as DataStore;
            if (model is Simulations)
                store.DeleteAllTables();
            else
                store.RemoveUnwantedSimulations(simulations);
            store.Disconnect();

            JobSequence parentJob = new JobSequence();
            JobParallel simulationJobs = new JobParallel();
            FindAllSimulationsToRun(model, simulationJobs);
            parentJob.Jobs.Add(simulationJobs);
            parentJob.Jobs.Add(new RunAllCompletedEvent(simulations));

            if (runTests)
            {
                foreach (Tests tests in Apsim.ChildrenRecursively(model, typeof(Tests)))
                    parentJob.Jobs.Add(tests);
            }
            jobManager.AddChildJob(this, parentJob);
        }
예제 #2
0
        /// <summary>Find simulations/experiments to run.</summary>
        /// <param name="model">The model and its children to search.</param>
        /// <param name="parentJob">The parent job to add the child jobs to.</param>
        /// <param name="simulationNames">Simulations names found.</param>
        private void FindAllSimulationsToRun(IModel model, JobParallel parentJob, List<string> simulationNames)
        {
            // Get jobs to run and give them to job manager.
            List<JobManager.IRunnable> jobs = new List<JobManager.IRunnable>();

            if (model is Experiment)
            {
                parentJob.Jobs.Add(model as Experiment);
                simulationNames.AddRange((model as Experiment).Names());
            }
            else if (model is Simulation)
            {
                simulationNames.Add(model.Name);
                if (model.Parent == null)
                    parentJob.Jobs.Add(model as Simulation);
                else
                    parentJob.Jobs.Add(new RunClonedSimulation(model as Simulation));
            }
            else
            {
                // Look for simulations.
                foreach (Model child in model.Children)
                    if (child is Experiment || child is Simulation || child is Folder)
                        FindAllSimulationsToRun(child, parentJob, simulationNames);
            }
        }
예제 #3
0
        /// <summary>Create a runnable job for the YP simulations</summary>
        /// <param name="FilesToRun">The files to run.</param>
        /// <param name="ApsimExecutable">APSIM.exe path. Can be null.</param>
        /// <returns>A runnable job for all simulations</returns>
        protected override JobManager.IRunnable CreateRunnableJob(string jobName, string jobXML, string workingDirectory, string ApsimExecutable)
        {
            // Create a sequential job.
            JobSequence completeJob = new JobSequence();
            completeJob.Jobs = new List<JobManager.IRunnable>();

            List<JobManager.IRunnable> jobs = new List<JobManager.IRunnable>();

            // Create a YieldProphet object from our YP xml file
            YieldProphet spec = YieldProphetUtility.YieldProphetFromXML(jobXML, workingDirectory);

            string fileBaseToWrite;
            if (spec.ReportType == YieldProphet.ReportTypeEnum.None && spec.ReportName != null)
                fileBaseToWrite = spec.ReportName;
            else
                fileBaseToWrite = "YieldProphet";

            // Convert YieldProphet spec into a simulation set.
            List<APSIMSpec> simulations = YieldProphetToAPSIM.ToAPSIM(spec);

            // Create all the files needed to run APSIM.
            string apsimFileName = APSIMFiles.Create(simulations, workingDirectory, fileBaseToWrite + ".apsim");

            // Fill in calculated fields.
            foreach (Paddock paddock in spec.Paddock)
                YieldProphetUtility.FillInCalculatedFields(paddock, paddock.ObservedData, workingDirectory);

            // Save YieldProphet.xml to working folder.
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(YieldProphetUtility.YieldProphetToXML(spec));
            doc.Save(Path.Combine(workingDirectory, fileBaseToWrite + ".xml"));

            // Convert .apsim file to sims so that we can run ApsimModel.exe rather than Apsim.exe
            // This will avoid using the old APSIM job runner. It assumes though that there are 
            // no other APSIMJob instances running in the workingDirectory. This is because it
            // looks and runs all .sim files it finds in the workingDirectory.
            JobParallel simJobs = new JobParallel();
            simJobs.Jobs = new List<JobManager.IRunnable>();
            string[] simFileNames = CreateSimFiles(apsimFileName, workingDirectory);
            foreach (string simFileName in simFileNames)
            {
                if (simFileName == null || simFileName.Trim() == string.Empty)
                    throw new Exception("Blank .sim file names found for apsim file: " + apsimFileName);
                simJobs.Jobs.Add(new RunnableJobs.APSIMJob(simFileName, workingDirectory, ApsimExecutable, true));
            }
            completeJob.Jobs.Add(simJobs);
            completeJob.Jobs.Add(new RunnableJobs.APSIMPostSimulationJob(workingDirectory));
            if (spec.Paddock.Count > 0 && spec.Paddock[0].RunType != Paddock.RunTypeEnum.Validation)
                completeJob.Jobs.Add(new RunnableJobs.YPPostSimulationJob(jobName, spec.Paddock[0].NowDate, workingDirectory));

            return completeJob;
        }