英文:
How to calculate the coverage of a particle in a surface simulated in C?
问题
我正在尝试在C中模拟一个能够捕获两种粒子的正方形表面。模拟遵循以下步骤:
-
我从一个完全空白的表面开始,然后随机选择其中的一个入口。
-
如果该入口为空,我继续到步骤3。否则,我选择另一个入口。
-
我随机决定捕获粒子#1或粒子#2。通过定义一个随机数r。
-
如果r小于或等于Y,我选择粒子#1,否则我选择粒子#2。
-
然后,我计算每种粒子被捕获的数量,直到整个网格被填满。
但是,我需要在每次迭代中获得每种粒子的覆盖率,考虑到个体覆盖率由#粒子/N定义,其中N是矩阵中的总入口数,我不确定如何继续。
我需要在每次迭代中计算覆盖率,然后将这些数据打印到一个名为"data"的文件中。您能否给我一些建议?谢谢您提前。
英文:
I'm trying to simulate in C a square surface that traps two kind of particles. The steps that the simulation follows are:
-
I start with a completely empty surface and then, select an entry of it at random.
-
If the entry is empty, I continue to the step 3). Otherwise, I select another entry
-
I decide to trap particle #1 or particle #2 at random as well. By defining a random number r
-
If r is less or equal to Y, I choose particle #1, otherwise, I choose particle #2
-
Then, I count how many particles of each kind were trapped until the grid was filled completely
But, I need to obtain the coverages of every particle in each iteration, taking into account that the individual coverage is defined by #particle / N, where N is the number of total entries in the matrix and I'm not sure how to proceed.
I need to calculate the coverages in each iteration and then, print that data in a file, called "data". Could you give me some suggestions for that, please? Thank you in advance.
答案1
得分: 2
Your code already tracks the count of each type of particle generated So if N
can be defined as:
double N = 1.0*sizeof(grid)/sizeof(grid[0][0]);//double avoids integer division
Then coverage (in percent of grid size) for either particle type 1 or 2 can simply be:
double coverage1 = 0.0;
double coverage2 = 0.0;
if(particle1 > 0.0) coverage1 = particle1/N;
if(particle2 > 0.0) coverage2 = particle2/N;
Your original code modified to integrate updated status of coverages:
if(r <= Y ){//The particle #1 is chosen (note modifications)
printf("r = %lf is less than Y = %lf. We choose the particle #1\n\n", r, Y);
grid[j][i] = P1_OCCUPIED;
particle1++;//count tracked here.
//output both columns here:
printf("%0.2lf%%\t%0.2lf%%", 100.0*coverage1,100.0*coverage2);
availcells--;//necessary?
fullcells++;//necessary?
}
else{//The particle #2 is chosen
printf("r = %lf is greater than Y = %lf. We choose the particle #2\n\n", r, Y);
grid[j][i] = P2_OCCUPIED;
particle2++;//count tracked here.
//output both columns here:
printf("%0.2lf%%\t%0.2lf%%", 100.0*coverage1,100.0*coverage2);
availcells--;//necessary?
fullcells++;//necessary?
}
The output below is modified to take advantage of the few changes
printf("The process took %d rounds\n\n", rounds);
printf("#particle1 = %d\n\n", particle1);
printf("#particle2 = %d\n\n", particle2);
printf("#availcells = %d\n\n",(int)(N - (particle1 + particle12)));
printf("#fullcells = %d\n\n",(particle1 + particle12));
printf("coverage particle1: %0.2lf%%\n", 100.0*coverage1);
printf("coverage particle2: %0.2lf%%\n", 100.0*coverage2);
英文:
> But, I need to obtain the coverages of every particle in each
> iterations, taking into account that the individual coverage is
> defined by #particle / N, where N is the number of total entries in
> the matrix and I'm not sure how to proceed.
Your code already tracks the count of each type of particle generated So if N
can be defined as:
double N = 1.0*sizeof(grid)/sizeof(grid[0][0]);//double avoids integer division
Then coverage (in percent of grid size) for either particle type 1 or 2 can simply be:
double coverage1 = 0.0;
double coverage2 = 0.0;
if(particle1 > 0.0) coverage1 = particle1/N;
if(particle2 > 0.0) coverage2 = particle2/N/;
Your original code modified to integrate updated status of coverages:
if(r <= Y ){//The particle #1 is chosen (note modifications)
printf("r = %lf is less than Y = %lf. We choose the particle #1\n\n", r, Y);
grid[j][i] = P1_OCCUPIED;
particle1++;//count tracked here.
//output both columns here:
printf("%0.2lf%%\t%0.2lf%%", 100.0*coverage1,100.0*coverage2);
availcells--;//necessary?
fullcells++;//necessary?
}
else{//The particle #2 is chosen
printf("r = %lf is greater than Y = %lf. We choose the particle #2\n\n", r, Y);
grid[j][i] = P2_OCCUPIED;
particle2++;//count tracked here.
//output both columns here:
printf("%0.2lf%%\t%0.2lf%%", 100.0*coverage1,100.0*coverage2);
availcells--;//necessary?
fullcells++;//necessary?
}
The output below is modified to take advantage of the few changes
printf("The process took %d rounds\n\n", rounds);
printf("#particle1 = %d\n\n", particle1);
printf("#particle2 = %d\n\n", particle2);
printf("#availcells = %d\n\n",(int)(N - (particle1 + particle12)));
printf("#fullcells = %d\n\n",(particle1 + particle12));
printf("coverage particle1: %0.2lf%%\n", 100.0*coverage1);
printf("coverage particle2: %0.2lf%%\n", 100.0*coverage2);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论