public override dynamic CalculateInteraction(Particle Input, Particle Comparison, Simulations.Simulation Sim) { //First we create a new force vector to store our result to Vector Force = new Vector(Input.NetForce.Capacity); //Make sure our input particle has "charge" and obtain it if (!Input.HasProperty("Charge")) return Force; double Charge = Input.GetPropertyVal("Charge"); if (Charge == 0) return Force; //Determine the input's siblings List<Particle> Siblings; if (Input.ParentParticle != null) { Siblings = Input.ParentParticle.SubParticles; } else { //Assume top layer Siblings = Sim.SimUniverse.Particles; } //Get the electric and magnetic fields on the charge Vector ElectricField = CalculateElectricFieldAtPosition(Input.Position, Siblings, Sim.Parameters); Vector MagneticField = CalculateMagneticFieldAtPosition(Input.Position, Siblings, Sim.Parameters); //Calculate the Lorentz force Force = (Charge * ElectricField) + Vector.Vector_3DCross((Charge * Input.Velocity), MagneticField); //Return the new net force return Force; }
/// <summary> /// Returns a copy of an input particle updated against all particles under the same parent /// </summary> /// <param name="Input">The particle to update</param> /// <param name="Sim">The simulation object</param> public static Particle UpdateParticle(Particle Input, Simulations.Simulation Sim) { //Extract all scripts that have the "Force" tag InteractionScripts ForceInteractions = Sim.Interactions.ExtractByTag("Force"); //Extract all scripts that have the "PostForceInteraction" tag InteractionScripts PostForceInteractions = Sim.Interactions.ExtractByTag("PostForceInteraction"); //Complain if we dont have any interaction types if (ForceInteractions == null && PostForceInteractions == null) return Input; //Create a copy of the input particle that we can return without modifying the origional Particle ReturnParticle = Particle.Copy_Shallow(Input, true); //Zero-out the net force and acceleration of the particle we are returning ReturnParticle.Acceleration = new Vector(Input.Acceleration.Capacity); ReturnParticle.NetForce = new Vector(Input.NetForce.Capacity); //Calculate force interactions if (ForceInteractions != null) { //For each possible comparison particle: foreach (Particle Comparison in Sim.SimUniverse.Particles) { //Make sure they aren't the same particle if (ReturnParticle.ID == Comparison.ID) continue; //Calculate the net force of that particle on our return particle foreach (ParticleInteractionScript ForceInteraction in ForceInteractions) { ReturnParticle.NetForce += ForceInteraction.CalculateInteraction(ReturnParticle, Comparison, Sim); } } } //Calculate post force interactions if (PostForceInteractions != null) { foreach (ParticleInteractionScript PostForceInteraction in PostForceInteractions) { ReturnParticle.NetForce += PostForceInteraction.CalculateInteraction(ReturnParticle, null, Sim); } } //Here we multiply by 0.0001 to account for the max timer speed ReturnParticle.NetForce *= 0.0001; //Now lets calculate the acceleration of the particle by dividing the net force by the mass of the particle ReturnParticle.Acceleration = ReturnParticle.NetForce / ReturnParticle.GetPropertyVal("Mass"); //Now we need to calculate velocity ReturnParticle.Velocity += ReturnParticle.Acceleration; //Now we calculate position and we're done ReturnParticle.Position += ReturnParticle.Velocity; //Return the updated particle return ReturnParticle; }
public override dynamic CalculateInteraction(Particle Input, Particle Comparison, Simulations.Simulation Sim) { //First we create a new force vector to store our result to Vector Force = new Vector(Input.NetForce.Capacity); //Make sure both our input and comparison particles have "mass" if (!Input.HasProperty("Mass") || !Comparison.HasProperty("Mass")) return Force; //Now we calculate the actual distance between the particles (we will need this later) double ActualDistance = (Comparison.Position - Input.Position).Magnitude; Vector Dir = (Input.Position - Comparison.Position).Direction; //If the "Precision" simulation parameter is available to us: if (Sim.Parameters.HasParam("Precision")) { //Check to see if the actual distance is under the precision value if ((System.Math.Abs(ActualDistance) - Input.Radius - Comparison.Radius) <= Sim.Parameters.GetParam("Precision")) { //Return no change in acceleration return new Vector(Input.Acceleration.Count); } } else //Otherwise: { //Assume a precision of zero if ((System.Math.Abs(ActualDistance) - Input.Radius - Comparison.Radius) <= 0) { //Return no change in acceleration return new Vector(Input.Acceleration.Count); } } //Get all of our needed parts double InputMass = Input.GetPropertyVal("Mass"); double ComparisonMass = Comparison.GetPropertyVal("Mass"); double GravitationalConstant; if (Sim.Parameters.HasParam("GravitationalConstant")) { GravitationalConstant = Sim.Parameters.GetParam("GravitationalConstant"); } else { GravitationalConstant = PhysicsConstants.GravitationalConstant; } Force = GravitationalConstant * InputMass * ComparisonMass / ActualDistance * Dir; //Return the new net force return Force; }
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 LocatorGetExpression() { 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].A1+[ModelD].D2.Year"), 2001); }
/// <summary> /// Create a specific simulation. /// </summary> public Simulation CreateSpecificSimulation(string name) { List <List <FactorValue> > allCombinations = AllCombinations(); Simulation baseSimulation = Apsim.Child(this, typeof(Simulation)) as Simulation; Simulations parentSimulations = Apsim.Parent(this, typeof(Simulations)) as Simulations; foreach (List <FactorValue> combination in allCombinations) { string newSimulationName = Name; foreach (FactorValue value in combination) { newSimulationName += value.Name; } if (newSimulationName == name) { Simulation newSimulation = Apsim.Clone(baseSimulation) as Simulation; newSimulation.Name = newSimulationName; newSimulation.Parent = null; newSimulation.FileName = parentSimulations.FileName; Apsim.ParentAllChildren(newSimulation); // Make substitutions. Simulations.MakeSubstitutions(parentSimulations, new List <Simulation> { newSimulation }); // Connect events and links in our new simulation. Events events = new Events(); events.AddModelEvents(newSimulation); events.CallEventHandler(newSimulation, "Loaded", null); foreach (FactorValue value in combination) { value.ApplyToSimulation(newSimulation); } PushFactorsToReportModels(newSimulation, combination); return(newSimulation); } } return(null); }
/// <summary> /// Returns a lightweight skeleton simulation which can be run. /// </summary> public static Simulations GetRunnableSim() { Simulations sims = new Simulations() { FileName = Path.ChangeExtension(Path.GetTempFileName(), ".apsimx"), Children = new List <IModel>() { new DataStore(), new Simulation() { Children = new List <IModel>() { new Clock() { StartDate = new DateTime(2017, 1, 1), EndDate = new DateTime(2017, 1, 10) // January 10 }, new Summary(), new Zone() { Area = 1, Children = new List <IModel>() { new Models.Report() { VariableNames = new string[] { "[Clock].Today.DayOfYear as n" }, EventNames = new string[] { "[Clock].DoReport" } } } } } } } }; sims.ParentAllDescendants(); sims.Write(sims.FileName); return(sims); }
private static void Document(DocumentOptions options) { if (options.Files == null || !options.Files.Any()) { throw new ArgumentException($"No files were specified"); } IEnumerable <string> files = options.Files.SelectMany(f => DirectoryUtilities.FindFiles(f, options.Recursive)); if (!files.Any()) { files = options.Files; } foreach (string file in files) { Simulations sims = FileFormat.ReadFromFile <Simulations>(file, e => throw e, false); IModel model = sims; if (Path.GetExtension(file) == ".json") { sims.Links.Resolve(sims, true, true, false); } if (!string.IsNullOrEmpty(options.Path)) { IVariable variable = model.FindByPath(options.Path); if (variable == null) { throw new Exception($"Unable to resolve path {options.Path}"); } object value = variable.Value; if (value is IModel modelAtPath) { model = modelAtPath; } else { throw new Exception($"{options.Path} resolved to {value}, which is not a model"); } } string pdfFile = Path.ChangeExtension(file, ".pdf"); string directory = Path.GetDirectoryName(file); PdfWriter writer = new PdfWriter(new PdfOptions(directory, null)); IEnumerable <ITag> tags = options.ParamsDocs ? new ParamsInputsOutputs(model).Document() : model.Document(); writer.Write(pdfFile, tags); } }
/// <summary> /// Attach the model (report) and the view (IReportView) /// </summary> /// <param name="model">The report model object</param> /// <param name="view">The view object</param> /// <param name="explorerPresenter">The explorer presenter</param> public void Attach(object model, object view, ExplorerPresenter explorerPresenter) { this.report = model as Report; this.explorerPresenter = explorerPresenter; this.view = view as IReportView; this.intellisense = new IntellisensePresenter(view as ViewBase); intellisense.ItemSelected += OnIntellisenseItemSelected; this.view.VariableList.ScriptMode = false; this.view.EventList.ScriptMode = false; this.view.VariableList.Lines = report.VariableNames; this.view.EventList.Lines = report.EventNames; this.view.VariableList.ContextItemsNeeded += OnNeedVariableNames; this.view.EventList.ContextItemsNeeded += OnNeedEventNames; this.view.VariableList.TextHasChangedByUser += OnVariableNamesChanged; this.view.EventList.TextHasChangedByUser += OnEventNamesChanged; this.explorerPresenter.CommandHistory.ModelChanged += OnModelChanged; Simulations simulations = Apsim.Parent(report, typeof(Simulations)) as Simulations; if (simulations != null) { dataStore = Apsim.Child(simulations, typeof(IStorageReader)) as IStorageReader; } //// TBI this.view.VariableList.SetSyntaxHighlighter("Report"); dataStorePresenter = new DataStorePresenter(); Simulation simulation = Apsim.Parent(report, typeof(Simulation)) as Simulation; if (simulation != null) { if (simulation.Parent is Experiment) { dataStorePresenter.ExperimentFilter = simulation.Parent as Experiment; } else { dataStorePresenter.SimulationFilter = simulation; } } dataStorePresenter.Attach(dataStore, this.view.DataStoreView, explorerPresenter); this.view.DataStoreView.TableList.SelectedValue = this.report.Name; this.view.TabIndex = this.report.ActiveTabIndex; }
/// <summary> /// Updates a simulation over a single generation /// </summary> /// <param name="Sim">The link to the simulation data</param> public override void Update(Simulations.Simulation Sim) { //Create a new temporary list List<Particle> NewList = new List<Particle>(Sim.SimUniverse.Particles.Count); //For each particle: Parallel.For(0, Sim.SimUniverse.Particles.Count, i => { //Create a new temporary particle with the same properties of the old one Particle NewParticle = Particle.Copy_Shallow(Sim.SimUniverse.Particles[i], true); //Get simulation parameters double MinDistance; if (Sim.Parameters.HasParam("MinimumDistance")) { MinDistance = Sim.Parameters.GetParam("MinimumDistance"); } else { MinDistance = 0; } double MaxDistance; if (Sim.Parameters.HasParam("MaximumDistance")) { MaxDistance = Sim.Parameters.GetParam("MaximumDistance"); } else { MaxDistance = 5; } //Update the new particle NewParticle.Position = SimMath.RandomWalk(NewParticle.Position, MinDistance, MaxDistance); //Add the updated particle to the new list lock (NewList) { NewList.Add(NewParticle); } }); //Save the temporary list to the new one Sim.SimUniverse.Particles = NewList; }
public void TestOverridingInMultiplePaddocks() { string json = ReflectionUtilities.GetResourceAsString("UnitTests.Factorial.MultiPaddockFactorOverride.apsimx"); Simulations sims = FileFormat.ReadFromString <Simulations>(json, out List <Exception> errors); if (errors != null && errors.Count > 0) { throw errors[0]; } Runner runner = new Runner(sims); errors = runner.Run(); if (errors != null && errors.Count > 0) { throw errors[0]; } }
public void ImporterTests_AreaImports() { string oldXml = "<folder version=\"36\" creator=\"Apsim 7.5-r3183\" name=\"simulations\">" + " <simulation name=\"Continuous Wheat\">" + " <area name=\"paddock\">" + " <paddock_area>100</paddock_area>" + " </area>" + " </simulation>" + "</folder>"; var importer = new Importer(); Simulations sims = importer.CreateSimulationsFromXml(oldXml); Zone z = sims.Children[0].Children[0] as Zone; Assert.AreEqual(z.Area, 100); }
public void ImporterTests_MetFileImports() { string oldXml = "<folder version=\"36\" creator=\"Apsim 7.5-r3183\" name=\"simulations\">" + " <simulation name=\"Continuous Wheat\">" + " <metfile name=\"met\">" + " <filename name=\"filename\" input=\"yes\">%apsim%/Examples/MetFiles/Goond.met</filename>" + " </metfile>" + " </simulation>" + "</folder>"; APSIMImporter importer = new APSIMImporter(); Simulations sims = importer.CreateSimulationsFromXml(oldXml); var w = sims.Children[0].Children[0] as Models.Weather; Assert.AreEqual(w.FileName, @"\Examples\MetFiles\Goond.met"); }
/// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void HLBackgroundWorker_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e) { HLBackGroundWorker hlbw = (HLBackGroundWorker)sender; List <object> Arguments = e.Argument as List <object>; Simulation sim = (Simulation)Arguments[0]; //hlbw.Sim = SimulationFactory.GenerateSimulationXML(simElement, InputDataModels); //hlbw.Sim.Id = SimulationElements.IndexOf(simElement) + 1; hlbw.Sim = sim; hlbw.Sim.Index = Simulations.IndexOf(sim) + 1; //Setup output controllers if (OutputType == OutputType.CSVOutput) { hlbw.Sim.OutputModelController = new CSVOutputModelController(hlbw.Sim, OutputPath); } else if (OutputType == OutputType.SQLiteOutput) { hlbw.Sim.OutputModelController = new SQLiteOutputModelController(hlbw.Sim, SQLConn); //HLRDB.Simulation DBSim = new HLRDB.Simulation { Id = hlbw.Sim.Id, Name = hlbw.Sim.Name }; //DBSim.Data = new List<HLRDB.Data>(); //DBSim.Models = new List<Model>(); //foreach (InputModel im in hlbw.Sim.InputModels) //{ // DBSim.Models.Add(new HLRDB.Model { Name = im.Name, Type = im.GetType().ToString(), Content = "" }); //} //DBContext.Simulations.Add(new HLRDB.Simulation { Id = hlbw.Sim.Id, Name = hlbw.Sim.Name }); //DBContext.SaveChanges(); } else if (OutputType == OutputType.NetCDF) { //hlbw.Sim.OutputModelController = new NetCDFOutputModelController(hlbw.Sim, HLNC); } hlbw.Sim.Run(); }
public void Attach(object model, object view, ExplorerPresenter explorerPresenter) { this.ApsimXFile = model as Simulations; this.ExplorerPresenter = explorerPresenter; this.View = view as IModelDetailsWrapperView; if (model != null) { ViewNameAttribute viewName = ReflectionUtilities.GetAttribute(model.GetType(), typeof(ViewNameAttribute), false) as ViewNameAttribute; PresenterNameAttribute presenterName = ReflectionUtilities.GetAttribute(model.GetType(), typeof(PresenterNameAttribute), false) as PresenterNameAttribute; View.ModelTypeText = model.GetType().ToString().Substring("Models.".Length); DescriptionAttribute descAtt = ReflectionUtilities.GetAttribute(model.GetType(), typeof(DescriptionAttribute), false) as DescriptionAttribute; if (descAtt != null) { View.ModelDescriptionText = descAtt.ToString(); } else { View.ModelDescriptionText = ""; } // Set CLEM specific colours for title if (View.ModelTypeText.Contains(".Resources.")) { View.ModelTypeTextColour = "996633"; } else if (View.ModelTypeText.Contains("Activities.")) { View.ModelTypeTextColour = "009999"; } HelpUriAttribute helpAtt = ReflectionUtilities.GetAttribute(model.GetType(), typeof(HelpUriAttribute), false) as HelpUriAttribute; View.ModelHelpURL = ""; if (helpAtt != null) { View.ModelHelpURL = helpAtt.ToString(); } if (viewName != null && presenterName != null) { ShowInLowerPanel(model, viewName.ToString(), presenterName.ToString()); } } }
/// <summary>Called to start the job.</summary> /// <param name="jobManager">The job manager running this job.</param> /// <param name="workerThread">The thread this job is running on.</param> public void Run(JobManager jobManager, BackgroundWorker workerThread) { List <List <FactorValue> > allCombinations = AllCombinations(); Simulation baseSimulation = Apsim.Child(this, typeof(Simulation)) as Simulation; Simulations parentSimulations = Apsim.Parent(this, typeof(Simulations)) as Simulations; Stream serialisedBase = Apsim.SerialiseToStream(baseSimulation) as Stream; List <Simulation> simulations = new List <Simulation>(); foreach (List <FactorValue> combination in allCombinations) { string newSimulationName = Name; foreach (FactorValue value in combination) { newSimulationName += value.Name; } Simulation newSimulation = Apsim.DeserialiseFromStream(serialisedBase) as Simulation; newSimulation.Name = newSimulationName; newSimulation.Parent = null; newSimulation.FileName = parentSimulations.FileName; Apsim.ParentAllChildren(newSimulation); // Make substitutions. Simulations.MakeSubstitutions(parentSimulations, new List <Simulation> { newSimulation }); // Call OnLoaded in all models. Events events = new Events(); events.AddModelEvents(newSimulation); events.CallEventHandler(newSimulation, "Loaded", null); foreach (FactorValue value in combination) { value.ApplyToSimulation(newSimulation); } PushFactorsToReportModels(newSimulation, combination); StoreFactorsInDataStore(newSimulation, combination); jobManager.AddChildJob(this, newSimulation); } }
public void EnsureDataIsNotWrittenTwice() { Simulations sims = Utilities.GetRunnableSim(); Simulation sim = sims.FindChild <Simulation>(); Summary summary = sim.FindChild <Summary>(); // Write 2 messages to the DB during StartOfSimulation. string message1 = "message 1"; string message2 = "A slightly longer message"; string message3 = "Written in OnCompleted"; SummaryWriter writer = new SummaryWriter(); writer.AddMessage("[Clock].StartOfSimulation", message1); writer.AddMessage("[Clock].StartOfSimulation", message2); writer.AddMessage("[Simulation].Completed", message3); Structure.Add(writer, sim); Runner runner = new Runner(sims); List <Exception> errors = runner.Run(); if (errors != null && errors.Count > 0) { throw errors[0]; } IDataStore storage = sims.FindChild <IDataStore>(); DataTable messages = storage.Reader.GetData("_Messages"); // Clock will write its own "Simulation terminated normally" message. Assert.AreEqual(5, messages.Rows.Count); // The first row will be a warning caused by the lack of a // microclimate model. Assert.AreEqual(message1, messages.Rows[1][6]); Assert.AreEqual(message2, messages.Rows[2][6]); // The fourth row should not be written by SummaryWriter. Assert.AreNotEqual(writer.Name, messages.Rows[3]["ComponentName"]); // The fifth will be the "Simulation terminated normally" message. Assert.AreEqual(message3, messages.Rows[4][6]); }
/// <summary> /// Open an .apsimx file into the current tab. /// </summary> /// <param name="fileName">The file to open</param> public void OpenApsimXFileInTab(string fileName) { if (fileName != null) { ExplorerView explorerView = new ExplorerView(); ExplorerPresenter presenter = new ExplorerPresenter(); this.Presenters.Add(presenter); try { Cursor.Current = Cursors.WaitCursor; Simulations simulations = Simulations.Read(fileName); presenter.Attach(simulations, explorerView, null); this.view.AddTab(fileName, Properties.Resources.apsim_logo32, explorerView, true); // restore the simulation tree width on the form if (simulations.ExplorerWidth == 0) { presenter.TreeWidth = 250; } else { presenter.TreeWidth = Math.Min(simulations.ExplorerWidth, this.view.TabWidth - 20); // ? } Utility.Configuration.Settings.AddMruFile(fileName); List <string> validMrus = new List <string>(); // make sure recently used files still exist before displaying them foreach (string s in Utility.Configuration.Settings.MruList) { if (File.Exists(s)) { validMrus.Add(s); } } Utility.Configuration.Settings.MruList = validMrus; //this.view.FillMruList(validMrus); Cursor.Current = Cursors.Default; } catch (Exception err) { this.view.ShowError(err.Message); } } }
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"); }
/// <summary> /// Generates .apsimx files for each simulation in a runner. /// Returns the names of the generated files. /// </summary> /// <param name="runner">A runner containing a set of simulations.</param> /// <param name="simsPerFile">Number of simulations in each generated file.</param> /// <param name="path">Path which the files will be saved to.</param> /// <param name="progressCallBack">Invoked when the method needs to indicate progress.</param> /// <param name="collectExternalFiles">Collect all external files and store on path?</param> /// <returns>Names of the generated files.</returns> public static IEnumerable <string> Generate(Runner runner, uint simsPerFile, string path, OnProgress progressCallBack, bool collectExternalFiles = false) { if (simsPerFile > int.MaxValue) { // This would be over 2 billion sims. There would be other, more fundamental // problems well before this point is reached. Like max capacity of a list. throw new InvalidOperationException("too many simulations"); } Directory.CreateDirectory(path); int i = 0; Queue <Simulation> simulations = new Queue <Simulation>(runner.Simulations()); List <Simulation> simsInCurrentFile = new List <Simulation>(); int numSims = simulations.Count; int numFiles = simulations.Count / (int)simsPerFile + 1; List <string> generatedFiles = new List <string>(numFiles); while (simulations.Any()) { Simulations sims = new Simulations() { Name = "Simulations", Children = new List <IModel>() { new DataStore() } }; foreach (Simulation sim in simulations.DequeueChunk(simsPerFile)) { FixSimulation(sim, path, collectExternalFiles); sims.Children.Add(sim); } string st = FileFormat.WriteToString(sims); string fileName = Path.Combine(path, $"generated-{i}.apsimx"); generatedFiles.Add(fileName); File.WriteAllText(fileName, st); progressCallBack?.Invoke(1.0 * (i + 1) / numSims); i++; } return(generatedFiles); }
public void ImporterTests_MetFileImports() { string oldXml = "<folder version=\"36\" creator=\"Apsim 7.5-r3183\" name=\"simulations\">" + " <simulation name=\"Continuous Wheat\">" + " <metfile name=\"met\">" + " <filename name=\"filename\" input=\"yes\">%apsim%/Examples/MetFiles/Goond.met</filename>" + " </metfile>" + " </simulation>" + "</folder>"; var importer = new Importer(); Simulations sims = importer.CreateSimulationsFromXml(oldXml); var w = sims.Children[0].Children[0] as Models.Climate.Weather; string expected = Path.Combine(Path.DirectorySeparatorChar.ToString(), "Examples", "MetFiles", "Goond.met"); Assert.AreEqual(w.FileName, expected); }
public static void WriteApsimX(Simulations simulations, string name) { using (StreamWriter stream = new StreamWriter($"{OutDir}\\{name}.apsimx")) using (JsonWriter writer = new JsonTextWriter(stream)) { writer.CloseOutput = true; writer.AutoCompleteOnClose = true; JsonSerializer serializer = new JsonSerializer() { Formatting = Formatting.Indented, TypeNameHandling = TypeNameHandling.Objects }; serializer.Serialize(writer, simulations); serializer = null; simulations.Dispose(); } }
/// <summary> /// Replace a model with a model from another file. /// </summary> /// <param name="topLevel">The top-level model of the file being modified.</param> /// <param name="modelToReplace">Path to the model which is to be replaced.</param> /// <param name="replacementFile">Path of the .apsimx file containing the model which will be inserted.</param> /// <param name="replacementPath">Path to the model in replacementFile which will be used to replace a model in topLevel.</param> private static void ReplaceModelFromFile(Simulations topLevel, string modelToReplace, string replacementFile, string replacementPath) { IModel toBeReplaced = topLevel.FindByPath(modelToReplace)?.Value as IModel; if (toBeReplaced == null) { throw new Exception($"Unable to find model which is to be replaced ({modelToReplace}) in file {topLevel.FileName}"); } IModel extFile = FileFormat.ReadFromFile <IModel>(replacementFile, e => throw e, false); IModel replacement; if (string.IsNullOrEmpty(replacementPath)) { replacement = extFile.FindAllDescendants().Where(d => toBeReplaced.GetType().IsAssignableFrom(d.GetType())).FirstOrDefault(); if (replacement == null) { throw new Exception($"Unable to find replacement model of type {toBeReplaced.GetType().Name} in file {replacementFile}"); } } else { replacement = extFile.FindByPath(replacementPath)?.Value as IModel; if (replacement == null) { throw new Exception($"Unable to find model at path {replacementPath} in file {replacementFile}"); } } IModel parent = toBeReplaced.Parent; int index = parent.Children.IndexOf((Model)toBeReplaced); parent.Children.Remove((Model)toBeReplaced); // Need to call Structure.Add to add the model to the parent. Structure.Add(replacement, parent); // Move the new model to the index in the list at which the // old model previously resided. parent.Children.Remove((Model)replacement); parent.Children.Insert(index, (Model)replacement); }
public void TestGetAllSimulations() { String addr = Properties.Settings.Default.simulationAddress; Debug.WriteLine("Address: " + addr, this.GetType()); SimulationClient cli = new SimulationClient(); cli.SetBasicAuth(Properties.Settings.Default.username, Properties.Settings.Default.password); Simulations sims = cli.GetSimulationRoot(new Uri(addr)); //Assert.AreEqual(10, sims.Count); Assert.IsTrue(sims.Count >= 1); Debug.WriteLine("Name: " + simulation_mea_uq, this.GetType()); Simulation sim = sims.Single(s => s.Name == simulation_mea_uq); Assert.IsNotNull(sim); Assert.AreEqual <String>(sim.Name, simulation_mea_uq); }
/// <summary> /// Generates .apsimx files for each simulation in a runner. /// Returns any exceptions thrown. /// </summary> /// <param name="runner">A runner containing a set of simulations.</param> /// <param name="path">Path which the files will be saved to.</param> /// <param name="progressCallBack">Invoked when the method needs to indicate progress.</param> /// <returns>null for success or a list of exceptions.</returns> public static List <Exception> Generate(Runner runner, string path, OnProgress progressCallBack) { List <Exception> errors = null; if (runner.TotalNumberOfSimulations > 0) { Directory.CreateDirectory(path); int i = 0; foreach (var simulation in runner.Simulations()) { try { Simulations sims = new Simulations() { Name = "Simulations", Children = new List <Model>() { new Storage.DataStore() { Name = "DataStore" }, simulation } }; string st = FileFormat.WriteToString(sims); File.WriteAllText(Path.Combine(path, simulation.Name + ".apsimx"), st); } catch (Exception err) { if (errors == null) { errors = new List <Exception>(); } errors.Add(err); } i++; progressCallBack?.Invoke(100 * i / runner.TotalNumberOfSimulations); } } return(errors); }
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>Attach the model (report) and the view (IReportView)</summary> public void Attach(object model, object view, ExplorerPresenter explorerPresenter) { this.report = model as Report; this.explorerPresenter = explorerPresenter; this.view = view as IReportActivityLedgerView; this.explorerPresenter.CommandHistory.ModelChanged += OnModelChanged; Simulations simulations = Apsim.Parent(report, typeof(Simulations)) as Simulations; if (simulations != null) { dataStore = Apsim.Child(simulations, typeof(IDataStore)) as IDataStore; } dataStorePresenter = new DataStorePresenter(); activityGridPresenter = new ActivityLedgerGridPresenter(); Simulation simulation = Apsim.Parent(report, typeof(Simulation)) as Simulation; Zone paddock = Apsim.Parent(report, typeof(Zone)) as Zone; if (paddock != null) { dataStorePresenter.ZoneFilter = paddock; } if (simulation != null) { if (simulation.Parent is Experiment) { dataStorePresenter.ExperimentFilter = simulation.Parent as Experiment; } else { dataStorePresenter.SimulationFilter = simulation; } } dataStorePresenter.Attach(dataStore, this.view.DataStoreView, explorerPresenter); activityGridPresenter.ModelName = this.report.Name; activityGridPresenter.SimulationName = simulation.Name; activityGridPresenter.ZoneName = paddock.Name; activityGridPresenter.Attach(dataStore, this.view.DisplayView, explorerPresenter); dataStorePresenter.tableDropDown.SelectedValue = this.report.Name; }
/// <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) { if (allCombinations == null || allCombinations.Count < 1) { allCombinations = EnabledCombinations(); } 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 HttpResponseMessage PostPredict([FromBody] PredictionRequest pr) { var volatility = Simulations.Volatilities .TryFind(pr.Symbol).ToResult(); var price = pr.Price; if (volatility.IsOk) { var calcReq = new Simulations.CalcRequest(pr.NumTimesteps, pr.Price, volatility.Ok); price = Simulations.calcPriceCPU(calcReq); } var prediction = new PredictionResponse(price, new double[0]); return(this.Request.CreateResponse(HttpStatusCode.OK, prediction)); }
/// <summary> /// Generates .apsimx files for each simulation in a runner. /// Returns any exceptions thrown. /// </summary> /// <param name="runner">A runner containing a set of simulations.</param> /// <param name="path">Path which the files will be saved to.</param> /// <param name="progressCallBack">Invoked when the method needs to indicate progress.</param> /// <returns>null for success or a list of exceptions.</returns> public static List <Exception> Generate(Runner runner, string path, OnProgress progressCallBack) { List <Exception> errors = null; Directory.CreateDirectory(path); int i = 0; List <Simulation> simulations = runner.Simulations().ToList(); foreach (var simulation in simulations) { try { Simulations sims = new Simulations() { Name = "Simulations", Children = new List <IModel>() { new Storage.DataStore() { Name = "DataStore" }, simulation } }; string st = FileFormat.WriteToString(sims); File.WriteAllText(Path.Combine(path, simulation.Name + ".apsimx"), st); } catch (Exception err) { if (errors == null) { errors = new List <Exception>(); } errors.Add(err); } progressCallBack?.Invoke(Convert.ToInt32(100 * (i + 1) / simulations.Count)); i++; } return(errors); }
public static void TestReportingOnModelEvents() { string json = ReflectionUtilities.GetResourceAsString("UnitTests.Report.ReportOnEvents.apsimx"); Simulations file = FileFormat.ReadFromString <Simulations>(json, out List <Exception> fileErrors); if (fileErrors != null && fileErrors.Count > 0) { throw fileErrors[0]; } // This simulation needs a weather node, but using a legit // met component will just slow down the test. IModel sim = file.FindInScope <Simulation>(); Model weather = new MockWeather(); sim.Children.Add(weather); weather.Parent = sim; // Run the file. var Runner = new Runner(file); Runner.Run(); // Check that the report reported on the correct dates. var storage = file.FindInScope <IDataStore>(); List <string> fieldNames = new List <string>() { "doy" }; DataTable data = storage.Reader.GetData("ReportOnFertilisation", fieldNames: fieldNames); double[] values = DataTableUtilities.GetColumnAsDoubles(data, "doy"); double[] expected = new double[] { 1, 32, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 364 }; Assert.AreEqual(expected, values); data = storage.Reader.GetData("ReportOnIrrigation", fieldNames: fieldNames); values = DataTableUtilities.GetColumnAsDoubles(data, "doy"); // There is one less irrigation event, as the manager script doesn't irrigate. expected = new double[] { 1, 32, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335 }; Assert.AreEqual(expected, values); }
public void EnsureScopedPostSimulationToolsAreRun() { Simulation sim0 = new Simulation() { Name = "s0", Children = new List <IModel>() { new MockClock(), new MockSummary(), new PostSimToolWhichThrows() } }; Simulation sim1 = sim0.Clone(); sim1.Name = "s1"; Simulations sims = new Simulations() { Children = new List <IModel>() { new MockStorage() { Children = new List <IModel>() { new PostSimToolWhichThrows() } }, sim0, sim1 } }; sims.ParentAllDescendants(); Runner runner = new Runner(sim1); List <Exception> errors = runner.Run(); // We ran sim1 only. Therefore two post-simulation tools // should have been run (the one under the simulation and the // one under the datastore). Assert.AreEqual(2, errors.Count); }
public override string ToString() { var sb = new StringBuilder(); sb.AppendLine($"Project file: {ProjectFile}"); this.LogOutputFolder(sb); if (Simulations.Any()) { sb.AppendLine($"Exporting simulation: {Simulations.ToString(", ")}"); } else { sb.AppendLine($"Exporting all simulations"); } sb.AppendLine($"Run simulation: {RunSimulation}"); LogDefaultOptions(sb); return(sb.ToString()); }
public void TestFileNameChange() { Simulations sims = Utilities.GetRunnableSim(); IDataStore storage = Apsim.Find(sims, typeof(IDataStore)) as IDataStore; // Write the simulations to disk. sims.Write(sims.FileName); // Record the database's filename. string oldDbFileName = storage.FileName; // Write the simulations to disk under a new filename. sims.Write(Path.ChangeExtension(Path.GetTempFileName(), ".apsimx")); // Record the database's new filename. string newDbFileName = storage.FileName; // The new file name should not be the same as the old one. Assert.AreNotEqual(oldDbFileName, newDbFileName); }
/// <summary>Constructor</summary> /// <param name="model">The model to run.</param> /// <param name="simulations">simulations object.</param> /// <param name="runTests">Run the test nodes?</param> public RunOrganiser(Simulations simulations, Model model, bool runTests) { this.simulations = simulations; this.model = model; this.runTests = runTests; }
/// <summary> /// Calculates the interaction between two particles, returning /// </summary> /// <param name="Input">The particle to update</param> /// <param name="Comparison">The particle to compare this particle to</param> /// <param name="SimParams">The list of simulation parameters</param> /// <param name="Interactions">The list of available interaction scripts</param> /// <param name="Cosmos">The universe of available particles</param> public abstract dynamic CalculateInteraction(Particle Input, Particle Comparison, Simulations.Simulation Sim);
/// <summary> /// Updates a simulation over a single generation /// </summary> /// <param name="Sim">The link to the simulation data</param> public abstract void Update(Simulations.Simulation Sim);
public override dynamic CalculateInteraction(Particle Input, Particle Comparison, Simulations.Simulation Sim) { //First we create a new force vector to store our result to Vector Force = new Vector(Input.NetForce.Capacity); //Make sure both our input and comparison particles have "charge" if (!Input.HasProperty("Charge") || !Comparison.HasProperty("Charge")) return Force; double InputCharge = Input.GetPropertyVal("Charge"); double ComparisonCharge = Comparison.GetPropertyVal("Charge"); if (InputCharge == 0 || ComparisonCharge == 0) return Force; //Now we calculate the actual distance between the particles (we will need this later) double ActualDistance = (Comparison.Position - Input.Position).Magnitude; if (ActualDistance == 0) return new Vector(Input.Acceleration.Count); //If the "Precision" simulation parameter is available to us: if (Sim.Parameters.HasParam("Precision")) { //Check to see if the actual distance is under the precision value if ((System.Math.Abs(ActualDistance) - Input.Radius - Comparison.Radius) <= Sim.Parameters.GetParam("Precision")) { //Return no change in acceleration return new Vector(Input.Acceleration.Count); } } else //Otherwise: { //Assume a precision of zero if ((System.Math.Abs(ActualDistance) - Input.Radius - Comparison.Radius) <= 0) { //Return no change in acceleration return new Vector(Input.Acceleration.Count); } } //If the "SofteningValue" simulation parameter is available to us: double Denominator; if (Sim.Parameters.HasParam("SofteningValue")) { //Calculate the denominator of the equation double SofteningValue = Sim.Parameters.GetParam("SofteningValue"); Denominator = System.Math.Pow(((ActualDistance * ActualDistance) + (SofteningValue * SofteningValue)), (3.0 / 2.0)); } else //Otherwise: { //Calculate the denominator of the equation Denominator = System.Math.Pow((ActualDistance * ActualDistance), (3.0 / 2.0)); } //Get all of our needed parts double ElectrostaticConstant; if (Sim.Parameters.HasParam("ElectrostaticConstant")) { ElectrostaticConstant = Sim.Parameters.GetParam("ElectrostaticConstant"); } else { ElectrostaticConstant = PhysicsConstants.ElectrostaticConstant; } //For each dimension of movement/location: for (int i = 0; i < Force.Capacity; i++) { //Calculate the component distance of this dimension double ComponentDistance = Input.Position[i] - Comparison.Position[i]; //Calculate the new force Force[i] = ElectrostaticConstant * (InputCharge * ComparisonCharge * ComponentDistance) / Denominator; } //Return the new net force return Force; }
/// <summary>Initializes a new instance of the <see cref="RunAllCompletedEvent"/> class.</summary> /// <param name="simulations">Top level simulations object.</param> public RunAllCompletedEvent(Simulations simulations) { this.simulations = simulations; }
/// <summary> /// Updates a simulation over a single generation /// </summary> /// <param name="Sim">The link to the simulation data</param> public override void Update(Simulations.Simulation Sim) { //Create a new temporary list List<Particle> NewList = new List<Particle>(Sim.SimUniverse.Particles.Count); //For each particle: Parallel.For(0, Sim.SimUniverse.Particles.Count, i => { //Create a new temporary particle with the same properties of the old one Particle NewParticle = Particle.Copy_Shallow(Sim.SimUniverse.Particles[i], true); //Update the new particle NewParticle = UpdateParticle(NewParticle, Sim); //Add the updated particle to the new list lock (NewList) { NewList.Add(NewParticle); } }); //Save the temporary list to the new one Sim.SimUniverse.Particles = NewList; }