xref: /petsc/src/ksp/pc/interface/pcset.c (revision b45d2f2cb7e031d9c0de5873eca80614ca7b863b)
17d0a6c19SBarry Smith 
24b9ad928SBarry Smith /*
34b9ad928SBarry Smith     Routines to set PC methods and options.
44b9ad928SBarry Smith */
54b9ad928SBarry Smith 
6*b45d2f2cSJed Brown #include <petsc-private/pcimpl.h>      /*I "petscpc.h" I*/
74b9ad928SBarry Smith 
8ace3abfcSBarry Smith PetscBool  PCRegisterAllCalled = PETSC_FALSE;
94b9ad928SBarry Smith /*
104b9ad928SBarry Smith    Contains the list of registered KSP routines
114b9ad928SBarry Smith */
124b9ad928SBarry Smith PetscFList PCList = 0;
134b9ad928SBarry Smith 
144b9ad928SBarry Smith #undef __FUNCT__
154b9ad928SBarry Smith #define __FUNCT__ "PCSetType"
164b9ad928SBarry Smith /*@C
174b9ad928SBarry Smith    PCSetType - Builds PC for a particular preconditioner.
184b9ad928SBarry Smith 
194b9ad928SBarry Smith    Collective on PC
204b9ad928SBarry Smith 
214b9ad928SBarry Smith    Input Parameter:
224b9ad928SBarry Smith +  pc - the preconditioner context.
234b9ad928SBarry Smith -  type - a known method
244b9ad928SBarry Smith 
254b9ad928SBarry Smith    Options Database Key:
264b9ad928SBarry Smith .  -pc_type <type> - Sets PC type
274b9ad928SBarry Smith 
284b9ad928SBarry Smith    Use -help for a list of available methods (for instance,
294b9ad928SBarry Smith    jacobi or bjacobi)
304b9ad928SBarry Smith 
314b9ad928SBarry Smith   Notes:
324b9ad928SBarry Smith   See "petsc/include/petscpc.h" for available methods (for instance,
334b9ad928SBarry Smith   PCJACOBI, PCILU, or PCBJACOBI).
344b9ad928SBarry Smith 
354b9ad928SBarry Smith   Normally, it is best to use the KSPSetFromOptions() command and
364b9ad928SBarry Smith   then set the PC type from the options database rather than by using
374b9ad928SBarry Smith   this routine.  Using the options database provides the user with
384b9ad928SBarry Smith   maximum flexibility in evaluating the many different preconditioners.
394b9ad928SBarry Smith   The PCSetType() routine is provided for those situations where it
404b9ad928SBarry Smith   is necessary to set the preconditioner independently of the command
414b9ad928SBarry Smith   line or options database.  This might be the case, for example, when
424b9ad928SBarry Smith   the choice of preconditioner changes during the execution of the
434b9ad928SBarry Smith   program, and the user's application is taking responsibility for
444b9ad928SBarry Smith   choosing the appropriate preconditioner.  In other words, this
454b9ad928SBarry Smith   routine is not for beginners.
464b9ad928SBarry Smith 
474b9ad928SBarry Smith   Level: intermediate
484b9ad928SBarry Smith 
494b9ad928SBarry Smith .keywords: PC, set, method, type
504b9ad928SBarry Smith 
514b9ad928SBarry Smith .seealso: KSPSetType(), PCType
524b9ad928SBarry Smith 
534b9ad928SBarry Smith @*/
547087cfbeSBarry Smith PetscErrorCode  PCSetType(PC pc,const PCType type)
554b9ad928SBarry Smith {
56dfbe8321SBarry Smith   PetscErrorCode ierr,(*r)(PC);
57ace3abfcSBarry Smith   PetscBool      match;
584b9ad928SBarry Smith 
594b9ad928SBarry Smith   PetscFunctionBegin;
600700a824SBarry Smith   PetscValidHeaderSpecific(pc,PC_CLASSID,1);
614482741eSBarry Smith   PetscValidCharPointer(type,2);
624b9ad928SBarry Smith 
634b9ad928SBarry Smith   ierr = PetscTypeCompare((PetscObject)pc,type,&match);CHKERRQ(ierr);
644b9ad928SBarry Smith   if (match) PetscFunctionReturn(0);
654b9ad928SBarry Smith 
664b91b6eaSBarry Smith   ierr =  PetscFListFind(PCList,((PetscObject)pc)->comm,type,PETSC_TRUE,(void (**)(void)) &r);CHKERRQ(ierr);
6765e19b50SBarry Smith   if (!r) SETERRQ1(((PetscObject)pc)->comm,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) {
70b5c23020SJed Brown     ierr =  (*pc->ops->destroy)(pc);CHKERRQ(ierr);
71b5c23020SJed Brown     pc->ops->destroy = PETSC_NULL;
72b5c23020SJed Brown     pc->data = 0;
73b5c23020SJed Brown   }
747adad957SLisandro Dalcin   ierr = PetscFListDestroy(&((PetscObject)pc)->qlist);CHKERRQ(ierr);
75a7d21a52SLisandro Dalcin   /* Reinitialize function pointers in PCOps structure */
76a7d21a52SLisandro Dalcin   ierr = PetscMemzero(pc->ops,sizeof(struct _PCOps));CHKERRQ(ierr);
77a7d21a52SLisandro Dalcin   /* XXX Is this OK?? */
78a7d21a52SLisandro Dalcin   pc->modifysubmatrices        = 0;
79a7d21a52SLisandro Dalcin   pc->modifysubmatricesP       = 0;
80a7d21a52SLisandro Dalcin   /* Call the PCCreate_XXX routine for this particular preconditioner */
81a7d21a52SLisandro Dalcin   pc->setupcalled = 0;
824b9ad928SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)pc,type);CHKERRQ(ierr);
8303bfa161SLisandro Dalcin   ierr = (*r)(pc);CHKERRQ(ierr);
849fb22e1aSBarry Smith #if defined(PETSC_HAVE_AMS)
859fb22e1aSBarry Smith   if (PetscAMSPublishAll) {
869fb22e1aSBarry Smith     ierr = PetscObjectAMSPublish((PetscObject)pc);CHKERRQ(ierr);
879fb22e1aSBarry Smith   }
889fb22e1aSBarry Smith #endif
894b9ad928SBarry Smith   PetscFunctionReturn(0);
904b9ad928SBarry Smith }
914b9ad928SBarry Smith 
924b9ad928SBarry Smith #undef __FUNCT__
934b9ad928SBarry Smith #define __FUNCT__ "PCRegisterDestroy"
94f39d8e23SSatish Balay /*@
954b9ad928SBarry Smith    PCRegisterDestroy - Frees the list of preconditioners that were
964b9ad928SBarry Smith    registered by PCRegisterDynamic().
974b9ad928SBarry Smith 
984b9ad928SBarry Smith    Not Collective
994b9ad928SBarry Smith 
1004b9ad928SBarry Smith    Level: advanced
1014b9ad928SBarry Smith 
1024b9ad928SBarry Smith .keywords: PC, register, destroy
1034b9ad928SBarry Smith 
1044b9ad928SBarry Smith .seealso: PCRegisterAll(), PCRegisterAll()
1054b9ad928SBarry Smith 
1064b9ad928SBarry Smith @*/
1077087cfbeSBarry Smith PetscErrorCode  PCRegisterDestroy(void)
1084b9ad928SBarry Smith {
109dfbe8321SBarry Smith   PetscErrorCode ierr;
1104b9ad928SBarry Smith 
1114b9ad928SBarry Smith   PetscFunctionBegin;
1121441b1d3SBarry Smith   ierr = PetscFListDestroy(&PCList);CHKERRQ(ierr);
1134b9ad928SBarry Smith   PCRegisterAllCalled = PETSC_FALSE;
1144b9ad928SBarry Smith   PetscFunctionReturn(0);
1154b9ad928SBarry Smith }
1164b9ad928SBarry Smith 
1174b9ad928SBarry Smith #undef __FUNCT__
1184b9ad928SBarry Smith #define __FUNCT__ "PCGetType"
1194b9ad928SBarry Smith /*@C
1204b9ad928SBarry Smith    PCGetType - Gets the PC method type and name (as a string) from the PC
1214b9ad928SBarry Smith    context.
1224b9ad928SBarry Smith 
1234b9ad928SBarry Smith    Not Collective
1244b9ad928SBarry Smith 
1254b9ad928SBarry Smith    Input Parameter:
1264b9ad928SBarry Smith .  pc - the preconditioner context
1274b9ad928SBarry Smith 
1284b9ad928SBarry Smith    Output Parameter:
129c4e43342SLisandro Dalcin .  type - name of preconditioner method
1304b9ad928SBarry Smith 
1314b9ad928SBarry Smith    Level: intermediate
1324b9ad928SBarry Smith 
1334b9ad928SBarry Smith .keywords: PC, get, method, name, type
1344b9ad928SBarry Smith 
1354b9ad928SBarry Smith .seealso: PCSetType()
1364b9ad928SBarry Smith 
1374b9ad928SBarry Smith @*/
1387087cfbeSBarry Smith PetscErrorCode  PCGetType(PC pc,const PCType *type)
1394b9ad928SBarry Smith {
1404b9ad928SBarry Smith   PetscFunctionBegin;
1410700a824SBarry Smith   PetscValidHeaderSpecific(pc,PC_CLASSID,1);
142c4e43342SLisandro Dalcin   PetscValidPointer(type,2);
143c4e43342SLisandro Dalcin   *type = ((PetscObject)pc)->type_name;
1444b9ad928SBarry Smith   PetscFunctionReturn(0);
1454b9ad928SBarry Smith }
1464b9ad928SBarry Smith 
14709573ac7SBarry Smith extern PetscErrorCode PCGetDefaultType_Private(PC,const char*[]);
148566e8bf2SBarry Smith 
1494b9ad928SBarry Smith #undef __FUNCT__
1504b9ad928SBarry Smith #define __FUNCT__ "PCSetFromOptions"
1514b9ad928SBarry Smith /*@
1524b9ad928SBarry Smith    PCSetFromOptions - Sets PC options from the options database.
1534b9ad928SBarry Smith    This routine must be called before PCSetUp() if the user is to be
1544b9ad928SBarry Smith    allowed to set the preconditioner method.
1554b9ad928SBarry Smith 
1564b9ad928SBarry Smith    Collective on PC
1574b9ad928SBarry Smith 
1584b9ad928SBarry Smith    Input Parameter:
1594b9ad928SBarry Smith .  pc - the preconditioner context
1604b9ad928SBarry Smith 
1614b9ad928SBarry Smith    Level: developer
1624b9ad928SBarry Smith 
1634b9ad928SBarry Smith .keywords: PC, set, from, options, database
1644b9ad928SBarry Smith 
1654b9ad928SBarry Smith .seealso:
1664b9ad928SBarry Smith 
1674b9ad928SBarry Smith @*/
1687087cfbeSBarry Smith PetscErrorCode  PCSetFromOptions(PC pc)
1694b9ad928SBarry Smith {
170dfbe8321SBarry Smith   PetscErrorCode ierr;
1712fc52814SBarry Smith   char           type[256];
1722fc52814SBarry Smith   const char     *def;
173ace3abfcSBarry Smith   PetscBool      flg;
1744b9ad928SBarry Smith 
1754b9ad928SBarry Smith   PetscFunctionBegin;
1760700a824SBarry Smith   PetscValidHeaderSpecific(pc,PC_CLASSID,1);
1774b9ad928SBarry Smith 
1784b9ad928SBarry Smith   if (!PCRegisterAllCalled) {ierr = PCRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
1793194b578SJed Brown   ierr = PetscObjectOptionsBegin((PetscObject)pc);CHKERRQ(ierr);
1807adad957SLisandro Dalcin   if (!((PetscObject)pc)->type_name) {
181566e8bf2SBarry Smith     ierr = PCGetDefaultType_Private(pc,&def);CHKERRQ(ierr);
1824b9ad928SBarry Smith   } else {
1837adad957SLisandro Dalcin     def = ((PetscObject)pc)->type_name;
1844b9ad928SBarry Smith   }
1854b9ad928SBarry Smith 
1864b9ad928SBarry Smith   ierr = PetscOptionsList("-pc_type","Preconditioner","PCSetType",PCList,def,type,256,&flg);CHKERRQ(ierr);
1874b9ad928SBarry Smith   if (flg) {
1884b9ad928SBarry Smith     ierr = PCSetType(pc,type);CHKERRQ(ierr);
1897adad957SLisandro Dalcin   } else if (!((PetscObject)pc)->type_name){
190566e8bf2SBarry Smith     ierr = PCSetType(pc,def);CHKERRQ(ierr);
1914b9ad928SBarry Smith   }
1924b9ad928SBarry Smith 
193dcc718f4SShri Abhyankar   ierr = PetscOptionsGetInt(((PetscObject)pc)->prefix,"-pc_reuse",&pc->reuse,PETSC_NULL);CHKERRQ(ierr);
194b4813acdSShri Abhyankar 
1954b9ad928SBarry Smith   if (pc->ops->setfromoptions) {
1964b9ad928SBarry Smith     ierr = (*pc->ops->setfromoptions)(pc);CHKERRQ(ierr);
1974b9ad928SBarry Smith   }
1985d973c19SBarry Smith 
1995d973c19SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
2005d973c19SBarry Smith   ierr = PetscObjectProcessOptionsHandlers((PetscObject)pc);CHKERRQ(ierr);
2014b9ad928SBarry Smith   ierr = PetscOptionsEnd();CHKERRQ(ierr);
2020c24e6a1SHong Zhang   pc->setfromoptionscalled++;
2034b9ad928SBarry Smith   PetscFunctionReturn(0);
2044b9ad928SBarry Smith }
2056c699258SBarry Smith 
2066c699258SBarry Smith #undef __FUNCT__
2076c699258SBarry Smith #define __FUNCT__ "PCSetDM"
2086c699258SBarry Smith /*@
2096c699258SBarry Smith    PCSetDM - Sets the DM that may be used by some preconditioners
2106c699258SBarry Smith 
2113f9fe445SBarry Smith    Logically Collective on PC
2126c699258SBarry Smith 
2136c699258SBarry Smith    Input Parameters:
2146c699258SBarry Smith +  pc - the preconditioner context
2156c699258SBarry Smith -  dm - the dm
2166c699258SBarry Smith 
2176c699258SBarry Smith    Level: intermediate
2186c699258SBarry Smith 
2196c699258SBarry Smith 
2206c699258SBarry Smith .seealso: PCGetDM(), KSPSetDM(), KSPGetDM()
2216c699258SBarry Smith @*/
2227087cfbeSBarry Smith PetscErrorCode  PCSetDM(PC pc,DM dm)
2236c699258SBarry Smith {
2246c699258SBarry Smith   PetscErrorCode ierr;
2256c699258SBarry Smith 
2266c699258SBarry Smith   PetscFunctionBegin;
2270700a824SBarry Smith   PetscValidHeaderSpecific(pc,PC_CLASSID,1);
228d0660788SBarry Smith   if (dm) {ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr);}
2296bf464f9SBarry Smith   ierr = DMDestroy(&pc->dm);CHKERRQ(ierr);
2306c699258SBarry Smith   pc->dm = dm;
2316c699258SBarry Smith   PetscFunctionReturn(0);
2326c699258SBarry Smith }
2336c699258SBarry Smith 
2346c699258SBarry Smith #undef __FUNCT__
2356c699258SBarry Smith #define __FUNCT__ "PCGetDM"
2366c699258SBarry Smith /*@
2376c699258SBarry Smith    PCGetDM - Gets the DM that may be used by some preconditioners
2386c699258SBarry Smith 
2393f9fe445SBarry Smith    Not Collective
2406c699258SBarry Smith 
2416c699258SBarry Smith    Input Parameter:
2426c699258SBarry Smith . pc - the preconditioner context
2436c699258SBarry Smith 
2446c699258SBarry Smith    Output Parameter:
2456c699258SBarry Smith .  dm - the dm
2466c699258SBarry Smith 
2476c699258SBarry Smith    Level: intermediate
2486c699258SBarry Smith 
2496c699258SBarry Smith 
2506c699258SBarry Smith .seealso: PCSetDM(), KSPSetDM(), KSPGetDM()
2516c699258SBarry Smith @*/
2527087cfbeSBarry Smith PetscErrorCode  PCGetDM(PC pc,DM *dm)
2536c699258SBarry Smith {
2546c699258SBarry Smith   PetscFunctionBegin;
2550700a824SBarry Smith   PetscValidHeaderSpecific(pc,PC_CLASSID,1);
2566c699258SBarry Smith   *dm = pc->dm;
2576c699258SBarry Smith   PetscFunctionReturn(0);
2586c699258SBarry Smith }
2596c699258SBarry Smith 
2601b2093e4SBarry Smith #undef __FUNCT__
2611b2093e4SBarry Smith #define __FUNCT__ "PCSetApplicationContext"
262b07ff414SBarry Smith /*@
2631b2093e4SBarry Smith    PCSetApplicationContext - Sets the optional user-defined context for the linear solver.
2641b2093e4SBarry Smith 
2651b2093e4SBarry Smith    Logically Collective on PC
2661b2093e4SBarry Smith 
2671b2093e4SBarry Smith    Input Parameters:
2681b2093e4SBarry Smith +  pc - the PC context
2691b2093e4SBarry Smith -  usrP - optional user context
2701b2093e4SBarry Smith 
2711b2093e4SBarry Smith    Level: intermediate
2721b2093e4SBarry Smith 
2731b2093e4SBarry Smith .keywords: PC, set, application, context
2741b2093e4SBarry Smith 
2751b2093e4SBarry Smith .seealso: PCGetApplicationContext()
2761b2093e4SBarry Smith @*/
2771b2093e4SBarry Smith PetscErrorCode  PCSetApplicationContext(PC pc,void *usrP)
2781b2093e4SBarry Smith {
2791b2093e4SBarry Smith   PetscFunctionBegin;
2801b2093e4SBarry Smith   PetscValidHeaderSpecific(pc,PC_CLASSID,1);
2811b2093e4SBarry Smith   pc->user = usrP;
2821b2093e4SBarry Smith   PetscFunctionReturn(0);
2831b2093e4SBarry Smith }
2841b2093e4SBarry Smith 
2851b2093e4SBarry Smith #undef __FUNCT__
2861b2093e4SBarry Smith #define __FUNCT__ "PCGetApplicationContext"
287b07ff414SBarry Smith /*@
2881b2093e4SBarry Smith    PCGetApplicationContext - Gets the user-defined context for the linear solver.
2891b2093e4SBarry Smith 
2901b2093e4SBarry Smith    Not Collective
2911b2093e4SBarry Smith 
2921b2093e4SBarry Smith    Input Parameter:
2931b2093e4SBarry Smith .  pc - PC context
2941b2093e4SBarry Smith 
2951b2093e4SBarry Smith    Output Parameter:
2961b2093e4SBarry Smith .  usrP - user context
2971b2093e4SBarry Smith 
2981b2093e4SBarry Smith    Level: intermediate
2991b2093e4SBarry Smith 
3001b2093e4SBarry Smith .keywords: PC, get, application, context
3011b2093e4SBarry Smith 
3021b2093e4SBarry Smith .seealso: PCSetApplicationContext()
3031b2093e4SBarry Smith @*/
3041b2093e4SBarry Smith PetscErrorCode  PCGetApplicationContext(PC pc,void *usrP)
3051b2093e4SBarry Smith {
3061b2093e4SBarry Smith   PetscFunctionBegin;
3071b2093e4SBarry Smith   PetscValidHeaderSpecific(pc,PC_CLASSID,1);
3081b2093e4SBarry Smith   *(void**)usrP = pc->user;
3091b2093e4SBarry Smith   PetscFunctionReturn(0);
3101b2093e4SBarry Smith }
3111b2093e4SBarry Smith 
312