Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

#includ #include #include / / TODO / / Implement the below function / / Simulate one particle moving n steps in random directions / /

#includ
#include
#include
//TODO
//Implement the below function
//Simulate one particle moving n steps in random directions
//Use a random number generator to decide which way to go at every step
//When the particle stops at a final location, use the memory pointed to by grid to
//record the number of particles that stop at this final location
//Feel free to declare, implement and use other functions when needed
void one_particle(int *grid, int n)
{
}
//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)
{
}
//use this function to print results
void print_result(int *grid, int n)
{
printf("radius density
");
for(int k =1; k <=20; k++)
{
printf("%.2lf %lf
",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
for(int i =1; i<=m; i++) one_particle(grid, n);
print_result(grid, n);
//fill in some code below
}
int main(int argc, char *argv[])
{
if(argc !|=3)
{
printf("Usage: %s n m
", argv[0]);
return 0;
}
int n = atoi(argv[1]);
int m = atoi(argv[2]);
assert(n >=1 && n <=50);
assert(m >=1 && m <=1000000);
srand(12345);
diffusion(n, m);
return 0;
}
In this exercise, we use a 3D random walk to simulate a diffusion process. Imagine a particle starting at
the origin (0,0,0) that has equal probabilities to go in 6 possible directions - left, right, backward, forward,
down, and up. For example, when the particle is at (x, y, z), with equal probability 1/6, its next location is
at (x 1, y, z),(x +1, y, z),(x, y 1, z),(x, y +1, z),(x, y, z 1) or (x, y, z +1).
The particle will conduct the random walk for n steps. We are interested in the distribution of the final
locations of particles after each takes n steps. Specifically, we would like to know the distribution of the
distance between the final location and the origin. In order to obtain this distribution, we simulate m such
particles, and check the proportion of the particles that lies within rn distance from the origin, where r is a
real number between 0 and 1. Note all the particles will be within a sphere with radius n since particles only
move n steps and the furthest they can go is a distance n from the origin. In our simulation, we will calculate
the proportion of particles that are within rn from the origin for r =0.05,0.10,0.15,...,0.90,0.95,1.00.
Below is the main() function for this program. Note how we use command line arguments.
int main(int argc, char *argv[])
{
if(argc !=3)
{
printf("Usage: %s n m
", argv[0]);
return 0;
}
int n = atoi(argv[1]);
int m = atoi(argv[2]);
srand(12345);
diffusion(n, m);
return 0;
}
We need to implement the function
void diffusion(int n, int m)
{
}
In this function, we need to dynamically allocate memory to represent a 3D grid where x, y, z coordinates
range from n to n. For all the possible x, y, z coordinates within the range, we save the number of particles
that end up at the location (x, y, z). During the simulation, if the final location of a particle is (x, y, z), the
corresponding value will be incremented by 1.
Generate a random number using rand()%6. If this number is 0 or 1, you should move left or right
on the x-axis, respectively. If it is 2 or 3, you should move up or down on the y-axis, respectively. If it is 4
or 5, move up or down on the z-axis, respectively.
In the implementation of main, we use the below function to print out results.
1
void print_result(int *grid, int n)
{
printf("radius density
");
for(int k =1; k <=20; k++)
{
printf("%.2lf %lf
",0.05*k, density(grid, n,0.05*k));
}
}
The function
double density(int *grid, int n, double r)
{
}
returns the proportion of particles in the grid that are within the sphere of rn radius from the origin. Note
that to implement the above function, we need to calculate the Euclidean distance between (x, y, z) and the
origin (0,0,0). The formula is p
x
2+ y
2+ z
2. Think about how we can avoid using the square root in our
code.
Below are outputs from a few sample runs. We can use these outputs to check our code.
$ ./diffusion 101000000
radius density
0.050.019415
0.100.019415
0.150.196635
0.200.263900
0.250.465962
0.300.542348
0.350.686846
0.400.826187
0.450.908716
0.500.938628
0.550.982609
0.600.991070
0.650.997860
0.700.999031
0.750.999883
0.800.999959
0.850.999996
0.900.999996
0.951.000000
1.001.000000
$ ./diffusion 201000000
radius density
0.050.007120
0.100.113322
0.150.272127
0.200

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Professional Microsoft SQL Server 2012 Administration

Authors: Adam Jorgensen, Steven Wort

1st Edition

1118106881, 9781118106884

More Books

Students also viewed these Databases questions

Question

find all matrices A (a) A = 13 (b) A + A = 213

Answered: 1 week ago