/// <summary>
        /// Starts a model run or set of model runs
        /// </summary>
        static void Main()
        {
            // Write out model details to the console
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("Madingley model v. 0.3333333\n");
            Console.ForegroundColor = ConsoleColor.White;

            // Declare an instance of RunSimulations
            RunSimulations MakeSimulations = new RunSimulations();

            // Specify the working directory
            string OutputDir = "MadingleyOutputs";
            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);

            // Declare an instance of ScenarioParameterInitialisation to read in the parameters for this model run or set of runs
            ScenarioParameterInitialisation Scenarios = new ScenarioParameterInitialisation("Scenarios.csv", OutputDir);

            // Run the desired simulation or batch of simulations
            MakeSimulations.RunAllSimulations("SimulationControlParameters.csv", "FileLocationParameters.csv", "OutputControlParameters.csv",Scenarios, OutputDir);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Starts a model run or set of model runs
        /// </summary>
        static void Main()
        {
            // Write out model details to the console
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("Madingley model v. 0.3333333\n");
            Console.ForegroundColor = ConsoleColor.White;

            // Declare an instance of RunSimulations
            RunSimulations MakeSimulations = new RunSimulations();

            // 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);

            // Declare an instance of ScenarioParameterInitialisation to read in the parameters for this model run or set of runs
            ScenarioParameterInitialisation Scenarios = new ScenarioParameterInitialisation("Scenarios.csv", OutputDir);


            // Run the desired simulation or batch of simulations
            MakeSimulations.RunAllSimulations("SimulationControlParameters.csv", "FileLocationParameters.csv", "OutputControlParameters.csv", Scenarios, OutputDir);

            Console.ReadKey();
        }
Exemplo n.º 3
0
        /// <summary>
        /// Runs a single simulation of the Madingley model
        /// </summary>
        /// <param name="scenarios">Parameter information and simulation number for all scenarios to be run</param>
        /// <param name="scenarioIndex">The index of the scenario to be run in this simulation</param>
        /// <param name="initialiseMadingley">Model initialization information for all simulations</param>
        /// <param name="outputFileSuffix">Suffix to be applied to the names of files written out by this simulation</param>
        /// <param name="simulation">The index of the simulation being run</param>
        public void RunSimulation(ScenarioParameterInitialisation scenarios, int scenarioIndex, MadingleyModelInitialisation initialiseMadingley,
                                  string outputFileSuffix, int simulation)
        {
            // Declare an instance of the class that runs a Madingley model simulation
            MadingleyModel MadingleyEcosystemModel;

            // Declare and start a timer
            StopWatch s = new StopWatch();

            s.Start();
            StopWatch t = new StopWatch();

            t.Start();

            // Initialize the instance of MadingleyModel
            MadingleyEcosystemModel = new MadingleyModel(initialiseMadingley, scenarios, scenarioIndex, outputFileSuffix,
                                                         initialiseMadingley.GlobalModelTimeStepUnit, simulation);
            t.Stop();

            // Run the simulation
            MadingleyEcosystemModel.RunMadingley(initialiseMadingley);

            // Stop the timer and write out the time taken to run this simulation
            s.Stop();
            Console.WriteLine("Model run finished");
            Console.WriteLine("Total elapsed time was {0} seconds", s.GetElapsedTimeSecs());
            Console.WriteLine("Model setup time was {0} seconds", t.GetElapsedTimeSecs());
            Console.WriteLine("Model run time was {0} seconds", s.GetElapsedTimeSecs() - t.GetElapsedTimeSecs());
        }
Exemplo n.º 4
0
        /// <summary>
        /// Sets up the model grid within a Madingley model run
        /// </summary>
        /// <param name="initialisation">An instance of the model initialisation class</param> 
        /// <param name="scenarioParameters">The parameters for the scenarios to run</param>
        /// <param name="scenarioIndex">The index of the scenario that this model is to run</param>
        public void SetUpModelGrid(MadingleyModelInitialisation initialisation,
            ScenarioParameterInitialisation scenarioParameters, int scenarioIndex, int simulation)
