Actual source code: ex43.c
petsc-3.4.0 2013-05-13
2: static char help[] = "Newton's method to solve a many-variable system that comes from the 2 variable Rosenbrock function + trivial.\n\n";
4: /*
6: ./ex43 -snes_monitor_range -snes_max_it 1000 -snes_rtol 1.e-14 -n 10 -snes_converged_reason -sub_snes_monito -sub_snes_mf -sub_snes_converged_reason -sub_snes_rtol 1.e-10 -sub_snes_max_it 1000 -sub_snes_monitor
8: Accelerates Newton's method by solving a small problem defined by those elements with large residual plus one level of overlap
10: This is a toy code for playing around
12: Counts residual entries as small if they are less then .2 times the maximum
13: Decides to solve a reduced problem if the number of large entries is less than 20 percent of all entries (and this has been true for criteria_reduce iterations)
14: */
15: #include "ex43-44.h"
18: extern PetscErrorCode FormJacobian1(SNES,Vec,Mat*,Mat*,MatStructure*,void*);
19: extern PetscErrorCode FormFunction1(SNES,Vec,Vec,void*);
21: typedef struct {
22: PetscInt n,p;
23: } Ctx;
27: int main(int argc,char **argv)
28: {
29: SNES snes; /* nonlinear solver context */
30: Vec x,r; /* solution, residual vectors */
31: Mat J; /* Jacobian matrix */
32: PetscErrorCode ierr;
33: PetscScalar *xx;
34: PetscInt i,max_snes_solves = 20,snes_steps_per_solve = 2,criteria_reduce = 1;
35: Ctx ctx;
36: SNESConvergedReason reason;
38: PetscInitialize(&argc,&argv,(char*)0,help);
39: ctx.n = 0;
40: PetscOptionsGetInt(NULL,"-n",&ctx.n,NULL);
41: ctx.p = 0;
42: PetscOptionsGetInt(NULL,"-p",&ctx.p,NULL);
43: PetscOptionsGetInt(NULL,"-max_snes_solves",&max_snes_solves,NULL);
44: PetscOptionsGetInt(NULL,"-snes_steps_per_solve",&snes_steps_per_solve,NULL);
45: PetscOptionsGetInt(NULL,"-criteria_reduce",&criteria_reduce,NULL);
47: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
48: Create nonlinear solver context
49: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
50: SNESCreate(PETSC_COMM_WORLD,&snes);
52: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
53: Create matrix and vector data structures; set corresponding routines
54: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
55: /*
56: Create vectors for solution and nonlinear function
57: */
58: VecCreate(PETSC_COMM_WORLD,&x);
59: VecSetSizes(x,PETSC_DECIDE,2+ctx.n+ctx.p);
60: VecSetFromOptions(x);
61: VecDuplicate(x,&r);
63: /*
64: Create Jacobian matrix data structure
65: */
66: MatCreate(PETSC_COMM_WORLD,&J);
67: MatSetSizes(J,PETSC_DECIDE,PETSC_DECIDE,2+ctx.p+ctx.n,2+ctx.p+ctx.n);
68: MatSetFromOptions(J);
70: /*
71: Set function evaluation routine and vector.
72: */
73: SNESSetFunction(snes,r,FormFunction1,(void*)&ctx);
75: /*
76: Set Jacobian matrix data structure and Jacobian evaluation routine
77: */
78: SNESSetJacobian(snes,J,J,FormJacobian1,(void*)&ctx);
80: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
81: Customize nonlinear solver; set runtime options
82: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
83: SNESSetFromOptions(snes);
85: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
86: Evaluate initial guess; then solve nonlinear system
87: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
88: VecSet(x,0.0);
89: VecGetArray(x,&xx);
90: xx[0] = -1.2;
91: for (i=1; i<ctx.p+2; i++) xx[i] = 1.0;
92: VecRestoreArray(x,&xx);
94: /*
95: Note: The user should initialize the vector, x, with the initial guess
96: for the nonlinear solver prior to calling SNESSolve(). In particular,
97: to employ an initial guess of zero, the user should explicitly set
98: this vector to zero by calling VecSet().
99: */
101: SNESMonitorSet(snes,MonitorRange,0,0);
102: SNESSetTolerances(snes,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT,snes_steps_per_solve,PETSC_DEFAULT);
103: for (i=0; i<max_snes_solves; i++) {
104: SNESSolve(snes,NULL,x);
105: SNESGetConvergedReason(snes,&reason);
106: if (reason && reason != SNES_DIVERGED_MAX_IT) break;
107: if (CountGood > criteria_reduce) {
108: SolveSubproblem(snes);
109: CountGood = 0;
110: }
111: }
113: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
114: Free work space. All PETSc objects should be destroyed when they
115: are no longer needed.
116: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
118: VecDestroy(&x); VecDestroy(&r);
119: MatDestroy(&J); SNESDestroy(&snes);
120: PetscFinalize();
121: return 0;
122: }
123: /* ------------------------------------------------------------------- */
126: /*
127: FormFunction1 - Evaluates nonlinear function, F(x).
129: Input Parameters:
130: . snes - the SNES context
131: . x - input vector
132: . ctx - optional user-defined context
134: Output Parameter:
135: . f - function vector
136: */
137: PetscErrorCode FormFunction1(SNES snes,Vec x,Vec f,void *ictx)
138: {
140: PetscScalar *xx,*ff;
141: PetscInt i;
142: Ctx *ctx = (Ctx*)ictx;
144: /*
145: Get pointers to vector data.
146: - For default PETSc vectors, VecGetArray() returns a pointer to
147: the data array. Otherwise, the routine is implementation dependent.
148: - You MUST call VecRestoreArray() when you no longer need access to
149: the array.
150: */
151: VecGetArray(x,&xx);
152: VecGetArray(f,&ff);
154: /* Compute function */
155: ff[0] = -2.0 + 2.0*xx[0] + 400.0*xx[0]*xx[0]*xx[0] - 400.0*xx[0]*xx[1];
156: for (i=1; i<1+ctx->p; i++) {
157: ff[i] = -2.0 + 2.0*xx[i] + 400.0*xx[i]*xx[i]*xx[i] - 400.0*xx[i]*xx[i+1] + 200.0*(xx[i] - xx[i-1]*xx[i-1]);
158: }
159: ff[ctx->p+1] = -200.0*xx[ctx->p]*xx[ctx->p] + 200.0*xx[ctx->p+1];
160: for (i=ctx->p+2; i<2+ctx->p+ctx->n; i++) {
161: ff[i] = xx[i] - xx[0] + .7*xx[1] - .2*xx[i-1]*xx[i-1];
162: }
164: /* Restore vectors */
165: VecRestoreArray(x,&xx);
166: VecRestoreArray(f,&ff);
167: return 0;
168: }
169: /* ------------------------------------------------------------------- */
172: /*
173: FormJacobian1 - Evaluates Jacobian matrix.
175: Input Parameters:
176: . snes - the SNES context
177: . x - input vector
178: . dummy - optional user-defined context (not used here)
180: Output Parameters:
181: . jac - Jacobian matrix
182: . B - optionally different preconditioning matrix
183: . flag - flag indicating matrix structure
184: */
185: PetscErrorCode FormJacobian1(SNES snes,Vec x,Mat *jac,Mat *B,MatStructure *flag,void *ictx)
186: {
187: PetscScalar *xx;
189: PetscInt i;
190: Ctx *ctx = (Ctx*)ictx;
192: MatZeroEntries(*B);
193: /*
194: Get pointer to vector data
195: */
196: VecGetArray(x,&xx);
198: /*
199: Compute Jacobian entries and insert into matrix.
200: - Since this is such a small problem, we set all entries for
201: the matrix at once.
202: */
203: MatSetValue(*B,0,0, 2.0 + 1200.0*xx[0]*xx[0] - 400.0*xx[1],ADD_VALUES);
204: MatSetValue(*B,0,1,-400.0*xx[0],ADD_VALUES);
206: for (i=1; i<ctx->p+1; i++) {
207: MatSetValue(*B,i,i-1, -400.0*xx[i-1],ADD_VALUES);
208: MatSetValue(*B,i,i, 2.0 + 1200.0*xx[i]*xx[i] - 400.0*xx[i+1] + 200.0,ADD_VALUES);
209: MatSetValue(*B,i,i+1,-400.0*xx[i],ADD_VALUES);
210: }
212: MatSetValue(*B,ctx->p+1,ctx->p, -400.0*xx[ctx->p],ADD_VALUES);
213: MatSetValue(*B,ctx->p+1,ctx->p+1,200,ADD_VALUES);
215: *flag = SAME_NONZERO_PATTERN;
217: for (i=ctx->p+2; i<2+ctx->p+ctx->n; i++) {
218: MatSetValue(*B,i,i,1.0,ADD_VALUES);
219: MatSetValue(*B,i,0,-1.0,ADD_VALUES);
220: MatSetValue(*B,i,1,.7,ADD_VALUES);
221: MatSetValue(*B,i,i-1,-.4*xx[i-1],ADD_VALUES);
222: }
223: /*
224: Restore vector
225: */
226: VecRestoreArray(x,&xx);
228: /*
229: Assemble matrix
230: */
231: MatAssemblyBegin(*B,MAT_FINAL_ASSEMBLY);
232: MatAssemblyEnd(*B,MAT_FINAL_ASSEMBLY);
233: if (*jac != *B) {
234: MatAssemblyBegin(*jac,MAT_FINAL_ASSEMBLY);
235: MatAssemblyEnd(*jac,MAT_FINAL_ASSEMBLY);
236: }
237: return 0;
238: }