Esempio n. 1
0
        /// <summary>Run all simulations.</summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void Run(object sender, System.ComponentModel.DoWorkEventArgs e)
        {
            this.ErrorMessage = null;

            // Get a reference to the JobManager so that we can add jobs to it.
            jobManager = e.Argument as JobManager;

            // Get a reference to our child DataStore.
            DataStore store = Apsim.Child(this, typeof(DataStore)) as DataStore;

            // Remove old simulation data.
            store.RemoveUnwantedSimulations(this);

            Simulation[] simulationsToRun;
            if (SimulationToRun == null)
            {
                // As 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.
                store.DeleteAllTables();

                store.Disconnect();

                simulationsToRun = Simulations.FindAllSimulationsToRun(this);
            }
            else
            {
                simulationsToRun = Simulations.FindAllSimulationsToRun(SimulationToRun);
            }

            MakeSubstitutions(simulationsToRun);

            NumToRun     = simulationsToRun.Length;
            NumCompleted = 0;

            if (NumToRun == 1)
            {
                // Skip running in another thread.
                simulationsToRun[0].Commencing -= OnSimulationCommencing;
                simulationsToRun[0].Commencing += OnSimulationCommencing;
                simulationsToRun[0].Run(null, null);
            }
            else
            {
                foreach (Simulation simulation in simulationsToRun)
                {
                    simulation.Commencing -= OnSimulationCommencing;
                    simulation.Commencing += OnSimulationCommencing;
                    jobManager.AddJob(simulation);
                }
            }
        }
Esempio n. 2
0
            /// <summary>Run the jobs.</summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            public void Run(object sender, System.ComponentModel.DoWorkEventArgs e)
            {
                JobManager jobManager = e.Argument as JobManager;

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

                DataStore store = Apsim.Child(simulations, typeof(DataStore)) as DataStore;

                Simulation[] simulationsToRun;
                if (model is Simulations)
                {
                    // As 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.
                    store.DeleteAllTables();
                    simulationsToRun = Simulations.FindAllSimulationsToRun(simulations);
                }
                else
                {
                    store.RemoveUnwantedSimulations(simulations);

                    if (model is Simulation)
                    {
                        if (model.Parent == null)
                        {
                            // model is already a cloned simulation, probably from user running a single
                            // simulation from an experiment.
                            simulationsToRun = new Simulation[1] {
                                model as Simulation
                            };
                        }
                        else
                        {
                            simulationsToRun = new Simulation[1] {
                                Apsim.Clone(model as Simulation) as Simulation
                            };
                            Simulations.CallOnLoaded(simulationsToRun[0]);
                        }
                    }
                    else
                    {
                        simulationsToRun = Simulations.FindAllSimulationsToRun(model);
                    }
                }

                store.Disconnect();

                simulations.MakeSubstitutions(simulationsToRun);

                foreach (Simulation simulation in simulationsToRun)
                {
                    jobs.Add(simulation);
                    jobManager.AddJob(simulation);
                }

                // Wait until all our jobs are all finished.
                while (AreSomeJobsRunning(jobs))
                {
                    Thread.Sleep(200);
                }

                // Collect all error messages.
                foreach (Simulation job in simulationsToRun)
                {
                    if (job.ErrorMessage != null)
                    {
                        ErrorMessage += job.ErrorMessage + Environment.NewLine;
                    }
                }

                // <summary>Call the all completed event in all models.</summary>
                object[] args = new object[] { this, new EventArgs() };
                foreach (Model childModel in Apsim.ChildrenRecursively(simulations))
                {
                    try
                    {
                        Apsim.CallEventHandler(childModel, "AllCompleted", args);
                    }
                    catch (Exception err)
                    {
                        ErrorMessage += "Error in file: " + simulations.FileName + Environment.NewLine;
                        ErrorMessage += err.ToString() + Environment.NewLine + Environment.NewLine;
                    }
                }

                if (ErrorMessage != null)
                {
                    throw new Exception(ErrorMessage);
                }
            }