/// <summary> /// Generates an .apsimx file for each simulation in the experiment and returns an error message (if it fails). /// </summary> /// <param name="path">Full path including filename and extension.</param> /// <returns>Empty string if successful, error message if it fails.</returns> public string GenerateApsimXFile(string path) { if (allCombinations == null || allCombinations.Count < 1) { allCombinations = EnabledCombinations(); } string err = ""; Simulation sim = NextSimulationToRun(); while (sim != null) { Simulations sims = Simulations.Create(new List <IModel> { sim, new Models.Storage.DataStore() }); string xml = Apsim.Serialise(sims); try { File.WriteAllText(Path.Combine(path, sim.Name + ".apsimx"), xml); } catch (Exception e) { err += e.ToString(); } sim = NextSimulationToRun(); } return(err); }
public void OnCopyClick(object sender, EventArgs e) { Model model = Apsim.Get(this.explorerPresenter.ApsimXFile, this.explorerPresenter.CurrentNodePath) as Model; if (model != null) { // Set the clipboard text. this.explorerPresenter.SetClipboardText(Apsim.Serialise(model)); } }
public void OnCopyClick(object sender, EventArgs e) { Model model = Apsim.Get(this.explorerPresenter.ApsimXFile, this.explorerPresenter.CurrentNodePath) as Model; if (model != null) { // Set the clipboard text. string xml = Apsim.Serialise(model); this.explorerPresenter.SetClipboardText(xml, "_APSIM_MODEL"); this.explorerPresenter.SetClipboardText(xml, "CLIPBOARD"); } }
/// <summary> /// Generates an .apsimx file for each simulation in the experiment and returns an error message (if it fails). /// </summary> /// <param name="path">Full path including filename and extension.</param> /// <returns>Empty string if successful, error message if it fails.</returns> public void GenerateApsimXFile(string path) { Simulation sim = NextSimulationToRun(); while (sim != null) { Simulations sims = Simulations.Create(new List <IModel> { sim, new Models.Storage.DataStore() }); string xml = Apsim.Serialise(sims); File.WriteAllText(Path.Combine(path, sim.Name + ".apsimx"), xml); sim = NextSimulationToRun(); } }
/// <summary>A node has begun to be dragged.</summary> /// <param name="sender">Sending object</param> /// <param name="e">Drag arguments</param> private void OnDragStart(object sender, DragStartArgs e) { Model obj = Apsim.Get(this.ApsimXFile, e.NodePath) as Model; if (obj != null) { string xml = Apsim.Serialise(obj); this.SetClipboardText(xml); DragObject dragObject = new DragObject(); dragObject.NodePath = e.NodePath; dragObject.ModelType = obj.GetType(); dragObject.Xml = xml; e.DragObject = dragObject; } }
/// <summary> /// User has clicked "copy graph xml" menu item. /// </summary> /// <param name="sender">Sender of event</param> /// <param name="e">Event arguments</param> private void CopyGraphXML(object sender, EventArgs e) { // Set the clipboard text. System.Windows.Forms.Clipboard.SetText(Apsim.Serialise(this.Graph)); }
/// <summary> /// Handles the bulk of the work for submitting the job to the cloud. /// Zips up ApsimX (if necessary), uploads tools and ApsimX, /// </summary> /// <param name="e">Event arg, containing the job parameters</param> private void SubmitJob_DoWork(object o, DoWorkEventArgs e) { JobParameters jp = (JobParameters)e.Argument; jp.JobId = Guid.NewGuid(); jp.CoresPerProcess = 1; jp.JobManagerShouldSubmitTasks = true; jp.AutoScale = true; jp.PoolMaxTasksPerVM = 16; string tmpZip = ""; SetAzureMetaData("job-" + jp.JobId, "Owner", Environment.UserName.ToLower()); // if jp.ApplicationPackagePath is a directory it will need to be zipped up if (Directory.Exists(jp.ApplicationPackagePath)) { view.Status = "Zipping APSIM"; tmpZip = Path.Combine(Path.GetTempPath(), "Apsim-tmp-X-" + Environment.UserName.ToLower() + ".zip"); if (File.Exists(tmpZip)) { File.Delete(tmpZip); } if (createApsimXZip(jp.ApplicationPackagePath, tmpZip) > 0) { ShowError("Error zipping up Apsim"); } jp.ApplicationPackagePath = tmpZip; jp.ApplicationPackageVersion = Path.GetFileName(tmpZip).Substring(Path.GetFileName(tmpZip).IndexOf('-') + 1); } // add current job to the list of jobs // TODO : do we actually need/use the APSIMJob class? APSIMJob job = new APSIMJob(jp.JobDisplayName, "", jp.ApplicationPackagePath, jp.ApplicationPackageVersion, jp.Recipient, batchCredentials, storageCredentials, PoolSettings.FromConfiguration()); job.PoolInfo.MaxTasksPerVM = jp.PoolMaxTasksPerVM; job.PoolInfo.VMCount = jp.PoolVMCount; // upload tools such as 7zip, AzCopy, CMail, etc. view.Status = "Checking tools"; string executableDirectory = GetExecutableDirectory(); string toolsDir = Path.Combine(executableDirectory, "tools"); if (!Directory.Exists(toolsDir)) { ShowError("Tools Directory not found: " + toolsDir); } foreach (string filePath in Directory.EnumerateFiles(toolsDir)) { UploadFileIfNeeded("tools", filePath); } if (jp.Recipient.Length > 0) { try { // Store a config file into the job directory that has the e-mail config string tmpConfig = Path.Combine(Path.GetTempPath(), SETTINGS_FILENAME); using (StreamWriter file = new StreamWriter(tmpConfig)) { file.WriteLine("EmailRecipient=" + jp.Recipient); file.WriteLine("EmailSender=" + AzureSettings.Default["EmailSender"]); file.WriteLine("EmailPW=" + AzureSettings.Default["EmailPW"]); } UploadFileIfNeeded("job-" + jp.JobId, tmpConfig); File.Delete(tmpConfig); } catch (Exception ex) { ShowError("Error writing to settings file; you may not receive an email upon job completion: " + ex.ToString()); } } // upload job manager UploadFileIfNeeded("jobmanager", Path.Combine(executableDirectory, "azure-apsim.exe")); // upload apsim view.Status = "Uploading APSIM Next Generation"; UploadFileIfNeeded("apsim", jp.ApplicationPackagePath); // create models // TODO : show error message if other files already exist in model output directory? // may be necessary if using a user-selected directory string path = ""; // generate model files view.Status = "Generating model files"; if (!Directory.Exists(jp.ModelPath)) { Directory.CreateDirectory(jp.ModelPath); } // generate xml string xml = ""; foreach (Simulation sim in Runner.AllSimulations(model)) { path = Path.Combine(jp.ModelPath, sim.Name + ".apsimx"); // if weather file is not in the same directory as the .apsimx file, display an error then abort foreach (var child in sim.Children) { if (child is Models.Weather) { string childPath = sim.Children.OfType <Models.Weather>().ToList()[0].FileName; if (Path.GetDirectoryName(childPath) != "") { ShowError(childPath + " must be in the same directory as the .apsimx file" + (sim.FileName != null ? " (" + Path.GetDirectoryName(sim.FileName) + ")" : "")); view.Status = "Cancelled"; return; } string sourcePath = sim.Children.OfType <Models.Weather>().ToList()[0].FullFileName; string destPath = Path.Combine(jp.ModelPath, sim.Children.OfType <Models.Weather>().ToList()[0].FileName); if (!File.Exists(destPath)) { File.Copy(sourcePath, destPath); } } } xml = Apsim.Serialise(sim); // delete model file if it already exists if (File.Exists(path)) { File.Delete(path); } string pattern = @"<\?xml[^<>]+>"; System.Text.RegularExpressions.Regex rx = new System.Text.RegularExpressions.Regex(pattern, System.Text.RegularExpressions.RegexOptions.IgnoreCase); var matches = rx.Matches(xml); xml = "<Simulations xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" Version=\"23\">\r\n<Name>" + sim.Name + "</Name>\r\n" + xml.Replace(matches[0].ToString(), "") + "<DataStore><Name>DataStore</Name><AutoExport>false</AutoExport><MaximumResultsPerPage>0</MaximumResultsPerPage></DataStore><ExplorerWidth>296</ExplorerWidth></Simulations>"; // write xml to file using (FileStream fs = File.Create(path)) { Byte[] info = new UTF8Encoding(true).GetBytes(xml); fs.Write(info, 0, info.Length); } } tmpZip = ""; // zip up models directory if (Directory.Exists(jp.ModelPath)) // this test may be unnecessary { tmpZip = GetTempFileName("Model-", ".zip", true); ZipFile.CreateFromDirectory(jp.ModelPath, tmpZip, CompressionLevel.Fastest, false); jp.ModelPath = tmpZip; } // upload models view.Status = "Uploading models"; job.ModelZipFileSas = uploader.UploadFile(jp.ModelPath, jp.JobId.ToString(), Path.GetFileName(jp.ModelPath)); // clean up temporary model files if (File.Exists(tmpZip)) { File.Delete(tmpZip); } if (!jp.SaveModelFiles) { if (Directory.Exists(jp.ModelPath)) { Directory.Delete(jp.ModelPath); } } view.Status = "Submitting Job"; // submit job try { CloudJob cloudJob = batchClient.JobOperations.CreateJob(jp.JobId.ToString(), GetPoolInfo(job.PoolInfo)); cloudJob.DisplayName = job.DisplayName; cloudJob.JobPreparationTask = job.ToJobPreparationTask(jp.JobId, storageAccount.CreateCloudBlobClient()); cloudJob.JobReleaseTask = job.ToJobReleaseTask(jp.JobId, storageAccount.CreateCloudBlobClient()); cloudJob.JobManagerTask = job.ToJobManagerTask(jp.JobId, storageAccount.CreateCloudBlobClient(), jp.JobManagerShouldSubmitTasks, jp.AutoScale); cloudJob.Commit(); } catch (Exception ex) { ShowError(ex.ToString()); } view.Status = "Job Successfully submitted"; // in theory, instantiating an AzureResultsDownloader here and passing in this job's ID and name, then calling its DownloadResults() method would allow // for automatic job downloading as soon as the job is finished. The trouble is it needs an AzureJobDisplayPresenter (for progress bar updating, etc). // explorerPresenter.MainPresenter.ShowMessage("Job Successfully submitted", Simulation.ErrorLevel.Information); // AzureResultsDownloader dl = new AzureResultsDownloader(jp.JobId, jp.JobDisplayName, jp.OutputDir, this.explorerPresenter, true, false, false); }