[petsc-users] PC shell destroy

Barry Smith bsmith at petsc.dev
Thu Sep 30 17:08:56 CDT 2021


  Alfredo,

  I think the best approach for you to use is to have your own MATSHELL and your own PCSHELL. You will use your MATSHELL as the second matrix argument to TSSetIJacobian(). It should record the current x and the current shift. 

  Your PCSHELL will then, in PCSetUp(), get access to the current x and the current shift from your MATSHELL and build itself. In other words most of your

> / Get necessary objects from TS context
>   TSGetTime(ts,&time);
>   TSGetApplicationContext(ts,&user);
>   TSGetSolution(ts,&X);
>   TSGetTimeStep(ts,&dt);
>   TSGetStepNumber(ts, &stepi);
>   TSGetDM(ts,&da);
> 
>   tdt = time+dt;
>   // Update preconditioner context with current values
>   ierr = ShellPCSetUp(pc,da,tdt,dt,X,user);CHKERRQ(ierr);
 
 code will disappear and you won't need to mess with the internals of the TS (getting current dt etc) at all. What you need is handed off to your TSSetIJacobian() function which will stick it into your MATSHELL. So nice and clean code.

  Regarding the PCDestroy() for your PCSHELL. It only gets called when the PC is finally destroyed which is when the TS is destroy. So if building your PC in PCSetUp() requires creating new objects you should destroy any previous ones when you create the new ones, hence "lost" objects won't persist in the code.

  Barry





> On Sep 30, 2021, at 4:14 PM, Alfredo J Duarte Gomez <aduarteg at utexas.edu> wrote:
> 
> Good afternoon PETSC team,
> 
> I am currently developing an application for PETSC in which I use my own preconditioner with a PCSHELL.
> 
> I have successfully set all the functions and the performance of the preconditioner is good. I am using this PCSHELL within a TS object, and it is imperative that the objects in the PCSHELL context are freed every time since the memory requirements of those are large.
> 
> I have set up the Preconditioner before the TS starts with the following block of code:
> ----------------------------------------------------------------------------------------------------
> 
> ierr = PCSetType(pc,PCSHELL);CHKERRQ(ierr);
>   ierr = ShellPCCreate(&shell);CHKERRQ(ierr);
>   ierr = PCShellSetApply(pc,MatrixFreePreconditioner);CHKERRQ(ierr);
>   ierr = PCShellSetContext(pc,shell);CHKERRQ(ierr);
>   ierr = PCShellSetDestroy(pc,ShellPCDestroy);CHKERRQ(ierr);
>   ierr = PCShellSetName(pc,"MyPreconditioner");CHKERRQ(ierr);
>   ierr = ShellPCSetUp(pc,da,0.0,dt,u,user);CHKERRQ(ierr);
>   ierr = TSSetPreStep(ts,PreStep);CHKERRQ(ierr);
> 
> ------------------------------------------------------------------------------------------------
> 
> The shell context is then updated by using the following code within the TSPreStep function:
> 
> ---------------------------------------------------------------------------
> 
>  ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
>   ierr = SNESGetKSP(snes,&ksp);CHKERRQ(ierr);
>   ierr = KSPGetPC(ksp,&pc);CHKERRQ(ierr);
> 
>   // Get necessary objects from TS context
>   TSGetTime(ts,&time);
>   TSGetApplicationContext(ts,&user);
>   TSGetSolution(ts,&X);
>   TSGetTimeStep(ts,&dt);
>   TSGetStepNumber(ts, &stepi);
>   TSGetDM(ts,&da);
> 
>   tdt = time+dt;
>   // Update preconditioner context with current values
>   ierr = ShellPCSetUp(pc,da,tdt,dt,X,user);CHKERRQ(ierr);
> ---------------------------------------------------------------------------
> 
> I have set up the necessary code in the function ShellPCDestroy to free the objects within this context, however I am unsure when/if this function is called automatically. Do I have to free the context myself after every step? How would I call the function myself?
> 
> I am running out of memory after a few steps, and I think this shell context is the culprit.
> 
> In addition to that, is it possible to get what is called the "ashift" dF/dU + a*dF/dU_t in this function from the TS object?
> 
> https://petsc.org/release/docs/manualpages/TS/TSSetIJacobian.html <https://petsc.org/release/docs/manualpages/TS/TSSetIJacobian.html>
> 
> I need it as an input for my preconditioner (currrently hardcoded for TSBEULER where ashift is always 1/dt).
> 
> Thank you,
> 
> -Alfredo
> 
> -- 
> Alfredo Duarte
> Graduate Research Assistant
> The University of Texas at Austin

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/petsc-users/attachments/20210930/a7429039/attachment-0001.html>


More information about the petsc-users mailing list