public static List <string> CreateSimulationJobs(ApsimFile _F, string[] SimsToRun, ref List <JobScheduler.Job> ApsimJobs, ProgressNotifier Notifier)
        {
            List <string> SimFiles = new List <string>();

            foreach (string FullSimulationPath in SimsToRun)
            {
                Notifier(0, "Opening " + FullSimulationPath);
                FactorBuilder builder       = new FactorBuilder();
                List <string> allFactorials = new List <string>();
                int           totalCount    = 0;
                foreach (FactorItem item in builder.BuildFactorItems(_F.FactorComponent, FullSimulationPath))
                {
                    totalCount += item.CalcCount();
                    var scratch = new SortedDictionary <string, string>();
                    item.CalcFactorialList(allFactorials, "", scratch);
                }


                string FullSimulationPathName = _F.Find(FullSimulationPath).Name;
                int    sequenceNumber         = 0;
                foreach (string str in allFactorials)
                {
                    SimFiles.Add(FullSimulationPath + "@factorial=" + str);
                    Notifier((int)(100 * ((double)sequenceNumber / totalCount)), "Found factorial " + sequenceNumber.ToString());
                    sequenceNumber++;
                }
            }
            return(SimFiles);
        }
        public static List <string> CalcFactorialList(ApsimFile _F, string SimulationPath)
        {
            FactorBuilder builder       = new FactorBuilder();
            List <string> allFactorials = new List <string>();

            foreach (FactorItem item in builder.BuildFactorItems(_F.FactorComponent, SimulationPath))
            {
                var scratch = new SortedDictionary <string, string>();
                item.CalcFactorialList(allFactorials, "", scratch);
            }
            return(allFactorials);
        }
        public static List <string> CreateSimFiles(ApsimFile _F, string[] SimsToRun, string destFolder = "")
        {
            List <string> SimFiles = new List <string>();

            foreach (string FullSimulationPath in SimsToRun)
            {
                string[] simPathParts          = FullSimulationPath.Split(new string[] { "@factorial=" }, StringSplitOptions.None);
                string   simXmlPath            = simPathParts[0];
                string   simPathFactorInstance = "";
                if (simPathParts.Count() > 1)
                {
                    simPathFactorInstance = simPathParts[1].Trim(new char[] { '\'', ' ' });
                }

                FactorBuilder builder       = new FactorBuilder();
                List <string> allFactorials = new List <string>();
                foreach (FactorItem item in builder.BuildFactorItems(_F.FactorComponent, simXmlPath))
                {
                    var scratch = new SortedDictionary <string, string>();
                    item.CalcFactorialList(allFactorials, "", scratch);
                }

                if (simPathFactorInstance != "")
                {
                    var myFactors      = new SortedDictionary <string, string>(simPathFactorInstance.Split(';').Select(s => s.Split('=')).ToDictionary(a => a[0].Trim(), a => a[1].Trim()));
                    var sequenceNumber = 1 + allFactorials.FindIndex(x => x == simPathFactorInstance);
                    if (sequenceNumber < 1)
                    {
                        throw new Exception("factor level '" + simPathFactorInstance + "' not found");
                    }
                    //work on a copy of the file - names will change during the processing of the factorial nodes
                    SimFiles.Add(Factor.ProcessSingleSimulation(CreateCopy(_F), simXmlPath, myFactors, sequenceNumber, destFolder));
                }
                else
                {
                    foreach (string str in allFactorials)
                    {
                        var myFactors      = new SortedDictionary <string, string>(str.Split(';').Select(s => s.Split('=')).ToDictionary(a => a[0].Trim(), a => a[1].Trim()));
                        var sequenceNumber = 1 + allFactorials.FindIndex(x => x == str);
                        SimFiles.Add(Factor.ProcessSingleSimulation(CreateCopy(_F), simXmlPath, myFactors, sequenceNumber, destFolder));
                    }
                }
            }
            return(SimFiles);
        }
        private static string ProcessSingleSimulation(ApsimFile copiedFile, string SimulationPath, SortedDictionary <string, string> factorInstance, int uniqueId, string destFolder = "")
        {
            if (copiedFile == null)
            {
                throw new Exception("copiedFile is null in ProcessSingleSimulation");
            }
            if (factorInstance == null)
            {
                throw new Exception("factorInstance is null in ProcessSingleSimulation");
            }

            Component Simulation = copiedFile.Find(SimulationPath);

            if (Simulation == null)
            {
                throw new Exception("Simulation is null in ProcessSingleSimulation");
            }

            string rootName   = Simulation.Name;
            int    totalCount = 0;

            try
            {
                FactorBuilder     builder = new FactorBuilder();
                List <FactorItem> items   = builder.BuildFactorItems(copiedFile.FactorComponent, SimulationPath);

                foreach (FactorItem item in items)
                {
                    totalCount += item.CalcCount();
                    item.Process(Simulation, "", factorInstance);
                }

                Simulation.Name = simulationName(builder.SaveExtraInfoInFilename, rootName, factorInstance, uniqueId, totalCount);
                return(CreateJobFromSimulation(Simulation, factorInstance, builder.TitleIsSingleLine, destFolder));
            }
            catch (Exception ex)
            {
                throw new Exception("Error encountered creating Factorials. \r\n" +
                                    "SimulationPath: " + SimulationPath + "\r\n" +
                                    "Total count: " + totalCount +
                                    "Exception: " + ex.ToString());
            }
        }
 public ManagerFactorItem(FactorBuilder b)
     : base(b)
 {
 }
 public FactorItem(FactorBuilder b)
 {
     Builder = b;
 }
