[I2 logo] [RWTH logo] MOVES: Software Modeling and Verification
(Informatik 2)
Computer Science / RWTH / I2 / Research / Skil / Examples / Mcarlo
Printer-friendly
Skil Sample Applications

A Monte Carlo Method for Solving a Partial Differential Equation



#include "Par/par.c"
#include "Array/array.h"


static void pck_f ($t v, Buff buff)
{
   return ;
}


static void upck_f (Buff buff, $t *vptr)
{
   return ;
}


static $t dup_f ($t v)
{
   return (v) ;
}


static $t ident ($t v, Index ix)
{
   return (v) ;
}


static void destroy_f ($t v)
{
   return ;
}


static int *def_lowerbd (int procNr)
{
   int *res = (int *) VMalloc (2 * sizeof (int), "def_lowerbd") ;

   res[0] = 0 ;
   res[1] = procNr ;

   return (res) ;
}


static double rand_traj (double x0, double y0, double h, int nrtraj, Index ix)
{
   int    i, rand_dir, maxrand ;
   double res = 0.0            ;
   double x, y                 ;
   int    running              ;

   maxrand = RAND_MAX + 1 ;

   for (i = 0 ; i < nrtraj ; i++)
   {
      x = x0 ;
      y = y0 ;

      do
      {
         rand_dir = (int) ((double) rand () / maxrand * 4.0) ;

         switch (rand_dir)
         {
            case 0:  y += h            ;                        /* NORTH */
                     running = y < 1.0 ;
                     break             ;
            case 1:  x += h            ;                        /* EAST  */
                     running = x < 1.0 ;
                     break             ;
            case 2:  y -= h            ;                        /* SOUTH */
                     running = y > 0.0 ;
                     break             ;
            case 3:  x -= h            ;                        /* WEST  */
                     running = x > 0.0 ;
                     break             ;
         }
      }
      while (running) ;

      x = x < 0 ? 0 : x > 1 ? 1 : x ;
      y = y < 0 ? 0 : y > 1 ? 1 : y ;

      res += x + y ;
   }

   return (res) ;
}


int main (int argc, char **argv)
{
   array <double>  a                     ;
   int             n, rseed              ;
   char           *FName = "main"        ;
   Index           totalsize, def_blsize ;
   double          x0, y0, h, res        ;

   x0    = atof (argv[1]) ;
   y0    = atof (argv[2]) ;
   h     = atof (argv[3]) ;
   n     = atoi (argv[4]) ;
   rseed = atoi (argv[5]) ;

   par_init   () ;
   array_init () ;

   totalsize[0] = 1       ;
   totalsize[1] = netSize ;

   def_blsize[0] = 0 ;
   def_blsize[1] = 0 ;

   srand ((unsigned) (rseed * procId)) ;

   a = array_create (1, totalsize, def_blsize, def_lowerbd,
                     rand_traj (x0, y0, h, n/netSize)) ;

   res  = array_fold (ident, (+), a, pck_f, upck_f, dup_f) ;
   res /= n                                                ;

   array_destroy (a, destroy_f) ;

   array_close () ;
   par_close   () ;

   return (0) ;
}


Back to the Skil home page

Valid HTML 4.01 Strict! Valid CSS!