1 #ifndef lint 2 static char vcid[] = "$Id: pcset.c,v 1.30 1995/11/30 22:32:59 bsmith Exp $"; 3 #endif 4 /* 5 Routines to set PC methods and options. 6 */ 7 8 #include "petsc.h" 9 #include "pcimpl.h" /*I "pc.h" I*/ 10 #include <stdio.h> 11 #include "sys/nreg.h" 12 #include "sys.h" 13 14 static NRList *__PCList = 0; 15 16 /*@ 17 PCSetMethod - Builds PC for a particular preconditioner. It is 18 best to use the SLESSetFromOptions() command and 19 set the PC method from the command line rather then 20 by using this routine. 21 22 Input Parameter: 23 . pc - the preconditioner context. 24 . method - a known method 25 26 Options Database Command: 27 $ -pc_method <method> 28 $ Use -help for a list of available methods 29 $ (for instance, jacobi or bjacobi) 30 31 Notes: 32 See "petsc/include/pc.h" for available methods (for instance, 33 PCJACOBI, PCILU, or PCBJACOBI). 34 35 .keywords: PC, set, method 36 @*/ 37 int PCSetMethod(PC ctx,PCMethod method) 38 { 39 int ierr,(*r)(PC); 40 PETSCVALIDHEADERSPECIFIC(ctx,PC_COOKIE); 41 if (ctx->setupcalled) { 42 if (ctx->destroy) ierr = (*ctx->destroy)((PetscObject)ctx); 43 else {if (ctx->data) PetscFree(ctx->data);} 44 ctx->data = 0; 45 } 46 /* Get the function pointers for the method requested */ 47 if (!__PCList) {PCRegisterAll();} 48 if (!__PCList) {SETERRQ(1,"PCSetMethod:Could not get list of methods");} 49 r = (int (*)(PC))NRFindRoutine( __PCList, (int)method, (char *)0 ); 50 if (!r) {SETERRQ(1,"PCSetMethod:Unknown method");} 51 if (ctx->data) PetscFree(ctx->data); 52 ctx->setfrom = ( int (*)(PC) ) 0; 53 ctx->printhelp = ( int (*)(PC) ) 0; 54 ctx->setup = ( int (*)(PC) ) 0; 55 ctx->destroy = ( int (*)(PetscObject) ) 0; 56 return (*r)(ctx); 57 } 58 59 /*@C 60 PCRegister - Adds the iterative method to the preconditioner 61 package, given an iterative name (PCMethod) and a function pointer. 62 63 Input Parameters: 64 . name - for instance PCJACOBI, ... 65 . sname - corresponding string for name 66 . create - routine to create method context 67 68 .keywords: PC, register 69 70 .seealso: PCRegisterAll(), PCRegisterDestroy() 71 @*/ 72 int PCRegister(PCMethod name,char *sname,int (*create)(PC)) 73 { 74 int ierr; 75 if (!__PCList) {ierr = NRCreate(&__PCList); CHKERRQ(ierr);} 76 return NRRegister( __PCList, (int) name, sname, (int (*)(void*)) create ); 77 } 78 79 /*@C 80 PCRegisterDestroy - Frees the list of preconditioners that were 81 registered by PCRegister(). 82 83 .keywords: PC, register, destroy 84 85 .seealso: PCRegisterAll(), PCRegisterAll() 86 @*/ 87 int PCRegisterDestroy() 88 { 89 if (__PCList) { 90 NRDestroy( __PCList ); 91 __PCList = 0; 92 } 93 return 0; 94 } 95 96 /* 97 PCGetMethodFromOptions_Private - Sets the selected PC method from the 98 options database. 99 100 Input Parameter: 101 . pc - the preconditioner context 102 103 Output Parameter: 104 . method - PC method 105 106 Returns: 107 1 if method is found; otherwise 0. 108 109 Options Database Key: 110 $ -pc_method method 111 */ 112 int PCGetMethodFromOptions_Private(PC pc,PCMethod *method ) 113 { 114 int ierr; 115 char sbuf[50]; 116 if (OptionsGetString( pc->prefix,"-pc_method", sbuf, 50 )) { 117 if (!__PCList) {ierr = PCRegisterAll(); CHKERRQ(ierr);} 118 *method = (PCMethod)NRFindID( __PCList, sbuf ); 119 return 1; 120 } 121 return 0; 122 } 123 124 /*@C 125 PCGetMethodName - Gets the PC method name (as a string) from the 126 method type. 127 128 Input Parameter: 129 . meth - preconditioner method 130 131 Output Parameter: 132 . name - name of preconditioner 133 134 .keywords: PC, get, method, name 135 @*/ 136 int PCGetMethodName(PCMethod meth,char **name) 137 { 138 int ierr; 139 if (!__PCList) {ierr = PCRegisterAll(); CHKERRQ(ierr);} 140 *name = NRFindName( __PCList, (int)meth ); 141 return 0; 142 } 143 144 /* 145 PCPrintMethods_Private - Prints the PC methods available from the options 146 database. 147 148 Input Parameters: 149 . prefix - prefix (usually "-") 150 . name - the options database name (by default "pc_method") 151 */ 152 int PCPrintMethods_Private(char *prefix,char *name) 153 { 154 FuncList *entry; 155 int ierr; 156 if (!__PCList) {ierr = PCRegisterAll(); CHKERRQ(ierr);} 157 entry = __PCList->head; 158 MPIU_printf(MPI_COMM_WORLD," %s%s (one of)",prefix,name); 159 while (entry) { 160 MPIU_printf(MPI_COMM_WORLD," %s",entry->name); 161 entry = entry->next; 162 } 163 MPIU_printf(MPI_COMM_WORLD,"\n"); 164 return 0; 165 } 166 167 /*@ 168 PCSetFromOptions - Sets PC options from the options database. 169 This routine must be called before PCSetUp() if the user is to be 170 allowed to set the preconditioner method. 171 172 Input Parameters: 173 . pc - the preconditioner context 174 175 .keywords: PC, set, from, options, database 176 177 .seealso: PCPrintHelp() 178 @*/ 179 int PCSetFromOptions(PC pc) 180 { 181 PCMethod method; 182 PETSCVALIDHEADERSPECIFIC(pc,PC_COOKIE); 183 184 if (PCGetMethodFromOptions_Private(pc,&method)) { 185 PCSetMethod(pc,method); 186 } 187 if (OptionsHasName(PetscNull,"-help")){ 188 PCPrintHelp(pc); 189 } 190 if (pc->setfrom) return (*pc->setfrom)(pc); 191 return 0; 192 } 193