Actual source code: rand48.c

  1: #define PETSC_DLL

  3: #include "src/sys/random/randomimpl.h"
  4: #include "petscfix.h"
  5: #if defined (PETSC_HAVE_STDLIB_H)
  6: #include <stdlib.h>
  7: #endif

 11: PetscErrorCode  PetscRandomSeed_Rand48(PetscRandom r)
 12: {
 14:   srand48(r->seed);
 15:   return(0);
 16: }

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

 39: PetscErrorCode  PetscRandomGetValueReal_Rand48(PetscRandom r,PetscReal *val)
 40: {
 42: #if defined(PETSC_USE_COMPLEX)
 43:   if (r->iset) *val = PetscRealPart(r->width)*drand48() + PetscRealPart(r->low);
 44:   else         *val = drand48();
 45: #else
 46:   if (r->iset) *val = r->width * drand48() + r->low;
 47:   else         *val = drand48();
 48: #endif
 49:   return(0);
 50: }

 54: PetscErrorCode  PetscRandomGetValueImaginary_Rand48(PetscRandom r,PetscScalar *val)
 55: {
 57: #if defined(PETSC_USE_COMPLEX)
 58:   if (r->iset) *val = (PetscImaginaryPart(r->width)*drand48()+PetscImaginaryPart(r->low))*PETSC_i;
 59:   else         *val = drand48()*PETSC_i;
 60: #else
 61:   if (r->iset) *val = r->width * drand48() + r->low;
 62:   else         *val = drand48();
 63: #endif
 64:   return(0);
 65: }

 67: static struct _PetscRandomOps PetscRandomOps_Values = {
 68:   /* 0 */
 69:   PetscRandomSeed_Rand48,
 70:   PetscRandomGetValue_Rand48,
 71:   PetscRandomGetValueReal_Rand48,
 72:   PetscRandomGetValueImaginary_Rand48,
 73:   0,
 74:   /* 5 */
 75:   0
 76: };

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

 91:   PetscMemcpy(r->ops,&PetscRandomOps_Values,sizeof(PetscRandomOps_Values));
 92:   /* r->bops->publish   = PetscRandomPublish; */
 93:   /*  r->petscnative     = PETSC_TRUE;  */

 95:   PetscObjectChangeTypeName((PetscObject)r,PETSCRAND48);
 96:   PetscPublishAll(r);
 97:   return(0);
 98: }