如何在C语言中计算表面上粒子的覆盖率?

huangapple go评论64阅读模式
英文:

How to calculate the coverage of a particle in a surface simulated in C?

问题

我正在尝试在C中模拟一个能够捕获两种粒子的正方形表面。模拟遵循以下步骤:

  1. 我从一个完全空白的表面开始,然后随机选择其中的一个入口。

  2. 如果该入口为空,我继续到步骤3。否则,我选择另一个入口。

  3. 我随机决定捕获粒子#1或粒子#2。通过定义一个随机数r。

  4. 如果r小于或等于Y,我选择粒子#1,否则我选择粒子#2。

  5. 然后,我计算每种粒子被捕获的数量,直到整个网格被填满。

但是,我需要在每次迭代中获得每种粒子的覆盖率,考虑到个体覆盖率由#粒子/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:

  1. I start with a completely empty surface and then, select an entry of it at random.

  2. If the entry is empty, I continue to the step 3). Otherwise, I select another entry

  3. I decide to trap particle #1 or particle #2 at random as well. By defining a random number r

  4. If r is less or equal to Y, I choose particle #1, otherwise, I choose particle #2

  5. 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 &gt; 0.0) coverage1 = particle1/N;
if(particle2 &gt; 0.0) coverage2 = particle2/N/; 

Your original code modified to integrate updated status of coverages:

if(r &lt;= Y ){//The particle #1 is chosen  (note modifications)       
    printf(&quot;r = %lf is less than Y = %lf. We choose the particle #1\n\n&quot;, r, Y);
    grid[j][i] = P1_OCCUPIED;
    particle1++;//count tracked here.
    //output both columns here:
    printf(&quot;%0.2lf%%\t%0.2lf%%&quot;, 100.0*coverage1,100.0*coverage2);
    availcells--;//necessary?
    fullcells++;//necessary?
}
else{//The particle #2 is chosen
    printf(&quot;r = %lf is greater than Y = %lf. We choose the particle #2\n\n&quot;, r, Y);
    grid[j][i] = P2_OCCUPIED;
    particle2++;//count tracked here.
    //output both columns here:
    printf(&quot;%0.2lf%%\t%0.2lf%%&quot;, 100.0*coverage1,100.0*coverage2);        
    availcells--;//necessary?
    fullcells++;//necessary?                
}   

The output below is modified to take advantage of the few changes

printf(&quot;The process took %d rounds\n\n&quot;, rounds);
printf(&quot;#particle1 = %d\n\n&quot;, particle1);
printf(&quot;#particle2 = %d\n\n&quot;, particle2);
printf(&quot;#availcells = %d\n\n&quot;,(int)(N - (particle1  + particle12))); 
printf(&quot;#fullcells = %d\n\n&quot;,(particle1  + particle12)); 
printf(&quot;coverage particle1: %0.2lf%%\n&quot;, 100.0*coverage1);
printf(&quot;coverage particle2: %0.2lf%%\n&quot;, 100.0*coverage2);

huangapple
  • 本文由 发表于 2023年2月8日 16:01:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/75382810.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定