Exemple #7
0
        private int FillProjectWithAllFactorialJobs(ApsimFile AFile, string FileName, ref List <IJob> jobs)
        {
            int numFound = 0;

            if (AFile.FactorComponent != null)
            {
                List <string> SimulationPaths = new List <string>();
                ApsimFile.ExpandSimsToRun(AFile.RootComponent, ref SimulationPaths);
                foreach (string simXmlPath in SimulationPaths)
                {
                    FactorBuilder b             = new FactorBuilder(AFile.FactorComponent);
                    Component     Simulation    = AFile.Find(simXmlPath);
                    List <string> allFactorials = Factor.CalcFactorialList(AFile, simXmlPath);
                    int           totalCount    = allFactorials.Count;
                    for (int instanceCount = 1; instanceCount <= totalCount; instanceCount++)
                    {
                        string rootName = Simulation.Name;
                        if (b.SaveExtraInfoInFilename)
                        {
                            rootName += ";" + allFactorials [instanceCount - 1];
                        }
                        else
                        {
                            //write a spacer to list sims in order eg: 01 or 001 or 0001 depending on count
                            string sPad = "";
                            double tot  = Math.Floor(Math.Log10(totalCount) + 1);
                            double file = Math.Floor(Math.Log10(instanceCount) + 1);
                            for (int i = 0; i < (int)(tot - file); ++i)
                            {
                                sPad += "0";
                            }

                            rootName += "_" + sPad + instanceCount;
                        }
                        Job J = new Job();
                        J.WorkingDirectory = Path.GetDirectoryName(FileName);
                        string fullSimPath = simXmlPath + "@factorial=" + allFactorials [instanceCount - 1];
                        J.CommandLine += StringManip.DQuote(Path.Combine(Configuration.ApsimBinDirectory(), "ApsimToSim.exe")) +
                                         " " + StringManip.DQuote(FileName) + " " + StringManip.DQuote("Simulation=" + fullSimPath);
                        J.CommandLineUnix     = "mono " + J.CommandLine;
                        J.Name                = "ApsimToSim " + FileName + " " + fullSimPath;
                        J.DependsOn           = new List <DependsOn> ();
                        J.SendStdErrToConsole = false;
                        jobs.Add(J);

                        J = new Job();
                        J.WorkingDirectory = Path.GetDirectoryName(FileName);
                        J.CommandLine     += StringManip.DQuote(Path.Combine(Configuration.ApsimBinDirectory(), "ApsimModel.exe")) +
                                             " " + StringManip.DQuote(rootName + ".sim");
                        J.CommandLineUnix = J.CommandLine;
                        J.Name            = "ApsimModel " + rootName + ".sim";
                        J.DependsOn       = new List <DependsOn> ();
                        J.DependsOn.Add(new DependsOn("ApsimToSim " + FileName + " " + fullSimPath));
                        J.SendStdErrToConsole = false;
                        J.StdOutFilename      = Path.Combine(Path.GetDirectoryName(FileName), rootName + ".sum");
                        jobs.Add(J);


                        J = CleanupJob(Path.Combine(Path.GetDirectoryName(FileName), rootName + ".sim"),
                                       "ApsimModel " + rootName + ".sim");
                        jobs.Add(J);
                        numFound++;
                    }
                }
            }
            return(numFound);
        }
