<div dir="ltr"><div class="gmail_quote"><div dir="ltr">On Thu, Nov 22, 2018 at 11:23 AM barry via petsc-users <<a href="mailto:petsc-users@mcs.anl.gov">petsc-users@mcs.anl.gov</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi,<br>
<br>
I want to solve a nonlinear equation tan(x) = x, and the code i wrote <br>
occur some error.<br>
<br>
In the error, it say that I need to use MatAssemblyBegin/End(), but <br>
there aren't any Mat in my program.<br>
<br>
How can I fix it?<br></blockquote><div><br></div><div>You could</div><div><br></div><div> a) Use a method without a Jacobian, like nonlinear Richardson</div><div><br></div><div> b) Use Newton with a finite difference Jacobian, -snes_type fd, but this is not scalable</div><div><br></div><div> c) Provide a Jacobian for Newton</div><div><br></div><div> Thanks,</div><div><br></div><div> Matt</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
This is what i write (major part):<br>
<br>
int main(){<br>
<br>
.....<br>
<br>
VecCreate(PETSC_COMM_WORLD,&f);<br>
VecSetSizes(f,PETSC_DECIDE,400);<br>
VecSetFromOptions(f);<br>
VecDuplicate(f,&b);<br>
<br>
SNESCreate(PETSC_COMM_WORLD,&snes);<br>
SNESSetFunction(snes,f,FormFunction,&user);<br>
SNESSetComputeInitialGuess(snes,FormInitialGuess,NULL);<br>
SNESSetFromOptions(snes);<br>
SNESSolve(snes,NULL,b);<br>
<br>
SNESDestroy(&snes);<br>
<br>
.....<br>
<br>
}<br>
<br>
PetscErrorCode FormFunction(SNES snes,Vec b,Vec f,void *ctx)<br>
{<br>
const PetscScalar *bb;<br>
PetscScalar *ff;<br>
PetscInt i;<br>
PetscErrorCode ierr;<br>
<br>
PetscInt A1 = 1;<br>
PetscInt A2 = 1;<br>
<br>
VecGetArrayRead(b,&bb);<br>
VecGetArray(f,&ff);<br>
<br>
/* Compute function */<br>
for (i=0; i<400; i++) ff[i] = tan(bb[i])-(A1/A2)*bb[i];<br>
<br>
VecRestoreArrayRead(b,&bb);<br>
VecRestoreArray(f,&ff);<br>
return 0;<br>
}<br>
<br>
PetscErrorCode FormInitialGuess(SNES snes,Vec b,void *ctx)<br>
{<br>
PetscScalar *bb;<br>
PetscInt i;<br>
PetscErrorCode ierr;<br>
<br>
VecGetArray(b,&bb);<br>
<br>
/* Compute initial guess */<br>
for (i=2; i<400; i++)<br>
{<br>
if (i==0) bb[i] = 0;<br>
else if (i==1) bb[i] = 4.5;<br>
else bb[i] = bb[i-1] + 3.2;<br>
}<br>
<br>
VecRestoreArray(b,&bb);<br>
return(0);<br>
}<br>
<br>
<br>
Here is the error come out:<br>
<br>
[0]PETSC ERROR: --------------------- Error Message <br>
--------------------------------------------------------------<br>
[0]PETSC ERROR: Object is in wrong state<br>
[0]PETSC ERROR: Matrix must be assembled by calls to MatAssemblyBegin/End();<br>
[0]PETSC ERROR: See <a href="http://www.mcs.anl.gov/petsc/documentation/faq.html" rel="noreferrer" target="_blank">http://www.mcs.anl.gov/petsc/documentation/faq.html</a> <br>
for trouble shooting.<br>
[0]PETSC ERROR: Petsc Release Version 3.10.2, unknown<br>
[0]PETSC ERROR: ./test2 on a arch-linux2-c-debug named G1ngy by barry <br>
Thu Nov 22 21:07:18 2018<br>
[0]PETSC ERROR: Configure options --with-cc=gcc --with-cxx=g++ <br>
--with-fc=gfortran --download-mpich --download-fblaslapack<br>
[0]PETSC ERROR: #1 MatFDColoringCreate() line 469 in <br>
/home/barry/petsc/src/mat/matfd/fdmatrix.c<br>
[0]PETSC ERROR: #2 SNESComputeJacobianDefaultColor() line 84 in <br>
/home/barry/petsc/src/snes/interface/snesj2.c<br>
[0]PETSC ERROR: #3 SNESComputeJacobian() line 2555 in <br>
/home/barry/petsc/src/snes/interface/snes.c<br>
[0]PETSC ERROR: #4 SNESSolve_NEWTONLS() line 222 in <br>
/home/barry/petsc/src/snes/impls/ls/ls.c<br>
[0]PETSC ERROR: #5 SNESSolve() line 4396 in <br>
/home/barry/petsc/src/snes/interface/snes.c<br>
[0]PETSC ERROR: #6 main() line 89 in /home/barry/Desktop/petsc/test2.c<br>
[0]PETSC ERROR: No PETSc Option Table entries<br>
[0]PETSC ERROR: ----------------End of Error Message -------send entire <br>
error message to petsc-maint@mcs.anl.gov----------<br>
application called MPI_Abort(MPI_COMM_WORLD, 73) - process 0<br>
[unset]: write_line error; fd=-1 buf=:cmd=abort exitcode=73<br>
:<br>
system msg for write_line failure : Bad file descriptor<br>
<br>
<br>
Thank you,<br>
<br>
Barry<br>
<br>
</blockquote></div><br clear="all"><div><br></div>-- <br><div dir="ltr" class="gmail_signature" data-smartmail="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div>What most experimenters take for granted before they begin their experiments is infinitely more interesting than any results to which their experiments lead.<br>-- Norbert Wiener</div><div><br></div><div><a href="http://www.cse.buffalo.edu/~knepley/" target="_blank">https://www.cse.buffalo.edu/~knepley/</a><br></div></div></div></div></div></div></div></div>