public override void OnRefresh() { treeSims.Nodes.Clear(); FactorBuilder builder = new FactorBuilder(); List <string> SimsToRun = new List <string>(); ApsimFile.ApsimFile.ExpandSimsToRun(Controller.ApsimData.RootComponent, ref SimsToRun); double iTotalCount = 0; foreach (string SimulationPath in SimsToRun) { int tmpCounter = 0; ApsimFile.Component tmpComp = Controller.ApsimData.Find(SimulationPath); if (tmpComp != null) { List <string> factorials = ApsimFile.Factor.CalcFactorialList(Controller.ApsimData, SimulationPath); TreeNode treeNode = treeSims.Nodes.Add(tmpComp.Name + " (" + factorials.Count.ToString() + ")"); treeNode.ImageIndex = 0; treeNode.SelectedImageIndex = 0; AddFactorsToTreeNode(treeNode, factorials); tmpCounter += factorials.Count; } iTotalCount += tmpCounter; txtTotalSims.Text = iTotalCount.ToString(); } //double tmp = treeSims.Nodes[0].Nodes.Count; XmlNode varNode = Data.SelectSingleNode("//settings"); if (XmlHelper.Attribute(varNode, "fn") == "1") { radDesc.Checked = true; } else { radCount.Checked = true; } if (XmlHelper.Attribute(varNode, "tl") == "1") { radMultiple.Checked = true; } else { radSingle.Checked = true; } if (XmlHelper.Attribute(varNode, "fqKeys") == "1") { fqKeys.Checked = true; } else { fqKeys.Checked = false; } }
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); } } }