Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void one_particle(int *grid, int n)
- {
- /*//dynamically allocate memory for 3d array
- int* A = (int*)malloc(M*N*O*sizeof(int));
- for(int i = 0; i<M; i++)
- for(int j = 0; j<N; j++)for(int k=0; k<O;k++)
- *(A+i*N*O + j*O + k)=0;
- //print array
- for(int i = 0; i < M; i++){
- for(int j = 0; j<N; j++){
- for(int k = 0; k<O; k++)
- printf("%d", *(A + i*N*O + j*O + k));
- printf("\n");}
- printf("\n");
- }}*/
- int x=n,y=n,z=n;
- for(int i=1; i<=n; i++){
- int move = rand() % 6;
- if (move==0)
- x+=1;
- if (move==1)
- x-=1;
- if (move==2)
- y+=1;
- if (move==3)
- y-=1;
- if (move==4)
- z+=1;
- if (move==5)
- z-=1;}
- grid[((x * (2*n + 1) * (2* n + 1)) + (y * (2* n + 1)) +z)] += 1;
- printf("%ls", grid);
- }
- double squarert(int x) //square root bby
- {
- double temp, number;
- if (x == 1)
- return (double) x;
- number = x/2;
- temp = 0;
- while (number!=temp){
- temp = number;
- number = (x/temp + temp) / 2;
- }
- return number;
- }
- //TODO
- //Implement the following function
- //This function returns the fraction of particles that lie within the distance
- //r*n from the origin (including particles exactly r*n away)
- //The distance used here is Euclidean distance
- //Note: you will not have access to math.h when submitting on Mimir
- double density(int *grid, int n, double r)
- {
- double tot = 0;
- int index;
- int particles = 0;
- for (int i = 0; i < ((2*n +1) * (2*n+1) * (2*n+1)); i++){
- double distance = squarert(grid[i]);
- if (distance <= ((r*r)*(n*n)))
- tot += grid[i];
- particles += grid[i];}
- /* for (int x = 0; x < (2*n + 1); x++){
- for (int y = 0; y < (2 * n + 1); y++){
- for (int z = 0; z < (2*n + 1); z++){
- index = ((x * (2*n + 1) * (2* n + 1)) + (y * (2* n + 1)) + z);
- printf("%d\n", index);
- if (squarert(x*x + y*y + z*z) <= (r*n)){
- //printf("x = %d,y = %d,z = %d\n", x,y,z);
- tot += grid[index];}}}}
- printf("%f\n", tot);
- return (double)tot / ((2 * n + 1)*(2*n + 1)*(2*n + 1));*/
- return tot/particles;
- }
- //use this function to print results
- void print_result(int *grid, int n)
- {
- printf("radius density\n");
- for(int k = 1; k <= 20; k++)
- {
- printf("%.2lf %lf\n", 0.05*k, density(grid, n, 0.05*k));
- }
- }
- //TODO
- //Finish the following function
- //See the assignment decription on Piazza for more details
- void diffusion(int n, int m)
- {
- //fill in a few line of code below
- int *grid=calloc((2*n+1)*(2*n+1)*(2*n+1), sizeof(int));
- for(int i = 1; i<=m; i++) one_particle(grid, n);
- //int *grid=calloc((2*n+1)*(2*n+1)*(2*n+1), sizeof(int));
- print_result(grid, n);
- //fill in some code below
- free(grid);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement