/*** Predator-prey model in continuous space Option to have prey either density dependent or density dependent population growth in the absence of the predator. ***/ #include #include #include /****************************************************************/ /* */ /* GLOBAL VARIABLES + CONSTANTS */ /* */ /****************************************************************/ #define max_bin 20 /*This is the radius of the second moment we are tracking*/ #define out_file "numerical2.rcfs.dat" #define No_file "numerical2.No.dat" #define density_file "results.dat" #define o 0 #define e 1 /***These are the 3 corner weightings for the power 2 closure***/ #define alpha 4.0 /***i corner weighting***/ #define beta 1.0 /***j corner weighting***/ #define Gamma 1.0 /***k corner weighting***/ #define resolution 0 #define check_moments 1 #define nspecies 2 #define closure_method 2 /*2: Power 2, symmetric*/ /*3: Power 3*/ /*1: Power1, symmetric*/ #define movement_kernel 4 /*1:Normal distribution*/ /*2:Exponential distribution*/ /*3:Uniform distribution*/ /*4:Bivariate Normal*/ #define weight_kernel 1 /*1: Normal kernel*/ #define xbin 20 /*These are the number of bins within the*/ #define ybin 20 /*second moment grid*/ #define Pi 3.1415927 #define tmax 25000.0 #define bin_width 0.05 /*Equal to max_bin/xbin*/ double move_stdv[nspecies] = {0.05, 0.05}; double rmax[nspecies] = {0.15, 0.15}; double W_stdv[nspecies][nspecies]; #define radial_max 4 #define rad 4 double N[nspecies] = {100.0, 100.0}; /*Stores first moment*/ FILE *data, *out10, *first; double C2_bin[nspecies][nspecies][2*(xbin-1)+1] [2*(ybin-1)+1]; /*Tracks the cross-correlation function at each distance*/ double C2_rhs[nspecies][nspecies][2*(xbin-1)+1] [2*(ybin-1)+1]; /*Used to compute the rhs at each time step*/ double prob_move [nspecies] [2*(radial_max-1)+1] [2*(radial_max-1)+1]; /***Movement/dispersal kernel***/ double Wc [nspecies] [nspecies] [2*(radial_max-1)+1] [2*(radial_max-1)+1]; /***Neighbour weighting function***/ double B1[nspecies] = {0.4, 0.0}; double D1[nspecies] = {0.1, 0.05}; /***Density independent death rate***/ double D2[nspecies][nspecies]; /***Density dependent death rate***/ double conv[nspecies][nspecies]; double attack[nspecies][nspecies]; double M1[nspecies] = {0.0, 0.0}; double mean_move = 0.0; /*This is the mean length per movement event where the distribution is normal*/ #define dt 1.0/20.0 /*This is the time increment for each step of the integration*/ double equilibrium= 0.001*dt; double elapsed; double tock; FILE *temp_out, *detail, *eq_m; int C2flag=0; double moment_closure(); void write_moments(); double dndt[nspecies]; void write_eq(); /****************************************************************/ /* */ /* Initialise Second moment bins */ /* */ /****************************************************************/ /*This needs to be called once at the beginning of the program*/ void init_C2() { int i, j, spp1, spp2; attack[0][0]=0.0; attack[0][1]=0.002; attack[1][0]=0.00; attack[1][1]=0.0; conv[0][0]=0.0; conv[0][1]=0.3; conv[1][0]=0.0; conv[1][1]=0.0; D2[0][0]=0.001; D2[0][1]=0.00; D2[1][1]=0.00; D2[1][0]=0.00; W_stdv[0][0]=0.05; W_stdv[0][1]=0.05; W_stdv[1][1]=0.05; W_stdv[1][0]=0.05; for(i=0; i<2*(xbin-1)+1; i++) for(j=0; j<2*(ybin-1)+1; j++) for(spp1=0;spp1=0 && move_y>=0 && move_x=0 && m=0 && n0.000001) C2_rhs[spp1][spp2][i][j] += conv[spp2][spp1]*attack[spp2][spp1] * Wc[spp2][spp1][W_x][W_y] * prob_move[spp1][move_x][move_y] * C2_bin[spp2][spp1][m][n]*bin_width*bin_width; if(attack[spp1][spp2]>0.000001) C2_rhs[spp1][spp2][i][j] += conv[spp1][spp2]*attack[spp1][spp2] * Wc[spp1][spp2][W_x][W_y] * prob_move[spp2][move_x][move_y] * C2_bin[spp1][spp2][m][n]*bin_width*bin_width; } } /**************************************/ /* Density dependent deaths */ /**************************************/ if(abs(m-i)0.000001) C2_rhs[spp1][spp2][i][j] -= D2[spp1][k] * Wc[spp1][k][W_x][W_y] * moment_closure(spp1, spp2, k, i, j, (m-i+(xbin-1)), (n-j+(ybin-1)))*bin_width*bin_width; if(D2[spp2][k]>0.000001) C2_rhs[spp1][spp2][i][j] -= D2[spp2][k] * Wc[spp2][k][W_x][W_y] * moment_closure(spp2, spp1, k, i, j, (m-i+(xbin-1)), (n-j+(ybin-1)))*bin_width*bin_width; /****Predation****/ if(attack[spp1][k]>0.000001) C2_rhs[spp1][spp2][i][j] -= attack[spp1][k] * Wc[spp1][k][W_x][W_y] * moment_closure(spp1, spp2, k, i, j, (m-i+(xbin-1)), (n-j+(ybin-1)))*bin_width*bin_width; if(attack[spp2][k]>0.000001) C2_rhs[spp1][spp2][i][j] -= attack[spp2][k] * Wc[spp2][k][W_x][W_y] * moment_closure(spp2, spp1, k, i, j, (m-i+(xbin-1)), (n-j+(ybin-1)))*bin_width*bin_width; /***Creation of predator parent-offspring pairs***/ /***Corrections***/ if(spp1==spp2){ move_x = i - (xbin-1) + radial_max-1; move_y = j - (ybin-1) + radial_max-1; if(move_x>=0 && move_x<2*radial_max-1 && move_y>=0 && move_y <2*radial_max-1){ { C2_rhs[spp1][spp2][i][j]+=attack[k][spp1]*conv[k][spp1]*prob_move[spp1][move_x][move_y] * Wc[k][spp1][W_x][W_y] * C2_bin[k][spp1][m-i+xbin-1][n-j+ybin-1]*bin_width*bin_width; C2_rhs[spp1][spp2][i][j]+=attack[k][spp2]*conv[k][spp2]*prob_move[spp2][move_x][move_y] * Wc[k][spp2][W_x][W_y] * C2_bin[k][spp2][m-i+xbin-1][n-j+ybin-1]*bin_width*bin_width; } } } if(spp1 == 1 || spp2 == 1) for(p=m-rad-1; p=0 && W_y>=0 && W_x<2*radial_max-1 && W_y<2*radial_max-1) { if(conv[k][spp1]>0.00001) C2_rhs[spp1][spp2][i][j] += attack[k][spp1]*conv[k][spp1]*prob_move[spp1][move_x][move_y]*Wc[k][spp1][W_x][W_y]*moment_closure(spp1, spp2, k, m, n, (m-p+(xbin-1)), (n-q+(ybin-1)))*bin_width*bin_width*bin_width*bin_width; if(conv[k][spp2]>0.00001) C2_rhs[spp1][spp2][i][j] += attack[k][spp2] * conv[k][spp2] * prob_move[spp2][move_x][move_y] * Wc[k][spp2][W_x][W_y] * moment_closure(spp2, spp1, k, m, n, (m-p+(xbin-1)), (n-q+(ybin-1)))*bin_width*bin_width*bin_width*bin_width; } } } } } } } } /****************************************************************/ /* */ /* CARRY OUT THE EULER STEP */ /* */ /****************************************************************/ void step() { int m, n, a, b; int i,j; int W_x, W_y; int spp1, spp2; dndt[0]=N[0]; dndt[1]=N[1]; /***Euler step for the first moments***/ for(spp1=0;spp1=0 && W_x<2*radial_max-1 && W_y>=0 && W_y<2*radial_max-1) { N[spp1] -= (double)(dt * D2[spp1][spp2] * Wc[spp1][spp2][W_x][W_y] * C2_bin[spp1][spp2][m][n] * bin_width * bin_width); N[spp1] -= (double)(dt * attack[spp1][spp2] * Wc[spp1][spp2][W_x][W_y] * C2_bin[spp1][spp2][m][n] * bin_width * bin_width); N[spp1] += (double)(dt * attack[spp2][spp1] * conv[spp2][spp1] * Wc[spp2][spp1][W_x][W_y] * C2_bin[spp2][spp1][m][n] * bin_width * bin_width); } } if(N[spp1]<0.0) N[spp1]=0.0; } /* printf("dN1/dt=%lf\tdN2/dt=%lf\n", N[0]-dndt[0], N[1]-dndt[1]); fflush(stdout);*/ /***Euler step for the second moments and neighbour***/ for(m=0; m<2*(xbin-1)+1; m++) for(n=0; n<2*(ybin-1)+1; n++) for(spp1=0;spp1dt) if(fabs(dndt[0]-N[0])=0 && x1=0 && y1=0 && x2=0 && y2=0 && xbin-1+x2-x1=0 && ybin-1+y2-y1 WHOOPEEEEEE\n"); fflush(out); return 0; }