xref: /petsc/src/sys/classes/random/impls/sprng/sprng.c (revision c4762a1b19cd2af06abeed90e8f9d34fb975dd94)
1 
2 #include <../src/sys/classes/random/randomimpl.h>
3 
4 #define USE_MPI
5 #define SIMPLE_SPRNG
6 EXTERN_C_BEGIN
7 #include <sprng.h>
8 EXTERN_C_END
9 
10 PetscErrorCode  PetscRandomSeed_Sprng(PetscRandom r)
11 {
12   PetscFunctionBegin;
13   init_sprng(r->seed,SPRNG_DEFAULT);
14   PetscFunctionReturn(0);
15 }
16 
17 PetscErrorCode  PetscRandomGetValue_Sprng(PetscRandom r,PetscScalar *val)
18 {
19   PetscFunctionBegin;
20 #if defined(PETSC_USE_COMPLEX)
21   if (r->iset) {
22     *val = PetscRealPart(r->width)*sprng() + PetscRealPart(r->low) + (PetscImaginaryPart(r->width)*sprng() + PetscImaginaryPart(r->low)) * PETSC_i;
23   } else {
24     *val = sprng() + sprng()*PETSC_i;
25   }
26 #else
27   if (r->iset) *val = r->width * sprng() + r->low;
28   else         *val = sprng();
29 #endif
30   PetscFunctionReturn(0);
31 }
32 
33 PetscErrorCode  PetscRandomGetValueReal_Sprng(PetscRandom r,PetscReal *val)
34 {
35   PetscFunctionBegin;
36 #if defined(PETSC_USE_COMPLEX)
37   if (r->iset) *val = PetscRealPart(r->width)*sprng() + PetscRealPart(r->low);
38   else         *val = sprng();
39 #else
40   if (r->iset) *val = r->width * sprng() + r->low;
41   else         *val = sprng();
42 #endif
43   PetscFunctionReturn(0);
44 }
45 
46 static struct _PetscRandomOps PetscRandomOps_Values = {
47   /* 0 */
48   PetscRandomSeed_Sprng,
49   PetscRandomGetValue_Sprng,
50   PetscRandomGetValueReal_Sprng,
51   0,
52   /* 5 */
53   0
54 };
55 
56 /*MC
57    PETSCSPRNG- access to the publically available random number generator sprng
58 
59    Options Database Keys:
60 . -random_type <rand,rand48,sprng>
61 
62   Level: beginner
63 
64    PETSc must have been ./configure with the option --download-sprng to use
65    this random number generator.
66 
67    This is NOT currently using a parallel random number generator. Sprng does have
68    an MPI version we should investigate.
69 
70 .seealso: RandomCreate(), RandomSetType(), PETSCRAND, PETSCRAND48
71 M*/
72 
73 PETSC_EXTERN PetscErrorCode PetscRandomCreate_Sprng(PetscRandom r)
74 {
75   PetscErrorCode ierr;
76 
77   PetscFunctionBegin;
78   ierr = PetscMemcpy(r->ops,&PetscRandomOps_Values,sizeof(PetscRandomOps_Values));CHKERRQ(ierr);
79   ierr = PetscObjectChangeTypeName((PetscObject)r,PETSCSPRNG);CHKERRQ(ierr);
80   PetscFunctionReturn(0);
81 }
82