Boa tarde, eu preciso passar o seguinte código em C para Fortran 90, porém tudo que fiz não consegui o resultado do programa em C que já compilei e deu o resultado esperado. Por favor, preciso de ajuda:
FILE *output; /* save data in dla.dat */
output = fopen("dla.dat","w");
for(i=0; i<size; i++) /* clear grid */
{
for(j=0; j<size; j++) grid[j] = 0;
}
grid[200][200] = 1; /* one particle at the center */
srand48(seed); /* seed number generator */
for(i=0; i<max; i++) /* choose starting point */
{
hit = 0;
angle = (2*PI*drand48()); /* random angle */
x = (200+rad*cos(angle)); /* coordinates */
y = (200+rad*sin(angle));
dist = gauss_ran(); /* random number gaussian dist. */
if (dist<0) step=-1; /* move forwards or backwards */
else step=1;
trav=0;
while((hit==0)&&(x<399)&&(x>1)&&(y<399)&&(y>1)&&(trav<abs(dist)))
{
if(grid[x+1][y]+grid[x-1][y]+grid[x][y+1]+grid[x][y-1]>=1)
{
hit = 1; /* one neighbor is occupied */
grid[x][y] = 1; /* particle sticks, walk is over */
}
else if(drand48() < 0.5) x+=step; /* move horizontally */
else y+=step; /* move vertically */
trav++;
}
}
for(i=0; i<size; i++) /* print resulting grid */
{
for(j=0; j<size; j++)
{
if(grid[j]==1) fprintf(output,"%d\t%d\n", i, j);
}
}
printf("data stored in dla.dat\n");
fclose(output);
}
abaixo segue o programa completo para maior entendimento:
main()
{
double angle, rad = 180.0;
int i,j, x, y, dist, dir, step, trav;
int grid, hit;
int gauss_ran(); /* gaussian random number */
FILE *output; /* save data in dla.dat */
output = fopen("dla.dat","w");
for(i=0; i<size; i++) /* clear grid */
{
for(j=0; j<size; j++) grid[j] = 0;
}
grid[200][200] = 1; /* one particle at the center */
srand48(seed); /* seed number generator */
for(i=0; i<max; i++) /* choose starting point */
{
hit = 0;
angle = (2*PI*drand48()); /* random angle */
x = (200+rad*cos(angle)); /* coordinates */
y = (200+rad*sin(angle));
dist = gauss_ran(); /* random number gaussian dist. */
if (dist<0) step=-1; /* move forwards or backwards */
else step=1;
trav=0;
while((hit==0)&&(x<399)&&(x>1)&&(y<399)&&(y>1)&&(trav<abs(dist)))
{
if(grid[x+1][y]+grid[x-1][y]+grid[x][y+1]+grid[x][y-1]>=1)
{
hit = 1; /* one neighbor is occupied */
grid[x][y] = 1; /* particle sticks, walk is over */
}
else if(drand48() < 0.5) x+=step; /* move horizontally */
else y+=step; /* move vertically */
trav++;
}
}
for(i=0; i<size; i++) /* print resulting grid */
{
for(j=0; j<size; j++)
{
if(grid[j]==1) fprintf(output,"%d\t%d\n", i, j);
}
}
printf("data stored in dla.dat\n");
fclose(output);
}
/*--------------------------end of main program-----------------------*/
/* generates random numbers with gaussian distribution using the */
/* Box-Mueller method */
int gauss_ran()
{
double fac, rr, r1, r2;
static int old=0; /* have to be static so information */
static int mem; /* survives between function calls */
if (old==0) /* no random number left from */
{ /* previous function call */
do
{
r1= 2.0*drand48()-1.0; /* choose random point in */
r2= 2.0*drand48()-1.0; /* the unit circle */
rr= r1*r1+r2*r2;
}while ((rr>=1)||(rr==0));
fac=sqrt(-2*log(rr)/rr);
mem=5000*r1*fac; /* save for next call */
old=1; /* set flag */
return((int)(5000*r2*fac));
}
else /* return second number */
{ /* from last call */
old=0; /* unset flag */
return mem; /* return number from last call */
}
}
Pergunta
Modelamento
Boa tarde, eu preciso passar o seguinte código em C para Fortran 90, porém tudo que fiz não consegui o resultado do programa em C que já compilei e deu o resultado esperado. Por favor, preciso de ajuda:
FILE *output; /* save data in dla.dat */
output = fopen("dla.dat","w");
for(i=0; i<size; i++) /* clear grid */
{
for(j=0; j<size; j++) grid[j] = 0;
}
grid[200][200] = 1; /* one particle at the center */
srand48(seed); /* seed number generator */
for(i=0; i<max; i++) /* choose starting point */
{
hit = 0;
angle = (2*PI*drand48()); /* random angle */
x = (200+rad*cos(angle)); /* coordinates */
y = (200+rad*sin(angle));
dist = gauss_ran(); /* random number gaussian dist. */
if (dist<0) step=-1; /* move forwards or backwards */
else step=1;
trav=0;
while((hit==0)&&(x<399)&&(x>1)&&(y<399)&&(y>1)&&(trav<abs(dist)))
{
if(grid[x+1][y]+grid[x-1][y]+grid[x][y+1]+grid[x][y-1]>=1)
{
hit = 1; /* one neighbor is occupied */
grid[x][y] = 1; /* particle sticks, walk is over */
}
else if(drand48() < 0.5) x+=step; /* move horizontally */
else y+=step; /* move vertically */
trav++;
}
}
for(i=0; i<size; i++) /* print resulting grid */
{
for(j=0; j<size; j++)
{
if(grid[j]==1) fprintf(output,"%d\t%d\n", i, j);
}
}
printf("data stored in dla.dat\n");
fclose(output);
}
abaixo segue o programa completo para maior entendimento:
main()
{
double angle, rad = 180.0;
int i,j, x, y, dist, dir, step, trav;
int grid, hit;
int gauss_ran(); /* gaussian random number */
FILE *output; /* save data in dla.dat */
output = fopen("dla.dat","w");
for(i=0; i<size; i++) /* clear grid */
{
for(j=0; j<size; j++) grid[j] = 0;
}
grid[200][200] = 1; /* one particle at the center */
srand48(seed); /* seed number generator */
for(i=0; i<max; i++) /* choose starting point */
{
hit = 0;
angle = (2*PI*drand48()); /* random angle */
x = (200+rad*cos(angle)); /* coordinates */
y = (200+rad*sin(angle));
dist = gauss_ran(); /* random number gaussian dist. */
if (dist<0) step=-1; /* move forwards or backwards */
else step=1;
trav=0;
while((hit==0)&&(x<399)&&(x>1)&&(y<399)&&(y>1)&&(trav<abs(dist)))
{
if(grid[x+1][y]+grid[x-1][y]+grid[x][y+1]+grid[x][y-1]>=1)
{
hit = 1; /* one neighbor is occupied */
grid[x][y] = 1; /* particle sticks, walk is over */
}
else if(drand48() < 0.5) x+=step; /* move horizontally */
else y+=step; /* move vertically */
trav++;
}
}
for(i=0; i<size; i++) /* print resulting grid */
{
for(j=0; j<size; j++)
{
if(grid[j]==1) fprintf(output,"%d\t%d\n", i, j);
}
}
printf("data stored in dla.dat\n");
fclose(output);
}
/*--------------------------end of main program-----------------------*/
/* generates random numbers with gaussian distribution using the */
/* Box-Mueller method */
int gauss_ran()
{
double fac, rr, r1, r2;
static int old=0; /* have to be static so information */
static int mem; /* survives between function calls */
if (old==0) /* no random number left from */
{ /* previous function call */
do
{
r1= 2.0*drand48()-1.0; /* choose random point in */
r2= 2.0*drand48()-1.0; /* the unit circle */
rr= r1*r1+r2*r2;
}while ((rr>=1)||(rr==0));
fac=sqrt(-2*log(rr)/rr);
mem=5000*r1*fac; /* save for next call */
old=1; /* set flag */
return((int)(5000*r2*fac));
}
else /* return second number */
{ /* from last call */
old=0; /* unset flag */
return mem; /* return number from last call */
}
}
Link para o comentário
Compartilhar em outros sites
0 respostass a esta questão
Posts Recomendados
Participe da discussão
Você pode postar agora e se registrar depois. Se você já tem uma conta, acesse agora para postar com sua conta.