1 #define PETSC_DESIRE_FEATURE_TEST_MACROS /* for drand48() */ 2 #include <petsc/private/randomimpl.h> 3 4 PetscErrorCode PetscRandomSeed_Rand48(PetscRandom r) { 5 PetscFunctionBegin; 6 srand48(r->seed); 7 PetscFunctionReturn(0); 8 } 9 10 PetscErrorCode PetscRandomGetValue_Rand48(PetscRandom r, PetscScalar *val) { 11 PetscFunctionBegin; 12 #if defined(PETSC_USE_COMPLEX) 13 if (r->iset) { 14 *val = PetscRealPart(r->width) * (PetscReal)drand48() + PetscRealPart(r->low) + (PetscImaginaryPart(r->width) * (PetscReal)drand48() + PetscImaginaryPart(r->low)) * PETSC_i; 15 } else { 16 *val = (PetscReal)drand48() + (PetscReal)drand48() * PETSC_i; 17 } 18 #else 19 if (r->iset) *val = r->width * drand48() + r->low; 20 else *val = drand48(); 21 #endif 22 PetscFunctionReturn(0); 23 } 24 25 PetscErrorCode PetscRandomGetValueReal_Rand48(PetscRandom r, PetscReal *val) { 26 PetscFunctionBegin; 27 #if defined(PETSC_USE_COMPLEX) 28 if (r->iset) *val = PetscRealPart(r->width) * drand48() + PetscRealPart(r->low); 29 else *val = drand48(); 30 #else 31 if (r->iset) *val = r->width * drand48() + r->low; 32 else *val = drand48(); 33 #endif 34 PetscFunctionReturn(0); 35 } 36 37 static struct _PetscRandomOps PetscRandomOps_Values = { 38 PetscDesignatedInitializer(seed, PetscRandomSeed_Rand48), 39 PetscDesignatedInitializer(getvalue, PetscRandomGetValue_Rand48), 40 PetscDesignatedInitializer(getvaluereal, PetscRandomGetValueReal_Rand48), 41 }; 42 43 /*MC 44 PETSCRAND48 - access to the basic Unix drand48() random number generator 45 46 Options Database Keys: 47 . -random_type <rand,rand48,sprng> - select the random number generator at runtime 48 49 Level: beginner 50 51 Notes: 52 Not recommended because it may produce different results on different systems. 53 54 .seealso: `PetscRandomCreate()`, `PetscRandomSetType()`, `PETSCRAND`, `PETSCSPRNG`, `PetscRandomSetFromOptions()` 55 M*/ 56 57 PETSC_EXTERN PetscErrorCode PetscRandomCreate_Rand48(PetscRandom r) { 58 PetscFunctionBegin; 59 PetscCall(PetscMemcpy(r->ops, &PetscRandomOps_Values, sizeof(PetscRandomOps_Values))); 60 PetscCall(PetscObjectChangeTypeName((PetscObject)r, PETSCRAND48)); 61 PetscFunctionReturn(0); 62 } 63