Nemo  2.3.46
LCE_Disperse_base Class Reference

The base class of the dispersal LCEs, all events move offspring to the post-dispersal patch containers. More...

#include <LCEdisperse.h>

+ Inheritance diagram for LCE_Disperse_base:
+ Collaboration diagram for LCE_Disperse_base:

Public Member Functions

 LCE_Disperse_base ()
 
virtual ~LCE_Disperse_base ()
 Deallocates the disp matrix. More...
 
bool setBaseParameters (string prefix)
 
void setParamPrefix (string pref)
 
void addParameters (string prefix, ParamUpdaterBase *updater)
 
unsigned int getMigrationPatchForward (sex_t SEX, unsigned int LocalPatch)
 
unsigned int getMigrationPatchBackward (sex_t SEX, unsigned int LocalPatch)
 
void setPropaguleTargets ()
 
void swapPostDisp ()
 
void reset_counters ()
 
Dispersal Matrix
void set_isForward (bool val)
 
bool checkForwardDispersalMatrix (TMatrix *mat)
 
bool checkBackwardDispersalMatrix (TMatrix *mat)
 
void allocateDispMatrix (sex_t sex, unsigned int dim)
 
bool setDispMatrix ()
 
bool setReducedDispMatrix ()
 The reduced dispersal matrix contains the indices of the patches to which each patch is connected. More...
 
bool updateDispMatrix ()
 
bool setIsland_MigrantPool_Matrix ()
 
bool setIsland_PropagulePool_Matrix ()
 
bool setSteppingStone1DMatrix ()
 
bool setLatticeMatrix ()
 Sets the dispersal matrices for the Lattice dispersal model. More...
 
bool setBasicLatticeMatrix (int side, double phi_mal, double phi_fem, double disp_mal, double disp_fem)
 
bool setLatticeTorrusMatrix (int side, double disp_mal, double disp_fem, TMatrix *grid)
 
bool setLatticeAbsorbingMatrix ()
 
bool setLatticeReflectingMatrix (int side, TMatrix *grid)
 
Accessors
unsigned int getDispersalModel ()
 
double getPropaguleProb ()
 
unsigned int getPropaguleTarget (unsigned int home)
 
Implementations
virtual void loadFileServices (FileServices *loader)
 
virtual void loadStatServices (StatServices *loader)
 
virtual age_t removeAgeClass ()
 
virtual age_t addAgeClass ()
 
virtual age_t requiredAgeClass ()
 
- Public Member Functions inherited from LifeCycleEvent
 LifeCycleEvent (const char *name, const char *trait_link)
 Cstor. More...
 
virtual ~LifeCycleEvent ()
 
virtual void init (Metapop *popPtr)
 Sets the pointer to the current Metapop and the trait link if applicable. More...
 
virtual bool attach_trait (string trait)
 
virtual void set_paramset (std::string name, bool required, SimComponent *owner)
 
virtual void set_event_name (std::string &name)
 Set the name of the event (name of the ParamSet) and add the corresponding parameter to the set. More...
 
virtual void set_event_name (const char *name)
 
virtual string & get_event_name ()
 Accessor to the LCE's name. More...
 
virtual int get_rank ()
 Accessor to the LCE rank in the life cycle. More...
 
virtual void set_pop_ptr (Metapop *popPtr)
 Accessors for the population pointer. More...
 
virtual Metapopget_pop_ptr ()
 
virtual void execute ()=0
 Execute the event on the pop. More...
 
virtual LifeCycleEventclone ()=0
 Cloning interface. More...
 
- Public Member Functions inherited from SimComponent
 SimComponent ()
 
virtual ~SimComponent ()
 
virtual void loadUpdaters (UpdaterServices *loader)
 Loads the parameters and component updater onto the updater manager. More...
 
virtual bool setParameters ()=0
 Default interface needed to initialize the component's variables from its input parameters value. More...
 
virtual void set_paramset (ParamSet *paramset)
 Sets the ParamSet member. More...
 
virtual void set_paramsetFromCopy (const ParamSet &PSet)
 Reset the set of parameters from a another set. More...
 
virtual ParamSetget_paramset ()
 ParamSet accessor. More...
 
virtual void add_parameter (Param *param)
 Interface to add a parameter to the set. More...
 
virtual void add_parameter (std::string Name, param_t Type, bool isRequired, bool isBounded, double low_bnd, double up_bnd)
 Interface to add a parameter to the set. More...
 
virtual void add_parameter (std::string Name, param_t Type, bool isRequired, bool isBounded, double low_bnd, double up_bnd, ParamUpdaterBase *updater)
 Interface to add a parameter and its updater to the set. More...
 
virtual Paramget_parameter (std::string name)
 Param getter. More...
 
virtual double get_parameter_value (std::string name)
 Param value getter. More...
 
virtual string get_name ()
 Returnd the name of the ParamSet, i.e. More...
 

Protected Attributes

unsigned int _npatch
 
- Protected Attributes inherited from LifeCycleEvent
std::string _event_name
 The param name to be read in the init file. More...
 
Metapop_popPtr
 The ptr to the current Metapop. More...
 
std::string _LCELinkedTraitType
 The name of the linked trait. More...
 
int _LCELinkedTraitIndex
 The index in the individual's trait table of the linked trait. More...
 
- Protected Attributes inherited from SimComponent
ParamSet_paramSet
 The parameters container. More...
 

Private Attributes

int _disp_model
 
double _disp_propagule_prob
 
vector< unsigned int_PropaguleTargets
 
double _fem_rate
 
double _mal_rate
 
bool _isForward
 
TMatrix_DispMatrix [2]
 The sex-specific dispersal matrices, [0] for males, [1] for females, might be used as connectivity matrix as well. More...
 
vector< vector< double > > _reducedDispMat [2]
 
vector< vector< double > > _reducedDispMatProba [2]
 
string _prefix
 

Friends

class LCE_Disperse_ConstDisp
 
class LCE_Disperse_EvolDisp
 

Detailed Description

The base class of the dispersal LCEs, all events move offspring to the post-dispersal patch containers.

Stores the dispersal matrices and dispersal model parameters and interface.

Constructor & Destructor Documentation

LCE_Disperse_base::LCE_Disperse_base ( )

References _DispMatrix.

47 , _fem_rate (-1), _mal_rate(-1), _isForward(1)
48 {
49  _DispMatrix[0] = NULL;
50  _DispMatrix[1] = NULL;
51 }
LifeCycleEvent(const char *name, const char *trait_link)
Cstor.
Definition: lifecycleevent.h:98
int _disp_model
Definition: LCEdisperse.h:42
double _fem_rate
Definition: LCEdisperse.h:45
TMatrix * _DispMatrix[2]
The sex-specific dispersal matrices, [0] for males, [1] for females, might be used as connectivity ma...
Definition: LCEdisperse.h:49
vector< unsigned int > _PropaguleTargets
Definition: LCEdisperse.h:44
double _mal_rate
Definition: LCEdisperse.h:45
bool _isForward
Definition: LCEdisperse.h:46
double _disp_propagule_prob
Definition: LCEdisperse.h:43
LCE_Disperse_base::~LCE_Disperse_base ( )
virtual

Deallocates the disp matrix.

References _DispMatrix.

54 {
55  if(NULL != _DispMatrix[0])
56  delete _DispMatrix[0];
57 
58  if(NULL != _DispMatrix[1])
59  delete _DispMatrix[1];
60 }
TMatrix * _DispMatrix[2]
The sex-specific dispersal matrices, [0] for males, [1] for females, might be used as connectivity ma...
Definition: LCEdisperse.h:49

Member Function Documentation

virtual age_t LCE_Disperse_base::addAgeClass ( )
inlinevirtual

Implements LifeCycleEvent.

Reimplemented in LCE_Breed_Selection_Disperse, and LCE_Breed_Disperse.

References NONE.

109 {return NONE;}
#define NONE
No age flag.
Definition: types.h:48
void LCE_Disperse_base::addParameters ( string  prefix,
ParamUpdaterBase updater 
)

References SimComponent::add_parameter(), DBL, INT, MAT, and STR.

Referenced by LCE_Breed_Disperse::LCE_Breed_Disperse(), LCE_Disperse_ConstDisp::LCE_Disperse_ConstDisp(), LCE_Disperse_EvolDisp::LCE_Disperse_EvolDisp(), and LCE_SeedDisp::LCE_SeedDisp().

63 {
64  add_parameter(prefix + "_model",INT,false,true,1,4,updater);
65  add_parameter(prefix + "_border_model",INT,false,true,1,3,updater);
66  add_parameter(prefix + "_lattice_range",INT,false,true,1,2,updater);
67  add_parameter(prefix + "_propagule_prob",DBL,false,true,0,1,updater);
68  add_parameter(prefix + "_matrix",MAT,false,false,0,0,updater);
69  add_parameter(prefix + "_matrix_fem",STR,false,false,0,0,updater);
70  add_parameter(prefix + "_matrix_mal",STR,false,false,0,0,updater);
71  add_parameter(prefix + "_rate",DBL,false,true,0,1,updater);
72  add_parameter(prefix + "_rate_fem",DBL,false,true,0,1,updater);
73  add_parameter(prefix + "_rate_mal",DBL,false,true,0,1,updater);
74  add_parameter(prefix + "_connectivity_matrix",MAT,false,false,0,0,updater);
75  add_parameter(prefix + "_reduced_matrix",MAT,false,false,0,0,updater);
76 
77 }
Definition: types.h:78
virtual void add_parameter(Param *param)
Interface to add a parameter to the set.
Definition: simcomponent.h:112
Definition: types.h:78
Definition: types.h:78
Definition: types.h:78
void LCE_Disperse_base::allocateDispMatrix ( sex_t  sex,
unsigned int  dim 
)

