Actual source code: randreg.c
2: #include <../src/sys/random/randomimpl.h> /*I "petscsys.h" I*/
4: PetscFList PetscRandomList = PETSC_NULL;
5: PetscBool PetscRandomRegisterAllCalled = PETSC_FALSE;
9: /*@C
10: PetscRandomSetType - Builds a context for generating particular type of random numbers.
12: Collective on PetscRandom
14: Input Parameters:
15: + rnd - The random number generator context
16: - type - The name of the random type
18: Options Database Key:
19: . -random_type <type> - Sets the random type; use -help for a list
20: of available types
22: Notes:
23: See "petsc/include/petscsys.h" for available random types (for instance, PETSCRAND48, PETSCRAND).
25: Level: intermediate
27: .keywords: random, set, type
28: .seealso: PetscRandomGetType(), PetscRandomCreate()
29: @*/
31: PetscErrorCode PetscRandomSetType(PetscRandom rnd, const PetscRandomType type)
32: {
33: PetscErrorCode (*r)(PetscRandom);
34: PetscBool match;
39: PetscTypeCompare((PetscObject)rnd, type, &match);
40: if (match) return(0);
42: PetscFListFind(PetscRandomList,((PetscObject)rnd)->comm, type,PETSC_TRUE,(void (**)(void)) &r);
43: if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown random type: %s", type);
45: if (rnd->ops->destroy) {
46: (*rnd->ops->destroy)(rnd);
47: }
48: (*r)(rnd);
49: PetscRandomSeed(rnd);
51: PetscObjectChangeTypeName((PetscObject)rnd, type);
52: #if defined(PETSC_HAVE_AMS)
53: if (PetscAMSPublishAll) {
54: PetscObjectAMSPublish((PetscObject)rnd);
55: }
56: #endif
57: return(0);
58: }
62: /*@C
63: PetscRandomGetType - Gets the type name (as a string) from the PetscRandom.
65: Not Collective
67: Input Parameter:
68: . rnd - The random number generator context
70: Output Parameter:
71: . type - The type name
73: Level: intermediate
75: .keywords: random, get, type, name
76: .seealso: PetscRandomSetType(), PetscRandomCreate()
77: @*/
78: PetscErrorCode PetscRandomGetType(PetscRandom rnd, const PetscRandomType *type)
79: {
83: *type = ((PetscObject)rnd)->type_name;
84: return(0);
85: }
89: /*@C
90: PetscRandomRegister - See PetscRandomRegisterDynamic()
92: Level: advanced
93: @*/
94: PetscErrorCode PetscRandomRegister(const char sname[], const char path[], const char name[], PetscErrorCode (*function)(PetscRandom))
95: {
96: char fullname[PETSC_MAX_PATH_LEN];
100: PetscFListConcat(path,name,fullname);
101: PetscFListAdd(&PetscRandomList,sname,fullname,(void (*)(void))function);
102: return(0);
103: }
106: /*--------------------------------------------------------------------------------------------------------------------*/
109: /*@C
110: PetscRandomRegisterDestroy - Frees the list of Random types that were registered by PetscRandomRegister()/PetscRandomRegisterDynamic().
112: Not Collective
114: Level: advanced
116: .keywords: PetscRandom, register, destroy
117: .seealso: PetscRandomRegister(), PetscRandomRegisterAll(), PetscRandomRegisterDynamic()
118: @*/
119: PetscErrorCode PetscRandomRegisterDestroy(void)
120: {
124: PetscFListDestroy(&PetscRandomList);
125: PetscRandomRegisterAllCalled = PETSC_FALSE;
126: return(0);
127: }
130: #if defined(PETSC_HAVE_RAND)
132: #endif
133: #if defined(PETSC_HAVE_DRAND48)
135: #endif
136: #if defined(PETSC_HAVE_SPRNG)
138: #endif
143: /*@C
144: PetscRandomRegisterAll - Registers all of the components in the PetscRandom package.
146: Not Collective
148: Input parameter:
149: . path - The dynamic library path
151: Level: advanced
153: .keywords: PetscRandom, register, all
154: .seealso: PetscRandomRegister(), PetscRandomRegisterDestroy(), PetscRandomRegisterDynamic()
155: @*/
156: PetscErrorCode PetscRandomRegisterAll(const char path[])
157: {
161: PetscRandomRegisterAllCalled = PETSC_TRUE;
162: #if defined(PETSC_HAVE_RAND)
163: PetscRandomRegisterDynamic(PETSCRAND, path,"PetscRandomCreate_Rand", PetscRandomCreate_Rand);
164: #endif
165: #if defined(PETSC_HAVE_DRAND48)
166: PetscRandomRegisterDynamic(PETSCRAND48,path,"PetscRandomCreate_Rand48",PetscRandomCreate_Rand48);
167: #endif
168: #if defined(PETSC_HAVE_SPRNG)
169: PetscRandomRegisterDynamic(PETSCSPRNG,path,"PetscRandomCreate_Sprng",PetscRandomCreate_Sprng);
170: #endif
171: return(0);
172: }