-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommunity.cpp
1562 lines (1437 loc) · 52.1 KB
/
Community.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 "Community.h"
//---------------------------------------------------------------------------
ofstream outrange;
ofstream outoccup, outsuit;
ofstream outtraitsrows;
//---------------------------------------------------------------------------
Community::Community(Landscape* pLand) {
pLandscape = pLand;
indIx = 0;
}
Community::~Community(void) {
int nsubcomms = (int)subComms.size();
for (int i = 0; i < nsubcomms; i++) { // all sub-communities
delete subComms[i];
}
subComms.clear();
}
SubCommunity* Community::addSubComm(Patch* pPch, int num) {
int nsubcomms = (int)subComms.size();
subComms.push_back(new SubCommunity(pPch, num));
return subComms[nsubcomms];
}
void Community::initialise(Species* pSpecies, int year)
{
int nsubcomms, npatches, ndistcells, spratio, patchnum, rr = 0;
locn distloc;
patchData pch;
patchLimits limits;
intptr ppatch, subcomm;
std::vector <intptr> subcomms;
std::vector <bool> selected;
SubCommunity* pSubComm;
Patch* pPatch;
Cell* pCell;
landParams ppLand = pLandscape->getLandParams();
initParams init = paramsInit->getInit();
nsubcomms = (int)subComms.size();
spratio = ppLand.spResol / ppLand.resol;
#if RSDEBUG
DEBUGLOG << endl << "Community::initialise(): this=" << this
<< " seedType=" << init.seedType << " freeType=" << init.freeType
<< " minSeedX=" << init.minSeedX << " minSeedY=" << init.minSeedY
<< " maxSeedX=" << init.maxSeedX << " maxSeedY=" << init.maxSeedY
<< " indsFile=" << init.indsFile
<< " nsubcomms=" << nsubcomms << " spratio=" << spratio
<< endl;
#endif
switch (init.seedType) {
case 0: // free initialisation
switch (init.freeType) {
case 0: // random
// determine no. of patches / cells within the specified initialisation limits
// and record their corresponding sub-communities in a list
// parallel list records which have been selected
npatches = pLandscape->patchCount();
limits.xMin = init.minSeedX; limits.xMax = init.maxSeedX;
limits.yMin = init.minSeedY; limits.yMax = init.maxSeedY;
for (int i = 0; i < npatches; i++) {
pch = pLandscape->getPatchData(i);
if (pch.pPatch->withinLimits(limits)) {
if (ppLand.patchModel) {
if (pch.pPatch->getPatchNum() != 0) {
subcomms.push_back(pch.pPatch->getSubComm());
selected.push_back(false);
}
}
else { // cell-based model - is cell(patch) suitable
if (pch.pPatch->getK() > 0.0)
{
subcomms.push_back(pch.pPatch->getSubComm());
selected.push_back(false);
}
}
}
}
// select specified no. of patches/cells at random
npatches = (int)subcomms.size();
if (init.nSeedPatches > npatches / 2) { // use backwards selection method
for (int i = 0; i < npatches; i++) selected[i] = true;
for (int i = 0; i < (npatches - init.nSeedPatches); i++) {
do {
rr = pRandom->IRandom(0, npatches - 1);
} while (!selected[rr]);
selected[rr] = false;
}
}
else { // use forwards selection method
for (int i = 0; i < init.nSeedPatches; i++) {
do {
rr = pRandom->IRandom(0, npatches - 1);
} while (selected[rr]);
selected[rr] = true;
}
}
// selected sub-communities for initialisation
for (int i = 0; i < nsubcomms; i++) { // all sub-communities
subComms[i]->setInitial(false);
}
for (int i = 0; i < npatches; i++) {
if (selected[i]) {
pSubComm = (SubCommunity*)subcomms[i];
pSubComm->setInitial(true);
}
}
break;
case 1: // all suitable patches/cells
npatches = pLandscape->patchCount();
limits.xMin = init.minSeedX; limits.xMax = init.maxSeedX;
limits.yMin = init.minSeedY; limits.yMax = init.maxSeedY;
for (int i = 0; i < npatches; i++) {
pch = pLandscape->getPatchData(i);
if (pch.pPatch->withinLimits(limits)) {
patchnum = pch.pPatch->getPatchNum();
if (patchnum != 0) {
if (pch.pPatch->getK() > 0.0)
{ // patch is suitable
subcomm = pch.pPatch->getSubComm();
if (subcomm == 0) {
// create a sub-community in the patch
pSubComm = addSubComm(pch.pPatch, patchnum);
}
else {
pSubComm = (SubCommunity*)subcomm;
}
pSubComm->setInitial(true);
}
}
}
}
break;
case 2: // manually selected patches/cells
break;
} // end of switch (init.freeType)
nsubcomms = (int)subComms.size();
for (int i = 0; i < nsubcomms; i++) { // all sub-communities
subComms[i]->initialise(pLandscape, pSpecies);
}
break;
case 1: // from species distribution
if (ppLand.spDist)
{
// deselect all existing sub-communities
for (int i = 0; i < nsubcomms; i++) {
subComms[i]->setInitial(false);
}
// initialise from loaded species distribution
switch (init.spDistType) {
case 0: // all presence cells
pLandscape->setDistribution(pSpecies, 0); // activate all patches
break;
case 1: // some randomly selected presence cells
pLandscape->setDistribution(pSpecies, init.nSpDistPatches); // activate random patches
break;
case 2: // manually selected presence cells
// cells have already been identified - no further action here
break;
}
// THE FOLLOWING WILL HAVE TO BE CHANGED FOR MULTIPLE SPECIES...
ndistcells = pLandscape->distCellCount(0);
for (int i = 0; i < ndistcells; i++) {
distloc = pLandscape->getSelectedDistnCell(0, i);
if (distloc.x >= 0) { // distribution cell is selected
// process each landscape cell within the distribution cell
for (int x = 0; x < spratio; x++) {
for (int y = 0; y < spratio; y++) {
pCell = pLandscape->findCell(distloc.x * spratio + x, distloc.y * spratio + y);
if (pCell != 0) { // not a no-data cell
ppatch = pCell->getPatch();
if (ppatch != 0) {
pPatch = (Patch*)ppatch;
if (pPatch->getSeqNum() != 0) { // not the matrix patch
subcomm = pPatch->getSubComm();
if (subcomm != 0) {
pSubComm = (SubCommunity*)subcomm;
pSubComm->setInitial(true);
}
}
}
}
}
}
}
}
nsubcomms = (int)subComms.size();
for (int i = 0; i < nsubcomms; i++) { // all sub-communities
subComms[i]->initialise(pLandscape, pSpecies);
}
}
else {
// WHAT HAPPENS IF INITIAL DISTRIBUTION IS NOT LOADED ??? ....
// should not occur - take no action - no initialisation will occur
}
break;
case 2: // initial individuals in specified patches/cells
if (year < 0) {
// initialise matrix sub-community only
subComms[0]->initialise(pLandscape, pSpecies);
indIx = 0; // reset index for initial individuals
}
else { // add any initial individuals for the current year
initInd iind; iind.year = 0;
int ninds = paramsInit->numInitInds();
while (indIx < ninds && iind.year <= year) {
iind = paramsInit->getInitInd(indIx);
while (iind.year == year) {
if (ppLand.patchModel) {
if (pLandscape->existsPatch(iind.patchID)) {
pPatch = pLandscape->findPatch(iind.patchID);
if (pPatch->getK() > 0.0)
{ // patch is suitable
subcomm = pPatch->getSubComm();
if (subcomm == 0) {
// create a sub-community in the patch
pSubComm = addSubComm(pPatch, iind.patchID);
}
else {
pSubComm = (SubCommunity*)subcomm;
}
pSubComm->initialInd(pLandscape, pSpecies, pPatch, pPatch->getRandomCell(), indIx);
}
}
}
else { // cell-based model
pCell = pLandscape->findCell(iind.x, iind.y);
if (pCell != 0) {
intptr ppatch = pCell->getPatch();
if (ppatch != 0) {
pPatch = (Patch*)ppatch;
if (pPatch->getK() > 0.0)
{ // patch is suitable
subcomm = pPatch->getSubComm();
if (subcomm == 0) {
// create a sub-community in the patch
pSubComm = addSubComm(pPatch, iind.patchID);
}
else {
pSubComm = (SubCommunity*)subcomm;
}
pSubComm->initialInd(pLandscape, pSpecies, pPatch, pCell, indIx);
}
}
}
}
indIx++;
if (indIx < ninds) {
iind = paramsInit->getInitInd(indIx);
}
else {
iind.year = 99999999;
}
}
}
}
break;
case 3: // from file
// this condition cannot occur here, as init.seedType will have been changed to 0 or 1
// when the initialisation file was read
break;
} // end of switch (init.seedType)
#if RSDEBUG
DEBUGLOG << "Community::initialise(): this=" << this
<< " nsubcomms=" << nsubcomms
<< endl;
#endif
}
// Add manually selected patches/cells to the selected set for initialisation
void Community::addManuallySelected(void) {
int npatches;
intptr subcomm, patch;
locn initloc;
Cell* pCell;
Patch* pPatch;
SubCommunity* pSubComm;
landParams ppLand = pLandscape->getLandParams();
npatches = pLandscape->initCellCount(); // no. of patches/cells specified
#if RSDEBUG
DEBUGLOG << "Community::addManuallySelected(): this = " << this
<< " npatches = " << npatches << endl;
#endif
// identify sub-communities to be initialised
if (ppLand.patchModel) {
for (int i = 0; i < npatches; i++) {
initloc = pLandscape->getInitCell(i); // patch number held in x-coord of list
pPatch = pLandscape->findPatch(initloc.x);
if (pPatch != 0) {
subcomm = pPatch->getSubComm();
if (subcomm != 0) {
pSubComm = (SubCommunity*)subcomm;
pSubComm->setInitial(true);
}
}
}
}
else { // cell-based model
for (int i = 0; i < npatches; i++) {
initloc = pLandscape->getInitCell(i);
if (initloc.x >= 0 && initloc.x < ppLand.dimX
&& initloc.y >= 0 && initloc.y < ppLand.dimY) {
pCell = pLandscape->findCell(initloc.x, initloc.y);
if (pCell != 0) { // not no-data cell
patch = pCell->getPatch();
#if RSDEBUG
DEBUGLOG << "Community::initialise(): i = " << i
<< " x = " << initloc.x << " y = " << initloc.y
<< " pCell = " << pCell << " patch = " << patch
<< endl;
#endif
if (patch != 0) {
pPatch = (Patch*)patch;
subcomm = pPatch->getSubComm();
#if RSDEBUG
DEBUGLOG << "Community::initialise(): i = " << i
<< " pPatch = " << pPatch << " subcomm = " << subcomm
<< endl;
#endif
if (subcomm != 0) {
pSubComm = (SubCommunity*)subcomm;
pSubComm->setInitial(true);
#if RSDEBUG
DEBUGLOG << "Community::initialise(): i = " << i
<< " pSubComm = " << pSubComm
<< endl;
#endif
}
}
}
}
}
}
}
void Community::resetPopns(void) {
int nsubcomms = (int)subComms.size();
for (int i = 0; i < nsubcomms; i++) { // all sub-communities
subComms[i]->resetPopns();
}
// reset the individual ids to start from zero
Individual::indCounter = 0;
}
void Community::localExtinction(int option) {
int nsubcomms = (int)subComms.size();
for (int i = 0; i < nsubcomms; i++) { // all sub-communities
if (subComms[i]->getNum() > 0) { // except in matrix
subComms[i]->localExtinction(option);
}
}
}
void Community::patchChanges(void) {
int nsubcomms = (int)subComms.size();
for (int i = 0; i < nsubcomms; i++) { // all sub-communities
if (subComms[i]->getNum() > 0) { // except in matrix
subComms[i]->patchChange();
}
}
}
void Community::reproduction(int yr)
{
float eps = 0.0; // epsilon for environmental stochasticity
landParams land = pLandscape->getLandParams();
envStochParams env = paramsStoch->getStoch();
int nsubcomms = (int)subComms.size();
#if RSDEBUG
DEBUGLOG << "Community::reproduction(): this=" << this
<< " nsubcomms=" << nsubcomms << endl;
#endif
for (int i = 0; i < nsubcomms; i++) { // all sub-communities
if (env.stoch) {
if (!env.local) { // global stochasticty
eps = pLandscape->getGlobalStoch(yr);
}
}
subComms[i]->reproduction(land.resol, eps, land.rasterType, land.patchModel);
}
#if RSDEBUG
DEBUGLOG << "Community::reproduction(): finished" << endl;
#endif
}
void Community::emigration(void)
{
int nsubcomms = (int)subComms.size();
#if RSDEBUG
DEBUGLOG << "Community::emigration(): this=" << this
<< " nsubcomms=" << nsubcomms << endl;
#endif
for (int i = 0; i < nsubcomms; i++) { // all sub-communities
subComms[i]->emigration();
}
#if RSDEBUG
DEBUGLOG << "Community::emigration(): finished" << endl;
#endif
}
#if RS_RCPP // included also SEASONAL
void Community::dispersal(short landIx, short nextseason)
#else
void Community::dispersal(short landIx)
#endif // SEASONAL || RS_RCPP
{
#if RSDEBUG
int t0, t1, t2;
t0 = time(0);
#endif
simParams sim = paramsSim->getSim();
int nsubcomms = (int)subComms.size();
// initiate dispersal - all emigrants leave their natal community and join matrix community
SubCommunity* matrix = subComms[0]; // matrix community is always the first
for (int i = 0; i < nsubcomms; i++) { // all populations
subComms[i]->initiateDispersal(matrix);
}
#if RSDEBUG
t1 = time(0);
DEBUGLOG << "Community::dispersal(): this=" << this
<< " nsubcomms=" << nsubcomms << " initiation time=" << t1 - t0 << endl;
#endif
// dispersal is undertaken by all individuals now in the matrix patch
// (even if not physically in the matrix)
int ndispersers = 0;
do {
for (int i = 0; i < nsubcomms; i++) { // all populations
subComms[i]->resetPossSettlers();
}
#if RS_RCPP // included also SEASONAL
ndispersers = matrix->transfer(pLandscape, landIx, nextseason);
#else
ndispersers = matrix->transfer(pLandscape, landIx);
#endif // SEASONAL || RS_RCPP
matrix->completeDispersal(pLandscape, sim.outConnect);
} while (ndispersers > 0);
#if RSDEBUG
DEBUGLOG << "Community::dispersal(): matrix=" << matrix << endl;
t2 = time(0);
DEBUGLOG << "Community::dispersal(): transfer time=" << t2 - t1 << endl;
#endif
}
void Community::survival(short part, short option0, short option1)
{
int nsubcomms = (int)subComms.size();
for (int i = 0; i < nsubcomms; i++) { // all communities (including in matrix)
subComms[i]->survival(part, option0, option1);
}
}
void Community::ageIncrement(void) {
int nsubcomms = (int)subComms.size();
for (int i = 0; i < nsubcomms; i++) { // all communities (including in matrix)
subComms[i]->ageIncrement();
}
}
// Calculate total no. of individuals of all species
int Community::totalInds(void) {
popStats p;
int total = 0;
int nsubcomms = (int)subComms.size();
for (int i = 0; i < nsubcomms; i++) { // all communities (including in matrix)
p = subComms[i]->getPopStats();
total += p.nInds;
}
return total;
}
// Find the population of a given species in a given patch
Population* Community::findPop(Species* pSp, Patch* pPch) {
Population* pPop = 0;
int nsubcomms = (int)subComms.size();
for (int i = 0; i < nsubcomms; i++) { // all communities (including in matrix)
pPop = subComms[i]->findPop(pSp, pPch);
if (pPop != 0) break;
}
return pPop;
}
//---------------------------------------------------------------------------
void Community::createOccupancy(int nrows, int reps) {
int nsubcomms = (int)subComms.size();
for (int i = 0; i < nsubcomms; i++) {
subComms[i]->createOccupancy(nrows);
}
// Initialise array for occupancy of suitable cells/patches
occSuit = new float* [nrows];
for (int i = 0; i < nrows; i++)
{
occSuit[i] = new float[reps];
for (int ii = 0; ii < reps; ii++) occSuit[i][ii] = 0.0;
}
}
void Community::updateOccupancy(int row, int rep)
{
#if RSDEBUG
DEBUGLOG << "Community::updateOccupancy(): row=" << row << endl;
#endif
int nsubcomms = (int)subComms.size();
for (int i = 0; i < nsubcomms; i++) {
subComms[i]->updateOccupancy(row);
}
commStats s = getStats();
occSuit[row][rep] = (float)s.occupied / (float)s.suitable;
}
void Community::deleteOccupancy(int nrows) {
int nsubcomms = (int)subComms.size();
for (int i = 0; i < nsubcomms; i++) {
subComms[i]->deleteOccupancy();
}
for (int i = 0; i < nrows; i++)
delete[] occSuit[i];
delete[] occSuit;
}
//---------------------------------------------------------------------------
// Count no. of sub-communities (suitable patches) and those occupied (non-zero populations)
// Determine range margins
commStats Community::getStats(void)
{
commStats s;
landParams ppLand = pLandscape->getLandParams();
s.ninds = s.nnonjuvs = s.suitable = s.occupied = 0;
s.minX = ppLand.maxX; s.minY = ppLand.maxY; s.maxX = s.maxY = 0;
float localK;
popStats patchPop;
int nsubcomms = (int)subComms.size();
for (int i = 0; i < nsubcomms; i++) { // all sub-communities
patchPop = subComms[i]->getPopStats();
s.ninds += patchPop.nInds;
s.nnonjuvs += patchPop.nNonJuvs;
if (patchPop.pPatch != 0) { // not the matrix patch
if (patchPop.pPatch->getPatchNum() != 0) { // not matrix patch
localK = patchPop.pPatch->getK();
if (localK > 0.0) s.suitable++;
if (patchPop.nInds > 0 && patchPop.breeding) {
s.occupied++;
patchLimits pchlim = patchPop.pPatch->getLimits();
if (pchlim.xMin < s.minX) s.minX = pchlim.xMin;
if (pchlim.xMax > s.maxX) s.maxX = pchlim.xMax;
if (pchlim.yMin < s.minY) s.minY = pchlim.yMin;
if (pchlim.yMax > s.maxY) s.maxY = pchlim.yMax;
}
}
}
}
return s;
}
//---------------------------------------------------------------------------
// Functions to control production of output files
// Open population file and write header record
bool Community::outPopHeaders(Species* pSpecies, int option) {
return subComms[0]->outPopHeaders(pLandscape, pSpecies, option);
}
// Write records to population file
void Community::outPop(int rep, int yr, int gen)
{
// generate output for each sub-community (patch) in the community
int nsubcomms = (int)subComms.size();
for (int i = 0; i < nsubcomms; i++) { // all sub-communities
subComms[i]->outPop(pLandscape, rep, yr, gen);
}
}
// Write records to individuals file
void Community::outInds(int rep, int yr, int gen, int landNr) {
if (landNr >= 0) { // open the file
subComms[0]->outInds(pLandscape, rep, yr, gen, landNr);
return;
}
if (landNr == -999) { // close the file
subComms[0]->outInds(pLandscape, rep, yr, gen, -999);
return;
}
// generate output for each sub-community (patch) in the community
int nsubcomms = (int)subComms.size();
for (int i = 0; i < nsubcomms; i++) { // all sub-communities
subComms[i]->outInds(pLandscape, rep, yr, gen, landNr);
}
}
// Write records to genetics file
void Community::outGenetics(int rep, int yr, int gen, int landNr) {
//landParams ppLand = pLandscape->getLandParams();
if (landNr >= 0) { // open the file
subComms[0]->outGenetics(rep, yr, gen, landNr);
return;
}
if (landNr == -999) { // close the file
subComms[0]->outGenetics(rep, yr, gen, landNr);
return;
}
// generate output for each sub-community (patch) in the community
int nsubcomms = (int)subComms.size();
for (int i = 0; i < nsubcomms; i++) { // all sub-communities
subComms[i]->outGenetics(rep, yr, gen, landNr);
}
}
// Open range file and write header record
bool Community::outRangeHeaders(Species* pSpecies, int landNr)
{
if (landNr == -999) { // close the file
if (outrange.is_open()) outrange.close();
outrange.clear();
return true;
}
string name;
landParams ppLand = pLandscape->getLandParams();
envStochParams env = paramsStoch->getStoch();
simParams sim = paramsSim->getSim();
// NEED TO REPLACE CONDITIONAL COLUMNS BASED ON ATTRIBUTES OF ONE SPECIES TO COVER
// ATTRIBUTES OF *ALL* SPECIES AS DETECTED AT MODEL LEVEL
demogrParams dem = pSpecies->getDemogr();
stageParams sstruct = pSpecies->getStage();
emigRules emig = pSpecies->getEmig();
trfrRules trfr = pSpecies->getTrfr();
settleType sett = pSpecies->getSettle();
#if RSDEBUG
DEBUGLOG << "Community::outRangeHeaders(): simulation=" << sim.simulation
<< " sim.batchMode=" << sim.batchMode
<< " landNr=" << landNr << endl;
#endif
if (sim.batchMode) {
name = paramsSim->getDir(2)
#if RS_RCPP
+ "Batch" + Int2Str(sim.batchNum) + "_"
+ "Sim" + Int2Str(sim.simulation) + "_Land"
+ Int2Str(landNr)
#else
+ "Batch" + Int2Str(sim.batchNum) + "_"
+ "Sim" + Int2Str(sim.simulation) + "_Land"
+ Int2Str(landNr)
#endif
+ "_Range.txt";
}
else {
name = paramsSim->getDir(2) + "Sim" + Int2Str(sim.simulation) + "_Range.txt";
}
outrange.open(name.c_str());
outrange << "Rep\tYear\tRepSeason";
if (env.stoch && !env.local) outrange << "\tEpsilon";
outrange << "\tNInds";
if (dem.stageStruct) {
for (int i = 1; i < sstruct.nStages; i++) outrange << "\tNInd_stage" << i;
outrange << "\tNJuvs";
}
if (ppLand.patchModel) outrange << "\tNOccupPatches";
else outrange << "\tNOccupCells";
outrange << "\tOccup/Suit\tmin_X\tmax_X\tmin_Y\tmax_Y";
if (emig.indVar) {
if (emig.sexDep) {
if (emig.densDep) {
outrange << "\tF_meanD0\tF_stdD0\tM_meanD0\tM_stdD0";
outrange << "\tF_meanAlpha\tF_stdAlpha\tM_meanAlpha\tM_stdAlpha";
outrange << "\tF_meanBeta\tF_stdBeta\tM_meanBeta\tM_stdBeta";
}
else {
outrange << "\tF_meanEP\tF_stdEP\tM_meanEP\tM_stdEP";
}
}
else {
if (emig.densDep) {
outrange << "\tmeanD0\tstdD0\tmeanAlpha\tstdAlpha";
outrange << "\tmeanBeta\tstdBeta";
}
else {
outrange << "\tmeanEP\tstdEP";
}
}
}
if (trfr.indVar) {
if (trfr.moveModel) {
if (trfr.moveType == 1) {
outrange << "\tmeanDP\tstdDP\tmeanGB\tstdGB";
outrange << "\tmeanAlphaDB\tstdAlphaDB\tmeanBetaDB\tstdBetaDB";
}
if (trfr.moveType == 2) {
outrange << "\tmeanStepLength\tstdStepLength\tmeanRho\tstdRho";
}
}
else {
if (trfr.sexDep) {
outrange << "\tF_mean_distI\tF_std_distI\tM_mean_distI\tM_std_distI";
if (trfr.twinKern)
outrange << "\tF_mean_distII\tF_std_distII\tM_mean_distII\tM_std_distII"
<< "\tF_meanPfirstKernel\tF_stdPfirstKernel"
<< "\tM_meanPfirstKernel\tM_stdPfirstKernel";
}
else {
outrange << "\tmean_distI\tstd_distI";
if (trfr.twinKern)
outrange << "\tmean_distII\tstd_distII\tmeanPfirstKernel\tstdPfirstKernel";
}
}
}
if (sett.indVar) {
if (sett.sexDep) {
outrange << "\tF_meanS0\tF_stdS0\tM_meanS0\tM_stdS0";
outrange << "\tF_meanAlphaS\tF_stdAlphaS\tM_meanAlphaS\tM_stdAlphaS";
outrange << "\tF_meanBetaS\tF_stdBetaS\tM_meanBetaS\tM_stdBetaS";
}
else {
outrange << "\tmeanS0\tstdS0";
outrange << "\tmeanAlphaS\tstdAlphaS";
outrange << "\tmeanBetaS\tstdBetaS";
}
}
outrange << endl;
#if RSDEBUG
DEBUGLOG << "Community::outRangeHeaders(): finished" << endl;
#endif
return outrange.is_open();
}
// Write record to range file
void Community::outRange(Species* pSpecies, int rep, int yr, int gen)
{
#if RSDEBUG
DEBUGLOG << "Community::outRange(): rep=" << rep
<< " yr=" << yr << " gen=" << gen << endl;
#endif
landParams ppLand = pLandscape->getLandParams();
envStochParams env = paramsStoch->getStoch();
// NEED TO REPLACE CONDITIONAL COLUMNS BASED ON ATTRIBUTES OF ONE SPECIES TO COVER
// ATTRIBUTES OF *ALL* SPECIES AS DETECTED AT MODEL LEVEL
demogrParams dem = pSpecies->getDemogr();
stageParams sstruct = pSpecies->getStage();
emigRules emig = pSpecies->getEmig();
trfrRules trfr = pSpecies->getTrfr();
settleType sett = pSpecies->getSettle();
outrange << rep << "\t" << yr << "\t" << gen;
if (env.stoch && !env.local) // write global environmental stochasticity
outrange << "\t" << pLandscape->getGlobalStoch(yr);
commStats s = getStats();
if (dem.stageStruct) {
outrange << "\t" << s.nnonjuvs;
int stagepop;
int nsubcomms = (int)subComms.size();
// all non-juvenile stages
for (int stg = 1; stg < sstruct.nStages; stg++) {
stagepop = 0;
for (int i = 0; i < nsubcomms; i++) { // all sub-communities
stagepop += subComms[i]->stagePop(stg);
}
outrange << "\t" << stagepop;
}
// juveniles born in current reproductive season
stagepop = 0;
for (int i = 0; i < nsubcomms; i++) { // all sub-communities
stagepop += subComms[i]->stagePop(0);
}
outrange << "\t" << stagepop;
}
else { // non-structured species
outrange << "\t" << s.ninds;
}
float occsuit = 0.0;
if (s.suitable > 0) occsuit = (float)s.occupied / (float)s.suitable;
outrange << "\t" << s.occupied << "\t" << occsuit;
// RANGE MINIMA AND MAXIMA NEED TO BECOME A PROPERTY OF THE SPECIES
if (s.ninds > 0) {
landOrigin origin = pLandscape->getOrigin();
outrange << "\t" << (float)s.minX * (float)ppLand.resol + origin.minEast
<< "\t" << (float)(s.maxX + 1) * (float)ppLand.resol + origin.minEast
<< "\t" << (float)s.minY * (float)ppLand.resol + origin.minNorth
<< "\t" << (float)(s.maxY + 1) * (float)ppLand.resol + origin.minNorth;
}
else
outrange << "\t0\t0\t0\t0";
if (emig.indVar || trfr.indVar || sett.indVar) { // output trait means
traitsums ts;
traitsums scts; // sub-community traits
traitCanvas tcanv;
int ngenes, popsize;
tcanv.pcanvas[0] = NULL;
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;
}
int nsubcomms = (int)subComms.size();
for (int i = 0; i < nsubcomms; i++) { // all sub-communities (incl. matrix)
scts = subComms[i]->outTraits(tcanv, pLandscape, rep, yr, gen, true);
for (int j = 0; j < NSEXES; j++) {
ts.ninds[j] += scts.ninds[j];
ts.sumD0[j] += scts.sumD0[j]; ts.ssqD0[j] += scts.ssqD0[j];
ts.sumAlpha[j] += scts.sumAlpha[j]; ts.ssqAlpha[j] += scts.ssqAlpha[j];
ts.sumBeta[j] += scts.sumBeta[j]; ts.ssqBeta[j] += scts.ssqBeta[j];
ts.sumDist1[j] += scts.sumDist1[j]; ts.ssqDist1[j] += scts.ssqDist1[j];
ts.sumDist2[j] += scts.sumDist2[j]; ts.ssqDist2[j] += scts.ssqDist2[j];
ts.sumProp1[j] += scts.sumProp1[j]; ts.ssqProp1[j] += scts.ssqProp1[j];
ts.sumDP[j] += scts.sumDP[j]; ts.ssqDP[j] += scts.ssqDP[j];
ts.sumGB[j] += scts.sumGB[j]; ts.ssqGB[j] += scts.ssqGB[j];
ts.sumAlphaDB[j] += scts.sumAlphaDB[j]; ts.ssqAlphaDB[j] += scts.ssqAlphaDB[j];
ts.sumBetaDB[j] += scts.sumBetaDB[j]; ts.ssqBetaDB[j] += scts.ssqBetaDB[j];
ts.sumStepL[j] += scts.sumStepL[j]; ts.ssqStepL[j] += scts.ssqStepL[j];
ts.sumRho[j] += scts.sumRho[j]; ts.ssqRho[j] += scts.ssqRho[j];
ts.sumS0[j] += scts.sumS0[j]; ts.ssqS0[j] += scts.ssqS0[j];
ts.sumAlphaS[j] += scts.sumAlphaS[j]; ts.ssqAlphaS[j] += scts.ssqAlphaS[j];
ts.sumBetaS[j] += scts.sumBetaS[j]; ts.ssqBetaS[j] += scts.ssqBetaS[j];
}
}
if (emig.indVar) {
if (emig.sexDep) { // must be a sexual species
ngenes = 2;
}
else {
if (dem.repType == 0) { // asexual reproduction
ngenes = 1;
}
else { // sexual reproduction
ngenes = 1;
}
}
double mnD0[2], mnAlpha[2], mnBeta[2], sdD0[2], sdAlpha[2], sdBeta[2];
for (int g = 0; g < ngenes; g++) {
mnD0[g] = mnAlpha[g] = mnBeta[g] = sdD0[g] = sdAlpha[g] = sdBeta[g] = 0.0;
// individuals may have been counted by sex if there was
// sex dependency in another dispersal phase
if (ngenes == 2) popsize = ts.ninds[g];
else popsize = ts.ninds[0] + ts.ninds[1];
if (popsize > 0) {
mnD0[g] = ts.sumD0[g] / (double)popsize;
mnAlpha[g] = ts.sumAlpha[g] / (double)popsize;
mnBeta[g] = ts.sumBeta[g] / (double)popsize;
if (popsize > 1) {
sdD0[g] = ts.ssqD0[g] / (double)popsize - mnD0[g] * mnD0[g];
if (sdD0[g] > 0.0) sdD0[g] = sqrt(sdD0[g]); else sdD0[g] = 0.0;
sdAlpha[g] = ts.ssqAlpha[g] / (double)popsize - mnAlpha[g] * mnAlpha[g];
if (sdAlpha[g] > 0.0) sdAlpha[g] = sqrt(sdAlpha[g]); else sdAlpha[g] = 0.0;
sdBeta[g] = ts.ssqBeta[g] / (double)popsize - mnBeta[g] * mnBeta[g];
if (sdBeta[g] > 0.0) sdBeta[g] = sqrt(sdBeta[g]); else sdBeta[g] = 0.0;
}
else {
sdD0[g] = sdAlpha[g] = sdBeta[g] = 0.0;
}
}
}
if (emig.sexDep) {
outrange << "\t" << mnD0[0] << "\t" << sdD0[0];
outrange << "\t" << mnD0[1] << "\t" << sdD0[1];
if (emig.densDep) {
outrange << "\t" << mnAlpha[0] << "\t" << sdAlpha[0];
outrange << "\t" << mnAlpha[1] << "\t" << sdAlpha[1];
outrange << "\t" << mnBeta[0] << "\t" << sdBeta[0];
outrange << "\t" << mnBeta[1] << "\t" << sdBeta[1];
}
}
else { // sex-independent
outrange << "\t" << mnD0[0] << "\t" << sdD0[0];
if (emig.densDep) {
outrange << "\t" << mnAlpha[0] << "\t" << sdAlpha[0];
outrange << "\t" << mnBeta[0] << "\t" << sdBeta[0];
}
}
}
if (trfr.indVar) {
if (trfr.moveModel) {
// CURRENTLY INDIVIDUAL VARIATION CANNOT BE SEX-DEPENDENT
ngenes = 1;
}
else {
if (trfr.sexDep) { // must be a sexual species
ngenes = 2;
}
else {
ngenes = 1;
}
}
double mnDist1[2], mnDist2[2], mnProp1[2], mnStepL[2], mnRho[2];
double sdDist1[2], sdDist2[2], sdProp1[2], sdStepL[2], sdRho[2];
double mnDP[2], mnGB[2], mnAlphaDB[2], mnBetaDB[2];
double sdDP[2], sdGB[2], sdAlphaDB[2], sdBetaDB[2];
for (int g = 0; g < ngenes; g++) {
mnDist1[g] = mnDist2[g] = mnProp1[g] = mnStepL[g] = mnRho[g] = 0.0;
sdDist1[g] = sdDist2[g] = sdProp1[g] = sdStepL[g] = sdRho[g] = 0.0;
mnDP[g] = mnGB[g] = mnAlphaDB[g] = mnBetaDB[g] = 0.0;
sdDP[g] = sdGB[g] = sdAlphaDB[g] = sdBetaDB[g] = 0.0;
// individuals may have been counted by sex if there was
// sex dependency in another dispersal phase
if (ngenes == 2) popsize = ts.ninds[g];
else popsize = ts.ninds[0] + ts.ninds[1];
if (popsize > 0) {
mnDist1[g] = ts.sumDist1[g] / (double)popsize;
mnDist2[g] = ts.sumDist2[g] / (double)popsize;
mnProp1[g] = ts.sumProp1[g] / (double)popsize;
mnStepL[g] = ts.sumStepL[g] / (double)popsize;
mnRho[g] = ts.sumRho[g] / (double)popsize;
mnDP[g] = ts.sumDP[g] / (double)popsize;
mnGB[g] = ts.sumGB[g] / (double)popsize;
mnAlphaDB[g] = ts.sumAlphaDB[g] / (double)popsize;
mnBetaDB[g] = ts.sumBetaDB[g] / (double)popsize;
if (popsize > 1) {
sdDist1[g] = ts.ssqDist1[g] / (double)popsize - mnDist1[g] * mnDist1[g];
if (sdDist1[g] > 0.0) sdDist1[g] = sqrt(sdDist1[g]); else sdDist1[g] = 0.0;
sdDist2[g] = ts.ssqDist2[g] / (double)popsize - mnDist2[g] * mnDist2[g];