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); }
// Fired when the current selection has changed. public BaseController(Form MainForm, string SectionName, bool MainController) { // ----------------------- // constructor // ----------------------- MyMainForm = MainForm; ApsimFile.Configuration.Instance.ApplicationName = SectionName; ApsimData = new ApsimFile.ApsimFile(); // Setup the image lists. LargeIcons = new ImageList(); MediumIcons = new ImageList(); SmallIcons = new ImageList(); LargeIcons.ImageSize = new Size(32, 32); MediumIcons.ImageSize = new Size(24, 24); SmallIcons.ImageSize = new Size(16, 16); LargeIcons.Tag = "LargeIcon"; MediumIcons.Tag = "MediumIcon"; SmallIcons.Tag = "SmallIcon"; ImageFileNames = new List <string>(); ImageIndexes = new List <int>(); // Setup the types foreach (string FileName in Types.Instance.SmallIconFileNames()) { LoadIcon(FileName, ref SmallIcons); } foreach (string FileName in Types.Instance.LargeIconFileNames()) { LoadIcon(FileName, ref LargeIcons); } // Setup the actions file and load all images specified by it. string ActionFileName = Configuration.Instance.Setting("ActionFile"); if (!string.IsNullOrEmpty(ActionFileName)) { XmlDocument ActionDoc = new XmlDocument(); ActionDoc.Load(ActionFileName); ActionFile = ActionDoc.DocumentElement; XmlNode ActionsNode = XmlHelper.Find(ActionDoc.DocumentElement, "Actions"); List <string> SmallIconFileNames = XmlHelper.ValuesRecursive(ActionsNode, "SmallIcon"); foreach (string IconFileName in SmallIconFileNames) { LoadIcon(IconFileName, ref SmallIcons); } List <string> MediumIconFileNames = XmlHelper.ValuesRecursive(ActionsNode, "MediumIcon"); foreach (string IconFileName in MediumIconFileNames) { LoadIcon(IconFileName, ref MediumIcons); } } if (MainController) { ApsimData.FileNameChanged += OnFileNameChanged; } }
internal Component(ApsimFile File, Component Parent, Component Source) { this.MyFile = File; Name = Source.Name; Type = Source.Type; this.MyParent = Parent; MyShortCutTo = Source; }
public static List <string> GetSimsInApsimFile(string FileName) { List <string> SimsToRun = new List <string>(); ApsimFile Apsim = new ApsimFile(FileName); ExpandSimsToRun(Apsim.RootComponent, ref SimsToRun); return(SimsToRun); }
private static ApsimFile CreateCopy(ApsimFile apsimfile) { return(apsimfile.Clone()); //XmlDocument doc = new XmlDocument(); //doc.LoadXml(apsimfile.RootComponent.FullXML()); //XmlHelper.SetAttribute(doc.DocumentElement, "version", APSIMChangeTool.CurrentVersion.ToString()); //ApsimFile tmpFile = new ApsimFile(); //tmpFile.New(doc.OuterXml); //return tmpFile; }
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); }
public ApsimFile Clone() { ApsimFile newFile = new ApsimFile(); if (MyRootNode != null) { newFile.MyRootNode = MyRootNode.Clone(newFile, null); } if (MyFactorNode != null) { newFile.MyFactorNode = MyFactorNode.Clone(newFile, newFile.MyRootNode); } newFile.Dirty = Dirty; newFile.ReadOnly = ReadOnly; newFile.MyFileName = MyFileName; newFile.DisabledEventCount = DisabledEventCount; return(newFile); }
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 Component Clone(ApsimFile file, Component parent) { Component newComponent = new Component(); newComponent.MyName = MyName; newComponent.MyType = MyType; newComponent.MyContents = MyContents; newComponent.MyDescription = MyDescription; newComponent.MyEnabled = MyEnabled; newComponent.MyShortCutTo = MyShortCutTo; newComponent.MyFile = file; newComponent.MyParent = parent; newComponent.TempShortCutName = TempShortCutName; foreach (Component child in MyChildNodes) { newComponent.MyChildNodes.Add(child.Clone(file, newComponent)); } return(newComponent); }
internal Component(ApsimFile File, Component Parent) { this.MyFile = File; this.MyParent = Parent; }
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); } } }
// Add individual .apsim files to the job public void AddFiles(XmlNode job, List <string> FilesToRun, ProgressNotifier Notifier) { PlugIns.LoadAll(); foreach (string FileName in FilesToRun) { if (Notifier != null) { Notifier(0, "Reading " + FileName); } XmlDocument Doc = new XmlDocument(); Doc.Load(FileName); XmlNode apsimFileNode = job.AppendChild(job.OwnerDocument.CreateElement("apsimfile")); XmlHelper.SetAttribute(apsimFileNode, "source", FileName); List <string> globalInputs = new List <string>(); foreach (XmlNode pluginNode in Doc.DocumentElement.OwnerDocument.SelectNodes("//PlugIns/PlugIn[@enabled='yes']")) { if (pluginNode.InnerText.IndexOf("%apsim%") < 0) { if (!File.Exists(pluginNode.InnerText)) { throw new Exception("Plugin file '" + pluginNode.InnerText + "' doesnt exist - cant send it to the cluster."); } File.Copy(pluginNode.InnerText, Path.Combine(WorkingFolder, Path.GetFileName(pluginNode.InnerText))); pluginNode.InnerText = Path.GetFileName(pluginNode.InnerText); if (!globalInputs.Contains(pluginNode.InnerText)) { globalInputs.Add(pluginNode.InnerText); } } } List <XmlNode> simulations = new List <XmlNode>(); XmlHelper.FindAllRecursivelyByType(Doc.DocumentElement, "simulation", ref simulations); foreach (XmlNode simulation in simulations) { if (XmlHelper.Attribute(simulation, "enabled") != "no") { List <string> simsToRun = new List <string>(); XmlNode factorialNode = XmlHelper.FindByType(Doc.DocumentElement, "factorial"); if (factorialNode == null) { simsToRun.Add(XmlHelper.FullPath(simulation)); } else { ApsimFile F = new ApsimFile(); F.OpenFile(FileName); simsToRun = Factor.CreateSimulationNames(F, new string [] { XmlHelper.FullPath(simulation) }, Notifier); } foreach (string simToRun in simsToRun) { XmlNode simulationNode = apsimFileNode.AppendChild(apsimFileNode.OwnerDocument.CreateElement("simulation")); XmlHelper.SetAttribute(simulationNode, "name", simToRun); XmlHelper.SetAttribute(simulationNode, "source", XmlHelper.Attribute(apsimFileNode, "source")); foreach (var g in globalInputs) { XmlNode input = simulationNode.AppendChild(simulationNode.OwnerDocument.CreateElement("input")); XmlHelper.SetAttribute(input, "source", g); XmlHelper.SetAttribute(input, "name", g); } var filenames = new List <XmlNode>(); filenames = simulation.SelectNodes(".//filename").Cast <XmlNode>().ToList(); if (factorialNode != null) { // Send all input files to each job foreach (XmlNode n in factorialNode.SelectNodes(".//filename")) { if (n.InnerText.IndexOf(",") > 0) { foreach (string f in n.InnerText.Split(',')) { XmlNode d = n.CloneNode(false); d.InnerText = f; filenames.Add(d); } } else { filenames.Add(n); } } } foreach (XmlNode linkedNode in simulation.SelectNodes(".//*[string(@shortcut)]")) { XmlNode target = XmlHelper.Find(linkedNode, linkedNode.Attributes["shortcut"].Value); if (target != null) { foreach (XmlNode targetFilename in target.SelectNodes(".//filename")) { filenames.Add(targetFilename); } } } foreach (XmlNode node in filenames) { if (XmlHelper.Attribute(node, "output") != "yes" && XmlHelper.Attribute(node.ParentNode, "enabled") != "no") { string src = Configuration.RemoveMacros(node.InnerText); string dest = Path.GetFileName(src); XmlNode input = simulationNode.AppendChild(simulationNode.OwnerDocument.CreateElement("input")); if (!File.Exists(src) && !File.Exists(Path.Combine(WorkingFolder, dest))) { // When this is called by web service then can't assume src is relative to working // directory. Instead see if the file is relative to where the main file file. src = Path.Combine(Path.GetDirectoryName(FileName), Path.GetFileName(src)); if (!File.Exists(src)) { throw new Exception("File '" + src + "' doesnt exist - cant send it to the cluster."); } } XmlHelper.SetAttribute(input, "source", src); XmlHelper.SetAttribute(input, "name", dest); node.InnerText = dest; if (!File.Exists(Path.Combine(WorkingFolder, dest))) { File.Copy(src, Path.Combine(WorkingFolder, dest)); } } else { XmlNode output = simulationNode.AppendChild(simulationNode.OwnerDocument.CreateElement("output")); XmlHelper.SetAttribute(output, "name", node.InnerText); } } } } } Doc.Save(Path.Combine(WorkingFolder, Path.GetFileName(FileName))); } }
/// <summary> /// Create, and add to ApsimJobs, a series of jobs to run APSIM for each simulation. /// </summary> private void CreateJobsFromAPSIM(string FileName, string[] SimulationPaths, ref List <IJob> jobs, bool doAllFactors) { // Load all plugin (.xml) files. if (Types.Instance.TypeNames.Length == 0) { PlugIns.LoadAll(); } ApsimFile AFile = new ApsimFile(FileName); // Look for factorials. if (FillProjectWithSpecifiedFactorialJobs(AFile, FileName, SimulationPaths, ref jobs) > 0) { return; } else if (doAllFactors && FillProjectWithAllFactorialJobs(AFile, FileName, ref jobs) > 0) { return; } // No factorials. if (SimulationPaths == null || SimulationPaths.Length == 0) { // No simulation specified. Run everything. string ApsimToSimExe = Path.Combine(Configuration.ApsimBinDirectory(), "ApsimToSim.exe"); Process ApsimToSim = new Process(); ApsimToSim.StartInfo.FileName = ApsimToSimExe; ApsimToSim.StartInfo.Arguments = StringManip.DQuote(Path.GetFileName(FileName)); ApsimToSim.StartInfo.WorkingDirectory = Directory.GetCurrentDirectory(); ApsimToSim.StartInfo.CreateNoWindow = true; ApsimToSim.StartInfo.UseShellExecute = false; ApsimToSim.Start(); ApsimToSim.WaitForExit(); List <String> AllPaths = new List <String>(); ApsimFile.ExpandSimsToRun(AFile.RootComponent, ref AllPaths); for (int i = 0; i < AllPaths.Count; i++) { int lastSlash = AllPaths[i].LastIndexOf('/'); if (lastSlash != -1) { AllPaths[i] = Path.Combine(Directory.GetCurrentDirectory(), AllPaths[i].Substring(lastSlash + 1)) + ".sim"; } } // Create a series of jobs for each simulation in the .con file. foreach (string SimFileName in AllPaths) { Job J = CreateJob(SimFileName, SimFileName.Replace(".sim", ".sum")); jobs.Add(J); J = CleanupJob(SimFileName, J.Name); jobs.Add(J); } } else if (SimulationPaths.Length == 1) { string SumFileName = SimulationPaths[0]; int PosLastSlash = SumFileName.LastIndexOf('/'); if (PosLastSlash != -1) { SumFileName = SumFileName.Substring(PosLastSlash + 1); } SumFileName += ".sum"; SumFileName = Path.Combine(Path.GetDirectoryName(FileName), SumFileName); Job J = CreateJob(FileName, SumFileName, SimulationPaths[0]); J.IgnoreErrors = false; jobs.Add(J); } else if (SimulationPaths.Length > 1) { // For each path, create a simfile, and a job in our target. foreach (string SimulationPath in SimulationPaths) { string simName = SimulationPath; int PosLastSlash = simName.LastIndexOf('/'); if (PosLastSlash != -1) { simName = simName.Substring(PosLastSlash + 1); } string simFileName = Path.Combine(Path.GetDirectoryName(FileName), simName + ".sim"); try { Job J = CreateJob(FileName, Path.Combine(Path.GetDirectoryName(FileName), simName + ".sum"), SimulationPath); J.IgnoreErrors = false; jobs.Add(J); J = CleanupJob(simFileName, J.Name); jobs.Add(J); } catch (Exception err) { string sumFileName = Path.ChangeExtension(simFileName, ".sum"); WriteErrorToSummaryFile(sumFileName, err.Message); } } } }
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); } }
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); }