-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubCommunity.cpp
More file actions
1475 lines (1343 loc) · 47.6 KB
/
SubCommunity.cpp
File metadata and controls
1475 lines (1343 loc) · 47.6 KB
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) 2026 Greta Bocedi, Stephen C.F. Palmer, Justin M.J. Travis, Anne-Kathleen Malchow, Roslyn Henry, Théo Pannetier, Jette Wolff, 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 "SubCommunity.h"
//---------------------------------------------------------------------------
ofstream outtraits;
//---------------------------------------------------------------------------
SubCommunity::SubCommunity(Patch* pPch, int num) {
subCommNum = num;
pPatch = pPch;
// record the new sub-community no. in the patch
pPatch->setSubComm(this);
initial = false;
occupancy = 0;
}
SubCommunity::~SubCommunity() {
pPatch->setSubComm(nullptr);
int npops = (int)popns.size();
for (int i = 0; i < npops; i++) { // all populations
delete popns[i];
}
popns.clear();
if (occupancy != 0) delete[] occupancy;
}
int SubCommunity::getNum(void) { return subCommNum; }
Patch* SubCommunity::getPatch(void) { return pPatch; }
locn SubCommunity::getLocn(void) {
locn loc = pPatch->getCellLocn(0);
return loc;
}
void SubCommunity::setInitial(bool b) { initial = b; }
void SubCommunity::initialise(Landscape* pLandscape, Species* pSpecies)
{
int ncells;
landParams ppLand = pLandscape->getLandParams();
initParams init = paramsInit->getInit();
// determine size of initial population
int nInds = 0;
if (subCommNum == 0 // matrix patch
|| !initial) // not in initial region or distribution
nInds = 0;
else {
float k = pPatch->getK();
if (k > 0.0) { // patch is currently suitable for this species
switch (init.initDens) {
case 0: // at carrying capacity
nInds = (int)k;
break;
case 1: // at half carrying capacity
nInds = (int)(k / 2.0);
break;
case 2: // specified no. per cell or density
ncells = pPatch->getNCells();
if (ppLand.patchModel) {
nInds = (int)(init.indsHa * (float)(ncells * ppLand.resol * ppLand.resol) / 10000.0);
}
else {
nInds = init.indsCell * ncells;
}
break;
}
}
else nInds = 0;
}
// create new population only if it is non-zero or the matrix popn
if (subCommNum == 0 || nInds > 0) {
newPopn(pLandscape, pSpecies, pPatch, nInds);
}
}
// initialise a specified individual
void SubCommunity::initialInd(Landscape* pLandscape, Species* pSpecies,
Patch* pPatch, Cell* pCell, int ix)
{
demogrParams dem = pSpecies->getDemogrParams();
stageParams sstruct = pSpecies->getStageParams();
emigRules emig = pSpecies->getEmigRules();
transferRules trfr = pSpecies->getTransferRules();
settleType sett = pSpecies->getSettle();
short stg, age, repInt;
Individual* pInd;
float probmale;
// create new population if not already in existence
int npopns = (int)popns.size();
if (npopns < 1) {
newPopn(pLandscape, pSpecies, pPatch, 0);
}
// create new individual
initInd iind = paramsInit->getInitInd(ix);
if (dem.stageStruct) {
stg = iind.stage; age = iind.age; repInt = sstruct.repInterval;
}
else {
age = stg = 1; repInt = 0;
}
if (dem.repType == 0) {
probmale = 0.0;
}
else {
if (iind.sex == 1) probmale = 1.0; else probmale = 0.0;
}
pInd = new Individual(pSpecies, pCell, pPatch, stg, age, repInt, probmale, trfr.usesMovtProc, trfr.moveType);
if (pSpecies->getNTraits() > 0) {
// individual variation - set up genetics
landData land = pLandscape->getLandData();
pInd->setUpGenes(pSpecies, land.resol);
}
popns[0]->recruit(pInd);
}
// Create a new population, and return its address
Population* SubCommunity::newPopn(Landscape* pLandscape, Species* pSpecies,
Patch* pPatch, int nInds)
{
landParams land = pLandscape->getLandParams();
int npopns = (int)popns.size();
popns.push_back(new Population(pSpecies, pPatch, nInds, land.resol));
return popns[npopns];
}
popStats SubCommunity::getPopStats(void) {
popStats p, pop;
p.pSpecies = 0; p.spNum = 0; p.nInds = p.nAdults = p.nNonJuvs = 0; p.breeding = false;
p.pPatch = pPatch;
// FOR SINGLE SPECIES IMPLEMENTATION, THERE IS ONLY ONE POPULATION IN THE PATCH
int npops = (int)popns.size();
for (int i = 0; i < npops; i++) { // all populations
pop = popns[i]->getStats(pPatch->getDemoScaling());
p.pSpecies = pop.pSpecies;
p.spNum = pop.spNum;
p.nInds += pop.nInds;
p.nNonJuvs += pop.nNonJuvs;
p.nAdults += pop.nAdults;
p.breeding = pop.breeding;
}
return p;
}
void SubCommunity::resetPopns(void) {
int npops = (int)popns.size();
for (int i = 0; i < npops; i++) { // all populations
delete popns[i];
}
popns.clear();
// clear the list of populations in the corresponding patch
pPatch->resetPopn();
}
void SubCommunity::resetPossSettlers(void) {
if (subCommNum == 0) return; // not applicable in the matrix
pPatch->resetPossSettlers();
}
// Extirpate all populations according to
// option 0 - random local extinction probability
// option 1 - local extinction probability gradient
// NB only applied for cell-based model
void SubCommunity::localExtinction(int option) {
double pExtinct = 0.0;
if (option == 0) {
envStochParams env = paramsStoch->getStoch();
if (env.localExt) pExtinct = env.locExtProb;
}
else {
envGradParams grad = paramsGrad->getGradient();
Cell* pCell = pPatch->getRandomCell(); // get only cell in the patch
// extinction prob is complement of cell gradient value plus any non-zero prob at the optimum
pExtinct = 1.0 - pCell->getEnvVal() + grad.extProbOpt;
if (pExtinct > 1.0) pExtinct = 1.0;
}
if (pRandom->Bernoulli(pExtinct)) {
int npops = (int)popns.size();
for (int i = 0; i < npops; i++) { // all populations
popns[i]->extirpate();
}
}
}
// Action in event of patch becoming unsuitable owing to landscape change
void SubCommunity::patchChange(void) {
if (subCommNum == 0) return; // no reproduction in the matrix
Species* pSpecies;
float localK = 0.0;
int npops = (int)popns.size();
// THE FOLLOWING MAY BE MORE EFFICIENT WHILST THERE IS ONLY ONE SPECIES ...
if (npops < 1) return;
localK = pPatch->getK();
if (localK <= 0.0) { // patch in dynamic landscape has become unsuitable
for (int i = 0; i < npops; i++) { // all populations
pSpecies = popns[i]->getSpecies();
demogrParams dem = pSpecies->getDemogrParams();
if (dem.stageStruct) {
stageParams sstruct = pSpecies->getStageParams();
if (sstruct.disperseOnLoss) popns[i]->allEmigrate();
else popns[i]->extirpate();
}
else { // non-stage-structured species is destroyed
popns[i]->extirpate();
}
}
}
}
void SubCommunity::reproduction(int resol, float epsGlobal, short rasterType, bool patchModel)
{
if (subCommNum == 0) return; // no reproduction in the matrix
float localK, envval;
std::vector <float> localDemoScaling;
Cell* pCell;
envGradParams grad = paramsGrad->getGradient();
envStochParams env = paramsStoch->getStoch();
int npops = (int)popns.size();
// THE FOLLOWING MAY BE MORE EFFICIENT WHILST THERE IS ONLY ONE SPECIES ...
if (npops < 1) return;
localK = pPatch->getK();
localDemoScaling = pPatch->getDemoScaling();
if (localK > 0.0) {
if (patchModel) {
envval = 1.0; // environmental gradient is currently not applied for patch-based model
}
else { // cell-based model
if (grad.gradient && grad.gradType == 2)
{ // gradient in fecundity
Cell* pCell = pPatch->getRandomCell(); // locate the only cell in the patch
envval = pCell->getEnvVal();
}
else envval = 1.0;
}
if (env.stoch && !env.inK) { // stochasticity in fecundity
if (env.local) {
if (!patchModel) { // only permitted for cell-based model
pCell = pPatch->getRandomCell();
if (pCell != 0) envval += pCell->getEps();
}
}
else { // global stochasticity
envval += epsGlobal;
}
}
for (int i = 0; i < npops; i++) { // all populations
popns[i]->reproduction(localK, envval, resol, localDemoScaling);
popns[i]->fledge();
}
}
}
void SubCommunity::emigration(void)
{
if (subCommNum == 0) return; // no emigration from the matrix
int npops = static_cast<int>(popns.size());
if (npops < 1) return;
float localK = pPatch->getK();
// NOTE that even if K is zero, it could have been >0 in previous time-step, and there
// might be emigrants if there is non-juvenile emigration
for (int i = 0; i < npops; i++) { // all populations
popns[i]->emigration(localK);
}
}
// Remove emigrants from their natal patch and add to a map of vectors
void SubCommunity::recruitDispersers(std::map<Species*,std::vector<Individual *>>& disperserPool) {
if (subCommNum == 0) return; // no dispersal initiation in the matrix
popStats pop;
disperser disp;
int npops = (int)popns.size();
for (int i = 0; i < npops; i++) { // all populations
pop = popns[i]->getStats(pPatch->getDemoScaling());
Species* pSpecies = popns[i]->getSpecies();
for (int j = 0; j < pop.nInds; j++) {
disp = popns[i]->extractDisperser(j);
if (disp.yes) { // disperser - has already been removed from natal population
disperserPool[pSpecies].push_back(disp.pInd);
}
}
// remove pointers to emigrants
popns[i]->clean();
}
}
// Add all individuals in the matrix to the disperser pool
void SubCommunity::disperseMatrix(std::map<Species*,std::vector<Individual *>> &inds_map) {
if (subCommNum != 0) return;
popStats pop;
int npops = (int)popns.size();
for (int i = 0; i < npops; i++) {
pop = popns[i]->getStats(pPatch->getDemoScaling());
Species* pSpecies = popns[i]->getSpecies();
#pragma omp for schedule(static)
for (int j = 0; j < pop.nInds; j++) {
Individual *pInd = popns[i]->extractIndividual(j);
inds_map[pSpecies].push_back(pInd);
}
#pragma omp single
popns[i]->clean();
}
}
// Add an individual into the local population of its species in the patch
void SubCommunity::recruit(Individual* pInd, Species* pSpecies) {
int npops = (int)popns.size();
for (int i = 0; i < npops; i++) { // all populations
if (pSpecies == popns[i]->getSpecies()) {
popns[i]->recruit(pInd);
}
}
}
// Add individuals into the local population of their species in the patch
void SubCommunity::recruitMany(std::vector<Individual*>& inds, Species* pSpecies) {
int npops = (int)popns.size();
for (int i = 0; i < npops; i++) { // all populations
if (pSpecies == popns[i]->getSpecies()) {
popns[i]->recruitMany(inds);
}
}
}
// Transfer through the matrix - run for a per-species map of vectors of individuals
int SubCommunity::resolveTransfer(std::map<Species*,vector<Individual*>>& dispersingInds, Landscape* pLandscape, short landIx)
{
int nbStillDispersing = 0;
int disperser;
Patch* pPatch = nullptr;
Cell* pCell = nullptr;
simParams sim = paramsSim->getSim();
for (auto & it : dispersingInds) { // all species
Species* const& pSpecies = it.first;
short reptype = pSpecies->getRepType();
transferRules trfr = pSpecies->getTransferRules();
vector<Individual*>& inds = it.second;
// each individual takes one step
// for dispersal by kernel, this should be the only step taken
for (auto& pInd : inds) {
if (trfr.usesMovtProc) {
disperser = pInd->moveStep(pLandscape, pSpecies, landIx, sim.absorbing);
}
else {
disperser = pInd->moveKernel(pLandscape, pSpecies, sim.absorbing);
}
nbStillDispersing += disperser;
if (disperser) {
if (reptype > 0)
{ // sexual species - record as potential settler in new patch
if (pInd->getStatus() == 2)
{ // disperser has found a patch
pCell = pInd->getCurrCell();
pPatch = pCell->getPatch();
if (pPatch != nullptr) { // not no-data area
pPatch->incrPossSettler(pSpecies, pInd->getSex());
}
}
}
}
}
}
return nbStillDispersing;
}
// Determine whether there is a potential mate present in a patch which a potential
// settler has reached
bool SubCommunity::matePresent(Species* pSpecies, Cell* pCell, short othersex)
{
Patch* pPatch;
Population* pNewPopn;
int popsize = 0;
bool matefound = false;
pPatch = pCell->getPatch();
if (pPatch != nullptr) {
if (pPatch->getPatchNum() > 0) { // not the matrix patch
if (pPatch->getK() > 0.0)
{ // suitable
pNewPopn = pPatch->getPopn(pSpecies);
if (pNewPopn != nullptr) {
const stageParams sstruct = pSpecies->getStageParams();
// count members of other sex already resident in the patch
for (int stg = 0; stg < sstruct.nStages; stg++) {
popsize += pNewPopn->getNbInds(stg, othersex);
}
}
if (popsize < 1) {
// add any potential settlers of the other sex
popsize += pPatch->getPossSettlers(pSpecies, othersex);
}
}
}
}
if (popsize > 0) matefound = true;
return matefound;
}
// Transfer is run for populations in the matrix only
#if RS_RCPP // included also SEASONAL
int SubCommunity::resolveSettlement(std::map<Species*, vector<Individual*>>& dispersingInds, Landscape* pLandscape, short nextseason)
#else
int SubCommunity::resolveSettlement(std::map<Species*, vector<Individual*>>& dispersingInds, Landscape* pLandscape)
#endif
{
int nbStillDispersing = 0;
short othersex;
bool mateOK, densdepOK;
int patchnum;
double localK, popsize, settprob;
Patch* pPatch = 0;
Cell* pCell = 0;
indStats ind;
Population* pNewPopn = 0;
locn newloc, nbrloc;
landData ppLand = pLandscape->getLandData();
settleRules sett;
settleTraits settDD;
settlePatch settle;
simParams sim = paramsSim->getSim();
for (auto& it : dispersingInds) { // all species
Species* const& pSpecies = it.first;
transferRules trfr = pSpecies->getTransferRules();
settleType settletype = pSpecies->getSettle();
vector<Individual*>& inds = it.second;
// each individual which has reached a potential patch decides whether to settle
for (auto& pInd : inds) {
ind = pInd->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 = pInd->getCurrCell();
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(pSpecies, pCell, othersex)) mateOK = true;
}
else { // no requirement to find a mate
mateOK = true;
}
densdepOK = false;
settle = pInd->getSettPatch();
if (sett.densDep)
{
pPatch = pCell->getPatch();
if (pPatch != nullptr) { // not no-data area
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();
pNewPopn = pPatch->getPopn(pSpecies);
if (pNewPopn == nullptr) { // population has not been set up in the new patch
popsize = 0.0;
}
else {
popsize = (double)pNewPopn->getNbInds();
}
if (localK > 0.0) {
// make settlement decision
if (settletype.indVar) settDD = pInd->getIndSettTraits();
#if RS_RCPP
else settDD = pSpecies->getSpSettTraits(ind.stage, ind.sex);
#else
else {
if (settletype.sexDep) {
if (settletype.stgDep)
settDD = pSpecies->getSpSettTraits(ind.stage, ind.sex);
else
settDD = pSpecies->getSpSettTraits(0, ind.sex);
}
else {
if (settletype.stgDep)
settDD = pSpecies->getSpSettTraits(ind.stage, 0);
else
settDD = pSpecies->getSpSettTraits(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;
}
pInd->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;
pInd->setSettPatch(settle);
}
if (mateOK && densdepOK) { // can recruit to patch
ind.status = 4;
nbStillDispersing--;
}
else { // does not recruit
if (trfr.usesMovtProc) {
ind.status = 1; // continue dispersing, unless ...
// ... maximum steps has been exceeded
pathSteps steps = pInd->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)
}
nbStillDispersing--;
}
}
}
pInd->setStatus(ind.status);
}
#if RS_RCPP
// write each individuals current movement step and status to paths file
if (trfr.usesMovtProc && sim.outPaths) {
if (nextseason >= sim.outStartPaths && nextseason % sim.outIntPaths == 0) {
pInd->outMovePath(nextseason);
}
}
#endif
if (!trfr.usesMovtProc && sett.go2nbrLocn && (ind.status == 3 || ind.status == 6))
{
// for kernel-based transfer only ...
// determine whether recruitment to a neighbouring cell is possible
pCell = pInd->getCurrCell();
newloc = pCell->getLocn();
vector <Cell*> nbrlist;
for (int dx = -1; dx < 2; dx++) {
for (int dy = -1; dy < 2; dy++) {
if (dx != 0 || dy != 0) { //cell is not the current cell
nbrloc.x = newloc.x + dx; nbrloc.y = newloc.y + dy;
if (nbrloc.x >= 0 && nbrloc.x <= ppLand.maxX
&& nbrloc.y >= 0 && nbrloc.y <= ppLand.maxY) { // within landscape
// add to list of potential neighbouring cells if suitable, etc.
pCell = pLandscape->findCell(nbrloc.x, nbrloc.y);
if (pCell != 0) { // not no-data area
pPatch = pCell->getPatch();
if (pPatch != nullptr) { // not no-data area
patchnum = pPatch->getPatchNum();
if (patchnum > 0 && pPatch != pInd->getNatalPatch())
{ // not the matrix or natal patch
if (pPatch->getK() > 0.0)
{ // suitable
if (sett.findMate) {
if (matePresent(pSpecies, pCell, othersex)) nbrlist.push_back(pCell);
}
else
nbrlist.push_back(pCell);
}
}
}
}
}
}
}
}
int listsize = (int)nbrlist.size();
if (listsize > 0) { // there is at least one suitable neighbouring cell
if (listsize == 1) {
pInd->moveto(nbrlist[0]);
}
else { // select at random from the list
int rrr = pRandom->IRandom(0, listsize - 1);
pInd->moveto(nbrlist[rrr]);
}
}
// else list empty - do nothing - individual retains its current location and status
}
} // loop through inds
} // loop through disperser map
return nbStillDispersing;
}
//---------------------------------------------------------------------------
// Remove emigrants from the vectors map and transfer to sub-community
// in which their destination co-ordinates fall
void SubCommunity::completeDispersal(std::map<Species*,vector<Individual*>>& inds_map, Landscape* pLandscape, bool connect)
{
Population* pPop;
Patch* pPrevPatch;
Patch* pNewPatch;
Cell* pPrevCell;
SubCommunity* pSubComm;
for (auto & it : inds_map) { // all species
Species* const& pSpecies = it.first;
vector<Individual*>& inds = it.second;
for (Individual*& pInd : inds) {
indStats ind = pInd->getStats();
bool settled = ind.status == 4 || ind.status == 5;
if (settled) {
// find new patch
pNewPatch = pInd->getCurrCell()->getPatch();
// find population within the patch (if there is one)
{
#ifdef _OPENMP
const std::unique_lock<std::mutex> lock = pNewPatch->lockPopns();
#endif // _OPENMP
pPop = pNewPatch->getPopn(pSpecies);
if (pPop == 0) { // settler is the first in a previously uninhabited patch
// create a new population in the corresponding sub-community
pSubComm = pNewPatch->getSubComm();
pPop = pSubComm->newPopn(pLandscape, pSpecies, pNewPatch, 0);
}
}
pPop->recruit(pInd);
if (connect) { // increment connectivity totals
int newpatch = pNewPatch->getSeqNum();
pPrevCell = pInd->getPrevCell();
pPrevPatch = pPrevCell->getPatch();
if (pPrevPatch != nullptr) {
int prevpatch = pPrevPatch->getSeqNum();
pLandscape->incrConnectMatrix(prevpatch, newpatch);
}
}
pInd = nullptr;
}
else { // for group dispersal only
}
}
// remove settled individuals
inds.erase(std::remove(inds.begin(), inds.end(), (Individual *)nullptr), inds.end());
}
}
//---------------------------------------------------------------------------
void SubCommunity::survival0(short option0, short option1)
{
int npops = (int)popns.size();
std::vector <float> localDemoScaling;
localDemoScaling = pPatch->getDemoScaling();
float localK = pPatch->getK();
for (int i = 0; i < npops; i++) { // all populations
popns[i]->survival0(localK, option0, option1, localDemoScaling);
}
}
void SubCommunity::survival1()
{
int npops = (int)popns.size();
for (int i = 0; i < npops; i++) { // all populations
popns[i]->survival1();
}
}
void SubCommunity::survival(short part, short option0, short option1
) {
if (part == 0) {
return survival0(option0, option1);
}
else {
return survival1();
}
}
void SubCommunity::ageIncrement(void) {
int npops = (int)popns.size();
for (int i = 0; i < npops; i++) { // all populations
popns[i]->ageIncrement();
}
}
// Find the population of a given species in a given patch
Population* SubCommunity::findPop(Species* pSp, Patch* pPch) {
Population* pPop = 0;
popStats pop;
int npops = (int)popns.size();
for (int i = 0; i < npops; i++) { // all populations
pop = popns[i]->getStats(pPatch->getDemoScaling());
if (pop.pSpecies == pSp && pop.pPatch == pPch) { // population located
pPop = popns[i];
break;
}
else pPop = 0;
}
return pPop;
}
//---------------------------------------------------------------------------
void SubCommunity::createOccupancy(int nrows) {
if (occupancy != 0) deleteOccupancy();
if (nrows > 0) {
occupancy = new int[nrows];
for (int i = 0; i < nrows; i++) occupancy[i] = 0;
}
}
void SubCommunity::updateOccupancy(int row) {
popStats pop;
int npops = (int)popns.size();
for (int i = 0; i < npops; i++) {
pop = popns[i]->getStats(pPatch->getDemoScaling());
if (pop.nInds > 0 && pop.breeding) {
occupancy[row]++;
i = npops;
}
}
}
int SubCommunity::getOccupancy(int row) {
if (row >= 0) return occupancy[row];
else return 0;
}
void SubCommunity::deleteOccupancy(void) {
delete[] occupancy;
occupancy = 0;
}
//---------------------------------------------------------------------------
// Close population file
bool SubCommunity::outPopFinishLandscape()
{
bool fileOK;
Population* pPop;
// as all populations may have been deleted, set up a dummy one
// species is not necessary
pPop = new Population();
fileOK = pPop->outPopFinishLandscape();
delete pPop;
return fileOK;
}
//---------------------------------------------------------------------------
// Open population file and write header record
bool SubCommunity::outPopStartLandscape(Landscape* pLandscape, Species* pSpecies)
{
bool fileOK;
Population* pPop;
landParams land = pLandscape->getLandParams();
// as no population has yet been created, set up a dummy one
// species is necessary, as columns depend on stage and sex structure
pPop = new Population(pSpecies, pPatch, 0, land.resol);
fileOK = pPop->outPopStartLandscape(land.landNum, land.patchModel);
delete pPop;
return fileOK;
}
// Write records to population file
void SubCommunity::outPop(Landscape* pLandscape, int rep, int yr, int gen)
{
landParams land = pLandscape->getLandParams();
envGradParams grad = paramsGrad->getGradient();
envStochParams env = paramsStoch->getStoch();
bool writeEnv = false;
bool gradK = false;
if (grad.gradient) {
writeEnv = true;
if (grad.gradType == 1) gradK = true; // ... carrying capacity
}
if (env.stoch) writeEnv = true;
// generate output for each population within the sub-community (patch)
// provided that the patch is suitable (i.e. non-zero carrying capacity)
// or the population is above zero (possible if there is stochasticity or a moving gradient)
// or it is the matrix patch in a patch-based model
int npops = (int)popns.size();
int patchnum;
Cell* pCell;
float localK;
float eps = 0.0;
if (env.stoch) {
if (env.local) {
pCell = pPatch->getRandomCell();
if (pCell != 0) eps = pCell->getEps();
}
else {
eps = pLandscape->getGlobalStoch(yr);
}
}
patchnum = pPatch->getPatchNum();
for (int i = 0; i < npops; i++) { // all populations
localK = pPatch->getK();
if (localK > 0.0 || (land.patchModel && patchnum == 0)) {
popns[i]->outPopulation(rep, yr, gen, eps, land.patchModel, writeEnv, gradK);
}
else {
if (popns[i]->getNbInds() > 0) {
popns[i]->outPopulation(rep, yr, gen, eps, land.patchModel, writeEnv, gradK);
}
}
}
}
// Close individuals file
void SubCommunity::outIndsFinishReplicate() {
// as all populations have been deleted, set up a dummy one
Population* pPop = new Population();
pPop->outIndsFinishReplicate();
delete pPop;
return;
}
// Open individuals file and write header record
void SubCommunity::outIndsStartReplicate(Landscape* pLandscape, int rep, int landNr) {
landParams ppLand = pLandscape->getLandParams();
popns[0]->outIndsStartReplicate(rep, landNr, ppLand.patchModel);
}
// Write records to individuals file
void SubCommunity::outIndividuals(Landscape* pLandscape, int rep, int yr, int gen) {
// generate output for each population within the sub-community (patch)
int npops = (int)popns.size();
for (int i = 0; i < npops; i++) { // all populations
popns[i]->outIndividual(pLandscape, rep, yr, gen, pPatch->getPatchNum());
}
}
// Population size of a specified stage
int SubCommunity::getNbInds(int stage) const {
int popsize = 0;
int npops = (int)popns.size();
for (int i = 0; i < npops; i++) { // all populations
popsize += popns[i]->getNbInds(stage);
}
return popsize;
}
// Close traits file
bool SubCommunity::outTraitsFinishLandscape()
{
if (outtraits.is_open()) outtraits.close();
outtraits.clear();
return true;
}
// Open traits file and write header record
bool SubCommunity::outTraitsStartLandscape(Landscape* pLandscape, Species* pSpecies, int landNr)
{
landParams land = pLandscape->getLandParams();
string name;
emigRules emig = pSpecies->getEmigRules();
transferRules trfr = pSpecies->getTransferRules();
settleType sett = pSpecies->getSettle();
simParams sim = paramsSim->getSim();
string DirOut = paramsSim->getDir(2);
if (sim.batchMode) {
if (land.patchModel) {
name = DirOut
+ "Batch" + to_string(sim.batchNum) + "_"
+ "Sim" + to_string(sim.simulation) + "_Land" + to_string(landNr)
+ "_TraitsXpatch.txt";
}
else {
name = DirOut
+ "Batch" + to_string(sim.batchNum) + "_"
+ "Sim" + to_string(sim.simulation) + "_Land" + to_string(landNr)
+ "_TraitsXcell.txt";
}
}
else {
if (land.patchModel) {
name = DirOut + "Sim" + to_string(sim.simulation) + "_TraitsXpatch.txt";
}
else {
name = DirOut + "Sim" + to_string(sim.simulation) + "_TraitsXcell.txt";
}
}
outtraits.open(name.c_str());
outtraits << "Rep\tYear\tRepSeason";
if (land.patchModel) outtraits << "\tPatchID";
else
outtraits << "\tx\ty";
if (emig.indVar) {
if (emig.sexDep) {
if (emig.densDep) {
outtraits << "\tF_meanD0\tF_stdD0\tM_meanD0\tM_stdD0";
outtraits << "\tF_meanAlpha\tF_stdAlpha\tM_meanAlpha\tM_stdAlpha";
outtraits << "\tF_meanBeta\tF_stdBeta\tM_meanBeta\tM_stdBeta";
}
else {
outtraits << "\tF_meanEP\tF_stdEP\tM_meanEP\tM_stdEP";
}
}
else {
if (emig.densDep) {
outtraits << "\tmeanD0\tstdD0\tmeanAlpha\tstdAlpha";
outtraits << "\tmeanBeta\tstdBeta";
}
else {
outtraits << "\tmeanEP\tstdEP";
}
}
}
if (trfr.indVar) {
if (trfr.usesMovtProc) {
if (trfr.moveType == 1) {
outtraits << "\tmeanDP\tstdDP\tmeanGB\tstdGB";
outtraits << "\tmeanAlphaDB\tstdAlphaDB\tmeanBetaDB\tstdBetaDB";
}
if (trfr.moveType == 2) {