Exemple #1
0
        /// <summary>Documents the specified model.</summary>
        /// <param name="modelNameToDocument">The model name to document.</param>
        /// <param name="tags">The auto doc tags.</param>
        /// <param name="headingLevel">The starting heading level.</param>
        public void DocumentModel(string modelNameToDocument, List <AutoDocumentation.ITag> tags, int headingLevel)
        {
            Simulation simulation = Apsim.Find(this, typeof(Simulation)) as Simulation;

            if (simulation != null)
            {
                // Find the model of the right name.
                IModel modelToDocument = Apsim.Find(simulation, modelNameToDocument);

                // If not found then find a model of the specified type.
                if (modelToDocument == null)
                {
                    modelToDocument = Apsim.Get(simulation, "[" + modelNameToDocument + "]") as IModel;
                }

                // If the simulation has the same name as the model we want to document, dig a bit deeper
                if (modelToDocument == simulation)
                {
                    modelToDocument = Apsim.ChildrenRecursivelyVisible(simulation).FirstOrDefault(m => m.Name.Equals(modelNameToDocument, StringComparison.OrdinalIgnoreCase));
                }

                // If still not found throw an error.
                if (modelToDocument != null)
                {
                    // Get the path of the model (relative to parentSimulation) to document so that
                    // when replacements happen below we will point to the replacement model not the
                    // one passed into this method.
                    string pathOfSimulation      = Apsim.FullPath(simulation) + ".";
                    string pathOfModelToDocument = Apsim.FullPath(modelToDocument).Replace(pathOfSimulation, "");

                    // Clone the simulation
                    Simulation clonedSimulation = Apsim.Clone(simulation) as Simulation;

                    // Make any substitutions.
                    Simulations.MakeSubstitutions(this, new List <Simulation> {
                        clonedSimulation
                    });

                    // Now use the path to get the model we want to document.
                    modelToDocument = Apsim.Get(clonedSimulation, pathOfModelToDocument) as IModel;

                    if (modelToDocument == null)
                    {
                        throw new Exception("Cannot find model to document: " + modelNameToDocument);
                    }

                    // resolve all links in cloned simulation.
                    Links links = new Core.Links();
                    links.Resolve(clonedSimulation);

                    // Document the model.
                    modelToDocument.Document(tags, headingLevel, 0);

                    // Unresolve links.
                    links.Unresolve(clonedSimulation);
                }
            }
        }
Exemple #2
0
        /// <summary>Documents the specified model.</summary>
        /// <param name="modelNameToDocument">The model name to document.</param>
        /// <param name="tags">The auto doc tags.</param>
        /// <param name="headingLevel">The starting heading level.</param>
        public void DocumentModel(string modelNameToDocument, List <AutoDocumentation.ITag> tags, int headingLevel)
        {
            Simulation simulation = Apsim.Find(this, typeof(Simulation)) as Simulation;

            if (simulation != null)
            {
                // Find the model of the right name.
                IModel modelToDocument = Apsim.Find(simulation, modelNameToDocument);

                // If not found then find a model of the specified type.
                if (modelToDocument == null)
                {
                    modelToDocument = Apsim.Get(simulation, "[" + modelNameToDocument + "]") as IModel;
                }

                // If still not found throw an error.
                if (modelToDocument == null)
                {
                    throw new ApsimXException(this, "Could not find a model of the name " + modelNameToDocument + ". Simulation file name must match the name of the node to document.");
                }

                // Get the path of the model (relative to parentSimulation) to document so that
                // when replacements happen below we will point to the replacement model not the
                // one passed into this method.
                string pathOfSimulation      = Apsim.FullPath(simulation) + ".";
                string pathOfModelToDocument = Apsim.FullPath(modelToDocument).Replace(pathOfSimulation, "");

                // Clone the simulation
                Simulation clonedSimulation = Apsim.Clone(simulation) as Simulation;

                // Make any substitutions.
                MakeSubstitutions(new Simulation[] { clonedSimulation });

                // Now use the path to get the model we want to document.
                modelToDocument = Apsim.Get(clonedSimulation, pathOfModelToDocument) as IModel;

                // resolve all links in cloned simulation.
                Apsim.ResolveLinks(clonedSimulation);
                foreach (Model child in Apsim.ChildrenRecursively(clonedSimulation))
                {
                    Apsim.ResolveLinks(child);
                }

                // Document the model.
                modelToDocument.Document(tags, headingLevel, 0);

                // Unresolve links.
                Apsim.UnresolveLinks(clonedSimulation);
                foreach (Model child in Apsim.ChildrenRecursively(clonedSimulation))
                {
                    Apsim.UnresolveLinks(child);
                }
            }
        }
