Actual source code: rand48.c

  1: #define PETSC_DLL

  3: #include "src/sys/utils/random/randomimpl.h"
  4: #if defined (PETSC_HAVE_STDLIB_H)
  5: #include <stdlib.h>
  6: #else
  7: /* maybe the protypes are missing */
 12: #endif

 16: PetscErrorCode  PetscRandomSeed_Rand48(PetscRandom r)
 17: {
 19:   srand48(r->seed);
 20:   return(0);
 21: }

 25: PetscErrorCode  PetscRandomGetValue_Rand48(PetscRandom r,PetscScalar *val)
 26: {
 28: #if defined(PETSC_USE_COMPLEX)  
 29:   if (r->iset) {
 30:     *val = PetscRealPart(r->width)*drand48() + PetscRealPart(r->low) +
 31:       (PetscImaginaryPart(r->width)*drand48() + PetscImaginaryPart(r->low)) * PETSC_i;
 32:   } else {
 33:     *val = drand48() + drand48()*PETSC_i;
 34:   }
 35: #else
 36:   if (r->iset) *val = r->width * drand48() + r->low;
 37:   else         *val = drand48();
 38: #endif
 39:   return(0);
 40: }

 44: PetscErrorCode  PetscRandomGetValueReal_Rand48(PetscRandom r,PetscReal *val)
 45: {
 47: #if defined(PETSC_USE_COMPLEX)
 48:   if (r->iset) *val = PetscRealPart(r->width)*drand48() + PetscRealPart(r->low);
 49:   else         *val = drand48();
 50: #else
 51:   if (r->iset) *val = r->width * drand48() + r->low;
 52:   else         *val = drand48();
 53: #endif
 54:   return(0);
 55: }

 59: PetscErrorCode  PetscRandomGetValueImaginary_Rand48(PetscRandom r,PetscScalar *val)
 60: {
 62: #if defined(PETSC_USE_COMPLEX)
 63:   if (r->iset) *val = (PetscImaginaryPart(r->width)*drand48()+PetscImaginaryPart(r->low))*PETSC_i;
 64:   else         *val = drand48()*PETSC_i;
 65: #else
 66:   if (r->iset) *val = r->width * drand48() + r->low;
 67:   else         *val = drand48();
 68: #endif
 69:   return(0);
 70: }

 72: static struct _PetscRandomOps PetscRandomOps_Values = {
 73:   /* 0 */
 74:   PetscRandomSeed_Rand48,
 75:   PetscRandomGetValue_Rand48,
 76:   PetscRandomGetValueReal_Rand48,
 77:   PetscRandomGetValueImaginary_Rand48,
 78:   0,
 79:   /* 5 */
 80:   0
 81: };

 83: /*
 84:    For now we have set up using the DRAND48() generater. We need to deal 
 85:    with other variants of random number generators. We should also add
 86:    a routine to enable restarts [seed48()] 
 87: */
 91: PetscErrorCode  PetscRandomCreate_Rand48(PetscRandom r)
 92: {

 96:   PetscMemcpy(r->ops,&PetscRandomOps_Values,sizeof(PetscRandomOps_Values));
 97:   /* r->bops->publish   = PetscRandomPublish; */
 98:   /*  r->petscnative     = PETSC_TRUE;  */

100:   PetscObjectChangeTypeName((PetscObject)r,PETSCRAND48);
101:   PetscPublishAll(r);
102:   return(0);
103: }