#endif
        {
            // If the intialisation file contains a column pointing to another file of specific locations, and if this column is not blank then read the 
            // file indicated
            if (SpecificLocations)
            {
                // Set up the model grid using these locations
#if true
                EcosystemModelGrid = new ModelGrid(BottomLatitude, LeftmostLongitude, TopLatitude, RightmostLongitude,
                    CellSize, CellSize, _CellList, CohortFunctionalGroupDefinitions, StockFunctionalGroupDefinitions,
                    GlobalDiagnosticVariables, initialisation.TrackProcesses, SpecificLocations, RunGridCellsInParallel);
#else
                EcosystemModelGrid = new ModelGrid(BottomLatitude, LeftmostLongitude, TopLatitude, RightmostLongitude,
                    CellSize, CellSize, _CellList, EnviroStack, CohortFunctionalGroupDefinitions, StockFunctionalGroupDefinitions,
                    GlobalDiagnosticVariables, initialisation.TrackProcesses, SpecificLocations,RunGridCellsInParallel);
#endif

            }
            else
            {
                EcologyTimer = new StopWatch();
                EcologyTimer.Start();

                // Set up a full model grid (i.e. not for specific locations)
                // Set up the model grid using these locations
#if true
                EcosystemModelGrid = new ModelGrid(BottomLatitude, LeftmostLongitude, TopLatitude, RightmostLongitude,
                    CellSize, CellSize, _CellList, CohortFunctionalGroupDefinitions, StockFunctionalGroupDefinitions,
                    GlobalDiagnosticVariables, initialisation.TrackProcesses, SpecificLocations, RunGridCellsInParallel);
#else
                EcosystemModelGrid = new ModelGrid(BottomLatitude, LeftmostLongitude, TopLatitude, RightmostLongitude,
                    CellSize, CellSize, _CellList, EnviroStack, CohortFunctionalGroupDefinitions, StockFunctionalGroupDefinitions,
                    GlobalDiagnosticVariables, initialisation.TrackProcesses, SpecificLocations, RunGridCellsInParallel);
#endif

                EcologyTimer.Stop();
                Console.WriteLine("Time to initialise cells: {0}", EcologyTimer.GetElapsedTimeSecs());

                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("Madingley Model memory usage post grid cell seed: {0}", GC.GetTotalMemory(true) / 1E9, " (G Bytes)\n");
                Console.ForegroundColor = ConsoleColor.White;

            }

            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("Madingley Model memory usage pre Collect: {0}", Math.Round(GC.GetTotalMemory(true) / 1E9, 2), " (GBytes)");
            Console.ForegroundColor = ConsoleColor.White;
            GC.Collect();

            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("Madingley Model memory usage post Collect: {0}", Math.Round(GC.GetTotalMemory(true) / 1E9, 5), " (GBytes)\n");
            Console.ForegroundColor = ConsoleColor.White;

        }
Exemplo n.º 5
0
        /// <summary>
        /// Assigns the properties of the current model run
        /// </summary>
        /// <param name="initialisation">An instance of the model initialisation class</param> 
        /// <param name="scenarioParameters">The parameters for the scenarios to run</param>
        /// <param name="scenarioIndex">The index of the scenario that this model is to run</param>
        /// <param name="outputFilesSuffix">The suffix to be applied to all outputs from this model run</param>
        public void AssignModelRunProperties(MadingleyModelInitialisation initialisation, 
            ScenarioParameterInitialisation scenarioParameters, int scenarioIndex,
            string outputFilesSuffix)
