-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctionsALUMSS.cpp
More file actions
1288 lines (1117 loc) · 40.3 KB
/
functionsALUMSS.cpp
File metadata and controls
1288 lines (1117 loc) · 40.3 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
/*
Implementation of all the functions required for the simulation and used in the
main program.
*/
#include "functionsALUMSS.h"
#include <boost/config.hpp>
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <utility>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/connected_components.hpp>
#include <math.h>
#include <stdio.h>
#include <gsl/gsl_rng.h>
#include <gsl/gsl_randist.h>
using namespace std;
/*
1-Helper functions
2-Calculation of Ecosystem Service provision
3-Calculation of events' propensities
4-Initialization functions
5-ODEs and solver
*/
///////////////////////////////////////////////////////////////////////////////
// 1- Helper functions:
// - getNeighbourMatrix
// - getNeighbours
// - getNeighboursState
///////////////////////////////////////////////////////////////////////////////
void getNeighbourMatrix(vector<vector<unsigned int>> &neighbourMatrix, unsigned int n, double d)
{
/*fills a vector containing the neighbours indexes for each patch*/
int ix,jx,xi,yi,xj,yj;
unsigned int dx,dy;
unsigned int manhattanDist;
neighbourMatrix.resize(n*n);
for (ix=0; ix<neighbourMatrix.size(); ++ix){
xi = ix%n;
yi = (int)ix/n;
for (xj=0; xj<n; xj++){
dx=abs(xi-xj);
// calculating cyclic distances to account for periodic borders
if (dx>n/2){
dx=n-dx;
}
for (yj=0; yj<n; yj++){
dy=abs(yi-yj);
// calculating cyclic distances to account for periodic borders
if (dy>n/2){
dy=n-dy;
}
manhattanDist = dx+dy;
if (manhattanDist<=d and manhattanDist>0){
jx = xj + yj*n;
neighbourMatrix[ix].push_back(jx);
}
}
}
}
return;
}
void getNeighbours(vector<unsigned int> &neighboursList, const vector<vector<unsigned int>> &neighbourMatrix, unsigned int i)
{
neighboursList = neighbourMatrix[i];
return;
}
void getNeighboursState(vector<unsigned int> &neighboursState, const vector<vector<unsigned int>> &neighbourMatrix, const vector<unsigned int> &landscape, unsigned int i, unsigned int state)
{
/*
fills the vector neighboursState so that it contains the indexes of all the
closest neighbours of i in a given state. the landscape is passed as a constant
reference so that the vector cannot be modified by the function in main
*/
/*
getting the neighbours indexes in neighbour_list vector
*/
vector<unsigned int> neighboursList;
getNeighbours(neighboursList,neighbourMatrix,i);
/*
getting the index of neighbours in the wanted state
*/
unsigned long ix;
for (ix=0 ; ix<neighboursList.size() ; ++ix){
if (landscape[neighboursList[ix]] == state) {
neighboursState.push_back( neighboursList[ix] );
}
}
return;
}
////////////////////////////////////////////////////////////////////////////////
// 2- Calculation of Ecosystem Service provision:
// - getNaturalConnectedComponents
// - updateNCCadding
// - updateNCCremoving
// - getEcosystemServiceProvision
////////////////////////////////////////////////////////////////////////////////
void getNaturalConnectedComponents(vector<vector<int>> &naturalComponents, const vector<unsigned int> &landscape)
{
/*
fills a vector where each member is a vector containing the indexes of all the
natural patches belonging to the same cluster
*/
vector<unsigned int> naturalPatches;
unsigned int manhattanDist;
unsigned int i, j;
int xi, xj, yi, yj;
unsigned int dx, dy;
unsigned int n = (unsigned int) sqrt(landscape.size());
// clearing natural components vector for refilling
naturalComponents.clear();
/*
get the list of natural patches in the landscape
*/
for(i=0 ; i<landscape.size() ; ++i){
if (landscape[i]==0){
naturalPatches.push_back(i);
}
}
/*
create an undirected graph with the set of natural patches to calculate
the connected components. to estimate whether two patches are connected
we calculate the manhattan distance between them
*/
using namespace boost;
{
typedef adjacency_list< vecS, vecS, undirectedS > Graph;
Graph G;
for(i=0 ; i<naturalPatches.size() ; ++i){
add_edge(i,i,G);
// converting 1-D coordinates to 2-D
xi=naturalPatches[i]%n;
yi=(int)naturalPatches[i]/n;
for(j=0 ; j<i ; ++j){
// converting 1-D coordinates to 2-D
xj=naturalPatches[j]%n;
yj=(int)naturalPatches[j]/n;
// calculating manhattan distance between points
dx=abs(xi-xj);
dy=abs(yi-yj);
// calculating cyclic distances to account for periodic borders
if (dx>n/2){
dx=n-dx;
}
if (dy>n/2){
dy=n-dy;
}
//
manhattanDist=dx+dy;
if ( manhattanDist<2 ){
add_edge(i, j, G);
}
}
}
/*
initializing the vector containing the components and calculating components
*/
vector<int> component(num_vertices(G));
int num = connected_components(G, &component[0]);
/*
converting the nodes indexes into actual landscape coordinates
*/
naturalComponents.resize(num);
for (i=0 ; i<naturalComponents.size() ; ++i){
for (j=0; j<component.size(); j++){
if(component[j]==i){
naturalComponents[i].push_back(naturalPatches[j]);
}
}
}
}
return;
}
void updateNCCadding(vector<vector<int>> &naturalComponents, const vector<vector<unsigned int>> &neighbourMatrix, const vector<unsigned int> &landscape, unsigned int i)
{
vector<unsigned int> neighboursNatural;
getNeighboursState(neighboursNatural,neighbourMatrix,landscape,i,0); // state 0 is natural
vector<int> newNaturalComponent, newNaturalComponent2;
newNaturalComponent.push_back(i);
if(neighboursNatural.size()==0){ //no natural neighbour
naturalComponents.push_back(newNaturalComponent); // add it to the list
}
else{
vector<unsigned int>::iterator it1;
vector<vector<int>>::iterator it2;
vector<int>::iterator it3;
vector<vector<vector<int>>::iterator> toErase;
/*
first traversing the natural components and then the neighbours guarantees that
pointers to components are located in a sorted way. this is key to ensure
that the erasing process doesn't mess up with the memory.
*/
unsigned int neighboursFound = 0;
for(it2=naturalComponents.begin();it2!=naturalComponents.end();it2++){ // traverse all the components
for(it1=neighboursNatural.begin();it1!=neighboursNatural.end();it1++){ // traverse all the natural neigbhours
if( find( it2->begin(), it2->end(), *it1) != it2->end() ){ // found a neighbour in this component
neighboursFound+=1;
if ( find( toErase.begin(), toErase.end(), it2 ) == toErase.end() ){
toErase.push_back(it2);
}
}
}
if(neighboursFound==neighboursNatural.size()){ // end the search if all neighbours were located
break;
}
}
// if there is a single component in toErase just add i to that component, no need of merging
if (toErase.size()>0 && toErase.size()<2){
toErase[0]->push_back(i);
}
else if(toErase.size()>1){ // if there are more, erase them and push back the merged ones
// erasing components that are going to be merged
long ix;
for(ix=0;ix<toErase.size();ix++){
for(it3=toErase[ix]->begin();it3!=toErase[ix]->end();it3++){
newNaturalComponent.push_back(*it3);
}
}
/*now erase the components: traverse erase vector backwards to be sure of
addressing the correct bits of memory erasing first the furthest pointers*/
for(ix=toErase.size();ix>=1;ix--){
naturalComponents.erase(toErase[ix-1]);
}
/*now add the new natural component*/
naturalComponents.push_back(newNaturalComponent);
}
else{ // toErase is empty, in which case there is an error in the code
cout << "Error: toErase size is " << toErase.size() << " but toErase cannot be empty\n";
}
}
return;
}
void updateNCCremoving(vector<vector<int>> &naturalComponents, const vector<unsigned int> &landscape, int l)
{
//cout <<"\n";
unsigned int n = (unsigned int) sqrt(landscape.size());
// find cluster of cell l
unsigned long ix;
// these iterators are to traverse the naturalComponents
vector<vector<int>>::iterator it1;
vector<int>::iterator it2;
// iterating over natural components and checking to which natural component
// belonged the natural cell that needs to be removed.
//this iterator is to store the component to which the removed cell belonged
vector<vector<int>>::iterator itComp;
// iterate over components
for(it1=naturalComponents.begin();it1!=naturalComponents.end();it1++){
// find whether natural cell l is in this natural component
it2=find(it1->begin(), it1->end(),l);
// if it is then erase it and store the component
if(it2!=it1->end()){
it1->erase(it2);
itComp =it1;
break;
}
}
// fill natural patches to get connected natural components
vector<unsigned int> naturalPatches;
for (it2=itComp->begin();it2!=itComp->end();it2++){
naturalPatches.push_back(*it2);
}
// erase concerned cluster from naturalComponents
naturalComponents.erase(itComp);
//get connected components from the naturalPatches
unsigned int manhattanDist;
unsigned int i, j;
int xi, xj, yi, yj;
unsigned int dx, dy;
/*
create an undirected graph with the set of natural patches to calculate
the connected components. to estimate whether two patches are connected
we calculate the manhattan distance between them
*/
using namespace boost;
{
typedef adjacency_list< vecS, vecS, undirectedS > Graph;
Graph G;
for(i=0 ; i<naturalPatches.size() ; ++i){
add_edge(i,i,G);
// converting 1-D coordinates to 2-D
xi=naturalPatches[i]%n;
yi=(int)naturalPatches[i]/n;
for(j=0 ; j<i ; ++j){
// converting 1-D coordinates to 2-D
xj=naturalPatches[j]%n;
yj=(int)naturalPatches[j]/n;
// calculating manhattan distance between points
dx=abs(xi-xj);
dy=abs(yi-yj);
// calculating cyclic distances to account for periodic borders
if (dx>n/2){
dx=n-dx;
}
if (dy>n/2){
dy=n-dy;
}
//
manhattanDist=dx+dy;
if ( manhattanDist<2 ){
add_edge(i, j, G);
}
}
}
/*
initializing the vector containing the components and calculating components
*/
vector<int> component(num_vertices(G));
int num = connected_components(G, &component[0]);
// adding the new components to naturalComponents
vector<int> newComponent;
for(i=0; i<num; i++){
for(j=0; j<component.size(); j++){
if(i==component[j]){
newComponent.push_back(naturalPatches[j]);
}
}
naturalComponents.push_back(newComponent);
newComponent.clear();
}
}
return;
}
void getEcosystemServiceProvision(vector<double> &ecosystemServices, const vector<vector<int>> &naturalComponents, const vector<vector<unsigned int>> &neighbourMatrix, const vector<unsigned int> &landscape, double sar)
{
/*
returns the exposure to the wanted state of patch i. currently it is only used
for the exposure to nature. the exposure to nature scales with biodiversity
hence like a SAR, where the area is the total natural area in contact with
patch i
*/
double area;
double ecosystemServiceProvision;
vector<unsigned int> neighboursState;
unsigned long i,ix,jx;
unsigned int nNeighbours;
nNeighbours = neighbourMatrix[0].size();
ecosystemServices.clear();
for(i=0;i<landscape.size();i++){
ecosystemServiceProvision=0;
/*
getting the state neighbours indexes in neighboursState vector
*/
getNeighboursState(neighboursState,neighbourMatrix,landscape,i,0); // state 0 is natural
/*
calculate the area of each of the neighbour's component
*/
for (ix=0;ix<neighboursState.size();ix++){
// for each of the natural neighbours check their cluster membership
for (jx=0; jx<naturalComponents.size(); jx++){
// check if neighbour belongs to cluster jx
if (find( naturalComponents[jx].begin(),naturalComponents[jx].end(),neighboursState[ix]) != naturalComponents[jx].end()){
area=(double)naturalComponents[jx].size()/landscape.size();
ecosystemServiceProvision+=(double) pow(area,sar)/nNeighbours;
break;
}
}
}
ecosystemServices.push_back(ecosystemServiceProvision);
neighboursState.clear();
}
return;
}
////////////////////////////////////////////////////////////////////////////////
// 3- Calculation of events' propensities:
// - esSaturationFunction
// - getAgriculturalProduction
// - getConsumptionDeficit
// - getSpontaneousPropensity
// - getAgroPropensity
// - getAbandonmentPropensity
// - getPropensityVector
////////////////////////////////////////////////////////////////////////////////
void getAgriculturalProduction(vector<double> &agriculturalProduction, const vector<unsigned int> &landscape, const vector<double> &ecosystemServices, double ksi, double y0)
{
/*
returns the total agricultural production for a given "landscape" and
minimum yield "y". the minimum yield is for a cropped patch with only
non natural neighbours. natural neighbours raise yield.
*/
agriculturalProduction.clear();
unsigned long ix;
for (ix=0 ; ix<landscape.size() ; ++ix){
if(landscape[ix]==2){ // cropped patches
// putting baseline production 0.5 as a test...
agriculturalProduction.push_back( y0 + ecosystemServices[ix] ) ;
}
else if(landscape[ix]==3){ //intense
agriculturalProduction.push_back( ksi );
}
else{
agriculturalProduction.push_back(0);
}
}
return;
}
double getConsumptionDeficit(const vector<double> &agriculturalProduction, const vector<double> &population)
{
double totalAgriculturalProduction=0;
double consumptionDeficit;
unsigned long ix;
for(ix=0;ix<agriculturalProduction.size();ix++){
totalAgriculturalProduction+=agriculturalProduction[ix];
}
if(population[0]>0){
consumptionDeficit=population[0] - totalAgriculturalProduction;
}
else{
consumptionDeficit=0;
}
return consumptionDeficit;
}
void getSpontaneousPropensity(vector<double> &recoveryPropensity, vector<double> °radationPropensity, const vector<unsigned int> &landscape, const vector<double> &ecosystemServices, double Tr, double Td)
{
/*
fills the recoveryPropensity vector and the degradationPropensity vector
*/
/*
traverse the landscape and calculate the recovery propensity for the degraded
patches and the degradation propensity for the natural patches
*/
unsigned long ix;
for (ix=0 ; ix<landscape.size() ; ++ix){
if (landscape[ix] == 1) { // if patch ix is degraded
recoveryPropensity.push_back( 1/Tr * ecosystemServices[ix] );
degradationPropensity.push_back(0);
}
else if(landscape[ix] == 0){
recoveryPropensity.push_back( 0 );
degradationPropensity.push_back( 1/Td*( 1 - ecosystemServices[ix] ));
}
else{
recoveryPropensity.push_back( 0 );
degradationPropensity.push_back( 0 );
}
}
return;
}
void getAgroPropensity(vector<double> &expansionPropensity, vector<double> &intensePropensity, const vector<vector<unsigned int>> &neighbourMatrix, const vector<unsigned int> &landscape, const vector<double> &agriculturalProduction, const vector<double> &population, double w, double a, double Tag)
{
/*
fills the cropping_propensity and restoring_propensity vector
*/
// use them to store the accumulated probability in order to normalize afterwards
double expansionCumSum = 0;
double intenseCumSum = 0;
/*
first checking if there is a consumption deficit that justifys human action
*/
double consumptionDeficit = getConsumptionDeficit(agriculturalProduction,population);
if (consumptionDeficit<0){ // if humans are satisfied...
unsigned long ix;
for(ix=0 ; ix<landscape.size() ; ++ix){
expansionPropensity.push_back( 0 ); // ... they do not transform the landscape
intensePropensity.push_back( 0 ); // ... they do not transform the landscape
}
}
else{ // if humans are not happy with they current resource access ...
/*
in this loop we calculate the probabilities of choosing one patch over other
for cropping as a function of the clustering parameter w. we also signal
all the degraded patches that can be restored. the restoration probability
is uniform over space.
*/
unsigned int ix;
vector<unsigned int> organicNeighbours;
vector<unsigned int> intenseNeighbours;
for (ix=0 ; ix<landscape.size() ; ++ix){
if (landscape[ix] == 0){ // if patch is natural, hence can be converted to organic
// get the indexes of the organic neighbours
organicNeighbours.clear();
getNeighboursState(organicNeighbours,neighbourMatrix,landscape,ix, 2); // state 2 is organic
// cropping probability expression is taken from bart's and dani's paper
expansionPropensity.push_back( pow( max(0.1 , (double)organicNeighbours.size() ) , w ) );
expansionCumSum += expansionPropensity.back();
intensePropensity.push_back( 0 );
}
else if (landscape[ix] == 2 ){ // if patch is organic it can be intensifyed
// get the indexes of the intense neighbours
intenseNeighbours.clear();
getNeighboursState(intenseNeighbours,neighbourMatrix,landscape,ix, 3); // state 3 is intense
intensePropensity.push_back( pow( max(0.1 , (double)intenseNeighbours.size() ) , w ) );
intenseCumSum += intensePropensity.back();
expansionPropensity.push_back(0);
}
else{
expansionPropensity.push_back(0);
intensePropensity.push_back( 0 );
}
}
/*
now we normalize the previously obtained probabilities and get calculate the
probability per unit time of action given the consumption deficit
*/
if (expansionCumSum>0 && intenseCumSum>0){ // this is to avoid dividing by zero
for (ix=0; ix<landscape.size() ; ++ix){
expansionPropensity[ix] = expansionPropensity[ix] / expansionCumSum / Tag * consumptionDeficit * (1-a);
intensePropensity[ix] = intensePropensity[ix] / intenseCumSum / Tag * consumptionDeficit * a;
}
}
else{
if (expansionCumSum>0){
for (ix=0; ix<landscape.size() ; ++ix){
expansionPropensity[ix] = expansionPropensity[ix] / expansionCumSum / Tag * consumptionDeficit;
}
}
else if (intenseCumSum>0){
if(a>0){
for (ix=0; ix<landscape.size() ; ++ix){
intensePropensity[ix] = intensePropensity[ix] / intenseCumSum / Tag * consumptionDeficit;
}
}
else{ // this makes that in the case a=0 there is no intensification
for (ix=0; ix<landscape.size() ; ++ix){
intensePropensity[ix] = 0;
}
}
}
}
}
return;
}
void getAbandonmentPropensity(vector<double> &naturalAbandonPropensity, vector<double> °radedAbandonPropensity, const vector<unsigned int> &landscape, const vector<double> &ecosystemServices, double Tab)
{
/*
fills the abandonment_propensity vector.
*/
unsigned int ix;
for (ix=0; ix<landscape.size(); ++ix){
if (landscape[ix]==2){ // organic patch
degradedAbandonPropensity.push_back(0);
naturalAbandonPropensity.push_back( 1/Tab * ( 1 - ecosystemServices[ix] ) );
}
else if(landscape[ix]==3){ //intensive patch
degradedAbandonPropensity.push_back( 1/Tab * ( 1 - ecosystemServices[ix] ) );
naturalAbandonPropensity.push_back( 0 );
}
else{ // non cropped patches
naturalAbandonPropensity.push_back(0);
degradedAbandonPropensity.push_back(0);
}
}
return;
}
void getPropensityVector(vector<double> &propensityVector, const vector<vector<unsigned int>> &neighbourMatrix, const vector<unsigned int> &landscape, const vector<double> &ecosystemServices, const vector<double> &agriculturalProduction, const vector<double> &population, double Tr, double Td, double w, double a, double Tag, double Tab)
{
/*
calls all the functions to calculate the propensity of each event and merges
them in a single propensity vector that can be used to run the gillespie algo
*/
vector<double> recoveryPropensity;
vector<double> degradationPropensity;
vector<double> expansionPropensity;
vector<double> intensePropensity;
vector<double> naturalAbandonPropensity;
vector<double> degradedAbandonPropensity;
getSpontaneousPropensity(recoveryPropensity,degradationPropensity,landscape,ecosystemServices,Tr,Td);
getAgroPropensity(expansionPropensity,intensePropensity,neighbourMatrix,landscape,agriculturalProduction,population,w,a,Tag);
getAbandonmentPropensity(naturalAbandonPropensity,degradedAbandonPropensity,landscape,ecosystemServices,Tab);
// clearing the previous propensity vector to refill it
propensityVector.clear();
// making sure the vector is not empty to avoid bad behaviour in next loop
propensityVector.push_back(recoveryPropensity[0]);
unsigned long ix;
// over these loops the cumulative sum of each propensity vector is added to
// the total propensity vector
for (ix=1 ; ix<recoveryPropensity.size() ; ++ix){
propensityVector.push_back(propensityVector.back()+recoveryPropensity[ix]);
}
for (ix=0 ; ix<degradationPropensity.size() ; ++ix){
propensityVector.push_back(propensityVector.back()+degradationPropensity[ix]);
}
for (ix=0 ; ix<expansionPropensity.size() ; ++ix){
propensityVector.push_back(propensityVector.back()+expansionPropensity[ix]);
}
for (ix=0 ; ix<intensePropensity.size() ; ++ix){
propensityVector.push_back(propensityVector.back()+intensePropensity[ix]);
}
for (ix=0 ; ix<naturalAbandonPropensity.size() ; ++ix){
propensityVector.push_back(propensityVector.back()+naturalAbandonPropensity[ix]);
}
for (ix=0 ; ix<degradedAbandonPropensity.size() ; ++ix){
propensityVector.push_back(propensityVector.back()+degradedAbandonPropensity[ix]);
}
return;
}
////////////////////////////////////////////////////////////////////////////////
// 4- Initialization functions:
// - initializeLandscape
// - initializePopulation
// - initializeSES
////////////////////////////////////////////////////////////////////////////////
void initializeLandscape( vector<unsigned int> &landscape, const vector<vector<unsigned int>> &neighbourMatrix, unsigned int n, double a0, double d0, double a, double w, gsl_rng *r)
{
/*
initializes the landscape given a fraction of initial agricultural patches a0 and degraded patches d0
*/
//unsigned int number_cropped_patches = 1;
unsigned long ix,jx,lx;
// this vector contains the indexes of all the natural patches
vector<double> probConversion;
vector<double> probIntense;
vector<double> probDegradation;
double xRand;
unsigned int nao0=(unsigned int) (a0*n*n*(1-a));
unsigned int nai0=(unsigned int) (a0*n*n*a);
unsigned int na0=nao0+nai0;
// first build a completely natural landscape with n*n patches
landscape.push_back(0);
probConversion.push_back(1);
for (ix=1 ; ix<n*n; ++ix){
landscape.push_back(0);
probConversion.push_back(probConversion.back()+1);
probIntense.push_back(0);
}
double cumprob;
vector<unsigned int> organicNeighbours;
for(ix=0;ix<na0;++ix){
jx=0;
cumprob = gsl_rng_uniform(r)*probConversion.back();
while (cumprob > probConversion[jx]){
jx++;
}
landscape[jx]=2;
probIntense[jx]=1;
// recalculating probconversion
if (landscape[0]==0){
organicNeighbours.clear();
getNeighboursState(organicNeighbours,neighbourMatrix,landscape,0, 2);
probConversion[0]=pow( max(0.1 , (double)organicNeighbours.size() ) , w ) ;
}
else{
probConversion[0]=0;
}
for(lx=1;lx<probConversion.size();lx++){
if (landscape[lx]==0){
organicNeighbours.clear();
getNeighboursState(organicNeighbours,neighbourMatrix,landscape,lx, 2);
probConversion[lx]=probConversion[lx-1]+pow( max(0.1 , (double)organicNeighbours.size() ) , w ) ;
}
else{
probConversion[lx]=probConversion[lx-1];
}
}
}
// perform the cumulative sum of probIntense
for(ix=1; ix<probIntense.size(); ix++){
probIntense[ix]=probIntense[ix]+probIntense[ix-1];
}
vector<unsigned int> intenseNeighbours;
for(ix=0;ix<nai0;++ix){
jx=0;
xRand = gsl_rng_uniform(r)*probIntense.back();
while (xRand>probIntense[jx]){
jx++;
}
landscape[jx]=3;
// recalculating probintense
// first initialize the vector to do the cumulative sum
if (landscape[0]==2){
intenseNeighbours.clear();
getNeighboursState(intenseNeighbours,neighbourMatrix,landscape,0, 3);
probIntense[0]=pow( max(0.1 , (double)intenseNeighbours.size() ) , w ) ;
}
else{
probIntense[0]=0;
}
for(lx=1;lx<probIntense.size();lx++){
if (landscape[lx]==2){
intenseNeighbours.clear();
getNeighboursState(intenseNeighbours,neighbourMatrix,landscape,lx, 3);
probIntense[lx]=probIntense[lx-1]+pow( max(0.1 , (double)intenseNeighbours.size() ) , w ) ;
}
else{
probIntense[lx]=probIntense[lx-1];
}
}
}
// now deal with degraded patches
unsigned int nd0=(unsigned int) (d0*n*n);
// initialize probDegradation
if (landscape[0]==0){
probDegradation.push_back(1);
}
else{
probDegradation.push_back(0);
}
// fill probDegradation for first pick
for (ix=1; ix<landscape.size(); ix++){
if (landscape[ix]==0){
probDegradation.push_back(probDegradation.back()+1);
}
else{
probDegradation.push_back(probDegradation.back());
}
}
// now start filling the landscape with the degraded cells
vector<unsigned int> degradedNeighbours;
for (ix=0; ix<nd0; ++ix){
// select to be degraded cell
jx=0;
cumprob = gsl_rng_uniform(r)*probDegradation.back();
while (cumprob > probDegradation[jx]){
jx++;
}
landscape[jx]=1;
// update the probDegradation
// first initialize the vector
if (landscape[0]==0){
degradedNeighbours.clear();
getNeighboursState(degradedNeighbours,neighbourMatrix,landscape,0, 1);
probDegradation[0]=pow( max(0.1 , (double)degradedNeighbours.size() ) , w ) ;
}
else{
probDegradation[0]=0;
}
for(lx=1;lx<probDegradation.size();lx++){
if (landscape[lx]==0){
degradedNeighbours.clear();
getNeighboursState(degradedNeighbours,neighbourMatrix,landscape,lx, 1);
probDegradation[lx]=probDegradation[lx-1]+pow( max(0.1 , (double)degradedNeighbours.size() ) , w ) ;
}
else{
// cout << "lx = " << lx << "\n";
probDegradation[lx]=probDegradation[lx-1];
}
}
}
return;
}
void initializePopulation( vector<double> &population, const vector<double> &agriculturalProduction)
{
/*given an agricultural production, it sets the population at an equilibrium
level
*/
unsigned int ix;
double totalAgriculturalProduction=0;
for(ix=0; ix<agriculturalProduction.size(); ++ix){
totalAgriculturalProduction+=agriculturalProduction[ix];
}
population.push_back(totalAgriculturalProduction);
return;
}
void initializeSES( vector<unsigned int> &landscape, vector<double> &population, vector<vector<int>> &naturalComponents, vector<double> &agriculturalProduction, vector<double> &ecosystemServices, vector<vector<unsigned int>> &neighbourMatrix, vector<vector<unsigned int>> &neighbourMatrixES, unsigned int n, double a0, double d0, double a, double ksi, double y0, double sar, double w, gsl_rng *r)
{
initializeLandscape(landscape,neighbourMatrix,n,a0,d0,a,w,r);
getNaturalConnectedComponents(naturalComponents,landscape);
getEcosystemServiceProvision(ecosystemServices,naturalComponents,neighbourMatrixES,landscape,sar);
getAgriculturalProduction(agriculturalProduction, landscape, ecosystemServices, ksi, y0);
initializePopulation(population,agriculturalProduction);
return;
}
////////////////////////////////////////////////////////////////////////////////
// 5- ODEs and solver:
// - populationEquation
// - rungeKutta4
////////////////////////////////////////////////////////////////////////////////
double populationEquation(double population, double agriculturalProduction)
{
/*
returns the expression of the population ODE
*/
return population*(1-population/agriculturalProduction);
}
void rungeKutta4(vector<double> &population, vector<double> &agriculturalProduction, double dt)
{
/*
returns the actualized population after solving the ODE with runge kutta 4 method
*/
double totalAgriculturalProduction=0;
unsigned long ix;
for(ix=0;ix<agriculturalProduction.size();ix++){
totalAgriculturalProduction+=agriculturalProduction[ix];
}
double k1p,k2p,k3p,k4p;
double p1,p2,p3;
double deltaP;
k1p=populationEquation(population[0],totalAgriculturalProduction);
p1=population[0]+0.5*k1p*dt;
k2p=populationEquation(p1,totalAgriculturalProduction);
p2=population[0]+0.5*k2p*dt;
k3p=populationEquation(p2,totalAgriculturalProduction);
p3=population[0]+k3p*dt;
k4p=populationEquation(p3,totalAgriculturalProduction);
deltaP=dt*(k1p+2*k2p+2*k3p+k4p)/6;
population[0]+=deltaP;
return;
}
////////////////////////////////////////////////////////////////////////////////
// 6- Outputs:
// - getRadiusOfGyration
// - saveAggregated
// - saveLandscape
// - saveComponents
////////////////////////////////////////////////////////////////////////////////
double getRadiusOfGyration(const vector<int> &naturalComponent, unsigned int n)
{
vector<int>::const_iterator it;
unsigned int xi,yi;
double radiusOfGyration=0;
double xMean,yMean;
if (naturalComponent.size()>0){
xMean=0;
yMean=0;
// iterate over the cells of the natural component to get their mean position
for(it=naturalComponent.begin();it!=naturalComponent.end();it++){
xi=(int)*it%n;
yi=(int)*it/n;
xMean+=xi;
yMean+=yi;
}
xMean/= (double) naturalComponent.size();
yMean/= (double) naturalComponent.size();
// now iterate again to calculate the radius of gyration
for(it=naturalComponent.begin();it!=naturalComponent.end();it++){
xi=(int)*it%n;
yi=(int)*it/n;
radiusOfGyration+=sqrt((xi-xMean)*(xi-xMean)+(yi-yMean)*(yi-yMean));
}
radiusOfGyration/= (double) naturalComponent.size();
}
return radiusOfGyration;
}
void saveAggregated(ofstream &file, double t, const vector<double> &population, const vector<unsigned int> &landscape, const vector<double> &agriculturalProduction, const vector<vector<int>> &naturalComponents, const vector<double> &ecosystemServices, unsigned int nn, double ripleyDistance, double nMax, double nMin, double pMax, double pMin)
{
unsigned long numComponents = naturalComponents.size();
unsigned long ix;
double meanSize=0;
double squaredMeanSize=0;
double maxSize=0;
double stdSize;
double componentSize;
for (ix=0;ix<naturalComponents.size();ix++){
componentSize=(double)naturalComponents[ix].size()/ecosystemServices.size();
meanSize+=componentSize;
squaredMeanSize+=componentSize*componentSize;
if(componentSize>maxSize){
maxSize=componentSize;
}
}
if (numComponents>0){
meanSize/=numComponents;
squaredMeanSize/=numComponents;
}
stdSize=squaredMeanSize-meanSize*meanSize;
if(stdSize<0){
stdSize=0;
}
else{
stdSize=sqrt(stdSize);
}
double meanES=0;
double squaredMeanES=0;
double stdES=0;
if (ecosystemServices.size()>0){
for (ix=0;ix<ecosystemServices.size();ix++){
meanES+=ecosystemServices[ix];
squaredMeanES+=ecosystemServices[ix]*ecosystemServices[ix];
}
meanES/=ecosystemServices.size();
squaredMeanES/=ecosystemServices.size();
stdES=sqrt(squaredMeanES-meanES*meanES);