17d0a6c19SBarry Smith 24b9ad928SBarry Smith /* 34b9ad928SBarry Smith Routines to set PC methods and options. 44b9ad928SBarry Smith */ 54b9ad928SBarry Smith 6af0996ceSBarry Smith #include <petsc/private/pcimpl.h> /*I "petscpc.h" I*/ 71e25c274SJed Brown #include <petscdm.h> 84b9ad928SBarry Smith 9ace3abfcSBarry Smith PetscBool PCRegisterAllCalled = PETSC_FALSE; 104b9ad928SBarry Smith /* 11f3b08a26SMatthew G. Knepley Contains the list of registered PC routines 124b9ad928SBarry Smith */ 130a545947SLisandro Dalcin PetscFunctionList PCList = NULL; 144b9ad928SBarry Smith 154b9ad928SBarry Smith /*@C 168f6c3df8SBarry Smith PCSetType - Builds PC for a particular preconditioner type 174b9ad928SBarry Smith 184b9ad928SBarry Smith Collective on PC 194b9ad928SBarry Smith 20d8d19677SJose E. Roman Input Parameters: 214b9ad928SBarry Smith + pc - the preconditioner context. 224b9ad928SBarry Smith - type - a known method 234b9ad928SBarry Smith 244b9ad928SBarry Smith Options Database Key: 254b9ad928SBarry Smith . -pc_type <type> - Sets PC type 264b9ad928SBarry Smith 274b9ad928SBarry Smith Use -help for a list of available methods (for instance, 284b9ad928SBarry Smith jacobi or bjacobi) 294b9ad928SBarry Smith 304b9ad928SBarry Smith Notes: 314b9ad928SBarry Smith See "petsc/include/petscpc.h" for available methods (for instance, 324b9ad928SBarry Smith PCJACOBI, PCILU, or PCBJACOBI). 334b9ad928SBarry Smith 344b9ad928SBarry Smith Normally, it is best to use the KSPSetFromOptions() command and 354b9ad928SBarry Smith then set the PC type from the options database rather than by using 364b9ad928SBarry Smith this routine. Using the options database provides the user with 374b9ad928SBarry Smith maximum flexibility in evaluating the many different preconditioners. 384b9ad928SBarry Smith The PCSetType() routine is provided for those situations where it 394b9ad928SBarry Smith is necessary to set the preconditioner independently of the command 404b9ad928SBarry Smith line or options database. This might be the case, for example, when 414b9ad928SBarry Smith the choice of preconditioner changes during the execution of the 424b9ad928SBarry Smith program, and the user's application is taking responsibility for 434b9ad928SBarry Smith choosing the appropriate preconditioner. In other words, this 444b9ad928SBarry Smith routine is not for beginners. 454b9ad928SBarry Smith 464b9ad928SBarry Smith Level: intermediate 474b9ad928SBarry Smith 488f6c3df8SBarry Smith Developer Note: PCRegister() is used to add preconditioner types to PCList from which they 498f6c3df8SBarry Smith are accessed by PCSetType(). 508f6c3df8SBarry Smith 518f6c3df8SBarry Smith .seealso: KSPSetType(), PCType, PCRegister(), PCCreate(), KSPGetPC() 524b9ad928SBarry Smith 534b9ad928SBarry Smith @*/ 5419fd82e9SBarry Smith PetscErrorCode PCSetType(PC pc,PCType type) 554b9ad928SBarry Smith { 56ace3abfcSBarry Smith PetscBool match; 575f80ce2aSJacob Faibussowitsch PetscErrorCode (*r)(PC); 584b9ad928SBarry Smith 594b9ad928SBarry Smith PetscFunctionBegin; 600700a824SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 614482741eSBarry Smith PetscValidCharPointer(type,2); 624b9ad928SBarry Smith 639566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)pc,type,&match)); 644b9ad928SBarry Smith if (match) PetscFunctionReturn(0); 654b9ad928SBarry Smith 669566063dSJacob Faibussowitsch PetscCall(PetscFunctionListFind(PCList,type,&r)); 675f80ce2aSJacob Faibussowitsch PetscCheck(r,PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_UNKNOWN_TYPE,"Unable to find requested PC type %s",type); 68a7d21a52SLisandro Dalcin /* Destroy the previous private PC context */ 69b5c23020SJed Brown if (pc->ops->destroy) { 709566063dSJacob Faibussowitsch PetscCall((*pc->ops->destroy)(pc)); 710298fd71SBarry Smith pc->ops->destroy = NULL; 720a545947SLisandro Dalcin pc->data = NULL; 73b5c23020SJed Brown } 749566063dSJacob Faibussowitsch PetscCall(PetscFunctionListDestroy(&((PetscObject)pc)->qlist)); 75a7d21a52SLisandro Dalcin /* Reinitialize function pointers in PCOps structure */ 769566063dSJacob Faibussowitsch PetscCall(PetscMemzero(pc->ops,sizeof(struct _PCOps))); 77a7d21a52SLisandro Dalcin /* XXX Is this OK?? */ 780a545947SLisandro Dalcin pc->modifysubmatrices = NULL; 790a545947SLisandro Dalcin pc->modifysubmatricesP = NULL; 80a7d21a52SLisandro Dalcin /* Call the PCCreate_XXX routine for this particular preconditioner */ 81a7d21a52SLisandro Dalcin pc->setupcalled = 0; 822fa5cd67SKarl Rupp 839566063dSJacob Faibussowitsch PetscCall(PetscObjectChangeTypeName((PetscObject)pc,type)); 849566063dSJacob Faibussowitsch PetscCall((*r)(pc)); 854b9ad928SBarry Smith PetscFunctionReturn(0); 864b9ad928SBarry Smith } 874b9ad928SBarry Smith 884b9ad928SBarry Smith /*@C 894b9ad928SBarry Smith PCGetType - Gets the PC method type and name (as a string) from the PC 904b9ad928SBarry Smith context. 914b9ad928SBarry Smith 924b9ad928SBarry Smith Not Collective 934b9ad928SBarry Smith 944b9ad928SBarry Smith Input Parameter: 954b9ad928SBarry Smith . pc - the preconditioner context 964b9ad928SBarry Smith 974b9ad928SBarry Smith Output Parameter: 98c4e43342SLisandro Dalcin . type - name of preconditioner method 994b9ad928SBarry Smith 1004b9ad928SBarry Smith Level: intermediate 1014b9ad928SBarry Smith 1024b9ad928SBarry Smith .seealso: PCSetType() 1034b9ad928SBarry Smith 1044b9ad928SBarry Smith @*/ 10519fd82e9SBarry Smith PetscErrorCode PCGetType(PC pc,PCType *type) 1064b9ad928SBarry Smith { 1074b9ad928SBarry Smith PetscFunctionBegin; 1080700a824SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 109c4e43342SLisandro Dalcin PetscValidPointer(type,2); 110c4e43342SLisandro Dalcin *type = ((PetscObject)pc)->type_name; 1114b9ad928SBarry Smith PetscFunctionReturn(0); 1124b9ad928SBarry Smith } 1134b9ad928SBarry Smith 11409573ac7SBarry Smith extern PetscErrorCode PCGetDefaultType_Private(PC,const char*[]); 115566e8bf2SBarry Smith 1164b9ad928SBarry Smith /*@ 1174b9ad928SBarry Smith PCSetFromOptions - Sets PC options from the options database. 1184b9ad928SBarry Smith This routine must be called before PCSetUp() if the user is to be 1194b9ad928SBarry Smith allowed to set the preconditioner method. 1204b9ad928SBarry Smith 1214b9ad928SBarry Smith Collective on PC 1224b9ad928SBarry Smith 1234b9ad928SBarry Smith Input Parameter: 1244b9ad928SBarry Smith . pc - the preconditioner context 1254b9ad928SBarry Smith 126b9ee023eSBarry Smith Options Database: 127147403d9SBarry Smith . -pc_use_amat true,false - see PCSetUseAmat() 128b9ee023eSBarry Smith 1294b9ad928SBarry Smith Level: developer 1304b9ad928SBarry Smith 131b9ee023eSBarry Smith .seealso: PCSetUseAmat() 1324b9ad928SBarry Smith 1334b9ad928SBarry Smith @*/ 1347087cfbeSBarry Smith PetscErrorCode PCSetFromOptions(PC pc) 1354b9ad928SBarry Smith { 1362fc52814SBarry Smith char type[256]; 1372fc52814SBarry Smith const char *def; 138ace3abfcSBarry Smith PetscBool flg; 1394b9ad928SBarry Smith 1404b9ad928SBarry Smith PetscFunctionBegin; 1410700a824SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 1424b9ad928SBarry Smith 1439566063dSJacob Faibussowitsch PetscCall(PCRegisterAll()); 144*d0609cedSBarry Smith PetscObjectOptionsBegin((PetscObject)pc); 1457adad957SLisandro Dalcin if (!((PetscObject)pc)->type_name) { 1469566063dSJacob Faibussowitsch PetscCall(PCGetDefaultType_Private(pc,&def)); 1474b9ad928SBarry Smith } else { 1487adad957SLisandro Dalcin def = ((PetscObject)pc)->type_name; 1494b9ad928SBarry Smith } 1504b9ad928SBarry Smith 1519566063dSJacob Faibussowitsch PetscCall(PetscOptionsFList("-pc_type","Preconditioner","PCSetType",PCList,def,type,256,&flg)); 1524b9ad928SBarry Smith if (flg) { 1539566063dSJacob Faibussowitsch PetscCall(PCSetType(pc,type)); 1547adad957SLisandro Dalcin } else if (!((PetscObject)pc)->type_name) { 1559566063dSJacob Faibussowitsch PetscCall(PCSetType(pc,def)); 1564b9ad928SBarry Smith } 1574b9ad928SBarry Smith 1589566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)pc,PCNONE,&flg)); 15925c41539SBarry Smith if (flg) goto skipoptions; 16025c41539SBarry Smith 1619566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-pc_use_amat","use Amat (instead of Pmat) to define preconditioner in nested inner solves","PCSetUseAmat",pc->useAmat,&pc->useAmat,NULL)); 162b4813acdSShri Abhyankar 1634b9ad928SBarry Smith if (pc->ops->setfromoptions) { 1649566063dSJacob Faibussowitsch PetscCall((*pc->ops->setfromoptions)(PetscOptionsObject,pc)); 1654b9ad928SBarry Smith } 1665d973c19SBarry Smith 16725c41539SBarry Smith skipoptions: 1685d973c19SBarry Smith /* process any options handlers added with PetscObjectAddOptionsHandler() */ 1699566063dSJacob Faibussowitsch PetscCall(PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject)pc)); 170*d0609cedSBarry Smith PetscOptionsEnd(); 1710c24e6a1SHong Zhang pc->setfromoptionscalled++; 1724b9ad928SBarry Smith PetscFunctionReturn(0); 1734b9ad928SBarry Smith } 1746c699258SBarry Smith 1756c699258SBarry Smith /*@ 1766c699258SBarry Smith PCSetDM - Sets the DM that may be used by some preconditioners 1776c699258SBarry Smith 1783f9fe445SBarry Smith Logically Collective on PC 1796c699258SBarry Smith 1806c699258SBarry Smith Input Parameters: 1816c699258SBarry Smith + pc - the preconditioner context 1822a808120SBarry Smith - dm - the dm, can be NULL 1836c699258SBarry Smith 1846c699258SBarry Smith Level: intermediate 1856c699258SBarry Smith 18695452b02SPatrick Sanan Developer Notes: 18795452b02SPatrick Sanan The routines KSP/SNES/TSSetDM() require the dm to be non-NULL, but this one can be NULL since all it does is 1882a808120SBarry Smith replace the current DM 1896c699258SBarry Smith 1906c699258SBarry Smith .seealso: PCGetDM(), KSPSetDM(), KSPGetDM() 1916c699258SBarry Smith @*/ 1927087cfbeSBarry Smith PetscErrorCode PCSetDM(PC pc,DM dm) 1936c699258SBarry Smith { 1946c699258SBarry Smith PetscFunctionBegin; 1950700a824SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 1969566063dSJacob Faibussowitsch if (dm) PetscCall(PetscObjectReference((PetscObject)dm)); 1979566063dSJacob Faibussowitsch PetscCall(DMDestroy(&pc->dm)); 1986c699258SBarry Smith pc->dm = dm; 1996c699258SBarry Smith PetscFunctionReturn(0); 2006c699258SBarry Smith } 2016c699258SBarry Smith 2026c699258SBarry Smith /*@ 2036c699258SBarry Smith PCGetDM - Gets the DM that may be used by some preconditioners 2046c699258SBarry Smith 2053f9fe445SBarry Smith Not Collective 2066c699258SBarry Smith 2076c699258SBarry Smith Input Parameter: 2086c699258SBarry Smith . pc - the preconditioner context 2096c699258SBarry Smith 2106c699258SBarry Smith Output Parameter: 2116c699258SBarry Smith . dm - the dm 2126c699258SBarry Smith 2136c699258SBarry Smith Level: intermediate 2146c699258SBarry Smith 2156c699258SBarry Smith .seealso: PCSetDM(), KSPSetDM(), KSPGetDM() 2166c699258SBarry Smith @*/ 2177087cfbeSBarry Smith PetscErrorCode PCGetDM(PC pc,DM *dm) 2186c699258SBarry Smith { 2196c699258SBarry Smith PetscFunctionBegin; 2200700a824SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 2216c699258SBarry Smith *dm = pc->dm; 2226c699258SBarry Smith PetscFunctionReturn(0); 2236c699258SBarry Smith } 2246c699258SBarry Smith 225b07ff414SBarry Smith /*@ 2261b2093e4SBarry Smith PCSetApplicationContext - Sets the optional user-defined context for the linear solver. 2271b2093e4SBarry Smith 2281b2093e4SBarry Smith Logically Collective on PC 2291b2093e4SBarry Smith 2301b2093e4SBarry Smith Input Parameters: 2311b2093e4SBarry Smith + pc - the PC context 2321b2093e4SBarry Smith - usrP - optional user context 2331b2093e4SBarry Smith 2341b2093e4SBarry Smith Level: intermediate 2351b2093e4SBarry Smith 2361b2093e4SBarry Smith .seealso: PCGetApplicationContext() 2371b2093e4SBarry Smith @*/ 2381b2093e4SBarry Smith PetscErrorCode PCSetApplicationContext(PC pc,void *usrP) 2391b2093e4SBarry Smith { 2401b2093e4SBarry Smith PetscFunctionBegin; 2411b2093e4SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 2421b2093e4SBarry Smith pc->user = usrP; 2431b2093e4SBarry Smith PetscFunctionReturn(0); 2441b2093e4SBarry Smith } 2451b2093e4SBarry Smith 246b07ff414SBarry Smith /*@ 2471b2093e4SBarry Smith PCGetApplicationContext - Gets the user-defined context for the linear solver. 2481b2093e4SBarry Smith 2491b2093e4SBarry Smith Not Collective 2501b2093e4SBarry Smith 2511b2093e4SBarry Smith Input Parameter: 2521b2093e4SBarry Smith . pc - PC context 2531b2093e4SBarry Smith 2541b2093e4SBarry Smith Output Parameter: 2551b2093e4SBarry Smith . usrP - user context 2561b2093e4SBarry Smith 2571b2093e4SBarry Smith Level: intermediate 2581b2093e4SBarry Smith 2591b2093e4SBarry Smith .seealso: PCSetApplicationContext() 2601b2093e4SBarry Smith @*/ 2611b2093e4SBarry Smith PetscErrorCode PCGetApplicationContext(PC pc,void *usrP) 2621b2093e4SBarry Smith { 2631b2093e4SBarry Smith PetscFunctionBegin; 2641b2093e4SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 2651b2093e4SBarry Smith *(void**)usrP = pc->user; 2661b2093e4SBarry Smith PetscFunctionReturn(0); 2671b2093e4SBarry Smith } 268