#endif
        {
            // Assign the properties of this model run from the same properties in the specified model initialisation
            _GlobalModelTimeStepUnit = initialisation.GlobalModelTimeStepUnit;
            NumTimeSteps = initialisation.NumTimeSteps;
            CellSize = (float)initialisation.CellSize;
            _CellList = initialisation.CellList;
            BottomLatitude = initialisation.BottomLatitude;
            TopLatitude = initialisation.TopLatitude;
            LeftmostLongitude = initialisation.LeftmostLongitude;
            RightmostLongitude = initialisation.RightmostLongitude;
            InitialisationFileStrings = initialisation.InitialisationFileStrings;
            CohortFunctionalGroupDefinitions = initialisation.CohortFunctionalGroupDefinitions;
            StockFunctionalGroupDefinitions = initialisation.StockFunctionalGroupDefinitions;
            EnviroStack = initialisation.EnviroStack;
            OutputFilesSuffix = outputFilesSuffix;
            OutputModelStateTimestep = initialisation.OutputStateTimestep;
            SpecificLocations = initialisation.SpecificLocations;
        }
        /// <summary>
        /// Sets up the model grid within a Madingley model run
        /// </summary>
        /// <param name="initialisation">An instance of the model initialisation class</param> 
        /// <param name="scenarioParameters">The parameters for the scenarios to run</param>
        /// <param name="scenarioIndex">The index of the scenario that this model is to run</param>
        public void SetUpModelGrid(MadingleyModelInitialisation initialisation,
            ScenarioParameterInitialisation scenarioParameters, int scenarioIndex, int simulation)
        {
            // If the intialisation file contains a column pointing to another file of specific locations, and if this column is not blank then read the
            // file indicated
            if (SpecificLocations)
            {
                // Set up the model grid using these locations
                EcosystemModelGrid = new ModelGrid(BottomLatitude, LeftmostLongitude, TopLatitude, RightmostLongitude,
                    CellSize, CellSize, _CellList, EnviroStack, CohortFunctionalGroupDefinitions, StockFunctionalGroupDefinitions,
                    GlobalDiagnosticVariables, initialisation.TrackProcesses, SpecificLocations, RunGridCellsInParallel,GlobalModelTimeStepUnit);
            }
            else
            {
                _CellList = new List<uint[]>();
                //Switched order so we create cell list first then initialise cells using list rather than grid.

                uint NumLatCells = (uint)((TopLatitude - BottomLatitude) / CellSize);
                uint NumLonCells = (uint)((RightmostLongitude - LeftmostLongitude) / CellSize);

                // Loop over all cells in the model
                for (uint ii = 0; ii < NumLatCells; ii += 1)
                {
                    for (uint jj = 0; jj < NumLonCells; jj += 1)
                    {
                        // Define a vector to hold the pair of latitude and longitude indices for this grid cell
                        uint[] cellIndices = new uint[2];

                        // Add the latitude and longitude indices to this vector
                        cellIndices[0] = ii;
                        cellIndices[1] = jj;

                        // Add the vector to the list of all active grid cells
                        _CellList.Add(cellIndices);

                    }
                }

                EcologyTimer = new StopWatch();
                EcologyTimer.Start();

                // Set up a full model grid (i.e. not for specific locations)
                // Set up the model grid using these locations
                EcosystemModelGrid = new ModelGrid(BottomLatitude, LeftmostLongitude, TopLatitude, RightmostLongitude,
                    CellSize, CellSize, _CellList, EnviroStack, CohortFunctionalGroupDefinitions, StockFunctionalGroupDefinitions,
                    GlobalDiagnosticVariables, initialisation.TrackProcesses, SpecificLocations, RunGridCellsInParallel, GlobalModelTimeStepUnit);

                List<int> cellsToRemove = new List<int>();
                if (initialisation.RunRealm == "terrestrial")
                {
                    for (int ii = 0; ii < _CellList.Count; ii += 1)
                    {
                        if ((EcosystemModelGrid.GetCellEnvironment(_CellList[ii][0], _CellList[ii][1])["Realm"][0] == 2.0) ||
                            (EcosystemModelGrid.GetCellEnvironment(_CellList[ii][0], _CellList[ii][1])["LandSeaMask"][0] == 0.0))
                        {
                            cellsToRemove.Add(ii);
                        }
                    }
                }
                else if (initialisation.RunRealm == "marine")
                {
                    for (int ii = 0; ii < _CellList.Count; ii += 1)
                    {
                        if (EcosystemModelGrid.GetCellEnvironment(_CellList[ii][0], _CellList[ii][1])["Realm"][0] == 1.0)
                        {
                            cellsToRemove.Add(ii);
                        }
                    }
                }

                for (int ii = (cellsToRemove.Count - 1); ii >= 0; ii--)
                {
                    _CellList.RemoveAt(cellsToRemove[ii]);
                }

                EcologyTimer.Stop();
                Console.WriteLine("Time to initialise cells: {0}", EcologyTimer.GetElapsedTimeSecs());

                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("Madingley Model memory usage post grid cell seed: {0}", GC.GetTotalMemory(true) / 1E9, " (G Bytes)\n");
                Console.ForegroundColor = ConsoleColor.White;

            }

            if (initialisation.InputState)
            {
                InputModelState = new InputModelState(initialisation.ModelStatePath[simulation],
                    initialisation.ModelStateFilename[simulation],EcosystemModelGrid, _CellList);
            }

            // When the last simulation for the current scenario
            // if ((scenarioParameters.scenarioSimulationsNumber.Count == 1) && (scenarioIndex == scenarioParameters.scenarioSimulationsNumber[scenarioIndex] - 1)) EnviroStack.Clear();
            // Seed stocks and cohorts in the grid cells
            // If input state from output from a previous simulation
            if (initialisation.InputState)
            {
                // Seed grid cell cohort and stocks
                EcosystemModelGrid.SeedGridCellStocksAndCohorts(_CellList, InputModelState, CohortFunctionalGroupDefinitions, StockFunctionalGroupDefinitions);

                //remove cohorts that do not contain any biomass
                foreach (uint[] CellPair in _CellList)
                {

                    GridCellCohortHandler workingGridCellCohorts = EcosystemModelGrid.GetGridCellCohorts(CellPair[0], CellPair[1]);

                    for (int kk = 0; kk < CohortFunctionalGroupDefinitions.GetNumberOfFunctionalGroups(); kk++)
                    {
                        // Loop through each cohort in the functional group
                        for (int ll = (workingGridCellCohorts[kk].Count - 1); ll >= 0; ll--)
                        {
                            // If cohort abundance is less than the extinction threshold then add to the list for extinction
                            if (workingGridCellCohorts[kk][ll].CohortAbundance.CompareTo(0) <= 0 || workingGridCellCohorts[kk][ll].IndividualBodyMass.CompareTo(0.0) == 0)
                            {
                                // Remove the extinct cohort from the list of cohorts
                                workingGridCellCohorts[kk].RemoveAt(ll);
                            }
                        }

                    }
                }

            }
            else
            {
                EcosystemModelGrid.SeedGridCellStocksAndCohorts(_CellList, CohortFunctionalGroupDefinitions, StockFunctionalGroupDefinitions,
                    GlobalDiagnosticVariables, ref NextCohortID, InitialisationFileStrings["OutputDetail"] == "high", DrawRandomly,
                    initialisation.DispersalOnly, InitialisationFileStrings["DispersalOnlyType"], RunGridCellsInParallel);
            }

            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("Madingley Model memory usage pre Collect: {0}", Math.Round(GC.GetTotalMemory(true) / 1E9, 2), " (GBytes)");
            Console.ForegroundColor = ConsoleColor.White;
            GC.Collect();

            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("Madingley Model memory usage post Collect: {0}", Math.Round(GC.GetTotalMemory(true) / 1E9, 5), " (GBytes)\n");
            Console.ForegroundColor = ConsoleColor.White;
        }
        /// <summary>
        /// Assigns the properties of the current model run
        /// </summary>
        /// <param name="initialisation">An instance of the model initialisation class</param> 
        /// <param name="scenarioParameters">The parameters for the scenarios to run</param>
        /// <param name="scenarioIndex">The index of the scenario that this model is to run</param>
        /// <param name="outputFilesSuffix">The suffix to be applied to all outputs from this model run</param>
        public void AssignModelRunProperties(MadingleyModelInitialisation initialisation, 
            ScenarioParameterInitialisation scenarioParameters, int scenarioIndex,
            string outputFilesSuffix)
        {
            // Assign the properties of this model run from the same properties in the specified model initialisation
            _GlobalModelTimeStepUnit = initialisation.GlobalModelTimeStepUnit;
            NumTimeSteps = initialisation.NumTimeSteps;
            NumBurninSteps = initialisation.BurninTimeSteps;
            NumImpactSteps = initialisation.ImpactTimeSteps;
            NumRecoverySteps = initialisation.RecoveryTimeSteps;
            CellSize = (float)initialisation.CellSize;
            _CellList = initialisation.CellList;
            BottomLatitude = initialisation.BottomLatitude;
            TopLatitude = initialisation.TopLatitude;
            LeftmostLongitude = initialisation.LeftmostLongitude;
            RightmostLongitude = initialisation.RightmostLongitude;
            RunGridCellsInParallel = initialisation.RunCellsInParallel;
            DrawRandomly = initialisation.DrawRandomly;
            _ExtinctionThreshold = initialisation.ExtinctionThreshold;
            MergeDifference = initialisation.MergeDifference;
            InitialisationFileStrings = initialisation.InitialisationFileStrings;
            CohortFunctionalGroupDefinitions = initialisation.CohortFunctionalGroupDefinitions;
            StockFunctionalGroupDefinitions = initialisation.StockFunctionalGroupDefinitions;
            EnviroStack = initialisation.EnviroStack;
            _HumanNPPScenario = scenarioParameters.scenarioParameters.ElementAt(scenarioIndex).Item3["npp"];
            _TemperatureScenario = scenarioParameters.scenarioParameters.ElementAt(scenarioIndex).Item3["temperature"];
            _HarvestingScenario = scenarioParameters.scenarioParameters.ElementAt(scenarioIndex).Item3["harvesting"];
            OutputFilesSuffix = outputFilesSuffix;
            EnvironmentalDataUnits = initialisation.Units;
            OutputModelStateTimestep = initialisation.OutputStateTimestep;
            SpecificLocations = initialisation.SpecificLocations;

            // Initialise the cohort ID to zero
            NextCohortID = 0;
        }
        /// <summary>
        /// Initializes the ecosystem model
        /// </summary>
        /// <param name="initialisation">An instance of the model initialisation class</param> 
        /// <param name="scenarioParameters">The parameters for the scenarios to run</param>
        /// <param name="scenarioIndex">The index of the scenario being run</param>
        /// <param name="outputFilesSuffix">The suffix to be applied to all outputs from this model run</param>
        /// <param name="globalModelTimeStepUnit">The time step unit used in the model</param>
        /// <param name="simulation">The index of the simulation being run</param>
        public MadingleyModel(MadingleyModelInitialisation initialisation, ScenarioParameterInitialisation scenarioParameters, int scenarioIndex,
            string outputFilesSuffix, string globalModelTimeStepUnit, int simulation)
        {
            // Assign the properties for this model run
            AssignModelRunProperties(initialisation, scenarioParameters, scenarioIndex, outputFilesSuffix);

            // Set up list of global diagnostics
            SetUpGlobalDiagnosticsList();

            // Set up the model grid
            SetUpModelGrid(initialisation, scenarioParameters, scenarioIndex, simulation);

            // Set up model outputs
            SetUpOutputs(initialisation, simulation, scenarioIndex);

            // Make the initial outputs
            InitialOutputs(outputFilesSuffix, initialisation, CurrentMonth);

            // Instance the array of process trackers
            ProcessTrackers = new ProcessTracker[_CellList.Count];

            // Temporary variables
            Boolean varExists;

            // Set up process trackers for each grid cell
            for (int i = 0; i < _CellList.Count; i++)
            {
                ProcessTrackers[i] = new ProcessTracker(NumTimeSteps,
                EcosystemModelGrid.Lats, EcosystemModelGrid.Lons,
                _CellList,
                initialisation.ProcessTrackingOutputs,
                initialisation.TrackProcesses,
                CohortFunctionalGroupDefinitions,
                EcosystemModelGrid.GlobalMissingValue,
                outputFilesSuffix,
                initialisation.OutputPath, initialisation.ModelMassBins,
                SpecificLocations, i, initialisation,
                EcosystemModelGrid.GetEnviroLayer("Realm", 0, _CellList[i][0], _CellList[i][1], out varExists) == 2.0,
                EcosystemModelGrid.LatCellSize,
                EcosystemModelGrid.LonCellSize);
            }

            // Set up a cross cell process tracker
            TrackCrossCellProcesses = new CrossCellProcessTracker(initialisation.TrackCrossCellProcesses, "DispersalData", initialisation.OutputPath, outputFilesSuffix);

            //Set up a global process tracker
            if (SpecificLocations) initialisation.TrackGlobalProcesses = false;

            TrackGlobalProcesses = new GlobalProcessTracker(NumTimeSteps,
                EcosystemModelGrid.Lats, EcosystemModelGrid.Lons,
                _CellList,
                initialisation.ProcessTrackingOutputs,
                initialisation.TrackGlobalProcesses,
                CohortFunctionalGroupDefinitions,
                StockFunctionalGroupDefinitions,
                EcosystemModelGrid.GlobalMissingValue,
                outputFilesSuffix,
                initialisation.OutputPath, initialisation.ModelMassBins,
                SpecificLocations, initialisation,
                EcosystemModelGrid.LatCellSize,
                EcosystemModelGrid.LonCellSize);

            //Set-up the instance of OutputModelState
            WriteModelState = new OutputModelState(initialisation, outputFilesSuffix, simulation);

            if (SpecificLocations) initialisation.RunRealm = "";

            // Record the initial cohorts in the process trackers
            RecordInitialCohorts();

            // Initialise the class for cross-grid-cell ecology
            MadingleyEcologyCrossGridCell = new EcologyCrossGridCell();

            // Initialise the time step timer
            TimeStepTimer = new StopWatch();
            EcologyTimer = new StopWatch();
            OutputTimer = new StopWatch();

            // Set the global model time step unit
            _GlobalModelTimeStepUnit = globalModelTimeStepUnit;

            // Initialise the utility functions
            Utilities = new UtilityFunctions();

            // Initialise the climate change impacts class
            ClimateChangeSimulator = new ClimateChange();

            // Initialise the harvesting impacts class
            HarvestingSimulator = new Harvesting(EcosystemModelGrid.Lats, EcosystemModelGrid.Lons, (float)EcosystemModelGrid.LatCellSize);
        }
