1 2 #include <../src/sys/classes/random/randomimpl.h> 3 #if defined (PETSC_HAVE_STDLIB_H) 4 #include <stdlib.h> 5 #endif 6 7 #undef __FUNCT__ 8 #define __FUNCT__ "PetscRandomSeed_Rand48" 9 PetscErrorCode PetscRandomSeed_Rand48(PetscRandom r) 10 { 11 PetscFunctionBegin; 12 srand48(r->seed); 13 PetscFunctionReturn(0); 14 } 15 16 #undef __FUNCT__ 17 #define __FUNCT__ "PetscRandomGetValue_Rand48" 18 PetscErrorCode PetscRandomGetValue_Rand48(PetscRandom r,PetscScalar *val) 19 { 20 PetscFunctionBegin; 21 #if defined(PETSC_USE_COMPLEX) 22 if (r->iset) { 23 *val = PetscRealPart(r->width)*(PetscReal)drand48() + PetscRealPart(r->low) + 24 (PetscImaginaryPart(r->width)*(PetscReal)drand48() + PetscImaginaryPart(r->low)) * PETSC_i; 25 } else { 26 *val = (PetscReal)drand48() + (PetscReal)drand48()*PETSC_i; 27 } 28 #else 29 if (r->iset) *val = r->width * drand48() + r->low; 30 else *val = drand48(); 31 #endif 32 PetscFunctionReturn(0); 33 } 34 35 #undef __FUNCT__ 36 #define __FUNCT__ "PetscRandomGetValueReal_Rand48" 37 PetscErrorCode PetscRandomGetValueReal_Rand48(PetscRandom r,PetscReal *val) 38 { 39 PetscFunctionBegin; 40 #if defined(PETSC_USE_COMPLEX) 41 if (r->iset) *val = PetscRealPart(r->width)*drand48() + PetscRealPart(r->low); 42 else *val = drand48(); 43 #else 44 if (r->iset) *val = r->width * drand48() + r->low; 45 else *val = drand48(); 46 #endif 47 PetscFunctionReturn(0); 48 } 49 50 static struct _PetscRandomOps PetscRandomOps_Values = { 51 /* 0 */ 52 PetscRandomSeed_Rand48, 53 PetscRandomGetValue_Rand48, 54 PetscRandomGetValueReal_Rand48, 55 0, 56 /* 5 */ 57 0 58 }; 59 60 /*MC 61 PETSCRAND48 - access to the basic Unix drand48() random number generator 62 63 Options Database Keys: 64 . -random_type <rand,rand48,sprng> 65 66 Level: beginner 67 68 .seealso: RandomCreate(), RandomSetType(), PETSCRAND, PETSCSPRNG 69 M*/ 70 71 EXTERN_C_BEGIN 72 #undef __FUNCT__ 73 #define __FUNCT__ "PetscRandomCreate_Rand48" 74 PetscErrorCode PetscRandomCreate_Rand48(PetscRandom r) 75 { 76 PetscErrorCode ierr; 77 78 PetscFunctionBegin; 79 ierr = PetscMemcpy(r->ops,&PetscRandomOps_Values,sizeof(PetscRandomOps_Values));CHKERRQ(ierr); 80 /* r->bops->publish = PetscRandomPublish; */ 81 /* r->petscnative = PETSC_TRUE; */ 82 83 ierr = PetscObjectChangeTypeName((PetscObject)r,PETSCRAND48);CHKERRQ(ierr); 84 PetscFunctionReturn(0); 85 } 86 EXTERN_C_END 87