Program Listing for File SimulateDispersal.h

Return to documentation for file (necsim/SimulateDispersal.h)

// This file is part of necsim project which is released under MIT license.
// See file **LICENSE.txt** or visit https://opensource.org/licenses/MIT) for full license details.

#ifndef DISPERSAL_TEST
#define DISPERSAL_TEST
#ifndef PYTHON_COMPILE
#define PYTHON_COMPILE
#endif

#include<string>
#include <cstdio>
#include <vector>
#include <iostream>
#include <fstream>
#include <cmath>
#include <stdexcept>
#include <sqlite3.h>
#include <set>
#include <mutex>
#include "Landscape.h"
#include "DispersalCoordinator.h"
#include "RNGController.h"
#include "Cell.h"
#include "DataMask.h"
#include "SQLiteHandler.h"

namespace necsim
{
    class SimulateDispersal
    {
    protected:
        // The density map object
        shared_ptr<Landscape> density_landscape;
        // The samplemask object
        DataMask data_mask;
        // Dispersal coordinator
        DispersalCoordinator dispersal_coordinator{};
        // Stores all key simulation current_metacommunity_parameters for the Landscape object
        shared_ptr<SimParameters> simParameters;
        // The random number generator object
        shared_ptr<RNGController> random;
        // The random number seed
        unsigned long seed;
        // The sqlite3 database object for storing outputs
        SQLiteHandler database;
        // Vector for storing pairs of dispersal distances to parameter references
        vector<tuple<unsigned long, Cell, double>> distances;
        // Maps distances to parameter references
        map<unsigned long, unsigned long> parameter_references;
        // Vector for storing the cells (for randomly choosing from)
        vector<Cell> cells;
        // The number of repeats to run the dispersal loop for
        unsigned long num_repeats;
        // The number of num_steps within each dispersal loop for the average distance travelled, which should be
        set<unsigned long> num_steps;
        // The number of threads launched to parallelise the distance simulation
        unsigned long num_workers;
        // generation counter
        double generation;
        // If true, sequentially selects dispersal probabilities, default is true
        bool is_sequential;
        // Reference number for this set of current_metacommunity_parameters in the database output
        unsigned long max_parameter_reference;

    public:

        SimulateDispersal() : density_landscape(make_shared<Landscape>()), data_mask(), dispersal_coordinator(),
                              simParameters(make_shared<SimParameters>()), random(make_shared<RNGController>()),
                              database(), distances(), parameter_references(), cells(), num_steps()
        {
            num_repeats = 0;
            seed = 0;
            is_sequential = false;
            max_parameter_reference = 0;
            generation = 0.0;
        }

        ~

        SimulateDispersal()
        {
            database.close();
        }

        void setSequential(bool bSequential);

        void setSimulationParameters(shared_ptr<SimParameters> sim_parameters, bool print = true);

        void importMaps();

        void setSizes();

        void setDispersalParameters();

        void setSeed(unsigned long s)
        {
            seed = s;
            random->wipeSeed();
            random->setSeed(s);
        }

        void setOutputDatabase(string out_database);

        void setNumberRepeats(unsigned long n);

        void setNumberSteps(const vector<unsigned long> &s);

        void setNumberWorkers(unsigned long n);

        unsigned long getMaxNumberSteps();

        void storeCellList();

        const Cell &getRandomCell();

        void getEndPoint(Cell &this_cell);

        void getEndPoint(Cell &this_cell, DispersalCoordinator &dispersal_coordinator);

        template <bool chooseRandomCells=false>
        void runDistanceLoop(const unsigned long bidx,
                             const unsigned long eidx,
                             const unsigned long num_repeats,
                             std::mutex &mutex,
                             unsigned long &finished,
                             DispersalCoordinator &dispersal_coordinator,
                             double &generation);

        template <bool chooseRandomCells=false>
        void runDistanceWorker(const unsigned long seed,
                               const unsigned long bidx,
                               const unsigned long eidx,
                               const unsigned long num_repeats,
                               std::mutex &mutex,
                               unsigned long &finished);

        void runMeanDispersalDistance();

        void runMeanDistanceTravelled();

        void runAllDistanceTravelled();

        void runSampleDistanceTravelled(const vector<Cell> &samples);

        void writeRepeatInfo(unsigned long i);

        void writeDatabase(string table_name);

        void writeParameters(string table_name);

        void clearParameters();

        void checkMaxParameterReference();

        unsigned long checkMaxIdNumber(string table_name);
    };
}
#endif