]> pd.if.org Git - afpopgen/blobdiff - sim/sim.c
added file output option
[afpopgen] / sim / sim.c
index 7919300933f8ec045c7deacd6d78cd7d44b22e5e..d0e96695cc584b6bb1f39a8dc339baa772131b66 100644 (file)
--- a/sim/sim.c
+++ b/sim/sim.c
@@ -15,11 +15,17 @@ void hexlistjson(struct afrhex *map);
 static void setpop(struct afrhex *map, int hexid, int *pop) {
        struct afrhex *hex;
        int i;
+       int sum = 0;
 
        hex = find_hex(map, hexid);
        for (i=0; i < 2; i++) {
                hex->freq[i] = pop[i];
+               sum += pop[i];
        }
+       if (sum % 2) {
+               fprintf(stderr, "allele frequency for hex %d not even\n", hexid);
+       }
+       hex->pop = sum/2;
 }
 
 static void setpopstr(struct afrhex *map, char *spec) {
@@ -71,7 +77,7 @@ int main (int ac, char *av[]) {
        clock_gettime(CLOCK_REALTIME, &ts);
        seed = ((unsigned long)(ts.tv_sec) ^ (unsigned long)ts.tv_nsec) & 0xffffffff;
 
-       while ((opt = getopt(ac, av, "r:s:ehp:y:g:m:t:d:")) != -1) {
+       while ((opt = getopt(ac, av, "f:r:s:ehp:y:g:m:t:d:")) != -1) {
                switch (opt) {
                        case 's': seed = atoi(optarg); break;
                        case 'r': run = atoi(optarg); break;
@@ -82,6 +88,11 @@ int main (int ac, char *av[]) {
                        case 'e': eemian = 1; break;
                        case 'h': holocene = 1; break;
                        case 'p': setpopstr(map, optarg); defaultpop = 0;break;
+                       case 'f': if (freopen(optarg, "w", stdout) == NULL) {
+                                         perror("sim");
+                                         exit(EXIT_FAILURE);
+                                 }
+                                 break;
                        default:
                                  fprintf(stderr, "Usage: %s [-s seed] [-r run] [-y startyear] [-eh] [-p hex:freq,freq[:freq,freq...]\n",
                                                  av[0]);
@@ -123,6 +134,7 @@ int main (int ac, char *av[]) {
 
        printf("{");
        printf(" \"run\": %d, \"seed\": %d, \"gens\": %d", run, seed, gens);
+       printf(",\n\"options\": { \"capfactor\": %g }", capfactor);
        printf(",\n\"gendata\": [\n");
 
        dumpgenjson(map, 0, year);