void main(int argc,char **argv) { int i,j,n,dist,c; double density,r; long seed=0; short **con; short *labeled; if((argc != 3 && argc != 5) || (density=atof(argv[2])) >1 || density < 0 || (n=atoi(argv[1])) <= 1){ fprintf(stderr,"USAGE: %s <number of nodes> <density> [-R <rand seed>]\n" "Density is a fraction in (0,1] giving the proability of any of\n" "the n*(n-1)/2 possible edges being in the graph\n" "OUTPUT table of numbers representing lower triangular matrix\n" "of a distance matrix for a set of n nodes with costs in 1--1000\n" "Disconnected nodes have a cost of n*1000\n", argv[0]); exit(1); } if(argc == 5){ seed = atol(argv[1]); seed_all(seed); }else seed = seed_time(); /* initialise random number generator */ con = (short **)malloc(n * sizeof(short *)); labeled = (short *)malloc(n * sizeof(short)); do{ /* keep generating until a connected graph exists */ for(i=0;i<n;i++) labeled[i] = FALSE; labeled[n-1] = TRUE; for(i=n-1;i>0;i--){ con[i] = (short *)malloc(i * sizeof(short)); for(j=0;j<i;j++){ r = rand2(); con[i][j] = (r < density); /* nodes i & j connected */ if(labeled[i] && con[i][j]) labeled[j] = TRUE; if(labeled[j] && con[i][j]) labeled[i] = TRUE; } } do{ /* check if there are any other reachable unlabeled nodes */ c = FALSE; for(i=0;i<n;i++) for(j=0;j<i;j++) if(con[i][j]){ if(!labeled[i] && labeled[j]) c = labeled[i] = TRUE; if(labeled[i] && !labeled[j]) c = labeled[j] = TRUE; } }while(c); c=TRUE; /* connected */ for(i=0;i<n;i++) c = (c && labeled[i]); }while(!c); /* try again if graph is not connected */ for(i=0;i<n;i++){ for(j=0;j<i;j++) printf("%d%c",((con[i][j]) ? irand2(999)+1 : (n*1000)), ( ((j+1)%10) ? ' ' : '\n')); printf("\n"); } }
void main(int argc,char **argv) { int i,n; if(argc != 2 || (n=atoi(argv[1])) <= 1){ fprintf(stderr,"USAGE: %s <number of nodes>\n" "OUTPUT: table of numbers representing lower triangular matrix\n" "of a totaly random distance matrix.\n",argv[0]); exit(1); } seed_time(); for(i=0;i<n*(n-1)/2;i++) printf("%d%c",irand2(500)+1,( (i%10) ? ' ' : '\n')); }
size_t select_random(size_t lo, size_t hi) { static bool first = true; if (first) { // This is a copy of the way GalSim seeds its random number generator using urandom first, // and then if that fails, using the time. // Except we just use this to seed the std rand function, not a boost rng. // Should be fine for this purpose. try { seed_urandom(); } catch(...) { seed_time(); } first = false; } if (lo == hi) { return lo; } else { size_t mid = int((rand() / RAND_MAX) * (hi-lo+1)) + lo; if (mid > hi) mid = hi; // Just in case rand() == RAND_MAX return mid; } }
int main (int argc, char **argv) { // Default simulation parameters char filename[100] = ""; double Tmax = 10000; // length of simulation double Tsample = 1000; // time of first sample (and intervals) int sampleSize = 10; // number of seqs sampled double dt = 1.0; // time-step // Default epidemiological parameters int hostPopSize = 10000; int N0 = 10; // initial viral pop size double gamma = 0.1; // decay of immunity double beta = 0.5; // transmission rate double delta = 0.1; // recovery rate // Default sequence parameters double kappa = 3.0; double mut_rate = 1e-5; int Ld = 0; int Lb = 2; double sb = 0.01; double sd = 1e-3; double sigma = 1.0; // User-specified arguments int opt_char; while ((opt_char = getopt(argc, argv, "ho:T:S:Z:t:H:N:g:b:d:L:u:k:l:a:s:c:")) != -1) { switch (opt_char) { case 'h': printf("%s\n", helpStr); exit(0); break; case 'o': strcpy(filename, optarg); break; case 'T': if ((Tmax = strtod(optarg, NULL)) <= 0.0) { fprintf (stderr, "Invalid -T parameter: %s\n", optarg); exit(1); } break; case 'S': if ((Tsample = strtod(optarg, NULL)) <= 0.0) { fprintf (stderr, "Invalid -S parameter: %s\n", optarg); exit(1); } break; case 'Z': sampleSize = atoi(optarg); break; case 't': if ((dt = strtod(optarg, NULL)) <= 0.0) { fprintf (stderr, "Invalid -t parameter: %s\n", optarg); exit(1); } break; case 'N': if ((N0 = atoi(optarg)) <= 0) { fprintf (stderr, "Invalid -N parameter: %s\n", optarg); exit(1); } break; case 'H': if ((hostPopSize = atoi(optarg)) <= 0) { fprintf (stderr, "Invalid -H parameter: %s\n", optarg); exit(1); } break; case 'g': if ((gamma = strtod(optarg, NULL)) < 0.0) { fprintf (stderr, "Invalid -g parameter: %s\n", optarg); exit(1); } break; case 'b': if ((beta = strtod(optarg, NULL)) <= 0.0) { fprintf (stderr, "Invalid -b parameter: %s\n", optarg); exit(1); } break; case 'd': if ((delta = strtod(optarg, NULL)) <= 0.0) { fprintf (stderr, "Invalid -d parameter: %s\n", optarg); exit(1); } break; case 'L': if ((Ld = atoi(optarg)) < 0) { fprintf (stderr, "Invalid -L parameter: %s\n", optarg); exit(1); } break; case 'u': if ((mut_rate = strtod(optarg, NULL)) < 0.0) { fprintf (stderr, "Invalid -u parameter: %s\n", optarg); exit(1); } break; case 'k': if ((kappa = strtod(optarg, NULL)) < 0.0) { fprintf (stderr, "Invalid -k parameter: %s\n", optarg); exit(1); } break; case 'l': if ((Lb = atoi(optarg)) < 0) { fprintf (stderr, "Invalid -l parameter: %s\n", optarg); exit(1); } break; case 'a': sb = strtod(optarg, NULL); break; case 's': sd = strtod(optarg, NULL); break; case 'c': if ((sigma = strtod(optarg, NULL)) < 0.0) { fprintf (stderr, "Invalid -c parameter: %s\n", optarg); exit(1); } break; case '?': fprintf (stderr, "Unrecognized argument\n"); exit(1); } } if (filename[0] == '\0') { fprintf (stderr, "Output filename (option -o) must be specified\n"); exit(1); } FILE *outfile; if ((outfile = fopen(filename, "w")) == NULL) { fprintf (stderr, "Cannot open %s\n", filename); exit(1); } seed_time(); codonString::init_trans_matrix (kappa); codonString viralPop(N0, Lb, Ld, sb, sd, sigma); double t = 0.0; int N = 0; while ((N = viralPop.popSize()) > 0 && t < Tmax) { viralPop.mutate(mut_rate * dt); viralPop.transmit(beta * dt, hostPopSize); viralPop.recover(rbinom(N, delta * dt)); if (gamma > 0) viralPop.immuneDecay(gamma * dt); if (fmod(t, Tsample) < 1e-4) viralPop.printSample(outfile, t, sampleSize); t += dt; } fclose(outfile); }