<html><head><meta http-equiv="Content-Type" content="text/html; charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><div class=""><br class=""></div><div class=""> Alfredo,</div><div class=""><br class=""></div> 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. <div class=""><br class=""></div><div class=""> 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</div><div class=""><br class=""></div><div class=""><blockquote type="cite" class=""><div dir="ltr" class=""><div class="">/ Get necessary objects from TS context<br class=""> TSGetTime(ts,&time);<br class=""> TSGetApplicationContext(ts,&user);<br class=""> TSGetSolution(ts,&X);<br class=""> TSGetTimeStep(ts,&dt);<br class=""> TSGetStepNumber(ts, &stepi);<br class=""> TSGetDM(ts,&da);<br class=""><br class=""> tdt = time+dt;<br class=""> // Update preconditioner context with current values<br class=""> ierr = ShellPCSetUp(pc,da,tdt,dt,X,user);CHKERRQ(ierr);</div></div></blockquote> </div><div class=""> 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.</div><div class=""><br class=""></div><div class=""> 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.</div><div class=""><br class=""></div><div class=""> Barry</div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><br class=""><div><br class=""><blockquote type="cite" class=""><div class="">On Sep 30, 2021, at 4:14 PM, Alfredo J Duarte Gomez <<a href="mailto:aduarteg@utexas.edu" class="">aduarteg@utexas.edu</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class="">Good afternoon PETSC team,<div class=""><br class=""></div><div class="">I am currently developing an application for PETSC in which I use my own preconditioner with a PCSHELL.</div><div class=""><br class=""></div><div class="">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.</div><div class=""><br class=""></div><div class="">I have set up the Preconditioner before the TS starts with the following block of code:</div><div class="">----------------------------------------------------------------------------------------------------</div><div class=""><br class=""></div><div class="">ierr = PCSetType(pc,PCSHELL);CHKERRQ(ierr);<br class=""> ierr = ShellPCCreate(&shell);CHKERRQ(ierr);<br class=""> ierr = PCShellSetApply(pc,MatrixFreePreconditioner);CHKERRQ(ierr);<br class=""> ierr = PCShellSetContext(pc,shell);CHKERRQ(ierr);<br class=""> ierr = PCShellSetDestroy(pc,ShellPCDestroy);CHKERRQ(ierr);<br class=""> ierr = PCShellSetName(pc,"MyPreconditioner");CHKERRQ(ierr);<br class=""> ierr = ShellPCSetUp(pc,da,0.0,dt,u,user);CHKERRQ(ierr);<br class=""> ierr = TSSetPreStep(ts,PreStep);CHKERRQ(ierr);<br class=""></div><div class=""><br class=""></div><div class="">------------------------------------------------------------------------------------------------</div><div class=""><br class=""></div><div class="">The shell context is then updated by using the following code within the TSPreStep function:</div><div class=""><br class=""></div><div class="">---------------------------------------------------------------------------</div><div class=""><br class=""></div><div class=""> ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);<br class=""> ierr = SNESGetKSP(snes,&ksp);CHKERRQ(ierr);<br class=""> ierr = KSPGetPC(ksp,&pc);CHKERRQ(ierr);<br class=""><br class=""> // Get necessary objects from TS context<br class=""> TSGetTime(ts,&time);<br class=""> TSGetApplicationContext(ts,&user);<br class=""> TSGetSolution(ts,&X);<br class=""> TSGetTimeStep(ts,&dt);<br class=""> TSGetStepNumber(ts, &stepi);<br class=""> TSGetDM(ts,&da);<br class=""><br class=""> tdt = time+dt;<br class=""> // Update preconditioner context with current values<br class=""> ierr = ShellPCSetUp(pc,da,tdt,dt,X,user);CHKERRQ(ierr);<br class=""></div><div class="">---------------------------------------------------------------------------</div><div class=""><div class=""><br class=""></div><div class="">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?</div><div class=""><br class=""></div><div class="">I am running out of memory after a few steps, and I think this shell context is the culprit.</div><div class=""><br class=""></div><div class="">In addition to that, is it possible to get what is called the "ashift" <span style="font-family: "Times New Roman"; font-size: inherit;" class="">dF/dU + a*dF/dU_t in this function from the TS object?</span></div><div class=""><br class=""></div><div class=""><a href="https://petsc.org/release/docs/manualpages/TS/TSSetIJacobian.html" class="">https://petsc.org/release/docs/manualpages/TS/TSSetIJacobian.html</a><br class=""></div><div class=""><br class=""></div><div class="">I need it as an input for my preconditioner (currrently hardcoded for TSBEULER where ashift is always 1/dt).</div><div class=""><br class=""></div><div class="">Thank you,</div><div class=""><br class=""></div><div class="">-Alfredo</div><div class=""><br class=""></div>-- <br class=""><div dir="ltr" class="gmail_signature" data-smartmail="gmail_signature"><div dir="ltr" class=""><div class=""><div dir="ltr" class=""><font face="arial, sans-serif" class="">Alfredo Duarte</font><div class=""><font face="arial, sans-serif" class="">Graduate Research Assistant</font></div><div class=""><font face="arial, sans-serif" class="">The University of Texas at Austin</font></div></div></div></div></div></div></div>
</div></blockquote></div><br class=""></div></body></html>