/// <summary>
        /// Constructor for dispersal: assigns all parameter values
        /// </summary>
        public AdvectiveDispersal(string globalModelTimeStepUnit, Boolean DrawRandomly)
        {
            InitialiseParatemersAdvectiveDispersal();

            // Calculate the scalar to convert from the time step units used by this implementation of dispersal to the global model time step units
            _DeltaT = Utilities.ConvertTimeUnits(globalModelTimeStepUnit, _TimeUnitImplementation);

            // Initialise the advective dispersal temporal scaling to adjust between time steps appropriately
            _AdvectionTimeStepsPerModelTimeStep = Utilities.ConvertTimeUnits(globalModelTimeStepUnit, "day") * 24 / _AdvectiveModelTimeStepLengthHours;

            // Convert velocity from m/s to km/month. Note that if the _TimeUnitImplementation changes, this will also have to change.
            VelocityUnitConversion = 60 * 60 * 24 * Utilities.ConvertTimeUnits(globalModelTimeStepUnit, "day") * _DeltaT / 1000;

            // Set the seed for the random number generator
            RandomNumberGenerator = new NonStaticSimpleRNG();
            if (DrawRandomly)
            {
                RandomNumberGenerator.SetSeedFromSystemTime();
            }
            else
            {
                RandomNumberGenerator.SetSeed(14141);
            }

        }