References _DispMatrix, and TMatrix::reset().

Referenced by setIsland_MigrantPool_Matrix(), setIsland_PropagulePool_Matrix(), setLatticeMatrix(), and setSteppingStone1DMatrix().

306 {
307  if(_DispMatrix[sex] != NULL)
308  _DispMatrix[sex]->reset(dim,dim);
309  else
310  _DispMatrix[sex] = new TMatrix(dim,dim);
311 }
TMatrix * _DispMatrix[2]
The sex-specific dispersal matrices, [0] for males, [1] for females, might be used as connectivity ma...
Definition: LCEdisperse.h:49
A class to handle matrix in params, coerces matrix into a vector of same total size.
Definition: tmatrix.h:49
void reset(unsigned int rows, unsigned int cols)
Re-allocate the existing matrix with assigned rows and cols dimensions.
Definition: tmatrix.h:97
bool LCE_Disperse_base::checkBackwardDispersalMatrix ( TMatrix mat)

References error(), TMatrix::get(), TMatrix::getNbCols(), and TMatrix::getNbRows().

Referenced by setBaseParameters(), and setDispMatrix().

335 {
336  double cntr;
337 
338  for(unsigned int i = 0; i < mat->getNbCols(); ++i) {
339  cntr = 0;
340  for(unsigned int j = 0; j < mat->getNbRows(); ++j)
341  cntr += mat->get(j,i);
342  if(cntr < 0.999999 || cntr > 1.000001) {
343  error("The elements of column %i of the dispersal matrix do not sum to 1!\n",i+1);
344  return false;
345  }
346  }
347  return true;
348 }
unsigned int getNbCols()
Gives the number of columns.
Definition: tmatrix.h:142
unsigned int getNbRows()
Gives the number of rows.
Definition: tmatrix.h:139
double get(unsigned int i, unsigned int j)
Accessor to element at row i and column j.
Definition: tmatrix.h:120
int error(const char *str,...)
Definition: output.cc:73
bool LCE_Disperse_base::checkForwardDispersalMatrix ( TMatrix mat)

References error(), TMatrix::get(), TMatrix::getNbCols(), and TMatrix::getNbRows().

Referenced by setBaseParameters(), and setDispMatrix().

316 {
317  double cntr;
318 
319  for(unsigned int i = 0; i < mat->getNbRows(); ++i) {
320  cntr = 0;
321  for(unsigned int j = 0; j < mat->getNbCols(); ++j)
322  cntr += mat->get(i,j);
323  if(cntr < 0.999999 || cntr > 1.000001) {
324  error("the elements of row %i of the dispersal matrix do not sum to 1!\n",i+1);
325  error("sum of row %i is: %f\n",i+1, cntr);
326  return false;
327  }
328  }
329  return true;
330 }
unsigned int getNbCols()
Gives the number of columns.
Definition: tmatrix.h:142
unsigned int getNbRows()
Gives the number of rows.
Definition: tmatrix.h:139
double get(unsigned int i, unsigned int j)
Accessor to element at row i and column j.
Definition: tmatrix.h:120
int error(const char *str,...)
Definition: output.cc:73
unsigned int LCE_Disperse_base::getMigrationPatchBackward ( sex_t  SEX,
unsigned int  LocalPatch 
)

References _reducedDispMat, _reducedDispMatProba, and RAND::Uniform().

Referenced by LCE_Breed_Disperse::get_parent().

