<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class="">
<div class="">Here is an TS example using DMDA and matrix-free Jacobians. Though the matrix-free part is faked, it demonstrates the workflow.</div>
<div class=""><br class="">
</div>
<a href="https://gitlab.com/petsc/petsc/-/blob/hongzh/ts-matshell-example/src/ts/examples/tutorials/advection-diffusion-reaction/ex5_mf.c" class="">https://gitlab.com/petsc/petsc/-/blob/hongzh/ts-matshell-example/src/ts/examples/tutorials/advection-diffusion-reaction/ex5_mf.c</a>
<div class=""><br class="">
</div>
<div class="">Hong (Mr.)</div>
<div class=""><br class="">
<div><br class="">
<blockquote type="cite" class="">
<div class="">On Feb 18, 2020, at 8:26 AM, Yuyun Yang <<a href="mailto:yyang85@stanford.edu" class="">yyang85@stanford.edu</a>> wrote:</div>
<br class="Apple-interchange-newline">
<div class="">
<div class="WordSection1" style="page: WordSection1; caret-color: rgb(0, 0, 0); font-family: Verdana; font-size: 14px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;">
<div style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
Thanks. Also, when using KSP, would the syntax be KSPSetOperators(ksp,A,A)? Since you mentioned preconditioners are not generally used for matrix-free operators, I wasn’t sure whether I should still put “A” in the Pmat field.<o:p class=""></o:p></div>
<div style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<o:p class=""> </o:p></div>
<div style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
Is it still possible to use TS in conjunction with the matrix-free operator? I’d like to create a simple test case that solves the 1d heat equation implicitly with variable coefficients, but didn’t know how the time stepping can be set up.<o:p class=""></o:p></div>
<div style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<o:p class=""> </o:p></div>
<div style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
Thanks,<o:p class=""></o:p></div>
<div style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
Yuyun<o:p class=""></o:p></div>
<div style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<o:p class=""> </o:p></div>
<div style="border-style: solid none none; border-top-width: 1pt; border-top-color: rgb(181, 196, 223); padding: 3pt 0in 0in;" class="">
<div style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<b class=""><span style="font-size: 12pt;" class="">From:<span class="Apple-converted-space"> </span></span></b><span style="font-size: 12pt;" class="">Matthew Knepley <<a href="mailto:knepley@gmail.com" class="">knepley@gmail.com</a>><br class="">
<b class="">Date:<span class="Apple-converted-space"> </span></b>Tuesday, February 18, 2020 at 9:23 PM<br class="">
<b class="">To:<span class="Apple-converted-space"> </span></b>Yuyun Yang <<a href="mailto:yyang85@stanford.edu" class="">yyang85@stanford.edu</a>><br class="">
<b class="">Cc:<span class="Apple-converted-space"> </span></b>"Smith, Barry F." <<a href="mailto:bsmith@mcs.anl.gov" class="">bsmith@mcs.anl.gov</a>>, "<a href="mailto:petsc-users@mcs.anl.gov" class="">petsc-users@mcs.anl.gov</a>" <<a href="mailto:petsc-users@mcs.anl.gov" class="">petsc-users@mcs.anl.gov</a>><br class="">
<b class="">Subject:<span class="Apple-converted-space"> </span></b>Re: [petsc-users] Matrix-free method in PETSc<o:p class=""></o:p></span></div>
</div>
<div class="">
<div style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<o:p class=""> </o:p></div>
</div>
<div class="">
<div class="">
<div style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
On Tue, Feb 18, 2020 at 8:20 AM Yuyun Yang <<a href="mailto:yyang85@stanford.edu" style="color: blue; text-decoration: underline;" class="">yyang85@stanford.edu</a>> wrote:<o:p class=""></o:p></div>
</div>
<div class="">
<blockquote style="border-style: none none none solid; border-left-width: 1pt; border-left-color: rgb(204, 204, 204); padding: 0in 0in 0in 6pt; margin-left: 4.8pt; margin-right: 0in;" class="">
<div style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
Thanks for the clarification.<br class="">
<br class="">
Got one more question: if I have variable coefficients, my stencil will be updated at every time step, so will the coefficients in myMatMult. In that case, is it necessary to destroy the shell matrix and create it all over again, or can I use it as it is, only
 calling the stencil update function, assuming the result will be passed into the matrix operation automatically?<o:p class=""></o:p></div>
</blockquote>
<div class="">
<div style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<o:p class=""> </o:p></div>
</div>
<div class="">
<div style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
You update the information in the context associated with the shell matrix. No need to destroy it.<o:p class=""></o:p></div>
</div>
<div class="">
<div style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<o:p class=""> </o:p></div>
</div>
<div class="">
<div style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
  Thanks,<o:p class=""></o:p></div>