Ejemplo n.º 2
0
        /// <summary>
        /// Constructor for dispersal: assigns all parameter values
        /// </summary>
        public AdvectiveDispersal(string globalModelTimeStepUnit, Boolean DrawRandomly)
        {
            InitialiseParatemersAdvectiveDispersal();

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

            // Calculate the scalar to convert from the time step units used by this implementation of dispersal to the global model time step units
            _DeltaT = Utilities.ConvertTimeUnits(globalModelTimeStepUnit, _TimeUnitImplementation);

            // Initialise the advective dispersal temporal scaling to adjust between time steps appropriately
            _AdvectionTimeStepsPerModelTimeStep = Utilities.ConvertTimeUnits(globalModelTimeStepUnit, "day") * 24 / _AdvectiveModelTimeStepLengthHours;

            // Convert velocity from m/s to km/month. Note that if the _TimeUnitImplementation changes, this will also have to change.
            VelocityUnitConversion = 60 * 60 * 24 * Utilities.ConvertTimeUnits(globalModelTimeStepUnit, "day") * _DeltaT / 1000;

            // Set the seed for the random number generator
            RandomNumberGenerator = new NonStaticSimpleRNG();
            if (DrawRandomly)
            {
                RandomNumberGenerator.SetSeedFromSystemTime();
            }
            else
            {
                RandomNumberGenerator.SetSeed(14141);
            }
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Constructor for CohortMerge: sets the seed for the random number generator
 /// </summary>
 /// <param name="DrawRandomly"></param>
 public CohortMerge(Boolean DrawRandomly)
 {
     // Seed the random number generator
     // Set the seed for the random number generator
     RandomNumberGenerator = new NonStaticSimpleRNG();
     if (DrawRandomly)
     {
         RandomNumberGenerator.SetSeedFromSystemTime();
     }
     else
     {
         RandomNumberGenerator.SetSeed(4000);
     }
 }
Ejemplo n.º 4
0
 /// <summary>
 /// Constructor for CohortMerge: sets the seed for the random number generator
 /// </summary>
 /// <param name="DrawRandomly"></param>
 public CohortMerge(Boolean DrawRandomly)
 {
     // Seed the random number generator
     // Set the seed for the random number generator
     RandomNumberGenerator = new NonStaticSimpleRNG();
     if (DrawRandomly)
     {
         RandomNumberGenerator.SetSeedFromSystemTime();
     }
     else
     {
         RandomNumberGenerator.SetSeed(4000);
     }
 }
        /// <summary>
        /// Assigns all parameter values for repsonsive dispersal
        /// </summary>
        public ResponsiveDispersal(string globalModelTimeStepUnit, Boolean DrawRandomly)
        {
            InitialiseParametersResponsiveDispersal();

            // Calculate the scalar to convert from the time step units used by this implementation of dispersal to the global model time step units
            _DeltaT = Utilities.ConvertTimeUnits(globalModelTimeStepUnit, _TimeUnitImplementation);

            // Set the seed for the random number generator
            RandomNumberGenerator = new NonStaticSimpleRNG();
            if (DrawRandomly)
            {
                RandomNumberGenerator.SetSeedFromSystemTime();
            }
            else
            {
                RandomNumberGenerator.SetSeed(14141);
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Assigns all parameter values for repsonsive dispersal
        /// </summary>
        public ResponsiveDispersal(string globalModelTimeStepUnit, Boolean DrawRandomly)
        {
            InitialiseParametersResponsiveDispersal();

            // Calculate the scalar to convert from the time step units used by this implementation of dispersal to the global model time step units
            _DeltaT = Utilities.ConvertTimeUnits(globalModelTimeStepUnit, _TimeUnitImplementation);

            // Set the seed for the random number generator
            RandomNumberGenerator = new NonStaticSimpleRNG();
            if (DrawRandomly)
            {
                RandomNumberGenerator.SetSeedFromSystemTime();
            }
            else
            {
                RandomNumberGenerator.SetSeed(14141);
            }
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Constructor for reproduction: assigns all parameter values
        /// <param name="globalModelTimeStepUnit">The time step of the global model</param>
        /// <param name="drawRandomly">Indicates whether to draw values randomly</param>
        /// </summary>
        public ReproductionBasic(string globalModelTimeStepUnit, Boolean drawRandomly)
        {
            // Initialise ecological parameters for reproduction
            InitialiseReproductionParameters();

            // Calculate the scalar to convert from the time step units used by this implementation of herbivory to the global model time step units
            _DeltaT = Utilities.ConvertTimeUnits(globalModelTimeStepUnit, _TimeUnitImplementation);

            // Instantiate the random number generator
            RandomNumberGenerator = new NonStaticSimpleRNG();

            // Set the seed for the random number generator
            if (drawRandomly)
            {
                RandomNumberGenerator.SetSeedFromSystemTime();
            }
            else
            {
                RandomNumberGenerator.SetSeed(4000);
            }
        }
        /// <summary>
        /// Constructor for reproduction: assigns all parameter values
        /// <param name="globalModelTimeStepUnit">The time step of the global model</param>
        /// <param name="drawRandomly">Indicates whether to draw values randomly</param>
        /// </summary>
        public ReproductionBasic(string globalModelTimeStepUnit, Boolean drawRandomly)
        {

            // Initialise ecological parameters for reproduction
            InitialiseReproductionParameters();

            // Calculate the scalar to convert from the time step units used by this implementation of herbivory to the global model time step units
            _DeltaT = Utilities.ConvertTimeUnits(globalModelTimeStepUnit, _TimeUnitImplementation);

            // Instantiate the random number generator
            RandomNumberGenerator = new NonStaticSimpleRNG();

            // Set the seed for the random number generator
            if (drawRandomly)
            {
                RandomNumberGenerator.SetSeedFromSystemTime();
            }
            else
            {
                RandomNumberGenerator.SetSeed(4000);
            }
        }
        /// <summary>
        /// Constructor for model grid: assigns grid properties and initialises the grid cells
        /// </summary>
        /// <param name="minLat">Minimum grid latitude (degrees)</param>
        /// <param name="minLon">Minimum grid longitude (degrees, currently -180 to 180)</param>
        /// <param name="maxLat">Maximum grid latitude (degrees)</param>
        /// <param name="maxLon">Maximum grid longitude (degrees, currently -180 to 180)</param>
        /// <param name="latCellSize">Latitudinal resolution of grid cell</param>
        /// <param name="lonCellSize">Longitudinal resolution of grid cell</param>
        /// <param name="cellRarefaction">The rarefaction to be applied to active grid cells in the model</param>
        /// <param name="enviroStack">Environmental data layers</param>
        /// <param name="cohortFunctionalGroups">The functional group definitions for cohorts in the model</param>
        /// <param name="stockFunctionalGroups">The functional group definitions for stocks in the model</param>
        /// <param name="globalDiagnostics">Global daignostic variables</param>
        /// <param name="tracking">Whether process-tracking is enabled</param>
        /// <param name="DrawRandomly">Whether the model is set to use a random draw</param>
        /// <param name="specificLocations">Whether the model is to be run for specific locations</param>
        public ModelGrid(float minLat, float minLon,float maxLat,float maxLon,float latCellSize,float lonCellSize, 
            SortedList<string,EnviroData> enviroStack, FunctionalGroupDefinitions cohortFunctionalGroups, FunctionalGroupDefinitions
            stockFunctionalGroups, SortedList<string, double> globalDiagnostics, Boolean tracking, Boolean DrawRandomly, 
            Boolean specificLocations)
        {
            // Add one to the counter of the number of grids. If there is more than one model grid, exit the program with a debug crash.
            NumGrids = NumGrids + 1;
            //Debug.Assert(NumGrids < 2, "You have initialised more than one grid on which to apply models. At present, this is not supported");

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

            // Seed the random number generator
            // Set the seed for the random number generator
            RandomNumberGenerator = new NonStaticSimpleRNG();
            if (DrawRandomly)
            {
                RandomNumberGenerator.SetSeedFromSystemTime();
            }
            else
            {
                RandomNumberGenerator.SetSeed(4315);
            }

            // CURRENTLY DEFINING MODEL CELLS BY BOTTOM LEFT CORNER
            _MinLatitude = minLat;
            _MinLongitude = minLon;
            _MaxLatitude = maxLat;
            _MaxLongitude = maxLon;
            _LatCellSize = latCellSize;
            _LonCellSize = lonCellSize;

            // Check to see if the number of grid cells is an integer
            Debug.Assert((((_MaxLatitude - _MinLatitude) % _LatCellSize) == 0), "Error: number of grid cells is non-integer: check cell size");

            _NumLatCells = (UInt32)((_MaxLatitude - _MinLatitude) / _LatCellSize);
            _NumLonCells = (UInt32)((_MaxLongitude - _MinLongitude) / _LonCellSize);
            _Lats = new float[_NumLatCells];
            _Lons = new float[_NumLonCells];

            // Set up latitude and longitude vectors - lower left
            for (int ii = 0; ii < _NumLatCells; ii++)
            {
                _Lats[ii] = _MinLatitude + ii * _LatCellSize;
            }
            for (int jj = 0; jj < _NumLonCells; jj++)
            {
                _Lons[jj] = _MinLongitude + jj * _LonCellSize;
            }

            // Instantiate a grid of grid cells
            InternalGrid = new GridCell[_NumLatCells, _NumLonCells];

            // Instantiate the arrays of lists of cohorts to disperse
            DeltaFunctionalGroupDispersalArray = new List<uint>[_NumLatCells, _NumLonCells];
            DeltaCohortNumberDispersalArray = new List<uint>[_NumLatCells, _NumLonCells];

            // Instantiate the array of lists of grid cells to disperse those cohorts to
            DeltaCellToDisperseToArray = new List<uint[]>[_NumLatCells, _NumLonCells];

            // Instantiate the arrays of cell entry and exit directions
            DeltaCellExitDirection = new List<uint>[_NumLatCells, _NumLonCells];
            DeltaCellEntryDirection = new List<uint>[_NumLatCells, _NumLonCells];

            // An array of lists of cells to which organisms in each cell can disperse to; includes all cells which contribute to the
            // perimeter list, plus diagonal cells if they are in the same realm
            CellsForDispersal = new List<uint[]>[_NumLatCells, _NumLonCells];

            // An array of lists of directions corresponding to cells which organisms can disperse to
            CellsForDispersalDirection = new List<uint>[_NumLatCells, _NumLonCells];

            Console.WriteLine("Initialising grid cell environment:");

            // Loop through to set up model grid
            for (int ii = 0; ii < _NumLatCells; ii+=GridCellRarefaction)
            {
                for (int jj = 0; jj < _NumLonCells; jj+=GridCellRarefaction)
                {
                    InternalGrid[ii, jj] = new GridCell(_Lats[ii],(uint)ii, _Lons[jj],(uint)jj, LatCellSize, LonCellSize, enviroStack,
                        GlobalMissingValue, cohortFunctionalGroups, stockFunctionalGroups, globalDiagnostics, tracking, specificLocations);
                    CellsForDispersal[ii,jj] = new List<uint[]>();
                    CellsForDispersalDirection[ii, jj] = new List<uint>();
                    Console.Write("\rRow {0} of {1}", ii+1, NumLatCells/GridCellRarefaction);
                }
            }
            Console.WriteLine("");
            Console.WriteLine("");

            InterpolateMissingValues();

            // Fill in the array of dispersable perimeter lengths for each grid cell
            CalculatePerimeterLengthsAndCellsDispersableTo();

            CellHeightsKm = new double[_Lats.Length];
            CellWidthsKm = new double[_Lats.Length];

            // Calculate the lengths of widths of grid cells in each latitudinal strip
            // Assume that we are at the midpoint of each cell when calculating lengths
            for (int ii = 0; ii < _Lats.Length; ii++)
            {
                 CellHeightsKm[ii] = Utilities.CalculateLengthOfDegreeLatitude(_Lats[ii] + _LatCellSize / 2) * _LatCellSize;
                 CellWidthsKm[ii] = Utilities.CalculateLengthOfDegreeLongitude(_Lats[ii] + _LatCellSize / 2) * _LonCellSize;
            }
        }
        public uint[] WeightedMassOrderedIndices(GridCellCohortHandler gridCellCohorts, uint[][] cohortIndices, uint numberIndices)
        {
            NonStaticSimpleRNG random = new NonStaticSimpleRNG();

            random.SetSeedFromSystemTime();

            // A vector to hold indices of cohorts in order
            int[] MassOrderedIndices;

            double AboveMinMass = 0;

            double OverallMinMass = 1E9;


            double MaxMass = 0;
            int    MaxFG   = 0;
            int    MaxC    = 0;
            double MinMass = 1E9;
            int    MinFG   = 0;
            int    MinC    = 0;

            for (int ll = 0; ll < gridCellCohorts.Count; ll++)
            {
                // Loop over gridCellCohorts in the functional group
                for (int kk = 0; kk < gridCellCohorts[ll].Count(); kk++)
                {
                    //Check if this cohort is the smallest
                    if (gridCellCohorts[ll][kk].IndividualBodyMass.CompareTo(MaxMass) > 0)
                    {
                        MaxFG   = ll;
                        MaxC    = kk;
                        MaxMass = gridCellCohorts[ll][kk].IndividualBodyMass;
                    }

                    //Check if this cohort is the smallest
                    if (gridCellCohorts[ll][kk].IndividualBodyMass.CompareTo(OverallMinMass) < 0)
                    {
                        MinFG          = ll;
                        MinC           = kk;
                        OverallMinMass = gridCellCohorts[ll][kk].IndividualBodyMass;
                    }
                }
            }

            double MassRange    = MaxMass - OverallMinMass;
            double logMassRange = Math.Log(MaxMass) - Math.Log(OverallMinMass);

            MassOrderedIndices = (int[])(object)this.RandomlyOrderedIndices(numberIndices);//this.MassOrderedIndices(gridCellCohorts,cohortIndices,numberIndices);

            uint[] OrderedIndices = new uint[numberIndices];
            int[]  Acted          = new int[numberIndices];
            int[]  UseIndices;
            int[]  UnActedIndices;
            uint   UnActedCounter;
            double p          = 0;
            int    OrderIndex = 0;

            int[] Cinds;
            do
            {
                //UseIndices = MassOrderedIndices.Where((x,idx) => Acted[idx] == 0).ToArray();
                UseIndices     = new int[numberIndices - Acted.Sum()];
                UnActedIndices = new int[numberIndices - Acted.Sum()];
                UnActedCounter = 0;
                for (int i = 0; i < Acted.Length; i++)
                {
                    if (Acted[i] == 0)
                    {
                        UnActedIndices[UnActedCounter] = i;
                        UnActedCounter++;
                    }
                }

                for (int i = 0; i < UnActedIndices.Length; i++)
                {
                    UseIndices[i] = MassOrderedIndices[UnActedIndices[i]];
                }

                foreach (uint i in UseIndices)
                {
                    Cinds = FindJaggedArrayIndex(i, cohortIndices, numberIndices);

                    p = (Math.Log(gridCellCohorts[Cinds[0]][Cinds[1]].IndividualBodyMass) - Math.Log(OverallMinMass)) / logMassRange;

                    if (random.GetUniform() > p)
                    {
                        OrderedIndices[OrderIndex] = cohortIndices[Cinds[0]][Cinds[1]];
                        OrderIndex++;
                        Acted[Array.FindIndex(MassOrderedIndices, row => row == i)] = 1;
                    }
                }
            } while (Acted.Sum() < (int)(numberIndices * 0.8));

            UseIndices     = new int[numberIndices - Acted.Sum()];
            UnActedIndices = new int[numberIndices - Acted.Sum()];
            UnActedCounter = 0;
            for (int i = 0; i < Acted.Length; i++)
            {
                if (Acted[i] == 0)
                {
                    UnActedIndices[UnActedCounter] = i;
                    UnActedCounter++;
                }
            }

            for (int i = 0; i < UnActedIndices.Length; i++)
            {
                UseIndices[i] = MassOrderedIndices[UnActedIndices[i]];
            }

            foreach (uint i in UseIndices)
            {
                Cinds = FindJaggedArrayIndex(i, cohortIndices, numberIndices);
                OrderedIndices[OrderIndex] = cohortIndices[Cinds[0]][Cinds[1]];
                OrderIndex++;
                Acted[Array.FindIndex(MassOrderedIndices, row => row == i)] = 1;
            }

            //OrderedIndices[OrderIndex] = cohortIndices[MaxFG][MaxC];



            return(OrderedIndices);
        }