Exemplo n.º 9
0
        /// <summary>
        /// Runs the specified number of simulations for each of the specified scenarios
        /// </summary>
        /// <param name="simulationInitialisationFilename">Filename of the file from which to read initialisation information</param>
        /// <param name="scenarios">Contains scenario information for this set of simulations</param>
        /// <param name="outputPath">The path to which outputs should be written</param>
        public void RunAllSimulations(string simulationInitialisationFilename, string definitionsFilename, string outputsFilename, ScenarioParameterInitialisation scenarios, string outputPath)
        {
            // Declare an instance of the class for initializing the Madingley model
            MadingleyModelInitialisation InitialiseMadingley = new MadingleyModelInitialisation(simulationInitialisationFilename, definitionsFilename, outputsFilename, outputPath);

            // Specify the output path in this instance
            InitialiseMadingley.OutputPath = outputPath;

            // List to hold the names of the scenarios to run
            List <string> ScenarioNames = new List <string>();
            // String variable to hold the index suffix to apply to output files for a given simulation
            string OutputFilesSuffix;

            // Loop over scenario names and add the name of the scenario to the list of scenarion names
            foreach (var scenario in scenarios.scenarioParameters)
            {
                ScenarioNames.Add(scenario.Item1);
            }

            // Check whether there is only one simulation to run
            if (scenarios.scenarioNumber == 1 && scenarios.scenarioParameters.ElementAt(scenarios.scenarioNumber - 1).Item2 == 1)
            {
                // For a single simulation

                // Set-up the suffix for the output files
                OutputFilesSuffix = "_";

                // Loop over the parameters for this scenario
                for (int i = 0; i < ScenarioNames.Count; i++)
                {
                    // Add the parameter information to the suffix for this simulation
                    OutputFilesSuffix += ScenarioNames[0] + "_";
                }
                // Add a zero index to the end of the suffix
                OutputFilesSuffix += "0";

                //Run the simulation
                RunSimulation(scenarios, 0, InitialiseMadingley, OutputFilesSuffix, 0);
            }
            else
            {
                if (InitialiseMadingley.RunSimulationsInParallel)
                {
                    // Loop over specified scenarios iteratively
                    for (int ScenarioIndex = 0; ScenarioIndex < scenarios.scenarioNumber; ScenarioIndex++)
                    {
                        //Create an array of new MadingleyModel instances for simulations under this scenario combination
                        MadingleyModel[] MadingleyEcosystemModels = new MadingleyModel
                                                                    [scenarios.scenarioParameters.ElementAt(ScenarioIndex).Item2];

                        for (int simulation = 0; simulation < scenarios.scenarioParameters.ElementAt(ScenarioIndex).Item2; simulation++)
                        {
                            // Set up the suffix for the output files
                            OutputFilesSuffix = "_";

                            // Add the scenario label to the suffix for the output files
                            OutputFilesSuffix += ScenarioNames[ScenarioIndex] + "_";

                            // Add the simulation index number to the suffix
                            OutputFilesSuffix += simulation.ToString();

                            // Initialize the instance of MadingleyModel
                            MadingleyEcosystemModels[simulation] = new MadingleyModel(InitialiseMadingley, scenarios, ScenarioIndex, OutputFilesSuffix,
                                                                                      InitialiseMadingley.GlobalModelTimeStepUnit, simulation);
                        }

                        // Loop over the specified number of simulations for each scenario
                        //for (int simulation = 0; simulation<  scenarios.scenarioSimulationsNumber[ScenarioIndex]; simulation++)
                        Parallel.For(0, scenarios.scenarioParameters.ElementAt(ScenarioIndex).Item2, simulation =>
                        {
                            // Declare and start a timer
                            StopWatch s = new StopWatch();
                            s.Start();

                            // Run the simulation
                            MadingleyEcosystemModels[simulation].RunMadingley(InitialiseMadingley);

                            // Stop the timer and write out the time taken to run this simulation
                            s.Stop();
                            Console.WriteLine("Model run finished");
                            Console.WriteLine("Total elapsed time was {0} seconds", s.GetElapsedTimeSecs());
                        });
                    }
                }
                else
                {
                    //Run simulations sequentially

                    // Loop over specified scenarios
                    for (int ScenarioIndex = 0; ScenarioIndex < scenarios.scenarioNumber; ScenarioIndex++)
                    {
                        // Loop over the specified number of simulations for each scenario
                        for (int simulation = 0; simulation < scenarios.scenarioParameters.ElementAt(ScenarioIndex).Item2; simulation++)
                        {
                            // Set up the suffix for the output files
                            OutputFilesSuffix = "_";

                            // Add the scenario label to the suffix for the output files
                            OutputFilesSuffix += ScenarioNames[ScenarioIndex] + "_";

                            // Add the simulation index number to the suffix
                            OutputFilesSuffix += simulation.ToString();

                            // Run the current simulation
                            RunSimulation(scenarios, ScenarioIndex, InitialiseMadingley, OutputFilesSuffix, simulation);
                        }
                    }
                }
            }
        }
        /// <summary>
        /// Runs the specified number of simulations for each of the specified scenarios
        /// </summary>
        /// <param name="simulationInitialisationFilename">Filename of the file from which to read initialisation information</param>
        /// <param name="scenarios">Contains scenario information for this set of simulations</param>
        /// <param name="outputPath">The path to which outputs should be written</param>
        public void RunAllSimulations(string simulationInitialisationFilename, string definitionsFilename, string outputsFilename, ScenarioParameterInitialisation scenarios, string outputPath)
        {
            // Declare an instance of the class for initializing the Madingley model
            MadingleyModelInitialisation InitialiseMadingley = new MadingleyModelInitialisation(simulationInitialisationFilename, definitionsFilename, outputsFilename, outputPath);
            // Specify the output path in this instance
            InitialiseMadingley.OutputPath = outputPath;

            // List to hold the names of the scenarios to run
            List<string> ScenarioNames = new List<string>();
            // String variable to hold the index suffix to apply to output files for a given simulation
            string OutputFilesSuffix;

            // Loop over scenario names and add the name of the scenario to the list of scenarion names
            foreach (var scenario in scenarios.scenarioParameters)
            {
                ScenarioNames.Add(scenario.Item1);
            }
            
            // Check whether there is only one simulation to run
            if (scenarios.scenarioNumber == 1 && scenarios.scenarioParameters.ElementAt(scenarios.scenarioNumber-1).Item2==1)
            {
                // For a single simulation

                // Set-up the suffix for the output files
                OutputFilesSuffix = "_";
                
                // Loop over the parameters for this scenario
                for (int i = 0; i < ScenarioNames.Count; i++)
                {
                    // Add the parameter information to the suffix for this simulation
                    OutputFilesSuffix +=  ScenarioNames[0] + "_";
                }
                // Add a zero index to the end of the suffix
                OutputFilesSuffix += "0";

                //Run the simulation
                RunSimulation(scenarios, 0, InitialiseMadingley, OutputFilesSuffix, 0);

                Console.ReadKey();
            }
            else
            {

                if (InitialiseMadingley.RunSimulationsInParallel)
                {
                    // Loop over specified scenarios iteratively
                    for (int ScenarioIndex = 0; ScenarioIndex < scenarios.scenarioNumber; ScenarioIndex++)
                    {
                        //Create an array of new MadingleyModel instances for simulations under this scenario combination
                        MadingleyModel[] MadingleyEcosystemModels = new MadingleyModel
                            [scenarios.scenarioParameters.ElementAt(ScenarioIndex).Item2];

                        for (int simulation = 0; simulation < scenarios.scenarioParameters.ElementAt(ScenarioIndex).Item2; simulation++)
                        {
                            // Set up the suffix for the output files
                            OutputFilesSuffix = "_";

                            // Add the scenario label to the suffix for the output files
                            OutputFilesSuffix += ScenarioNames[ScenarioIndex] + "_";

                            // Add the simulation index number to the suffix
                            OutputFilesSuffix += simulation.ToString();

                            // Initialize the instance of MadingleyModel
                            MadingleyEcosystemModels[simulation] = new MadingleyModel(InitialiseMadingley, scenarios, ScenarioIndex, OutputFilesSuffix,
                                InitialiseMadingley.GlobalModelTimeStepUnit, simulation);
                        }

                        // Loop over the specified number of simulations for each scenario
                        //for (int simulation = 0; simulation<  scenarios.scenarioSimulationsNumber[ScenarioIndex]; simulation++)
                        Parallel.For(0, scenarios.scenarioParameters.ElementAt(ScenarioIndex).Item2, simulation =>
                        {
                            // Declare and start a timer
                            StopWatch s = new StopWatch();
                            s.Start();

                            // Run the simulation
                            MadingleyEcosystemModels[simulation].RunMadingley(InitialiseMadingley);

                            // Stop the timer and write out the time taken to run this simulation
                            s.Stop();
                            Console.WriteLine("Model run finished");
                            Console.WriteLine("Total elapsed time was {0} seconds", s.GetElapsedTimeSecs());

                        });
                    }
                }
                else
                {
                    //Run simulations sequentially

                    // Loop over specified scenarios
                    for (int ScenarioIndex = 0; ScenarioIndex < scenarios.scenarioNumber; ScenarioIndex++)
                    {
                        // Loop over the specified number of simulations for each scenario
                        for (int simulation = 0; simulation < scenarios.scenarioParameters.ElementAt(ScenarioIndex).Item2; simulation++)
                        {
                            // Set up the suffix for the output files
                            OutputFilesSuffix = "_";

                            // Add the scenario label to the suffix for the output files
                            OutputFilesSuffix += ScenarioNames[ScenarioIndex] + "_";

                            // Add the simulation index number to the suffix
                            OutputFilesSuffix += simulation.ToString();

                            // Run the current simulation
                            RunSimulation(scenarios, ScenarioIndex, InitialiseMadingley, OutputFilesSuffix, simulation);
                        }
                    }
                }
               
            }

        }
        /// <summary>
        /// Runs a single simulation of the Madingley model
        /// </summary>
        /// <param name="scenarios">Parameter information and simulation number for all scenarios to be run</param>
        /// <param name="scenarioIndex">The index of the scenario to be run in this simulation</param>
        /// <param name="initialiseMadingley">Model initialization information for all simulations</param>
        /// <param name="outputFileSuffix">Suffix to be applied to the names of files written out by this simulation</param>
        /// <param name="simulation">The index of the simulation being run</param>
        public void RunSimulation(ScenarioParameterInitialisation scenarios, int scenarioIndex, MadingleyModelInitialisation initialiseMadingley, 
            string outputFileSuffix, int simulation)
        {
            // Declare an instance of the class that runs a Madingley model simulation
            MadingleyModel MadingleyEcosystemModel;
            
            // Declare and start a timer
            StopWatch s = new StopWatch();
            s.Start();
            StopWatch t = new StopWatch();
            t.Start();

            // Initialize the instance of MadingleyModel
            MadingleyEcosystemModel = new MadingleyModel(initialiseMadingley, scenarios, scenarioIndex, outputFileSuffix, 
                initialiseMadingley.GlobalModelTimeStepUnit,simulation);
            t.Stop();

            // Run the simulation
            MadingleyEcosystemModel.RunMadingley(initialiseMadingley);

            // Stop the timer and write out the time taken to run this simulation
            s.Stop();
            Console.WriteLine("Model run finished");
            Console.WriteLine("Total elapsed time was {0} seconds", s.GetElapsedTimeSecs());
            Console.WriteLine("Model setup time was {0} seconds", t.GetElapsedTimeSecs());
            Console.WriteLine("Model run time was {0} seconds", s.GetElapsedTimeSecs() - t.GetElapsedTimeSecs());
        }