</div>
<div class="">
<div style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<o:p class=""> </o:p></div>
</div>
<div class="">
<div style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
    Matt<o:p class=""></o:p></div>
</div>
<div class="">
<div style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
 <o:p class=""></o:p></div>
</div>
<blockquote style="border-style: none none none solid; border-left-width: 1pt; border-left-color: rgb(204, 204, 204); padding: 0in 0in 0in 6pt; margin-left: 4.8pt; margin-right: 0in;" class="">
<p class="MsoNormal" style="margin: 0in 0in 12pt; font-size: 11pt; font-family: Calibri, sans-serif;">
Thanks,<br class="">
Yuyun<br class="">
<br class="">
On 2/18/20, 7:34 AM, "Smith, Barry F." <<a href="mailto:bsmith@mcs.anl.gov" target="_blank" style="color: blue; text-decoration: underline;" class="">bsmith@mcs.anl.gov</a>> wrote:<br class="">
<br class="">
<br class="">
<br class="">
    > On Feb 17, 2020, at 7:56 AM, Yuyun Yang <<a href="mailto:yyang85@stanford.edu" target="_blank" style="color: blue; text-decoration: underline;" class="">yyang85@stanford.edu</a>> wrote:<br class="">
    ><span class="Apple-converted-space"> </span><br class="">
    > Hello,<br class="">
    ><span class="Apple-converted-space"> </span><br class="">
    > I actually have a question about the usage of DMDA since I'm quite new to this. I wonder if the DMDA suite of functions can be directly called on vectors created from VecCreate?<br class="">
<br class="">
       Yes, but you have to make sure the ones you create have the same sizes and parallel layouts. Generally best to get them from the DMDA or VecDuplicate() than the hassle of figuring out sizes.<br class="">
<br class="">
    > Or the vectors have to be formed by DMDACreateGlobalVector? I'm also not sure about what the dof and stencil width arguments do.<br class="">
    ><span class="Apple-converted-space"> </span><br class="">
    > I'm still unsure about the usage of MatCreateShell and MatShellSetOperation, since it seems that MyMatMult should still have 3 inputs just like MatMult (the matrix and two vectors). Since I'm not forming the matrix, does that mean the matrix input is
 meaningless but still needs to exist for the sake of this format?<br class="">
<br class="">
        Well the matrix input is your shell matrix so it likely has information you need to do your multiply routine. MatShellGetContext() (No you do not want to put your information about the matrix stencil inside global variables!)<br class="">
