/// <summary> /// Replace the object specified by 'path' in 'newSimulation' with the specified 'value' /// </summary> private static void ApplyModelReplacement(Simulation newSimulation, string path, IModel value) { IModel newModel = Apsim.Clone(value); newSimulation.Locator().Clear(); IModel modelToReplace = newSimulation.Get(path) as IModel; if (modelToReplace == null) { throw new Exception("Cannot find model to replace. Model path: " + path); } int index = modelToReplace.Parent.Children.IndexOf(modelToReplace as Model); if (index == -1) { throw new Exception("Cannot find model to replace. Model path: " + path); } modelToReplace.Parent.Children.RemoveAt(index); modelToReplace.Parent.Children.Insert(index, newModel as Model); newModel.Name = modelToReplace.Name; newModel.Parent = modelToReplace.Parent; Apsim.CallEventHandler(newModel, "Loaded", null); }
/// <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; 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; Apsim.ParentAllChildren(newSimulation); // Connect events and links in our new simulation. foreach (Model child in Apsim.ChildrenRecursively(newSimulation)) { Apsim.CallEventHandler(child, "Loaded", null); } foreach (FactorValue value in combination) { value.ApplyToSimulation(newSimulation); } PushFactorsToReportModels(newSimulation, combination); return(newSimulation); } } return(null); }
/// <summary> /// Create all simulations. /// </summary> public Simulation[] Create() { List <List <FactorValue> > allCombinations = AllCombinations(); Simulation baseSimulation = Apsim.Child(this, typeof(Simulation)) as Simulation; 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.Clone(baseSimulation) as Simulation; newSimulation.Name = newSimulationName; newSimulation.Parent = null; Apsim.ParentAllChildren(newSimulation); // Call OnLoaded in all models. foreach (Model child in Apsim.ChildrenRecursively(newSimulation)) { Apsim.CallEventHandler(child, "Loaded", null); } foreach (FactorValue value in combination) { value.ApplyToSimulation(newSimulation); } PushFactorsToReportModels(newSimulation, combination); simulations.Add(newSimulation); } return(simulations.ToArray()); }