14b9ad928SBarry Smith /*$Id: pcset.c,v 1.118 2001/08/21 21:03:13 bsmith Exp $*/ 24b9ad928SBarry Smith /* 34b9ad928SBarry Smith Routines to set PC methods and options. 44b9ad928SBarry Smith */ 54b9ad928SBarry Smith 64b9ad928SBarry Smith #include "src/ksp/pc/pcimpl.h" /*I "petscpc.h" I*/ 74b9ad928SBarry Smith #include "petscsys.h" 84b9ad928SBarry Smith 94b9ad928SBarry Smith PetscTruth PCRegisterAllCalled = PETSC_FALSE; 104b9ad928SBarry Smith /* 114b9ad928SBarry Smith Contains the list of registered KSP routines 124b9ad928SBarry Smith */ 134b9ad928SBarry Smith PetscFList PCList = 0; 144b9ad928SBarry Smith 154b9ad928SBarry Smith #undef __FUNCT__ 164b9ad928SBarry Smith #define __FUNCT__ "PCSetType" 174b9ad928SBarry Smith /*@C 184b9ad928SBarry Smith PCSetType - Builds PC for a particular preconditioner. 194b9ad928SBarry Smith 204b9ad928SBarry Smith Collective on PC 214b9ad928SBarry Smith 224b9ad928SBarry Smith Input Parameter: 234b9ad928SBarry Smith + pc - the preconditioner context. 244b9ad928SBarry Smith - type - a known method 254b9ad928SBarry Smith 264b9ad928SBarry Smith Options Database Key: 274b9ad928SBarry Smith . -pc_type <type> - Sets PC type 284b9ad928SBarry Smith 294b9ad928SBarry Smith Use -help for a list of available methods (for instance, 304b9ad928SBarry Smith jacobi or bjacobi) 314b9ad928SBarry Smith 324b9ad928SBarry Smith Notes: 334b9ad928SBarry Smith See "petsc/include/petscpc.h" for available methods (for instance, 344b9ad928SBarry Smith PCJACOBI, PCILU, or PCBJACOBI). 354b9ad928SBarry Smith 364b9ad928SBarry Smith Normally, it is best to use the KSPSetFromOptions() command and 374b9ad928SBarry Smith then set the PC type from the options database rather than by using 384b9ad928SBarry Smith this routine. Using the options database provides the user with 394b9ad928SBarry Smith maximum flexibility in evaluating the many different preconditioners. 404b9ad928SBarry Smith The PCSetType() routine is provided for those situations where it 414b9ad928SBarry Smith is necessary to set the preconditioner independently of the command 424b9ad928SBarry Smith line or options database. This might be the case, for example, when 434b9ad928SBarry Smith the choice of preconditioner changes during the execution of the 444b9ad928SBarry Smith program, and the user's application is taking responsibility for 454b9ad928SBarry Smith choosing the appropriate preconditioner. In other words, this 464b9ad928SBarry Smith routine is not for beginners. 474b9ad928SBarry Smith 484b9ad928SBarry Smith Level: intermediate 494b9ad928SBarry Smith 504b9ad928SBarry Smith .keywords: PC, set, method, type 514b9ad928SBarry Smith 524b9ad928SBarry Smith .seealso: KSPSetType(), PCType 534b9ad928SBarry Smith 544b9ad928SBarry Smith @*/ 550e33f6ddSBarry Smith int PCSetType(PC pc,const PCType type) 564b9ad928SBarry Smith { 574b9ad928SBarry Smith int ierr,(*r)(PC); 584b9ad928SBarry Smith PetscTruth match; 594b9ad928SBarry Smith 604b9ad928SBarry Smith PetscFunctionBegin; 614b9ad928SBarry Smith PetscValidHeaderSpecific(pc,PC_COOKIE); 624b9ad928SBarry Smith PetscValidCharPointer(type); 634b9ad928SBarry Smith 644b9ad928SBarry Smith ierr = PetscTypeCompare((PetscObject)pc,type,&match);CHKERRQ(ierr); 654b9ad928SBarry Smith if (match) PetscFunctionReturn(0); 664b9ad928SBarry Smith 674b9ad928SBarry Smith if (pc->ops->destroy) {ierr = (*pc->ops->destroy)(pc);CHKERRQ(ierr);} 684b9ad928SBarry Smith ierr = PetscFListDestroy(&pc->qlist);CHKERRQ(ierr); 694b9ad928SBarry Smith pc->data = 0; 704b9ad928SBarry Smith pc->setupcalled = 0; 714b9ad928SBarry Smith 724b9ad928SBarry Smith /* Get the function pointers for the method requested */ 734b9ad928SBarry Smith if (!PCRegisterAllCalled) {ierr = PCRegisterAll(0);CHKERRQ(ierr);} 744b9ad928SBarry Smith 754b9ad928SBarry Smith /* Determine the PCCreateXXX routine for a particular preconditioner */ 764b9ad928SBarry Smith ierr = PetscFListFind(pc->comm,PCList,type,(void (**)(void)) &r);CHKERRQ(ierr); 774b9ad928SBarry Smith if (!r) SETERRQ1(1,"Unable to find requested PC type %s",type); 784b9ad928SBarry Smith if (pc->data) {ierr = PetscFree(pc->data);CHKERRQ(ierr);} 794b9ad928SBarry Smith 804b9ad928SBarry Smith pc->ops->destroy = (int (*)(PC)) 0; 814b9ad928SBarry Smith pc->ops->view = (int (*)(PC,PetscViewer)) 0; 824b9ad928SBarry Smith pc->ops->apply = (int (*)(PC,Vec,Vec)) 0; 834b9ad928SBarry Smith pc->ops->setup = (int (*)(PC)) 0; 844b9ad928SBarry Smith pc->ops->applyrichardson = (int (*)(PC,Vec,Vec,Vec,PetscReal,PetscReal,PetscReal,int)) 0; 854b9ad928SBarry Smith pc->ops->applyBA = (int (*)(PC,int,Vec,Vec,Vec)) 0; 864b9ad928SBarry Smith pc->ops->setfromoptions = (int (*)(PC)) 0; 874b9ad928SBarry Smith pc->ops->applytranspose = (int (*)(PC,Vec,Vec)) 0; 884b9ad928SBarry Smith pc->ops->applyBAtranspose = (int (*)(PC,int,Vec,Vec,Vec)) 0; 894b9ad928SBarry Smith pc->ops->presolve = (int (*)(PC,KSP,Vec,Vec)) 0; 904b9ad928SBarry Smith pc->ops->postsolve = (int (*)(PC,KSP,Vec,Vec)) 0; 914b9ad928SBarry Smith pc->ops->getfactoredmatrix = (int (*)(PC,Mat*)) 0; 924b9ad928SBarry Smith pc->ops->applysymmetricleft = (int (*)(PC,Vec,Vec)) 0; 934b9ad928SBarry Smith pc->ops->applysymmetricright = (int (*)(PC,Vec,Vec)) 0; 944b9ad928SBarry Smith pc->ops->setuponblocks = (int (*)(PC)) 0; 954b9ad928SBarry Smith pc->modifysubmatrices = (int (*)(PC,int,const IS[],const IS[],Mat[],void*)) 0; 964b9ad928SBarry Smith 974b9ad928SBarry Smith /* Call the PCCreateXXX routine for this particular preconditioner */ 984b9ad928SBarry Smith ierr = (*r)(pc);CHKERRQ(ierr); 994b9ad928SBarry Smith 1004b9ad928SBarry Smith ierr = PetscObjectChangeTypeName((PetscObject)pc,type);CHKERRQ(ierr); 1014b9ad928SBarry Smith PetscFunctionReturn(0); 1024b9ad928SBarry Smith } 1034b9ad928SBarry Smith 1044b9ad928SBarry Smith #undef __FUNCT__ 1054b9ad928SBarry Smith #define __FUNCT__ "PCRegisterDestroy" 1064b9ad928SBarry Smith /*@C 1074b9ad928SBarry Smith PCRegisterDestroy - Frees the list of preconditioners that were 1084b9ad928SBarry Smith registered by PCRegisterDynamic(). 1094b9ad928SBarry Smith 1104b9ad928SBarry Smith Not Collective 1114b9ad928SBarry Smith 1124b9ad928SBarry Smith Level: advanced 1134b9ad928SBarry Smith 1144b9ad928SBarry Smith .keywords: PC, register, destroy 1154b9ad928SBarry Smith 1164b9ad928SBarry Smith .seealso: PCRegisterAll(), PCRegisterAll() 1174b9ad928SBarry Smith 1184b9ad928SBarry Smith @*/ 1194b9ad928SBarry Smith int PCRegisterDestroy(void) 1204b9ad928SBarry Smith { 1214b9ad928SBarry Smith int ierr; 1224b9ad928SBarry Smith 1234b9ad928SBarry Smith PetscFunctionBegin; 1244b9ad928SBarry Smith if (PCList) { 1254b9ad928SBarry Smith ierr = PetscFListDestroy(&PCList);CHKERRQ(ierr); 1264b9ad928SBarry Smith PCList = 0; 1274b9ad928SBarry Smith } 1284b9ad928SBarry Smith PCRegisterAllCalled = PETSC_FALSE; 1294b9ad928SBarry Smith PetscFunctionReturn(0); 1304b9ad928SBarry Smith } 1314b9ad928SBarry Smith 1324b9ad928SBarry Smith #undef __FUNCT__ 1334b9ad928SBarry Smith #define __FUNCT__ "PCGetType" 1344b9ad928SBarry Smith /*@C 1354b9ad928SBarry Smith PCGetType - Gets the PC method type and name (as a string) from the PC 1364b9ad928SBarry Smith context. 1374b9ad928SBarry Smith 1384b9ad928SBarry Smith Not Collective 1394b9ad928SBarry Smith 1404b9ad928SBarry Smith Input Parameter: 1414b9ad928SBarry Smith . pc - the preconditioner context 1424b9ad928SBarry Smith 1434b9ad928SBarry Smith Output Parameter: 1444b9ad928SBarry Smith . name - name of preconditioner 1454b9ad928SBarry Smith 1464b9ad928SBarry Smith Level: intermediate 1474b9ad928SBarry Smith 1484b9ad928SBarry Smith .keywords: PC, get, method, name, type 1494b9ad928SBarry Smith 1504b9ad928SBarry Smith .seealso: PCSetType() 1514b9ad928SBarry Smith 1524b9ad928SBarry Smith @*/ 1534b9ad928SBarry Smith int PCGetType(PC pc,PCType *meth) 1544b9ad928SBarry Smith { 1554b9ad928SBarry Smith PetscFunctionBegin; 1564b9ad928SBarry Smith *meth = (PCType) pc->type_name; 1574b9ad928SBarry Smith PetscFunctionReturn(0); 1584b9ad928SBarry Smith } 1594b9ad928SBarry Smith 1604b9ad928SBarry Smith #undef __FUNCT__ 1614b9ad928SBarry Smith #define __FUNCT__ "PCSetFromOptions" 1624b9ad928SBarry Smith /*@ 1634b9ad928SBarry Smith PCSetFromOptions - Sets PC options from the options database. 1644b9ad928SBarry Smith This routine must be called before PCSetUp() if the user is to be 1654b9ad928SBarry Smith allowed to set the preconditioner method. 1664b9ad928SBarry Smith 1674b9ad928SBarry Smith Collective on PC 1684b9ad928SBarry Smith 1694b9ad928SBarry Smith Input Parameter: 1704b9ad928SBarry Smith . pc - the preconditioner context 1714b9ad928SBarry Smith 1724b9ad928SBarry Smith Level: developer 1734b9ad928SBarry Smith 1744b9ad928SBarry Smith .keywords: PC, set, from, options, database 1754b9ad928SBarry Smith 1764b9ad928SBarry Smith .seealso: 1774b9ad928SBarry Smith 1784b9ad928SBarry Smith @*/ 1794b9ad928SBarry Smith int PCSetFromOptions(PC pc) 1804b9ad928SBarry Smith { 1814b9ad928SBarry Smith int ierr; 182*2fc52814SBarry Smith char type[256]; 183*2fc52814SBarry Smith const char *def; 1844b9ad928SBarry Smith PetscTruth flg; 1854b9ad928SBarry Smith 1864b9ad928SBarry Smith PetscFunctionBegin; 1874b9ad928SBarry Smith PetscValidHeaderSpecific(pc,PC_COOKIE); 1884b9ad928SBarry Smith 1894b9ad928SBarry Smith if (!PCRegisterAllCalled) {ierr = PCRegisterAll(PETSC_NULL);CHKERRQ(ierr);} 1904b9ad928SBarry Smith ierr = PetscOptionsBegin(pc->comm,pc->prefix,"Preconditioner (PC) Options","PC");CHKERRQ(ierr); 1914b9ad928SBarry Smith if (!pc->type_name) { 1924b9ad928SBarry Smith PetscTruth ismatshell; 1934b9ad928SBarry Smith int size; 1944b9ad928SBarry Smith 1954b9ad928SBarry Smith /* 1964b9ad928SBarry Smith Shell matrix (probably) cannot support Bjacobi and ILU 1974b9ad928SBarry Smith */ 1984b9ad928SBarry Smith ierr = MPI_Comm_size(pc->comm,&size);CHKERRQ(ierr); 1994b9ad928SBarry Smith if (pc->pmat) { 2004b9ad928SBarry Smith ierr = PetscTypeCompare((PetscObject)pc->pmat,MATSHELL,&ismatshell);CHKERRQ(ierr); 2014b9ad928SBarry Smith } else { 2024b9ad928SBarry Smith ismatshell = PETSC_FALSE; /* matrix is not yet set, so guess that it will not be MATSHELL */ 2034b9ad928SBarry Smith } 2044b9ad928SBarry Smith /* 2054b9ad928SBarry Smith MATMFFD cannot support BJacobia and ILU 2064b9ad928SBarry Smith */ 2074b9ad928SBarry Smith if (!ismatshell) { 2084b9ad928SBarry Smith ierr = PetscTypeCompare((PetscObject)pc->pmat,MATMFFD,&ismatshell);CHKERRQ(ierr); 2094b9ad928SBarry Smith } 2104b9ad928SBarry Smith 2114b9ad928SBarry Smith if (ismatshell) { 2124b9ad928SBarry Smith def = PCNONE; 2134b9ad928SBarry Smith PetscLogInfo(pc,"PCSetOperators:Setting default PC to PCNONE since MATSHELL doesn't support\n\ 2144b9ad928SBarry Smith preconditioners (unless defined by the user)\n"); 2154b9ad928SBarry Smith } else if (size == 1) { 2164b9ad928SBarry Smith ierr = PetscTypeCompare((PetscObject)pc->pmat,MATSEQSBAIJ,&flg);CHKERRQ(ierr); 2174b9ad928SBarry Smith if (flg) { 2184b9ad928SBarry Smith def = PCICC; 2194b9ad928SBarry Smith } else { 2204b9ad928SBarry Smith def = PCILU; 2214b9ad928SBarry Smith } 2224b9ad928SBarry Smith } else { 2234b9ad928SBarry Smith def = PCBJACOBI; 2244b9ad928SBarry Smith } 2254b9ad928SBarry Smith } else { 2264b9ad928SBarry Smith def = pc->type_name; 2274b9ad928SBarry Smith } 2284b9ad928SBarry Smith 2294b9ad928SBarry Smith ierr = PetscOptionsList("-pc_type","Preconditioner","PCSetType",PCList,def,type,256,&flg);CHKERRQ(ierr); 2304b9ad928SBarry Smith if (flg) { 2314b9ad928SBarry Smith ierr = PCSetType(pc,type);CHKERRQ(ierr); 2324b9ad928SBarry Smith } 2334b9ad928SBarry Smith 2344b9ad928SBarry Smith ierr = PetscOptionsName("-pc_constant_null_space","Add constant null space to preconditioner","PCNullSpaceAttach",&flg);CHKERRQ(ierr); 2354b9ad928SBarry Smith if (flg) { 2364b9ad928SBarry Smith MatNullSpace nsp; 2374b9ad928SBarry Smith 2384b9ad928SBarry Smith ierr = MatNullSpaceCreate(pc->comm,1,0,0,&nsp);CHKERRQ(ierr); 2394b9ad928SBarry Smith ierr = PCNullSpaceAttach(pc,nsp);CHKERRQ(ierr); 2404b9ad928SBarry Smith ierr = MatNullSpaceDestroy(nsp);CHKERRQ(ierr); 2414b9ad928SBarry Smith } 2424b9ad928SBarry Smith 2434b9ad928SBarry Smith 2444b9ad928SBarry Smith /* option is actually checked in PCSetUp() */ 2454b9ad928SBarry Smith if (pc->nullsp) { 2464b9ad928SBarry Smith ierr = PetscOptionsName("-pc_test_null_space","Is provided null space correct","None",&flg);CHKERRQ(ierr); 2474b9ad928SBarry Smith } 2484b9ad928SBarry Smith 2494b9ad928SBarry Smith /* 2504b9ad928SBarry Smith Set the type if it was never set. 2514b9ad928SBarry Smith */ 2524b9ad928SBarry Smith if (!pc->type_name) { 2534b9ad928SBarry Smith ierr = PCSetType(pc,def);CHKERRQ(ierr); 2544b9ad928SBarry Smith } 2554b9ad928SBarry Smith 2564b9ad928SBarry Smith 2574b9ad928SBarry Smith 2584b9ad928SBarry Smith if (pc->ops->setfromoptions) { 2594b9ad928SBarry Smith ierr = (*pc->ops->setfromoptions)(pc);CHKERRQ(ierr); 2604b9ad928SBarry Smith } 2614b9ad928SBarry Smith ierr = PetscOptionsEnd();CHKERRQ(ierr); 2624b9ad928SBarry Smith #if defined(__cplusplus) && !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_SINGLE) && defined(PETSC_HAVE_CXX_NAMESPACE) 2634b9ad928SBarry Smith ierr = PCESISetFromOptions(pc);CHKERRQ(ierr); 2644b9ad928SBarry Smith #endif 2654b9ad928SBarry Smith PetscFunctionReturn(0); 2664b9ad928SBarry Smith } 267