public void BuildAPSIMXRuntimeEnvironmentWithRuntimePackage() { string binDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); string runtimeDirectory = Path.Combine(binDirectory, "ApsimX-2473"); // Remove old runtime directory if it exists. if (Directory.Exists(runtimeDirectory)) { Directory.Delete(runtimeDirectory, true); } // Build the new runtime environment. APSIMSpecification simulation = GetDefaultSimulationSpec(); List <APSIMSpecification> simulations = new List <APSIMSpecification>(); RuntimeEnvironment environment = new RuntimeEnvironment { APSIMxBuildNumber = 2473 // issue number that was resovled. }; simulations.Add(simulation); RunYPJob job = new RunYPJob(simulations, environment); // Make sure it is built correctly. Assert.IsTrue(Directory.Exists(runtimeDirectory)); string modelsExe = Path.Combine(runtimeDirectory, "Bin", "Models.exe"); Assert.IsTrue(File.Exists(modelsExe)); // Remove newly created runtime directory Directory.Delete(runtimeDirectory, true); }
public void RunAPSIMXGetOutputs() { APSIMSpecification simulation = GetDefaultSimulationSpec(); List <APSIMSpecification> simulations = new List <APSIMSpecification>(); simulations.Add(simulation); RuntimeEnvironment environment = new RuntimeEnvironment { APSIMxBuildNumber = 2473 // issue number that was resovled. }; RunYPJob job = new RunYPJob(simulations, environment); IJobRunner runner = new JobRunnerAsync(); runner.Run(job, wait: true); // Make sure we don't have an error. Assert.AreEqual(job.Errors.Count, 0); // Make sure we have a daily output table. Assert.AreEqual(job.Outputs.Tables.Count, 1); Assert.AreEqual(job.Outputs.Tables[0].TableName, "Daily"); double[] biomass = DataTableUtilities.GetColumnAsDoubles(job.Outputs.Tables[0], "Wheat.Aboveground.Wt"); Assert.IsTrue(MathUtilities.Max(biomass) > 5); // make sure something is growing. }
/// <summary>Runs the job (.xml file) specified on the command line.</summary> /// <returns>True if something was run.</returns> private static bool RunJobFromCommandLine(Dictionary <string, string> appSettings) { if (appSettings.ContainsKey("FileName")) { string jobFileName = appSettings["FileName"]; if (File.Exists(jobFileName)) { appSettings.TryGetValue("APSIMXExecutable", out string executable); string jobXML = File.ReadAllText(jobFileName); string jobName = Path.GetFileNameWithoutExtension(jobFileName); var environment = new APSIM.Cloud.Shared.RuntimeEnvironment { APSIMRevision = appSettings["APSIMRevision"], RuntimePackage = appSettings["RuntimePackage"], }; RunYPJob job = new RunYPJob(jobXML, environment) { ApsimXExecutable = executable }; if (job.Errors.Count == 0) { IJobRunner runner = new JobRunnerAsync(); runner.Run(job, wait: true); } if (job.AllFilesZipped != null) { string destZipFileName = Path.ChangeExtension(jobFileName, ".out.zip"); using (Stream s = File.Create(destZipFileName)) { job.AllFilesZipped.Seek(0, SeekOrigin.Begin); job.AllFilesZipped.CopyTo(s); } } if (job.Errors.Count > 0) { string msg = string.Empty; foreach (string error in job.Errors) { msg += error + Environment.NewLine; } throw new Exception(msg); } return(true); } } return(false); }
public void APSIMSpecificationTests_CreateJobFromApsimSpecXML() { APSIMSpecification spec = new APSIMSpecification() { Name = "NameOfPaddock", StartDate = new DateTime(2016, 4, 1), EndDate = new DateTime(2016, 12, 1), NowDate = new DateTime(2016, 7, 1), StationNumber = 41023, RunType = APSIMSpecification.RunTypeEnum.Normal, StubbleMass = 100, StubbleType = "Wheat", Samples = new List <Sample>() { new Sample() { Thickness = new double[] { 100, 300, 300, 300 }, NO3 = new double[] { 34, 6.9, 3.1, 1.8 }, NH4 = new double[] { 5.5, 1.8, 1.8, 1.5 }, SW = new double[] { 0.13, 0.18, 0.20, 0.24 }, SWUnits = Sample.SWUnitsEnum.Gravimetric, } }, SoilPath = "Soils/Australia/Victoria/Wimmera/Clay (Rupanyup North No742)", Management = new List <Management>() { new Sow() { Crop = "Wheat", Date = new DateTime(2016, 5, 1), } } }; List <APSIMSpecification> simulations = new List <APSIMSpecification>() { spec }; RuntimeEnvironment environment = new RuntimeEnvironment { APSIMRevision = "Apsim7.8-R4013" }; string xml = XmlUtilities.Serialise(simulations, true); RunYPJob job = new RunYPJob(xml, environment); Assert.IsNotNull(job.GetNextJobToRun()); }
/// <summary> /// Get the next job from the server. /// </summary> /// <returns></returns> private IJobManager GetJobFromServer() { IJobManager nextJob = null; while (nextJob == null && !cancel) { using (JobsService.JobsClient jobsClient = new JobsService.JobsClient()) { JobsService.Job runningJobDescription = jobsClient.GetNextToRun(); if (runningJobDescription != null) { nameOfCurrentJob = runningJobDescription.Name; string jobXML = jobsClient.GetJobXML(nameOfCurrentJob); if (IsF4PJob(runningJobDescription.Name) == true) { RuntimeEnvironment environment = new RuntimeEnvironment { AusfarmRevision = appSettings["AusfarmRevision"], }; nextJob = new RunF4PJob(jobXML, environment); } else { RuntimeEnvironment environment = new RuntimeEnvironment { APSIMRevision = appSettings["APSIMRevision"], RuntimePackage = appSettings["RuntimePackage"], }; nextJob = new RunYPJob(jobXML, environment); if ((nextJob as RunYPJob).Errors.Count > 0) { UpdateServerForCompletedJob(nextJob); nextJob = null; } } } else { // No jobs to run so wait a bit. Thread.Sleep(5 * 1000); // 5 sec. } } } return(nextJob); }
public void BuildAPSIMRuntimeEnvironmentWithRuntimePackage() { string binDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); string runtimeDirectory = Path.Combine(binDirectory, "Apsim7.8-R4000-Testing"); // Remove old runtime directory if it exists. if (Directory.Exists(runtimeDirectory)) { Directory.Delete(runtimeDirectory, true); } // Build the new runtime environment. APSIMSpecification simulation = GetDefaultSimulationSpec(); List <APSIMSpecification> simulations = new List <APSIMSpecification>(); RuntimeEnvironment environment = new RuntimeEnvironment { APSIMRevision = "Apsim7.8-R4000", RuntimePackage = "Testing" }; simulations.Add(simulation); RunYPJob job = new RunYPJob(simulations, environment); // Make sure it is built correctly. Assert.IsTrue(Directory.Exists(runtimeDirectory)); string apsimExe = Path.Combine(runtimeDirectory, "Temp", "Model", "Apsim.exe"); Assert.IsTrue(File.Exists(apsimExe)); string testFileName = Path.Combine(runtimeDirectory, "Temp", "Model", "Test.txt"); Assert.AreEqual(File.ReadAllText(testFileName), "Test contents"); // Remove newly created runtime directory Directory.Delete(runtimeDirectory, true); }
public void RunAPSIMGetError() { APSIMSpecification simulation = GetDefaultSimulationSpec(); (simulation.Management[0] as Sow).Cultivar = string.Empty; List <APSIMSpecification> simulations = new List <APSIMSpecification>(); simulations.Add(simulation); RuntimeEnvironment environment = new RuntimeEnvironment { APSIMRevision = "Apsim7.8-R4013" }; RunYPJob job = new RunYPJob(simulations, environment); IJobRunner runner = new JobRunnerAsync(); runner.Run(job, wait: true); // Make sure we have an error. Assert.AreEqual(job.Errors.Count, 1); Assert.AreEqual(job.Errors[0], "Cannot sow plant - : Cultivar not specified\r\n Component name: Paddock.Wheat"); }
public void RunAPSIMGetOutputs() { APSIMSpecification simulation = GetDefaultSimulationSpec(); List <APSIMSpecification> simulations = new List <APSIMSpecification>(); simulations.Add(simulation); RuntimeEnvironment environment = new RuntimeEnvironment { APSIMRevision = "Apsim7.8-R4013" }; RunYPJob job = new RunYPJob(simulations, environment); IJobRunner runner = new JobRunnerAsync(); runner.Run(job, wait: true); // Make sure we don't have an error. Assert.AreEqual(job.Errors.Count, 0); // Make sure we have a daily output table. Assert.AreEqual(job.Outputs.Tables.Count, 3); Assert.AreEqual(job.Outputs.Tables[0].TableName, "Summary"); Assert.AreEqual(job.Outputs.Tables[1].TableName, "YieldProphetDaily"); Assert.AreEqual(job.Outputs.Tables[1].Rows.Count, 92); double[] biomass = DataTableUtilities.GetColumnAsDoubles(job.Outputs.Tables[1], "biomass"); Assert.IsTrue(MathUtilities.Max(biomass) > 20.0); // make sure something is growing. // Make sure we have a depth table. Assert.AreEqual(job.Outputs.Tables[2].TableName, "YieldProphetDepth"); Assert.AreEqual(job.Outputs.Tables[2].Rows.Count, 8); double[] sw = DataTableUtilities.GetColumnAsDoubles(job.Outputs.Tables[2], "sw"); Assert.IsTrue(MathUtilities.Max(sw) > 0.0); // make sure there are sw values }
/// <summary>Runs the job (.xml file) specified on the command line.</summary> /// <returns>True if something was run.</returns> private static bool RunJobFromCommandLine(Dictionary <string, string> appSettings) { if (appSettings.ContainsKey("FileName")) { string jobFileName = appSettings["FileName"]; if (File.Exists(jobFileName)) { var ypEnvironment = new APSIM.Cloud.Shared.RuntimeEnvironment { APSIMRevision = appSettings["APSIMRevision"], RuntimePackage = appSettings["RuntimePackage"], }; if (appSettings.ContainsKey("UpdateFile")) { YieldProphetOld.UpdateFile(jobFileName); return(true); } else if (appSettings.ContainsKey("ConvertToAPSIM")) { string jobXML = File.ReadAllText(jobFileName); RunYPJob job = new RunYPJob(jobXML, ypEnvironment, createSims: false); AllocConsole(); Console.WriteLine(job.WorkingDirectory); Console.WriteLine(); if (job.Errors != null) { string msg = null; foreach (string error in job.Errors) { msg += error + Environment.NewLine; } throw new Exception(msg); } return(true); } else { appSettings.TryGetValue("APSIMXExecutable", out string executable); string jobXML = File.ReadAllText(jobFileName); string jobName = Path.GetFileNameWithoutExtension(jobFileName); IYPJob job = null; if (jobXML.Contains("Farm4Prophet")) { var environment = new APSIM.Cloud.Shared.RuntimeEnvironment { AusfarmRevision = appSettings["AusfarmRevision"], }; job = new RunF4PJob(jobXML, environment); } else { job = new RunYPJob(jobXML, ypEnvironment) { ApsimXExecutable = executable }; } if (job.Errors == null || job.Errors.Count == 0) { IJobRunner runner = new JobRunnerAsync(); runner.Run(job as IJobManager, wait: true); } if (job.AllFilesZipped != null) { string destZipFileName = Path.ChangeExtension(jobFileName, ".out.zip"); using (Stream s = File.Create(destZipFileName)) { job.AllFilesZipped.Seek(0, SeekOrigin.Begin); job.AllFilesZipped.CopyTo(s); } } if (job.Errors != null && job.Errors.Count > 0) { string msg = string.Empty; foreach (string error in job.Errors) { msg += error + Environment.NewLine; } throw new Exception(msg); } return(true); } } } return(false); }