Exemple #3
0
        /// <summary>Find all simulations under the specified parent model.</summary>
        /// <param name="parent">The parent.</param>
        /// <returns></returns>
        public static Simulation[] FindAllSimulationsToRun(Model parent)
        {
            List <Simulation> simulations = new List <Simulation>();

            if (parent is Experiment)
            {
                simulations.AddRange((parent as Experiment).Create());
            }
            else if (parent is Simulation)
            {
                Simulation clonedSim = Apsim.Clone(parent) as Simulation;
                CallOnLoaded(clonedSim);
                simulations.Add(clonedSim);
            }
            else
            {
                // Look for simulations.
                foreach (Model model in Apsim.ChildrenRecursively(parent))
                {
                    if (model is Experiment)
                    {
                        simulations.AddRange((model as Experiment).Create());
                    }
                    else if (model is Simulation && !(model.Parent is Experiment))
                    {
                        Simulation clonedSim = Apsim.Clone(model) as Simulation;
                        CallOnLoaded(clonedSim);
                        simulations.Add(clonedSim);
                    }
                }
            }
            // Make sure each simulation has it's filename set correctly.
            foreach (Simulation simulation in simulations)
            {
                if (simulation.FileName == null)
                {
                    simulation.FileName = RootSimulations(parent).FileName;
                }
            }
            return(simulations.ToArray());
        }
Exemple #4
0
        /// <summary>Gets the next job to run</summary>
        public Simulation NextSimulationToRun()
        {
            if (Parent is ISimulationGenerator || hasRun)
            {
                return(null);
            }
            hasRun = true;

            Simulation simulationToRun;

            if (this.Parent == null)
            {
                simulationToRun = this;
            }
            else
            {
                Simulations simulationEngine = Apsim.Parent(this, typeof(Simulations)) as Simulations;
                simulationToRun = Apsim.Clone(this) as Simulation;
                simulationEngine.MakeSubstitutions(simulationToRun);
            }
            return(simulationToRun);
        }
Exemple #5
0
        /// <summary>Gets the next job to run</summary>
        public IRunnable NextJobToRun()
        {
            if (Parent is IJobGenerator || hasRun)
            {
                return(null);
            }
            hasRun = true;

            Simulation simulationToRun;

            if (this.Parent == null)
            {
                simulationToRun = this;
            }
            else
            {
                Simulations simulationEngine = Apsim.Parent(this, typeof(Simulations)) as Simulations;
                simulationToRun = Apsim.Clone(this) as Simulation;
                simulationEngine.MakeSubstitutions(simulationToRun);
            }
            return(new RunSimulation(simulationToRun, doClone: false));
        }
Exemple #6
0
        /// <summary>Gets the next job to run</summary>
        public Simulation NextSimulationToRun(bool doFullFactorial = true)
        {
            if (Parent is ISimulationGenerator || hasRun)
            {
                return(null);
            }
            hasRun = true;

            Simulation simulationToRun;

            if (this.Parent == null)
            {
                simulationToRun = this;
            }
            else
            {
                Simulations simulationEngine = Apsim.Parent(this, typeof(Simulations)) as Simulations;
                simulationToRun = Apsim.Clone(this) as Simulation;

                // We are breaking.NET naming rules with our manager scripts.All our manager scripts are class
                // Script in the Models namespace.This is OK until we do a clone(binary serialise/deserialise).
                // When this happens, the serialisation engine seems to choose the first assembly it can find
                // that has the 'right' code.It seems that if the script c# is close to an existing assembly then
                // it chooses that assembly. In the attached .apsimx, it chooses the wrong temporary assembly for
                // SowingRule2. It chooses SowingRule assembly even though the script for the 2 manager files is
                // different. I'm not sure how to fix this yet. A brute force way is to recompile all manager
                // scripts after cloning.
                // https://github.com/APSIMInitiative/ApsimX/issues/2603
                Events          events     = new Events(simulationToRun);
                LoadedEventArgs loadedArgs = new LoadedEventArgs();
                events.Publish("Loaded", new object[] { simulationToRun, loadedArgs });

                simulationEngine.MakeSubstitutions(simulationToRun);
            }
            return(simulationToRun);
        }
Exemple #7
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);
                }
            }