I came up with a simple example to demonstrate this "eliminating row" behavior. It happens when the solution x to the linearized equation Ax=b is out of the bound set by SNESVISetVariableBounds();
In the attached example, I use snes to solve a simple function x-b=0. When you run it, it outputs the matrix as 25 rows, while the real Jacobian should be 5*5*2=50 rows. If one changes the lower bound in line 125 to be -inf, it will output 50 rows for the Jacobian. In the first case, the norm given by SNESGetFunctionNorm and SNESGetFunction+VecNorm are also different. In solving the nonlinear equations, it is likely that the solution of the linearized equation is out of bound, but then we can reset the out-of-bound solution to be lower or upper bound instead of eliminating the variables (the rows). Any suggestions on doing this in petsc? Thank you. Best, Xiangdong P.S. If we change the lower bound of field u (line 124) to be zero, then the Jacobian matrix is set to be NULL by petsc. On Thu, May 1, 2014 at 3:43 PM, Xiangdong <epsco...@gmail.com> wrote: > Here is the order of functions I called: > > DMDACreate3d(); > > SNESCreate(); > > SNESSetDM(); (DM with dof=2); > > DMSetApplicationContext(); > > DMDASNESSetFunctionLocal(); > > SNESVISetVariableBounds(); > > DMDASNESetJacobianLocal(); > > SNESSetFromOptions(); > > SNESSolve(); > > SNESGetKSP(); > KSPGetSolution(); > KSPGetRhs(); > KSPGetOperators(); //get operator kspA, kspx, kspb; > > SNESGetFunctionNorm(); ==> get norm fnorma; > SNESGetFunction(); VecNorm(); ==> get norm fnormb; > SNESComputeFunction(); VecNorm(); ==> function evaluation fx at the > solution x and get norm fnormc; > > Inside the FormJacobianLocal(), I output the matrix jac and preB; > > I found that fnorma matches the default SNES monitor output "SNES Function > norm", but fnormb=fnormc != fnorma. The solution x, the residue fx obtained > by snescomputefunction, mat jac and preB are length 50 or 50-by-50, while > the kspA, kspx, kspb are 25-by-25 or length 25. > > I checked that kspA=jac(1:2:end,1:2:end) and x(1:2:end)= kspA\kspb; > x(2:2:end)=0; It seems that it completely ignores the second degree of > freedom (setting it to zero). I saw this for (close to) constant initial > guess, while for heterogeneous initial guess, it works fine and the matrix > and vector size are correct, and the solution is correct. So this > eliminating row behavior seems to be initial guess dependent. > > I saw this even if I use snes_fd, so we can rule out the possibility of > wrong Jacobian. For the FormFunctionLocal(), I checked via > SNESComputeFunction and it output the correct vector of residue. > > Are the orders of function calls correct? > > Thank you. > > Xiangdong > > > > > > > > On Thu, May 1, 2014 at 1:58 PM, Barry Smith <bsm...@mcs.anl.gov> wrote: > >> >> On May 1, 2014, at 10:32 AM, Xiangdong <epsco...@gmail.com> wrote: >> >> > Under what condition, SNESGetFunctionNorm() will output different >> results from SENEGetFunction + VecNorm (with NORM_2)? >> > >> > For most of my test cases, it is the same. However, when I have some >> special (trivial) initial guess to the SNES problem, I see different norms. >> >> Please send more details on your “trivial” case where the values are >> different. It could be that we are not setting the function norm properly >> on early exit from the solvers. >> > >> > Another phenomenon I noticed with this is that KSP in SNES squeeze my >> matrix by eliminating rows. I have a Jacobian supposed to be 50-by-50. When >> I use KSPGetOperators/rhs/solutions, I found that the operator is 25-by-25, >> and the rhs and solution is with length 25. Do you have any clue on what >> triggered this? To my surprise, when I output the Jacobian inside the >> FormJacobianLocal, it outputs the correct matrix 50-by-50 with correct >> numerical entries. Why does the operator obtained from KSP is different and >> got rows eliminated? These rows got eliminated have only one entries per >> row, but the rhs in that row is not zero. Eliminating these rows would give >> wrong solutions. >> >> Hmm, we never squeeze out rows/columns from the Jacobian. The size of >> the Jacobian set with SNESSetJacobian() should always match that obtained >> with KSPGetOperators() on the linear system. Please send more details on >> how you get this. Are you calling the KSPGetOperators() inside a >> preconditioner where the the preconditioner has chopped up the operator? >> >> Barry >> >> > >> > Thank you. >> > >> > Xiangdong >> > >> > >> > >> > >> > >> > >> > On Tue, Apr 29, 2014 at 3:12 PM, Matthew Knepley <knep...@gmail.com> >> wrote: >> > On Tue, Apr 29, 2014 at 2:09 PM, Xiangdong <epsco...@gmail.com> wrote: >> > It turns out to a be a bug in my FormFunctionLocal(DMDALocalInfo >> *info,PetscScalar **x,PetscScalar **f,AppCtx *user). I forgot to initialize >> the array f. Zero the array f solved the problem and gave consistent result. >> > >> > Just curious, why does not petsc initialize the array f to zero by >> default inside petsc when passing the f array to FormFunctionLocal? >> > >> > If you directly set entires, you might not want us to spend the time >> writing those zeros. >> > >> > I have another quick question about the array x passed to >> FormFunctionLocal. If I want to know the which x is evaluated, how can I >> output x in a vector format? Currently, I created a global vector vecx and >> a local vector vecx_local, get the array of vecx_local_array, copy the x to >> vecx_local_array, scatter to global vecx and output vecx. Is there a quick >> way to restore the array x to a vector and output? >> > >> > I cannot think of a better way than that. >> > >> > Matt >> > >> > Thank you. >> > >> > Best, >> > Xiangdong >> > >> > >> > >> > On Mon, Apr 28, 2014 at 10:28 PM, Barry Smith <bsm...@mcs.anl.gov> >> wrote: >> > >> > On Apr 28, 2014, at 3:23 PM, Xiangdong <epsco...@gmail.com> wrote: >> > >> > > Hello everyone, >> > > >> > > When I run snes program, >> > >> > ^^^^ what SNES program”? >> > >> > > it outputs "SNES Function norm 1.23456789e+10". It seems that this >> norm is different from residue norm (even if solving F(x)=0) >> > >> > Please send the full output where you see this. >> > >> > > and also differ from norm of the Jacobian. What is the definition of >> this "SNES Function Norm”? >> > >> > The SNES Function Norm as printed by PETSc is suppose to the 2-norm >> of F(x) - b (where b is usually zero) and this is also the same thing as >> the “residue norm” >> > >> > Barry >> > >> > > >> > > Thank you. >> > > >> > > Best, >> > > Xiangdong >> > >> > >> > >> > >> > >> > -- >> > What most experimenters take for granted before they begin their >> experiments is infinitely more interesting than any results to which their >> experiments lead. >> > -- Norbert Wiener >> > >> >> >
#include <petsc.h> typedef struct { PetscReal u, v; } Field; /*structure for unknowns*/ typedef struct { } AppCtx; PetscErrorCode SetVariableBounds(DM da, Vec xl, Vec xu); PetscErrorCode FormFunctionLocal(DMDALocalInfo *infopt, Field ***statenext, Field ***residual, void *ptr); #undef __FUNCT__ #define __FUNCT__ "main" int main(int argc, char **argv) { PetscInitialize(&argc, &argv, PETSC_NULL, PETSC_NULL); PetscPrintf(PETSC_COMM_WORLD,"----Initializing------\n"); PetscErrorCode ierr; int myrank; MPI_Comm_rank(MPI_COMM_WORLD,&myrank); PetscInt Nx=1, Ny=5, Nz=5; int dof=2, width=1; DM da; ierr = DMDACreate3d(PETSC_COMM_WORLD,DMDA_BOUNDARY_NONE,DMDA_BOUNDARY_NONE,DMDA_BOUNDARY_NONE, DMDA_STENCIL_STAR,Nx,Ny,Nz,PETSC_DECIDE,PETSC_DECIDE, PETSC_DECIDE,dof,width,NULL,NULL,NULL,&da);CHKERRQ(ierr); AppCtx user; SNES snes; ierr = SNESCreate(PETSC_COMM_WORLD,&snes); CHKERRQ(ierr); ierr = SNESSetDM(snes, (DM) da); CHKERRQ(ierr); ierr = DMSetApplicationContext(da,&user);CHKERRQ(ierr); ierr = DMDASNESSetFunctionLocal(da,INSERT_VALUES,(PetscErrorCode (*)(DMDALocalInfo*,void*,void*,void*))FormFunctionLocal,&user); CHKERRQ(ierr); /* set lower and upper bounds for solutions */ Vec xl, xu; ierr = DMCreateGlobalVector(da,&xl); CHKERRQ(ierr); ierr = DMCreateGlobalVector(da,&xu); CHKERRQ(ierr); ierr = PetscObjectSetName((PetscObject) xl, "xl"); CHKERRQ(ierr); ierr = PetscObjectSetName((PetscObject) xu, "xu"); CHKERRQ(ierr); SetVariableBounds(da,xl,xu); ierr = SNESVISetVariableBounds(snes,xl,xu); CHKERRQ(ierr); //VecView(xl,PETSC_VIEWER_STDOUT_WORLD); CHKERRQ(ierr); //VecView(xu,PETSC_VIEWER_STDOUT_WORLD); CHKERRQ(ierr); Vec solcur; ierr = DMCreateGlobalVector(da,&solcur); CHKERRQ(ierr); ierr = PetscObjectSetName((PetscObject) solcur, "solcur"); CHKERRQ(ierr); ierr = VecSet(solcur,0.0); CHKERRQ(ierr); ierr = SNESSetFromOptions(snes); CHKERRQ(ierr); VecView(solcur,PETSC_VIEWER_STDOUT_WORLD); CHKERRQ(ierr); ierr = SNESSolve(snes,NULL,solcur); CHKERRQ(ierr); Vec myx,myb; Mat myjac; KSP ksp; SNESGetKSP(snes,&ksp); KSPGetSolution(ksp,&myx); KSPGetRhs(ksp,&myb); KSPGetOperators(ksp,&myjac,NULL,NULL); MatView(myjac,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); VecView(myb,PETSC_VIEWER_STDOUT_WORLD); CHKERRQ(ierr); VecView(myx,PETSC_VIEWER_STDOUT_WORLD); CHKERRQ(ierr); Vec gva; double gna,gna2; ierr = SNESGetFunctionNorm(snes,&gna); ierr = SNESGetFunction(snes,&gva,NULL,NULL); VecNorm(gva,NORM_2,&gna2); double fna, xna; Vec frescur; VecDuplicate(solcur,&frescur); PetscObjectSetName((PetscObject) frescur, "frescur"); CHKERRQ(ierr); SNESComputeFunction(snes,solcur,frescur); VecNorm(frescur,NORM_2,&fna); VecNorm(solcur,NORM_2,&xna); PetscPrintf(PETSC_COMM_WORLD,"the norm by getnorm is %.16e and by getfun+vecnorm is %.16e and by computefun is %.16e\n",gna,gna2,fna); VecView(solcur,PETSC_VIEWER_STDOUT_WORLD); CHKERRQ(ierr); /*--------finalzie and exit the program --------*/ ierr = PetscFinalize(); CHKERRQ(ierr); PetscFunctionReturn(0); } #undef __FUNCT__ #define __FUNCT__ "SetVariableBounds" PetscErrorCode SetVariableBounds(DM da, Vec xl, Vec xu) { PetscScalar ****l, ****u; PetscInt mxstart, mystart, mzstart, mxlen, mylen, mzlen; PetscInt ix, iy, iz; PetscErrorCode ierr; ierr=DMDAVecGetArrayDOF(da,xl,&l); CHKERRQ(ierr); ierr=DMDAVecGetArrayDOF(da,xu,&u); CHKERRQ(ierr); ierr=DMDAGetCorners(da,&mxstart,&mystart,&mzstart,&mxlen,&mylen,&mzlen); for (iz=mzstart; iz<mzstart+mzlen; iz++){ for (iy=mystart; iy<mystart+mylen; iy++) { for (ix=mxstart; ix<mxstart+mxlen; ix++) { l[iz][iy][ix][0]=-SNES_VI_INF; l[iz][iy][ix][1]=0; //-SNES_VI_INF u[iz][iy][ix][0]=SNES_VI_INF; u[iz][iy][ix][1]=SNES_VI_INF; }}} ierr=DMDAVecRestoreArrayDOF(da,xl,&l); CHKERRQ(ierr); ierr=DMDAVecRestoreArrayDOF(da,xu,&u); CHKERRQ(ierr); PetscFunctionReturn(0); } #undef __FUNCT__ #define __FUNCT__ "FormFunctionLocal" PetscErrorCode FormFunctionLocal(DMDALocalInfo *infopt, Field ***statenext, Field ***residual, void *ptr) { PetscInt ix, iy, iz, mxstart, mystart, mzstart, mxlen, mylen, mzlen, mxend, myend, mzend; mxstart = infopt->xs; mystart = infopt->ys; mzstart = infopt->zs; mxlen = infopt->xm; mylen = infopt->ym; mzlen = infopt->zm; mxend = mxstart + mxlen; myend = mystart + mylen; mzend = mzstart + mzlen; for (iz=mzstart; iz<mzend; iz++){ for (iy=mystart; iy<myend; iy++) { for (ix=mxstart; ix<mxend; ix++) { residual[iz][iy][ix].u = statenext[iz][iy][ix].u; residual[iz][iy][ix].v = statenext[iz][iy][ix].v; }}} residual[0][0][0].u += 100; residual[0][4][0].u += 100; residual[0][0][4].u += 100; residual[0][4][4].u += 100; residual[0][2][2].v += 100; PetscFunctionReturn(0); }