Advertisement
Guest User

Untitled

a guest
Feb 18th, 2020
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.02 KB | None | 0 0
  1. void one_particle(int *grid, int n)
  2. {
  3. /*//dynamically allocate memory for 3d array
  4. int* A = (int*)malloc(M*N*O*sizeof(int));
  5. for(int i = 0; i<M; i++)
  6. for(int j = 0; j<N; j++)for(int k=0; k<O;k++)
  7. *(A+i*N*O + j*O + k)=0;
  8. //print array
  9. for(int i = 0; i < M; i++){
  10. for(int j = 0; j<N; j++){
  11. for(int k = 0; k<O; k++)
  12. printf("%d", *(A + i*N*O + j*O + k));
  13. printf("\n");}
  14. printf("\n");
  15. }}*/
  16. int x=n,y=n,z=n;
  17. for(int i=1; i<=n; i++){
  18. int move = rand() % 6;
  19. if (move==0)
  20. x+=1;
  21. if (move==1)
  22. x-=1;
  23. if (move==2)
  24. y+=1;
  25. if (move==3)
  26. y-=1;
  27. if (move==4)
  28. z+=1;
  29. if (move==5)
  30. z-=1;}
  31. grid[((x * (2*n + 1) * (2* n + 1)) + (y * (2* n + 1)) +z)] += 1;
  32. printf("%ls", grid);
  33. }
  34.  
  35. double squarert(int x) //square root bby
  36. {
  37. double temp, number;
  38. if (x == 1)
  39. return (double) x;
  40. number = x/2;
  41. temp = 0;
  42. while (number!=temp){
  43. temp = number;
  44. number = (x/temp + temp) / 2;
  45. }
  46. return number;
  47. }
  48.  
  49. //TODO
  50. //Implement the following function
  51. //This function returns the fraction of particles that lie within the distance
  52. //r*n from the origin (including particles exactly r*n away)
  53. //The distance used here is Euclidean distance
  54. //Note: you will not have access to math.h when submitting on Mimir
  55. double density(int *grid, int n, double r)
  56. {
  57. double tot = 0;
  58. int index;
  59. int particles = 0;
  60. for (int i = 0; i < ((2*n +1) * (2*n+1) * (2*n+1)); i++){
  61. double distance = squarert(grid[i]);
  62. if (distance <= ((r*r)*(n*n)))
  63. tot += grid[i];
  64. particles += grid[i];}
  65. /* for (int x = 0; x < (2*n + 1); x++){
  66. for (int y = 0; y < (2 * n + 1); y++){
  67. for (int z = 0; z < (2*n + 1); z++){
  68. index = ((x * (2*n + 1) * (2* n + 1)) + (y * (2* n + 1)) + z);
  69. printf("%d\n", index);
  70. if (squarert(x*x + y*y + z*z) <= (r*n)){
  71.  
  72. //printf("x = %d,y = %d,z = %d\n", x,y,z);
  73. tot += grid[index];}}}}
  74. printf("%f\n", tot);
  75.  
  76. return (double)tot / ((2 * n + 1)*(2*n + 1)*(2*n + 1));*/
  77. return tot/particles;
  78.  
  79. }
  80.  
  81. //use this function to print results
  82. void print_result(int *grid, int n)
  83. {
  84. printf("radius density\n");
  85. for(int k = 1; k <= 20; k++)
  86. {
  87. printf("%.2lf %lf\n", 0.05*k, density(grid, n, 0.05*k));
  88. }
  89. }
  90.  
  91. //TODO
  92. //Finish the following function
  93. //See the assignment decription on Piazza for more details
  94. void diffusion(int n, int m)
  95. {
  96. //fill in a few line of code below
  97. int *grid=calloc((2*n+1)*(2*n+1)*(2*n+1), sizeof(int));
  98. for(int i = 1; i<=m; i++) one_particle(grid, n);
  99. //int *grid=calloc((2*n+1)*(2*n+1)*(2*n+1), sizeof(int));
  100. print_result(grid, n);
  101. //fill in some code below
  102. free(grid);
  103.  
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement