Ir ao conteúdo
  • Cadastre-se

Código c para Fortran 90


Posts recomendados

Boa tarde, preciso de ajuda para passar este código em c para fortran 90. Já montei o programa em fortran, porém não consigo o mesmo resultado, por favor, preciso de ajuda.

 

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
 
/* if you don't have drand48 uncomment the following two lines */
/*    #define drand48 1.0/RAND_MAX*rand  
      #define srand48 srand                */                    
       
#define max 40000                        /* number of iterations  */
#define size 401                         /* size of grid array  */
#define PI 3.1415926535897932385E0
#define seed 68111             /* seed for number generator */
 
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

Visitante
Este tópico está impedido de receber novas respostas.

Sobre o Clube do Hardware

No ar desde 1996, o Clube do Hardware é uma das maiores, mais antigas e mais respeitadas comunidades sobre tecnologia do Brasil. Leia mais

Direitos autorais

Não permitimos a cópia ou reprodução do conteúdo do nosso site, fórum, newsletters e redes sociais, mesmo citando-se a fonte. Leia mais

×
×
  • Criar novo...