Actual source code: ex53.c

  2: #include <petscsnes.h>
  3: #include <../src/snes/impls/vi/viimpl.h>

  5: typedef struct {
  6:   Vec         q,zz,lb,ub;
  7:   Mat         M,Jac;
  8: } AppCtx;

 10: /* 
 11:    User-defined routines
 12: */

 18: int main(int argc,char **argv)
 19: {
 20:   SNES           snes;         /* nonlinear solver context */
 21:   Vec            r;          /* solution, residual vectors */
 23:   AppCtx         user;         /* user-defined work context */
 24:   PetscViewer    viewer;

 26:   PetscInitialize(&argc,&argv,(char *)0,0);
 27:   PetscViewerBinaryOpen(PETSC_COMM_SELF,"videfinition",FILE_MODE_READ,&viewer);
 28:   MatCreate(PETSC_COMM_SELF,&user.M); MatLoad(user.M,viewer);
 29:   MatDuplicate(user.M,MAT_COPY_VALUES,&user.Jac);
 30:   VecCreate(PETSC_COMM_SELF,&user.q); VecLoad(user.q,viewer);
 31:   VecCreate(PETSC_COMM_SELF,&user.lb); VecLoad(user.lb,viewer);
 32:   VecCreate(PETSC_COMM_SELF,&user.ub);VecLoad(user.ub,viewer);
 33:   VecCreate(PETSC_COMM_SELF,&user.zz);VecLoad(user.zz,viewer);
 34:   VecView(user.zz,PETSC_VIEWER_STDOUT_SELF);
 35:   /*  VecSet(user.zz,0.01);*/
 36:   PetscViewerDestroy(&viewer);
 37:   VecDuplicate(user.q,&r);

 39:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 40:      Create nonlinear solver context
 41:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
 42:   SNESCreate(PETSC_COMM_WORLD,&snes);

 44:   SNESSetFunction(snes,r,FormFunction1,&user);

 46:   SNESSetJacobian(snes,user.Jac,user.Jac,FormJacobian1,&user);

 48:   SNESSetFromOptions(snes);
 49:   SNESVISetVariableBounds(snes,user.lb,user.ub);
 50:   SNESSolve(snes,PETSC_NULL,user.zz);
 51:   PetscObjectSetName((PetscObject)user.zz,"x*");
 52:   VecView(user.zz,PETSC_VIEWER_STDOUT_SELF);
 53:   PetscObjectSetName((PetscObject)r,"f(x*)");
 54:   FormFunction1(snes,user.zz,r,&user);
 55:   VecView(r,PETSC_VIEWER_STDOUT_SELF);
 56:   PetscFinalize();
 57:   return 0;
 58: }

 60: /* ------------------------------------------------------------------- */
 63: /* 
 64:    FormFunction1 - Evaluates nonlinear function, F(x).

 66:    Input Parameters:
 67: .  snes - the SNES context
 68: .  x    - input vector
 69: .  ctx  - optional user-defined context

 71:    Output Parameter:
 72: .  f - function vector
 73:  */
 74: PetscErrorCode FormFunction1(SNES snes,Vec x,Vec f,void *ctx)
 75: {
 77:   AppCtx         *user = (AppCtx*)ctx;

 79:   MatMult(user->M,x,f);
 80:   VecAXPY(f,1.0,user->q);
 81:   return 0;
 82: }
 83: /* ------------------------------------------------------------------- */
 86: /*
 87:    FormJacobian1 - Evaluates Jacobian matrix.

 89:    Input Parameters:
 90: .  snes - the SNES context
 91: .  x - input vector
 92: .  ctx - optional user-defined context

 94:    Output Parameters:
 95: .  jac - Jacobian matrix
 96: .  B - optionally different preconditioning matrix
 97: .  flag - flag indicating matrix structure
 98: */
 99: PetscErrorCode FormJacobian1(SNES snes,Vec x,Mat *jac,Mat *B,MatStructure *flag,void *ctx)
100: {
102:   AppCtx         *user = (AppCtx*)ctx;
103:   MatCopy(user->M,*jac,DIFFERENT_NONZERO_PATTERN);
104:   MatAssemblyBegin(*jac,MAT_FINAL_ASSEMBLY);
105:   MatAssemblyEnd(*jac,MAT_FINAL_ASSEMBLY);

107:   return 0;
108: }