Exemple #8
0
        /// <summary>
        /// The specified ApsimFile has a factorial in it. Create a series of jobs and add to ApsimJobs.
        /// </summary>
        private int FillProjectWithSpecifiedFactorialJobs(ApsimFile AFile, string FileName, string[] SimulationPaths, ref List <IJob> jobs)
        {
            int numFound = 0;

            if (AFile.FactorComponent != null)
            {
                foreach (string SimulationPath in SimulationPaths)
                {
                    string[] simPathParts          = SimulationPath.Split(new string[] { "@factorial=" }, StringSplitOptions.None);
                    string   simXmlPath            = simPathParts [0];
                    string   simPathFactorInstance = "";
                    if (simPathParts.Length < 2)
                    {
                        continue;
                    }
                    simPathFactorInstance = simPathParts [1].Trim(new char[] { '\'', ' ' });
                    List <string> allFactorials = Factor.CalcFactorialList(AFile, simXmlPath);
                    int           totalCount    = allFactorials.Count;
                    int           instanceCount = 1 + allFactorials.IndexOf(simPathFactorInstance);
                    if (instanceCount < 1)
                    {
                        throw new Exception("Factor level " + simPathFactorInstance + "wasnt found");
                    }
                    FactorBuilder b          = new FactorBuilder(AFile.FactorComponent);
                    Component     Simulation = AFile.Find(simXmlPath);
                    string        rootName   = Simulation.Name;
                    if (b.SaveExtraInfoInFilename)
                    {
                        rootName += ";" + simPathFactorInstance;
                    }
                    else
                    {
                        //write a spacer to list sims in order eg: 01 or 001 or 0001 depending on count
                        string sPad = "";
                        double tot  = Math.Floor(Math.Log10(totalCount) + 1);
                        double file = Math.Floor(Math.Log10(instanceCount) + 1);
                        for (int i = 0; i < (int)(tot - file); ++i)
                        {
                            sPad += "0";
                        }

                        rootName += "_" + sPad + instanceCount;
                    }
                    Job J = new Job();
                    J.WorkingDirectory = Path.GetDirectoryName(FileName);

                    J.CommandLine += StringManip.DQuote(Path.Combine(Configuration.ApsimBinDirectory(), "ApsimToSim.exe")) +
                                     " " + StringManip.DQuote(FileName) + " " + StringManip.DQuote("Simulation=" + SimulationPath);
                    J.CommandLineUnix     = "mono " + J.CommandLine;
                    J.Name                = "ApsimToSim " + FileName + " " + SimulationPath;
                    J.DependsOn           = new List <DependsOn> ();
                    J.SendStdErrToConsole = false;
                    jobs.Add(J);

                    J = new Job();
                    J.WorkingDirectory = Path.GetDirectoryName(FileName);
                    J.CommandLine     += StringManip.DQuote(Path.Combine(Configuration.ApsimBinDirectory(), "ApsimModel.exe")) +
                                         " " + StringManip.DQuote(rootName + ".sim");
                    J.CommandLineUnix = J.CommandLine;
                    J.Name            = "ApsimModel " + rootName + ".sim";
                    J.DependsOn       = new List <DependsOn> ();
                    J.DependsOn.Add(new DependsOn("ApsimToSim " + FileName + " " + SimulationPath));
                    J.SendStdErrToConsole = false;
                    J.StdOutFilename      = Path.Combine(Path.GetDirectoryName(FileName), rootName + ".sum");
                    jobs.Add(J);


                    J = CleanupJob(Path.Combine(Path.GetDirectoryName(FileName), rootName + ".sim"),
                                   "ApsimModel " + rootName + ".sim");
                    jobs.Add(J);
                    numFound++;
                }
            }
            return(numFound);
        }