int main(int argc, char *argv[]) { long seed, noutput, nskip; int ij, kl; long i, j, less; double x; if (argc != 4 || sscanf(argv[1], "%ld", &seed) != 1 || sscanf(argv[2], "%ld", &noutput) != 1 || sscanf(argv[3], "%ld", &nskip) != 1) { printf("usage: ranmar seed# #output #skip\n"); return 1; } ij = abs(seed); kl = 1.234 * ij; if (ij < 0) ij = -ij; if (kl < 0) kl = -kl; ij %= 31328; kl %= 30081; if (rmarin(ij, kl) != 0) { printf("Internal Error: call rmarin()\n"); return 1; } less = 0; for (i = 0; i < noutput; i++) { for (j = 0; j < nskip; j++) { x = ranmar(); if (x < .5) less++; } printf("%15d %17.15f %15ld %6.4f\n", nCallRanmar, x, less, (double) less / nCallRanmar); } return 0; }
/* int rnd(int init,double rnd1); void main() { double teste; rnd(0,teste); rnd(1,teste); rnd(1,teste); } */ void rnd(int init,double *rnd1) { float temp[2]; int ij, kl, len,i; /* random seeds for the test case: */ ij = 100; kl = 1002; /* do the initialization */ if (init == 0) { rmarin(ij,kl); } len = 6; ranmar(temp,len); *rnd1= (double) temp[1]; }
int main() { float temp[100]; int i; int ij, kl, len; /*These are the seeds needed to produce the test case results*/ ij = 1802; kl = 9373; /*Do the initialization*/ if (1 == rmarin(ij,kl)) return 1; /*Generate 20000 random numbers*/ len = 100; for ( i=0; i<=199 ; i++) if (1 == ranmar(temp, len)) return 1; /*If the random number generator is working properly, the next six random numbers should be: 6533892.0 14220222.0 7275067.0 6172232.0 8354498.0 10633180.0 */ len = 6; if (1 == ranmar(temp, len)) return 1; for ( i=0; i<=5; i++) printf("%12.1f\n",4096.0*4096.0*temp[i]); return 0; }
int main(int argc, char *argv[]) { long seed, noutput; int ij, kl; long i; double x; if (argc != 3 || sscanf(argv[1], "%ld", &seed) != 1 || sscanf(argv[2], "%ld", &noutput) != 1) { printf("usage: ranmar seed# #output\n"); return 1; } ij = abs(seed); kl = 1.234 * ij; if (ij < 0) ij = -ij; if (kl < 0) kl = -kl; ij %= 31328; kl %= 30081; if (rmarin(ij, kl) != 0) { printf("Internal Error: call rmarin()\n"); return 1; } for (i = 0; i < noutput; i++) { x = ranmar(); Fprintf(stdout, "%8.6f\n", x); } return 0; }
int main(int argc, char **argv){ int rmarin(int ij, int kl); int ranmar(float rvec[], int len); double **u, **v; /* arrays for the concentration fields */ double **ff; /* array for the reaction kinetics */ double **lap; /* array for the laplacian */ float *temp; /* array for the random number generator */ int seed1, seed2; /* user given parameters for the random number generator*/ int i, j, p, index=0; /* some indeces for the loops */ float a, b; /* the reaction parameters (input from command line) */ double f,g; double u_0, v_0, var; /* variables for the initial conditions */ FILE *tied; /* file pointer for the output stream*/ if(argc < 5){ printf("\nEnter four command line arguments (e.g. simu 1234 1234 4.5 5.0):\n1)First seed for the random number generator (between 0 and 31328)\n2)Second seed for the random number generator (between 0 and 30081)\n3)Parameter A value\n 4) Parameter B value\n\n"); return(1); } /* Allocating space for the arrays */ u = malloc(M*sizeof(double)); if(u==NULL) { printf("u out of memory\n"); exit(1); } for(i=0;i<M;i++){ u[i]=malloc(N*sizeof(double)); if(u[i]==NULL) { printf("u[%d] out of memory\n",i); exit(1); } } v = malloc(M*sizeof(double)); if(v==NULL) { printf("v out of memory\n"); exit(1); } for(i=0;i<M;i++){ v[i]=malloc(N*sizeof(double)); if(v[i]==NULL) { printf("v[%d] out of memory\n",i); exit(1); } } ff = malloc(M*sizeof(double *)); if(ff==NULL) { printf("ff out of memory\n"); exit(1); } for(i=0;i<M;i++){ ff[i]=malloc(N*sizeof(double)); if(ff[i]==NULL) { printf("ff[%d] out of memory\n",i); exit(1); } } lap = malloc(M*sizeof(double *)); if(lap == NULL) { printf("lap out of memory\n"); exit(1); } for(i=0;i<M;i++) { lap[i]=malloc(N*sizeof(double)); if(lap[i]==NULL) { printf("lap[%d] out of memory\n",i); exit(1); } } temp = calloc(N*M*2,sizeof(float)); /* Allocation completed */ /* Read the input line parameters (two seeds for the random number generator and the parameters a and b) */ seed1 = atoi(*++argv); seed2 = atoi(*++argv); a = atof(*++argv); b = atof(*++argv); /* Initialize the random number generator */ if(rmarin(seed1,seed2)==1) return 0; /* Fill the array temp with random numbers between 0 and 1 */ ranmar(temp,M*N*2); /* The initial values of the concentration fields are random deviations around the stationary state */ /************************ ADD HERE THE VALUES OF THE STATIONARY STATES YOU HAVE CALCULATED (U_0 and V_0) IN THE PROBLEM 1 AND AN AMPLITUDE FOR THE INITIAL RANDOM DEVIATIONS FROM THESE STATES (var). ************************/ u_0 = a; v_0 = b/a; var = 1.5; for(i=0;i<M;i++) for(j=0;j<N;j++){ u[i][j] = u_0 + var*(temp[index]-.5); index++; v[i][j] = v_0 + var*(temp[index]-.5); index++; } /***************************** Here begins the iteration loop *****************************/ for(p=0;p<iterations;p++){ /* calculation of the laplacian with respect to u with finite difference method and periodic boundary conditions */ for(i=0;i<M;i++) for(j=0;j<N;j++){ lap[i][j] = 0; if(j!=N-1) lap[i][j] += u[i][j+1]-u[i][j]; if(j!=0) lap[i][j] += u[i][j-1]-u[i][j]; if(i!=0) lap[i][j] += u[i-1][j]-u[i][j]; if(i!=M-1) lap[i][j] += u[i+1][j]-u[i][j]; if(j==N-1) lap[i][j] += u[i][0]-u[i][j]; if(j==0) lap[i][j] += u[i][N-1]-u[i][j]; if(i==M-1) lap[i][j] += u[0][j]-u[i][j]; if(i==0) lap[i][j] += u[M-1][j]-u[i][j]; lap[i][j] = lap[i][j]/(dx*dx); } for(i=0;i<M;i++) for(j=0;j<N;j++){ /************************ ADD HERE THE ITERATION STEP OF THE EULER'S METHOD FOR CONCENTRATION DATA u HINT: u THAT IS WRITTEN HERE IS THE u^{t+dt} OF THE ITERATION FORMULA ************************/ f = a - (b+1)*u[i][j] + u[i][j]*u[i][j] * v[i][j]; u[i][j] = u[i][j] + dt * (D_u*lap[i][j] + f) ; } /* calculation of the laplacian with respect to v with finite difference method and periodic boundary conditions */ for(i=0;i<M;i++) for(j=0;j<N;j++){ lap[i][j] = 0; if(j!=N-1) lap[i][j] += v[i][j+1]-v[i][j]; if(j!=0) lap[i][j] += v[i][j-1]-v[i][j]; if(i!=0) lap[i][j] += v[i-1][j]-v[i][j]; if(i!=M-1) lap[i][j] += v[i+1][j]-v[i][j]; if(j==N-1) lap[i][j] += v[i][0]-v[i][j]; if(j==0) lap[i][j] += v[i][N-1]-v[i][j]; if(i==M-1) lap[i][j] += v[0][j]-v[i][j]; if(i==0) lap[i][j] += v[M-1][j]-v[i][j]; lap[i][j] = lap[i][j]/(dx*dx); } for(i=0;i<M;i++) for(j=0;j<N;j++){ /************************ ADD HERE THE ITERATION STEP OF THE EULER'S METHOD FOR CONCENTRATION DATA v HINT: v THAT IS WRITTEN HERE IS THE v^{t+dt} OF THE ITERATION FORMULA ************************/ g = b*u[i][j] - u[i][j]*u[i][j]*v[i][j]; v[i][j] = v[i][j] + dt *(D_v * lap[i][j] + g); } if(p%100 == 0) { fprintf(stderr,"%d iterations\n",p); fprintf(stdout,"u%d = [",p/100); for(i=0;i<M;i++){ for(j=0;j<N;j++) fprintf(stdout,"%f ",u[i][j]); fprintf(stdout,";"); } fprintf(stdout,"];\n"); fprintf(stdout,"v%d = [",p/100); for(i=0;i<M;i++){ for(j=0;j<N;j++) fprintf(stdout,"%f ",v[i][j]); fprintf(stdout,";"); } fprintf(stdout,"];\n"); } } /* WRITE THE OUTPUT DATA */ tied = fopen("data.m","w"); fprintf(tied,"u = ["); for(i=0;i<M;i++){ for(j=0;j<N;j++) fprintf(tied,"%f ",u[i][j]); fprintf(tied,";"); } fprintf(tied,"];\n"); fprintf(tied,"v = ["); for(i=0;i<M;i++){ for(j=0;j<N;j++) fprintf(tied,"%f ",v[i][j]); fprintf(tied,";"); } fprintf(tied,"];\n"); fclose(tied); return (1); }