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