1 2 #include <../src/sys/classes/random/randomimpl.h> /*I "petscsys.h" I*/ 3 4 PetscFunctionList PetscRandomList = NULL; 5 PetscBool PetscRandomRegisterAllCalled = PETSC_FALSE; 6 7 #undef __FUNCT__ 8 #define __FUNCT__ "PetscRandomSetType" 9 /*@C 10 PetscRandomSetType - Builds a context for generating particular type of random numbers. 11 12 Collective on PetscRandom 13 14 Input Parameters: 15 + rnd - The random number generator context 16 - type - The name of the random type 17 18 Options Database Key: 19 . -random_type <type> - Sets the random type; use -help for a list 20 of available types 21 22 Notes: 23 See "petsc/include/petscsys.h" for available random types (for instance, PETSCRAND48, PETSCRAND). 24 25 Level: intermediate 26 27 .keywords: random, set, type 28 .seealso: PetscRandomGetType(), PetscRandomCreate() 29 @*/ 30 31 PetscErrorCode PetscRandomSetType(PetscRandom rnd, PetscRandomType type) 32 { 33 PetscErrorCode (*r)(PetscRandom); 34 PetscBool match; 35 PetscErrorCode ierr; 36 37 PetscFunctionBegin; 38 PetscValidHeaderSpecific(rnd, PETSC_RANDOM_CLASSID,1); 39 ierr = PetscObjectTypeCompare((PetscObject)rnd, type, &match);CHKERRQ(ierr); 40 if (match) PetscFunctionReturn(0); 41 42 ierr = PetscFunctionListFind(PetscObjectComm((PetscObject)rnd),PetscRandomList, type,PETSC_TRUE,(void (**)(void)) &r);CHKERRQ(ierr); 43 if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown random type: %s", type); 44 45 if (rnd->ops->destroy) { 46 ierr = (*rnd->ops->destroy)(rnd);CHKERRQ(ierr); 47 48 rnd->ops->destroy = NULL; 49 } 50 ierr = (*r)(rnd);CHKERRQ(ierr); 51 ierr = PetscRandomSeed(rnd);CHKERRQ(ierr); 52 53 ierr = PetscObjectChangeTypeName((PetscObject)rnd, type);CHKERRQ(ierr); 54 PetscFunctionReturn(0); 55 } 56 57 #undef __FUNCT__ 58 #define __FUNCT__ "PetscRandomGetType" 59 /*@C 60 PetscRandomGetType - Gets the type name (as a string) from the PetscRandom. 61 62 Not Collective 63 64 Input Parameter: 65 . rnd - The random number generator context 66 67 Output Parameter: 68 . type - The type name 69 70 Level: intermediate 71 72 .keywords: random, get, type, name 73 .seealso: PetscRandomSetType(), PetscRandomCreate() 74 @*/ 75 PetscErrorCode PetscRandomGetType(PetscRandom rnd, PetscRandomType *type) 76 { 77 PetscFunctionBegin; 78 PetscValidHeaderSpecific(rnd, PETSC_RANDOM_CLASSID,1); 79 PetscValidPointer(type,2); 80 *type = ((PetscObject)rnd)->type_name; 81 PetscFunctionReturn(0); 82 } 83 84 #undef __FUNCT__ 85 #define __FUNCT__ "PetscRandomRegister" 86 /*@C 87 PetscRandomRegister - See PetscRandomRegisterDynamic() 88 89 Level: advanced 90 @*/ 91 PetscErrorCode PetscRandomRegister(const char sname[], const char path[], const char name[], PetscErrorCode (*function)(PetscRandom)) 92 { 93 char fullname[PETSC_MAX_PATH_LEN]; 94 PetscErrorCode ierr; 95 96 PetscFunctionBegin; 97 ierr = PetscFunctionListConcat(path,name,fullname);CHKERRQ(ierr); 98 ierr = PetscFunctionListAdd(PETSC_COMM_WORLD,&PetscRandomList,sname,fullname,(void (*)(void))function);CHKERRQ(ierr); 99 PetscFunctionReturn(0); 100 } 101 102 103 /*--------------------------------------------------------------------------------------------------------------------*/ 104 #undef __FUNCT__ 105 #define __FUNCT__ "PetscRandomRegisterDestroy" 106 /*@C 107 PetscRandomRegisterDestroy - Frees the list of Random types that were registered by PetscRandomRegister()/PetscRandomRegisterDynamic(). 108 109 Not Collective 110 111 Level: advanced 112 113 .keywords: PetscRandom, register, destroy 114 .seealso: PetscRandomRegister(), PetscRandomRegisterAll(), PetscRandomRegisterDynamic() 115 @*/ 116 PetscErrorCode PetscRandomRegisterDestroy(void) 117 { 118 PetscErrorCode ierr; 119 120 PetscFunctionBegin; 121 ierr = PetscFunctionListDestroy(&PetscRandomList);CHKERRQ(ierr); 122 123 PetscRandomRegisterAllCalled = PETSC_FALSE; 124 PetscFunctionReturn(0); 125 } 126 127 #if defined(PETSC_HAVE_RAND) 128 PETSC_EXTERN PetscErrorCode PetscRandomCreate_Rand(PetscRandom); 129 #endif 130 #if defined(PETSC_HAVE_DRAND48) 131 PETSC_EXTERN PetscErrorCode PetscRandomCreate_Rand48(PetscRandom); 132 #endif 133 #if defined(PETSC_HAVE_SPRNG) 134 PETSC_EXTERN PetscErrorCode PetscRandomCreate_Sprng(PetscRandom); 135 #endif 136 137 #undef __FUNCT__ 138 #define __FUNCT__ "PetscRandomRegisterAll" 139 /*@C 140 PetscRandomRegisterAll - Registers all of the components in the PetscRandom package. 141 142 Not Collective 143 144 Input parameter: 145 . path - The dynamic library path 146 147 Level: advanced 148 149 .keywords: PetscRandom, register, all 150 .seealso: PetscRandomRegister(), PetscRandomRegisterDestroy(), PetscRandomRegisterDynamic() 151 @*/ 152 PetscErrorCode PetscRandomRegisterAll(const char path[]) 153 { 154 PetscErrorCode ierr; 155 156 PetscFunctionBegin; 157 PetscRandomRegisterAllCalled = PETSC_TRUE; 158 #if defined(PETSC_HAVE_RAND) 159 ierr = PetscRandomRegisterDynamic(PETSCRAND, path,"PetscRandomCreate_Rand", PetscRandomCreate_Rand);CHKERRQ(ierr); 160 #endif 161 #if defined(PETSC_HAVE_DRAND48) 162 ierr = PetscRandomRegisterDynamic(PETSCRAND48,path,"PetscRandomCreate_Rand48",PetscRandomCreate_Rand48);CHKERRQ(ierr); 163 #endif 164 #if defined(PETSC_HAVE_SPRNG) 165 ierr = PetscRandomRegisterDynamic(PETSCSPRNG,path,"PetscRandomCreate_Sprng",PetscRandomCreate_Sprng);CHKERRQ(ierr); 166 #endif 167 PetscFunctionReturn(0); 168 } 169 170