Note, your FormInitialGuess does not initialize bb[0] and bb[1]. On Thu, Nov 22, 2018 at 11:23 AM barry via petsc-users < petsc-users@mcs.anl.gov> wrote:
> Hi, > > I want to solve a nonlinear equation tan(x) = x, and the code i wrote > occur some error. > > In the error, it say that I need to use MatAssemblyBegin/End(), but > there aren't any Mat in my program. > > How can I fix it? > > This is what i write (major part): > > int main(){ > > ..... > > VecCreate(PETSC_COMM_WORLD,&f); > VecSetSizes(f,PETSC_DECIDE,400); > VecSetFromOptions(f); > VecDuplicate(f,&b); > > SNESCreate(PETSC_COMM_WORLD,&snes); > SNESSetFunction(snes,f,FormFunction,&user); > SNESSetComputeInitialGuess(snes,FormInitialGuess,NULL); > SNESSetFromOptions(snes); > SNESSolve(snes,NULL,b); > > SNESDestroy(&snes); > > ..... > > } > > PetscErrorCode FormFunction(SNES snes,Vec b,Vec f,void *ctx) > { > const PetscScalar *bb; > PetscScalar *ff; > PetscInt i; > PetscErrorCode ierr; > > PetscInt A1 = 1; > PetscInt A2 = 1; > > VecGetArrayRead(b,&bb); > VecGetArray(f,&ff); > > /* Compute function */ > for (i=0; i<400; i++) ff[i] = tan(bb[i])-(A1/A2)*bb[i]; > > VecRestoreArrayRead(b,&bb); > VecRestoreArray(f,&ff); > return 0; > } > > PetscErrorCode FormInitialGuess(SNES snes,Vec b,void *ctx) > { > PetscScalar *bb; > PetscInt i; > PetscErrorCode ierr; > > VecGetArray(b,&bb); > > /* Compute initial guess */ > for (i=2; i<400; i++) > { > if (i==0) bb[i] = 0; > else if (i==1) bb[i] = 4.5; > else bb[i] = bb[i-1] + 3.2; > } > > VecRestoreArray(b,&bb); > return(0); > } > > > Here is the error come out: > > [0]PETSC ERROR: --------------------- Error Message > -------------------------------------------------------------- > [0]PETSC ERROR: Object is in wrong state > [0]PETSC ERROR: Matrix must be assembled by calls to > MatAssemblyBegin/End(); > [0]PETSC ERROR: See http://www.mcs.anl.gov/petsc/documentation/faq.html > for trouble shooting. > [0]PETSC ERROR: Petsc Release Version 3.10.2, unknown > [0]PETSC ERROR: ./test2 on a arch-linux2-c-debug named G1ngy by barry > Thu Nov 22 21:07:18 2018 > [0]PETSC ERROR: Configure options --with-cc=gcc --with-cxx=g++ > --with-fc=gfortran --download-mpich --download-fblaslapack > [0]PETSC ERROR: #1 MatFDColoringCreate() line 469 in > /home/barry/petsc/src/mat/matfd/fdmatrix.c > [0]PETSC ERROR: #2 SNESComputeJacobianDefaultColor() line 84 in > /home/barry/petsc/src/snes/interface/snesj2.c > [0]PETSC ERROR: #3 SNESComputeJacobian() line 2555 in > /home/barry/petsc/src/snes/interface/snes.c > [0]PETSC ERROR: #4 SNESSolve_NEWTONLS() line 222 in > /home/barry/petsc/src/snes/impls/ls/ls.c > [0]PETSC ERROR: #5 SNESSolve() line 4396 in > /home/barry/petsc/src/snes/interface/snes.c > [0]PETSC ERROR: #6 main() line 89 in /home/barry/Desktop/petsc/test2.c > [0]PETSC ERROR: No PETSc Option Table entries > [0]PETSC ERROR: ----------------End of Error Message -------send entire > error message to petsc-ma...@mcs.anl.gov---------- > application called MPI_Abort(MPI_COMM_WORLD, 73) - process 0 > [unset]: write_line error; fd=-1 buf=:cmd=abort exitcode=73 > : > system msg for write_line failure : Bad file descriptor > > > Thank you, > > Barry > >