<br class="">
<br class="">
    ><span class="Apple-converted-space"> </span><br class="">
    > After I create such a shell matrix, can I use it like a regular matrix in KSP and utilize preconditioners?<br class="">
    ><span class="Apple-converted-space"> </span><br class="">
    > Thanks!<br class="">
    > Yuyun<br class="">
    > From: petsc-users <<a href="mailto:petsc-users-bounces@mcs.anl.gov" target="_blank" style="color: blue; text-decoration: underline;" class="">petsc-users-bounces@mcs.anl.gov</a>> on behalf of Yuyun Yang <<a href="mailto:yyang85@stanford.edu" target="_blank" style="color: blue; text-decoration: underline;" class="">yyang85@stanford.edu</a>><br class="">
    > Sent: Sunday, February 16, 2020 3:12 AM<br class="">
    > To: Smith, Barry F. <<a href="mailto:bsmith@mcs.anl.gov" target="_blank" style="color: blue; text-decoration: underline;" class="">bsmith@mcs.anl.gov</a>><br class="">
    > Cc:<span class="Apple-converted-space"> </span><a href="mailto:petsc-users@mcs.anl.gov" target="_blank" style="color: blue; text-decoration: underline;" class="">petsc-users@mcs.anl.gov</a><span class="Apple-converted-space"> </span><<a href="mailto:petsc-users@mcs.anl.gov" target="_blank" style="color: blue; text-decoration: underline;" class="">petsc-users@mcs.anl.gov</a>><br class="">
    > Subject: Re: [petsc-users] Matrix-free method in PETSc<br class="">
    > <span class="Apple-converted-space"> </span><br class="">
    > Thank you, that is very helpful information indeed! I will try it and send you my code when it works.<br class="">
    ><span class="Apple-converted-space"> </span><br class="">
    > Best regards,<br class="">
    > Yuyun<br class="">
    > From: Smith, Barry F. <<a href="mailto:bsmith@mcs.anl.gov" target="_blank" style="color: blue; text-decoration: underline;" class="">bsmith@mcs.anl.gov</a>><br class="">
    > Sent: Saturday, February 15, 2020 10:02 PM<br class="">
    > To: Yuyun Yang <<a href="mailto:yyang85@stanford.edu" target="_blank" style="color: blue; text-decoration: underline;" class="">yyang85@stanford.edu</a>><br class="">
    > Cc:<span class="Apple-converted-space"> </span><a href="mailto:petsc-users@mcs.anl.gov" target="_blank" style="color: blue; text-decoration: underline;" class="">petsc-users@mcs.anl.gov</a><span class="Apple-converted-space"> </span><<a href="mailto:petsc-users@mcs.anl.gov" target="_blank" style="color: blue; text-decoration: underline;" class="">petsc-users@mcs.anl.gov</a>><br class="">
    > Subject: Re: [petsc-users] Matrix-free method in PETSc<br class="">
    > <span class="Apple-converted-space"> </span><br class="">
    >   Yuyun,<br class="">
    ><span class="Apple-converted-space"> </span><br class="">
    >     If you are speaking about using a finite difference stencil on a structured grid where you provide the Jacobian vector products yourself by looping over the grid doing the stencil operation we unfortunately do not have exactly that kind of example.<span class="Apple-converted-space"> </span><br class="">
    ><span class="Apple-converted-space"> </span><br class="">
    >     But it is actually not difficult. I suggest starting with src/ts/examples/tests/ex22.c It computes the sparse matrix explicitly with FormIJacobian()<span class="Apple-converted-space"> </span><br class="">
    ><span class="Apple-converted-space"> </span><br class="">
    >     What you need to do is instead in main() use MatCreateShell() and MatShellSetOperation(,MATOP_MULT,(void (*)(void))MyMatMult) then provide the routine MyMatMult() to do your stencil based matrix free product; note that you can create this new routine
 by taking the structure of IFunction() and reorganizing it to do the Jacobian product instead. You will need to get the information about the shell matrix size on each process by calling DMDAGetCorners().<span class="Apple-converted-space"> </span><br class="">
    ><span class="Apple-converted-space"> </span><br class="">
    >     You will then remove the explicit computation of the Jacobian, and also remove the Event stuff since you don't need it.<br class="">
    ><span class="Apple-converted-space"> </span><br class="">
    >      Extending to 2 and 3d is straight forward.<span class="Apple-converted-space"> </span><br class="">
    ><span class="Apple-converted-space"> </span><br class="">
    >      Any questions let us know.<br class="">
    ><span class="Apple-converted-space"> </span><br class="">
    >    Barry<br class="">
    ><span class="Apple-converted-space"> </span><br class="">
    >    If you like this would make a great merge request with your code to improve our examples.<br class="">
    ><span class="Apple-converted-space"> </span><br class="">
    ><span class="Apple-converted-space"> </span><br class="">
    > > On Feb 15, 2020, at 9:42 PM, Yuyun Yang <<a href="mailto:yyang85@stanford.edu" target="_blank" style="color: blue; text-decoration: underline;" class="">yyang85@stanford.edu</a>> wrote:<br class="">
    > ><span class="Apple-converted-space"> </span><br class="">
    > > Hello team,<br class="">
    > ><span class="Apple-converted-space"> </span><br class="">
    > > I wanted to apply the Krylov subspace method to a matrix-free implementation of a stencil, such that the iterative method acts on the operation without ever constructing the matrix explicitly (for example, when doing backward Euler).<br class="">
    > ><span class="Apple-converted-space"> </span><br class="">
    > > I'm not sure whether there is already an example for that somewhere. If so, could you point me to a relevant example?<br class="">
    > ><span class="Apple-converted-space"> </span><br class="">
    > > Thank you!<br class="">
    > ><span class="Apple-converted-space"> </span><br class="">
    > > Best regards,<br class="">
    > > Yuyun<br class="">
<br class="">
<br class="">
<o:p class=""></o:p></p>
</blockquote>
</div>
<div style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<br clear="all" class="">
<o:p class=""></o:p></div>
<div class="">
<div style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<o:p class=""> </o:p></div>
</div>
<div style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
--<span class="Apple-converted-space"> </span><o:p class=""></o:p></div>
<div class="">
<div class="">
<div class="">
<div class="">
<div class="">
<div class="">
<div class="">
<div style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
What most experimenters take for granted before they begin their experiments is infinitely more interesting than any results to which their experiments lead.<br class="">
-- Norbert Wiener<o:p class=""></o:p></div>
</div>
<div class="">
<div style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<o:p class=""> </o:p></div>
</div>
<div class="">
<div style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<a href="http://www.cse.buffalo.edu/~knepley/" target="_blank" style="color: blue; text-decoration: underline;" class="">https://www.cse.buffalo.edu/~knepley/</a></div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</blockquote>
</div>
<br class="">
</div>
</body>
</html>