public static MadingleyModelInitialisation ConvertInitialisation( Madingley.Common.ModelState m, Madingley.Common.Configuration d, Madingley.Common.Environment e) { var i = new MadingleyModelInitialisation("", "", "", ""); i.GlobalModelTimeStepUnit = d.GlobalModelTimeStepUnit; i.NumTimeSteps = (uint)d.NumTimeSteps; i.BurninTimeSteps = (uint)d.BurninTimeSteps; i.ImpactTimeSteps = (uint)d.ImpactTimeSteps; i.RecoveryTimeSteps = (uint)d.RecoveryTimeSteps; i.CellSize = e.CellSize; i.BottomLatitude = (float)e.BottomLatitude; i.TopLatitude = (float)e.TopLatitude; i.LeftmostLongitude = (float)e.LeftmostLongitude; i.RightmostLongitude = (float)e.RightmostLongitude; i.RunCellsInParallel = d.RunCellsInParallel; i.RunSimulationsInParallel = d.RunSimulationsInParallel; i.RunRealm = d.RunRealm; i.DrawRandomly = d.DrawRandomly; i.ExtinctionThreshold = d.ExtinctionThreshold; i.MaxNumberOfCohorts = d.MaxNumberOfCohorts; i.DispersalOnly = d.DispersalOnly; i.PlanktonDispersalThreshold = d.PlanktonDispersalThreshold; i.SpecificLocations = e.SpecificLocations; i.InitialisationFileStrings = new SortedList <string, string>(); i.InitialisationFileStrings["OutputDetail"] = "high"; i.InitialisationFileStrings["DispersalOnlyType"] = d.DispersalOnlyType; i.CohortFunctionalGroupDefinitions = ConvertFunctionalGroupDefinitions(d.CohortFunctionalGroupDefinitions); i.StockFunctionalGroupDefinitions = ConvertFunctionalGroupDefinitions(d.StockFunctionalGroupDefinitions); if (m != null) { i.EnviroStack = ConvertEnvironment(m.GridCells); } else { i.EnviroStack = ConvertEnvironment(e.CellEnvironment); } i.CellList = e.FocusCells.Select(a => new UInt32[] { (uint)a.Item1, (uint)a.Item2 }).ToList(); i.TrackProcesses = true; i.TrackCrossCellProcesses = true; i.TrackGlobalProcesses = true; i.Units = new SortedList <string, string>(e.Units); i.ImpactCellIndices = d.ImpactCellIndices.Select(ii => (uint)ii).ToList(); i.ImpactAll = d.ImpactAll; if (m != null) { i.ModelStates = ConvertModelStates(m, d, e); i.InputState = true; i.InputGlobalDiagnosticVariables = new SortedList <string, double>(m.GlobalDiagnosticVariables); } else { i.ModelStates = null; i.InputState = false; } return(i); }
public static List <InputModelState> ConvertModelStates( Madingley.Common.ModelState modelState, Madingley.Common.Configuration c, Madingley.Common.Environment e) { var numLatCells = (UInt32)((e.TopLatitude - e.BottomLatitude) / e.CellSize); var numLonCells = (UInt32)((e.RightmostLongitude - e.LeftmostLongitude) / e.CellSize); // Set up a grid of grid cells var gridCellCohorts = new GridCellCohortHandler[numLatCells, numLonCells]; var gridCellStocks = new GridCellStockHandler[numLatCells, numLonCells]; gridCellCohorts[0, 0] = new GridCellCohortHandler(c.CohortFunctionalGroupDefinitions.Data.Count()); var cellList = e.FocusCells.ToArray(); var gridCells = modelState.GridCells.ToArray(); for (var ii = 0; ii < cellList.Count(); ii++) { var gridCell = gridCells[ii]; gridCellCohorts[cellList[ii].Item1, cellList[ii].Item2] = ConvertCohorts(gridCell.Cohorts); gridCellStocks[cellList[ii].Item1, cellList[ii].Item2] = ConvertStocks(gridCell.Stocks); } var inputModelState = new InputModelState(gridCellCohorts, gridCellStocks); return(new List <InputModelState>() { inputModelState }); }
public MadingleyModelOutput( string outputFilesSuffix, Madingley.Common.Environment environment, Madingley.Common.Configuration configuration, MadingleyModelInitialisation outputSettings, Madingley.Common.ModelState modelState) { var modelInitialisation = Converters.ConvertInitialisation( outputSettings, configuration, environment); this.ProcessTracker = new Madingley.Common.IProcessTracker[environment.FocusCells.Count()][]; for (var cellIndex = 0; cellIndex < environment.FocusCells.Count(); cellIndex++) { var processTracker = new GEMProcessTracker(cellIndex, this); this.ProcessTracker[cellIndex] = new Madingley.Common.IProcessTracker[] { processTracker }; } var globalProcessTracker = new GEMGlobalProcessTracker(this); this.GlobalProcessTracker = new Madingley.Common.IGlobalProcessTracker[] { globalProcessTracker }; var crossCellProcessTracker = new GEMCrossCellProcessTracker(this); this.CrossCellProcessTracker = new Madingley.Common.ICrossCellProcessTracker[] { crossCellProcessTracker }; this.model = new MadingleyModel( modelInitialisation, outputFilesSuffix, configuration.Simulation, modelState); }
public static void Serialize(Madingley.Common.ModelState modelState, TextWriter sw) { Action<Newtonsoft.Json.JsonWriter, Madingley.Common.Cohort> JsonAddPropertyCohort = (jsonWriter, value) => { jsonWriter.WriteStartObject(); Common.Writer.PropertyInt(jsonWriter, "FunctionalGroupIndex", value.FunctionalGroupIndex); Common.Writer.PropertyInt(jsonWriter, "BirthTimeStep", value.BirthTimeStep); Common.Writer.PropertyInt(jsonWriter, "MaturityTimeStep", value.MaturityTimeStep); Common.Writer.PropertyInlineArray(jsonWriter, "IDs", value.IDs, Common.Writer.WriteInt); Common.Writer.PropertyDouble(jsonWriter, "JuvenileMass", value.JuvenileMass); Common.Writer.PropertyDouble(jsonWriter, "AdultMass", value.AdultMass); Common.Writer.PropertyDouble(jsonWriter, "IndividualBodyMass", value.IndividualBodyMass); Common.Writer.PropertyDouble(jsonWriter, "IndividualReproductivePotentialMass", value.IndividualReproductivePotentialMass); Common.Writer.PropertyDouble(jsonWriter, "MaximumAchievedBodyMass", value.MaximumAchievedBodyMass); Common.Writer.PropertyDouble(jsonWriter, "Abundance", value.Abundance); Common.Writer.PropertyBoolean(jsonWriter, "Merged", value.Merged); Common.Writer.PropertyDouble(jsonWriter, "ProportionTimeActive", value.ProportionTimeActive); Common.Writer.PropertyDouble(jsonWriter, "TrophicIndex", value.TrophicIndex); Common.Writer.PropertyDouble(jsonWriter, "LogOptimalPreyBodySizeRatio", value.LogOptimalPreyBodySizeRatio); jsonWriter.WriteEndObject(); }; Action<Newtonsoft.Json.JsonWriter, Madingley.Common.Stock> JsonAddPropertyStock = (jsonWriter, value) => { jsonWriter.WriteStartObject(); Common.Writer.PropertyInt(jsonWriter, "FunctionalGroupIndex", value.FunctionalGroupIndex); Common.Writer.PropertyDouble(jsonWriter, "IndividualBodyMass", value.IndividualBodyMass); Common.Writer.PropertyDouble(jsonWriter, "TotalBiomass", value.TotalBiomass); jsonWriter.WriteEndObject(); }; Action<Newtonsoft.Json.JsonWriter, Madingley.Common.GridCell> JsonAddPropertyGridCell = (jsonWriter, gridCell) => { jsonWriter.WriteStartObject(); Common.Writer.PropertyDouble(jsonWriter, "Latitude", gridCell.Latitude); Common.Writer.PropertyDouble(jsonWriter, "Longitude", gridCell.Longitude); Common.Writer.PropertyArray(jsonWriter, "Cohorts", gridCell.Cohorts, (JsonWriter, value) => Common.Writer.WriteArray(JsonWriter, value, JsonAddPropertyCohort)); Common.Writer.PropertyArray(jsonWriter, "Stocks", gridCell.Stocks, (JsonWriter, value) => Common.Writer.WriteArray(JsonWriter, value, JsonAddPropertyStock)); Common.Writer.PropertyKeyValuePairs(jsonWriter, "Environment", gridCell.Environment, (JsonWriter, key, val) => Common.Writer.PropertyInlineArray(JsonWriter, key, val, Common.Writer.WriteDouble)); jsonWriter.WriteEndObject(); }; using (var writer = new Newtonsoft.Json.JsonTextWriter(sw)) { writer.Formatting = Newtonsoft.Json.Formatting.Indented; writer.WriteStartObject(); Common.Writer.PropertyInt(writer, "TimestepsComplete", modelState.TimestepsComplete); Common.Writer.PropertyKeyValuePairs(writer, "GlobalDiagnosticVariables", modelState.GlobalDiagnosticVariables, Common.Writer.PropertyDouble); Common.Writer.PropertyArray(writer, "GridCells", modelState.GridCells, JsonAddPropertyGridCell); Common.Writer.PropertyLong(writer, "NextCohortID", modelState.NextCohortID); writer.WriteEndObject(); } }
public void EndTimestep( int currentTimestep, Madingley.Common.ModelState modelState) { this.CurrentTimeStep = (uint)currentTimestep; this.CurrentMonth = Utilities.GetCurrentMonth(this.CurrentTimeStep, _GlobalModelTimeStepUnit); this.GlobalDiagnosticVariables = new SortedList<string, double>(modelState.GlobalDiagnosticVariables); var gridCells = Converters.ConvertGridCells(modelState.GridCells); this.EcosystemModelGrid.SetGridCells(gridCells, this._CellList); }
/// <summary> /// Initializes the ecosystem model /// </summary> /// <param name="initialisation">An instance of the model initialisation class</param> /// <param name="outputFilesSuffix">The suffix to be applied to all outputs from this model run</param> /// <param name="simulation">The index of the simulation being run</param> /// <param name="modelState">Existing model state or null</param> public MadingleyModel( MadingleyModelInitialisation initialisation, string outputFilesSuffix, int simulation, Madingley.Common.ModelState modelState) { this.ModelInitialisation = initialisation; var scenarioIndex = 0; var globalModelTimeStepUnit = initialisation.GlobalModelTimeStepUnit; this.GlobalDiagnosticVariables = new SortedList<string, double>(modelState.GlobalDiagnosticVariables); var gridCells = Converters.ConvertGridCells(modelState.GridCells); // Assign the properties for this model run AssignModelRunProperties(initialisation, outputFilesSuffix);
public static Madingley.Common.ModelState Deserialize(TextReader sr) { Func<Newtonsoft.Json.JsonTextReader, Madingley.Common.Cohort> JsonReadCohort = (reader) => { var ret = new Madingley.Common.Cohort(); Debug.Assert(reader.TokenType == Newtonsoft.Json.JsonToken.StartObject); while (reader.Read() && reader.TokenType != Newtonsoft.Json.JsonToken.EndObject) { Debug.Assert(reader.TokenType == Newtonsoft.Json.JsonToken.PropertyName); Debug.Assert(reader.ValueType == typeof(string)); var property = Convert.ToString(reader.Value); reader.Read(); switch (property) { case "FunctionalGroupIndex": ret.FunctionalGroupIndex = Common.Reader.ReadInt(reader); break; case "BirthTimeStep": ret.BirthTimeStep = Common.Reader.ReadInt(reader); break; case "MaturityTimeStep": ret.MaturityTimeStep = Common.Reader.ReadInt(reader); break; case "IDs": ret.IDs = Common.Reader.ReadArray(reader, Common.Reader.ReadInt).ToList(); break; case "JuvenileMass": ret.JuvenileMass = Common.Reader.ReadDouble(reader); break; case "AdultMass": ret.AdultMass = Common.Reader.ReadDouble(reader); break; case "IndividualBodyMass": ret.IndividualBodyMass = Common.Reader.ReadDouble(reader); break; case "IndividualReproductivePotentialMass": ret.IndividualReproductivePotentialMass = Common.Reader.ReadDouble(reader); break; case "MaximumAchievedBodyMass": ret.MaximumAchievedBodyMass = Common.Reader.ReadDouble(reader); break; case "Abundance": ret.Abundance = Common.Reader.ReadDouble(reader); break; case "Merged": ret.Merged = Common.Reader.ReadBoolean(reader); break; case "ProportionTimeActive": ret.ProportionTimeActive = Common.Reader.ReadDouble(reader); break; case "TrophicIndex": ret.TrophicIndex = Common.Reader.ReadDouble(reader); break; case "LogOptimalPreyBodySizeRatio": ret.LogOptimalPreyBodySizeRatio = Common.Reader.ReadDouble(reader); break; default: throw new Exception(string.Format("Unexpected property: {0}", property)); } } return ret; }; Func<Newtonsoft.Json.JsonTextReader, Madingley.Common.Stock> JsonReadStock = (reader) => { var ret = new Madingley.Common.Stock(); Debug.Assert(reader.TokenType == Newtonsoft.Json.JsonToken.StartObject); while (reader.Read() && reader.TokenType != Newtonsoft.Json.JsonToken.EndObject) { Debug.Assert(reader.TokenType == Newtonsoft.Json.JsonToken.PropertyName); Debug.Assert(reader.ValueType == typeof(string)); var property = Convert.ToString(reader.Value); reader.Read(); switch (property) { case "FunctionalGroupIndex": ret.FunctionalGroupIndex = Common.Reader.ReadInt(reader); break; case "IndividualBodyMass": ret.IndividualBodyMass = Common.Reader.ReadDouble(reader); break; case "TotalBiomass": ret.TotalBiomass = Common.Reader.ReadDouble(reader); break; default: throw new Exception(string.Format("Unexpected property: {0}", property)); } } return ret; }; Func<Newtonsoft.Json.JsonTextReader, IDictionary<string, double[]>> JsonReadCellEnvironment = (reader) => { var ret = new Dictionary<string, double[]>(); Debug.Assert(reader.TokenType == Newtonsoft.Json.JsonToken.StartObject); while (reader.Read() && reader.TokenType != Newtonsoft.Json.JsonToken.EndObject) { Debug.Assert(reader.TokenType == Newtonsoft.Json.JsonToken.PropertyName); Debug.Assert(reader.ValueType == typeof(string)); var key = Convert.ToString(reader.Value); reader.Read(); var value = Common.Reader.ReadArray(reader, Common.Reader.ReadDouble); ret.Add(key, value.ToArray()); } return ret; }; Func<Newtonsoft.Json.JsonTextReader, Madingley.Common.GridCell> JsonReadGridCell = (reader) => { var ret = new Madingley.Common.GridCell(); Debug.Assert(reader.TokenType == Newtonsoft.Json.JsonToken.StartObject); while (reader.Read() && reader.TokenType != Newtonsoft.Json.JsonToken.EndObject) { Debug.Assert(reader.TokenType == Newtonsoft.Json.JsonToken.PropertyName); Debug.Assert(reader.ValueType == typeof(string)); var property = Convert.ToString(reader.Value); reader.Read(); switch (property) { case "Latitude": ret.Latitude = Common.Reader.ReadDouble(reader); break; case "Longitude": ret.Longitude = Common.Reader.ReadDouble(reader); break; case "Cohorts": ret.Cohorts = Common.Reader.ReadArray(reader, r => Common.Reader.ReadArray(r, JsonReadCohort)).ToList(); break; case "Stocks": ret.Stocks = Common.Reader.ReadArray(reader, r => Common.Reader.ReadArray(r, JsonReadStock)).ToList(); break; case "Environment": ret.Environment = JsonReadCellEnvironment(reader); break; default: throw new Exception(string.Format("Unexpected property: {0}", property)); } } return ret; }; var modelState = new Madingley.Common.ModelState(); using (var reader = new Newtonsoft.Json.JsonTextReader(sr)) { reader.Read(); Debug.Assert(reader.TokenType == Newtonsoft.Json.JsonToken.StartObject); while (reader.Read() && reader.TokenType != Newtonsoft.Json.JsonToken.EndObject) { Debug.Assert(reader.TokenType == Newtonsoft.Json.JsonToken.PropertyName); Debug.Assert(reader.ValueType == typeof(string)); var property = Convert.ToString(reader.Value); reader.Read(); switch (property) { case "TimestepsComplete": modelState.TimestepsComplete = Common.Reader.ReadInt(reader); break; case "GlobalDiagnosticVariables": modelState.GlobalDiagnosticVariables = Common.Reader.ReadKeyValuePairs(reader, Common.Reader.ReadDouble); break; case "GridCells": modelState.GridCells = Common.Reader.ReadArray(reader, JsonReadGridCell).ToList(); break; case "NextCohortID": modelState.NextCohortID = Common.Reader.ReadLong(reader); break; default: throw new Exception(string.Format("Unexpected property: {0}", property)); } } } return modelState; }
public void EndTimestep( int currentTimestep, Madingley.Common.ModelState modelState) { this.model.EndTimestep(currentTimestep, modelState); }
public static Madingley.Common.IOutput Create( Madingley.Common.RunState state, Madingley.Common.Configuration configuration, Madingley.Common.Environment environment, Madingley.Common.ModelState modelState) { // Specify the working directory string OutputDir = "Parameters"; OutputDir += System.DateTime.Now.Year + "-" + System.DateTime.Now.Month + "-" + System.DateTime.Now.Day + "_" + System.DateTime.Now.Hour + "." + System.DateTime.Now.Minute + "." + System.DateTime.Now.Second + "/"; // Create the working directory if this does not already exist System.IO.Directory.CreateDirectory(OutputDir); foreach (var sourceFileName in configuration.FileNames) { var fileName = System.IO.Path.GetFileName(sourceFileName); var destFileName = System.IO.Path.Combine(OutputDir, fileName); System.IO.File.Copy(sourceFileName, destFileName, true); } foreach (var sourceFileName in environment.FileNames) { var fileName = System.IO.Path.GetFileName(sourceFileName); var destFileName = System.IO.Path.Combine(OutputDir, fileName); System.IO.File.Copy(sourceFileName, destFileName, true); } // Set up the suffix for the output files var OutputFilesSuffix = "_"; // Add the scenario label to the suffix for the output files OutputFilesSuffix += configuration.ScenarioParameters[configuration.ScenarioIndex].Label + "_"; // Add the simulation index number to the suffix OutputFilesSuffix += configuration.Simulation.ToString(); var i = new MadingleyModelInitialisation(simulationInitialisationFile, definitionsFilename, outputsFilename, outputPath, inputPath); i.OutputPath = OutputDir; var output = new Madingley.Output.MadingleyModelOutput( OutputFilesSuffix, environment, configuration, i, modelState); if (state != null) { var existing = (Madingley.Output.MadingleyModelOutput)state.Output; output.model.Copy(existing.model); } return(output); }