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; 57*5f80ce2aSJacob Faibussowitsch PetscErrorCode (*r)(PC); 584b9ad928SBarry Smith 594b9ad928SBarry Smith PetscFunctionBegin; 600700a824SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 614482741eSBarry Smith PetscValidCharPointer(type,2); 624b9ad928SBarry Smith 63*5f80ce2aSJacob Faibussowitsch CHKERRQ(PetscObjectTypeCompare((PetscObject)pc,type,&match)); 644b9ad928SBarry Smith if (match) PetscFunctionReturn(0); 654b9ad928SBarry Smith 66*5f80ce2aSJacob Faibussowitsch CHKERRQ(PetscFunctionListFind(PCList,type,&r)); 67*5f80ce2aSJacob 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) { 70*5f80ce2aSJacob Faibussowitsch CHKERRQ((*pc->ops->destroy)(pc)); 710298fd71SBarry Smith pc->ops->destroy = NULL; 720a545947SLisandro Dalcin pc->data = NULL; 73b5c23020SJed Brown } 74*5f80ce2aSJacob Faibussowitsch CHKERRQ(PetscFunctionListDestroy(&((PetscObject)pc)->qlist)); 75a7d21a52SLisandro Dalcin /* Reinitialize function pointers in PCOps structure */ 76*5f80ce2aSJacob Faibussowitsch CHKERRQ(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 83*5f80ce2aSJacob Faibussowitsch CHKERRQ(PetscObjectChangeTypeName((PetscObject)pc,type)); 84*5f80ce2aSJacob Faibussowitsch CHKERRQ((*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 { 136dfbe8321SBarry Smith PetscErrorCode ierr; 1372fc52814SBarry Smith char type[256]; 1382fc52814SBarry Smith const char *def; 139ace3abfcSBarry Smith PetscBool flg; 1404b9ad928SBarry Smith 1414b9ad928SBarry Smith PetscFunctionBegin; 1420700a824SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 1434b9ad928SBarry Smith 144*5f80ce2aSJacob Faibussowitsch CHKERRQ(PCRegisterAll()); 1453194b578SJed Brown ierr = PetscObjectOptionsBegin((PetscObject)pc);CHKERRQ(ierr); 1467adad957SLisandro Dalcin if (!((PetscObject)pc)->type_name) { 147*5f80ce2aSJacob Faibussowitsch CHKERRQ(PCGetDefaultType_Private(pc,&def)); 1484b9ad928SBarry Smith } else { 1497adad957SLisandro Dalcin def = ((PetscObject)pc)->type_name; 1504b9ad928SBarry Smith } 1514b9ad928SBarry Smith 152*5f80ce2aSJacob Faibussowitsch CHKERRQ(PetscOptionsFList("-pc_type","Preconditioner","PCSetType",PCList,def,type,256,&flg)); 1534b9ad928SBarry Smith if (flg) { 154*5f80ce2aSJacob Faibussowitsch CHKERRQ(PCSetType(pc,type)); 1557adad957SLisandro Dalcin } else if (!((PetscObject)pc)->type_name) { 156*5f80ce2aSJacob Faibussowitsch CHKERRQ(PCSetType(pc,def)); 1574b9ad928SBarry Smith } 1584b9ad928SBarry Smith 159*5f80ce2aSJacob Faibussowitsch CHKERRQ(PetscObjectTypeCompare((PetscObject)pc,PCNONE,&flg)); 16025c41539SBarry Smith if (flg) goto skipoptions; 16125c41539SBarry Smith 162*5f80ce2aSJacob Faibussowitsch CHKERRQ(PetscOptionsBool("-pc_use_amat","use Amat (instead of Pmat) to define preconditioner in nested inner solves","PCSetUseAmat",pc->useAmat,&pc->useAmat,NULL)); 163b4813acdSShri Abhyankar 1644b9ad928SBarry Smith if (pc->ops->setfromoptions) { 165*5f80ce2aSJacob Faibussowitsch CHKERRQ((*pc->ops->setfromoptions)(PetscOptionsObject,pc)); 1664b9ad928SBarry Smith } 1675d973c19SBarry Smith 16825c41539SBarry Smith skipoptions: 1695d973c19SBarry Smith /* process any options handlers added with PetscObjectAddOptionsHandler() */ 170*5f80ce2aSJacob Faibussowitsch CHKERRQ(PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject)pc)); 1714b9ad928SBarry Smith ierr = PetscOptionsEnd();CHKERRQ(ierr); 1720c24e6a1SHong Zhang pc->setfromoptionscalled++; 1734b9ad928SBarry Smith PetscFunctionReturn(0); 1744b9ad928SBarry Smith } 1756c699258SBarry Smith 1766c699258SBarry Smith /*@ 1776c699258SBarry Smith PCSetDM - Sets the DM that may be used by some preconditioners 1786c699258SBarry Smith 1793f9fe445SBarry Smith Logically Collective on PC 1806c699258SBarry Smith 1816c699258SBarry Smith Input Parameters: 1826c699258SBarry Smith + pc - the preconditioner context 1832a808120SBarry Smith - dm - the dm, can be NULL 1846c699258SBarry Smith 1856c699258SBarry Smith Level: intermediate 1866c699258SBarry Smith 18795452b02SPatrick Sanan Developer Notes: 18895452b02SPatrick Sanan The routines KSP/SNES/TSSetDM() require the dm to be non-NULL, but this one can be NULL since all it does is 1892a808120SBarry Smith replace the current DM 1906c699258SBarry Smith 1916c699258SBarry Smith .seealso: PCGetDM(), KSPSetDM(), KSPGetDM() 1926c699258SBarry Smith @*/ 1937087cfbeSBarry Smith PetscErrorCode PCSetDM(PC pc,DM dm) 1946c699258SBarry Smith { 1956c699258SBarry Smith PetscFunctionBegin; 1960700a824SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 197*5f80ce2aSJacob Faibussowitsch if (dm) CHKERRQ(PetscObjectReference((PetscObject)dm)); 198*5f80ce2aSJacob Faibussowitsch CHKERRQ(DMDestroy(&pc->dm)); 1996c699258SBarry Smith pc->dm = dm; 2006c699258SBarry Smith PetscFunctionReturn(0); 2016c699258SBarry Smith } 2026c699258SBarry Smith 2036c699258SBarry Smith /*@ 2046c699258SBarry Smith PCGetDM - Gets the DM that may be used by some preconditioners 2056c699258SBarry Smith 2063f9fe445SBarry Smith Not Collective 2076c699258SBarry Smith 2086c699258SBarry Smith Input Parameter: 2096c699258SBarry Smith . pc - the preconditioner context 2106c699258SBarry Smith 2116c699258SBarry Smith Output Parameter: 2126c699258SBarry Smith . dm - the dm 2136c699258SBarry Smith 2146c699258SBarry Smith Level: intermediate 2156c699258SBarry Smith 2166c699258SBarry Smith .seealso: PCSetDM(), KSPSetDM(), KSPGetDM() 2176c699258SBarry Smith @*/ 2187087cfbeSBarry Smith PetscErrorCode PCGetDM(PC pc,DM *dm) 2196c699258SBarry Smith { 2206c699258SBarry Smith PetscFunctionBegin; 2210700a824SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 2226c699258SBarry Smith *dm = pc->dm; 2236c699258SBarry Smith PetscFunctionReturn(0); 2246c699258SBarry Smith } 2256c699258SBarry Smith 226b07ff414SBarry Smith /*@ 2271b2093e4SBarry Smith PCSetApplicationContext - Sets the optional user-defined context for the linear solver. 2281b2093e4SBarry Smith 2291b2093e4SBarry Smith Logically Collective on PC 2301b2093e4SBarry Smith 2311b2093e4SBarry Smith Input Parameters: 2321b2093e4SBarry Smith + pc - the PC context 2331b2093e4SBarry Smith - usrP - optional user context 2341b2093e4SBarry Smith 2351b2093e4SBarry Smith Level: intermediate 2361b2093e4SBarry Smith 2371b2093e4SBarry Smith .seealso: PCGetApplicationContext() 2381b2093e4SBarry Smith @*/ 2391b2093e4SBarry Smith PetscErrorCode PCSetApplicationContext(PC pc,void *usrP) 2401b2093e4SBarry Smith { 2411b2093e4SBarry Smith PetscFunctionBegin; 2421b2093e4SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 2431b2093e4SBarry Smith pc->user = usrP; 2441b2093e4SBarry Smith PetscFunctionReturn(0); 2451b2093e4SBarry Smith } 2461b2093e4SBarry Smith 247b07ff414SBarry Smith /*@ 2481b2093e4SBarry Smith PCGetApplicationContext - Gets the user-defined context for the linear solver. 2491b2093e4SBarry Smith 2501b2093e4SBarry Smith Not Collective 2511b2093e4SBarry Smith 2521b2093e4SBarry Smith Input Parameter: 2531b2093e4SBarry Smith . pc - PC context 2541b2093e4SBarry Smith 2551b2093e4SBarry Smith Output Parameter: 2561b2093e4SBarry Smith . usrP - user context 2571b2093e4SBarry Smith 2581b2093e4SBarry Smith Level: intermediate 2591b2093e4SBarry Smith 2601b2093e4SBarry Smith .seealso: PCSetApplicationContext() 2611b2093e4SBarry Smith @*/ 2621b2093e4SBarry Smith PetscErrorCode PCGetApplicationContext(PC pc,void *usrP) 2631b2093e4SBarry Smith { 2641b2093e4SBarry Smith PetscFunctionBegin; 2651b2093e4SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 2661b2093e4SBarry Smith *(void**)usrP = pc->user; 2671b2093e4SBarry Smith PetscFunctionReturn(0); 2681b2093e4SBarry Smith } 269