-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForest.cpp
2054 lines (1566 loc) · 56.7 KB
/
Forest.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
// ---------------------------------------------------------------------------
#include "Forest.h"
#include <time.h>
#include <fstream>
#include <algorithm>
#include <vector>
#include <string>
#include <iomanip>
#include <cstdlib>
using namespace std;
// ---------------------------------------------------------------------------
CForest::CForest(int seed, int square_size, bool steps_out) {
time_output = steps_out;
Xmax = 1000.0;
Ymax = 500.0;
// Grid
SquareSize = square_size;
CSquare::sqSize = square_size;
//SquareSize = CSquare::sqSize; // square cell size
XSquares = (int) Xmax / SquareSize;
YSquares = (int) Ymax / SquareSize;
nSquares = XSquares*YSquares;
CSquare* pSquare;
SquareGrid = new (CSquare**[XSquares]);
for (int iX = 0; iX < XSquares; iX++){
SquareGrid[iX] = new (CSquare*[YSquares]);
for (int iY = 0; iY < YSquares; iY++){
pSquare = new CSquare(iX,iY);
SquareVec.push_back(pSquare);
SquareGrid[iX][iY] = pSquare;
}
}
// Random number generators
//int seed = (int) time(0); // random seed
// RandGen1 = new CRandomMersenneA(seed);
srand (seed);
RandGen1 = new CRandomMersenne(seed);
RandGen2 = new StochasticLib1(seed);
// Input-Output
//isim = 1;
//irep = 1;
ifstream InputFile;
//read input data
if (SquareSize == 10.0){
InputFile.open("InOut/Data/SquaresInput10.txt");
DensBin = 10;
}
if (SquareSize == 20.0){
InputFile.open("InOut/Data/SquaresInput20.txt");
DensBin = 40;
}
if (SquareSize == 50.0){
InputFile.open("InOut/Data/SquaresInput50.txt");
DensBin = 100;
}
if (SquareSize == 100.0){
InputFile.open("InOut/Data/SquaresInput100.txt");
DensBin = 200;
}
//string line, sq_label, year;
//int x,y, ntrees, nspec, nrec, ndead, dens_class;
string line, sq_label;
int ntrees, nrec, ndead, dens_class;
DensCl_Min = 100;
DensCl_Max = 0;
if (InputFile.good()){
getline(InputFile,line);
do {
//InputFile>>sq_label;
//InputFile>>x;
//InputFile>>y;
InputFile>>ntrees;
//InputFile>>nspec;
InputFile>>ndead;
InputFile>>nrec;
//InputFile>>year;
if (!InputFile.eof()){
//nTreesObs.push_back(ntrees);
nRecruitObs.push_back(nrec);
nDeathObs.push_back(ndead);
dens_class = (int) floor(ntrees/DensBin);
if (dens_class < DensCl_Min) DensCl_Min = dens_class;
if (dens_class > DensCl_Max) DensCl_Max = dens_class;
nRecruitObsDens[dens_class].push_back(nrec);
nDeathObsDens[dens_class].push_back(ndead);
double prec = static_cast<double>(nrec)/ntrees;
double pmort = static_cast<double>(ndead)/ntrees;
pRecruitObs.push_back(prec);
pDeathObs.push_back(pmort);
pRecruitObsDens[dens_class].push_back(prec);
pDeathObsDens[dens_class].push_back(pmort);
}
} while(!InputFile.eof());
InputFile.close();
}
else cout<<"Error InFile!"<<endl;
}
// ---------------------------------------------------------------------------
CForest::~CForest() {
SpecAbund.clear();
for (SquareIterV isquare = SquareVec.begin();
isquare != SquareVec.end(); isquare++){
delete (*isquare);
}
for (int ix = 0; ix < XSquares; ix++)
delete[] SquareGrid[ix];
delete[] SquareGrid;
delete RandGen1;
delete RandGen2;
//nTreesObs.clear();
nRecruitObs.clear();
nDeathObs.clear();
SquareFile.close();
}
// ---------------------------------------------------------------------------
void CForest::FileOpen(string label) {
string FileName;
string FileNameEnd = label + ".csv";
FileName = "InOut/Diversity" + FileNameEnd;
DivFile.open(FileName.c_str(), ios::in); {
if (DivFile.good()) {
DivFile.close();
DivFile.open(FileName.c_str(), ios::out | ios::app);
}
else {
DivFile.clear();
DivFile.open(FileName.c_str(), ios::out);
DivFile << "SimNr; RepNr; Step; NTrees; NSpec; Shannon" << endl;
}
}
/*
FileName = "InOut\\Abund" + FileNameEnd;
AbundFile.open(FileName.c_str(), ios::in); {
if (AbundFile.good()) {
AbundFile.close();
AbundFile.open(FileName.c_str(), ios::out | ios::app);
}
else {
AbundFile.clear();
AbundFile.open(FileName.c_str(), ios::out);
}
}
*/
/*
FileName = "InOut\\SAD" + FileNameEnd;
SAD_File.open(FileName.c_str(), ios::in); {
if (SAD_File.good()) {
SAD_File.close();
SAD_File.open(FileName.c_str(), ios::out | ios::app);
}
else {
SAD_File.clear();
SAD_File.open(FileName.c_str(), ios::out);
SAD_File << "A1; A2_3; A4_7; A8_15; A16_31; A32_63; A64_127; A128_255; "
<< "A256_511; A512_1023; A1024_2047; A2048-4095; A4096-8191; "
<< "A8192_16383; A16384_32767; A32768_65535; A65536-Inf"
<< endl;
// SAD_File<<"A1; A2; A3_4; A5_8; A9_16; A17_32; A33_64; A65_128;"
// <<"A129_256; A257_512; A513_1024; A1025_2048; A2049-Inf;"<<endl;
}
}
*/
}
// ---------------------------------------------------------------------------
void CForest::ClearForest() {
for (SquareIterV isquare = SquareVec.begin();
isquare != SquareVec.end(); ++isquare){
for (TreeIterV itree = (*isquare)->TreeList.begin();
itree != (*isquare)->TreeList.end(); ++itree)
delete (*itree);
(*isquare)->TreeList.clear();
(*isquare)->RecruitList.clear();
}
SpecAbund.clear();
}
// ---------------------------------------------------------------------------
inline double CForest::Distance(double x1, double y1, double x2, double y2) {
return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}
// ---------------------------------------------------------------------------
inline void CForest::BoundIntGrid(int& xx, int& yy, int Xmax, int Ymax) {
xx = xx % Xmax;
if (xx < 0)
xx = Xmax + xx;
yy = yy % Ymax;
if (yy < 0)
yy = Ymax + yy;
}
// ---------------------------------------------------------------------------
inline void CForest::PeriodBound(double& xx, double& yy) {
xx = Xmax * (xx / Xmax - floor(xx / Xmax));
yy = Ymax * (yy / Ymax - floor(yy / Ymax));
}
// ---------------------------------------------------------------------------
void CForest::Init() {
CTree *pTree;
int iX, iY;
// Init Raster Squares
for (iX = 0; iX < XSquares; iX++)
for (iY = 0; iY < YSquares; iY++)
SquareGrid[iX][iY]->InitSquare(iX, iY);
// Init Trees
double x, y;
unsigned int SpecID;
SpecMax = 0;
MaxTreeID = 0;
//NTrees = Pars->NTrees;
// random init
/*
for (int i = 0; i < NTrees; i++) {
x = RandGen1->Random() * Xmax;
y = RandGen1->Random() * Ymax;
SpecID = GetRandSpec();
pTree1 = new CTree(x, y, SpecID, Pars->r_max);
TreeList.push_back(pTree1);
iX1 = (int) floor(x / CellSize);
iY1 = (int) floor(y / CellSize);
Grid[iX1][iY1].TreeList.push_back(pTree1);
++SpecAbund[pTree1->SpecID];
}
*/
// initialization from input-file
ifstream BCI_File("InOut/Data/bci1995_1cm_20140212.txt");
string line, tag, SpecStr;
double dbh;
nTreesPlot = 0;
if (BCI_File.good()){
getline(BCI_File,line);
do {
BCI_File>>tag;
BCI_File>>x;
BCI_File>>y;
BCI_File>>dbh;
BCI_File>>SpecStr;
BCI_File>>SpecID;
if (!BCI_File.eof()){
if ((x<Xmax) && (y<Ymax)){
MaxTreeID++;
pTree = new CTree(MaxTreeID,x,y,SpecID);
++nTreesPlot;
++SpecAbund[pTree->SpecID];
iX = (int) floor(pTree->X/SquareSize);
iY = (int) floor(pTree->Y/SquareSize);
SquareGrid[iX][iY]->TreeList.push_back(pTree);
++SquareGrid[iX][iY]->SpecMap[SpecID];
//++SquareGrid[iX][iY]->nTrees_t0;
}
if (SpecID>SpecMax) SpecMax = SpecID;
}
} while(!BCI_File.eof());
BCI_File.close();
}
}
// ---------------------------------------------------------------------------
void CForest::GetNewXY(double &x1, double &y1) {
double x0 = x1;
double y0 = y1;
double r, r_dist, r_angle;
if (Pars->sdDisp<0.0001){
r = RandGen1->Random();
//r_dist = sqrt(-Pars->alpha*Pars->alpha*(log(1.0-r))); //Gaussian dispersal kernel following Clark et al. 1999
//this kernel produces the correct mean, but has a unimodal shape?
r_dist = sqrt(Pi/2.0) * std::abs(RandGen2->Normal(0.0,Pars->meanDisp)); //Gaussian kernel with the same mean,
//but different normalization constant
//maximum density at distance zero
//r_dist = r*2.0*Pars->mDisp; //uniform dispersal
}
else r_dist = exp(RandGen2->Normal(Pars->muDisp, Pars->sigmaDisp)); //log-normal dispersal kernel
r_angle = RandGen1->Random() * 2.0 * Pi;
x1 = x0 + cos(r_angle) * r_dist;
y1 = y0 + sin(r_angle) * r_dist;
PeriodBound(x1, y1);
}
// ---------------------------------------------------------------------------
int CForest::GetSpecID_mother(double x0, double y0, bool nearest) {
int ntrees{0}, iX_mother, iY_mother, SpecID_mother{0};
double r, r_dist, r_angle, x1,y1;
int ntrial{0};
int max_trial = 20;
do {
if (Pars->sdDisp<0.0001){
r_dist = sqrt(Pi/2.0) * std::abs(RandGen2->Normal(0.0,Pars->meanDisp));
}
else r_dist = exp(RandGen2->Normal(Pars->muDisp, Pars->sigmaDisp)); //log-normal dispersal kernel
r_angle = RandGen1->Random() * 2.0 * Pi;
x1 = x0 + cos(r_angle) * r_dist;
y1 = y0 + sin(r_angle) * r_dist;
PeriodBound(x1, y1);
iX_mother = floor(x1/SquareSize);
iY_mother = floor(y1/SquareSize);
//random tree in cell
ntrees = SquareGrid[iX_mother][iY_mother]->TreeList.size();
++ntrial;
} while ((ntrees == 0) && (ntrial < max_trial));
if (ntrial < max_trial){
if (nearest){//search nearest tree
double dmin = 2.0*SquareSize;
double d1;
CTree *itree_next = SquareGrid[iX_mother][iY_mother]->TreeList[0];
// for (TreeIterV itree = SquareGrid[iX_mother][iY_mother]->TreeList.begin();
// itree != SquareGrid[iX_mother][iY_mother]->TreeList.end();itree++)
for (const auto itree: SquareGrid[iX_mother][iY_mother]->TreeList)
{
d1 = Distance(x1,y1,itree->X,itree->Y);
if (d1 < dmin){
dmin = d1;
itree_next = itree;
}
}
SpecID_mother = itree_next->SpecID;
}
else {
//random tree from cell
int irand = RandGen1->IRandom(0,ntrees-1);
SpecID_mother = SquareGrid[iX_mother][iY_mother]->TreeList[irand]->SpecID;
}
}
else { //long distance dispersal
CSquare *pSquare_mother = nullptr;
do {
pSquare_mother = SquareVec[RandGen1->IRandom(0,nSquares-1)];
ntrees = pSquare_mother->TreeList.size();
} while (ntrees == 0);
//random tree in Square
SpecID_mother = pSquare_mother->TreeList[RandGen1->IRandom(0,ntrees-1)]->SpecID;
}
return(SpecID_mother);
}
//---------------------------------------------------------------------------
void CForest::mort_neutral_random_tree(int nmort)
{
int countMort{0};
int isquare{0};
int itree{0};
CSquare *pSquare = nullptr;
CTree *pTree = nullptr;
do {
//choose random square with probabilities of relative abundances
isquare = GetRandSquare();
pSquare = SquareVec[isquare];
//random tree in square
itree = RandGen1->IRandom(0,pSquare->TreeList.size()-1);
pTree = pSquare->TreeList[itree];
//update square
--pSquare->SpecMap[pTree->SpecID];
if (pSquare->SpecMap[pTree->SpecID] == 0)
pSquare->SpecMap.erase(pTree->SpecID);
++pSquare->nDeath_t1;
--pSquare->nTrees;
//update forest
--SpecAbund[pTree->SpecID];
if (SpecAbund[pTree->SpecID] == 0)
SpecAbund.erase(pTree->SpecID);
--nTreesPlot;
pSquare->TreeList.erase(pSquare->TreeList.begin()+itree);
delete pTree;
++countMort;
if (countMort == nmort) break; //finishes FOR loop
} while (countMort < nmort);
}
//---------------------------------------------------------------------------
void CForest::recruit_neutral_random_mother_fw(int nrecruit)
{
int countRecPlot{0};
int isquare{0};
int itree{0};
int SpecID_mother{0};
double x_new{0.0}, y_new{0.0};
double rand1;
CSquare *pSquare = nullptr;
CTree *pTree1 = nullptr;
CTree *pTree2 = nullptr;
int iX{0};
int iY{0};
do {
//choose random square with probabilities of relative abundances
isquare = GetRandSquare();
pSquare = SquareVec[isquare];
//random tree in square
itree = RandGen1->IRandom(0,pSquare->TreeList.size()-1);
pTree1 = pSquare->TreeList[itree];
rand1 = RandGen1->Random();
//immigration of novel species
if (rand1 < Pars->pImmi){
SpecMax++;
SpecID_mother = SpecMax;
//random position in plot
x_new = RandGen1->Random()*Xmax;
y_new = RandGen1->Random()*Ymax;
}
else {
//long-distance dispersal in the plot
if ((rand1 >= Pars->pImmi) & (rand1 < (Pars->pImmi+Pars->pLDD))){
//random position in plot
x_new = RandGen1->Random()*Xmax;
y_new = RandGen1->Random()*Ymax;
//random tree in Square
SpecID_mother = pTree1->SpecID;
}
else {
//local dispersal
x_new = pTree1->X;
y_new = pTree1->Y;
GetNewXY(x_new,y_new);
SpecID_mother = pTree1->SpecID;
}
} // if not immigration
MaxTreeID++;
pTree2 = new CTree(MaxTreeID,x_new,y_new,SpecID_mother);
iX = floor(pTree2->X/SquareSize);
iY = floor(pTree2->Y/SquareSize);
pSquare = SquareGrid[iX][iY];
pSquare->RecruitList.push_back(pTree2);
++pSquare->nRecruits_t1;
++countRecPlot;
} while (countRecPlot < nrecruit);
}
//---------------------------------------------------------------------------
void CForest::recruit_neutral_bw(int nrecruit, bool dens_dep)
{
CTree* pTree = nullptr;
CSquare* pSquare = nullptr;
CSquare* pSquare_mother = nullptr;
//int iX, iY
int SpecID_mother, irand;
double x_new, y_new, rand1;
int countRecPlot{0};
int isquare{0};
int sq_ntrees{0};
do {
//choose random square with or without density dependence
if (dens_dep == true) isquare = GetRandSquare();
else isquare = RandGen1->IRandom(0,nSquares-1);
pSquare = SquareVec[isquare];
//random position in cell
x_new = (pSquare->iX + RandGen1->Random() )*SquareSize;
y_new = (pSquare->iY + RandGen1->Random() )*SquareSize;
//find mother tree
rand1 = RandGen1->Random();
//immigration of novel species
if (rand1 < Pars->pImmi){
SpecMax++;
SpecID_mother = SpecMax;
}
else {
//long-distance dispersal in the plot
if ((rand1 >= Pars->pImmi) & (rand1 < (Pars->pImmi+Pars->pLDD))){
//random square with trees
do {
irand = RandGen1->IRandom(0,nSquares-1);
pSquare_mother = SquareVec[irand];
sq_ntrees = pSquare_mother->TreeList.size();
} while (sq_ntrees == 0);
//random tree in Square
SpecID_mother = pSquare_mother->TreeList[RandGen1->IRandom(0,sq_ntrees-1)]->SpecID;
}
else {
//local dispersal from one of the eight neighbors
SpecID_mother = GetSpecID_mother(x_new,y_new);
}
} // if not immigration
++MaxTreeID;
pTree = new CTree(MaxTreeID,x_new,y_new,SpecID_mother);
pSquare->RecruitList.push_back(pTree);
++pSquare->nRecruits_t1;
++countRecPlot;
} while (countRecPlot < nrecruit);
}
//---------------------------------------------------------------------------
void CForest::recruit_data_bw(int nrecruit, bool replace_sq, bool dens_dep)
{
CTree* pTree = nullptr;
CSquare* pSquare = nullptr;
CSquare* pSquare_mother = nullptr;
//int iX, iY
int SpecID_mother, irand;
double x_new, y_new, rand1;
int countRecPlot{0}, countRecSq{0};
int isquare{0};
int sq_ntrees{0};
int nRecSq{0.0};
int dens_cl{0};
int nsq_dens{0};
int nData = nRecruitObs.size();
if (!replace_sq) random_shuffle(SquareVec.begin(),SquareVec.end()); //random permutation of square order
do {
//choose random square without density dependence
if (replace_sq) isquare = RandGen1->IRandom(0,nSquares-1);
pSquare = SquareVec[isquare];
if (dens_dep){
dens_cl = floor(pSquare->TreeList.size()/DensBin);
if (dens_cl < DensCl_Min) dens_cl = DensCl_Min;
if (dens_cl > DensCl_Max) dens_cl = DensCl_Max;
nsq_dens = nRecruitObsDens[dens_cl].size();
nRecSq = nRecruitObsDens[dens_cl][RandGen1->IRandom(0,nsq_dens-1)];
}
else {
nRecSq = nRecruitObs[RandGen1->IRandom(0,nData-1)];
}
countRecSq = 0;
do {
//random position in cell
x_new = (pSquare->iX + RandGen1->Random() )*SquareSize;
y_new = (pSquare->iY + RandGen1->Random() )*SquareSize;
//find mother tree
rand1 = RandGen1->Random();
//immigration of novel species
if (rand1 < Pars->pImmi){
SpecMax++;
SpecID_mother = SpecMax;
}
else {
//long-distance dispersal in the plot
if ((rand1 >= Pars->pImmi) & (rand1 < (Pars->pImmi+Pars->pLDD))){
//random square with trees
do {
irand = RandGen1->IRandom(0,nSquares-1);
pSquare_mother = SquareVec[irand];
sq_ntrees = pSquare_mother->TreeList.size();
} while (sq_ntrees == 0);
//random tree in Square
SpecID_mother = pSquare_mother->TreeList[RandGen1->IRandom(0,sq_ntrees-1)]->SpecID;
}
else {
//local dispersal from one of the eight neighbors
SpecID_mother = GetSpecID_mother(x_new,y_new);
}
} // if not immigration
++MaxTreeID;
pTree = new CTree(MaxTreeID,x_new,y_new,SpecID_mother);
pSquare->RecruitList.push_back(pTree);
++pSquare->nRecruits_t1;
++countRecSq;
++countRecPlot;
} while ((countRecSq < nRecSq) && (countRecPlot < nrecruit));
if (!replace_sq){
++isquare;
if (isquare == nSquares){
random_shuffle(SquareVec.begin(),SquareVec.end());
isquare = 0;
}
}
} while (countRecPlot < nrecruit);
}
//---------------------------------------------------------------------------
void CForest::mort_data(int nmort, bool replace_sq, bool dens_dep)
{
CSquare *pSquare = nullptr;
CTree *pTree = nullptr;
int countMortPlot{0};
int isquare{0};
int sq_ntrees{0};
int itree{0};
int dens_cl{0};
int nsq_dens{0};
double pMortSq{0};
int nData = pDeathObs.size();
// run over all squares and assign potential mortality
for (auto &square: SquareVec){
if (dens_dep){
dens_cl = (int) floor(square->TreeList.size()/DensBin);
if (dens_cl < DensCl_Min) dens_cl = DensCl_Min;
if (dens_cl > DensCl_Max) dens_cl = DensCl_Max;
nsq_dens = pDeathObsDens[dens_cl].size();
pMortSq = pDeathObsDens[dens_cl][RandGen1->IRandom(0,nsq_dens-1)];
}
else {
pMortSq = pDeathObs[RandGen1->IRandom(0,nSquares-1)];
}
square->pDeath = pMortSq;
}
if (!replace_sq) random_shuffle(SquareVec.begin(),SquareVec.end()); //random permutation of square order
// kill trees in random order
do {
//if (replace_sq) isquare = RandGen1->IRandom(0,nSquares-1);
isquare = GetRandSquare();
pSquare = SquareVec[isquare];
sq_ntrees = pSquare->TreeList.size();
if (sq_ntrees > 0){
itree = RandGen1->IRandom(0,sq_ntrees-1); //random tree from square
pTree = pSquare->TreeList[itree];
//check if tree dies
if (RandGen1->Random() < pSquare->pDeath){
//update local abundances
pSquare->SpecMap[pTree->SpecID]--;
if (pSquare->SpecMap[pTree->SpecID] == 0)
pSquare->SpecMap.erase(pTree->SpecID);
++pSquare->nDeath_t1;
--pSquare->nTrees;
//update forest abundances
SpecAbund[pTree->SpecID]--;
if (SpecAbund[pTree->SpecID] == 0)
SpecAbund.erase(pTree->SpecID);
--nTreesPlot;
++countMortPlot;
//remove tree
pSquare->TreeList.erase(pSquare->TreeList.begin()+itree);
delete pTree;
}
} // if sq_nTrees >0
if (!replace_sq){
++isquare;
if (isquare == nSquares){
random_shuffle(SquareVec.begin(),SquareVec.end());
isquare = 0;
}
}
} while (countMortPlot < nmort);
}
//---------------------------------------------------------------------------
void CForest::mort_data_cluster(int nmort, bool replace_sq, bool dens_dep)
{
CSquare *pSquare = nullptr;
CTree *pTree = nullptr;
int countMortPlot{0};
int isquare{0};
int sq_ntrees{0};
int itree{0};
int dens_cl{0};
int nsq_dens{0};
double pMortSq{0.0};
int nData = nDeathObs.size();
// run over all squares and assign potential mortality
for (auto square: SquareVec){
if (dens_dep){
dens_cl = (int) floor(square->TreeList.size()/DensBin);
if (dens_cl < DensCl_Min) dens_cl = DensCl_Min;
if (dens_cl > DensCl_Max) dens_cl = DensCl_Max;
nsq_dens = pDeathObsDens[dens_cl].size();
pMortSq = pDeathObsDens[dens_cl][RandGen1->IRandom(0,nsq_dens-1)];
}
else {
pMortSq = pDeathObs[RandGen1->IRandom(0,nSquares-1)];
}
square->pDeath = pMortSq;
}
if (!replace_sq) random_shuffle(SquareVec.begin(),SquareVec.end()); //random permutation of square order
// kill trees in random order
do {
if (replace_sq) isquare = RandGen1->IRandom(0,nSquares-1);
pSquare = SquareVec[isquare];
sq_ntrees = pSquare->TreeList.size();
if (sq_ntrees > 0){
//check all trees from square
for (TreeIterV itree = pSquare->TreeList.begin();
itree != pSquare->TreeList.end();){
pTree = (*itree);
//check if tree dies
if (RandGen1->Random() < pSquare->pDeath){
//update local abundances
pSquare->SpecMap[pTree->SpecID]--;
if (pSquare->SpecMap[pTree->SpecID] == 0)
pSquare->SpecMap.erase(pTree->SpecID);
pSquare->nDeath_t1++;
//update forest abundances
SpecAbund[pTree->SpecID]--;
if (SpecAbund[pTree->SpecID] == 0)
SpecAbund.erase(pTree->SpecID);
--nTreesPlot;
++countMortPlot;
//remove tree
itree = pSquare->TreeList.erase(itree);
delete pTree;
if (countMortPlot == nmort)
break;
} // if death
else {
++itree;
}
} //for all trees in
} // if sq_nTrees >0
if (!replace_sq){
++isquare;
if (isquare == nSquares){
random_shuffle(SquareVec.begin(),SquareVec.end());
isquare = 0;
}
}
} while (countMortPlot < nmort);
}
//---------------------------------------------------------------------------
void CForest::mort_data_cluster2(int nmort, bool replace_sq, bool dens_dep)
{
CSquare *pSquare = nullptr;
CTree *pTree = nullptr;
int countMortPlot{0};
int countMortSq{0};
int isquare{0};
int sq_ntrees{0};
int itree{0};
int dens_cl{0};
int nsq_dens{0};
int nMortSq{0};
if (!replace_sq) random_shuffle(SquareVec.begin(), SquareVec.end()); //random permutation of square order
// kill trees in random order
do {
isquare = GetRandSquare();
//if (replace_sq) isquare = RandGen1->IRandom(0,nSquares-1);
pSquare = SquareVec[isquare];
if (dens_dep){
dens_cl = (int) floor(pSquare->TreeList.size()/DensBin);
if (dens_cl < DensCl_Min) dens_cl = DensCl_Min;
if (dens_cl > DensCl_Max) dens_cl = DensCl_Max;
nsq_dens = nDeathObsDens[dens_cl].size();
nMortSq = nDeathObsDens[dens_cl][RandGen1->IRandom(0,nsq_dens-1)];
}
else {
nMortSq = nDeathObs[RandGen1->IRandom(0,nSquares-1)];
}
countMortSq = 0;
while (((countMortSq < nMortSq) && (pSquare->TreeList.size()>0)) && (countMortPlot < nmort)){
//random tree from square
sq_ntrees = pSquare->TreeList.size();
itree = RandGen1->IRandom(0,sq_ntrees-1);
pTree = pSquare->TreeList[itree];
//update local abundances
--pSquare->SpecMap[pTree->SpecID];
if (pSquare->SpecMap[pTree->SpecID] == 0)
pSquare->SpecMap.erase(pTree->SpecID);
++pSquare->nDeath_t1;
//update forest abundances
-- SpecAbund[pTree->SpecID];
if (SpecAbund[pTree->SpecID] == 0)
SpecAbund.erase(pTree->SpecID);
--nTreesPlot;
++countMortPlot;
++countMortSq;
//remove tree
pSquare->TreeList.erase(pSquare->TreeList.begin() + itree);
delete pTree;
} // while ((countMortSq < nMortSq) && (countMortPlot < nmort)){
if (!replace_sq){
++isquare;
if (isquare == nSquares){
random_shuffle(SquareVec.begin(),SquareVec.end());
isquare = 0;
}
}
} while (countMortPlot < nmort);
}
//---------------------------------------------------------------------------
void CForest::Rec_Dens_Data3 (int nrecruit)
{
CTree* pTree;
CSquare* pSquare;
CSquare* pSquare_mother;
//int iX, iY;
int SpecID_mother, irand;
double x_new, y_new, rand1;
int countRecPlot = 0;
int countRecSq = 0;
int isquare = 0;
int nRecSq, sq_ntrees;
int dens_cl, nsq_dens;
random_shuffle(SquareVec.begin(),SquareVec.end()); //random permutation of square order
do {
pSquare = SquareVec[isquare];
//dens_cl = (int) floor(pSquare->nTrees_t0/DensBin);
dens_cl = (int) floor(pSquare->TreeList.size()/DensBin);
if (dens_cl < DensCl_Min) dens_cl = DensCl_Min;
if (dens_cl > DensCl_Max) dens_cl = DensCl_Max;
nsq_dens = nRecruitObsDens[dens_cl].size();