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