/// <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); }
/// <summary> /// Test that a simulation can be written to a string and then /// converted back into a simulation i.e. round trip. /// </summary> // [Test] // Temporarily disabled public void FileFormat_EnsureWriteReadRoundTripWorks() { // Create some models. Simulation simulationModel = new Simulation(); Simulations simulationsModel = Simulations.Create(new Model[] { simulationModel }); // Create a simulations object with child model wrappers. ModelWrapper rootNode1 = new ModelWrapper(); ModelWrapper simulations = rootNode1.Add(simulationsModel); ModelWrapper simulation = simulations.Add(simulationModel); Clock clock = new Clock(); clock.StartDate = new DateTime(2015, 1, 1); clock.EndDate = new DateTime(2015, 12, 31); simulation.Add(clock); ModelWrapper zone = simulation.Add(new Zone()); // Write the above simulations object to an xml string. FileFormat fileFormat = new FileFormat(); string xml = fileFormat.WriteXML(rootNode1); // Read XML back in. ModelWrapper rootNode2 = fileFormat.ReadXML(xml); // Make sure the two root nodes are the same. Assert.IsTrue(rootNode2.Model is Simulations); Assert.AreEqual(rootNode2.Children.Count, 1); Assert.IsTrue((rootNode2.Children[0] as ModelWrapper).Model is Simulation); Assert.AreEqual((rootNode2.Children[0] as ModelWrapper).Children.Count, 2); }
public void TestSet() { // Create a tree with a root node for our models. ModelWrapper models = new ModelWrapper(); // Create some models. Simulation simulationModel = new Simulation(); Simulations simulationsModel = Simulations.Create(new Model[] { simulationModel }); ModelWrapper simulations = models.Add(simulationsModel); ModelWrapper simulation = simulations.Add(simulationModel); Clock clock = new Clock(); clock.StartDate = new DateTime(2015, 1, 1); clock.EndDate = new DateTime(2015, 12, 31); simulation.Add(clock); simulation.Add(new MockSummary()); ModelWrapper zone = simulation.Add(new Zone()); // Fix case this should work. Assert.IsTrue(models.Set("Simulation.Clock.EndDate", new DateTime(2016, 1, 1))); Assert.AreEqual(clock.EndDate.Year, 2016); Assert.AreEqual(clock.EndDate.Month, 1); Assert.AreEqual(clock.EndDate.Day, 1); }
public void TestGet() { // Create a tree with a root node for our models. ModelWrapper models = new ModelWrapper(); // Create some models. Simulation simulationModel = new Simulation(); Simulations simulationsModel = Simulations.Create(new Model[] { simulationModel }); ModelWrapper simulations = models.Add(simulationsModel); ModelWrapper simulation = simulations.Add(simulationModel); Clock clock = new Clock(); clock.StartDate = new DateTime(2015, 1, 1); clock.EndDate = new DateTime(2015, 12, 31); simulation.Add(clock); simulation.Add(new MockSummary()); ModelWrapper zone = simulation.Add(new Zone()); // Check that it is case sensitive. Assert.IsNull(models.Get("simulation.clock.StartDate")); // Fix case this should work. object d = models.Get("Simulation.Clock.StartDate"); DateTime startDate = (DateTime)d; Assert.AreEqual(startDate.Year, 2015); Assert.AreEqual(startDate.Month, 1); Assert.AreEqual(startDate.Day, 1); }
public void ManagerScriptTest() { string managerCode = "using System; using Models.Core; namespace Models { public class Script : Model, ITest { public void Run() { @action } } }"; Manager testManager = new Manager(); testManager.Name = "TestScript"; Folder testFolder = new Folder(); testFolder.Name = "TestFolder"; testFolder.Children = new List <Model>() { testManager }; testManager.Parent = testFolder; MockStorage storage = new MockStorage(); Simulations simToRun = Simulations.Create(new List <IModel>() { testFolder, storage }); IJobManager jobManager = Runner.ForSimulations(simToRun, simToRun, true); // Test should fail if it throws. testManager.Code = managerCode.Replace("@action", "throw new Exception(\"Test has failed.\");"); TestWithAllJobRunners(jobManager, EnsureSimulationRanRed); // Test should pass if it doesn't throw. testManager.Code = managerCode.Replace("@action", "return;"); TestWithAllJobRunners(jobManager, EnsureSimulationRanGreen); }
public void TestSimNameRegex() { string models = typeof(IModel).Assembly.Location; IModel sim1 = Utilities.GetRunnableSim().Children[1]; sim1.Name = "sim1"; IModel sim2 = Utilities.GetRunnableSim().Children[1]; sim2.Name = "sim2"; IModel sim3 = Utilities.GetRunnableSim().Children[1]; sim3.Name = "simulation3"; IModel sim4 = Utilities.GetRunnableSim().Children[1]; sim4.Name = "Base"; Simulations sims = Simulations.Create(new[] { sim1, sim2, sim3, sim4, new DataStore() }); sims.ParentAllDescendants(); string apsimxFileName = Path.ChangeExtension(Path.GetTempFileName(), ".apsimx"); sims.Write(apsimxFileName); // Need to quote the regex on unix systems. string args; args = @"/Verbose /SimulationNameRegexPattern:sim\d"; string stdout = Utilities.RunModels(sims, args); Assert.True(stdout.Contains("sim1")); Assert.True(stdout.Contains("sim2")); Assert.False(stdout.Contains("simulation3")); Assert.False(stdout.Contains("Base")); args = @"/Verbose /SimulationNameRegexPattern:sim1"; stdout = Utilities.RunModels(sims, args); Assert.True(stdout.Contains("sim1")); Assert.False(stdout.Contains("sim2")); Assert.False(stdout.Contains("simulation3")); Assert.False(stdout.Contains("Base")); args = @"/Verbose /SimulationNameRegexPattern:(simulation3)|(Base)"; stdout = Utilities.RunModels(sims, args); Assert.False(stdout.Contains("sim1")); Assert.False(stdout.Contains("sim2")); Assert.True(stdout.Contains("simulation3")); Assert.True(stdout.Contains("Base")); }
public void ExcelWeatherFileTest() { Simulation baseSim = new Simulation(); baseSim.Name = "Base"; string weatherFilePath = Path.ChangeExtension(Path.GetTempFileName(), ".xlsx"); using (FileStream file = new FileStream(weatherFilePath, FileMode.Create, FileAccess.Write)) { Assembly.GetExecutingAssembly().GetManifestResourceStream("UnitTests.Weather.WeatherTestsExcelFile.xlsx").CopyTo(file); } var excelWeather = new Models.Weather() { Name = "Weather", Parent = baseSim, FullFileName = weatherFilePath, ExcelWorkSheetName = "Sheet1" }; Clock clock = new Clock() { Name = "Clock", Parent = baseSim, StartDate = new DateTime(1998, 11, 9), EndDate = new DateTime(1998, 11, 12) }; MockSummary summary = new MockSummary() { Name = "Summary", Parent = baseSim }; baseSim.Children = new List <Model>() { excelWeather, clock, summary }; MockStorage storage = new MockStorage(); Simulations simsToRun = Simulations.Create(new List <IModel> { baseSim, storage }); IJobManager jobManager = Runner.ForSimulations(simsToRun, simsToRun, false); IJobRunner jobRunner = new JobRunnerSync(); jobRunner.JobCompleted += Utilities.EnsureJobRanGreen; jobRunner.Run(jobManager, true); }
/// <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(); } }
public void Links_EnsureServicesResolve() { ModelWithServices modelWithServices = new ModelWithServices(); Simulation simulation = new Simulation(); simulation.Children.Add(new Clock()); simulation.Children.Add(new MockSummary()); simulation.Children.Add(modelWithServices); Simulations engine = Simulations.Create(new Model[] { simulation, new DataStore() }); engine.Links.Resolve(simulation); Assert.IsNotNull(modelWithServices.storage); Assert.IsNotNull(modelWithServices.locator); Assert.IsNotNull(modelWithServices.events); }
public void LocatorGetVariableWithRelativeAddress() { Simulation sim = new Simulation(); sim.Children.Add(new ModelA()); sim.Children.Add(new ModelB()); sim.Children.Add(new Zone()); sim.Children[2].Children.Add(new ModelC()); sim.Children[2].Children.Add(new ModelD()); Simulations sims = Simulations.Create(new Model[] { sim }); // locator for zone ILocator locatorForZone = sims.GetLocatorService(sim.Children[2]); Assert.AreEqual(locatorForZone.Get("ModelC.C1"), 5); }
public void LocatorGetModel() { Simulation sim = new Simulation(); sim.Children.Add(new ModelA()); sim.Children.Add(new ModelB()); sim.Children.Add(new Zone()); sim.Children[2].Children.Add(new ModelC()); sim.Children[2].Children.Add(new ModelD()); Simulations sims = Simulations.Create(new Model[] { sim }); // locator for modelC ILocator locatorForC = sims.GetLocatorService(sim.Children[2].Children[0]); Assert.AreEqual(locatorForC.Get("[ModelA]"), sim.Children[0]); }
public void Links_EnsureIFunctionLinksCorrectly() { // Create a tree with a root node for our models. ModelWrapper models = new ModelWrapper(); // Create some models. Simulation simulationModel = new Simulation(); Simulations simulationsModel = Simulations.Create(new Model[] { simulationModel }); ModelWrapper simulations = models.Add(simulationsModel); ModelWrapper simulation = simulations.Add(simulationModel); Clock clock = new Clock(); clock.StartDate = new DateTime(2015, 1, 1); clock.EndDate = new DateTime(2015, 12, 31); simulation.Add(clock); MockSummary summary = new MockSummary(); simulation.Add(summary); simulation.Add(new Zone()); simulation.Add(new Zone()); ModelWrapper links = simulation.Add(new ModelWithIFunctions()); simulation.Add(links); links.Add(new IFunctionProxy() { value = 1 }).Name = "model1"; links.Add(new IFunctionProxy() { value = 2 }).Name = "model2"; links.Add(new IFunctionProxy() { value = 3 }).Name = "model3"; Links linksAlgorithm = new Links(); linksAlgorithm.Resolve(simulations); Assert.AreEqual((links.Model as ModelWithIFunctions).model2.Value(), 2); }
public void Fertiliser_EnsureApplyWorks() { // Create a tree with a root node for our models. Simulation simulation = new Simulation(); Clock clock = new Clock(); clock.StartDate = new DateTime(2015, 1, 1); clock.EndDate = new DateTime(2015, 1, 1); simulation.Children.Add(clock); MockSummary summary = new MockSummary(); simulation.Children.Add(summary); MockSoil soil = new MockSoil(); soil.Thickness = new double[] { 100, 100, 100 }; soil.NO3 = new double[] { 1, 2, 3 }; simulation.Children.Add(soil); Fertiliser fertiliser = new Fertiliser(); fertiliser.Name = "Fertilise"; simulation.Children.Add(fertiliser); Operations operations = new Operations(); Operation fertiliseOperation = new Operation(); fertiliseOperation.Date = "1-jan"; fertiliseOperation.Action = "[Fertilise].Apply(Amount: 100, Type:Fertiliser.Types.NO3N, Depth:300)"; operations.Schedule = new List <Operation>(); operations.Schedule.Add(fertiliseOperation); simulation.Children.Add(operations); simulation.Children.Add(new SoluteManager()); ISimulationEngine simulationEngine = Simulations.Create(new Model[] { simulation }); simulationEngine.Run(simulation, doClone: false); Assert.AreEqual(soil.NO3, new double[] { 1, 2, 103 }); Assert.AreEqual(MockSummary.messages[0], "100 kg/ha of NO3N added at depth 300 layer 3"); }
public void LocatorGetVariableWithArrayIndex() { Simulation sim = new Simulation(); sim.Children.Add(new ModelA()); sim.Children.Add(new ModelB()); sim.Children.Add(new Zone()); sim.Children[2].Children.Add(new ModelC()); sim.Children[2].Children.Add(new ModelD()); Simulations sims = Simulations.Create(new Model[] { sim }); // locator for modelD ILocator locatorForD = sims.GetLocatorService(sim.Children[2].Children[1]); Assert.AreEqual(locatorForD.Get("[ModelC].C2[1]"), 6.0); Assert.AreEqual(locatorForD.Get("[ModelC].C2[2]"), 6.1); Assert.AreEqual(locatorForD.Get("[ModelC].C2[3]"), 6.2); }
/// <summary> /// Runs a list of simulations. /// </summary> /// <param name="names">Names of the simulations to be run.</param> public void RunSims(List <string> names) { try { Simulation sim; List <Model> simulationList = new List <Model>(); foreach (string simName in names) { sim = model.CreateSpecificSimulation(simName); simulationList.Add(sim); } Simulations simulationsToRun = Simulations.Create(simulationList); Commands.RunCommand command = new Commands.RunCommand(simulationsToRun, explorerPresenter, false, null); command.Do(null); } catch (Exception e) { explorerPresenter.MainPresenter.ShowError(e); } }
public void LocatorGetPropertyOfModelAtSpecificArrayElement() { Simulation sim = new Simulation(); sim.Children.Add(new ModelF()); sim.Children.Add(new ModelB()); sim.Children.Add(new Zone()); sim.Children[2].Children.Add(new ModelC()); ModelE e = new ModelE(); e.models[0].F = 20; e.models[1].F = 21; sim.Children[2].Children.Add(e); Simulations sims = Simulations.Create(new Model[] { sim }); // locator for modelC ILocator locatorForC = sims.GetLocatorService(sim.Children[2].Children[0]); Assert.AreEqual(locatorForC.Get("[ModelE].E1[1].F"), 20); Assert.AreEqual(locatorForC.Get("[ModelE].E1[2].F"), 21); }
public void LocatorGetVariableWithAbsoluteAddress() { Simulation sim = new Simulation(); sim.Children.Add(new ModelA()); sim.Children.Add(new ModelB()); sim.Children.Add(new Zone()); sim.Children[2].Children.Add(new ModelC()); sim.Children[2].Children.Add(new ModelD()); Simulations sims = Simulations.Create(new Model[] { sim }); // locator for modelC ILocator locatorForC = sims.GetLocatorService(sim.Children[2].Children[0]); Assert.AreEqual(locatorForC.Get(".Simulations.Simulation.ModelA.A1"), 1); // locator for modelD ILocator locatorForD = sims.GetLocatorService(sim.Children[2].Children[1]); Assert.AreEqual(locatorForD.Get(".Simulations.Simulation.Zone.ModelD.D2.Year"), 2000); }
public void Runner_CreateSimulations() { // Create a tree with a root node for our models. Simulation simulation = new Simulation(); Clock clock = new Clock(); clock.StartDate = new DateTime(2015, 1, 1); clock.EndDate = new DateTime(2015, 1, 1); simulation.Children.Add(clock); simulation.Children.Add(new MockSummary()); simulation.Children.Add(new MockStorage()); Experiment experiment = new Experiment(); Factors factors = new Factors(); Factor factor1 = new Factor(); factor1.Specifications = new List <string>(); factor1.Specifications.Add("[Clock].StartDate = 2003-11-01, 2003-12-20"); experiment.Children.Add(simulation); factors.Children.Add(factor1); experiment.Children.Add(factors); Simulations topLevelSimulationsModel = Simulations.Create(new IModel[] { experiment, Apsim.Clone(simulation) }); Runner.SimulationCreator simulationCreator = Runner.AllSimulations(topLevelSimulationsModel); string[] simulationNames = simulationCreator.Select(sim => sim.Name).ToArray(); Assert.AreEqual(simulationNames, new string[] { "ExperimentFactor2003-11-01", "ExperimentFactor2003-12-20", "Simulation" }); Assert.AreEqual(simulationCreator.SimulationNamesBeingRun, new string[] { "ExperimentFactor2003-11-01", "ExperimentFactor2003-12-20", "Simulation" }); }
public void Links_EnsureLinkByPathWorks() { ModelWithLinkByPath modelWithLinkByPath = new UnitTests.ModelWithLinkByPath(); // Create a simulation Simulation simulation = new Simulation(); simulation.Children.Add(new Clock()); simulation.Children.Add(new MockSummary()); simulation.Children.Add(new Zone() { Name = "zone1" }); simulation.Children.Add(new Zone() { Name = "zone2" }); simulation.Children[2].Children.Add(modelWithLinkByPath); // added to zone1 MockIrrigation irrig1 = new MockIrrigation() { Name = "irrig1" }; MockIrrigation irrig2 = new MockIrrigation() { Name = "irrig2" }; simulation.Children[3].Children.Add(irrig1); // added to zone2 simulation.Children[3].Children.Add(irrig2); // added to zone2 Simulations engine = Simulations.Create(new Model[] { simulation, new DataStore() }); engine.Links.Resolve(simulation); Assert.AreEqual(modelWithLinkByPath.irrigation1, irrig1); Assert.AreEqual(modelWithLinkByPath.irrigation2, irrig2); }
public void Links_EnsureOldStyleLinkWorks() { // Create a tree with a root node for our models. ModelWrapper models = new ModelWrapper(); // Create some models. Simulation simulationModel = new Simulation(); Simulations simulationsModel = Simulations.Create(new Model[] { simulationModel }); ModelWrapper simulations = models.Add(simulationsModel); ModelWrapper simulation = simulations.Add(simulationModel); Clock clock = new Clock(); clock.StartDate = new DateTime(2015, 1, 1); clock.EndDate = new DateTime(2015, 12, 31); simulation.Add(clock); MockSummary summary = new MockSummary(); simulation.Add(summary); simulation.Add(new Zone()); simulation.Add(new Zone()); ModelWithLinks links = new ModelWithLinks(); simulation.Add(links); Links linksAlgorithm = new Links(); linksAlgorithm.Resolve(simulations); Assert.AreEqual(links.zones.Length, 2); Assert.NotNull(links.zones[0]); Assert.NotNull(links.zones[1]); }
public void TestTablesModified() { IModel sim1 = new Simulation() { Name = "sim1", Children = new List <IModel>() { new Report() { Name = "Report1", VariableNames = new[] { "[Clock].Today" }, EventNames = new[] { "[Clock].DoReport" }, }, new MockSummary(), new Clock() { StartDate = new DateTime(2020, 1, 1), EndDate = new DateTime(2020, 1, 2), }, } }; IModel sim2 = Apsim.Clone(sim1); sim2.Name = "sim2"; sim2.Children[0].Name = "Report2"; TestPostSim testPostSim = new TestPostSim(); sim1.Children.Add(testPostSim); Simulations sims = Simulations.Create(new[] { sim1, sim2, new DataStore() }); Utilities.InitialiseModel(sims); Runner runner = new Runner(sims, simulationNamesToRun: new[] { "sim1" }); List <Exception> errors = runner.Run(); if (errors != null && errors.Count > 0) { throw errors[0]; } List <string> tablesMod = new List <string>() { "_Factors", "Report1", "_Simulations", "_Checkpoints", }; Assert.AreEqual(tablesMod.OrderBy(x => x), testPostSim.TablesModified.OrderBy(x => x)); runner = new Runner(sims, simulationNamesToRun: new[] { "sim2" }); errors = runner.Run(); if (errors != null && errors.Count > 0) { throw errors[0]; } tablesMod = new List <string>() { "_Factors", "Report2", "_Simulations", "_Checkpoints", }; Assert.AreEqual(tablesMod.OrderBy(x => x), testPostSim.TablesModified.OrderBy(x => x)); // Now run both sims runner = new Runner(sims); errors = runner.Run(); if (errors != null && errors.Count > 0) { throw errors[0]; } tablesMod = new List <string>() { "_Factors", "Report2", "Report1", "_Simulations", "_Checkpoints", }; Assert.AreEqual(tablesMod.OrderBy(x => x), testPostSim.TablesModified.OrderBy(x => x)); }
public void TestSimNameRegex() { string models = typeof(IModel).Assembly.Location; IModel sim1 = Utilities.GetRunnableSim().Children[1]; sim1.Name = "sim1"; IModel sim2 = Utilities.GetRunnableSim().Children[1]; sim2.Name = "sim2"; IModel sim3 = Utilities.GetRunnableSim().Children[1]; sim3.Name = "simulation3"; IModel sim4 = Utilities.GetRunnableSim().Children[1]; sim4.Name = "Base"; Simulations sims = Simulations.Create(new[] { sim1, sim2, sim3, sim4, new DataStore() }); sims.ParentAllDescendants(); string apsimxFileName = Path.ChangeExtension(Path.GetTempFileName(), ".apsimx"); sims.Write(apsimxFileName); // Need to quote the regex on unix systems. string args; if (ProcessUtilities.CurrentOS.IsWindows) { args = $@"{apsimxFileName} /Verbose /SimulationNameRegexPattern:sim\d"; } else { args = $@"{apsimxFileName} /Verbose '/SimulationNameRegexPattern:sim\d'"; } ProcessUtilities.ProcessWithRedirectedOutput proc = new ProcessUtilities.ProcessWithRedirectedOutput(); proc.Start(models, args, Directory.GetCurrentDirectory(), true); proc.WaitForExit(); Assert.Null(proc.StdErr); Assert.True(proc.StdOut.Contains("sim1")); Assert.True(proc.StdOut.Contains("sim2")); Assert.False(proc.StdOut.Contains("simulation3")); Assert.False(proc.StdOut.Contains("Base")); args = $@"{apsimxFileName} /Verbose /SimulationNameRegexPattern:sim1"; proc = new ProcessUtilities.ProcessWithRedirectedOutput(); proc.Start(models, args, Directory.GetCurrentDirectory(), true); proc.WaitForExit(); Assert.Null(proc.StdErr); Assert.True(proc.StdOut.Contains("sim1")); Assert.False(proc.StdOut.Contains("sim2")); Assert.False(proc.StdOut.Contains("simulation3")); Assert.False(proc.StdOut.Contains("Base")); args = $@"{apsimxFileName} /Verbose /SimulationNameRegexPattern:(simulation3)|(Base)"; proc = new ProcessUtilities.ProcessWithRedirectedOutput(); proc.Start(models, args, Directory.GetCurrentDirectory(), true); proc.WaitForExit(); Assert.Null(proc.StdErr); Assert.False(proc.StdOut.Contains("sim1")); Assert.False(proc.StdOut.Contains("sim2")); Assert.True(proc.StdOut.Contains("simulation3")); Assert.True(proc.StdOut.Contains("Base")); }