17d0a6c19SBarry Smith 24b9ad928SBarry Smith /* 34b9ad928SBarry Smith Routines to set PC methods and options. 44b9ad928SBarry Smith */ 54b9ad928SBarry Smith 6b45d2f2cSJed Brown #include <petsc-private/pcimpl.h> /*I "petscpc.h" I*/ 71e25c274SJed Brown #include <petscdm.h> 84b9ad928SBarry Smith 9ace3abfcSBarry Smith PetscBool PCRegisterAllCalled = PETSC_FALSE; 104b9ad928SBarry Smith /* 114b9ad928SBarry Smith Contains the list of registered KSP routines 124b9ad928SBarry Smith */ 13140e18c1SBarry Smith PetscFunctionList PCList = 0; 144b9ad928SBarry Smith 154b9ad928SBarry Smith #undef __FUNCT__ 164b9ad928SBarry Smith #define __FUNCT__ "PCSetType" 174b9ad928SBarry Smith /*@C 18*8f6c3df8SBarry Smith PCSetType - Builds PC for a particular preconditioner type 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 50*8f6c3df8SBarry Smith Developer Note: PCRegister() is used to add preconditioner types to PCList from which they 51*8f6c3df8SBarry Smith are accessed by PCSetType(). 52*8f6c3df8SBarry Smith 534b9ad928SBarry Smith .keywords: PC, set, method, type 544b9ad928SBarry Smith 55*8f6c3df8SBarry Smith .seealso: KSPSetType(), PCType, PCRegister(), PCCreate(), KSPGetPC() 564b9ad928SBarry Smith 574b9ad928SBarry Smith @*/ 5819fd82e9SBarry Smith PetscErrorCode PCSetType(PC pc,PCType type) 594b9ad928SBarry Smith { 60dfbe8321SBarry Smith PetscErrorCode ierr,(*r)(PC); 61ace3abfcSBarry Smith PetscBool match; 624b9ad928SBarry Smith 634b9ad928SBarry Smith PetscFunctionBegin; 640700a824SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 654482741eSBarry Smith PetscValidCharPointer(type,2); 664b9ad928SBarry Smith 67251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)pc,type,&match);CHKERRQ(ierr); 684b9ad928SBarry Smith if (match) PetscFunctionReturn(0); 694b9ad928SBarry Smith 701c9cd337SJed Brown ierr = PetscFunctionListFind(PCList,type,&r);CHKERRQ(ierr); 71ce94432eSBarry Smith if (!r) SETERRQ1(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_UNKNOWN_TYPE,"Unable to find requested PC type %s",type); 72a7d21a52SLisandro Dalcin /* Destroy the previous private PC context */ 73b5c23020SJed Brown if (pc->ops->destroy) { 74b5c23020SJed Brown ierr = (*pc->ops->destroy)(pc);CHKERRQ(ierr); 750298fd71SBarry Smith pc->ops->destroy = NULL; 76b5c23020SJed Brown pc->data = 0; 77b5c23020SJed Brown } 78140e18c1SBarry Smith ierr = PetscFunctionListDestroy(&((PetscObject)pc)->qlist);CHKERRQ(ierr); 79a7d21a52SLisandro Dalcin /* Reinitialize function pointers in PCOps structure */ 80a7d21a52SLisandro Dalcin ierr = PetscMemzero(pc->ops,sizeof(struct _PCOps));CHKERRQ(ierr); 81a7d21a52SLisandro Dalcin /* XXX Is this OK?? */ 82a7d21a52SLisandro Dalcin pc->modifysubmatrices = 0; 83a7d21a52SLisandro Dalcin pc->modifysubmatricesP = 0; 84a7d21a52SLisandro Dalcin /* Call the PCCreate_XXX routine for this particular preconditioner */ 85a7d21a52SLisandro Dalcin pc->setupcalled = 0; 862fa5cd67SKarl Rupp 874b9ad928SBarry Smith ierr = PetscObjectChangeTypeName((PetscObject)pc,type);CHKERRQ(ierr); 8803bfa161SLisandro Dalcin ierr = (*r)(pc);CHKERRQ(ierr); 894b9ad928SBarry Smith PetscFunctionReturn(0); 904b9ad928SBarry Smith } 914b9ad928SBarry Smith 924b9ad928SBarry Smith #undef __FUNCT__ 934b9ad928SBarry Smith #define __FUNCT__ "PCGetType" 944b9ad928SBarry Smith /*@C 954b9ad928SBarry Smith PCGetType - Gets the PC method type and name (as a string) from the PC 964b9ad928SBarry Smith context. 974b9ad928SBarry Smith 984b9ad928SBarry Smith Not Collective 994b9ad928SBarry Smith 1004b9ad928SBarry Smith Input Parameter: 1014b9ad928SBarry Smith . pc - the preconditioner context 1024b9ad928SBarry Smith 1034b9ad928SBarry Smith Output Parameter: 104c4e43342SLisandro Dalcin . type - name of preconditioner method 1054b9ad928SBarry Smith 1064b9ad928SBarry Smith Level: intermediate 1074b9ad928SBarry Smith 1084b9ad928SBarry Smith .keywords: PC, get, method, name, type 1094b9ad928SBarry Smith 1104b9ad928SBarry Smith .seealso: PCSetType() 1114b9ad928SBarry Smith 1124b9ad928SBarry Smith @*/ 11319fd82e9SBarry Smith PetscErrorCode PCGetType(PC pc,PCType *type) 1144b9ad928SBarry Smith { 1154b9ad928SBarry Smith PetscFunctionBegin; 1160700a824SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 117c4e43342SLisandro Dalcin PetscValidPointer(type,2); 118c4e43342SLisandro Dalcin *type = ((PetscObject)pc)->type_name; 1194b9ad928SBarry Smith PetscFunctionReturn(0); 1204b9ad928SBarry Smith } 1214b9ad928SBarry Smith 12209573ac7SBarry Smith extern PetscErrorCode PCGetDefaultType_Private(PC,const char*[]); 123566e8bf2SBarry Smith 1244b9ad928SBarry Smith #undef __FUNCT__ 1254b9ad928SBarry Smith #define __FUNCT__ "PCSetFromOptions" 1264b9ad928SBarry Smith /*@ 1274b9ad928SBarry Smith PCSetFromOptions - Sets PC options from the options database. 1284b9ad928SBarry Smith This routine must be called before PCSetUp() if the user is to be 1294b9ad928SBarry Smith allowed to set the preconditioner method. 1304b9ad928SBarry Smith 1314b9ad928SBarry Smith Collective on PC 1324b9ad928SBarry Smith 1334b9ad928SBarry Smith Input Parameter: 1344b9ad928SBarry Smith . pc - the preconditioner context 1354b9ad928SBarry Smith 136b9ee023eSBarry Smith Options Database: 137b9ee023eSBarry Smith . -pc_use_amat true,false see PCSetUseAmat() 138b9ee023eSBarry Smith 1394b9ad928SBarry Smith Level: developer 1404b9ad928SBarry Smith 1414b9ad928SBarry Smith .keywords: PC, set, from, options, database 1424b9ad928SBarry Smith 143b9ee023eSBarry Smith .seealso: PCSetUseAmat() 1444b9ad928SBarry Smith 1454b9ad928SBarry Smith @*/ 1467087cfbeSBarry Smith PetscErrorCode PCSetFromOptions(PC pc) 1474b9ad928SBarry Smith { 148dfbe8321SBarry Smith PetscErrorCode ierr; 1492fc52814SBarry Smith char type[256]; 1502fc52814SBarry Smith const char *def; 151ace3abfcSBarry Smith PetscBool flg; 1524b9ad928SBarry Smith 1534b9ad928SBarry Smith PetscFunctionBegin; 1540700a824SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 1554b9ad928SBarry Smith 156607a6623SBarry Smith if (!PCRegisterAllCalled) {ierr = PCRegisterAll();CHKERRQ(ierr);} 1573194b578SJed Brown ierr = PetscObjectOptionsBegin((PetscObject)pc);CHKERRQ(ierr); 1587adad957SLisandro Dalcin if (!((PetscObject)pc)->type_name) { 159566e8bf2SBarry Smith ierr = PCGetDefaultType_Private(pc,&def);CHKERRQ(ierr); 1604b9ad928SBarry Smith } else { 1617adad957SLisandro Dalcin def = ((PetscObject)pc)->type_name; 1624b9ad928SBarry Smith } 1634b9ad928SBarry Smith 1644b9ad928SBarry Smith ierr = PetscOptionsList("-pc_type","Preconditioner","PCSetType",PCList,def,type,256,&flg);CHKERRQ(ierr); 1654b9ad928SBarry Smith if (flg) { 1664b9ad928SBarry Smith ierr = PCSetType(pc,type);CHKERRQ(ierr); 1677adad957SLisandro Dalcin } else if (!((PetscObject)pc)->type_name) { 168566e8bf2SBarry Smith ierr = PCSetType(pc,def);CHKERRQ(ierr); 1694b9ad928SBarry Smith } 1704b9ad928SBarry Smith 1718ef11f2bSJed Brown ierr = PetscOptionsBool("-pc_use_amat","use Amat (instead of Pmat) to define preconditioner in nested inner solves","PCSetUseAmat",pc->useAmat,&pc->useAmat,NULL);CHKERRQ(ierr); 172b4813acdSShri Abhyankar 1734b9ad928SBarry Smith if (pc->ops->setfromoptions) { 1744b9ad928SBarry Smith ierr = (*pc->ops->setfromoptions)(pc);CHKERRQ(ierr); 1754b9ad928SBarry Smith } 1765d973c19SBarry Smith 1775d973c19SBarry Smith /* process any options handlers added with PetscObjectAddOptionsHandler() */ 1785d973c19SBarry Smith ierr = PetscObjectProcessOptionsHandlers((PetscObject)pc);CHKERRQ(ierr); 1794b9ad928SBarry Smith ierr = PetscOptionsEnd();CHKERRQ(ierr); 1800c24e6a1SHong Zhang pc->setfromoptionscalled++; 1814b9ad928SBarry Smith PetscFunctionReturn(0); 1824b9ad928SBarry Smith } 1836c699258SBarry Smith 1846c699258SBarry Smith #undef __FUNCT__ 1856c699258SBarry Smith #define __FUNCT__ "PCSetDM" 1866c699258SBarry Smith /*@ 1876c699258SBarry Smith PCSetDM - Sets the DM that may be used by some preconditioners 1886c699258SBarry Smith 1893f9fe445SBarry Smith Logically Collective on PC 1906c699258SBarry Smith 1916c699258SBarry Smith Input Parameters: 1926c699258SBarry Smith + pc - the preconditioner context 1936c699258SBarry Smith - dm - the dm 1946c699258SBarry Smith 1956c699258SBarry Smith Level: intermediate 1966c699258SBarry Smith 1976c699258SBarry Smith 1986c699258SBarry Smith .seealso: PCGetDM(), KSPSetDM(), KSPGetDM() 1996c699258SBarry Smith @*/ 2007087cfbeSBarry Smith PetscErrorCode PCSetDM(PC pc,DM dm) 2016c699258SBarry Smith { 2026c699258SBarry Smith PetscErrorCode ierr; 2036c699258SBarry Smith 2046c699258SBarry Smith PetscFunctionBegin; 2050700a824SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 206d0660788SBarry Smith if (dm) {ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr);} 2076bf464f9SBarry Smith ierr = DMDestroy(&pc->dm);CHKERRQ(ierr); 2086c699258SBarry Smith pc->dm = dm; 2096c699258SBarry Smith PetscFunctionReturn(0); 2106c699258SBarry Smith } 2116c699258SBarry Smith 2126c699258SBarry Smith #undef __FUNCT__ 2136c699258SBarry Smith #define __FUNCT__ "PCGetDM" 2146c699258SBarry Smith /*@ 2156c699258SBarry Smith PCGetDM - Gets the DM that may be used by some preconditioners 2166c699258SBarry Smith 2173f9fe445SBarry Smith Not Collective 2186c699258SBarry Smith 2196c699258SBarry Smith Input Parameter: 2206c699258SBarry Smith . pc - the preconditioner context 2216c699258SBarry Smith 2226c699258SBarry Smith Output Parameter: 2236c699258SBarry Smith . dm - the dm 2246c699258SBarry Smith 2256c699258SBarry Smith Level: intermediate 2266c699258SBarry Smith 2276c699258SBarry Smith 2286c699258SBarry Smith .seealso: PCSetDM(), KSPSetDM(), KSPGetDM() 2296c699258SBarry Smith @*/ 2307087cfbeSBarry Smith PetscErrorCode PCGetDM(PC pc,DM *dm) 2316c699258SBarry Smith { 2326c699258SBarry Smith PetscFunctionBegin; 2330700a824SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 2346c699258SBarry Smith *dm = pc->dm; 2356c699258SBarry Smith PetscFunctionReturn(0); 2366c699258SBarry Smith } 2376c699258SBarry Smith 2381b2093e4SBarry Smith #undef __FUNCT__ 2391b2093e4SBarry Smith #define __FUNCT__ "PCSetApplicationContext" 240b07ff414SBarry Smith /*@ 2411b2093e4SBarry Smith PCSetApplicationContext - Sets the optional user-defined context for the linear solver. 2421b2093e4SBarry Smith 2431b2093e4SBarry Smith Logically Collective on PC 2441b2093e4SBarry Smith 2451b2093e4SBarry Smith Input Parameters: 2461b2093e4SBarry Smith + pc - the PC context 2471b2093e4SBarry Smith - usrP - optional user context 2481b2093e4SBarry Smith 2491b2093e4SBarry Smith Level: intermediate 2501b2093e4SBarry Smith 2511b2093e4SBarry Smith .keywords: PC, set, application, context 2521b2093e4SBarry Smith 2531b2093e4SBarry Smith .seealso: PCGetApplicationContext() 2541b2093e4SBarry Smith @*/ 2551b2093e4SBarry Smith PetscErrorCode PCSetApplicationContext(PC pc,void *usrP) 2561b2093e4SBarry Smith { 2571b2093e4SBarry Smith PetscFunctionBegin; 2581b2093e4SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 2591b2093e4SBarry Smith pc->user = usrP; 2601b2093e4SBarry Smith PetscFunctionReturn(0); 2611b2093e4SBarry Smith } 2621b2093e4SBarry Smith 2631b2093e4SBarry Smith #undef __FUNCT__ 2641b2093e4SBarry Smith #define __FUNCT__ "PCGetApplicationContext" 265b07ff414SBarry Smith /*@ 2661b2093e4SBarry Smith PCGetApplicationContext - Gets the user-defined context for the linear solver. 2671b2093e4SBarry Smith 2681b2093e4SBarry Smith Not Collective 2691b2093e4SBarry Smith 2701b2093e4SBarry Smith Input Parameter: 2711b2093e4SBarry Smith . pc - PC context 2721b2093e4SBarry Smith 2731b2093e4SBarry Smith Output Parameter: 2741b2093e4SBarry Smith . usrP - user context 2751b2093e4SBarry Smith 2761b2093e4SBarry Smith Level: intermediate 2771b2093e4SBarry Smith 2781b2093e4SBarry Smith .keywords: PC, get, application, context 2791b2093e4SBarry Smith 2801b2093e4SBarry Smith .seealso: PCSetApplicationContext() 2811b2093e4SBarry Smith @*/ 2821b2093e4SBarry Smith PetscErrorCode PCGetApplicationContext(PC pc,void *usrP) 2831b2093e4SBarry Smith { 2841b2093e4SBarry Smith PetscFunctionBegin; 2851b2093e4SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 2861b2093e4SBarry Smith *(void**)usrP = pc->user; 2871b2093e4SBarry Smith PetscFunctionReturn(0); 2881b2093e4SBarry Smith } 2891b2093e4SBarry Smith 290