Created
February 20, 2013 21:17
-
-
Save tpoisot/4999699 to your computer and use it in GitHub Desktop.
Niche model in c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// gcc niche.c -o nm -lgsl -lgslcblas -O3 -DHAVE_INLINE | |
// clang niche.c -o nm -lgsl -lgslcblas -O3 -DHAVE_INLINE | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <math.h> | |
#include <time.h> | |
#include <string.h> | |
#include <gsl/gsl_rng.h> | |
#include <gsl/gsl_randist.h> | |
int has_niche(double n, double c, double r) { | |
int hl = 0; | |
if ((n < c+r) && (n > c-r)) { | |
hl = 1; | |
} | |
return hl; | |
} | |
int main(int argc, char *argv[]) { | |
clock_t start, stop; | |
// Initiate the GSL random number generator | |
gsl_rng *rng = gsl_rng_alloc(gsl_rng_taus2); | |
gsl_rng_set(rng, time(NULL)); | |
unsigned int S = 100; | |
double C = 0.02; | |
// Let's generate three arrays of n, r, and c | |
double *n = (double*) malloc(S * sizeof(double)); | |
double *r = (double*) malloc(S * sizeof(double)); | |
double *c = (double*) malloc(S * sizeof(double)); | |
// We then generate random values of n | |
int sp; | |
for (sp = 0; sp < S; ++sp) { | |
n[sp] = gsl_ran_flat (rng, 0.0, 1.0); | |
r[sp] = n[sp]*gsl_ran_beta(rng, 1, 1/(2*C)-1); | |
c[sp] = gsl_ran_flat(rng, r[sp]/2, n[sp]); | |
printf ("%.4f %.4f %.4f\n", n[sp], c[sp], r[sp]); | |
} | |
for (int sp1 = 0; sp1 < S; ++sp1) { | |
for (int sp2 = 0; sp2 < S; ++sp2) { | |
printf("%d %d %d\n", sp1, sp2, has_niche(n[sp2], c[sp1], r[sp1])); | |
} | |
} | |
gsl_rng_free(rng); | |
return EXIT_SUCCESS; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment