<html><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><br><div><br><div>Begin forwarded message:</div><br class="Apple-interchange-newline"><blockquote type="cite"><div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><font face="Helvetica" size="3" color="#000000" style="font: 12.0px Helvetica; color: #000000"><b>From: </b></font><font face="Helvetica" size="3" style="font: 12.0px Helvetica">Jed Brown &lt;<a href="mailto:jed@59A2.org">jed@59A2.org</a>&gt;</font></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><font face="Helvetica" size="3" color="#000000" style="font: 12.0px Helvetica; color: #000000"><b>Date: </b></font><font face="Helvetica" size="3" style="font: 12.0px Helvetica">July 21, 2010 8:04:17 PM EDT</font></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><font face="Helvetica" size="3" color="#000000" style="font: 12.0px Helvetica; color: #000000"><b>To: </b></font><font face="Helvetica" size="3" style="font: 12.0px Helvetica">Xuan YU &lt;<a href="mailto:xxy113@psu.edu">xxy113@psu.edu</a>&gt;, PETSc users list &lt;<a href="mailto:petsc-users@mcs.anl.gov">petsc-users@mcs.anl.gov</a>&gt;</font></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><font face="Helvetica" size="3" color="#000000" style="font: 12.0px Helvetica; color: #000000"><b>Subject: </b></font><font face="Helvetica" size="3" style="font: 12.0px Helvetica"><b>Re: [petsc-users] TSDefaultComputeJacobian</b></font></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; min-height: 14px; "><br></div> </div><div>On Wed, 21 Jul 2010 19:52:23 -0400, Xuan YU &lt;<a href="mailto:xxy113@psu.edu">xxy113@psu.edu</a>&gt; wrote:<br><blockquote type="cite">Hi,<br></blockquote><blockquote type="cite"><br></blockquote><blockquote type="cite">In SNES, Petsc can compute Jacobian by:<br></blockquote><blockquote type="cite"><br></blockquote><blockquote type="cite">ierr = &nbsp;<br></blockquote><blockquote type="cite">SNESSetJacobian <br></blockquote><blockquote type="cite">(snes,J,J,SNESDefaultComputeJacobian,PETSC_NULL);CHKERRQ(ierr);<br></blockquote><blockquote type="cite"><br></blockquote><blockquote type="cite">How about TS? I don't want to provide Jacobian function by myself, but &nbsp;<br></blockquote><blockquote type="cite">I didn't find something like TSDefaultComputeJacobian.<br></blockquote><br>You can use -snes_fd (or -snes_mf or -snes_mf_operator) with TS.<br>Programmatically,<br><br> &nbsp;TSGetSNES(ts,&amp;snes);<br></div></blockquote><blockquote type="cite"><div>&nbsp;SNESSetJacobian(snes,A,B,SNESDefaultComputeJacobian,ts);<br><br></div></blockquote><div><br></div><div>I have already tried this function.</div><div><br></div><div>I want to use&nbsp;<span class="Apple-style-span" style="font-size: 15px; ">Finite Difference Jacobian Approximations</span></div><div><br></div><div>So, before I call the function:&nbsp;MatGetColoring(J,MATCOLORINGSL,&amp;iscoloring);CHKERRQ(ierr); I should assemble J. I tried to use SNESComputeJacobian(snes,x,&amp;J,&amp;J,&amp;flag) to assemble J.</div><div><br></div><div>Here is the code</div><div><br></div><div><br></div><div><br></div><div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;ierr = MatCreateSeqAIJ(PETSC_COMM_SELF,N,N,10,PETSC_NULL,&amp;J);CHKERRQ(ierr);</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;ierr = TSCreate(PETSC_COMM_WORLD,&amp;ts);CHKERRQ(ierr);</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;ierr = TSSetProblemType(ts,TS_NONLINEAR);CHKERRQ(ierr);</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;ierr = TSMonitorSet(ts,Monitor,&amp;appctx,PETSC_NULL);CHKERRQ(ierr);</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;ierr = TSSetSolution(ts,CV_Y);CHKERRQ(ierr);</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;ierr = TSSetRHSFunction(ts,f,&amp;appctx);CHKERRQ(ierr);</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;ierr = TSSetType(ts,TSBEULER);CHKERRQ(ierr);</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;ierr = TSGetSNES(ts,&amp;ts_snes);</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;ierr= SNESSetJacobian(ts_snes,J,J,SNESDefaultComputeJacobian,PETSC_NULL);CHKERRQ(ierr);</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;MatStructure &nbsp;flag;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;ierr = SNESComputeJacobian(ts_snes,CV_Y,&amp;J,&amp;J,&amp;flag);CHKERRQ(ierr);</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;ierr = MatGetColoring(J,MATCOLORINGSL,&amp;iscoloring);CHKERRQ(ierr);</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;ierr = MatFDColoringCreate(J,iscoloring,&amp;matfdcoloring);CHKERRQ(ierr);</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;ISColoringDestroy(iscoloring);</div><div><br></div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;ierr = MatFDColoringSetFunction(matfdcoloring,(PetscErrorCode (*)(void))f,(void*)&amp;appctx);CHKERRQ(ierr);</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;ierr = MatFDColoringSetFromOptions(matfdcoloring);CHKERRQ(ierr);</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;ierr = TSSetRHSJacobian(ts,J,J,TSDefaultComputeJacobianColor,matfdcoloring);</div><div><br></div></div>But I got the error:</div><div><div>[0]PETSC ERROR: --------------------- Error Message ------------------------------------</div><div>[0]PETSC ERROR: Object is in wrong state!</div><div>[0]PETSC ERROR: Must call SNESSetFunction() before SNESComputeFunction(), likely called from SNESSolve().!</div><div>[0]PETSC ERROR: ------------------------------------------------------------------------</div><div>[0]PETSC ERROR: Petsc Development HG revision: fdc7be12de37b8a400b6e561de9850bcc7e79f4f &nbsp;HG Date: Fri Jul 02 21:17:36 2010 -0500</div><div>[0]PETSC ERROR: See docs/changes/index.html for recent updates.</div><div>[0]PETSC ERROR: See docs/faq.html for hints about trouble shooting.</div><div>[0]PETSC ERROR: See docs/index.html for manual pages.</div><div>[0]PETSC ERROR: ------------------------------------------------------------------------</div><div>[0]PETSC ERROR: ./pihm on a arch-linu named kernighan.aset.psu.edu by xxy113 Wed Jul 21 21:35:01 2010</div><div>[0]PETSC ERROR: Libraries linked from /gpfs/home/xxy113/soft/petsc-dev/arch-linux-gnu-c-debug/lib</div><div>[0]PETSC ERROR: Configure run at Sun Jul &nbsp;4 14:09:16 2010</div><div>[0]PETSC ERROR: Configure options --download-f-blas-lapack=1 --download-mpich=1</div><div>[0]PETSC ERROR: ------------------------------------------------------------------------</div><div>[0]PETSC ERROR: SNESComputeFunction() line 1180 in src/snes/interface/snes.c</div><div>[0]PETSC ERROR: SNESDefaultComputeJacobian() line 77 in src/snes/interface/snesj.c</div><div>[0]PETSC ERROR: SNESComputeJacobian() line 1264 in src/snes/interface/snes.c</div><div>[0]PETSC ERROR: VecRestoreArrayPrivate3() line 234 in /work/petsc/pihm.c</div><div><br></div><div><br></div><div><br></div><blockquote type="cite"><div>Jed<br><br></div></blockquote></div><br></body></html>