Exemplo n.º 12
0
        /// <summary>
        /// Initializes the ecosystem model
        /// </summary>
        /// <param name="initialisation">An instance of the model initialisation class</param> 
        /// <param name="scenarioParameters">The parameters for the scenarios to run</param>
        /// <param name="scenarioIndex">The index of the scenario being run</param>
        /// <param name="outputFilesSuffix">The suffix to be applied to all outputs from this model run</param>
        /// <param name="globalModelTimeStepUnit">The time step unit used in the model</param>
        /// <param name="simulation">The index of the simulation being run</param>
        public MadingleyModel(MadingleyModelInitialisation initialisation, ScenarioParameterInitialisation scenarioParameters, int scenarioIndex,
            string outputFilesSuffix, string globalModelTimeStepUnit, int simulation)
        {         
            // Assign the properties for this model run
            AssignModelRunProperties(initialisation, scenarioParameters, scenarioIndex, outputFilesSuffix);
#endif

            // Set up the model grid
#if true
            this._CellList = initialisation.CellList.ToList();
            SetUpModelGrid(initialisation);
            EcosystemModelGrid.SetGridCells(gridCells, this._CellList);
#else
            SetUpModelGrid(initialisation, scenarioParameters, scenarioIndex, simulation);
#endif

            // Set up model outputs
            SetUpOutputs(initialisation, simulation, scenarioIndex);

            // Make the initial outputs
            InitialOutputs(outputFilesSuffix, initialisation, CurrentMonth);

            // Instance the array of process trackers
            ProcessTrackers = new ProcessTracker[_CellList.Count];

            // Temporary variables
            Boolean varExists;

            // Set up process trackers for each grid cell
            for (int i = 0; i < _CellList.Count; i++)
            {
                ProcessTrackers[i] = new ProcessTracker(NumTimeSteps,
                EcosystemModelGrid.Lats, EcosystemModelGrid.Lons,
                _CellList,
                initialisation.ProcessTrackingOutputs,
                initialisation.TrackProcesses,
                CohortFunctionalGroupDefinitions,
                EcosystemModelGrid.GlobalMissingValue,
                outputFilesSuffix,
                initialisation.OutputPath, initialisation.ModelMassBins,
                SpecificLocations, i, initialisation,
                EcosystemModelGrid.GetEnviroLayer("Realm", 0, _CellList[i][0], _CellList[i][1], out varExists) == 2.0,
                EcosystemModelGrid.LatCellSize,
                EcosystemModelGrid.LonCellSize);
            }

            // Set up a cross cell process tracker
            TrackCrossCellProcesses = new CrossCellProcessTracker(initialisation.TrackCrossCellProcesses, "DispersalData", initialisation.OutputPath, outputFilesSuffix);

            //Set up a global process tracker
            if (SpecificLocations) initialisation.TrackGlobalProcesses = false;

            TrackGlobalProcesses = new GlobalProcessTracker(NumTimeSteps,
                EcosystemModelGrid.Lats, EcosystemModelGrid.Lons,
                _CellList,
                initialisation.ProcessTrackingOutputs,
                initialisation.TrackGlobalProcesses,
                CohortFunctionalGroupDefinitions,
                StockFunctionalGroupDefinitions,
                EcosystemModelGrid.GlobalMissingValue,
                outputFilesSuffix,
                initialisation.OutputPath, initialisation.ModelMassBins,
                SpecificLocations, initialisation,
                EcosystemModelGrid.LatCellSize,
                EcosystemModelGrid.LonCellSize);

            //Set-up the instance of OutputModelState
            WriteModelState = new OutputModelState(initialisation, outputFilesSuffix, simulation);

            if (SpecificLocations) initialisation.RunRealm = "";

            // Record the initial cohorts in the process trackers
            RecordInitialCohorts();

            // Initialise the time step timer
            TimeStepTimer = new StopWatch();
            EcologyTimer = new StopWatch();
            OutputTimer = new StopWatch();

            // Set the global model time step unit
            _GlobalModelTimeStepUnit = globalModelTimeStepUnit;
        }