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