Jump to content
Fórum Script Brasil
  • 0

Código C para Fortran 90


Modelamento
 Share

Question

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 to comment
Share on other sites

0 answers to this question

Recommended Posts

There have been no answers to this question yet

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

  • Forum Statistics

    • Total Topics
      149.8k
    • Total Posts
      646.6k
×
×
  • Create New...