Actual source code: randreg.c

petsc-3.3-p7 2013-05-11
  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:   PetscObjectTypeCompare((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:     rnd->ops->destroy = PETSC_NULL;
 48:   }
 49:   (*r)(rnd);
 50:   PetscRandomSeed(rnd);

 52:   PetscObjectChangeTypeName((PetscObject)rnd, type);
 53: #if defined(PETSC_HAVE_AMS)
 54:   if (PetscAMSPublishAll) {
 55:     PetscObjectAMSPublish((PetscObject)rnd);
 56:   }
 57: #endif
 58:   return(0);
 59: }

 63: /*@C
 64:   PetscRandomGetType - Gets the type name (as a string) from the PetscRandom.

 66:   Not Collective

 68:   Input Parameter:
 69: . rnd  - The random number generator context

 71:   Output Parameter:
 72: . type - The type name

 74:   Level: intermediate

 76: .keywords: random, get, type, name
 77: .seealso: PetscRandomSetType(), PetscRandomCreate()
 78: @*/
 79: PetscErrorCode  PetscRandomGetType(PetscRandom rnd, const PetscRandomType *type)
 80: {
 84:   *type = ((PetscObject)rnd)->type_name;
 85:   return(0);
 86: }

 90: /*@C
 91:   PetscRandomRegister - See PetscRandomRegisterDynamic()

 93:   Level: advanced
 94: @*/
 95: PetscErrorCode  PetscRandomRegister(const char sname[], const char path[], const char name[], PetscErrorCode (*function)(PetscRandom))
 96: {
 97:   char           fullname[PETSC_MAX_PATH_LEN];

101:   PetscFListConcat(path,name,fullname);
102:   PetscFListAdd(&PetscRandomList,sname,fullname,(void (*)(void))function);
103:   return(0);
104: }


107: /*--------------------------------------------------------------------------------------------------------------------*/
110: /*@C
111:    PetscRandomRegisterDestroy - Frees the list of Random types that were registered by PetscRandomRegister()/PetscRandomRegisterDynamic().

113:    Not Collective

115:    Level: advanced

117: .keywords: PetscRandom, register, destroy
118: .seealso: PetscRandomRegister(), PetscRandomRegisterAll(), PetscRandomRegisterDynamic()
119: @*/
120: PetscErrorCode  PetscRandomRegisterDestroy(void)
121: {

125:   PetscFListDestroy(&PetscRandomList);
126:   PetscRandomRegisterAllCalled = PETSC_FALSE;
127:   return(0);
128: }

130: EXTERN_C_BEGIN
131: #if defined(PETSC_HAVE_RAND)
132: extern PetscErrorCode  PetscRandomCreate_Rand(PetscRandom);
133: #endif
134: #if defined(PETSC_HAVE_DRAND48)
135: extern PetscErrorCode  PetscRandomCreate_Rand48(PetscRandom);
136: #endif
137: #if defined(PETSC_HAVE_SPRNG)
138: extern PetscErrorCode  PetscRandomCreate_Sprng(PetscRandom);
139: #endif
140: EXTERN_C_END

144: /*@C
145:   PetscRandomRegisterAll - Registers all of the components in the PetscRandom package.

147:   Not Collective

149:   Input parameter:
150: . path - The dynamic library path

152:   Level: advanced

154: .keywords: PetscRandom, register, all
155: .seealso:  PetscRandomRegister(), PetscRandomRegisterDestroy(), PetscRandomRegisterDynamic()
156: @*/
157: PetscErrorCode  PetscRandomRegisterAll(const char path[])
158: {

162:   PetscRandomRegisterAllCalled = PETSC_TRUE;
163: #if defined(PETSC_HAVE_RAND)
164:   PetscRandomRegisterDynamic(PETSCRAND,  path,"PetscRandomCreate_Rand",  PetscRandomCreate_Rand);
165: #endif
166: #if defined(PETSC_HAVE_DRAND48)
167:   PetscRandomRegisterDynamic(PETSCRAND48,path,"PetscRandomCreate_Rand48",PetscRandomCreate_Rand48);
168: #endif
169: #if defined(PETSC_HAVE_SPRNG)
170:   PetscRandomRegisterDynamic(PETSCSPRNG,path,"PetscRandomCreate_Sprng",PetscRandomCreate_Sprng);
171: #endif
172:   return(0);
173: }