Actual source code: ex25.c
1: /* $Id: ex18.c,v 1.23 2001/08/07 21:31:17 bsmith Exp $ */
4: static char help[] ="Minimum surface problemn
5: Uses 2-dimensional distributed arrays.n
6: n
7: Solves the linear systems via multilevel methods n
8: nn";
10: /*T
11: Concepts: SNES^solving a system of nonlinear equations
12: Concepts: DA^using distributed arrays
13: Concepts: multigrid;
14: Processors: n
15: T*/
17: /*
18:
19: This example models the partial differential equation
20:
21: - Div((1 + ||GRAD T||^2)^(1/2) (GRAD T)) = 0.
22:
23:
24: in the unit square, which is uniformly discretized in each of x and
25: y in this simple encoding. The degrees of freedom are vertex centered
26:
27: A finite difference approximation with the usual 5-point stencil
28: is used to discretize the boundary value problem to obtain a
29: nonlinear system of equations.
30:
31: */
33: #include petscsnes.h
34: #include petscda.h
35: #include petscmg.h
37: extern int FormFunction(SNES,Vec,Vec,void*);
38: extern int FormFunctionLocal(DALocalInfo*,PetscScalar**,PetscScalar**,void*);
40: #undef __FUNCT__
42: int main(int argc,char **argv)
43: {
44: DMMG *dmmg;
45: SNES snes;
46: int ierr,its,lits;
47: PetscReal litspit;
48: DA da;
50: PetscInitialize(&argc,&argv,PETSC_NULL,help);
53: /*
54: Create the multilevel DA data structure
55: */
56: DMMGCreate(PETSC_COMM_WORLD,3,0,&dmmg);
58: /*
59: Set the DA (grid structure) for the grids.
60: */
61: DACreate2d(PETSC_COMM_WORLD,DA_NONPERIODIC,DA_STENCIL_STAR,-5,-5,PETSC_DECIDE,PETSC_DECIDE,1,1,0,0,&da);
62: DMMGSetDM(dmmg,(DM)da);
63: DADestroy(da);
65: /*
66: Process adiC: FormFunctionLocal FormFunctionLocali
68: Create the nonlinear solver, and tell the DMMG structure to use it
69: */
70: /* DMMGSetSNES(dmmg,FormFunction,0); */
71: DMMGSetSNESLocal(dmmg,FormFunctionLocal,0,ad_FormFunctionLocal,0);
73: /*
74: PreLoadBegin() means that the following section of code is run twice. The first time
75: through the flag PreLoading is on this the nonlinear solver is only run for a single step.
76: The second time through (the actually timed code) the maximum iterations is set to 10
77: Preload of the executable is done to eliminate from the timing the time spent bring the
78: executable into memory from disk (paging in).
79: */
80: PreLoadBegin(PETSC_TRUE,"Solve");
81: DMMGSolve(dmmg);
82: PreLoadEnd();
83: snes = DMMGGetSNES(dmmg);
84: SNESGetIterationNumber(snes,&its);
85: SNESGetNumberLinearIterations(snes,&lits);
86: litspit = ((PetscReal)lits)/((PetscReal)its);
87: PetscPrintf(PETSC_COMM_WORLD,"Number of Newton iterations = %dn",its);
88: PetscPrintf(PETSC_COMM_WORLD,"Number of Linear iterations = %dn",lits);
89: PetscPrintf(PETSC_COMM_WORLD,"Average Linear its / Newton = %en",litspit);
91: DMMGDestroy(dmmg);
92: PetscFinalize();
94: return 0;
95: }
96: /* -------------------- Evaluate Function F(x) --------------------- */
97: #undef __FUNCT__
99: int FormFunction(SNES snes,Vec T,Vec F,void* ptr)
100: {
101: DMMG dmmg = (DMMG)ptr;
102: int ierr,i,j,mx,my,xs,ys,xm,ym;
103: PetscScalar hx,hy;
104: PetscScalar **t,**f,gradup,graddown,gradleft,gradright,gradx,grady;
105: PetscScalar coeffup,coeffdown,coeffleft,coeffright;
106: Vec localT;
109: DAGetLocalVector((DA)dmmg->dm,&localT);
110: DAGetInfo((DA)dmmg->dm,PETSC_NULL,&mx,&my,0,0,0,0,0,0,0,0);
111: hx = 1.0/(PetscReal)(mx-1); hy = 1.0/(PetscReal)(my-1);
112:
113: /* Get ghost points */
114: DAGlobalToLocalBegin((DA)dmmg->dm,T,INSERT_VALUES,localT);
115: DAGlobalToLocalEnd((DA)dmmg->dm,T,INSERT_VALUES,localT);
116: DAGetCorners((DA)dmmg->dm,&xs,&ys,0,&xm,&ym,0);
117: DAVecGetArray((DA)dmmg->dm,localT,(void**)&t);
118: DAVecGetArray((DA)dmmg->dm,F,(void**)&f);
120: /* Evaluate function */
121: for (j=ys; j<ys+ym; j++) {
122: for (i=xs; i<xs+xm; i++) {
124: if (i == 0 || i == mx-1 || j == 0 || j == my-1) {
126: f[j][i] = t[j][i] - (1.0 - (2.0*hx*(PetscReal)i - 1.0)*(2.0*hx*(PetscReal)i - 1.0));
127:
128: } else {
130: gradup = (t[j+1][i] - t[j][i])/hy;
131: graddown = (t[j][i] - t[j-1][i])/hy;
132: gradright = (t[j][i+1] - t[j][i])/hx;
133: gradleft = (t[j][i] - t[j][i-1])/hx;
135: gradx = .5*(t[j][i+1] - t[j][i-1])/hx;
136: grady = .5*(t[j+1][i] - t[j-1][i])/hy;
138: coeffup = 1.0/PetscSqrtScalar(1.0 + gradup*gradup + gradx*gradx);
139: coeffdown = 1.0/PetscSqrtScalar(1.0 + graddown*graddown + gradx*gradx);
141: coeffleft = 1.0/PetscSqrtScalar(1.0 + gradleft*gradleft + grady*grady);
142: coeffright = 1.0/PetscSqrtScalar(1.0 + gradright*gradright + grady*grady);
144: f[j][i] = (coeffup*gradup - coeffdown*graddown)*hx + (coeffright*gradright - coeffleft*gradleft)*hy;
145:
146: }
148: }
149: }
150: DAVecRestoreArray((DA)dmmg->dm,localT,(void**)&t);
151: DAVecRestoreArray((DA)dmmg->dm,F,(void**)&f);
152: DARestoreLocalVector((DA)dmmg->dm,&localT);
153: return(0);
154: }
156: int FormFunctionLocal(DALocalInfo *info,PetscScalar **t,PetscScalar **f,void *ptr)
157: {
158: int i,j;
159: PetscScalar hx,hy;
160: PetscScalar gradup,graddown,gradleft,gradright,gradx,grady;
161: PetscScalar coeffup,coeffdown,coeffleft,coeffright;
164: hx = 1.0/(PetscReal)(info->mx-1); hy = 1.0/(PetscReal)(info->my-1);
165:
166: /* Evaluate function */
167: for (j=info->ys; j<info->ys+info->ym; j++) {
168: for (i=info->xs; i<info->xs+info->xm; i++) {
170: if (i == 0 || i == info->mx-1 || j == 0 || j == info->my-1) {
172: f[j][i] = t[j][i] - (1.0 - (2.0*hx*(PetscReal)i - 1.0)*(2.0*hx*(PetscReal)i - 1.0));
173:
174: } else {
176: gradup = (t[j+1][i] - t[j][i])/hy;
177: graddown = (t[j][i] - t[j-1][i])/hy;
178: gradright = (t[j][i+1] - t[j][i])/hx;
179: gradleft = (t[j][i] - t[j][i-1])/hx;
181: gradx = .5*(t[j][i+1] - t[j][i-1])/hx;
182: grady = .5*(t[j+1][i] - t[j-1][i])/hy;
184: coeffup = 1.0/PetscSqrtScalar(1.0 + gradup*gradup + gradx*gradx);
185: coeffdown = 1.0/PetscSqrtScalar(1.0 + graddown*graddown + gradx*gradx);
187: coeffleft = 1.0/PetscSqrtScalar(1.0 + gradleft*gradleft + grady*grady);
188: coeffright = 1.0/PetscSqrtScalar(1.0 + gradright*gradright + grady*grady);
190: f[j][i] = (coeffup*gradup - coeffdown*graddown)*hx + (coeffright*gradright - coeffleft*gradleft)*hy;
191:
192: }
194: }
195: }
196: return(0);
197: }