1122 {
1123  double sum = 0, random = RAND::Uniform();
1124  unsigned int SourcePatch = 0;
1125 
1126  if(random > 0.999999) random = 0.999999;//this to avoid overflows when random == 1
1127 
1128  sum = _reducedDispMatProba[SEX][LocalPatch][SourcePatch];
1129 
1130  while (random > sum) {
1131  SourcePatch++;
1132  sum += _reducedDispMatProba[SEX][LocalPatch][SourcePatch];
1133  }
1134 
1135  return _reducedDispMat[SEX][LocalPatch][SourcePatch];
1136 }
static double Uniform()
Generates a random number from [0.0, 1.0[ uniformly distributed.
Definition: Uniform.h:101
vector< vector< double > > _reducedDispMat[2]
Definition: LCEdisperse.h:51
vector< vector< double > > _reducedDispMatProba[2]
Definition: LCEdisperse.h:52
unsigned int LCE_Disperse_base::getMigrationPatchForward ( sex_t  SEX,
unsigned int  LocalPatch 
)

References _reducedDispMat, _reducedDispMatProba, and RAND::Uniform().

Referenced by LCE_Disperse_ConstDisp::MigratePatch(), and LCE_Disperse_ConstDisp::MigratePatch_AbsorbingBorder().

1103 {
1104  double sum = 0, random = RAND::Uniform();
1105  unsigned int AimedPatch = 0;
1106 
1107  if(random > 0.999999) random = 0.999999;//this to avoid overflows when random == 1
1108 
1109  sum = _reducedDispMatProba[SEX][LocalPatch][AimedPatch];
1110 
1111  while (random > sum) {
1112  AimedPatch++;
1113  sum += _reducedDispMatProba[SEX][LocalPatch][AimedPatch];
1114  }
1115  //return the patch ID stored in the connectivity matrix:
1116  return _reducedDispMat[SEX][LocalPatch][AimedPatch];
1117 }
static double Uniform()
Generates a random number from [0.0, 1.0[ uniformly distributed.
Definition: Uniform.h:101
vector< vector< double > > _reducedDispMat[2]
Definition: LCEdisperse.h:51
vector< vector< double > > _reducedDispMatProba[2]
Definition: LCEdisperse.h:52
double LCE_Disperse_base::getPropaguleProb ( )
inline

References _disp_propagule_prob.

Referenced by LCE_Disperse_EvolDisp::Migrate_Island_Propagule(), and setIsland_PropagulePool_Matrix().

99 {return _disp_propagule_prob;}
double _disp_propagule_prob
Definition: LCEdisperse.h:43
unsigned int LCE_Disperse_base::getPropaguleTarget ( unsigned int  home)
inline

Referenced by LCE_Disperse_EvolDisp::Migrate_Island_Propagule(), and setIsland_PropagulePool_Matrix().

100 {return _PropaguleTargets[home];}
vector< unsigned int > _PropaguleTargets
Definition: LCEdisperse.h:44
virtual void LCE_Disperse_base::loadFileServices ( FileServices loader)
inlinevirtual

Implements SimComponent.

Reimplemented in LCE_Breed_Selection_Disperse, and LCE_Breed_Disperse.

106 {}
virtual void LCE_Disperse_base::loadStatServices ( StatServices loader)
inlinevirtual

Implements SimComponent.

Reimplemented in LCE_Breed_Selection_Disperse, and LCE_Breed_Disperse.

107 {}
virtual age_t LCE_Disperse_base::removeAgeClass ( )
inlinevirtual

Implements LifeCycleEvent.

Reimplemented in LCE_Breed_Selection_Disperse, and LCE_Breed_Disperse.

References NONE.

108 {return NONE;}
#define NONE
No age flag.
Definition: types.h:48
virtual age_t LCE_Disperse_base::requiredAgeClass ( )
inlinevirtual

Implements LifeCycleEvent.

Reimplemented in LCE_Breed_Selection_Disperse, and LCE_Breed_Disperse.

References OFFSPRG.

110 {return OFFSPRG;}
#define OFFSPRG
Offspring age class flag.
Definition: types.h:50
void LCE_Disperse_base::reset_counters ( )

References _npatch, LifeCycleEvent::_popPtr, Patch::flush(), Metapop::getPatch(), PDISPx, and Patch::reset_counters().

Referenced by LCE_Disperse_ConstDisp::execute(), LCE_Breed_Disperse::execute(), LCE_Disperse_EvolDisp::execute(), and LCE_Breed_Selection_Disperse::execute().

291 {
292  Patch *patch;
293  for(unsigned int i = 0; i < _npatch; i++) {
294 
295  patch = _popPtr->getPatch(i);
296 
297  patch->reset_counters();
298 
299  patch->flush(PDISPx, _popPtr);
300  }
301 }
void flush(sex_t SEX, age_idx AGE, Metapop *pop)
Removes all individual pointers of the appropriate sex and age class and flush them into the recyclin...
Definition: metapop.h:634
Second class in the metapopulation design structure, between the Metapop and Individual classes...
Definition: metapop.h:421
Patch * getPatch(unsigned int i)
Patch accessor, return the ith+1 patch in the metapop.
Definition: metapop.h:251
unsigned int _npatch
Definition: LCEdisperse.h:60
void reset_counters()
Definition: patch.cc:56
Metapop * _popPtr
The ptr to the current Metapop.
Definition: lifecycleevent.h:81
Definition: types.h:42
void LCE_Disperse_base::set_isForward ( bool  val)
inline

Referenced by LCE_Breed_Disperse::setParameters().

75 {_isForward = val;}
bool _isForward
Definition: LCEdisperse.h:46
bool LCE_Disperse_base::setBaseParameters ( string  prefix)

References _disp_model, _disp_propagule_prob, _DispMatrix, _fem_rate, _isForward, _mal_rate, _npatch, SimComponent::_paramSet, LifeCycleEvent::_popPtr, _prefix, _reducedDispMat, _reducedDispMatProba, checkBackwardDispersalMatrix(), checkForwardDispersalMatrix(), error(), FEM, SimComponent::get_parameter(), ParamSet::getMatrix(), TMatrix::getNbCols(), TMatrix::getNbRows(), Metapop::getPatchNbr(), ParamSet::getValue(), Param::getVariableMatrix(), ParamSet::isSet(), MAL, setDispMatrix(), setReducedDispMatrix(), and warning().

Referenced by LCE_Disperse_ConstDisp::setParameters(), and LCE_Breed_Disperse::setParameters().

82 {
83  _prefix = prefix;
84 
86 
87  _disp_model = (int)_paramSet->getValue(prefix + "_model");
88 
89  _disp_propagule_prob = _paramSet->getValue(prefix + "_propagule_prob");
90 
91 // for (unsigned int sex = 0; sex < 2; sex++) {
92 // if(_DispMatrix[sex]) {
93 // delete _DispMatrix[sex];
94 // _DispMatrix[sex] = NULL;
95 // }
96 // }
97 
98  if(_paramSet->isSet(prefix + "_connectivity_matrix") &&
99  _paramSet->isSet(prefix + "_reduced_matrix"))
100  {
101 
102  _disp_model=0;
103 
104  get_parameter(prefix + "_connectivity_matrix")->getVariableMatrix(&_reducedDispMat[0]);
105 
106  get_parameter(prefix + "_reduced_matrix")->getVariableMatrix(&_reducedDispMatProba[0]);
107 
108  //need to copy matrices for the other sex (a single matrix is given in input)
109 
110  _reducedDispMat[1].clear();
111  _reducedDispMatProba[1].clear();
112 
113  if (_reducedDispMat[0].size() != _reducedDispMatProba[0].size())
114  return error("The connectivity and reduced dispersal matrices don't have same number of rows\n");
115 
116  for (unsigned int i = 0; i < _reducedDispMat[0].size(); ++i) {
117 
118  if (_reducedDispMat[0][i].size() != _reducedDispMatProba[0][i].size())
119  return error("Row %i of the connectivity and reduced dispersal matrices are not of same size\n", i+1);
120 
121  _reducedDispMat[1].push_back(vector<double>());
122 
123  _reducedDispMatProba[1].push_back(vector<double>());
124 
125  double row_sum = 0;
126 
127  for (unsigned int j = 0; j < _reducedDispMat[0][i].size(); ++j) {
128  _reducedDispMat[1][i].push_back( _reducedDispMat[0][i][j] );
129  _reducedDispMatProba[1][i].push_back( _reducedDispMatProba[0][i][j] );
130  row_sum += _reducedDispMatProba[0][i][j];
131  }
132 
133  if(row_sum < 0.999999 || row_sum > 1.000001)
134  return error("the elements of row %i of the reduced dispersal matrix do not sum to 1!\n",i+1);
135 
136  }
137 
138 
139  }
140 
141  else if ( (_paramSet->isSet(prefix + "_connectivity_matrix") &&
142  !_paramSet->isSet(prefix + "_reduced_matrix")) ||
143  (!_paramSet->isSet(prefix + "_connectivity_matrix") &&
144  _paramSet->isSet(prefix + "_reduced_matrix")))
145 
146  {
147  return error("both \"%s_connectivity_matrix\" and \"%s_reduced_matrix\" must be set together\n", prefix.c_str(), prefix.c_str());
148  }
149 
150  else
151 
152  {
153 
154  if(_DispMatrix[0]) delete _DispMatrix[0];
155  if(_DispMatrix[1]) delete _DispMatrix[1];
156 
157  if(_paramSet->isSet(prefix + "_matrix")) {
158 
159  _DispMatrix[0] = new TMatrix();
160 
161  _paramSet->getMatrix(prefix + "_matrix",_DispMatrix[0]);
162 
163  //same dispersal matrix for males and females
164  _DispMatrix[1] = new TMatrix(*_DispMatrix[0]);
165 
166  } else {
167 
168  if(_paramSet->isSet(prefix + "_matrix_fem")) {
169 
170  _DispMatrix[FEM] = new TMatrix();
171 
172  _paramSet->getMatrix(prefix + "_matrix_fem",_DispMatrix[FEM]);
173 
174  }
175 
176  if(_paramSet->isSet(prefix + "_matrix_mal")) {
177 
178  _DispMatrix[MAL] = new TMatrix();
179 
180  _paramSet->getMatrix(prefix + "_matrix_mal",_DispMatrix[MAL]);
181 
182  }
183  }
184 
185  if( _paramSet->isSet(prefix + "_matrix") ||
186  ( _paramSet->isSet(prefix + "_matrix_fem") && _paramSet->isSet(prefix + "_matrix_mal") ) )
187  {
188 
189  if( ( _paramSet->isSet(prefix + "_rate") ||
190  ( _paramSet->isSet(prefix + "_rate_fem") && _paramSet->isSet(prefix + "_rate_mal")) )
191  || _paramSet->isSet(prefix + "_model") )
192  warning("parameter \"dispersal_matrix\" takes precedence over parameters \"dispersal_rate\" and \"dispersal_model\"\n");
193 
194  _disp_model = 0;
195 
196  if(_DispMatrix[FEM]) {
197 
198  if(_DispMatrix[FEM]->length() != _npatch*_npatch)
199  return error("the size of the female dispersal matrix is not equal to patch_number X patch_number (%i[%i,%i] != %i)!\n",
201 
202  if(_isForward) {
203  if(!checkForwardDispersalMatrix(_DispMatrix[FEM])) return false;
204  } else {
205  if(!checkBackwardDispersalMatrix(_DispMatrix[FEM])) return false;
206  }
207 
208  }
209 
210  if(_DispMatrix[MAL]) {
211 
212  if(_DispMatrix[MAL]->length() != _npatch*_npatch)
213  return error("the size of the male dispersal matrix is not equal to patch_number X patch_number (%i[%i,%i] != %i)!\n",
215 
216 
217  if(_isForward) {
218  if(!checkForwardDispersalMatrix(_DispMatrix[MAL])) return false;
219  } else {
220  if(!checkBackwardDispersalMatrix(_DispMatrix[MAL])) return false;
221  }
222  }
223 
225 
226  } else {
227 
228  if(!_paramSet->isSet(prefix + "_model")) return error("Dispersal model is not set!\n");
229 
230 
231  if(_paramSet->isSet(prefix + "_rate"))
232 
233  {
234 
235  _fem_rate = _mal_rate = _paramSet->getValue(prefix + "_rate");
236 
237  if(!setDispMatrix()) return false;
238 
239  }
240 
241  else if( _paramSet->isSet(prefix + "_rate_fem") && _paramSet->isSet(prefix + "_rate_mal") )
242 
243  {
244  _fem_rate = _paramSet->getValue(prefix + "_rate_fem");
245 
246  _mal_rate = _paramSet->getValue(prefix + "_rate_mal");
247 
248  if(!setDispMatrix()) return false;
249  }
250 
251  else
252 
253  {
254  return error("Dispersal rate parameters not set!\n");
255  }
256 
257  }
258 
259  }
260  return true;
261 }
bool checkBackwardDispersalMatrix(TMatrix *mat)
Definition: LCEdisperse.cc:334
int _disp_model
Definition: LCEdisperse.h:42
unsigned int getNbCols()
Gives the number of columns.
Definition: tmatrix.h:142
unsigned int getNbRows()
Gives the number of rows.
Definition: tmatrix.h:139
double _fem_rate
Definition: LCEdisperse.h:45
TMatrix * _DispMatrix[2]
The sex-specific dispersal matrices, [0] for males, [1] for females, might be used as connectivity ma...
Definition: LCEdisperse.h:49
void getMatrix(string name, TMatrix *mat)
Accessor to the parameters matrix.
Definition: param.h:289
void getVariableMatrix(vector< vector< double > > *mat)
Definition: param.cc:411
unsigned int _npatch
Definition: LCEdisperse.h:60
unsigned int getPatchNbr()
Definition: metapop.h:270
int error(const char *str,...)
Definition: output.cc:73
double _mal_rate
Definition: LCEdisperse.h:45
void warning(const char *str,...)
Definition: output.cc:56
bool setDispMatrix()
Definition: LCEdisperse.cc:404
A class to handle matrix in params, coerces matrix into a vector of same total size.
Definition: tmatrix.h:49
bool checkForwardDispersalMatrix(TMatrix *mat)
Definition: LCEdisperse.cc:315
ParamSet * _paramSet
The parameters container.
Definition: simcomponent.h:48
Definition: types.h:37
double getValue(string name)
Accessor the parameters value.
Definition: param.h:287
virtual Param * get_parameter(std::string name)
Param getter.
Definition: simcomponent.h:139
vector< vector< double > > _reducedDispMat[2]
Definition: LCEdisperse.h:51
bool setReducedDispMatrix()
The reduced dispersal matrix contains the indices of the patches to which each patch is connected...
Definition: LCEdisperse.cc:976
bool isSet()
Accessor to the status flag.
Definition: param.h:273
bool _isForward
Definition: LCEdisperse.h:46
Metapop * _popPtr
The ptr to the current Metapop.
Definition: lifecycleevent.h:81
Definition: types.h:37
double _disp_propagule_prob
Definition: LCEdisperse.h:43
vector< vector< double > > _reducedDispMatProba[2]
Definition: LCEdisperse.h:52
string _prefix
Definition: LCEdisperse.h:54
bool LCE_Disperse_base::setBasicLatticeMatrix ( int  side,
double  phi_mal,
double  phi_fem,
double  disp_mal,
double  disp_fem 
)

References _DispMatrix, _npatch, _prefix, TMatrix::assign(), error(), FEM, TMatrix::get(), SimComponent::get_parameter_value(), MAL, TMatrix::set(), setLatticeAbsorbingMatrix(), setLatticeReflectingMatrix(), and setLatticeTorrusMatrix().

Referenced by setLatticeMatrix().

651 {
652  TMatrix* mmat = _DispMatrix[MAL];
653  TMatrix* fmat = _DispMatrix[FEM];
654 
655  //init:
656  fmat->assign(0.0);
657  mmat->assign(0.0);
658 
659  TMatrix grid(side, side);
660 
661  int p = 0;
662  for (int i = 0; i < side; ++i) {
663  for (int j = 0; j < side ; ++j) {
664  grid.set(i, j, p++);
665  }
666  }
667  assert( p == side*side );
668 
669  //diagonal:
670  for (unsigned int i = 0; i < _npatch; ++i){
671  fmat->set(i, i, phi_fem);
672  mmat->set(i, i, phi_mal);
673  }
674 
675  int connect;
676 
677  for (int x = 0; x < side; ++x) {
678  for (int y = 0; y < side; ++y) {
679 
680  p = grid.get(x, y);
681 
682  connect = p + 1; //patch to the right
683 
684  if (connect < (x + 1)*side) { //stay on the same row
685  fmat->set(p, connect, disp_fem);
686  mmat->set(p, connect, disp_mal);
687  }
688 
689  connect = p - 1; //patch to the left
690 
691  if (connect >= x*side ) {
692  fmat->set(p, connect, disp_fem);
693  mmat->set(p, connect, disp_mal);
694  }
695 
696  connect = p + side; //patch one row up
697 
698  if (connect < (int)_npatch ) {
699  fmat->set(p, connect, disp_fem);
700  mmat->set(p, connect, disp_mal);
701  }
702 
703  connect = p - side; //patch one row down
704 
705  if (connect >= 0 ) {
706  fmat->set(p, connect, disp_fem);
707  mmat->set(p, connect, disp_mal);
708  }
709 
710  //diagonal steps:
711  if((unsigned int)get_parameter_value(_prefix + "_lattice_range") == 2) {
712 
713  connect = p + side; //patches one row up
714 
715  if (connect < (int)_npatch ) { //we are not on the last row
716 
717  if (connect + 1 < (x + 2)*side) {
718  fmat->set(p, connect + 1, disp_fem);
719  mmat->set(p, connect + 1, disp_mal);
720  }
721 
722  if (connect - 1 >= (x + 1)*side) {
723  fmat->set(p, connect - 1, disp_fem);
724  mmat->set(p, connect - 1, disp_mal);
725  }
726  }
727 
728  connect = p - side; //patches one row down
729 
730  if (connect >= 0) { //we are not on the first row
731 
732  if (connect + 1 < (x + 1)*side) {
733  fmat->set(p, connect + 1, disp_fem);
734  mmat->set(p, connect + 1, disp_mal);
735  }
736 
737  if (connect - 1 >= (x - 1)*side) {
738  fmat->set(p, connect - 1, disp_fem);
739  mmat->set(p, connect - 1, disp_mal);
740  }
741  }
742  } //lattice range == 2
743 
744  } //y
745  } //x
746 
747  switch((unsigned int)get_parameter_value(_prefix + "_border_model")) {
748  case 1:
749  return setLatticeTorrusMatrix((int)side, disp_mal, disp_fem, &grid);
750  case 2:
751  return setLatticeReflectingMatrix((int)side, &grid);
752  case 3:
753  return setLatticeAbsorbingMatrix();
754  default:
755  error("parameter \"%s_border_model\" accepts only three values: [1,2,3]\n", _prefix.c_str());
756  break;
757  }
758 
759  return false;
760 }
virtual double get_parameter_value(std::string name)
Param value getter.
Definition: simcomponent.h:143
void assign(double val)
Assigns a value to all element of the matrix.
Definition: tmatrix.h:92
bool setLatticeTorrusMatrix(int side, double disp_mal, double disp_fem, TMatrix *grid)
Definition: LCEdisperse.cc:764
TMatrix * _DispMatrix[2]
The sex-specific dispersal matrices, [0] for males, [1] for females, might be used as connectivity ma...
Definition: LCEdisperse.h:49
unsigned int _npatch
Definition: LCEdisperse.h:60
int error(const char *str,...)
Definition: output.cc:73
void set(unsigned int i, unsigned int j, double val)
Sets element at row i and column j to value val.
Definition: tmatrix.h:85
A class to handle matrix in params, coerces matrix into a vector of same total size.
Definition: tmatrix.h:49
Definition: types.h:37
bool setLatticeReflectingMatrix(int side, TMatrix *grid)
Definition: LCEdisperse.cc:874
Definition: types.h:37
bool setLatticeAbsorbingMatrix()
Definition: LCEdisperse.cc:918
string _prefix
Definition: LCEdisperse.h:54
bool LCE_Disperse_base::setDispMatrix ( )

References _DispMatrix, _isForward, checkBackwardDispersalMatrix(), checkForwardDispersalMatrix(), error(), FEM, getDispersalModel(), MAL, setIsland_MigrantPool_Matrix(), setIsland_PropagulePool_Matrix(), setLatticeMatrix(), setReducedDispMatrix(), and setSteppingStone1DMatrix().

Referenced by setBaseParameters(), and updateDispMatrix().

405 {
406 
407  switch ( getDispersalModel() ) {
408  case 1:
409  if( !setIsland_MigrantPool_Matrix() ) return false;
410  break;
411  case 2:
412  if( !setIsland_PropagulePool_Matrix() ) return false;
413  break;
414  case 3:
415  if( !setSteppingStone1DMatrix() ) return false;
416  break;
417  case 4:
418  if( !setLatticeMatrix() ) return false;
419  break;
420  default:
421  return error("Dispersal model '%i' not yet implemented\n",getDispersalModel());
422  }
423 
424  if(_isForward) {
425  if(!checkForwardDispersalMatrix(_DispMatrix[FEM])) return false;
426  if(!checkForwardDispersalMatrix(_DispMatrix[MAL])) return false;
427  } else {
428  if(!checkBackwardDispersalMatrix(_DispMatrix[FEM])) return false;
429  if(!checkBackwardDispersalMatrix(_DispMatrix[MAL])) return false;
430  }
431 
432  return setReducedDispMatrix();
433 }
unsigned int getDispersalModel()
Definition: LCEdisperse.h:98
bool checkBackwardDispersalMatrix(TMatrix *mat)
Definition: LCEdisperse.cc:334
bool setIsland_PropagulePool_Matrix()
Definition: LCEdisperse.cc:469
TMatrix * _DispMatrix[2]
The sex-specific dispersal matrices, [0] for males, [1] for females, might be used as connectivity ma...
Definition: LCEdisperse.h:49
int error(const char *str,...)
Definition: output.cc:73
bool checkForwardDispersalMatrix(TMatrix *mat)
Definition: LCEdisperse.cc:315
Definition: types.h:37
bool setReducedDispMatrix()
The reduced dispersal matrix contains the indices of the patches to which each patch is connected...
Definition: LCEdisperse.cc:976
bool setSteppingStone1DMatrix()
Definition: LCEdisperse.cc:519
bool _isForward
Definition: LCEdisperse.h:46
Definition: types.h:37
bool setIsland_MigrantPool_Matrix()
Definition: LCEdisperse.cc:437
bool setLatticeMatrix()
Sets the dispersal matrices for the Lattice dispersal model.
Definition: LCEdisperse.cc:606
bool LCE_Disperse_base::setIsland_MigrantPool_Matrix ( )

References _DispMatrix, _fem_rate, _mal_rate, _npatch, allocateDispMatrix(), FEM, MAL, and TMatrix::set().

Referenced by setDispMatrix().

438 {
439 #ifdef _DEBUG_
440  cout<<"setIsland_MigrantPool_Matrix(_npatch="<<_npatch<<", _mal_rate="
441  <<_mal_rate<<", _fem_rate="<<_fem_rate<<")"<<endl;
442 #endif
445 
446  TMatrix* mmat = _DispMatrix[MAL];
447  TMatrix* fmat = _DispMatrix[FEM];
448  double pmal = 1 - _mal_rate;
449  double pfem = 1 - _fem_rate;
450  double mmal = _mal_rate/(_npatch-1);
451  double mfem = _fem_rate/(_npatch-1);
452 
453  for (unsigned int i=0; i<_npatch; ++i){
454  for (unsigned int j=0; j<_npatch; ++j){
455  mmat->set(i,j, mmal);
456  fmat->set(i,j, mfem);
457  }
458  }
459 
460  for (unsigned int i=0; i<_npatch; ++i){
461  mmat->set(i,i, pmal);
462  fmat->set(i,i, pfem);
463  }
464  return true;
465 }
double _fem_rate
Definition: LCEdisperse.h:45
TMatrix * _DispMatrix[2]
The sex-specific dispersal matrices, [0] for males, [1] for females, might be used as connectivity ma...
Definition: LCEdisperse.h:49
unsigned int _npatch
Definition: LCEdisperse.h:60
void set(unsigned int i, unsigned int j, double val)
Sets element at row i and column j to value val.
Definition: tmatrix.h:85
double _mal_rate
Definition: LCEdisperse.h:45
A class to handle matrix in params, coerces matrix into a vector of same total size.
Definition: tmatrix.h:49
void allocateDispMatrix(sex_t sex, unsigned int dim)
Definition: LCEdisperse.cc:305
Definition: types.h:37
Definition: types.h:37
bool LCE_Disperse_base::setIsland_PropagulePool_Matrix ( )

References _DispMatrix, _fem_rate, _mal_rate, _npatch, SimComponent::_paramSet, _prefix, allocateDispMatrix(), error(), FEM, getPropaguleProb(), getPropaguleTarget(), ParamSet::isSet(), MAL, TMatrix::set(), and setPropaguleTargets().

Referenced by LCE_Breed_Selection_Disperse::breed_selection_disperse_propagule(), LCE_Breed_Disperse::do_breed_disperse_propagule(), LCE_Disperse_ConstDisp::Migrate_propagule(), and setDispMatrix().

470 {
473 
474  if( !_paramSet->isSet(_prefix + "_propagule_prob") ) {
475  error("Missing parameter \"dispersal_propagule_prob\" with dispersal model 2!\n");
476  return false;
477  }
478 
480 
481  double propagulePHI = getPropaguleProb();
482  double c1 = (1 - _fem_rate), c2 = (_fem_rate*propagulePHI),
483  c3 = (_fem_rate*(1.0 - propagulePHI)/(_npatch-2));
484 
485  TMatrix* mmat = _DispMatrix[MAL];
486  TMatrix* fmat = _DispMatrix[FEM];
487 
488  for (unsigned int i=0; i < _npatch; ++i){
489 
490  fmat->set(i, i, c1);
491 
492  for (unsigned int j=i+1; j < _npatch; ++j){
493  fmat->set(i, j, c3);
494  fmat->set(j, i, c3);
495  }
496  fmat->set(i, getPropaguleTarget(i), c2);
497  }
498 
499  c1 = (1 - _mal_rate);
500  c2 = (_mal_rate*propagulePHI);
501  c3 = (_mal_rate*(1.0 - propagulePHI)/(_npatch-2));
502 
503  for (unsigned int i=0; i < _npatch; ++i){
504 
505  mmat->set(i, i, c1);
506 
507  for (unsigned int j=i+1; j< _npatch; ++j) {
508  mmat->set(i, j, c3);
509  mmat->set(j, i, c3);
510  }
511  mmat->set(i, getPropaguleTarget(i), c2);
512  }
513 
514  return true;
515 }
double _fem_rate
Definition: LCEdisperse.h:45
void setPropaguleTargets()
Definition: LCEdisperse.cc:352
TMatrix * _DispMatrix[2]
The sex-specific dispersal matrices, [0] for males, [1] for females, might be used as connectivity ma...
Definition: LCEdisperse.h:49
unsigned int getPropaguleTarget(unsigned int home)
Definition: LCEdisperse.h:100
unsigned int _npatch
Definition: LCEdisperse.h:60
int error(const char *str,...)
Definition: output.cc:73
void set(unsigned int i, unsigned int j, double val)
Sets element at row i and column j to value val.
Definition: tmatrix.h:85
double _mal_rate
Definition: LCEdisperse.h:45
A class to handle matrix in params, coerces matrix into a vector of same total size.
Definition: tmatrix.h:49
ParamSet * _paramSet
The parameters container.
Definition: simcomponent.h:48
void allocateDispMatrix(sex_t sex, unsigned int dim)
Definition: LCEdisperse.cc:305
Definition: types.h:37
double getPropaguleProb()
Definition: LCEdisperse.h:99
bool isSet()
Accessor to the status flag.
Definition: param.h:273
Definition: types.h:37
string _prefix
Definition: LCEdisperse.h:54
bool LCE_Disperse_base::setLatticeAbsorbingMatrix ( )

References _DispMatrix, _isForward, _npatch, FEM, TMatrix::get(), MAL, TMatrix::plus(), and TMatrix::set().

Referenced by setBasicLatticeMatrix().

919 {
920  TMatrix* mmat = _DispMatrix[MAL];
921  TMatrix* fmat = _DispMatrix[FEM];
922 
923  fmat->set(_npatch, _npatch, 1.0);
924  mmat->set(_npatch, _npatch, 1.0);
925 
926  double sum;
927 
928  if(_isForward) {
929  //set the absorbing patch probs to 1 - sum(row)
930  for(unsigned int i = 0; i < _npatch; ++i) {
931  sum = 0;
932  for (unsigned int j = 0; j < _npatch; ++j) {
933  sum += fmat->get(i, j);
934  }
935  fmat->set(i, _npatch, 1.0 - sum);
936  }
937 
938  for(unsigned int i = 0; i < _npatch; ++i) {
939  sum = 0;
940  for (unsigned int j = 0; j < _npatch; ++j) {
941  sum += mmat->get(i, j);
942  }
943  mmat->set(i, _npatch, 1.0 - sum);
944  }
945  //backward migration:
946  }else {
947  //the missing immigrant rate from non-existing patches must be added to the "philopatric" rate
948  for(unsigned int i = 0; i < _npatch; ++i) {
949  sum = 0;
950  for (unsigned int j = 0; j < _npatch; ++j) {
951  sum += fmat->get(j, i);
952  }
953  fmat->plus(i, i, 1.0 - sum);
954  }
955 
956  for(unsigned int i = 0; i < _npatch; ++i) {
957  sum = 0;
958  for (unsigned int j = 0; j < _npatch; ++j) {
959  sum += mmat->get(j, i);
960  }
961  mmat->plus(i, i, 1.0 - sum);
962  }
963  }
964 
965  return true;
966 }
void plus(unsigned int i, unsigned int j, double value)
Adds a value to an element of the matrix.
Definition: tmatrix.h:183
TMatrix * _DispMatrix[2]
The sex-specific dispersal matrices, [0] for males, [1] for females, might be used as connectivity ma...
Definition: LCEdisperse.h:49
double get(unsigned int i, unsigned int j)
Accessor to element at row i and column j.
Definition: tmatrix.h:120
unsigned int _npatch
Definition: LCEdisperse.h:60
void set(unsigned int i, unsigned int j, double val)
Sets element at row i and column j to value val.
Definition: tmatrix.h:85
A class to handle matrix in params, coerces matrix into a vector of same total size.
Definition: tmatrix.h:49
Definition: types.h:37
bool _isForward
Definition: LCEdisperse.h:46
Definition: types.h:37
bool LCE_Disperse_base::setLatticeMatrix ( )

Sets the dispersal matrices for the Lattice dispersal model.

Each matrix has 'patch number' x 'patch number' cells unless the lattice model is the absorbing boundaries model where we add the sink patch.

The "dispersal_lattice_range" parameter defines the number of neighbouring patches to disperse into. Option 1 sets this number to 4 (left and right, up and down patches) whereas option 2 allows to disperse to the 8 neighbouring patches, including the patches on the diagonals.

References _fem_rate, _mal_rate, _npatch, SimComponent::_paramSet, _prefix, allocateDispMatrix(), error(), FEM, SimComponent::get_parameter_value(), ParamSet::isSet(), MAL, message(), and setBasicLatticeMatrix().

Referenced by setDispMatrix().

607 {
608 #ifdef _DEBUG_
609  message("setLatticeMatrix()\n");
610 #endif
611  if(!_paramSet->isSet(_prefix + "_border_model"))
612  return error("Missing parameter \"dispersal_border_model\" with dispersal model 4!\n");
613 
614  if(!_paramSet->isSet(_prefix + "_lattice_range") )
615  return error("Missing parameter \"dispersal_lattice_range\" with dispersal model 4!\n");
616 
617  unsigned int side = (unsigned int) sqrt((double)_npatch);
618 
619  if( side*side != _npatch )
620  return error("The number of patches is not a square number in the lattice dispersal model\n");
621 
624  if((unsigned int)get_parameter_value(_prefix + "_border_model") == 3) {
627  } else {
630  }
631 
635  unsigned int range = (unsigned int)get_parameter_value(_prefix + "_lattice_range");
636  //philopatry:
637  double pmal = 1 - _mal_rate, pfem = 1 - _fem_rate;
638  //migration:
639  double mmal = _mal_rate/(range == 1 ? 4 : 8), mfem = _fem_rate/(range == 1 ? 4 : 8);
640 
641  setBasicLatticeMatrix(side, pmal, pfem, mmal, mfem);
642 
643 
644  return true;
645 }
void message(const char *message,...)
Definition: output.cc:40
virtual double get_parameter_value(std::string name)
Param value getter.
Definition: simcomponent.h:143
double _fem_rate
Definition: LCEdisperse.h:45
unsigned int _npatch
Definition: LCEdisperse.h:60
int error(const char *str,...)
Definition: output.cc:73
double _mal_rate
Definition: LCEdisperse.h:45
bool setBasicLatticeMatrix(int side, double phi_mal, double phi_fem, double disp_mal, double disp_fem)
Definition: LCEdisperse.cc:649
void allocateDispMatrix(sex_t sex, unsigned int dim)
Definition: LCEdisperse.cc:305
ParamSet * _paramSet
The parameters container.
Definition: simcomponent.h:48
Definition: types.h:37
bool isSet()
Accessor to the status flag.
Definition: param.h:273
Definition: types.h:37
string _prefix
Definition: LCEdisperse.h:54
bool LCE_Disperse_base::setLatticeReflectingMatrix ( int  side,
TMatrix grid 
)

References _DispMatrix, _isForward, _npatch, FEM, TMatrix::get(), MAL, TMatrix::rowSum(), TMatrix::set(), and TMatrix::transpose().

Referenced by setBasicLatticeMatrix().

875 {
876  TMatrix* mmat = _DispMatrix[MAL];
877  TMatrix* fmat = _DispMatrix[FEM];
878 
879 // double dm, df;
880 // unsigned int range = (unsigned int)get_parameter_value(_prefix + "_lattice_range");
881 
882  vector<unsigned int> border_cells;
883 
884  for (unsigned int i = 0; i < _npatch; ++i) {
885 
886  for (int j = 0; j < side; ++j)
887  border_cells.push_back( j ); //first row
888 
889  for (int j = 1; j < side - 1; ++j) { //intermediate rows, only the two border cells
890  border_cells.push_back( j*side );
891  border_cells.push_back( (j + 1)*side - 1 );
892  }
893 
894  for (unsigned int j = side*(side-1); j < _npatch; ++j)
895  border_cells.push_back( j ); //last row
896 
897  }
898 
899  double sum;
900  // individuals who can't disperse past the border stay in place:
901  for (unsigned int i = 0; i < border_cells.size(); ++i) {
902  //sum of dispersal rates:
903  sum = fmat->rowSum( border_cells[i] ) - fmat->get(border_cells[i], border_cells[i]);
904  //difference gives 1 - m:
905  fmat->set(border_cells[i], border_cells[i], 1 - sum);
906 
907  sum = mmat->rowSum( border_cells[i] ) - mmat->get(border_cells[i], border_cells[i]);
908  mmat->set(border_cells[i], border_cells[i], 1 - sum);
909  }
910 
911  if(!_isForward) { fmat->transpose(); mmat->transpose(); }
912 
913  return true;
914 }
TMatrix * _DispMatrix[2]
The sex-specific dispersal matrices, [0] for males, [1] for females, might be used as connectivity ma...
Definition: LCEdisperse.h:49
double get(unsigned int i, unsigned int j)
Accessor to element at row i and column j.
Definition: tmatrix.h:120
unsigned int _npatch
Definition: LCEdisperse.h:60
double rowSum(unsigned int row)
Sum all elements in a row.
Definition: tmatrix.h:236
void set(unsigned int i, unsigned int j, double val)
Sets element at row i and column j to value val.
Definition: tmatrix.h:85
void transpose()
Transpose the matrix, swaps columns for rows.
Definition: tmatrix.h:215
A class to handle matrix in params, coerces matrix into a vector of same total size.
Definition: tmatrix.h:49
Definition: types.h:37
bool _isForward
Definition: LCEdisperse.h:46
Definition: types.h:37
bool LCE_Disperse_base::setLatticeTorrusMatrix ( int  side,
double  disp_mal,
double  disp_fem,
TMatrix grid 
)

References _DispMatrix, _prefix, FEM, TMatrix::get(), SimComponent::get_parameter_value(), MAL, and TMatrix::set().

Referenced by setBasicLatticeMatrix().

765 {
766  TMatrix* mmat = _DispMatrix[MAL];
767  TMatrix* fmat = _DispMatrix[FEM];
768 
769  int x, y;
770 
771  x = 0; //first row, connect with upper row
772  for (y = 0; y < side; ++y) {
773  fmat->set( grid->get(x, y), grid->get( side-1, y ), disp_fem);
774  mmat->set( grid->get(x, y), grid->get( side-1, y ), disp_mal);
775  }
776 
777  x = side - 1; //last row, connect with bottom row
778  for (y = 0; y < side; ++y) {
779  fmat->set( grid->get(x, y), grid->get( 0, y ), disp_fem);
780  mmat->set( grid->get(x, y), grid->get( 0, y ), disp_mal);
781  }
782 
783  y = 0; //left column, connect with right-most column
784  for (x = 0; x < side; ++x) {
785  fmat->set( grid->get(x, y), grid->get( x, side - 1 ), disp_fem);
786  mmat->set( grid->get(x, y), grid->get( x, side - 1 ), disp_mal);
787  }
788 
789  y = side - 1; //right-most column, connect with left-most column
790  for (x = 0; x < side; ++x) {
791  fmat->set( grid->get(x, y), grid->get( x, 0 ), disp_fem);
792  mmat->set( grid->get(x, y), grid->get( x, 0 ), disp_mal);
793  }
794 
795  //connect along diagonals
796 
797  if((int)get_parameter_value(_prefix + "_lattice_range") == 2) {
798 
799 
800  //CORNERS:
801 
802  //first patch on first row, wrap around to top right border (last patch)
803  fmat->set( grid->get(0, 0), grid->get( side - 1, side - 1 ), disp_fem);
804  mmat->set( grid->get(0, 0), grid->get( side - 1, side - 1 ), disp_mal);
805  //last patch to first patch
806  fmat->set( grid->get( side - 1, side - 1 ), grid->get(0, 0), disp_fem);
807  mmat->set( grid->get( side - 1, side - 1 ), grid->get(0, 0), disp_mal);
808 
809  //last patch on first row, wrap around to top left border
810  fmat->set( grid->get(0, side - 1), grid->get( side - 1, 0 ), disp_fem);
811  mmat->set( grid->get(0, side - 1), grid->get( side - 1, 0 ), disp_mal);
812  //third corner to second corner
813  fmat->set( grid->get( side - 1, 0 ), grid->get(0, side - 1), disp_fem);
814  mmat->set( grid->get( side - 1, 0 ), grid->get(0, side - 1), disp_mal);
815 
816 
817  //BORDERS:
818 
819  x = 0;//we are on the first row
820 
821  for (y = 0; y < side - 1; ++y) {
822  //diagonal step to the right, move to the other side of the grid
823  fmat->set( grid->get(x, y), grid->get( side-1, y + 1 ), disp_fem);
824  mmat->set( grid->get(x, y), grid->get( side-1, y + 1 ), disp_mal);
825  }
826  for (y = 1; y < side; ++y) {
827  //diagonal step to the left, move to the other side of the grid
828  fmat->set( grid->get(x, y), grid->get( side-1, y - 1 ), disp_fem);
829  mmat->set( grid->get(x, y), grid->get( side-1, y - 1 ), disp_mal);
830  }
831 
832  x = side - 1; //last row
833 
834  for (y = 0; y < side - 1; ++y) {
835  //diagonal step to the right, move to the first row
836  fmat->set( grid->get(x, y), grid->get( 0, y + 1 ), disp_fem);
837  mmat->set( grid->get(x, y), grid->get( 0, y + 1 ), disp_mal);
838  }
839  for (y = 1; y < side; ++y) {
840  //diagonal step to the left, move to the first row
841  fmat->set( grid->get(x, y), grid->get( 0, y - 1 ), disp_fem);
842  mmat->set( grid->get(x, y), grid->get( 0, y - 1 ), disp_mal);
843  }
844 
845  //in-between rows, connect first and last columns
846  for (x = 0; x < side; ++x) {
847 
848  if(x > 0) {
849  //diagonal step down, to previous row
850  y = 0; //first colum
851  fmat->set( grid->get(x, y), grid->get( x - 1, side - 1 ), disp_fem);
852  mmat->set( grid->get(x, y), grid->get( x - 1, side - 1 ), disp_mal);
853  y = side - 1; //last colum
854  fmat->set( grid->get(x, y), grid->get( x - 1, 0 ), disp_fem);
855  mmat->set( grid->get(x, y), grid->get( x - 1, 0 ), disp_mal);
856  }
857  if(x < side - 1){
858  //diagonal step up, to next row
859  y = 0;
860  fmat->set( grid->get(x, y), grid->get( x + 1, side - 1 ), disp_fem);
861  mmat->set( grid->get(x, y), grid->get( x + 1, side - 1 ), disp_mal);
862  y = side - 1;
863  fmat->set( grid->get(x, y), grid->get( x + 1, 0 ), disp_fem);
864  mmat->set( grid->get(x, y), grid->get( x + 1, 0 ), disp_mal);
865  }
866  }
867  }
868 //the matrix is symmetrical no need to transpose for backward migration
869  return true;
870 }
virtual double get_parameter_value(std::string name)
Param value getter.
Definition: simcomponent.h:143
TMatrix * _DispMatrix[2]
The sex-specific dispersal matrices, [0] for males, [1] for females, might be used as connectivity ma...
Definition: LCEdisperse.h:49
double get(unsigned int i, unsigned int j)
Accessor to element at row i and column j.
Definition: tmatrix.h:120
void set(unsigned int i, unsigned int j, double val)
Sets element at row i and column j to value val.
Definition: tmatrix.h:85
A class to handle matrix in params, coerces matrix into a vector of same total size.
Definition: tmatrix.h:49
Definition: types.h:37
Definition: types.h:37
string _prefix
Definition: LCEdisperse.h:54
void LCE_Disperse_base::setParamPrefix ( string  pref)
inline
70 {_prefix = pref;}
string _prefix
Definition: LCEdisperse.h:54
void LCE_Disperse_base::setPropaguleTargets ( )

References LifeCycleEvent::_popPtr, _PropaguleTargets, Metapop::getPatchNbr(), and RAND::Uniform().

Referenced by LCE_Disperse_EvolDisp::execute(), and setIsland_PropagulePool_Matrix().

353 {
354  unsigned int nb_patch = _popPtr->getPatchNbr();
355  unsigned int tmp_array[nb_patch];
356  unsigned int table_index, target_patch;
357  unsigned int size, last;
358 
359  //shuffling algorithm:
360  do {
361  for(unsigned int i = 0; i < nb_patch; ++i)
362  tmp_array[i] = i;
363 
364  size = nb_patch;
365 
366  for(unsigned int orig_patch = 0; orig_patch < nb_patch-1; ++orig_patch) {
367 
368  do{
369 
370  table_index = RAND::Uniform( size );
371 
372  target_patch = tmp_array[ table_index ];
373 
374  }while(target_patch == orig_patch);
375 
376  size--;
377 
378  last = tmp_array[size];
379 
380  tmp_array[table_index] = last;
381 
382  tmp_array[size] = target_patch;
383  }
384  //do this until the last element left is not the last patch:
385  }while (tmp_array[0] == nb_patch-1);
386 
387  _PropaguleTargets.assign(nb_patch,0);
388 
389  unsigned int reverse_i = nb_patch;
390 
391  //we read the shuffled array in reverse order:
392  for(unsigned int i=0; i < _PropaguleTargets.size(); i++) {
393  _PropaguleTargets[i] = tmp_array[--reverse_i];
394 
395 #ifdef _DEBUG_
396  cout<<" -- Patch "<<i<<" : assigned Patch "<<_PropaguleTargets[i]<<endl;
397 #endif
398 
399  }
400 }
vector< unsigned int > _PropaguleTargets
Definition: LCEdisperse.h:44
unsigned int getPatchNbr()
Definition: metapop.h:270
static double Uniform()
Generates a random number from [0.0, 1.0[ uniformly distributed.
Definition: Uniform.h:101
Metapop * _popPtr
The ptr to the current Metapop.
Definition: lifecycleevent.h:81
bool LCE_Disperse_base::setReducedDispMatrix ( )

The reduced dispersal matrix contains the indices of the patches to which each patch is connected.

The connected patches are further ordered in descending order of the migration rates. The migration rates are stored in a reduced dispersal matrix. The original dispersal matrices are cleared (de-allocated). This offers a double speed-up compared to the classical method.

References _DispMatrix, _isForward, _npatch, _prefix, _reducedDispMat, _reducedDispMatProba, FEM, SimComponent::get_parameter_value(), MAL, message(), and TMatrix::show_up().

Referenced by setBaseParameters(), and setDispMatrix().

977 {
978  unsigned int border_model = (unsigned int)get_parameter_value(_prefix + "_border_model");
979  unsigned int num_patch = (border_model == 3 ? _npatch + 1 : _npatch);
980 
981  //multimap automatically orders the key values in ascending order
982  multimap<double, unsigned int> ordered_rates_fem, ordered_rates_mal;
983 
984  typedef multimap<double, unsigned int>::const_iterator CI;
985 
986 #ifdef _DEBUG_
987  message("== Dispersal matrices ==\n");
988  _DispMatrix[FEM]->show_up();
989  _DispMatrix[MAL]->show_up();
990 #endif
991 
992  // purge the connectivity and reduced dipsersal matrices
993  for (unsigned int sex = 0; sex < 2; sex++) {
994  _reducedDispMat[sex].clear();
995  _reducedDispMatProba[sex].clear();
996  }
997  // build them from full dispersal matrices given in input
998  for (unsigned int i = 0; i < num_patch; ++i) {
999 
1000  _reducedDispMat[0].push_back(vector<double>());
1001  _reducedDispMat[1].push_back(vector<double>());
1002 
1003  _reducedDispMatProba[0].push_back(vector<double>());
1004  _reducedDispMatProba[1].push_back(vector<double>());
1005 
1006  ordered_rates_fem.clear();
1007  ordered_rates_mal.clear();
1008 
1009  if(_isForward) {
1010  //make pairs: {disp proba, patch connected}, will be ordered by dispersal probabilities:
1011  for (unsigned int j = 0; j < num_patch; ++j)
1012  if(_DispMatrix[0]->get(i, j) != 0) ordered_rates_mal.insert(make_pair(_DispMatrix[0]->get(i, j), j));
1013 
1014 
1015  for (unsigned int j = 0; j < num_patch; ++j)
1016  if(_DispMatrix[1]->get(i, j) != 0) ordered_rates_fem.insert(make_pair(_DispMatrix[1]->get(i, j),j));
1017 
1018 
1019  } else {
1020  //backward migration matrices are read column-wise
1021  for (unsigned int j = 0; j < num_patch; ++j)
1022  if(_DispMatrix[0]->get(j, i) != 0) ordered_rates_mal.insert(make_pair(_DispMatrix[0]->get(j, i),j));
1023 
1024  for (unsigned int j = 0; j < num_patch; ++j)
1025  if(_DispMatrix[1]->get(j, i) != 0) ordered_rates_fem.insert(make_pair(_DispMatrix[1]->get(j, i),j));
1026  }
1027 
1028  if(ordered_rates_fem.size() == 1) {
1029 
1030  _reducedDispMat[1][i].push_back(ordered_rates_fem.begin()->second);
1031 
1032  _reducedDispMatProba[1][i].push_back(ordered_rates_fem.begin()->first);
1033 
1034  } else {
1035 
1036  //store the patch indices in reverse order of the migration rates:
1037  //store the dispersal rates as well in a separate reduced matrix
1038  CI p;
1039 
1040  for (p = --ordered_rates_fem.end(); p != --ordered_rates_fem.begin(); --p) {
1041  _reducedDispMat[1][i].push_back(p->second);
1042  _reducedDispMatProba[1][i].push_back(p->first);
1043  }
1044 
1045  //bug fix for the case of 2 patches only (for some reason the second patch recieves only one value...???)
1046  if(p == ordered_rates_fem.begin() && (_reducedDispMat[1][i].size() < ordered_rates_fem.size()) ) {
1047  _reducedDispMat[1][i].push_back(p->second);
1048  _reducedDispMatProba[1][i].push_back(p->first);
1049  }
1050  }
1051 
1052  //same for the male dispersal matrices
1053  if(ordered_rates_mal.size() == 1) {
1054  _reducedDispMat[0][i].push_back(ordered_rates_mal.begin()->second);
1055  _reducedDispMatProba[0][i].push_back(ordered_rates_fem.begin()->first);
1056  } else {
1057  CI p;
1058  for (p = --ordered_rates_mal.end(); p != --ordered_rates_mal.begin(); --p) {
1059  _reducedDispMat[0][i].push_back(p->second);
1060  _reducedDispMatProba[0][i].push_back(p->first);
1061  }
1062 
1063  if(p == ordered_rates_mal.begin() && (_reducedDispMat[0][i].size() < ordered_rates_mal.size()) ) {
1064  _reducedDispMat[0][i].push_back(p->second);
1065  _reducedDispMatProba[0][i].push_back(p->first);
1066  }
1067  }
1068  }
1069 
1070  //we can now get rid of the dispersal matrices...
1071  for (unsigned int sex = 0; sex < 2; sex++) {
1072  delete _DispMatrix[sex];
1073  _DispMatrix[sex] = NULL;
1074  }
1075 
1076 #ifdef _DEBUG_
1077  cout << "=== female reduced dispersal matrix ===\n";
1078  for (unsigned int i = 0; i < num_patch; ++i) {
1079  cout << " [";
1080  for (unsigned int k = 0; k < _reducedDispMat[FEM][i].size(); k++) {
1081  cout << _reducedDispMatProba[FEM][i][k] << " [" << _reducedDispMat[FEM][i][k] <<"] ";
1082  }
1083  cout<<"]\n";
1084  }
1085 
1086  cout << "=== male reduced dispersal matrix ===\n";
1087  for (unsigned int i = 0; i < num_patch; ++i) {
1088  cout << " [";
1089  for (unsigned int k = 0; k < _reducedDispMat[MAL][i].size(); k++) {
1090  cout << _reducedDispMatProba[MAL][i][k] << " [" << _reducedDispMat[MAL][i][k] <<"] ";
1091  }
1092  cout<<"]\n";
1093 
1094  }
1095 #endif
1096 
1097  return true;
1098 }
void message(const char *message,...)
Definition: output.cc:40
virtual double get_parameter_value(std::string name)
Param value getter.
Definition: simcomponent.h:143
TMatrix * _DispMatrix[2]
The sex-specific dispersal matrices, [0] for males, [1] for females, might be used as connectivity ma...
Definition: LCEdisperse.h:49
unsigned int _npatch
Definition: LCEdisperse.h:60
void show_up()
Definition: tmatrix.h:246
Definition: types.h:37
vector< vector< double > > _reducedDispMat[2]
Definition: LCEdisperse.h:51
bool _isForward
Definition: LCEdisperse.h:46
Definition: types.h:37
vector< vector< double > > _reducedDispMatProba[2]
Definition: LCEdisperse.h:52
string _prefix
Definition: LCEdisperse.h:54
bool LCE_Disperse_base::setSteppingStone1DMatrix ( )

References _DispMatrix, _fem_rate, _isForward, _mal_rate, _npatch, _prefix, allocateDispMatrix(), TMatrix::assign(), FEM, SimComponent::get_parameter_value(), MAL, message(), TMatrix::set(), and TMatrix::transpose().

Referenced by setDispMatrix().

520 {
521 #ifdef _DEBUG_
522  message("setSteppingStone1DMatrix()\n");
523 #endif
524  int border_model = (unsigned int)get_parameter_value(_prefix + "_border_model");
525 
526  //check for the border model, the extra patch is the absorbing patch
527  if(border_model == 3) {
530  } else {
533  }
534 
535  TMatrix* mmat = _DispMatrix[MAL];
536  TMatrix* fmat = _DispMatrix[FEM];
537 
538  //philopatry:
539  double pmal = 1 - _mal_rate, pfem = 1 - _fem_rate;
540  //migration:
541  double mmal = _mal_rate/2, mfem = _fem_rate/2;
542 
543  fmat->assign(0);
544  mmat->assign(0);
545 
546  //diagonal:
547  for (unsigned int i = 0; i < _npatch; ++i){
548  fmat->set(i, i, pfem);
549  mmat->set(i, i, pmal);
550  }
551  //around the diagonal
552  for (unsigned int i = 0; i < _npatch-1; ++i){
553  fmat->set(i, i+1, mfem);
554  fmat->set(i+1, i, mfem);
555  mmat->set(i, i+1, mmal);
556  mmat->set(i+1, i, mmal);
557  }
558 
559  if(border_model == 3) {
560  //absorbing boders
561  //emigrants who leave the population are put in the sink patch
562  fmat->set(0, _npatch, mfem);
563  mmat->set(0, _npatch, mmal);
564  fmat->set(_npatch -1, _npatch, mfem);
565  mmat->set(_npatch -1, _npatch, mmal);
566  fmat->set(_npatch, _npatch, 1);
567  mmat->set(_npatch, _npatch, 1);
568 
569  if(!_isForward) {
570  fmat->transpose(); mmat->transpose(); //this creates artificial immigration from sink
571  //need to reset border patches as immigration from sink not allowed:
572  fmat->set(0, 0, pfem + mfem); //the proportion not comming from the sink must be added
573  fmat->set(_npatch, 0, 0); //no immigration from sink
574  fmat->set(_npatch-1, _npatch-1, pfem + mfem);
575  fmat->set(_npatch, _npatch-1, 0);
576 
577  mmat->set(0, 0, pmal + mmal);
578  mmat->set(_npatch, 0, 0);
579  mmat->set(_npatch-1, _npatch-1, pmal + mmal);
580  mmat->set(_npatch, _npatch-1, 0);
581  }
582 
583  } else if (border_model == 2) {
584  //reflective borders,
585  //emigrants that cannot leave stay in place
586  fmat->set(0, 0, pfem+mfem);
587  mmat->set(0, 0, pmal+mmal);
588  fmat->set(_npatch -1, _npatch -1, pfem+mfem);
589  mmat->set(_npatch -1, _npatch -1, pmal+mmal);
590 
591  //no need to transpose for backward migration as the matrix is symmetrical
592 
593  } else { //is a torus by default
594  //the 2 last elements, this is a ring population!
595  fmat->set(0, _npatch -1, mfem);
596  mmat->set(0, _npatch -1, mmal);
597  fmat->set(_npatch -1, 0, mfem);
598  mmat->set(_npatch -1, 0, mmal);
599  }
600  return true;
601 }
void message(const char *message,...)
Definition: output.cc:40
virtual double get_parameter_value(std::string name)
Param value getter.
Definition: simcomponent.h:143
void assign(double val)
Assigns a value to all element of the matrix.
Definition: tmatrix.h:92
double _fem_rate
Definition: LCEdisperse.h:45
TMatrix * _DispMatrix[2]
The sex-specific dispersal matrices, [0] for males, [1] for females, might be used as connectivity ma...
Definition: LCEdisperse.h:49
unsigned int _npatch
Definition: LCEdisperse.h:60
void set(unsigned int i, unsigned int j, double val)
Sets element at row i and column j to value val.
Definition: tmatrix.h:85
void transpose()
Transpose the matrix, swaps columns for rows.
Definition: tmatrix.h:215
double _mal_rate
Definition: LCEdisperse.h:45
A class to handle matrix in params, coerces matrix into a vector of same total size.
Definition: tmatrix.h:49
void allocateDispMatrix(sex_t sex, unsigned int dim)
Definition: LCEdisperse.cc:305
Definition: types.h:37
bool _isForward
Definition: LCEdisperse.h:46
Definition: types.h:37
string _prefix
Definition: LCEdisperse.h:54
void LCE_Disperse_base::swapPostDisp ( )

References _npatch, LifeCycleEvent::_popPtr, FEM, Metapop::getPatch(), MAL, OFFSx, PDISPx, and Patch::swap().

Referenced by LCE_Disperse_EvolDisp::execute(), and LCE_Disperse_ConstDisp::Migrate().

278 {
279  Patch *patch;
280 
281  for(unsigned int i = 0; i < _npatch; i++) {
282  patch = _popPtr->getPatch(i);
283  patch->swap(FEM, PDISPx, OFFSx);
284  patch->swap(MAL, PDISPx, OFFSx);
285  }
286 }
Second class in the metapopulation design structure, between the Metapop and Individual classes...
Definition: metapop.h:421
Patch * getPatch(unsigned int i)
Patch accessor, return the ith+1 patch in the metapop.
Definition: metapop.h:251
unsigned int _npatch
Definition: LCEdisperse.h:60
Definition: types.h:37
Definition: types.h:42
void swap(sex_t SEX, age_idx from, age_idx to)
Copies all elements in the 'from' age-class container to the 'to' age-class container of the same sex...
Definition: metapop.h:600
Metapop * _popPtr
The ptr to the current Metapop.
Definition: lifecycleevent.h:81
Definition: types.h:37
Definition: types.h:42
bool LCE_Disperse_base::updateDispMatrix ( )

References error(), getDispersalModel(), and setDispMatrix().

Referenced by LCE_Disperse_ConstDisp::execute(), LCE_Breed_Disperse::execute(), and LCE_Breed_Selection_Disperse::execute().

266 {
267  //called by the 'execute' function when a change in patch number is detected
268 
269  if ( getDispersalModel() == 0 )
270  return error("cannot update the dispersal matrix provided in input when number of populations changes.\n");
271 
272  return setDispMatrix();
273 }
unsigned int getDispersalModel()
Definition: LCEdisperse.h:98
int error(const char *str,...)
Definition: output.cc:73
bool setDispMatrix()
Definition: LCEdisperse.cc:404

Friends And Related Function Documentation

friend class LCE_Disperse_ConstDisp
friend
friend class LCE_Disperse_EvolDisp
friend

Member Data Documentation

int LCE_Disperse_base::_disp_model
private
double LCE_Disperse_base::_disp_propagule_prob
private
vector<unsigned int> LCE_Disperse_base::_PropaguleTargets
private

Referenced by setPropaguleTargets().

vector< vector<double> > LCE_Disperse_base::_reducedDispMat[2]
private
vector< vector<double> > LCE_Disperse_base::_reducedDispMatProba[2]
private

The documentation for this class was generated from the following files:

Generated for Nemo v2.3.0 by  doxygen 1.8.8 --
Catalogued on GSR