Actual source code: ex30.c
petsc-dev 2014-02-02
1: static const char help[] = "Steady-state 2D subduction flow, pressure and temperature solver.\n\
2: The flow is driven by the subducting slab.\n\
3: ---------------------------------ex30 help---------------------------------\n\
4: -OPTION <DEFAULT> = (UNITS) DESCRIPTION.\n\n\
5: -width <320> = (km) width of domain.\n\
6: -depth <300> = (km) depth of domain.\n\
7: -slab_dip <45> = (degrees) dip angle of the slab (determines the grid aspect ratio).\n\
8: -lid_depth <35> = (km) depth of the static conductive lid.\n\
9: -fault_depth <35> = (km) depth of slab-wedge mechanical coupling\n\
10: (fault dept >= lid depth).\n\
11: \n\
12: -ni <82> = grid cells in x-direction. (nj adjusts to accommodate\n\
13: the slab dip & depth). DO NOT USE -da_grid_x option!!!\n\
14: -ivisc <3> = rheology option.\n\
15: 0 --- constant viscosity.\n\
16: 1 --- olivine diffusion creep rheology (T&P-dependent, newtonian).\n\
17: 2 --- olivine dislocation creep rheology (T&P-dependent, non-newtonian).\n\
18: 3 --- Full mantle rheology, combination of 1 & 2.\n\
19: \n\
20: -slab_velocity <5> = (cm/year) convergence rate of slab into subduction zone.\n\
21: -slab_age <50> = (million yrs) age of slab for thermal profile boundary condition.\n\
22: -lid_age <50> = (million yrs) age of lid for thermal profile boundary condition.\n\
23: \n\
24: FOR OTHER PARAMETER OPTIONS AND THEIR DEFAULT VALUES, see SetParams() in ex30.c.\n\
25: ---------------------------------ex30 help---------------------------------\n";
This PETSc 2.2.0 example by Richard F. Katz
http://www.ldeo.columbia.edu/~katz/
The problem is modeled by the partial differential equation system
\begin{eqnarray}
-\nabla P + \nabla \cdot [\eta (\nabla v + \nabla v^T)] & = & 0 \\
\nabla \cdot v & = & 0 \\
dT/dt + \nabla \cdot (vT) - 1/Pe \triangle^2(T) & = & 0 \\
\end{eqnarray}
\begin{eqnarray}
\eta(T,Eps\_dot) & = & \hbox{constant } \hbox{if ivisc} ==0 \\
& = & \hbox{diffusion creep (T,P-dependent) } \hbox{if ivisc} ==1 \\
& = & \hbox{dislocation creep (T,P,v-dependent)} \hbox{if ivisc} ==2 \\
& = & \hbox{mantle viscosity (difn and disl) } \hbox{if ivisc} ==3
\end{eqnarray}
which is uniformly discretized on a staggered mesh:
-------$w_{ij}$------
$u_{i-1j}$ $P,T_{ij}$ $u_{ij}$
------$w_{ij-1}$-----
55: #include <petscsnes.h>
56: #include <petscdmda.h>
58: #define VISC_CONST 0
59: #define VISC_DIFN 1
60: #define VISC_DISL 2
61: #define VISC_FULL 3
62: #define CELL_CENTER 0
63: #define CELL_CORNER 1
64: #define BC_ANALYTIC 0
65: #define BC_NOSTRESS 1
66: #define BC_EXPERMNT 2
67: #define ADVECT_FV 0
68: #define ADVECT_FROMM 1
69: #define PLATE_SLAB 0
70: #define PLATE_LID 1
71: #define EPS_ZERO 0.00000001
73: typedef struct { /* holds the variables to be solved for */
74: PetscScalar u,w,p,T;
75: } Field;
77: typedef struct { /* parameters needed to compute viscosity */
78: PetscReal A,n,Estar,Vstar;
79: } ViscParam;
81: typedef struct { /* physical and miscelaneous parameters */
82: PetscReal width, depth, scaled_width, scaled_depth, peclet, potentialT;
83: PetscReal slab_dip, slab_age, slab_velocity, kappa, z_scale;
84: PetscReal c, d, sb, cb, skt, visc_cutoff, lid_age, eta0, continuation;
85: PetscReal L, V, lid_depth, fault_depth;
86: ViscParam diffusion, dislocation;
87: PetscInt ivisc, adv_scheme, ibound, output_ivisc;
88: PetscBool quiet, param_test, output_to_file, pv_analytic;
89: PetscBool interrupted, stop_solve, toggle_kspmon, kspmon;
90: char filename[PETSC_MAX_PATH_LEN];
91: } Parameter;
93: typedef struct { /* grid parameters */
94: DMDABoundaryType bx,by;
95: DMDAStencilType stencil;
96: PetscInt corner,ni,nj,jlid,jfault,inose;
97: PetscInt dof,stencil_width,mglevels;
98: PetscReal dx,dz;
99: } GridInfo;
101: typedef struct { /* application context */
102: Vec x,Xguess;
103: Parameter *param;
104: GridInfo *grid;
105: } AppCtx;
107: /* Callback functions (static interface) */
108: extern PetscErrorCode FormFunctionLocal(DMDALocalInfo*,Field**,Field**,void*);
110: /* Main routines */
111: extern PetscErrorCode SetParams(Parameter*, GridInfo*);
112: extern PetscErrorCode ReportParams(Parameter*, GridInfo*);
113: extern PetscErrorCode Initialize(DM);
114: extern PetscErrorCode UpdateSolution(SNES,AppCtx*, PetscInt*);
115: extern PetscErrorCode DoOutput(SNES,PetscInt);
117: /* Post-processing & misc */
118: extern PetscErrorCode ViscosityField(DM,Vec,Vec);
119: extern PetscErrorCode StressField(DM);
120: extern PetscErrorCode SNESConverged_Interactive(SNES, PetscInt, PetscReal, PetscReal, PetscReal, SNESConvergedReason*, void*);
121: extern PetscErrorCode InteractiveHandler(int, void*);
122: extern PetscBool OptionsHasName(const char pre[],const char name[]);
124: /*-----------------------------------------------------------------------*/
127: int main(int argc,char **argv)
128: /*-----------------------------------------------------------------------*/
129: {
130: SNES snes;
131: AppCtx *user; /* user-defined work context */
132: Parameter param;
133: GridInfo grid;
134: PetscInt nits;
136: MPI_Comm comm;
137: DM da;
139: PetscInitialize(&argc,&argv,(char*)0,help);
140: PetscOptionsSetValue("-file","ex30_output");
141: PetscOptionsSetValue("-snes_monitor_short",NULL);
142: PetscOptionsSetValue("-snes_max_it","20");
143: PetscOptionsSetValue("-ksp_max_it","1500");
144: PetscOptionsSetValue("-ksp_gmres_restart","300");
145: PetscOptionsInsert(&argc,&argv,NULL);
147: comm = PETSC_COMM_WORLD;
149: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
150: Set up the problem parameters.
151: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
152: SetParams(¶m,&grid);
153: ReportParams(¶m,&grid);
155: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
156: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
157: SNESCreate(comm,&snes);
158: DMDACreate2d(comm,grid.bx,grid.by,grid.stencil,grid.ni,grid.nj,PETSC_DECIDE,PETSC_DECIDE,grid.dof,grid.stencil_width,0,0,&da);
159: SNESSetDM(snes,da);
160: DMDASetFieldName(da,0,"x-velocity");
161: DMDASetFieldName(da,1,"y-velocity");
162: DMDASetFieldName(da,2,"pressure");
163: DMDASetFieldName(da,3,"temperature");
166: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
167: Create user context, set problem data, create vector data structures.
168: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
169: PetscMalloc(sizeof(AppCtx),&user);
170: user->param = ¶m;
171: user->grid = &grid;
172: DMSetApplicationContext(da,user);
173: DMCreateGlobalVector(da,&(user->Xguess));
176: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
177: Set up the SNES solver with callback functions.
178: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
179: DMDASNESSetFunctionLocal(da,INSERT_VALUES,(PetscErrorCode (*)(DMDALocalInfo*,void*,void*,void*))FormFunctionLocal,(void*)user);
180: SNESSetFromOptions(snes);
183: SNESSetConvergenceTest(snes,SNESConverged_Interactive,(void*)user,NULL);
184: PetscPushSignalHandler(InteractiveHandler,(void*)user);
186: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
187: Initialize and solve the nonlinear system
188: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
189: Initialize(da);
190: UpdateSolution(snes,user,&nits);
192: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
193: Output variables.
194: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
195: DoOutput(snes,nits);
197: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
198: Free work space.
199: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
200: VecDestroy(&user->Xguess);
201: VecDestroy(&user->x);
202: PetscFree(user);
203: SNESDestroy(&snes);
204: DMDestroy(&da);
205: PetscPopSignalHandler();
206: PetscFinalize();
207: return 0;
208: }
210: /*=====================================================================
211: PETSc INTERACTION FUNCTIONS (initialize & call SNESSolve)
212: =====================================================================*/
214: /*---------------------------------------------------------------------*/
217: /* manages solve: adaptive continuation method */
218: PetscErrorCode UpdateSolution(SNES snes, AppCtx *user, PetscInt *nits)
219: {
220: KSP ksp;
221: PC pc;
222: SNESConvergedReason reason;
223: Parameter *param = user->param;
224: PetscReal cont_incr=0.3;
225: PetscInt its;
226: PetscErrorCode ierr;
227: PetscBool q = PETSC_FALSE;
228: DM dm;
231: SNESGetDM(snes,&dm);
232: DMCreateGlobalVector(dm,&user->x);
233: SNESGetKSP(snes,&ksp);
234: KSPGetPC(ksp, &pc);
235: KSPSetComputeSingularValues(ksp, PETSC_TRUE);
237: *nits=0;
239: /* Isoviscous solve */
240: if (param->ivisc == VISC_CONST && !param->stop_solve) {
241: param->ivisc = VISC_CONST;
243: SNESSolve(snes,0,user->x);
244: SNESGetConvergedReason(snes,&reason);
245: SNESGetIterationNumber(snes,&its);
246: *nits += its;
247: VecCopy(user->x,user->Xguess);
248: if (param->stop_solve) goto done;
249: }
251: /* Olivine diffusion creep */
252: if (param->ivisc >= VISC_DIFN && !param->stop_solve) {
253: if (!q) PetscPrintf(PETSC_COMM_WORLD,"Computing Variable Viscosity Solution\n");
255: /* continuation method on viscosity cutoff */
256: for (param->continuation=0.0;; param->continuation+=cont_incr) {
257: if (!q) PetscPrintf(PETSC_COMM_WORLD," Continuation parameter = %g\n", (double)param->continuation);
259: /* solve the non-linear system */
260: VecCopy(user->Xguess,user->x);
261: SNESSolve(snes,0,user->x);
262: SNESGetConvergedReason(snes,&reason);
263: SNESGetIterationNumber(snes,&its);
264: *nits += its;
265: if (!q) PetscPrintf(PETSC_COMM_WORLD," SNES iterations: %D, Cumulative: %D\n", its, *nits);
266: if (param->stop_solve) goto done;
268: if (reason<0) {
269: /* NOT converged */
270: cont_incr = -fabs(cont_incr)/2.0;
271: if (fabs(cont_incr)<0.01) goto done;
273: } else {
274: /* converged */
275: VecCopy(user->x,user->Xguess);
276: if (param->continuation >= 1.0) goto done;
277: if (its<=3) cont_incr = 0.30001;
278: else if (its<=8) cont_incr = 0.15001;
279: else cont_incr = 0.10001;
281: if (param->continuation+cont_incr > 1.0) cont_incr = 1.0 - param->continuation;
282: } /* endif reason<0 */
283: }
284: }
285: done:
286: if (param->stop_solve && !q) PetscPrintf(PETSC_COMM_WORLD,"USER SIGNAL: stopping solve.\n");
287: if (reason<0 && !q) PetscPrintf(PETSC_COMM_WORLD,"FAILED TO CONVERGE: stopping solve.\n");
288: return(0);
289: }
292: /*=====================================================================
293: PHYSICS FUNCTIONS (compute the discrete residual)
294: =====================================================================*/
297: /*---------------------------------------------------------------------*/
300: PETSC_STATIC_INLINE PetscScalar UInterp(Field **x, PetscInt i, PetscInt j)
301: /*---------------------------------------------------------------------*/
302: {
303: return 0.25*(x[j][i].u+x[j+1][i].u+x[j][i+1].u+x[j+1][i+1].u);
304: }
306: /*---------------------------------------------------------------------*/
309: PETSC_STATIC_INLINE PetscScalar WInterp(Field **x, PetscInt i, PetscInt j)
310: /*---------------------------------------------------------------------*/
311: {
312: return 0.25*(x[j][i].w+x[j+1][i].w+x[j][i+1].w+x[j+1][i+1].w);
313: }
315: /*---------------------------------------------------------------------*/
318: PETSC_STATIC_INLINE PetscScalar PInterp(Field **x, PetscInt i, PetscInt j)
319: /*---------------------------------------------------------------------*/
320: {
321: return 0.25*(x[j][i].p+x[j+1][i].p+x[j][i+1].p+x[j+1][i+1].p);
322: }
324: /*---------------------------------------------------------------------*/
327: PETSC_STATIC_INLINE PetscScalar TInterp(Field **x, PetscInt i, PetscInt j)
328: /*---------------------------------------------------------------------*/
329: {
330: return 0.25*(x[j][i].T+x[j+1][i].T+x[j][i+1].T+x[j+1][i+1].T);
331: }
333: /*---------------------------------------------------------------------*/
336: /* isoviscous analytic solution for IC */
337: PETSC_STATIC_INLINE PassiveScalar HorizVelocity(PetscInt i, PetscInt j, AppCtx *user)
338: /*---------------------------------------------------------------------*/
339: {
340: Parameter *param = user->param;
341: GridInfo *grid = user->grid;
342: PetscScalar st, ct, th, c=param->c, d=param->d;
343: PetscReal x, z,r;
345: x = (i - grid->jlid)*grid->dx; z = (j - grid->jlid - 0.5)*grid->dz;
346: r = PetscSqrtReal(x*x+z*z);
347: st = z/r;
348: ct = x/r;
349: th = atan(z/x);
350: return ct*(c*th*st+d*(st+th*ct)) + st*(c*(st-th*ct)+d*th*st);
351: }
353: /*---------------------------------------------------------------------*/
356: /* isoviscous analytic solution for IC */
357: PETSC_STATIC_INLINE PetscScalar VertVelocity(PetscInt i, PetscInt j, AppCtx *user)
358: /*---------------------------------------------------------------------*/
359: {
360: Parameter *param = user->param;
361: GridInfo *grid = user->grid;
362: PetscScalar st, ct, th, c=param->c, d=param->d;
363: PetscReal x, z, r;
365: x = (i - grid->jlid - 0.5)*grid->dx; z = (j - grid->jlid)*grid->dz;
366: r = PetscSqrtReal(x*x+z*z); st = z/r; ct = x/r; th = atan(z/x);
367: return st*(c*th*st+d*(st+th*ct)) - ct*(c*(st-th*ct)+d*th*st);
368: }
370: /*---------------------------------------------------------------------*/
373: /* isoviscous analytic solution for IC */
374: PETSC_STATIC_INLINE PetscScalar Pressure(PetscInt i, PetscInt j, AppCtx *user)
375: /*---------------------------------------------------------------------*/
376: {
377: Parameter *param = user->param;
378: GridInfo *grid = user->grid;
379: PetscScalar x, z, r, st, ct, c=param->c, d=param->d;
381: x = (i - grid->jlid - 0.5)*grid->dx; z = (j - grid->jlid - 0.5)*grid->dz;
382: r = PetscSqrtReal(x*x+z*z); st = z/r; ct = x/r;
383: return (-2.0*(c*ct-d*st)/r);
384: }
388: /* computes the second invariant of the strain rate tensor */
389: PETSC_STATIC_INLINE PetscScalar CalcSecInv(Field **x, PetscInt i, PetscInt j, PetscInt ipos, AppCtx *user)
390: /*---------------------------------------------------------------------*/
391: {
392: Parameter *param = user->param;
393: GridInfo *grid = user->grid;
394: PetscInt ilim =grid->ni-1, jlim=grid->nj-1;
395: PetscScalar uN,uS,uE,uW,wN,wS,wE,wW;
396: PetscScalar eps11, eps12, eps22;
398: if (i<j) return EPS_ZERO;
399: if (i==ilim) i--;
400: if (j==jlim) j--;
402: if (ipos==CELL_CENTER) { /* on cell center */
403: if (j<=grid->jlid) return EPS_ZERO;
405: uE = x[j][i].u; uW = x[j][i-1].u;
406: wN = x[j][i].w; wS = x[j-1][i].w;
407: wE = WInterp(x,i,j-1);
408: if (i==j) {
409: uN = param->cb; wW = param->sb;
410: } else {
411: uN = UInterp(x,i-1,j); wW = WInterp(x,i-1,j-1);
412: }
414: if (j==grid->jlid+1) uS = 0.0;
415: else uS = UInterp(x,i-1,j-1);
417: } else { /* on CELL_CORNER */
418: if (j<grid->jlid) return EPS_ZERO;
420: uN = x[j+1][i].u; uS = x[j][i].u;
421: wE = x[j][i+1].w; wW = x[j][i].w;
422: if (i==j) {
423: wN = param->sb;
424: uW = param->cb;
425: } else {
426: wN = WInterp(x,i,j);
427: uW = UInterp(x,i-1,j);
428: }
430: if (j==grid->jlid) {
431: uE = 0.0; uW = 0.0;
432: uS = -uN;
433: wS = -wN;
434: } else {
435: uE = UInterp(x,i,j);
436: wS = WInterp(x,i,j-1);
437: }
438: }
440: eps11 = (uE-uW)/grid->dx; eps22 = (wN-wS)/grid->dz;
441: eps12 = 0.5*((uN-uS)/grid->dz + (wE-wW)/grid->dx);
443: return PetscSqrtReal(0.5*(eps11*eps11 + 2.0*eps12*eps12 + eps22*eps22));
444: }
446: /*---------------------------------------------------------------------*/
449: /* computes the shear viscosity */
450: PETSC_STATIC_INLINE PetscScalar Viscosity(PetscScalar T, PetscScalar eps, PassiveScalar z, Parameter *param)
451: /*---------------------------------------------------------------------*/
452: {
453: PetscReal result =0.0;
454: ViscParam difn =param->diffusion, disl=param->dislocation;
455: PetscInt iVisc =param->ivisc;
456: PetscScalar eps_scale=param->V/(param->L*1000.0);
457: PetscScalar strain_power, v1, v2, P;
458: PetscScalar rho_g = 32340.0, R=8.3144;
460: P = rho_g*(z*param->L*1000.0); /* Pa */
462: if (iVisc==VISC_CONST) {
463: /* constant viscosity */
464: return 1.0;
465: } else if (iVisc==VISC_DIFN) {
466: /* diffusion creep rheology */
467: result = PetscRealPart((difn.A*PetscExpScalar((difn.Estar + P*difn.Vstar)/R/(T+273.0))/param->eta0));
468: } else if (iVisc==VISC_DISL) {
469: /* dislocation creep rheology */
470: strain_power = PetscPowScalar(eps*eps_scale, (1.0-disl.n)/disl.n);
472: result = PetscRealPart(disl.A*PetscExpScalar((disl.Estar + P*disl.Vstar)/disl.n/R/(T+273.0))*strain_power/param->eta0);
473: } else if (iVisc==VISC_FULL) {
474: /* dislocation/diffusion creep rheology */
475: strain_power = PetscPowScalar(eps*eps_scale, (1.0-disl.n)/disl.n);
477: v1 = difn.A*PetscExpScalar((difn.Estar + P*difn.Vstar)/R/(T+273.0))/param->eta0;
478: v2 = disl.A*PetscExpScalar((disl.Estar + P*disl.Vstar)/disl.n/R/(T+273.0))*strain_power/param->eta0;
480: result = PetscRealPart(1.0/(1.0/v1 + 1.0/v2));
481: }
483: /* max viscosity is param->eta0 */
484: result = PetscMin(result, 1.0);
485: /* min viscosity is param->visc_cutoff */
486: result = PetscMax(result, param->visc_cutoff);
487: /* continuation method */
488: result = PetscPowReal(result,param->continuation);
489: return result;
490: }
492: /*---------------------------------------------------------------------*/
495: /* computes the residual of the x-component of eqn (1) above */
496: PETSC_STATIC_INLINE PetscScalar XMomentumResidual(Field **x, PetscInt i, PetscInt j, AppCtx *user)
497: /*---------------------------------------------------------------------*/
498: {
499: Parameter *param=user->param;
500: GridInfo *grid =user->grid;
501: PetscScalar dx = grid->dx, dz=grid->dz;
502: PetscScalar etaN,etaS,etaE,etaW,epsN=0.0,epsS=0.0,epsE=0.0,epsW=0.0;
503: PetscScalar TE=0.0,TN=0.0,TS=0.0,TW=0.0, dPdx, residual, z_scale;
504: PetscScalar dudxW,dudxE,dudzN,dudzS,dwdxN,dwdxS;
505: PetscInt jlim = grid->nj-1;
507: z_scale = param->z_scale;
509: if (param->ivisc==VISC_DIFN || param->ivisc>=VISC_DISL) { /* viscosity is T-dependent */
510: TS = param->potentialT * TInterp(x,i,j-1) * PetscExpScalar((j-1.0)*dz*z_scale);
511: if (j==jlim) TN = TS;
512: else TN = param->potentialT * TInterp(x,i,j) * PetscExpScalar(j*dz*z_scale);
513: TW = param->potentialT * x[j][i].T * PetscExpScalar((j-0.5)*dz*z_scale);
514: TE = param->potentialT * x[j][i+1].T * PetscExpScalar((j-0.5)*dz*z_scale);
515: if (param->ivisc>=VISC_DISL) { /* olivine dislocation creep */
516: epsN = CalcSecInv(x,i,j, CELL_CORNER,user);
517: epsS = CalcSecInv(x,i,j-1,CELL_CORNER,user);
518: epsE = CalcSecInv(x,i+1,j,CELL_CENTER,user);
519: epsW = CalcSecInv(x,i,j, CELL_CENTER,user);
520: }
521: }
522: etaN = Viscosity(TN,epsN,dz*(j+0.5),param);
523: etaS = Viscosity(TS,epsS,dz*(j-0.5),param);
524: etaW = Viscosity(TW,epsW,dz*j,param);
525: etaE = Viscosity(TE,epsE,dz*j,param);
527: dPdx = (x[j][i+1].p - x[j][i].p)/dx;
528: if (j==jlim) dudzN = etaN * (x[j][i].w - x[j][i+1].w)/dx;
529: else dudzN = etaN * (x[j+1][i].u - x[j][i].u) /dz;
530: dudzS = etaS * (x[j][i].u - x[j-1][i].u)/dz;
531: dudxE = etaE * (x[j][i+1].u - x[j][i].u) /dx;
532: dudxW = etaW * (x[j][i].u - x[j][i-1].u)/dx;
534: residual = -dPdx /* X-MOMENTUM EQUATION*/
535: +(dudxE - dudxW)/dx
536: +(dudzN - dudzS)/dz;
538: if (param->ivisc!=VISC_CONST) {
539: dwdxN = etaN * (x[j][i+1].w - x[j][i].w) /dx;
540: dwdxS = etaS * (x[j-1][i+1].w - x[j-1][i].w)/dx;
542: residual += (dudxE - dudxW)/dx + (dwdxN - dwdxS)/dz;
543: }
545: return residual;
546: }
548: /*---------------------------------------------------------------------*/
551: /* computes the residual of the z-component of eqn (1) above */
552: PETSC_STATIC_INLINE PetscScalar ZMomentumResidual(Field **x, PetscInt i, PetscInt j, AppCtx *user)
553: /*---------------------------------------------------------------------*/
554: {
555: Parameter *param=user->param;
556: GridInfo *grid =user->grid;
557: PetscScalar dx = grid->dx, dz=grid->dz;
558: PetscScalar etaN =0.0,etaS=0.0,etaE=0.0,etaW=0.0,epsN=0.0,epsS=0.0,epsE=0.0,epsW=0.0;
559: PetscScalar TE =0.0,TN=0.0,TS=0.0,TW=0.0, dPdz, residual,z_scale;
560: PetscScalar dudzE,dudzW,dwdxW,dwdxE,dwdzN,dwdzS;
561: PetscInt ilim = grid->ni-1;
563: /* geometric and other parameters */
564: z_scale = param->z_scale;
566: /* viscosity */
567: if (param->ivisc==VISC_DIFN || param->ivisc>=VISC_DISL) { /* viscosity is T-dependent */
568: TN = param->potentialT * x[j+1][i].T * PetscExpScalar((j+0.5)*dz*z_scale);
569: TS = param->potentialT * x[j][i].T * PetscExpScalar((j-0.5)*dz*z_scale);
570: TW = param->potentialT * TInterp(x,i-1,j) * PetscExpScalar(j*dz*z_scale);
571: if (i==ilim) TE = TW;
572: else TE = param->potentialT * TInterp(x,i,j) * PetscExpScalar(j*dz*z_scale);
573: if (param->ivisc>=VISC_DISL) { /* olivine dislocation creep */
574: epsN = CalcSecInv(x,i,j+1,CELL_CENTER,user);
575: epsS = CalcSecInv(x,i,j, CELL_CENTER,user);
576: epsE = CalcSecInv(x,i,j, CELL_CORNER,user);
577: epsW = CalcSecInv(x,i-1,j,CELL_CORNER,user);
578: }
579: }
580: etaN = Viscosity(TN,epsN,dz*(j+1.0),param);
581: etaS = Viscosity(TS,epsS,dz*(j+0.0),param);
582: etaW = Viscosity(TW,epsW,dz*(j+0.5),param);
583: etaE = Viscosity(TE,epsE,dz*(j+0.5),param);
585: dPdz = (x[j+1][i].p - x[j][i].p)/dz;
586: dwdzN = etaN * (x[j+1][i].w - x[j][i].w)/dz;
587: dwdzS = etaS * (x[j][i].w - x[j-1][i].w)/dz;
588: if (i==ilim) dwdxE = etaE * (x[j][i].u - x[j+1][i].u)/dz;
589: else dwdxE = etaE * (x[j][i+1].w - x[j][i].w) /dx;
590: dwdxW = 2.0*etaW * (x[j][i].w - x[j][i-1].w)/dx;
592: /* Z-MOMENTUM */
593: residual = -dPdz /* constant viscosity terms */
594: +(dwdzN - dwdzS)/dz
595: +(dwdxE - dwdxW)/dx;
597: if (param->ivisc!=VISC_CONST) {
598: dudzE = etaE * (x[j+1][i].u - x[j][i].u)/dz;
599: dudzW = etaW * (x[j+1][i-1].u - x[j][i-1].u)/dz;
601: residual += (dwdzN - dwdzS)/dz + (dudzE - dudzW)/dx;
602: }
604: return residual;
605: }
607: /*---------------------------------------------------------------------*/
610: /* computes the residual of eqn (2) above */
611: PETSC_STATIC_INLINE PetscScalar ContinuityResidual(Field **x, PetscInt i, PetscInt j, AppCtx *user)
612: /*---------------------------------------------------------------------*/
613: {
614: GridInfo *grid =user->grid;
615: PetscScalar uE,uW,wN,wS,dudx,dwdz;
617: uW = x[j][i-1].u; uE = x[j][i].u; dudx = (uE - uW)/grid->dx;
618: wS = x[j-1][i].w; wN = x[j][i].w; dwdz = (wN - wS)/grid->dz;
620: return dudx + dwdz;
621: }
623: /*---------------------------------------------------------------------*/
626: /* computes the residual of eqn (3) above */
627: PETSC_STATIC_INLINE PetscScalar EnergyResidual(Field **x, PetscInt i, PetscInt j, AppCtx *user)
628: /*---------------------------------------------------------------------*/
629: {
630: Parameter *param=user->param;
631: GridInfo *grid =user->grid;
632: PetscScalar dx = grid->dx, dz=grid->dz;
633: PetscInt ilim =grid->ni-1, jlim=grid->nj-1, jlid=grid->jlid;
634: PetscScalar TE, TN, TS, TW, residual;
635: PetscScalar uE,uW,wN,wS;
636: PetscScalar fN,fS,fE,fW,dTdxW,dTdxE,dTdzN,dTdzS;
638: dTdzN = (x[j+1][i].T - x[j][i].T) /dz;
639: dTdzS = (x[j][i].T - x[j-1][i].T)/dz;
640: dTdxE = (x[j][i+1].T - x[j][i].T) /dx;
641: dTdxW = (x[j][i].T - x[j][i-1].T)/dx;
643: residual = ((dTdzN - dTdzS)/dz + /* diffusion term */
644: (dTdxE - dTdxW)/dx)*dx*dz/param->peclet;
646: if (j<=jlid && i>=j) {
647: /* don't advect in the lid */
648: return residual;
649: } else if (i<j) {
650: /* beneath the slab sfc */
651: uW = uE = param->cb;
652: wS = wN = param->sb;
653: } else {
654: /* advect in the slab and wedge */
655: uW = x[j][i-1].u; uE = x[j][i].u;
656: wS = x[j-1][i].w; wN = x[j][i].w;
657: }
659: if (param->adv_scheme==ADVECT_FV || i==ilim-1 || j==jlim-1 || i==1 || j==1) {
660: /* finite volume advection */
661: TS = (x[j][i].T + x[j-1][i].T)/2.0;
662: TN = (x[j][i].T + x[j+1][i].T)/2.0;
663: TE = (x[j][i].T + x[j][i+1].T)/2.0;
664: TW = (x[j][i].T + x[j][i-1].T)/2.0;
665: fN = wN*TN*dx; fS = wS*TS*dx;
666: fE = uE*TE*dz; fW = uW*TW*dz;
668: } else {
669: /* Fromm advection scheme */
670: fE = (uE *(-x[j][i+2].T + 5.0*(x[j][i+1].T+x[j][i].T)-x[j][i-1].T)/8.0
671: - PetscAbsScalar(uE)*(-x[j][i+2].T + 3.0*(x[j][i+1].T-x[j][i].T)+x[j][i-1].T)/8.0)*dz;
672: fW = (uW *(-x[j][i+1].T + 5.0*(x[j][i].T+x[j][i-1].T)-x[j][i-2].T)/8.0
673: - PetscAbsScalar(uW)*(-x[j][i+1].T + 3.0*(x[j][i].T-x[j][i-1].T)+x[j][i-2].T)/8.0)*dz;
674: fN = (wN *(-x[j+2][i].T + 5.0*(x[j+1][i].T+x[j][i].T)-x[j-1][i].T)/8.0
675: - PetscAbsScalar(wN)*(-x[j+2][i].T + 3.0*(x[j+1][i].T-x[j][i].T)+x[j-1][i].T)/8.0)*dx;
676: fS = (wS *(-x[j+1][i].T + 5.0*(x[j][i].T+x[j-1][i].T)-x[j-2][i].T)/8.0
677: - PetscAbsScalar(wS)*(-x[j+1][i].T + 3.0*(x[j][i].T-x[j-1][i].T)+x[j-2][i].T)/8.0)*dx;
678: }
680: residual -= (fE - fW + fN - fS);
682: return residual;
683: }
685: /*---------------------------------------------------------------------*/
688: /* computes the shear stress---used on the boundaries */
689: PETSC_STATIC_INLINE PetscScalar ShearStress(Field **x, PetscInt i, PetscInt j, PetscInt ipos, AppCtx *user)
690: /*---------------------------------------------------------------------*/
691: {
692: Parameter *param=user->param;
693: GridInfo *grid =user->grid;
694: PetscInt ilim =grid->ni-1, jlim=grid->nj-1;
695: PetscScalar uN, uS, wE, wW;
697: if (j<=grid->jlid || i<j || i==ilim || j==jlim) return EPS_ZERO;
699: if (ipos==CELL_CENTER) { /* on cell center */
701: wE = WInterp(x,i,j-1);
702: if (i==j) {
703: wW = param->sb;
704: uN = param->cb;
705: } else {
706: wW = WInterp(x,i-1,j-1);
707: uN = UInterp(x,i-1,j);
708: }
709: if (j==grid->jlid+1) uS = 0.0;
710: else uS = UInterp(x,i-1,j-1);
712: } else { /* on cell corner */
714: uN = x[j+1][i].u; uS = x[j][i].u;
715: wW = x[j][i].w; wE = x[j][i+1].w;
717: }
719: return (uN-uS)/grid->dz + (wE-wW)/grid->dx;
720: }
722: /*---------------------------------------------------------------------*/
725: /* computes the normal stress---used on the boundaries */
726: PETSC_STATIC_INLINE PetscScalar XNormalStress(Field **x, PetscInt i, PetscInt j, PetscInt ipos, AppCtx *user)
727: /*---------------------------------------------------------------------*/
728: {
729: Parameter *param=user->param;
730: GridInfo *grid =user->grid;
731: PetscScalar dx = grid->dx, dz=grid->dz;
732: PetscInt ilim =grid->ni-1, jlim=grid->nj-1, ivisc;
733: PetscScalar epsC =0.0, etaC, TC, uE, uW, pC, z_scale;
734: if (i<j || j<=grid->jlid) return EPS_ZERO;
736: ivisc=param->ivisc; z_scale = param->z_scale;
738: if (ipos==CELL_CENTER) { /* on cell center */
740: TC = param->potentialT * x[j][i].T * PetscExpScalar((j-0.5)*dz*z_scale);
741: if (ivisc>=VISC_DISL) epsC = CalcSecInv(x,i,j,CELL_CENTER,user);
742: etaC = Viscosity(TC,epsC,dz*j,param);
744: uW = x[j][i-1].u; uE = x[j][i].u;
745: pC = x[j][i].p;
747: } else { /* on cell corner */
748: if (i==ilim || j==jlim) return EPS_ZERO;
750: TC = param->potentialT * TInterp(x,i,j) * PetscExpScalar(j*dz*z_scale);
751: if (ivisc>=VISC_DISL) epsC = CalcSecInv(x,i,j,CELL_CORNER,user);
752: etaC = Viscosity(TC,epsC,dz*(j+0.5),param);
754: if (i==j) uW = param->sb;
755: else uW = UInterp(x,i-1,j);
756: uE = UInterp(x,i,j); pC = PInterp(x,i,j);
757: }
759: return 2.0*etaC*(uE-uW)/dx - pC;
760: }
762: /*---------------------------------------------------------------------*/
765: /* computes the normal stress---used on the boundaries */
766: PETSC_STATIC_INLINE PetscScalar ZNormalStress(Field **x, PetscInt i, PetscInt j, PetscInt ipos, AppCtx *user)
767: /*---------------------------------------------------------------------*/
768: {
769: Parameter *param=user->param;
770: GridInfo *grid =user->grid;
771: PetscScalar dz =grid->dz;
772: PetscInt ilim =grid->ni-1, jlim=grid->nj-1, ivisc;
773: PetscScalar epsC =0.0, etaC, TC;
774: PetscScalar pC, wN, wS, z_scale;
775: if (i<j || j<=grid->jlid) return EPS_ZERO;
777: ivisc=param->ivisc; z_scale = param->z_scale;
779: if (ipos==CELL_CENTER) { /* on cell center */
781: TC = param->potentialT * x[j][i].T * PetscExpScalar((j-0.5)*dz*z_scale);
782: if (ivisc>=VISC_DISL) epsC = CalcSecInv(x,i,j,CELL_CENTER,user);
783: etaC = Viscosity(TC,epsC,dz*j,param);
784: wN = x[j][i].w; wS = x[j-1][i].w; pC = x[j][i].p;
786: } else { /* on cell corner */
787: if ((i==ilim) || (j==jlim)) return EPS_ZERO;
789: TC = param->potentialT * TInterp(x,i,j) * PetscExpScalar(j*dz*z_scale);
790: if (ivisc>=VISC_DISL) epsC = CalcSecInv(x,i,j,CELL_CORNER,user);
791: etaC = Viscosity(TC,epsC,dz*(j+0.5),param);
792: if (i==j) wN = param->sb;
793: else wN = WInterp(x,i,j);
794: wS = WInterp(x,i,j-1); pC = PInterp(x,i,j);
795: }
797: return 2.0*etaC*(wN-wS)/dz - pC;
798: }
800: /*---------------------------------------------------------------------*/
802: /*=====================================================================
803: INITIALIZATION, POST-PROCESSING AND OUTPUT FUNCTIONS
804: =====================================================================*/
806: /*---------------------------------------------------------------------*/
809: /* initializes the problem parameters and checks for
810: command line changes */
811: PetscErrorCode SetParams(Parameter *param, GridInfo *grid)
812: /*---------------------------------------------------------------------*/
813: {
814: PetscErrorCode ierr, ierr_out=0;
815: PetscReal SEC_PER_YR = 3600.00*24.00*365.2500;
816: PetscReal alpha_g_on_cp_units_inverse_km=4.0e-5*9.8;
818: /* domain geometry */
819: param->slab_dip = 45.0;
820: param->width = 320.0; /* km */
821: param->depth = 300.0; /* km */
822: param->lid_depth = 35.0; /* km */
823: param->fault_depth = 35.0; /* km */
825: PetscOptionsGetReal(NULL,"-slab_dip",&(param->slab_dip),NULL);
826: PetscOptionsGetReal(NULL,"-width",&(param->width),NULL);
827: PetscOptionsGetReal(NULL,"-depth",&(param->depth),NULL);
828: PetscOptionsGetReal(NULL,"-lid_depth",&(param->lid_depth),NULL);
829: PetscOptionsGetReal(NULL,"-fault_depth",&(param->fault_depth),NULL);
831: param->slab_dip = param->slab_dip*PETSC_PI/180.0; /* radians */
833: /* grid information */
834: PetscOptionsGetInt(NULL, "-jfault",&(grid->jfault),NULL);
835: grid->ni = 82;
836: PetscOptionsGetInt(NULL, "-ni",&(grid->ni),NULL);
838: grid->dx = param->width/((PetscReal)(grid->ni-2)); /* km */
839: grid->dz = grid->dx*tan(param->slab_dip); /* km */
840: grid->nj = (PetscInt)(param->depth/grid->dz + 3.0); /* gridpoints*/
841: param->depth = grid->dz*(grid->nj-2); /* km */
842: grid->inose = 0; /* gridpoints*/
843: PetscOptionsGetInt(NULL,"-inose",&(grid->inose),NULL);
844: grid->bx = DMDA_BOUNDARY_NONE;
845: grid->by = DMDA_BOUNDARY_NONE;
846: grid->stencil = DMDA_STENCIL_BOX;
847: grid->dof = 4;
848: grid->stencil_width = 2;
849: grid->mglevels = 1;
851: /* boundary conditions */
852: param->pv_analytic = PETSC_FALSE;
853: param->ibound = BC_NOSTRESS;
854: PetscOptionsGetInt(NULL,"-ibound",&(param->ibound),NULL);
856: /* physical constants */
857: param->slab_velocity = 5.0; /* cm/yr */
858: param->slab_age = 50.0; /* Ma */
859: param->lid_age = 50.0; /* Ma */
860: param->kappa = 0.7272e-6; /* m^2/sec */
861: param->potentialT = 1300.0; /* degrees C */
863: PetscOptionsGetReal(NULL,"-slab_velocity",&(param->slab_velocity),NULL);
864: PetscOptionsGetReal(NULL,"-slab_age",&(param->slab_age),NULL);
865: PetscOptionsGetReal(NULL,"-lid_age",&(param->lid_age),NULL);
866: PetscOptionsGetReal(NULL,"-kappa",&(param->kappa),NULL);
867: PetscOptionsGetReal(NULL,"-potentialT",&(param->potentialT),NULL);
869: /* viscosity */
870: param->ivisc = 3; /* 0=isovisc, 1=difn creep, 2=disl creep, 3=full */
871: param->eta0 = 1e24; /* Pa-s */
872: param->visc_cutoff = 0.0; /* factor of eta_0 */
873: param->continuation = 1.0;
875: /* constants for diffusion creep */
876: param->diffusion.A = 1.8e7; /* Pa-s */
877: param->diffusion.n = 1.0; /* dim'less */
878: param->diffusion.Estar = 375e3; /* J/mol */
879: param->diffusion.Vstar = 5e-6; /* m^3/mol */
881: /* constants for param->dislocationocation creep */
882: param->dislocation.A = 2.8969e4; /* Pa-s */
883: param->dislocation.n = 3.5; /* dim'less */
884: param->dislocation.Estar = 530e3; /* J/mol */
885: param->dislocation.Vstar = 14e-6; /* m^3/mol */
887: PetscOptionsGetInt(NULL, "-ivisc",&(param->ivisc),NULL);
888: PetscOptionsGetReal(NULL,"-visc_cutoff",&(param->visc_cutoff),NULL);
890: param->output_ivisc = param->ivisc;
892: PetscOptionsGetInt(NULL,"-output_ivisc",&(param->output_ivisc),NULL);
893: PetscOptionsGetReal(NULL,"-vstar",&(param->dislocation.Vstar),NULL);
895: /* output options */
896: param->quiet = PETSC_FALSE;
897: param->param_test = PETSC_FALSE;
899: PetscOptionsHasName(NULL,"-quiet",&(param->quiet));
900: PetscOptionsHasName(NULL,"-test",&(param->param_test));
901: PetscOptionsGetString(NULL,"-file",param->filename,PETSC_MAX_PATH_LEN,&(param->output_to_file));
903: /* advection */
904: param->adv_scheme = ADVECT_FROMM; /* advection scheme: 0=finite vol, 1=Fromm */
906: PetscOptionsGetInt(NULL,"-adv_scheme",&(param->adv_scheme),NULL);
908: /* misc. flags */
909: param->stop_solve = PETSC_FALSE;
910: param->interrupted = PETSC_FALSE;
911: param->kspmon = PETSC_FALSE;
912: param->toggle_kspmon = PETSC_FALSE;
914: /* derived parameters for slab angle */
915: param->sb = PetscSinReal(param->slab_dip);
916: param->cb = PetscCosReal(param->slab_dip);
917: param->c = param->slab_dip*param->sb/(param->slab_dip*param->slab_dip-param->sb*param->sb);
918: param->d = (param->slab_dip*param->cb-param->sb)/(param->slab_dip*param->slab_dip-param->sb*param->sb);
920: /* length, velocity and time scale for non-dimensionalization */
921: param->L = PetscMin(param->width,param->depth); /* km */
922: param->V = param->slab_velocity/100.0/SEC_PER_YR; /* m/sec */
924: /* other unit conversions and derived parameters */
925: param->scaled_width = param->width/param->L; /* dim'less */
926: param->scaled_depth = param->depth/param->L; /* dim'less */
927: param->lid_depth = param->lid_depth/param->L; /* dim'less */
928: param->fault_depth = param->fault_depth/param->L; /* dim'less */
929: grid->dx = grid->dx/param->L; /* dim'less */
930: grid->dz = grid->dz/param->L; /* dim'less */
931: grid->jlid = (PetscInt)(param->lid_depth/grid->dz); /* gridcells */
932: grid->jfault = (PetscInt)(param->fault_depth/grid->dz); /* gridcells */
933: param->lid_depth = grid->jlid*grid->dz; /* dim'less */
934: param->fault_depth = grid->jfault*grid->dz; /* dim'less */
935: grid->corner = grid->jlid+1; /* gridcells */
936: param->peclet = param->V /* m/sec */
937: * param->L*1000.0 /* m */
938: / param->kappa; /* m^2/sec */
939: param->z_scale = param->L * alpha_g_on_cp_units_inverse_km;
940: param->skt = PetscSqrtReal(param->kappa*param->slab_age*SEC_PER_YR);
941: PetscOptionsGetReal(NULL,"-peclet",&(param->peclet),NULL);
943: return ierr_out;
944: }
946: /*---------------------------------------------------------------------*/
949: /* prints a report of the problem parameters to stdout */
950: PetscErrorCode ReportParams(Parameter *param, GridInfo *grid)
951: /*---------------------------------------------------------------------*/
952: {
953: PetscErrorCode ierr, ierr_out=0;
954: char date[30];
956: PetscGetDate(date,30);
958: if (!(param->quiet)) {
959: PetscPrintf(PETSC_COMM_WORLD,"---------------------BEGIN ex30 PARAM REPORT-------------------\n");
960: /* PetscPrintf(PETSC_COMM_WORLD," %s\n",&(date[0]));*/
962: PetscPrintf(PETSC_COMM_WORLD,"Domain: \n");
963: PetscPrintf(PETSC_COMM_WORLD," Width = %g km, Depth = %g km\n",(double)param->width,(double)param->depth);
964: PetscPrintf(PETSC_COMM_WORLD," Slab dip = %g degrees, Slab velocity = %g cm/yr\n",(double)(param->slab_dip*180.0/PETSC_PI),(double)param->slab_velocity);
965: PetscPrintf(PETSC_COMM_WORLD," Lid depth = %5.2f km, Fault depth = %5.2f km\n",param->lid_depth*param->L,param->fault_depth*param->L);
967: PetscPrintf(PETSC_COMM_WORLD,"\nGrid: \n");
968: PetscPrintf(PETSC_COMM_WORLD," [ni,nj] = %D, %D [dx,dz] = %g, %g km\n",grid->ni,grid->nj,(double)grid->dx*param->L,(double)(grid->dz*param->L));
969: PetscPrintf(PETSC_COMM_WORLD," jlid = %3D jfault = %3D \n",grid->jlid,grid->jfault);
970: PetscPrintf(PETSC_COMM_WORLD," Pe = %g\n",(double)param->peclet);
972: PetscPrintf(PETSC_COMM_WORLD,"\nRheology:");
973: if (param->ivisc==VISC_CONST) {
974: PetscPrintf(PETSC_COMM_WORLD," Isoviscous \n");
975: if (param->pv_analytic) {
976: PetscPrintf(PETSC_COMM_WORLD," Pressure and Velocity prescribed! \n");
977: }
978: } else if (param->ivisc==VISC_DIFN) {
979: PetscPrintf(PETSC_COMM_WORLD," Diffusion Creep (T-Dependent Newtonian) \n");
980: PetscPrintf(PETSC_COMM_WORLD," Viscosity range: %g--%g Pa-sec \n",(double)param->eta0,(double)(param->visc_cutoff*param->eta0));
981: } else if (param->ivisc==VISC_DISL) {
982: PetscPrintf(PETSC_COMM_WORLD," Dislocation Creep (T-Dependent Non-Newtonian) \n");
983: PetscPrintf(PETSC_COMM_WORLD," Viscosity range: %g--%g Pa-sec \n",(double)param->eta0,(double)(param->visc_cutoff*param->eta0));
984: } else if (param->ivisc==VISC_FULL) {
985: PetscPrintf(PETSC_COMM_WORLD," Full Rheology \n");
986: PetscPrintf(PETSC_COMM_WORLD," Viscosity range: %g--%g Pa-sec \n",(double)param->eta0,(double)(param->visc_cutoff*param->eta0));
987: } else {
988: PetscPrintf(PETSC_COMM_WORLD," Invalid! \n");
989: ierr_out = 1;
990: }
992: PetscPrintf(PETSC_COMM_WORLD,"Boundary condition:");
993: if (param->ibound==BC_ANALYTIC) {
994: PetscPrintf(PETSC_COMM_WORLD," Isoviscous Analytic Dirichlet \n");
995: } else if (param->ibound==BC_NOSTRESS) {
996: PetscPrintf(PETSC_COMM_WORLD," Stress-Free (normal & shear stress)\n");
997: } else if (param->ibound==BC_EXPERMNT) {
998: PetscPrintf(PETSC_COMM_WORLD," Experimental boundary condition \n");
999: } else {
1000: PetscPrintf(PETSC_COMM_WORLD," Invalid! \n");
1001: ierr_out = 1;
1002: }
1004: if (param->output_to_file)
1005: #if defined(PETSC_HAVE_MATLAB_ENGINE)
1006: PetscPrintf(PETSC_COMM_WORLD,"Output Destination: Mat file \"%s\"\n",param->filename);
1007: #else
1008: PetscPrintf(PETSC_COMM_WORLD,"Output Destination: PETSc binary file \"%s\"\n",param->filename);
1009: #endif
1010: if (param->output_ivisc != param->ivisc) {
1011: PetscPrintf(PETSC_COMM_WORLD," Output viscosity: -ivisc %D\n",param->output_ivisc);
1012: }
1014: PetscPrintf(PETSC_COMM_WORLD,"---------------------END ex30 PARAM REPORT---------------------\n");
1015: }
1016: if (param->param_test) PetscEnd();
1017: return ierr_out;
1018: }
1020: /* ------------------------------------------------------------------- */
1023: /* generates an inital guess using the analytic solution for isoviscous
1024: corner flow */
1025: PetscErrorCode Initialize(DM da)
1026: /* ------------------------------------------------------------------- */
1027: {
1028: AppCtx *user;
1029: Parameter *param;
1030: GridInfo *grid;
1031: PetscInt i,j,is,js,im,jm;
1033: Field **x;
1034: Vec Xguess;
1036: /* Get the fine grid */
1037: DMGetApplicationContext(da,&user);
1038: Xguess = user->Xguess;
1039: param = user->param;
1040: grid = user->grid;
1041: DMDAGetCorners(da,&is,&js,NULL,&im,&jm,NULL);
1042: DMDAVecGetArray(da,Xguess,(void**)&x);
1044: /* Compute initial guess */
1045: for (j=js; j<js+jm; j++) {
1046: for (i=is; i<is+im; i++) {
1047: if (i<j) x[j][i].u = param->cb;
1048: else if (j<=grid->jlid) x[j][i].u = 0.0;
1049: else x[j][i].u = HorizVelocity(i,j,user);
1051: if (i<=j) x[j][i].w = param->sb;
1052: else if (j<=grid->jlid) x[j][i].w = 0.0;
1053: else x[j][i].w = VertVelocity(i,j,user);
1055: if (i<j || j<=grid->jlid) x[j][i].p = 0.0;
1056: else x[j][i].p = Pressure(i,j,user);
1058: x[j][i].T = PetscMin(grid->dz*(j-0.5),1.0);
1059: }
1060: }
1062: /* Restore x to Xguess */
1063: DMDAVecRestoreArray(da,Xguess,(void**)&x);
1065: return 0;
1066: }
1068: /*---------------------------------------------------------------------*/
1071: /* controls output to a file */
1072: PetscErrorCode DoOutput(SNES snes, PetscInt its)
1073: /*---------------------------------------------------------------------*/
1074: {
1075: AppCtx *user;
1076: Parameter *param;
1077: GridInfo *grid;
1078: PetscInt ivt;
1080: PetscMPIInt rank;
1081: PetscViewer viewer;
1082: Vec res, pars;
1083: MPI_Comm comm;
1084: DM da;
1086: SNESGetDM(snes,&da);
1087: DMGetApplicationContext(da,&user);
1088: param = user->param;
1089: grid = user->grid;
1090: ivt = param->ivisc;
1092: param->ivisc = param->output_ivisc;
1094: /* compute final residual and final viscosity/strain rate fields */
1095: SNESGetFunction(snes, &res, NULL, NULL);
1096: ViscosityField(da, user->x, user->Xguess);
1098: /* get the communicator and the rank of the processor */
1099: PetscObjectGetComm((PetscObject)snes, &comm);
1100: MPI_Comm_rank(comm, &rank);
1102: if (param->output_to_file) { /* send output to binary file */
1103: VecCreate(comm, &pars);
1104: if (!rank) { /* on processor 0 */
1105: VecSetSizes(pars, 20, PETSC_DETERMINE);
1106: VecSetFromOptions(pars);
1107: VecSetValue(pars,0, (PetscScalar)(grid->ni),INSERT_VALUES);
1108: VecSetValue(pars,1, (PetscScalar)(grid->nj),INSERT_VALUES);
1109: VecSetValue(pars,2, (PetscScalar)(grid->dx),INSERT_VALUES);
1110: VecSetValue(pars,3, (PetscScalar)(grid->dz),INSERT_VALUES);
1111: VecSetValue(pars,4, (PetscScalar)(param->L),INSERT_VALUES);
1112: VecSetValue(pars,5, (PetscScalar)(param->V),INSERT_VALUES);
1113: /* skipped 6 intentionally */
1114: VecSetValue(pars,7, (PetscScalar)(param->slab_dip),INSERT_VALUES);
1115: VecSetValue(pars,8, (PetscScalar)(grid->jlid),INSERT_VALUES);
1116: VecSetValue(pars,9, (PetscScalar)(param->lid_depth),INSERT_VALUES);
1117: VecSetValue(pars,10,(PetscScalar)(grid->jfault),INSERT_VALUES);
1118: VecSetValue(pars,11,(PetscScalar)(param->fault_depth),INSERT_VALUES);
1119: VecSetValue(pars,12,(PetscScalar)(param->potentialT),INSERT_VALUES);
1120: VecSetValue(pars,13,(PetscScalar)(param->ivisc),INSERT_VALUES);
1121: VecSetValue(pars,14,(PetscScalar)(param->visc_cutoff),INSERT_VALUES);
1122: VecSetValue(pars,15,(PetscScalar)(param->ibound),INSERT_VALUES);
1123: VecSetValue(pars,16,(PetscScalar)(its),INSERT_VALUES);
1124: } else { /* on some other processor */
1125: VecSetSizes(pars, 0, PETSC_DETERMINE);
1126: VecSetFromOptions(pars);
1127: }
1128: VecAssemblyBegin(pars); VecAssemblyEnd(pars);
1130: /* create viewer */
1131: #if defined(PETSC_HAVE_MATLAB_ENGINE)
1132: PetscViewerMatlabOpen(PETSC_COMM_WORLD,param->filename,FILE_MODE_WRITE,&viewer);
1133: #else
1134: PetscViewerBinaryOpen(PETSC_COMM_WORLD,param->filename,FILE_MODE_WRITE,&viewer);
1135: #endif
1137: /* send vectors to viewer */
1138: PetscObjectSetName((PetscObject)res,"res");
1139: VecView(res,viewer);
1140: PetscObjectSetName((PetscObject)user->x,"out");
1141: VecView(user->x, viewer);
1142: PetscObjectSetName((PetscObject)(user->Xguess),"aux");
1143: VecView(user->Xguess, viewer);
1144: StressField(da); /* compute stress fields */
1145: PetscObjectSetName((PetscObject)(user->Xguess),"str");
1146: VecView(user->Xguess, viewer);
1147: PetscObjectSetName((PetscObject)pars,"par");
1148: VecView(pars, viewer);
1150: /* destroy viewer and vector */
1151: PetscViewerDestroy(&viewer);
1152: VecDestroy(&pars);
1153: }
1155: param->ivisc = ivt;
1156: return 0;
1157: }
1159: /* ------------------------------------------------------------------- */
1162: /* Compute both the second invariant of the strain rate tensor and the viscosity, at both cell centers and cell corners */
1163: PetscErrorCode ViscosityField(DM da, Vec X, Vec V)
1164: /* ------------------------------------------------------------------- */
1165: {
1166: AppCtx *user;
1167: Parameter *param;
1168: GridInfo *grid;
1169: Vec localX;
1170: Field **v, **x;
1171: PassiveReal eps, /* dx,*/ dz, T, epsC, TC;
1172: PetscInt i,j,is,js,im,jm,ilim,jlim,ivt;
1176: DMGetApplicationContext(da,&user);
1177: param = user->param;
1178: grid = user->grid;
1179: ivt = param->ivisc;
1180: param->ivisc = param->output_ivisc;
1182: DMGetLocalVector(da, &localX);
1183: DMGlobalToLocalBegin(da, X, INSERT_VALUES, localX);
1184: DMGlobalToLocalEnd(da, X, INSERT_VALUES, localX);
1185: DMDAVecGetArray(da,localX,(void**)&x);
1186: DMDAVecGetArray(da,V,(void**)&v);
1188: /* Parameters */
1189: /* dx = grid->dx; */ dz = grid->dz;
1191: ilim = grid->ni-1; jlim = grid->nj-1;
1193: /* Compute real temperature, strain rate and viscosity */
1194: DMDAGetCorners(da,&is,&js,NULL,&im,&jm,NULL);
1195: for (j=js; j<js+jm; j++) {
1196: for (i=is; i<is+im; i++) {
1197: T = PetscRealPart(param->potentialT * x[j][i].T * PetscExpScalar((j-0.5)*dz*param->z_scale));
1198: if (i<ilim && j<jlim) {
1199: TC = PetscRealPart(param->potentialT * TInterp(x,i,j) * PetscExpScalar(j*dz*param->z_scale));
1200: } else {
1201: TC = T;
1202: }
1203: eps = PetscRealPart((CalcSecInv(x,i,j,CELL_CENTER,user)));
1204: epsC = PetscRealPart(CalcSecInv(x,i,j,CELL_CORNER,user));
1206: v[j][i].u = eps;
1207: v[j][i].w = epsC;
1208: v[j][i].p = Viscosity(T,eps,dz*(j-0.5),param);
1209: v[j][i].T = Viscosity(TC,epsC,dz*j,param);
1210: }
1211: }
1212: DMDAVecRestoreArray(da,V,(void**)&v);
1213: DMDAVecRestoreArray(da,localX,(void**)&x);
1214: DMRestoreLocalVector(da, &localX);
1216: param->ivisc = ivt;
1217: return(0);
1218: }
1220: /* ------------------------------------------------------------------- */
1223: /* post-processing: compute stress everywhere */
1224: PetscErrorCode StressField(DM da)
1225: /* ------------------------------------------------------------------- */
1226: {
1227: AppCtx *user;
1228: PetscInt i,j,is,js,im,jm;
1230: Vec locVec;
1231: Field **x, **y;
1233: DMGetApplicationContext(da,&user);
1235: /* Get the fine grid of Xguess and X */
1236: DMDAGetCorners(da,&is,&js,NULL,&im,&jm,NULL);
1237: DMDAVecGetArray(da,user->Xguess,(void**)&x);
1239: DMGetLocalVector(da, &locVec);
1240: DMGlobalToLocalBegin(da, user->x, INSERT_VALUES, locVec);
1241: DMGlobalToLocalEnd(da, user->x, INSERT_VALUES, locVec);
1242: DMDAVecGetArray(da,locVec,(void**)&y);
1244: /* Compute stress on the corner points */
1245: for (j=js; j<js+jm; j++) {
1246: for (i=is; i<is+im; i++) {
1247: x[j][i].u = ShearStress(y,i,j,CELL_CENTER,user);
1248: x[j][i].w = ShearStress(y,i,j,CELL_CORNER,user);
1249: x[j][i].p = XNormalStress(y,i,j,CELL_CENTER,user);
1250: x[j][i].T = ZNormalStress(y,i,j,CELL_CENTER,user);
1251: }
1252: }
1254: /* Restore the fine grid of Xguess and X */
1255: DMDAVecRestoreArray(da,user->Xguess,(void**)&x);
1256: DMDAVecRestoreArray(da,locVec,(void**)&y);
1257: DMRestoreLocalVector(da, &locVec);
1258: return 0;
1259: }
1261: /*=====================================================================
1262: UTILITY FUNCTIONS
1263: =====================================================================*/
1265: /*---------------------------------------------------------------------*/
1268: /* returns the velocity of the subducting slab and handles fault nodes
1269: for BC */
1270: PETSC_STATIC_INLINE PassiveScalar SlabVel(char c, PetscInt i, PetscInt j, AppCtx *user)
1271: /*---------------------------------------------------------------------*/
1272: {
1273: Parameter *param = user->param;
1274: GridInfo *grid = user->grid;
1276: if (c=='U' || c=='u') {
1277: if (i<j-1) return param->cb;
1278: else if (j<=grid->jfault) return 0.0;
1279: else return param->cb;
1281: } else {
1282: if (i<j) return param->sb;
1283: else if (j<=grid->jfault) return 0.0;
1284: else return param->sb;
1285: }
1286: }
1288: /*---------------------------------------------------------------------*/
1291: /* solution to diffusive half-space cooling model for BC */
1292: PETSC_STATIC_INLINE PassiveScalar PlateModel(PetscInt j, PetscInt plate, AppCtx *user)
1293: /*---------------------------------------------------------------------*/
1294: {
1295: Parameter *param = user->param;
1296: PassiveScalar z;
1297: if (plate==PLATE_LID) z = (j-0.5)*user->grid->dz;
1298: else z = (j-0.5)*user->grid->dz*param->cb; /* PLATE_SLAB */
1299: #if defined(PETSC_HAVE_ERF)
1300: return (erf(PetscRealPart(z*param->L/2.0/param->skt)));
1301: #else
1302: SETERRQ(PETSC_COMM_SELF,1,"erf() not available on this machine");
1303: #endif
1304: }
1307: /* ------------------------------------------------------------------- */
1310: /* utility function */
1311: PetscBool OptionsHasName(const char pre[],const char name[])
1312: /* ------------------------------------------------------------------- */
1313: {
1314: PetscBool retval;
1316: PetscOptionsHasName(pre,name,&retval);CHKERRABORT(PETSC_COMM_WORLD,ierr);
1317: return retval;
1318: }
1320: /*=====================================================================
1321: INTERACTIVE SIGNAL HANDLING
1322: =====================================================================*/
1324: /* ------------------------------------------------------------------- */
1327: PetscErrorCode SNESConverged_Interactive(SNES snes, PetscInt it,PetscReal xnorm, PetscReal snorm, PetscReal fnorm, SNESConvergedReason *reason, void *ctx)
1328: /* ------------------------------------------------------------------- */
1329: {
1330: AppCtx *user = (AppCtx*) ctx;
1331: Parameter *param = user->param;
1332: KSP ksp;
1336: if (param->interrupted) {
1337: param->interrupted = PETSC_FALSE;
1338: PetscPrintf(PETSC_COMM_WORLD,"USER SIGNAL: exiting SNES solve. \n");
1339: *reason = SNES_CONVERGED_FNORM_ABS;
1340: return(0);
1341: } else if (param->toggle_kspmon) {
1342: param->toggle_kspmon = PETSC_FALSE;
1344: SNESGetKSP(snes, &ksp);
1346: if (param->kspmon) {
1347: KSPMonitorCancel(ksp);
1349: param->kspmon = PETSC_FALSE;
1350: PetscPrintf(PETSC_COMM_WORLD,"USER SIGNAL: deactivating ksp singular value monitor. \n");
1351: } else {
1352: KSPMonitorSet(ksp,KSPMonitorSingularValue,NULL,NULL);
1354: param->kspmon = PETSC_TRUE;
1355: PetscPrintf(PETSC_COMM_WORLD,"USER SIGNAL: activating ksp singular value monitor. \n");
1356: }
1357: }
1358: PetscFunctionReturn(SNESConvergedDefault(snes,it,xnorm,snorm,fnorm,reason,ctx));
1359: }
1361: /* ------------------------------------------------------------------- */
1362: #include <signal.h>
1365: PetscErrorCode InteractiveHandler(int signum, void *ctx)
1366: /* ------------------------------------------------------------------- */
1367: {
1368: AppCtx *user = (AppCtx*) ctx;
1369: Parameter *param = user->param;
1371: if (signum == SIGILL) {
1372: param->toggle_kspmon = PETSC_TRUE;
1373: #if !defined(PETSC_MISSING_SIGCONT)
1374: } else if (signum == SIGCONT) {
1375: param->interrupted = PETSC_TRUE;
1376: #endif
1377: #if !defined(PETSC_MISSING_SIGURG)
1378: } else if (signum == SIGURG) {
1379: param->stop_solve = PETSC_TRUE;
1380: #endif
1381: }
1382: return 0;
1383: }
1385: /*---------------------------------------------------------------------*/
1388: /* main call-back function that computes the processor-local piece
1389: of the residual */
1390: PetscErrorCode FormFunctionLocal(DMDALocalInfo *info,Field **x,Field **f,void *ptr)
1391: /*---------------------------------------------------------------------*/
1392: {
1393: AppCtx *user = (AppCtx*)ptr;
1394: Parameter *param = user->param;
1395: GridInfo *grid = user->grid;
1396: PetscScalar mag_w, mag_u;
1397: PetscInt i,j,mx,mz,ilim,jlim;
1398: PetscInt is,ie,js,je,ibound; /* ,ivisc */
1401: /* Define global and local grid parameters */
1402: mx = info->mx; mz = info->my;
1403: ilim = mx-1; jlim = mz-1;
1404: is = info->xs; ie = info->xs+info->xm;
1405: js = info->ys; je = info->ys+info->ym;
1407: /* Define geometric and numeric parameters */
1408: /* ivisc = param->ivisc; */ ibound = param->ibound;
1410: for (j=js; j<je; j++) {
1411: for (i=is; i<ie; i++) {
1413: /************* X-MOMENTUM/VELOCITY *************/
1414: if (i<j) f[j][i].u = x[j][i].u - SlabVel('U',i,j,user);
1415: else if (j<=grid->jlid || (j<grid->corner+grid->inose && i<grid->corner+grid->inose)) {
1416: /* in the lithospheric lid */
1417: f[j][i].u = x[j][i].u - 0.0;
1418: } else if (i==ilim) {
1419: /* on the right side boundary */
1420: if (ibound==BC_ANALYTIC) {
1421: f[j][i].u = x[j][i].u - HorizVelocity(i,j,user);
1422: } else {
1423: f[j][i].u = XNormalStress(x,i,j,CELL_CENTER,user) - EPS_ZERO;
1424: }
1426: } else if (j==jlim) {
1427: /* on the bottom boundary */
1428: if (ibound==BC_ANALYTIC) {
1429: f[j][i].u = x[j][i].u - HorizVelocity(i,j,user);
1430: } else if (ibound==BC_NOSTRESS) {
1431: f[j][i].u = XMomentumResidual(x,i,j,user);
1432: } else {
1433: /* experimental boundary condition */
1434: }
1436: } else {
1437: /* in the mantle wedge */
1438: f[j][i].u = XMomentumResidual(x,i,j,user);
1439: }
1441: /************* Z-MOMENTUM/VELOCITY *************/
1442: if (i<=j) {
1443: f[j][i].w = x[j][i].w - SlabVel('W',i,j,user);
1445: } else if (j<=grid->jlid || (j<grid->corner+grid->inose && i<grid->corner+grid->inose)) {
1446: /* in the lithospheric lid */
1447: f[j][i].w = x[j][i].w - 0.0;
1449: } else if (j==jlim) {
1450: /* on the bottom boundary */
1451: if (ibound==BC_ANALYTIC) {
1452: f[j][i].w = x[j][i].w - VertVelocity(i,j,user);
1453: } else {
1454: f[j][i].w = ZNormalStress(x,i,j,CELL_CENTER,user) - EPS_ZERO;
1455: }
1457: } else if (i==ilim) {
1458: /* on the right side boundary */
1459: if (ibound==BC_ANALYTIC) {
1460: f[j][i].w = x[j][i].w - VertVelocity(i,j,user);
1461: } else if (ibound==BC_NOSTRESS) {
1462: f[j][i].w = ZMomentumResidual(x,i,j,user);
1463: } else {
1464: /* experimental boundary condition */
1465: }
1467: } else {
1468: /* in the mantle wedge */
1469: f[j][i].w = ZMomentumResidual(x,i,j,user);
1470: }
1472: /************* CONTINUITY/PRESSURE *************/
1473: if (i<j || j<=grid->jlid || (j<grid->corner+grid->inose && i<grid->corner+grid->inose)) {
1474: /* in the lid or slab */
1475: f[j][i].p = x[j][i].p;
1477: } else if ((i==ilim || j==jlim) && ibound==BC_ANALYTIC) {
1478: /* on an analytic boundary */
1479: f[j][i].p = x[j][i].p - Pressure(i,j,user);
1481: } else {
1482: /* in the mantle wedge */
1483: f[j][i].p = ContinuityResidual(x,i,j,user);
1484: }
1486: /************* TEMPERATURE *************/
1487: if (j==0) {
1488: /* on the surface */
1489: f[j][i].T = x[j][i].T + x[j+1][i].T + PetscMax(PetscRealPart(x[j][i].T),0.0);
1491: } else if (i==0) {
1492: /* slab inflow boundary */
1493: f[j][i].T = x[j][i].T - PlateModel(j,PLATE_SLAB,user);
1495: } else if (i==ilim) {
1496: /* right side boundary */
1497: mag_u = 1.0 - PetscPowRealInt((1.0-PetscMax(PetscMin(PetscRealPart(x[j][i-1].u)/param->cb,1.0),0.0)), 5);
1498: f[j][i].T = x[j][i].T - mag_u*x[j-1][i-1].T - (1.0-mag_u)*PlateModel(j,PLATE_LID,user);
1500: } else if (j==jlim) {
1501: /* bottom boundary */
1502: mag_w = 1.0 - PetscPowRealInt((1.0-PetscMax(PetscMin(PetscRealPart(x[j-1][i].w)/param->sb,1.0),0.0)), 5);
1503: f[j][i].T = x[j][i].T - mag_w*x[j-1][i-1].T - (1.0-mag_w);
1505: } else {
1506: /* in the mantle wedge */
1507: f[j][i].T = EnergyResidual(x,i,j,user);
1508: }
1509: }
1510: }
1511: return(0);
1512: }