-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPopulation.cpp
1673 lines (1551 loc) · 50 KB
/
Population.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*----------------------------------------------------------------------------
*
* Copyright (C) 2020 Greta Bocedi, Stephen C.F. Palmer, Justin M.J. Travis, Anne-Kathleen Malchow, Damaris Zurell
*
* This file is part of RangeShifter.
*
* RangeShifter is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* RangeShifter is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with RangeShifter. If not, see <https://www.gnu.org/licenses/>.
*
--------------------------------------------------------------------------*/
//---------------------------------------------------------------------------
#include "Population.h"
//---------------------------------------------------------------------------
ofstream outPop;
ofstream outInds;
//---------------------------------------------------------------------------
Population::Population(void) {
nSexes = nStages = 0;
pPatch = NULL;
pSpecies = NULL;
return;
}
Population::Population(Species* pSp, Patch* pPch, int ninds, int resol)
{
// constructor for a Population of a specified size
int n, nindivs, age = 0, minage, maxage, nAges = 0;
int cumtotal = 0;
float probmale;
double ageprob, ageprobsum;
std::vector <double> ageProb; // for quasi-equilibrium initial age distribution
Cell* pCell;
if (ninds > 0) {
inds.reserve(ninds);
juvs.reserve(ninds);
}
pSpecies = pSp;
pPatch = pPch;
// record the new population in the patch
patchPopn pp;
pp.pSp = (intptr)pSpecies; pp.pPop = (intptr)this;
pPatch->addPopn(pp);
demogrParams dem = pSpecies->getDemogr();
stageParams sstruct = pSpecies->getStage();
emigRules emig = pSpecies->getEmig();
trfrRules trfr = pSpecies->getTrfr();
settleType sett = pSpecies->getSettle();
genomeData gen = pSpecies->getGenomeData();
initParams init = paramsInit->getInit();
// determine no. of stages and sexes of species to initialise
if (dem.stageStruct) {
nStages = sstruct.nStages;
}
else // non-structured population has 2 stages, but user only ever sees stage 1
nStages = 2;
if (dem.repType == 0) { nSexes = 1; probmale = 0.0; }
else { nSexes = 2; probmale = dem.propMales; }
// set up population sub-totals
for (int stg = 0; stg < NSTAGES; stg++) {
for (int sex = 0; sex < NSEXES; sex++) {
nInds[stg][sex] = 0;
}
}
// set up local copy of minimum age table
short minAge[NSTAGES][NSEXES];
for (int stg = 0; stg < nStages; stg++) {
for (int sex = 0; sex < nSexes; sex++) {
if (dem.stageStruct) {
if (dem.repType == 1) { // simple sexual model
// both sexes use minimum ages recorded for females
minAge[stg][sex] = pSpecies->getMinAge(stg, 0);
}
else {
minAge[stg][sex] = pSpecies->getMinAge(stg, sex);
}
}
else { // non-structured population
minAge[stg][sex] = 0;
}
}
}
// individuals of new population must be >= stage 1
for (int stg = 1; stg < nStages; stg++) {
if (dem.stageStruct) { // allocate to stages according to initialisation conditions
// final stage is treated separately to ensure that correct total
// no. of individuals is created
if (stg == nStages - 1) {
n = ninds - cumtotal;
}
else {
n = (int)(ninds * paramsInit->getProp(stg) + 0.5);
cumtotal += n;
}
}
else { // non-structured - all individuals go into stage 1
n = ninds;
}
// establish initial age distribution
minage = maxage = stg;
if (dem.stageStruct) {
// allow for stage-dependent minimum ages (use whichever sex is greater)
if (minAge[stg][0] > 0 && minage < minAge[stg][0]) minage = minAge[stg][0];
if (nSexes == 2 && minAge[stg][1] > 0 && minage < minAge[stg][1]) minage = minAge[stg][1];
// allow for specified age distribution
if (init.initAge != 0) { // not lowest age
if (stg == nStages - 1) maxage = sstruct.maxAge; // final stage
else { // all other stages - use female max age, as sex of individuals is not predetermined
maxage = minAge[stg + 1][0] - 1;
}
if (maxage < minage) maxage = minage;
nAges = maxage - minage + 1;
if (init.initAge == 2) { // quasi-equilibrium distribution
double psurv = (double)pSpecies->getSurv(stg, 0); // use female survival for the stage
ageProb.clear();
ageprobsum = 0.0;
ageprob = 1.0;
for (int i = 0; i < nAges; i++) {
ageProb.push_back(ageprob); ageprobsum += ageprob; ageprob *= psurv;
}
for (int i = 0; i < nAges; i++) {
ageProb[i] /= ageprobsum;
if (i > 0) ageProb[i] += ageProb[i - 1]; // to give cumulative probability
}
}
}
}
// create individuals
int sex;
nindivs = (int)inds.size();
for (int i = 0; i < n; i++) {
pCell = pPatch->getRandomCell();
if (dem.stageStruct) {
switch (init.initAge) {
case 0: // lowest possible age
age = minage;
break;
case 1: // randomised
if (maxage > minage) age = pRandom->IRandom(minage, maxage);
else age = minage;
break;
case 2: // quasi-equilibrium
if (nAges > 1) {
double rrr = pRandom->Random();
int ageclass = 0;
while (rrr > ageProb[ageclass]) ageclass++;
age = minage + ageclass;
}
else age = minage;
break;
}
}
else age = stg;
#if RSDEBUG
// NOTE: CURRENTLY SETTING ALL INDIVIDUALS TO RECORD NO. OF STEPS ...
inds.push_back(new Individual(pCell, pPatch, stg, age, sstruct.repInterval,
probmale, true, trfr.moveType));
#else
inds.push_back(new Individual(pCell, pPatch, stg, age, sstruct.repInterval,
probmale, trfr.moveModel, trfr.moveType));
#endif
sex = inds[nindivs + i]->getSex();
if (emig.indVar || trfr.indVar || sett.indVar || gen.neutralMarkers)
{
// individual variation - set up genetics
inds[nindivs + i]->setGenes(pSpecies, resol);
}
nInds[stg][sex]++;
}
}
}
Population::~Population(void) {
int ninds = (int)inds.size();
for (int i = 0; i < ninds; i++) {
if (inds[i] != NULL) delete inds[i];
}
inds.clear();
int njuvs = (int)juvs.size();
for (int i = 0; i < njuvs; i++) {
if (juvs[i] != NULL) delete juvs[i];
}
juvs.clear();
}
traitsums Population::getTraits(Species* pSpecies) {
int g;
traitsums ts;
for (int i = 0; i < NSEXES; i++) {
ts.ninds[i] = 0;
ts.sumD0[i] = ts.ssqD0[i] = 0.0;
ts.sumAlpha[i] = ts.ssqAlpha[i] = 0.0; ts.sumBeta[i] = ts.ssqBeta[i] = 0.0;
ts.sumDist1[i] = ts.ssqDist1[i] = 0.0; ts.sumDist2[i] = ts.ssqDist2[i] = 0.0;
ts.sumProp1[i] = ts.ssqProp1[i] = 0.0;
ts.sumDP[i] = ts.ssqDP[i] = 0.0;
ts.sumGB[i] = ts.ssqGB[i] = 0.0;
ts.sumAlphaDB[i] = ts.ssqAlphaDB[i] = 0.0;
ts.sumBetaDB[i] = ts.ssqBetaDB[i] = 0.0;
ts.sumStepL[i] = ts.ssqStepL[i] = 0.0; ts.sumRho[i] = ts.ssqRho[i] = 0.0;
ts.sumS0[i] = ts.ssqS0[i] = 0.0;
ts.sumAlphaS[i] = ts.ssqAlphaS[i] = 0.0; ts.sumBetaS[i] = ts.ssqBetaS[i] = 0.0;
}
demogrParams dem = pSpecies->getDemogr();
emigRules emig = pSpecies->getEmig();
trfrRules trfr = pSpecies->getTrfr();
settleType sett = pSpecies->getSettle();
int ninds = (int)inds.size();
for (int i = 0; i < ninds; i++) {
int sex = inds[i]->getSex();
if (emig.sexDep || trfr.sexDep || sett.sexDep) g = sex; else g = 0;
ts.ninds[g] += 1;
// emigration traits
emigTraits e = inds[i]->getEmigTraits();
if (emig.sexDep) g = sex; else g = 0;
ts.sumD0[g] += e.d0; ts.ssqD0[g] += e.d0 * e.d0;
ts.sumAlpha[g] += e.alpha; ts.ssqAlpha[g] += e.alpha * e.alpha;
ts.sumBeta[g] += e.beta; ts.ssqBeta[g] += e.beta * e.beta;
// transfer traits
trfrKernTraits k = inds[i]->getKernTraits();
if (trfr.sexDep) g = sex; else g = 0;
ts.sumDist1[g] += k.meanDist1; ts.ssqDist1[g] += k.meanDist1 * k.meanDist1;
ts.sumDist2[g] += k.meanDist2; ts.ssqDist2[g] += k.meanDist2 * k.meanDist2;
ts.sumProp1[g] += k.probKern1; ts.ssqProp1[g] += k.probKern1 * k.probKern1;
trfrSMSTraits sms = inds[i]->getSMSTraits();
g = 0; // CURRENTLY INDIVIDUAL VARIATION CANNOT BE SEX-DEPENDENT
ts.sumDP[g] += sms.dp; ts.ssqDP[g] += sms.dp * sms.dp;
ts.sumGB[g] += sms.gb; ts.ssqGB[g] += sms.gb * sms.gb;
ts.sumAlphaDB[g] += sms.alphaDB; ts.ssqAlphaDB[g] += sms.alphaDB * sms.alphaDB;
ts.sumBetaDB[g] += sms.betaDB; ts.ssqBetaDB[g] += sms.betaDB * sms.betaDB;
trfrCRWTraits c = inds[i]->getCRWTraits();
g = 0; // CURRENTLY INDIVIDUAL VARIATION CANNOT BE SEX-DEPENDENT
ts.sumStepL[g] += c.stepLength; ts.ssqStepL[g] += c.stepLength * c.stepLength;
ts.sumRho[g] += c.rho; ts.ssqRho[g] += c.rho * c.rho;
// settlement traits
settleTraits s = inds[i]->getSettTraits();
if (sett.sexDep) g = sex; else g = 0;
ts.sumS0[g] += s.s0; ts.ssqS0[g] += s.s0 * s.s0;
ts.sumAlphaS[g] += s.alpha; ts.ssqAlphaS[g] += s.alpha * s.alpha;
ts.sumBetaS[g] += s.beta; ts.ssqBetaS[g] += s.beta * s.beta;
}
return ts;
}
int Population::getNInds(void) { return (int)inds.size(); }
popStats Population::getStats(void)
{
popStats p;
int ninds;
float fec;
bool breeders[2]; breeders[0] = breeders[1] = false;
demogrParams dem = pSpecies->getDemogr();
p.pSpecies = pSpecies;
p.pPatch = pPatch;
p.spNum = pSpecies->getSpNum();
p.nInds = (int)inds.size();
p.nNonJuvs = p.nAdults = 0;
p.breeding = false;
for (int stg = 1; stg < nStages; stg++) {
for (int sex = 0; sex < nSexes; sex++) {
ninds = nInds[stg][sex];
p.nNonJuvs += ninds;
if (ninds > 0) {
if (pSpecies->stageStructured()) {
if (dem.repType == 2) fec = pSpecies->getFec(stg, sex);
else fec = pSpecies->getFec(stg, 0);
if (fec > 0.0) { breeders[sex] = true; p.nAdults += ninds; }
}
else breeders[sex] = true;
}
}
}
// is there a breeding population present?
if (nSexes == 1) {
p.breeding = breeders[0];
}
else {
if (breeders[0] && breeders[1]) p.breeding = true;
}
return p;
}
Species* Population::getSpecies(void) { return pSpecies; }
int Population::totalPop(void) {
int t = 0;
for (int stg = 0; stg < nStages; stg++) {
for (int sex = 0; sex < nSexes; sex++) {
t += nInds[stg][sex];
}
}
return t;
}
int Population::stagePop(int stg) {
int t = 0;
if (stg < 0 || stg >= nStages) return t;
for (int sex = 0; sex < nSexes; sex++) {
t += nInds[stg][sex];
}
return t;
}
//---------------------------------------------------------------------------
// Remove all Individuals
void Population::extirpate(void) {
int ninds = (int)inds.size();
for (int i = 0; i < ninds; i++) {
if (inds[i] != NULL) delete inds[i];
}
inds.clear();
int njuvs = (int)juvs.size();
for (int i = 0; i < njuvs; i++) {
if (juvs[i] != NULL) delete juvs[i];
}
juvs.clear();
for (int sex = 0; sex < nSexes; sex++) {
for (int stg = 0; stg < nStages; stg++) {
nInds[stg][sex] = 0;
}
}
}
//---------------------------------------------------------------------------
// Produce juveniles and hold them in the juvs vector
void Population::reproduction(const float localK, const float envval, const int resol)
{
// get population size at start of reproduction
int ninds = (int)inds.size();
if (ninds == 0) return;
int nsexes, stage, sex, njuvs, nj, nmales, nfemales;
Cell* pCell;
indStats ind;
double expected;
bool skipbreeding;
envStochParams env = paramsStoch->getStoch();
demogrParams dem = pSpecies->getDemogr();
stageParams sstruct = pSpecies->getStage();
emigRules emig = pSpecies->getEmig();
trfrRules trfr = pSpecies->getTrfr();
settleType sett = pSpecies->getSettle();
genomeData gen = pSpecies->getGenomeData();
simView v = paramsSim->getViews();
if (dem.repType == 0) nsexes = 1; else nsexes = 2;
// set up local copy of species fecundity table
float fec[NSTAGES][NSEXES];
for (int stg = 0; stg < sstruct.nStages; stg++) {
for (int sex = 0; sex < nsexes; sex++) {
if (dem.stageStruct) {
if (dem.repType == 1) { // simple sexual model
// both sexes use fecundity recorded for females
fec[stg][sex] = pSpecies->getFec(stg, 0);
}
else fec[stg][sex] = pSpecies->getFec(stg, sex);
}
else { // non-structured population
if (stg == 1) fec[stg][sex] = dem.lambda; // adults
else fec[stg][sex] = 0.0; // juveniles
}
}
}
if (dem.stageStruct) {
// apply environmental effects and density dependence
// to all non-zero female non-juvenile stages
for (int stg = 1; stg < nStages; stg++) {
if (fec[stg][0] > 0.0) {
// apply any effect of environmental gradient and/or stochasticty
fec[stg][0] *= envval;
if (env.stoch && !env.inK) {
// fecundity (at low density) is constrained to lie between limits specified
// for the species
float limit;
limit = pSpecies->getMinMax(0);
if (fec[stg][0] < limit) fec[stg][0] = limit;
limit = pSpecies->getMinMax(1);
if (fec[stg][0] > limit) fec[stg][0] = limit;
}
if (sstruct.fecDens) { // apply density dependence
float effect = 0.0;
if (sstruct.fecStageDens) { // stage-specific density dependence
// NOTE: matrix entries represent effect of ROW on COLUMN
// AND males precede females
float weight = 0.0;
for (int effstg = 0; effstg < nStages; effstg++) {
for (int effsex = 0; effsex < nSexes; effsex++) {
if (dem.repType == 2) {
if (effsex == 0) weight = pSpecies->getDDwtFec(2 * stg + 1, 2 * effstg + 1);
else weight = pSpecies->getDDwtFec(2 * stg + 1, 2 * effstg);
}
else {
weight = pSpecies->getDDwtFec(stg, effstg);
}
effect += (float)nInds[effstg][effsex] * weight;
}
}
}
else // not stage-specific
effect = (float)totalPop();
if (localK > 0.0) fec[stg][0] *= exp(-effect / localK);
}
}
}
}
else { // non-structured - set fecundity for adult females only
// apply any effect of environmental gradient and/or stochasticty
fec[1][0] *= envval;
if (env.stoch && !env.inK) {
// fecundity (at low density) is constrained to lie between limits specified
// for the species
float limit;
limit = pSpecies->getMinMax(0);
if (fec[1][0] < limit) fec[1][0] = limit;
limit = pSpecies->getMinMax(1);
if (fec[1][0] > limit) fec[1][0] = limit;
}
// apply density dependence
if (localK > 0.0) {
if (dem.repType == 1 || dem.repType == 2) { // sexual model
// apply factor of 2 (as in manual, eqn. 6)
fec[1][0] *= 2.0;
}
fec[1][0] /= (1.0f + fabs(dem.lambda - 1.0f) * pow(((float)ninds / localK), dem.bc));
}
}
double propBreed;
Individual* father;
std::vector <Individual*> fathers;
switch (dem.repType) {
case 0: // asexual model
for (int i = 0; i < ninds; i++) {
stage = inds[i]->breedingFem();
if (stage > 0) { // female of breeding age
if (dem.stageStruct) {
// determine whether she must miss current breeding attempt
ind = inds[i]->getStats();
if (ind.fallow >= sstruct.repInterval) {
if (pRandom->Bernoulli(sstruct.probRep)) skipbreeding = false;
else skipbreeding = true;
}
else skipbreeding = true; // cannot breed this time
}
else skipbreeding = false; // not structured - always breed
if (skipbreeding) {
inds[i]->incFallow();
}
else { // attempt to breed
inds[i]->resetFallow();
expected = fec[stage][0];
if (expected <= 0.0) njuvs = 0;
else njuvs = pRandom->Poisson(expected);
nj = (int)juvs.size();
pCell = pPatch->getRandomCell();
for (int j = 0; j < njuvs; j++) {
#if RSDEBUG
// NOTE: CURRENTLY SETTING ALL INDIVIDUALS TO RECORD NO. OF STEPS ...
juvs.push_back(new Individual(pCell, pPatch, 0, 0, 0, 0.0, true, trfr.moveType));
#else
juvs.push_back(new Individual(pCell, pPatch, 0, 0, 0, 0.0, trfr.moveModel, trfr.moveType));
#endif
nInds[0][0]++;
if (emig.indVar || trfr.indVar || sett.indVar || gen.neutralMarkers)
{
// juv inherits genome from parent (mother)
juvs[nj + j]->setGenes(pSpecies, inds[i], 0, resol);
}
}
}
}
}
break;
case 1: // simple sexual model
case 2: // complex sexual model
// count breeding females and males
// add breeding males to list of potential fathers
nfemales = nmales = 0;
for (int i = 0; i < ninds; i++) {
ind = inds[i]->getStats();
if (ind.sex == 0 && fec[ind.stage][0] > 0.0) nfemales++;
if (ind.sex == 1 && fec[ind.stage][1] > 0.0) {
fathers.push_back(inds[i]);
nmales++;
}
}
if (nfemales > 0 && nmales > 0)
{ // population can breed
if (dem.repType == 2) { // complex sexual model
// calculate proportion of eligible females which breed
propBreed = (2.0 * dem.harem * nmales) / (nfemales + dem.harem * nmales);
if (propBreed > 1.0) propBreed = 1.0;
}
else propBreed = 1.0;
for (int i = 0; i < ninds; i++) {
stage = inds[i]->breedingFem();
if (stage > 0 && fec[stage][0] > 0.0) { // (potential) breeding female
if (dem.stageStruct) {
// determine whether she must miss current breeding attempt
ind = inds[i]->getStats();
if (ind.fallow >= sstruct.repInterval) {
if (pRandom->Bernoulli(sstruct.probRep)) skipbreeding = false;
else skipbreeding = true;
}
else skipbreeding = true; // cannot breed this time
}
else skipbreeding = false; // not structured - always breed
if (skipbreeding) {
inds[i]->incFallow();
}
else { // attempt to breed
inds[i]->resetFallow();
// NOTE: FOR COMPLEX SEXUAL MODEL, NO. OF FEMALES *ACTUALLY* BREEDING DOES NOT
// NECESSARILY EQUAL THE EXPECTED NO. FROM EQN. 7 IN THE MANUAL...
if (pRandom->Bernoulli(propBreed)) {
expected = fec[stage][0]; // breeds
}
else expected = 0.0; // fails to breed
if (expected <= 0.0) njuvs = 0;
else njuvs = pRandom->Poisson(expected);
if (njuvs > 0)
{
nj = (int)juvs.size();
// select father at random from breeding males ...
int rrr = 0;
if (nmales > 1) rrr = pRandom->IRandom(0, nmales - 1);
father = fathers[rrr];
pCell = pPatch->getRandomCell();
for (int j = 0; j < njuvs; j++) {
#if RSDEBUG
// NOTE: CURRENTLY SETTING ALL INDIVIDUALS TO RECORD NO. OF STEPS ...
juvs.push_back(new Individual(pCell, pPatch, 0, 0, 0, dem.propMales, true, trfr.moveType));
#else
juvs.push_back(new Individual(pCell, pPatch, 0, 0, 0, dem.propMales, trfr.moveModel, trfr.moveType));
#endif
sex = juvs[nj + j]->getSex();
nInds[0][sex]++;
if (emig.indVar || trfr.indVar || sett.indVar || gen.neutralMarkers)
{
// juv inherits genome from parents
juvs[nj + j]->setGenes(pSpecies, inds[i], father, resol);
}
}
}
}
}
}
}
fathers.clear();
break;
} // end of switch (dem.repType)
// THIS MAY NOT BE CORRECT FOR MULTIPLE SPECIES IF THERE IS SOME FORM OF
// CROSS-SPECIES DENSITY-DEPENDENT FECUNDITY
}
// Following reproduction of ALL species, add juveniles to the population prior to dispersal
void Population::fledge(void)
{
demogrParams dem = pSpecies->getDemogr();
if (dem.stageStruct) { // juveniles are added to the individuals vector
inds.insert(inds.end(), juvs.begin(), juvs.end());
}
else { // all adults die and juveniles replace adults
int ninds = (int)inds.size();
for (int i = 0; i < ninds; i++) {
delete inds[i];
}
inds.clear();
for (int sex = 0; sex < nSexes; sex++) {
nInds[1][sex] = 0; // set count of adults to zero
}
inds = juvs;
}
juvs.clear();
}
// Determine which individuals will disperse
void Population::emigration(float localK)
{
int nsexes;
double disp, Pdisp, NK;
demogrParams dem = pSpecies->getDemogr();
stageParams sstruct = pSpecies->getStage();
emigRules emig = pSpecies->getEmig();
emigTraits eparams;
trfrRules trfr = pSpecies->getTrfr();
indStats ind;
// to avoid division by zero, assume carrying capacity is at least one individual
// localK can be zero if there is a moving gradient or stochasticity in K
if (localK < 1.0) localK = 1.0;
NK = (float)totalPop() / localK;
int ninds = (int)inds.size();
// set up local copy of emigration probability table
// used when there is no individual variability
// NB - IT IS DOUBTFUL THIS CONTRIBUTES ANY SUBSTANTIAL TIME SAVING
if (dem.repType == 0) nsexes = 1; else nsexes = 2;
double Pemig[NSTAGES][NSEXES];
for (int stg = 0; stg < sstruct.nStages; stg++) {
for (int sex = 0; sex < nsexes; sex++) {
if (emig.indVar) Pemig[stg][sex] = 0.0;
else {
if (emig.densDep) {
if (emig.sexDep) {
if (emig.stgDep) {
eparams = pSpecies->getEmigTraits(stg, sex);
}
else {
eparams = pSpecies->getEmigTraits(0, sex);
}
}
else { // !emig.sexDep
if (emig.stgDep) {
eparams = pSpecies->getEmigTraits(stg, 0);
}
else {
eparams = pSpecies->getEmigTraits(0, 0);
}
}
Pemig[stg][sex] = eparams.d0 / (1.0 + exp(-(NK - eparams.beta) * eparams.alpha));
}
else { // density-independent
if (emig.sexDep) {
if (emig.stgDep) {
Pemig[stg][sex] = pSpecies->getEmigD0(stg, sex);
}
else { // !emig.stgDep
Pemig[stg][sex] = pSpecies->getEmigD0(0, sex);
}
}
else { // !emig.sexDep
if (emig.stgDep) {
Pemig[stg][sex] = pSpecies->getEmigD0(stg, 0);
}
else { // !emig.stgDep
Pemig[stg][sex] = pSpecies->getEmigD0(0, 0);
}
}
}
} // end of !emig.indVar
}
}
for (int i = 0; i < ninds; i++) {
ind = inds[i]->getStats();
if (ind.status < 1)
{
if (emig.indVar) { // individual variability in emigration
if (dem.stageStruct && ind.stage != emig.emigStage) {
// emigration may not occur
Pdisp = 0.0;
}
else { // non-structured or individual is in emigration stage
eparams = inds[i]->getEmigTraits();
if (emig.densDep) { // density-dependent
NK = (float)totalPop() / localK;
Pdisp = eparams.d0 / (1.0 + exp(-(NK - eparams.beta) * eparams.alpha));
}
else { // density-independent
if (emig.sexDep) {
Pdisp = Pemig[0][ind.sex] + eparams.d0;
}
else {
Pdisp = Pemig[0][0] + eparams.d0;
}
}
}
} // end of individual variability
else { // no individual variability
if (emig.densDep) {
if (emig.sexDep) {
if (emig.stgDep) {
Pdisp = Pemig[ind.stage][ind.sex];
}
else {
Pdisp = Pemig[0][ind.sex];
}
}
else { // !emig.sexDep
if (emig.stgDep) {
Pdisp = Pemig[ind.stage][0];
}
else {
Pdisp = Pemig[0][0];
}
}
}
else { // density-independent
if (emig.sexDep) {
if (emig.stgDep) {
Pdisp = Pemig[ind.stage][ind.sex];
}
else { // !emig.stgDep
Pdisp = Pemig[0][ind.sex];
}
}
else { // !emig.sexDep
if (emig.stgDep) {
Pdisp = Pemig[ind.stage][0];
}
else { // !emig.stgDep
Pdisp = Pemig[0][0];
}
}
}
} // end of no individual variability
disp = pRandom->Bernoulli(Pdisp);
if (disp == 1) { // emigrant
inds[i]->setStatus(1);
}
} // end of if (ind.status < 1) condition
} // end of for loop
}
// All individuals emigrate after patch destruction
void Population::allEmigrate(void) {
int ninds = (int)inds.size();
for (int i = 0; i < ninds; i++) {
inds[i]->setStatus(1);
}
}
// If an Individual has been identified as an emigrant, remove it from the Population
disperser Population::extractDisperser(int ix) {
disperser d;
indStats ind = inds[ix]->getStats();
if (ind.status == 1) { // emigrant
d.pInd = inds[ix]; d.yes = true;
inds[ix] = 0;
nInds[ind.stage][ind.sex]--;
}
else {
d.pInd = NULL; d.yes = false;
}
return d;
}
// For an individual identified as being in the matrix population:
// if it is a settler, return its new location and remove it from the current population
// otherwise, leave it in the matrix population for possible reporting before deletion
disperser Population::extractSettler(int ix) {
disperser d;
Cell* pCell;
indStats ind = inds[ix]->getStats();
pCell = inds[ix]->getLocn(1);
d.pInd = inds[ix]; d.pCell = pCell; d.yes = false;
if (ind.status == 4 || ind.status == 5) { // settled
d.yes = true;
inds[ix] = 0;
nInds[ind.stage][ind.sex]--;
}
return d;
}
// Add a specified individual to the new/current dispersal group
// Add a specified individual to the population
void Population::recruit(Individual* pInd) {
inds.push_back(pInd);
indStats ind = pInd->getStats();
nInds[ind.stage][ind.sex]++;
}
//---------------------------------------------------------------------------
// Transfer is run for populations in the matrix only
#if RS_RCPP // included also SEASONAL
int Population::transfer(Landscape* pLandscape, short landIx, short nextseason)
#else
int Population::transfer(Landscape* pLandscape, short landIx)
#endif
{
int ndispersers = 0;
int disperser;
short othersex;
bool mateOK, densdepOK;
intptr patch, popn;
int patchnum;
double localK, popsize, settprob;
Patch* pPatch = 0;
Cell* pCell = 0;
indStats ind;
Population* pNewPopn = 0;
locn newloc, nbrloc;
landData ppLand = pLandscape->getLandData();
short reptype = pSpecies->getRepType();
trfrRules trfr = pSpecies->getTrfr();
settleType settletype = pSpecies->getSettle();
settleRules sett;
settleTraits settDD;
settlePatch settle;
simParams sim = paramsSim->getSim();
// each individual takes one step
// for dispersal by kernel, this should be the only step taken
int ninds = (int)inds.size();
for (int i = 0; i < ninds; i++) {
if (trfr.moveModel) {
disperser = inds[i]->moveStep(pLandscape, pSpecies, landIx, sim.absorbing);
}
else {
disperser = inds[i]->moveKernel(pLandscape, pSpecies, reptype, sim.absorbing);
}
ndispersers += disperser;
if (disperser) {
if (reptype > 0)
{ // sexual species - record as potential settler in new patch
if (inds[i]->getStatus() == 2)
{ // disperser has found a patch
pCell = inds[i]->getLocn(1);
patch = pCell->getPatch();
if (patch != 0) { // not no-data area
pPatch = (Patch*)patch;
pPatch->incrPossSettler(pSpecies, inds[i]->getSex());
}
}
}
}
}
// each individual which has reached a potential patch decides whether to settle
for (int i = 0; i < ninds; i++) {
ind = inds[i]->getStats();
if (ind.sex == 0) othersex = 1; else othersex = 0;
if (settletype.stgDep) {
if (settletype.sexDep) sett = pSpecies->getSettRules(ind.stage, ind.sex);
else sett = pSpecies->getSettRules(ind.stage, 0);
}
else {
if (settletype.sexDep) sett = pSpecies->getSettRules(0, ind.sex);
else sett = pSpecies->getSettRules(0, 0);
}
if (ind.status == 2)
{ // awaiting settlement
pCell = inds[i]->getLocn(1);
if (pCell == 0) {
// this condition can occur in a patch-based model at the time of a dynamic landscape
// change when there is a range restriction in place, since a patch can straddle the
// range restriction and an individual forced to disperse upon patch removal could
// start its trajectory beyond the boundary of the restrictyed range - such a model is
// not good practice, but the condition must be handled by killing the individual conceerned
ind.status = 6;
}
else {
mateOK = false;
if (sett.findMate) {
// determine whether at least one individual of the opposite sex is present in the
// new population
if (matePresent(pCell, othersex)) mateOK = true;
}
else { // no requirement to find a mate
mateOK = true;
}
densdepOK = false;
settle = inds[i]->getSettPatch();
if (sett.densDep)
{
patch = pCell->getPatch();
if (patch != 0) { // not no-data area
pPatch = (Patch*)patch;
if (settle.settleStatus == 0
|| settle.pSettPatch != pPatch)
// note: second condition allows for having moved from one patch to another
// adjacent one
{
// determine whether settlement occurs in the (new) patch
localK = (double)pPatch->getK();
popn = pPatch->getPopn((intptr)pSpecies);
if (popn == 0) { // population has not been set up in the new patch
popsize = 0.0;
}
else {
pNewPopn = (Population*)popn;
popsize = (double)pNewPopn->totalPop();
}
if (localK > 0.0) {
// make settlement decision
if (settletype.indVar) settDD = inds[i]->getSettTraits();
#if RS_RCPP
else settDD = pSpecies->getSettTraits(ind.stage, ind.sex);
#else
else {
if (settletype.sexDep) {
if (settletype.stgDep)
settDD = pSpecies->getSettTraits(ind.stage, ind.sex);
else
settDD = pSpecies->getSettTraits(0, ind.sex);
}
else {
if (settletype.stgDep)
settDD = pSpecies->getSettTraits(ind.stage, 0);
else
settDD = pSpecies->getSettTraits(0, 0);
}
}
#endif //RS_RCPP
settprob = settDD.s0 /
(1.0 + exp(-(popsize / localK - (double)settDD.beta) * (double)settDD.alpha));
if (pRandom->Bernoulli(settprob)) { // settlement allowed
densdepOK = true;
settle.settleStatus = 2;
}
else { // settlement procluded
settle.settleStatus = 1;
}
settle.pSettPatch = pPatch;
}
inds[i]->setSettPatch(settle);
}
else {
if (settle.settleStatus == 2) { // previously allowed to settle
densdepOK = true;
}
}
}
}
else { // no density-dependent settlement
densdepOK = true;
settle.settleStatus = 2;
settle.pSettPatch = pPatch;
inds[i]->setSettPatch(settle);
}
if (mateOK && densdepOK) { // can recruit to patch
ind.status = 4;
ndispersers--;
}
else { // does not recruit
if (trfr.moveModel) {
ind.status = 1; // continue dispersing, unless ...
// ... maximum steps has been exceeded
pathSteps steps = inds[i]->getSteps();
settleSteps settsteps = pSpecies->getSteps(ind.stage, ind.sex);
if (steps.year >= settsteps.maxStepsYr) {
ind.status = 3; // waits until next year
}
if (steps.total >= settsteps.maxSteps) {
ind.status = 6; // dies
}
}
else { // dispersal kernel
if (sett.wait) {
ind.status = 3; // wait until next dispersal event
}
else {
ind.status = 6; // (dies unless a neighbouring cell is suitable)
}
ndispersers--;
}