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); }
internal void ResolveShortcuts() { // ------------------------------------------------------ // Now that reading is fully done we can go and resolve // any shortcuts that we found during reading. Do // this recursively for all children. // Some "compound" objects shouldnt be resolved - eg. outputfile, area, but their children should be // ------------------------------------------------------ if (TempShortCutName != "") { MyShortCutTo = MyFile.Find(TempShortCutName); TempShortCutName = ""; CheckOutputFileNode(); } foreach (Component Child in ChildNodes) { Child.ResolveShortcuts(); } }
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()); } }
private void ConvertApsimToSim(string ApsimFileName, string SimName, bool dontWriteSimFiles) { //if the filename is not 'rooted' then assume that the user intends to use the current working directory as the root ApsimFileName = ApsimFileName.Replace("\"", ""); if (!Path.IsPathRooted(ApsimFileName)) { ApsimFileName = Path.Combine(Directory.GetCurrentDirectory(), ApsimFileName); } //double-check file actually exists before proceeding if (!File.Exists(ApsimFileName)) { throw new Exception("Error: Specified APSIM File does not exist!\n\t" + ApsimFileName); } PlugIns.LoadAll(); Directory.SetCurrentDirectory(Path.GetDirectoryName(ApsimFileName)); // convert the specified simulations in the specified apsim file name // into a separate .sim file for each. ApsimFile.ApsimFile Apsim = new ApsimFile.ApsimFile(); Apsim.OpenFile(ApsimFileName); if (Apsim.FactorComponent == null) { FindSimsAndConvert(Apsim.RootComponent, SimName, dontWriteSimFiles); } else { bool factorialActive = XmlHelper.Value(Apsim.FactorComponent.ContentsAsXML, "active") == "1"; if (SimName.Contains("@factorial=")) { foreach (string simFileName in Factor.CreateSimFiles(Apsim, new string[] { SimName }, Directory.GetCurrentDirectory())) { Console.Error.WriteLine("Written " + simFileName); } } else if (factorialActive) { List <string> simulationPaths = new List <string>(); ApsimFile.ApsimFile.ExpandSimsToRun(Apsim.RootComponent, ref simulationPaths); foreach (string simXmlPath in simulationPaths) { FactorBuilder b = new FactorBuilder(Apsim.FactorComponent); Component Simulation = Apsim.Find(simXmlPath); List <string> allFactorials = Factor.CalcFactorialList(Apsim, simXmlPath); int totalCount = allFactorials.Count; Parallel.For(1, totalCount + 1, instanceCount => //for (int instanceCount = 0; 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; } string fullSimPath = simXmlPath + "@factorial=" + allFactorials[instanceCount - 1]; if (dontWriteSimFiles) { Console.WriteLine("Written " + fullSimPath); } else { Factor.CreateSimFiles(Apsim, new string[] { fullSimPath }, Directory.GetCurrentDirectory()); } }); } } else { FindSimsAndConvert(Apsim.RootComponent, SimName, dontWriteSimFiles); } } }
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); }
/// <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); }
public void RunApsim(ToolStrip Strip, BaseController Controller)//ApsimFile.ApsimFile F, StringCollection SelectedPaths) { // ---------------------------------------------------------- // Run APSIM for the specified file and simulation paths. // This method will also locate and look after the various // run button states. // ---------------------------------------------------------- // JKB 14/02/11 // changed function call as Controller reference was needed for factorial mode // was - RunApsim(ToolStrip Strip, ApsimFile.ApsimFile F, StringCollection SelectedPaths) _F = Controller.ApsimData; _SelectedPaths = Controller.SelectedPaths; _Strip = Strip; _Strip.Visible = true; ToolStripButton RunButton = (ToolStripButton)_Strip.Items["RunButton"]; ToolStripButton StopButton = (ToolStripButton)_Strip.Items["StopButton"]; ToolStripButton ErrorsButton = (ToolStripButton)_Strip.Items["ErrorsButton"]; ToolStripLabel PercentLabel = (ToolStripLabel)_Strip.Items["PercentLabel"]; ToolStripProgressBar ProgressBar = (ToolStripProgressBar)_Strip.Items["RunProgress"]; RunButton.Enabled = false; StopButton.Enabled = true; ErrorsButton.Visible = false; PercentLabel.Text = ""; ProgressBar.Value = 0; // Get a list of simulations to run. List <String> SimsToRun = new List <String>(); List <String> simNames = new List <String>(); List <String> simList = new List <String>(); foreach (String SimulationPath in _SelectedPaths) { ApsimFile.ApsimFile.ExpandSimsToRun(_F.Find(SimulationPath), ref SimsToRun); } // JF 061211 - Added check for duplicate simulation names in different folders. //Create a list of sim names simList.AddRange(ApsimFile.ApsimFile.GetSimsInApsimFile(_F.FileName)); List <String> duplicates = new List <String>(); for (int i = 0; i < simList.Count; i++) { String[] split = simList[i].Split('/'); simNames.Add(split[split.Length - 1]); } //compare them simNames.Sort(); for (int i = 0; i < simNames.Count - 1; i++) { if (simNames[i].ToLower().Equals(simNames[i + 1].ToLower())) { if (!duplicates.Contains(simNames[i])) { duplicates.Add(simNames[i]); } } } //if duplicates are found, return with error message if (duplicates.Count > 0) { simNames.Clear(); foreach (String dupe in duplicates) { foreach (String list in simList) { String[] name = list.Split('/'); if (name[name.Length - 1].Equals(dupe)) { simNames.Add(list); } } } String output = ""; foreach (String s in simNames) { output += s + "\n"; } MessageBox.Show("Error: The following simulations have the same name: \n" + output); //reset the menu bar RunButton.Enabled = true; StopButton.Enabled = false; ErrorsButton.Visible = false; PercentLabel.Text = ""; return; } if (SimsToRun.Count >= 1) { //See if the factorial component is "active" - whether the user wants just one or all simulations bool doAllFactors = true; if (_F.FactorComponent != null) { XmlNode varNode = _F.FactorComponent.ContentsAsXML.SelectSingleNode("//active"); if (varNode != null) { doAllFactors = XmlHelper.Value(_F.FactorComponent.ContentsAsXML, "active") == "1"; } } Timer.Enabled = true; Apsim.Start(new List <RunApsim.apsimRunFileSims> { new RunApsim.apsimRunFileSims { fileName = _F.FileName, simulationPaths = SimsToRun } }, doAllFactors); } }