Actual source code: ex49.c
1: static char help[] = " Solves the compressible plane strain elasticity equations in 2d on the unit domain using Q1 finite elements. \n\
2: Material properties E (Youngs modulus) and nu (Poisson ratio) may vary as a function of space. \n\
3: The model utilises boundary conditions which produce compression in the x direction. \n\
4: Options: \n"
5: "\
6: -mx : number of elements in x-direction \n\
7: -my : number of elements in y-direction \n\
8: -c_str : structure of the coefficients to use. \n"
9: "\
10: -c_str 0 => isotropic material with constant coefficients. \n\
11: Parameters: \n\
12: -iso_E : Youngs modulus \n\
13: -iso_nu : Poisson ratio \n\
14: -c_str 1 => step function in the material properties in x. \n\
15: Parameters: \n\
16: -step_E0 : Youngs modulus to the left of the step \n\
17: -step_nu0 : Poisson ratio to the left of the step \n\
18: -step_E1 : Youngs modulus to the right of the step \n\
19: -step_n1 : Poisson ratio to the right of the step \n\
20: -step_xc : x coordinate of the step \n"
21: "\
22: -c_str 2 => checkerboard material with alternating properties. \n\
23: Repeats the following pattern throughout the domain. For example with 4 materials specified, we would heve \n\
24: -------------------------\n\
25: | D | A | B | C |\n\
26: ------|-----|-----|------\n\
27: | C | D | A | B |\n\
28: ------|-----|-----|------\n\
29: | B | C | D | A |\n\
30: ------|-----|-----|------\n\
31: | A | B | C | D |\n\
32: -------------------------\n\
33: \n\
34: Parameters: \n\
35: -brick_E : a comma separated list of Young's modulii \n\
36: -brick_nu : a comma separated list of Poisson ratios \n\
37: -brick_span : the number of elements in x and y each brick will span \n\
38: -c_str 3 => sponge-like material with alternating properties. \n\
39: Repeats the following pattern throughout the domain \n"
40: "\
41: -----------------------------\n\
42: | [background] |\n\
43: | E0,nu0 |\n\
44: | ----------------- |\n\
45: | | [inclusion] | |\n\
46: | | E1,nu1 | |\n\
47: | | | |\n\
48: | | <---- w ----> | |\n\
49: | | | |\n\
50: | | | |\n\
51: | ----------------- |\n\
52: | |\n\
53: | |\n\
54: -----------------------------\n\
55: <-------- t + w + t ------->\n\
56: \n\
57: Parameters: \n\
58: -sponge_E0 : Youngs modulus of the surrounding material \n\
59: -sponge_E1 : Youngs modulus of the inclusion \n\
60: -sponge_nu0 : Poisson ratio of the surrounding material \n\
61: -sponge_nu1 : Poisson ratio of the inclusion \n\
62: -sponge_t : the number of elements defining the border around each inclusion \n\
63: -sponge_w : the number of elements in x and y each inclusion will span\n\
64: -use_gp_coords : Evaluate the Youngs modulus, Poisson ratio and the body force at the global coordinates of the quadrature points.\n\
65: By default, E, nu and the body force are evaulated at the element center and applied as a constant over the entire element.\n\
66: -use_nonsymbc : Option to use non-symmetric boundary condition imposition. This choice will use less memory.";
68: /* Contributed by Dave May */
70: #include <petscksp.h>
71: #include <petscdm.h>
72: #include <petscdmda.h>
74: static PetscErrorCode DMDABCApplyCompression(DM,Mat,Vec);
75: static PetscErrorCode DMDABCApplySymmetricCompression(DM elas_da,Mat A,Vec f,IS *dofs,Mat *AA,Vec *ff);
77: #define NSD 2 /* number of spatial dimensions */
78: #define NODES_PER_EL 4 /* nodes per element */
79: #define U_DOFS 2 /* degrees of freedom per displacement node */
80: #define GAUSS_POINTS 4
82: /* cell based evaluation */
83: typedef struct {
84: PetscScalar E,nu,fx,fy;
85: } Coefficients;
87: /* Gauss point based evaluation 8+4+4+4 = 20 */
88: typedef struct {
89: PetscScalar gp_coords[2*GAUSS_POINTS];
90: PetscScalar E[GAUSS_POINTS];
91: PetscScalar nu[GAUSS_POINTS];
92: PetscScalar fx[GAUSS_POINTS];
93: PetscScalar fy[GAUSS_POINTS];
94: } GaussPointCoefficients;
96: typedef struct {
97: PetscScalar ux_dof;
98: PetscScalar uy_dof;
99: } ElasticityDOF;
101: /*
103: D = E/((1+nu)(1-2nu)) * [ 1-nu nu 0 ]
104: [ nu 1-nu 0 ]
105: [ 0 0 0.5*(1-2nu) ]
107: B = [ d_dx 0 ]
108: [ 0 d_dy ]
109: [ d_dy d_dx ]
111: */
113: /* FEM routines */
114: /*
115: Element: Local basis function ordering
116: 1-----2
117: | |
118: | |
119: 0-----3
120: */
121: static void ConstructQ12D_Ni(PetscScalar _xi[],PetscScalar Ni[])
122: {
123: PetscScalar xi = _xi[0];
124: PetscScalar eta = _xi[1];
126: Ni[0] = 0.25*(1.0-xi)*(1.0-eta);
127: Ni[1] = 0.25*(1.0-xi)*(1.0+eta);
128: Ni[2] = 0.25*(1.0+xi)*(1.0+eta);
129: Ni[3] = 0.25*(1.0+xi)*(1.0-eta);
130: }
132: static void ConstructQ12D_GNi(PetscScalar _xi[],PetscScalar GNi[][NODES_PER_EL])
133: {
134: PetscScalar xi = _xi[0];
135: PetscScalar eta = _xi[1];
137: GNi[0][0] = -0.25*(1.0-eta);
138: GNi[0][1] = -0.25*(1.0+eta);
139: GNi[0][2] = 0.25*(1.0+eta);
140: GNi[0][3] = 0.25*(1.0-eta);
142: GNi[1][0] = -0.25*(1.0-xi);
143: GNi[1][1] = 0.25*(1.0-xi);
144: GNi[1][2] = 0.25*(1.0+xi);
145: GNi[1][3] = -0.25*(1.0+xi);
146: }
148: static void ConstructQ12D_GNx(PetscScalar GNi[][NODES_PER_EL],PetscScalar GNx[][NODES_PER_EL],PetscScalar coords[],PetscScalar *det_J)
149: {
150: PetscScalar J00,J01,J10,J11,J;
151: PetscScalar iJ00,iJ01,iJ10,iJ11;
152: PetscInt i;
154: J00 = J01 = J10 = J11 = 0.0;
155: for (i = 0; i < NODES_PER_EL; i++) {
156: PetscScalar cx = coords[2*i+0];
157: PetscScalar cy = coords[2*i+1];
159: J00 = J00+GNi[0][i]*cx; /* J_xx = dx/dxi */
160: J01 = J01+GNi[0][i]*cy; /* J_xy = dy/dxi */
161: J10 = J10+GNi[1][i]*cx; /* J_yx = dx/deta */
162: J11 = J11+GNi[1][i]*cy; /* J_yy = dy/deta */
163: }
164: J = (J00*J11)-(J01*J10);
166: iJ00 = J11/J;
167: iJ01 = -J01/J;
168: iJ10 = -J10/J;
169: iJ11 = J00/J;
171: for (i = 0; i < NODES_PER_EL; i++) {
172: GNx[0][i] = GNi[0][i]*iJ00+GNi[1][i]*iJ01;
173: GNx[1][i] = GNi[0][i]*iJ10+GNi[1][i]*iJ11;
174: }
176: if (det_J) *det_J = J;
177: }
179: static void ConstructGaussQuadrature(PetscInt *ngp,PetscScalar gp_xi[][2],PetscScalar gp_weight[])
180: {
181: *ngp = 4;
182: gp_xi[0][0] = -0.57735026919;gp_xi[0][1] = -0.57735026919;
183: gp_xi[1][0] = -0.57735026919;gp_xi[1][1] = 0.57735026919;
184: gp_xi[2][0] = 0.57735026919;gp_xi[2][1] = 0.57735026919;
185: gp_xi[3][0] = 0.57735026919;gp_xi[3][1] = -0.57735026919;
186: gp_weight[0] = 1.0;
187: gp_weight[1] = 1.0;
188: gp_weight[2] = 1.0;
189: gp_weight[3] = 1.0;
190: }
192: static PetscErrorCode DMDAGetElementOwnershipRanges2d(DM da,PetscInt **_lx,PetscInt **_ly)
193: {
194: PetscMPIInt rank;
195: PetscInt proc_I,proc_J;
196: PetscInt cpu_x,cpu_y;
197: PetscInt local_mx,local_my;
198: Vec vlx,vly;
199: PetscInt *LX,*LY,i;
200: PetscScalar *_a;
201: Vec V_SEQ;
202: VecScatter ctx;
205: MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
207: DMDAGetInfo(da,0,0,0,0,&cpu_x,&cpu_y,0,0,0,0,0,0,0);
209: proc_J = rank/cpu_x;
210: proc_I = rank-cpu_x*proc_J;
212: PetscMalloc1(cpu_x,&LX);
213: PetscMalloc1(cpu_y,&LY);
215: DMDAGetElementsSizes(da,&local_mx,&local_my,NULL);
216: VecCreate(PETSC_COMM_WORLD,&vlx);
217: VecSetSizes(vlx,PETSC_DECIDE,cpu_x);
218: VecSetFromOptions(vlx);
220: VecCreate(PETSC_COMM_WORLD,&vly);
221: VecSetSizes(vly,PETSC_DECIDE,cpu_y);
222: VecSetFromOptions(vly);
224: VecSetValue(vlx,proc_I,(PetscScalar)(local_mx+1.0e-9),INSERT_VALUES);
225: VecSetValue(vly,proc_J,(PetscScalar)(local_my+1.0e-9),INSERT_VALUES);
226: VecAssemblyBegin(vlx);VecAssemblyEnd(vlx);
227: VecAssemblyBegin(vly);VecAssemblyEnd(vly);
229: VecScatterCreateToAll(vlx,&ctx,&V_SEQ);
230: VecScatterBegin(ctx,vlx,V_SEQ,INSERT_VALUES,SCATTER_FORWARD);
231: VecScatterEnd(ctx,vlx,V_SEQ,INSERT_VALUES,SCATTER_FORWARD);
232: VecGetArray(V_SEQ,&_a);
233: for (i = 0; i < cpu_x; i++) LX[i] = (PetscInt)PetscRealPart(_a[i]);
234: VecRestoreArray(V_SEQ,&_a);
235: VecScatterDestroy(&ctx);
236: VecDestroy(&V_SEQ);
238: VecScatterCreateToAll(vly,&ctx,&V_SEQ);
239: VecScatterBegin(ctx,vly,V_SEQ,INSERT_VALUES,SCATTER_FORWARD);
240: VecScatterEnd(ctx,vly,V_SEQ,INSERT_VALUES,SCATTER_FORWARD);
241: VecGetArray(V_SEQ,&_a);
242: for (i = 0; i < cpu_y; i++) LY[i] = (PetscInt)PetscRealPart(_a[i]);
243: VecRestoreArray(V_SEQ,&_a);
244: VecScatterDestroy(&ctx);
245: VecDestroy(&V_SEQ);
247: *_lx = LX;
248: *_ly = LY;
250: VecDestroy(&vlx);
251: VecDestroy(&vly);
252: return 0;
253: }
255: static PetscErrorCode DMDACoordViewGnuplot2d(DM da,const char prefix[])
256: {
257: DM cda;
258: Vec coords;
259: DMDACoor2d **_coords;
260: PetscInt si,sj,nx,ny,i,j;
261: FILE *fp;
262: char fname[PETSC_MAX_PATH_LEN];
263: PetscMPIInt rank;
266: MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
267: PetscSNPrintf(fname,sizeof(fname),"%s-p%1.4d.dat",prefix,rank);
268: PetscFOpen(PETSC_COMM_SELF,fname,"w",&fp);
270: PetscFPrintf(PETSC_COMM_SELF,fp,"### Element geometry for processor %1.4d ### \n",rank);
272: DMGetCoordinateDM(da,&cda);
273: DMGetCoordinatesLocal(da,&coords);
274: DMDAVecGetArray(cda,coords,&_coords);
275: DMDAGetGhostCorners(cda,&si,&sj,0,&nx,&ny,0);
276: for (j = sj; j < sj+ny-1; j++) {
277: for (i = si; i < si+nx-1; i++) {
278: PetscFPrintf(PETSC_COMM_SELF,fp,"%1.6e %1.6e \n",PetscRealPart(_coords[j][i].x),PetscRealPart(_coords[j][i].y));
279: PetscFPrintf(PETSC_COMM_SELF,fp,"%1.6e %1.6e \n",PetscRealPart(_coords[j+1][i].x),PetscRealPart(_coords[j+1][i].y));
280: PetscFPrintf(PETSC_COMM_SELF,fp,"%1.6e %1.6e \n",PetscRealPart(_coords[j+1][i+1].x),PetscRealPart(_coords[j+1][i+1].y));
281: PetscFPrintf(PETSC_COMM_SELF,fp,"%1.6e %1.6e \n",PetscRealPart(_coords[j][i+1].x),PetscRealPart(_coords[j][i+1].y));
282: PetscFPrintf(PETSC_COMM_SELF,fp,"%1.6e %1.6e \n\n",PetscRealPart(_coords[j][i].x),PetscRealPart(_coords[j][i].y));
283: }
284: }
285: DMDAVecRestoreArray(cda,coords,&_coords);
287: PetscFClose(PETSC_COMM_SELF,fp);
288: return 0;
289: }
291: static PetscErrorCode DMDAViewGnuplot2d(DM da,Vec fields,const char comment[],const char prefix[])
292: {
293: DM cda;
294: Vec coords,local_fields;
295: DMDACoor2d **_coords;
296: FILE *fp;
297: char fname[PETSC_MAX_PATH_LEN];
298: const char *field_name;
299: PetscMPIInt rank;
300: PetscInt si,sj,nx,ny,i,j;
301: PetscInt n_dofs,d;
302: PetscScalar *_fields;
305: MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
306: PetscSNPrintf(fname,sizeof(fname),"%s-p%1.4d.dat",prefix,rank);
307: PetscFOpen(PETSC_COMM_SELF,fname,"w",&fp);
310: PetscFPrintf(PETSC_COMM_SELF,fp,"### %s (processor %1.4d) ### \n",comment,rank);
311: DMDAGetInfo(da,0,0,0,0,0,0,0,&n_dofs,0,0,0,0,0);
312: PetscFPrintf(PETSC_COMM_SELF,fp,"### x y ");
313: for (d = 0; d < n_dofs; d++) {
314: DMDAGetFieldName(da,d,&field_name);
315: PetscFPrintf(PETSC_COMM_SELF,fp,"%s ",field_name);
316: }
317: PetscFPrintf(PETSC_COMM_SELF,fp,"###\n");
319: DMGetCoordinateDM(da,&cda);
320: DMGetCoordinatesLocal(da,&coords);
321: DMDAVecGetArray(cda,coords,&_coords);
322: DMDAGetGhostCorners(cda,&si,&sj,0,&nx,&ny,0);
324: DMCreateLocalVector(da,&local_fields);
325: DMGlobalToLocalBegin(da,fields,INSERT_VALUES,local_fields);
326: DMGlobalToLocalEnd(da,fields,INSERT_VALUES,local_fields);
327: VecGetArray(local_fields,&_fields);
329: for (j = sj; j < sj+ny; j++) {
330: for (i = si; i < si+nx; i++) {
331: PetscScalar coord_x,coord_y;
332: PetscScalar field_d;
334: coord_x = _coords[j][i].x;
335: coord_y = _coords[j][i].y;
337: PetscFPrintf(PETSC_COMM_SELF,fp,"%1.6e %1.6e ",PetscRealPart(coord_x),PetscRealPart(coord_y));
338: for (d = 0; d < n_dofs; d++) {
339: field_d = _fields[n_dofs*((i-si)+(j-sj)*(nx))+d];
340: PetscFPrintf(PETSC_COMM_SELF,fp,"%1.6e ",PetscRealPart(field_d));
341: }
342: PetscFPrintf(PETSC_COMM_SELF,fp,"\n");
343: }
344: }
345: VecRestoreArray(local_fields,&_fields);
346: VecDestroy(&local_fields);
348: DMDAVecRestoreArray(cda,coords,&_coords);
350: PetscFClose(PETSC_COMM_SELF,fp);
351: return 0;
352: }
354: static PetscErrorCode DMDAViewCoefficientsGnuplot2d(DM da,Vec fields,const char comment[],const char prefix[])
355: {
356: DM cda;
357: Vec local_fields;
358: FILE *fp;
359: char fname[PETSC_MAX_PATH_LEN];
360: const char *field_name;
361: PetscMPIInt rank;
362: PetscInt si,sj,nx,ny,i,j,p;
363: PetscInt n_dofs,d;
364: GaussPointCoefficients **_coefficients;
365: PetscErrorCode ierr;
368: MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
369: PetscSNPrintf(fname,sizeof(fname),"%s-p%1.4d.dat",prefix,rank);
370: PetscFOpen(PETSC_COMM_SELF,fname,"w",&fp);
373: PetscFPrintf(PETSC_COMM_SELF,fp,"### %s (processor %1.4d) ### \n",comment,rank);
374: DMDAGetInfo(da,0,0,0,0,0,0,0,&n_dofs,0,0,0,0,0);
375: PetscFPrintf(PETSC_COMM_SELF,fp,"### x y ");
376: for (d = 0; d < n_dofs; d++) {
377: DMDAGetFieldName(da,d,&field_name);
378: PetscFPrintf(PETSC_COMM_SELF,fp,"%s ",field_name);
379: }
380: PetscFPrintf(PETSC_COMM_SELF,fp,"###\n");
382: DMGetCoordinateDM(da,&cda);
383: DMDAGetGhostCorners(cda,&si,&sj,0,&nx,&ny,0);
385: DMCreateLocalVector(da,&local_fields);
386: DMGlobalToLocalBegin(da,fields,INSERT_VALUES,local_fields);
387: DMGlobalToLocalEnd(da,fields,INSERT_VALUES,local_fields);
388: DMDAVecGetArray(da,local_fields,&_coefficients);
390: for (j = sj; j < sj+ny; j++) {
391: for (i = si; i < si+nx; i++) {
392: PetscScalar coord_x,coord_y;
394: for (p = 0; p < GAUSS_POINTS; p++) {
395: coord_x = _coefficients[j][i].gp_coords[2*p];
396: coord_y = _coefficients[j][i].gp_coords[2*p+1];
398: PetscFPrintf(PETSC_COMM_SELF,fp,"%1.6e %1.6e ",PetscRealPart(coord_x),PetscRealPart(coord_y));
400: PetscFPrintf(PETSC_COMM_SELF,fp,"%1.6e %1.6e %1.6e %1.6e",
401: PetscRealPart(_coefficients[j][i].E[p]),PetscRealPart(_coefficients[j][i].nu[p]),
402: PetscRealPart(_coefficients[j][i].fx[p]),PetscRealPart(_coefficients[j][i].fy[p]));
403: PetscFPrintf(PETSC_COMM_SELF,fp,"\n");
404: }
405: }
406: }
407: DMDAVecRestoreArray(da,local_fields,&_coefficients);
408: VecDestroy(&local_fields);
410: PetscFClose(PETSC_COMM_SELF,fp);
411: return 0;
412: }
414: static void FormStressOperatorQ1(PetscScalar Ke[],PetscScalar coords[],PetscScalar E[],PetscScalar nu[])
415: {
416: PetscInt ngp;
417: PetscScalar gp_xi[GAUSS_POINTS][2];
418: PetscScalar gp_weight[GAUSS_POINTS];
419: PetscInt p,i,j,k,l;
420: PetscScalar GNi_p[NSD][NODES_PER_EL],GNx_p[NSD][NODES_PER_EL];
421: PetscScalar J_p;
422: PetscScalar B[3][U_DOFS*NODES_PER_EL];
423: PetscScalar prop_E,prop_nu,factor,constit_D[3][3];
425: /* define quadrature rule */
426: ConstructGaussQuadrature(&ngp,gp_xi,gp_weight);
428: /* evaluate integral */
429: for (p = 0; p < ngp; p++) {
430: ConstructQ12D_GNi(gp_xi[p],GNi_p);
431: ConstructQ12D_GNx(GNi_p,GNx_p,coords,&J_p);
433: for (i = 0; i < NODES_PER_EL; i++) {
434: PetscScalar d_dx_i = GNx_p[0][i];
435: PetscScalar d_dy_i = GNx_p[1][i];
437: B[0][2*i] = d_dx_i; B[0][2*i+1] = 0.0;
438: B[1][2*i] = 0.0; B[1][2*i+1] = d_dy_i;
439: B[2][2*i] = d_dy_i; B[2][2*i+1] = d_dx_i;
440: }
442: /* form D for the quadrature point */
443: prop_E = E[p];
444: prop_nu = nu[p];
445: factor = prop_E / ((1.0+prop_nu)*(1.0-2.0*prop_nu));
446: constit_D[0][0] = 1.0-prop_nu; constit_D[0][1] = prop_nu; constit_D[0][2] = 0.0;
447: constit_D[1][0] = prop_nu; constit_D[1][1] = 1.0-prop_nu; constit_D[1][2] = 0.0;
448: constit_D[2][0] = 0.0; constit_D[2][1] = 0.0; constit_D[2][2] = 0.5*(1.0-2.0*prop_nu);
449: for (i = 0; i < 3; i++) {
450: for (j = 0; j < 3; j++) {
451: constit_D[i][j] = factor * constit_D[i][j] * gp_weight[p] * J_p;
452: }
453: }
455: /* form Bt tildeD B */
456: /*
457: Ke_ij = Bt_ik . D_kl . B_lj
458: = B_ki . D_kl . B_lj
459: */
460: for (i = 0; i < 8; i++) {
461: for (j = 0; j < 8; j++) {
462: for (k = 0; k < 3; k++) {
463: for (l = 0; l < 3; l++) {
464: Ke[8*i+j] = Ke[8*i+j] + B[k][i] * constit_D[k][l] * B[l][j];
465: }
466: }
467: }
468: }
470: } /* end quadrature */
471: }
473: static void FormMomentumRhsQ1(PetscScalar Fe[],PetscScalar coords[],PetscScalar fx[],PetscScalar fy[])
474: {
475: PetscInt ngp;
476: PetscScalar gp_xi[GAUSS_POINTS][2];
477: PetscScalar gp_weight[GAUSS_POINTS];
478: PetscInt p,i;
479: PetscScalar Ni_p[NODES_PER_EL];
480: PetscScalar GNi_p[NSD][NODES_PER_EL],GNx_p[NSD][NODES_PER_EL];
481: PetscScalar J_p,fac;
483: /* define quadrature rule */
484: ConstructGaussQuadrature(&ngp,gp_xi,gp_weight);
486: /* evaluate integral */
487: for (p = 0; p < ngp; p++) {
488: ConstructQ12D_Ni(gp_xi[p],Ni_p);
489: ConstructQ12D_GNi(gp_xi[p],GNi_p);
490: ConstructQ12D_GNx(GNi_p,GNx_p,coords,&J_p);
491: fac = gp_weight[p]*J_p;
493: for (i = 0; i < NODES_PER_EL; i++) {
494: Fe[NSD*i] += fac*Ni_p[i]*fx[p];
495: Fe[NSD*i+1] += fac*Ni_p[i]*fy[p];
496: }
497: }
498: }
500: /*
501: i,j are the element indices
502: The unknown is a vector quantity.
503: The s[].c is used to indicate the degree of freedom.
504: */
505: static PetscErrorCode DMDAGetElementEqnums_u(MatStencil s_u[],PetscInt i,PetscInt j)
506: {
508: /* displacement */
509: /* node 0 */
510: s_u[0].i = i;s_u[0].j = j;s_u[0].c = 0; /* Ux0 */
511: s_u[1].i = i;s_u[1].j = j;s_u[1].c = 1; /* Uy0 */
513: /* node 1 */
514: s_u[2].i = i;s_u[2].j = j+1;s_u[2].c = 0; /* Ux1 */
515: s_u[3].i = i;s_u[3].j = j+1;s_u[3].c = 1; /* Uy1 */
517: /* node 2 */
518: s_u[4].i = i+1;s_u[4].j = j+1;s_u[4].c = 0; /* Ux2 */
519: s_u[5].i = i+1;s_u[5].j = j+1;s_u[5].c = 1; /* Uy2 */
521: /* node 3 */
522: s_u[6].i = i+1;s_u[6].j = j;s_u[6].c = 0; /* Ux3 */
523: s_u[7].i = i+1;s_u[7].j = j;s_u[7].c = 1; /* Uy3 */
524: return 0;
525: }
527: static PetscErrorCode GetElementCoords(DMDACoor2d **_coords,PetscInt ei,PetscInt ej,PetscScalar el_coords[])
528: {
530: /* get coords for the element */
531: el_coords[NSD*0+0] = _coords[ej][ei].x; el_coords[NSD*0+1] = _coords[ej][ei].y;
532: el_coords[NSD*1+0] = _coords[ej+1][ei].x; el_coords[NSD*1+1] = _coords[ej+1][ei].y;
533: el_coords[NSD*2+0] = _coords[ej+1][ei+1].x; el_coords[NSD*2+1] = _coords[ej+1][ei+1].y;
534: el_coords[NSD*3+0] = _coords[ej][ei+1].x; el_coords[NSD*3+1] = _coords[ej][ei+1].y;
535: return 0;
536: }
538: static PetscErrorCode AssembleA_Elasticity(Mat A,DM elas_da,DM properties_da,Vec properties)
539: {
540: DM cda;
541: Vec coords;
542: DMDACoor2d **_coords;
543: MatStencil u_eqn[NODES_PER_EL*U_DOFS]; /* 2 degrees of freedom */
544: PetscInt sex,sey,mx,my;
545: PetscInt ei,ej;
546: PetscScalar Ae[NODES_PER_EL*U_DOFS*NODES_PER_EL*U_DOFS];
547: PetscScalar el_coords[NODES_PER_EL*NSD];
548: Vec local_properties;
549: GaussPointCoefficients **props;
550: PetscScalar *prop_E,*prop_nu;
553: /* setup for coords */
554: DMGetCoordinateDM(elas_da,&cda);
555: DMGetCoordinatesLocal(elas_da,&coords);
556: DMDAVecGetArray(cda,coords,&_coords);
558: /* setup for coefficients */
559: DMCreateLocalVector(properties_da,&local_properties);
560: DMGlobalToLocalBegin(properties_da,properties,INSERT_VALUES,local_properties);
561: DMGlobalToLocalEnd(properties_da,properties,INSERT_VALUES,local_properties);
562: DMDAVecGetArray(properties_da,local_properties,&props);
564: DMDAGetElementsCorners(elas_da,&sex,&sey,0);
565: DMDAGetElementsSizes(elas_da,&mx,&my,0);
566: for (ej = sey; ej < sey+my; ej++) {
567: for (ei = sex; ei < sex+mx; ei++) {
568: /* get coords for the element */
569: GetElementCoords(_coords,ei,ej,el_coords);
571: /* get coefficients for the element */
572: prop_E = props[ej][ei].E;
573: prop_nu = props[ej][ei].nu;
575: /* initialise element stiffness matrix */
576: PetscMemzero(Ae,sizeof(Ae));
578: /* form element stiffness matrix */
579: FormStressOperatorQ1(Ae,el_coords,prop_E,prop_nu);
581: /* insert element matrix into global matrix */
582: DMDAGetElementEqnums_u(u_eqn,ei,ej);
583: MatSetValuesStencil(A,NODES_PER_EL*U_DOFS,u_eqn,NODES_PER_EL*U_DOFS,u_eqn,Ae,ADD_VALUES);
584: }
585: }
586: MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
587: MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
589: DMDAVecRestoreArray(cda,coords,&_coords);
591: DMDAVecRestoreArray(properties_da,local_properties,&props);
592: VecDestroy(&local_properties);
593: return 0;
594: }
596: static PetscErrorCode DMDASetValuesLocalStencil_ADD_VALUES(ElasticityDOF **fields_F,MatStencil u_eqn[],PetscScalar Fe_u[])
597: {
598: PetscInt n;
601: for (n = 0; n < 4; n++) {
602: fields_F[u_eqn[2*n].j][u_eqn[2*n].i].ux_dof = fields_F[u_eqn[2*n].j][u_eqn[2*n].i].ux_dof+Fe_u[2*n];
603: fields_F[u_eqn[2*n+1].j][u_eqn[2*n+1].i].uy_dof = fields_F[u_eqn[2*n+1].j][u_eqn[2*n+1].i].uy_dof+Fe_u[2*n+1];
604: }
605: return 0;
606: }
608: static PetscErrorCode AssembleF_Elasticity(Vec F,DM elas_da,DM properties_da,Vec properties)
609: {
610: DM cda;
611: Vec coords;
612: DMDACoor2d **_coords;
613: MatStencil u_eqn[NODES_PER_EL*U_DOFS]; /* 2 degrees of freedom */
614: PetscInt sex,sey,mx,my;
615: PetscInt ei,ej;
616: PetscScalar Fe[NODES_PER_EL*U_DOFS];
617: PetscScalar el_coords[NODES_PER_EL*NSD];
618: Vec local_properties;
619: GaussPointCoefficients **props;
620: PetscScalar *prop_fx,*prop_fy;
621: Vec local_F;
622: ElasticityDOF **ff;
625: /* setup for coords */
626: DMGetCoordinateDM(elas_da,&cda);
627: DMGetCoordinatesLocal(elas_da,&coords);
628: DMDAVecGetArray(cda,coords,&_coords);
630: /* setup for coefficients */
631: DMGetLocalVector(properties_da,&local_properties);
632: DMGlobalToLocalBegin(properties_da,properties,INSERT_VALUES,local_properties);
633: DMGlobalToLocalEnd(properties_da,properties,INSERT_VALUES,local_properties);
634: DMDAVecGetArray(properties_da,local_properties,&props);
636: /* get access to the vector */
637: DMGetLocalVector(elas_da,&local_F);
638: VecZeroEntries(local_F);
639: DMDAVecGetArray(elas_da,local_F,&ff);
641: DMDAGetElementsCorners(elas_da,&sex,&sey,0);
642: DMDAGetElementsSizes(elas_da,&mx,&my,0);
643: for (ej = sey; ej < sey+my; ej++) {
644: for (ei = sex; ei < sex+mx; ei++) {
645: /* get coords for the element */
646: GetElementCoords(_coords,ei,ej,el_coords);
648: /* get coefficients for the element */
649: prop_fx = props[ej][ei].fx;
650: prop_fy = props[ej][ei].fy;
652: /* initialise element stiffness matrix */
653: PetscMemzero(Fe,sizeof(Fe));
655: /* form element stiffness matrix */
656: FormMomentumRhsQ1(Fe,el_coords,prop_fx,prop_fy);
658: /* insert element matrix into global matrix */
659: DMDAGetElementEqnums_u(u_eqn,ei,ej);
661: DMDASetValuesLocalStencil_ADD_VALUES(ff,u_eqn,Fe);
662: }
663: }
665: DMDAVecRestoreArray(elas_da,local_F,&ff);
666: DMLocalToGlobalBegin(elas_da,local_F,ADD_VALUES,F);
667: DMLocalToGlobalEnd(elas_da,local_F,ADD_VALUES,F);
668: DMRestoreLocalVector(elas_da,&local_F);
670: DMDAVecRestoreArray(cda,coords,&_coords);
672: DMDAVecRestoreArray(properties_da,local_properties,&props);
673: DMRestoreLocalVector(properties_da,&local_properties);
674: return 0;
675: }
677: static PetscErrorCode solve_elasticity_2d(PetscInt mx,PetscInt my)
678: {
679: DM elas_da,da_prop;
680: PetscInt u_dof,dof,stencil_width;
681: Mat A;
682: PetscInt mxl,myl;
683: DM prop_cda,vel_cda;
684: Vec prop_coords,vel_coords;
685: PetscInt si,sj,nx,ny,i,j,p;
686: Vec f,X;
687: PetscInt prop_dof,prop_stencil_width;
688: Vec properties,l_properties;
689: MatNullSpace matnull;
690: PetscReal dx,dy;
691: PetscInt M,N;
692: DMDACoor2d **_prop_coords,**_vel_coords;
693: GaussPointCoefficients **element_props;
694: KSP ksp_E;
695: PetscInt coefficient_structure = 0;
696: PetscInt cpu_x,cpu_y,*lx = NULL,*ly = NULL;
697: PetscBool use_gp_coords = PETSC_FALSE;
698: PetscBool use_nonsymbc = PETSC_FALSE;
699: PetscBool no_view = PETSC_FALSE;
700: PetscBool flg;
703: /* Generate the da for velocity and pressure */
704: /*
705: We use Q1 elements for the temperature.
706: FEM has a 9-point stencil (BOX) or connectivity pattern
707: Num nodes in each direction is mx+1, my+1
708: */
709: u_dof = U_DOFS; /* Vx, Vy - velocities */
710: dof = u_dof;
711: stencil_width = 1;
712: DMDACreate2d(PETSC_COMM_WORLD, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE,DMDA_STENCIL_BOX,mx+1,my+1,PETSC_DECIDE,PETSC_DECIDE,dof,stencil_width,NULL,NULL,&elas_da);
714: DMSetMatType(elas_da,MATAIJ);
715: DMSetFromOptions(elas_da);
716: DMSetUp(elas_da);
718: DMDASetFieldName(elas_da,0,"Ux");
719: DMDASetFieldName(elas_da,1,"Uy");
721: /* unit box [0,1] x [0,1] */
722: DMDASetUniformCoordinates(elas_da,0.0,1.0,0.0,1.0,0.0,1.0);
724: /* Generate element properties, we will assume all material properties are constant over the element */
725: /* local number of elements */
726: DMDAGetElementsSizes(elas_da,&mxl,&myl,NULL);
728: /* !!! IN PARALLEL WE MUST MAKE SURE THE TWO DMDA's ALIGN !!! */
729: DMDAGetInfo(elas_da,0,0,0,0,&cpu_x,&cpu_y,0,0,0,0,0,0,0);
730: DMDAGetElementOwnershipRanges2d(elas_da,&lx,&ly);
732: prop_dof = (PetscInt)(sizeof(GaussPointCoefficients)/sizeof(PetscScalar)); /* gauss point setup */
733: prop_stencil_width = 0;
734: DMDACreate2d(PETSC_COMM_WORLD, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE,DMDA_STENCIL_BOX,mx,my,cpu_x,cpu_y,prop_dof,prop_stencil_width,lx,ly,&da_prop);
735: DMSetFromOptions(da_prop);
736: DMSetUp(da_prop);
738: PetscFree(lx);
739: PetscFree(ly);
741: /* define centroid positions */
742: DMDAGetInfo(da_prop,0,&M,&N,0,0,0,0,0,0,0,0,0,0);
743: dx = 1.0/((PetscReal)(M));
744: dy = 1.0/((PetscReal)(N));
746: DMDASetUniformCoordinates(da_prop,0.0+0.5*dx,1.0-0.5*dx,0.0+0.5*dy,1.0-0.5*dy,0.0,1.0);
748: /* define coefficients */
749: PetscOptionsGetInt(NULL,NULL,"-c_str",&coefficient_structure,NULL);
751: DMCreateGlobalVector(da_prop,&properties);
752: DMCreateLocalVector(da_prop,&l_properties);
753: DMDAVecGetArray(da_prop,l_properties,&element_props);
755: DMGetCoordinateDM(da_prop,&prop_cda);
756: DMGetCoordinatesLocal(da_prop,&prop_coords);
757: DMDAVecGetArray(prop_cda,prop_coords,&_prop_coords);
759: DMDAGetGhostCorners(prop_cda,&si,&sj,0,&nx,&ny,0);
761: DMGetCoordinateDM(elas_da,&vel_cda);
762: DMGetCoordinatesLocal(elas_da,&vel_coords);
763: DMDAVecGetArray(vel_cda,vel_coords,&_vel_coords);
765: /* interpolate the coordinates */
766: for (j = sj; j < sj+ny; j++) {
767: for (i = si; i < si+nx; i++) {
768: PetscInt ngp;
769: PetscScalar gp_xi[GAUSS_POINTS][2],gp_weight[GAUSS_POINTS];
770: PetscScalar el_coords[8];
772: GetElementCoords(_vel_coords,i,j,el_coords);
773: ConstructGaussQuadrature(&ngp,gp_xi,gp_weight);
775: for (p = 0; p < GAUSS_POINTS; p++) {
776: PetscScalar gp_x,gp_y;
777: PetscInt n;
778: PetscScalar xi_p[2],Ni_p[4];
780: xi_p[0] = gp_xi[p][0];
781: xi_p[1] = gp_xi[p][1];
782: ConstructQ12D_Ni(xi_p,Ni_p);
784: gp_x = 0.0;
785: gp_y = 0.0;
786: for (n = 0; n < NODES_PER_EL; n++) {
787: gp_x = gp_x+Ni_p[n]*el_coords[2*n];
788: gp_y = gp_y+Ni_p[n]*el_coords[2*n+1];
789: }
790: element_props[j][i].gp_coords[2*p] = gp_x;
791: element_props[j][i].gp_coords[2*p+1] = gp_y;
792: }
793: }
794: }
796: /* define the coefficients */
797: PetscOptionsGetBool(NULL,NULL,"-use_gp_coords",&use_gp_coords,&flg);
799: for (j = sj; j < sj+ny; j++) {
800: for (i = si; i < si+nx; i++) {
801: PetscScalar centroid_x = _prop_coords[j][i].x; /* centroids of cell */
802: PetscScalar centroid_y = _prop_coords[j][i].y;
803: PETSC_UNUSED PetscScalar coord_x,coord_y;
805: if (coefficient_structure == 0) { /* isotropic */
806: PetscScalar opts_E,opts_nu;
808: opts_E = 1.0;
809: opts_nu = 0.33;
810: PetscOptionsGetScalar(NULL,NULL,"-iso_E",&opts_E,&flg);
811: PetscOptionsGetScalar(NULL,NULL,"-iso_nu",&opts_nu,&flg);
813: for (p = 0; p < GAUSS_POINTS; p++) {
814: element_props[j][i].E[p] = opts_E;
815: element_props[j][i].nu[p] = opts_nu;
817: element_props[j][i].fx[p] = 0.0;
818: element_props[j][i].fy[p] = 0.0;
819: }
820: } else if (coefficient_structure == 1) { /* step */
821: PetscScalar opts_E0,opts_nu0,opts_xc;
822: PetscScalar opts_E1,opts_nu1;
824: opts_E0 = opts_E1 = 1.0;
825: opts_nu0 = opts_nu1 = 0.333;
826: opts_xc = 0.5;
827: PetscOptionsGetScalar(NULL,NULL,"-step_E0",&opts_E0,&flg);
828: PetscOptionsGetScalar(NULL,NULL,"-step_nu0",&opts_nu0,&flg);
829: PetscOptionsGetScalar(NULL,NULL,"-step_E1",&opts_E1,&flg);
830: PetscOptionsGetScalar(NULL,NULL,"-step_nu1",&opts_nu1,&flg);
831: PetscOptionsGetScalar(NULL,NULL,"-step_xc",&opts_xc,&flg);
833: for (p = 0; p < GAUSS_POINTS; p++) {
834: coord_x = centroid_x;
835: coord_y = centroid_y;
836: if (use_gp_coords) {
837: coord_x = element_props[j][i].gp_coords[2*p];
838: coord_y = element_props[j][i].gp_coords[2*p+1];
839: }
841: element_props[j][i].E[p] = opts_E0;
842: element_props[j][i].nu[p] = opts_nu0;
843: if (PetscRealPart(coord_x) > PetscRealPart(opts_xc)) {
844: element_props[j][i].E[p] = opts_E1;
845: element_props[j][i].nu[p] = opts_nu1;
846: }
848: element_props[j][i].fx[p] = 0.0;
849: element_props[j][i].fy[p] = 0.0;
850: }
851: } else if (coefficient_structure == 2) { /* brick */
852: PetscReal values_E[10];
853: PetscReal values_nu[10];
854: PetscInt nbricks,maxnbricks;
855: PetscInt index,span;
856: PetscInt jj;
858: flg = PETSC_FALSE;
859: maxnbricks = 10;
860: PetscOptionsGetRealArray(NULL,NULL, "-brick_E",values_E,&maxnbricks,&flg);
861: nbricks = maxnbricks;
864: flg = PETSC_FALSE;
865: maxnbricks = 10;
866: PetscOptionsGetRealArray(NULL,NULL, "-brick_nu",values_nu,&maxnbricks,&flg);
870: span = 1;
871: PetscOptionsGetInt(NULL,NULL,"-brick_span",&span,&flg);
873: /* cycle through the indices so that no two material properties are repeated in lines of x or y */
874: jj = (j/span)%nbricks;
875: index = (jj+i/span)%nbricks;
876: /*printf("j=%d: index = %d \n", j,index); */
878: for (p = 0; p < GAUSS_POINTS; p++) {
879: element_props[j][i].E[p] = values_E[index];
880: element_props[j][i].nu[p] = values_nu[index];
881: }
882: } else if (coefficient_structure == 3) { /* sponge */
883: PetscScalar opts_E0,opts_nu0;
884: PetscScalar opts_E1,opts_nu1;
885: PetscInt opts_t,opts_w;
886: PetscInt ii,jj,ci,cj;
888: opts_E0 = opts_E1 = 1.0;
889: opts_nu0 = opts_nu1 = 0.333;
890: PetscOptionsGetScalar(NULL,NULL,"-sponge_E0",&opts_E0,&flg);
891: PetscOptionsGetScalar(NULL,NULL,"-sponge_nu0",&opts_nu0,&flg);
892: PetscOptionsGetScalar(NULL,NULL,"-sponge_E1",&opts_E1,&flg);
893: PetscOptionsGetScalar(NULL,NULL,"-sponge_nu1",&opts_nu1,&flg);
895: opts_t = opts_w = 1;
896: PetscOptionsGetInt(NULL,NULL,"-sponge_t",&opts_t,&flg);
897: PetscOptionsGetInt(NULL,NULL,"-sponge_w",&opts_w,&flg);
899: ii = (i)/(opts_t+opts_w+opts_t);
900: jj = (j)/(opts_t+opts_w+opts_t);
902: ci = i - ii*(opts_t+opts_w+opts_t);
903: cj = j - jj*(opts_t+opts_w+opts_t);
905: for (p = 0; p < GAUSS_POINTS; p++) {
906: element_props[j][i].E[p] = opts_E0;
907: element_props[j][i].nu[p] = opts_nu0;
908: }
909: if ((ci >= opts_t) && (ci < opts_t+opts_w)) {
910: if ((cj >= opts_t) && (cj < opts_t+opts_w)) {
911: for (p = 0; p < GAUSS_POINTS; p++) {
912: element_props[j][i].E[p] = opts_E1;
913: element_props[j][i].nu[p] = opts_nu1;
914: }
915: }
916: }
917: } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_USER,"Unknown coefficient_structure");
918: }
919: }
920: DMDAVecRestoreArray(prop_cda,prop_coords,&_prop_coords);
922: DMDAVecRestoreArray(vel_cda,vel_coords,&_vel_coords);
924: DMDAVecRestoreArray(da_prop,l_properties,&element_props);
925: DMLocalToGlobalBegin(da_prop,l_properties,ADD_VALUES,properties);
926: DMLocalToGlobalEnd(da_prop,l_properties,ADD_VALUES,properties);
928: PetscOptionsGetBool(NULL,NULL,"-no_view",&no_view,NULL);
929: if (!no_view) {
930: DMDAViewCoefficientsGnuplot2d(da_prop,properties,"Coeffcients for elasticity eqn.","properties");
931: DMDACoordViewGnuplot2d(elas_da,"mesh");
932: }
934: /* Generate a matrix with the correct non-zero pattern of type AIJ. This will work in parallel and serial */
935: DMCreateMatrix(elas_da,&A);
936: DMGetCoordinates(elas_da,&vel_coords);
937: MatNullSpaceCreateRigidBody(vel_coords,&matnull);
938: MatSetNearNullSpace(A,matnull);
939: MatNullSpaceDestroy(&matnull);
940: MatCreateVecs(A,&f,&X);
942: /* assemble A11 */
943: MatZeroEntries(A);
944: VecZeroEntries(f);
946: AssembleA_Elasticity(A,elas_da,da_prop,properties);
947: /* build force vector */
948: AssembleF_Elasticity(f,elas_da,da_prop,properties);
950: KSPCreate(PETSC_COMM_WORLD,&ksp_E);
951: KSPSetOptionsPrefix(ksp_E,"elas_"); /* elasticity */
953: PetscOptionsGetBool(NULL,NULL,"-use_nonsymbc",&use_nonsymbc,&flg);
954: /* solve */
955: if (!use_nonsymbc) {
956: Mat AA;
957: Vec ff,XX;
958: IS is;
959: VecScatter scat;
961: DMDABCApplySymmetricCompression(elas_da,A,f,&is,&AA,&ff);
962: VecDuplicate(ff,&XX);
964: KSPSetOperators(ksp_E,AA,AA);
965: KSPSetFromOptions(ksp_E);
967: KSPSolve(ksp_E,ff,XX);
969: /* push XX back into X */
970: DMDABCApplyCompression(elas_da,NULL,X);
972: VecScatterCreate(XX,NULL,X,is,&scat);
973: VecScatterBegin(scat,XX,X,INSERT_VALUES,SCATTER_FORWARD);
974: VecScatterEnd(scat,XX,X,INSERT_VALUES,SCATTER_FORWARD);
975: VecScatterDestroy(&scat);
977: MatDestroy(&AA);
978: VecDestroy(&ff);
979: VecDestroy(&XX);
980: ISDestroy(&is);
981: } else {
982: DMDABCApplyCompression(elas_da,A,f);
984: KSPSetOperators(ksp_E,A,A);
985: KSPSetFromOptions(ksp_E);
987: KSPSolve(ksp_E,f,X);
988: }
990: if (!no_view) DMDAViewGnuplot2d(elas_da,X,"Displacement solution for elasticity eqn.","X");
991: KSPDestroy(&ksp_E);
993: VecDestroy(&X);
994: VecDestroy(&f);
995: MatDestroy(&A);
997: DMDestroy(&elas_da);
998: DMDestroy(&da_prop);
1000: VecDestroy(&properties);
1001: VecDestroy(&l_properties);
1002: return 0;
1003: }
1005: int main(int argc,char **args)
1006: {
1007: PetscInt mx,my;
1009: PetscInitialize(&argc,&args,(char*)0,help);
1010: mx = my = 10;
1011: PetscOptionsGetInt(NULL,NULL,"-mx",&mx,NULL);
1012: PetscOptionsGetInt(NULL,NULL,"-my",&my,NULL);
1013: solve_elasticity_2d(mx,my);
1014: PetscFinalize();
1015: return 0;
1016: }
1018: /* -------------------------- helpers for boundary conditions -------------------------------- */
1020: static PetscErrorCode BCApply_EAST(DM da,PetscInt d_idx,PetscScalar bc_val,Mat A,Vec b)
1021: {
1022: DM cda;
1023: Vec coords;
1024: PetscInt si,sj,nx,ny,i,j;
1025: PetscInt M,N;
1026: DMDACoor2d **_coords;
1027: const PetscInt *g_idx;
1028: PetscInt *bc_global_ids;
1029: PetscScalar *bc_vals;
1030: PetscInt nbcs;
1031: PetscInt n_dofs;
1032: ISLocalToGlobalMapping ltogm;
1035: /* enforce bc's */
1036: DMGetLocalToGlobalMapping(da,<ogm);
1037: ISLocalToGlobalMappingGetIndices(ltogm,&g_idx);
1039: DMGetCoordinateDM(da,&cda);
1040: DMGetCoordinatesLocal(da,&coords);
1041: DMDAVecGetArray(cda,coords,&_coords);
1042: DMDAGetGhostCorners(cda,&si,&sj,0,&nx,&ny,0);
1043: DMDAGetInfo(da,0,&M,&N,0,0,0,0,&n_dofs,0,0,0,0,0);
1045: /* --- */
1047: PetscMalloc1(ny*n_dofs,&bc_global_ids);
1048: PetscMalloc1(ny*n_dofs,&bc_vals);
1050: /* init the entries to -1 so VecSetValues will ignore them */
1051: for (i = 0; i < ny*n_dofs; i++) bc_global_ids[i] = -1;
1053: i = nx-1;
1054: for (j = 0; j < ny; j++) {
1055: PetscInt local_id;
1056: PETSC_UNUSED PetscScalar coordx,coordy;
1058: local_id = i+j*nx;
1060: bc_global_ids[j] = g_idx[n_dofs*local_id+d_idx];
1062: coordx = _coords[j+sj][i+si].x;
1063: coordy = _coords[j+sj][i+si].y;
1065: bc_vals[j] = bc_val;
1066: }
1067: ISLocalToGlobalMappingRestoreIndices(ltogm,&g_idx);
1068: nbcs = 0;
1069: if ((si+nx) == (M)) nbcs = ny;
1071: if (b) {
1072: VecSetValues(b,nbcs,bc_global_ids,bc_vals,INSERT_VALUES);
1073: VecAssemblyBegin(b);
1074: VecAssemblyEnd(b);
1075: }
1076: if (A) {
1077: MatZeroRows(A,nbcs,bc_global_ids,1.0,0,0);
1078: }
1080: PetscFree(bc_vals);
1081: PetscFree(bc_global_ids);
1083: DMDAVecRestoreArray(cda,coords,&_coords);
1084: return 0;
1085: }
1087: static PetscErrorCode BCApply_WEST(DM da,PetscInt d_idx,PetscScalar bc_val,Mat A,Vec b)
1088: {
1089: DM cda;
1090: Vec coords;
1091: PetscInt si,sj,nx,ny,i,j;
1092: PetscInt M,N;
1093: DMDACoor2d **_coords;
1094: const PetscInt *g_idx;
1095: PetscInt *bc_global_ids;
1096: PetscScalar *bc_vals;
1097: PetscInt nbcs;
1098: PetscInt n_dofs;
1099: ISLocalToGlobalMapping ltogm;
1102: /* enforce bc's */
1103: DMGetLocalToGlobalMapping(da,<ogm);
1104: ISLocalToGlobalMappingGetIndices(ltogm,&g_idx);
1106: DMGetCoordinateDM(da,&cda);
1107: DMGetCoordinatesLocal(da,&coords);
1108: DMDAVecGetArray(cda,coords,&_coords);
1109: DMDAGetGhostCorners(cda,&si,&sj,0,&nx,&ny,0);
1110: DMDAGetInfo(da,0,&M,&N,0,0,0,0,&n_dofs,0,0,0,0,0);
1112: /* --- */
1114: PetscMalloc1(ny*n_dofs,&bc_global_ids);
1115: PetscMalloc1(ny*n_dofs,&bc_vals);
1117: /* init the entries to -1 so VecSetValues will ignore them */
1118: for (i = 0; i < ny*n_dofs; i++) bc_global_ids[i] = -1;
1120: i = 0;
1121: for (j = 0; j < ny; j++) {
1122: PetscInt local_id;
1123: PETSC_UNUSED PetscScalar coordx,coordy;
1125: local_id = i+j*nx;
1127: bc_global_ids[j] = g_idx[n_dofs*local_id+d_idx];
1129: coordx = _coords[j+sj][i+si].x;
1130: coordy = _coords[j+sj][i+si].y;
1132: bc_vals[j] = bc_val;
1133: }
1134: ISLocalToGlobalMappingRestoreIndices(ltogm,&g_idx);
1135: nbcs = 0;
1136: if (si == 0) nbcs = ny;
1138: if (b) {
1139: VecSetValues(b,nbcs,bc_global_ids,bc_vals,INSERT_VALUES);
1140: VecAssemblyBegin(b);
1141: VecAssemblyEnd(b);
1142: }
1143: if (A) {
1144: MatZeroRows(A,nbcs,bc_global_ids,1.0,0,0);
1145: }
1147: PetscFree(bc_vals);
1148: PetscFree(bc_global_ids);
1150: DMDAVecRestoreArray(cda,coords,&_coords);
1151: return 0;
1152: }
1154: static PetscErrorCode DMDABCApplyCompression(DM elas_da,Mat A,Vec f)
1155: {
1157: BCApply_EAST(elas_da,0,-1.0,A,f);
1158: BCApply_EAST(elas_da,1, 0.0,A,f);
1159: BCApply_WEST(elas_da,0,1.0,A,f);
1160: BCApply_WEST(elas_da,1,0.0,A,f);
1161: return 0;
1162: }
1164: static PetscErrorCode Orthogonalize(PetscInt n,Vec *vecs)
1165: {
1166: PetscInt i,j;
1167: PetscScalar dot;
1169: for (i=0; i<n; i++) {
1170: VecNormalize(vecs[i],NULL);
1171: for (j=i+1; j<n; j++) {
1172: VecDot(vecs[i],vecs[j],&dot);
1173: VecAXPY(vecs[j],-dot,vecs[i]);
1174: }
1175: }
1176: return 0;
1177: }
1179: static PetscErrorCode DMDABCApplySymmetricCompression(DM elas_da,Mat A,Vec f,IS *dofs,Mat *AA,Vec *ff)
1180: {
1181: PetscInt start,end,m;
1182: PetscInt *unconstrained;
1183: PetscInt cnt,i;
1184: Vec x;
1185: PetscScalar *_x;
1186: IS is;
1187: VecScatter scat;
1190: /* push bc's into f and A */
1191: VecDuplicate(f,&x);
1192: BCApply_EAST(elas_da,0,-1.0,A,x);
1193: BCApply_EAST(elas_da,1, 0.0,A,x);
1194: BCApply_WEST(elas_da,0,1.0,A,x);
1195: BCApply_WEST(elas_da,1,0.0,A,x);
1197: /* define which dofs are not constrained */
1198: VecGetLocalSize(x,&m);
1199: PetscMalloc1(m,&unconstrained);
1200: VecGetOwnershipRange(x,&start,&end);
1201: VecGetArray(x,&_x);
1202: cnt = 0;
1203: for (i = 0; i < m; i+=2) {
1204: PetscReal val1,val2;
1206: val1 = PetscRealPart(_x[i]);
1207: val2 = PetscRealPart(_x[i+1]);
1208: if (PetscAbs(val1) < 0.1 && PetscAbs(val2) < 0.1) {
1209: unconstrained[cnt] = start + i;
1210: cnt++;
1211: unconstrained[cnt] = start + i + 1;
1212: cnt++;
1213: }
1214: }
1215: VecRestoreArray(x,&_x);
1217: ISCreateGeneral(PETSC_COMM_WORLD,cnt,unconstrained,PETSC_COPY_VALUES,&is);
1218: PetscFree(unconstrained);
1219: ISSetBlockSize(is,2);
1221: /* define correction for dirichlet in the rhs */
1222: MatMult(A,x,f);
1223: VecScale(f,-1.0);
1225: /* get new matrix */
1226: MatCreateSubMatrix(A,is,is,MAT_INITIAL_MATRIX,AA);
1227: /* get new vector */
1228: MatCreateVecs(*AA,NULL,ff);
1230: VecScatterCreate(f,is,*ff,NULL,&scat);
1231: VecScatterBegin(scat,f,*ff,INSERT_VALUES,SCATTER_FORWARD);
1232: VecScatterEnd(scat,f,*ff,INSERT_VALUES,SCATTER_FORWARD);
1234: { /* Constrain near-null space */
1235: PetscInt nvecs;
1236: const Vec *vecs;
1237: Vec *uvecs;
1238: PetscBool has_const;
1239: MatNullSpace mnull,unull;
1241: MatGetNearNullSpace(A,&mnull);
1242: MatNullSpaceGetVecs(mnull,&has_const,&nvecs,&vecs);
1243: VecDuplicateVecs(*ff,nvecs,&uvecs);
1244: for (i=0; i<nvecs; i++) {
1245: VecScatterBegin(scat,vecs[i],uvecs[i],INSERT_VALUES,SCATTER_FORWARD);
1246: VecScatterEnd(scat,vecs[i],uvecs[i],INSERT_VALUES,SCATTER_FORWARD);
1247: }
1248: Orthogonalize(nvecs,uvecs);
1249: MatNullSpaceCreate(PetscObjectComm((PetscObject)A),PETSC_FALSE,nvecs,uvecs,&unull);
1250: MatSetNearNullSpace(*AA,unull);
1251: MatNullSpaceDestroy(&unull);
1252: VecDestroyVecs(nvecs,&uvecs);
1253: }
1255: VecScatterDestroy(&scat);
1257: *dofs = is;
1258: VecDestroy(&x);
1259: return 0;
1260: }
1262: /*TEST
1264: build:
1265: requires: !complex !single
1267: test:
1268: args: -mx 20 -my 30 -elas_ksp_monitor_short -no_view -c_str 3 -sponge_E0 1 -sponge_E1 1000 -sponge_nu0 0.4 -sponge_nu1 0.2 -sponge_t 1 -sponge_w 8 -elas_ksp_rtol 5e-3 -elas_ksp_view
1269: output_file: output/ex49_1.out
1271: test:
1272: suffix: 2
1273: nsize: 4
1274: args: -mx 20 -my 30 -elas_ksp_monitor_short -no_view -c_str 3 -sponge_E0 1 -sponge_E1 1000 -sponge_nu0 0.4 -sponge_nu1 0.2 -sponge_t 1 -sponge_w 8 -elas_ksp_type gcr -elas_pc_type asm -elas_sub_pc_type lu -elas_ksp_rtol 5e-3
1276: test:
1277: suffix: 3
1278: nsize: 4
1279: args: -mx 20 -my 30 -elas_ksp_monitor_short -no_view -c_str 2 -brick_E 1,10,1000,100 -brick_nu 0.4,0.2,0.3,0.1 -brick_span 3 -elas_pc_type asm -elas_sub_pc_type lu -elas_ksp_rtol 5e-3
1281: test:
1282: suffix: 4
1283: nsize: 4
1284: args: -elas_ksp_monitor_short -elas_ksp_converged_reason -elas_ksp_type cg -elas_ksp_norm_type unpreconditioned -mx 40 -my 40 -c_str 2 -brick_E 1,1e-6,1e-2 -brick_nu .3,.2,.4 -brick_span 8 -elas_mg_levels_ksp_type chebyshev -elas_pc_type ml -elas_mg_levels_ksp_chebyshev_esteig 0,0.2,0,1.1 -elas_mg_levels_pc_type pbjacobi -elas_mg_levels_ksp_max_it 3 -use_nonsymbc -elas_pc_ml_nullspace user
1285: requires: ml
1287: test:
1288: suffix: 5
1289: nsize: 3
1290: args: -elas_ksp_monitor_short -elas_ksp_converged_reason -elas_ksp_type cg -elas_ksp_norm_type natural -mx 22 -my 22 -c_str 2 -brick_E 1,1e-6,1e-2 -brick_nu .3,.2,.4 -brick_span 8 -elas_pc_type gamg -elas_mg_levels_ksp_type chebyshev -elas_mg_levels_ksp_max_it 1 -elas_mg_levels_ksp_chebyshev_esteig 0.2,1.1 -elas_mg_levels_pc_type jacobi
1292: test:
1293: suffix: 6
1294: nsize: 4
1295: args: -mx 20 -my 30 -elas_ksp_monitor_short -no_view -c_str 3 -sponge_E0 1 -sponge_E1 1000 -sponge_nu0 0.4 -sponge_nu1 0.2 -sponge_t 1 -sponge_w 8 -elas_ksp_type pipegcr -elas_pc_type asm -elas_sub_pc_type lu
1297: test:
1298: suffix: 7
1299: nsize: 4
1300: args: -mx 20 -my 30 -elas_ksp_monitor_short -no_view -c_str 3 -sponge_E0 1 -sponge_E1 1000 -sponge_nu0 0.4 -sponge_nu1 0.2 -sponge_t 1 -sponge_w 8 -elas_ksp_type pipegcr -elas_pc_type asm -elas_sub_pc_type ksp -elas_sub_ksp_ksp_type cg -elas_sub_ksp_ksp_max_it 15
1302: test:
1303: suffix: 8
1304: nsize: 4
1305: args: -mx 20 -my 30 -elas_ksp_monitor_short -no_view -c_str 3 -sponge_E0 1 -sponge_E1 1000 -sponge_nu0 0.4 -sponge_nu1 0.2 -sponge_t 1 -sponge_w 8 -elas_ksp_type pipefgmres -elas_pc_type asm -elas_sub_pc_type ksp -elas_sub_ksp_ksp_type cg -elas_sub_ksp_ksp_max_it 15
1307: test:
1308: suffix: hypre_nullspace
1309: requires: hypre !defined(PETSC_HAVE_HYPRE_DEVICE)
1310: args: -elas_ksp_monitor_short -elas_ksp_converged_reason -elas_ksp_type cg -elas_ksp_norm_type natural -mx 22 -my 22 -c_str 2 -brick_E 1,1e-6,1e-2 -brick_nu .3,.2,.4 -brick_span 8 -elas_pc_type hypre -elas_pc_hypre_boomeramg_nodal_coarsen 6 -elas_pc_hypre_boomeramg_vec_interp_variant 3 -elas_pc_hypre_boomeramg_interp_type ext+i -elas_ksp_view
1312: test:
1313: nsize: 4
1314: suffix: bddc
1315: args: -elas_ksp_monitor_short -no_view -elas_ksp_converged_reason -elas_ksp_type cg -elas_ksp_norm_type natural -mx 22 -my 22 -dm_mat_type is -elas_pc_type bddc -elas_pc_bddc_monolithic
1317: test:
1318: nsize: 4
1319: suffix: bddc_unsym
1320: args: -elas_ksp_monitor_short -no_view -elas_ksp_converged_reason -elas_ksp_type cg -elas_ksp_norm_type natural -mx 22 -my 22 -dm_mat_type is -elas_pc_type bddc -elas_pc_bddc_monolithic -use_nonsymbc -elas_pc_bddc_symmetric 0
1322: test:
1323: nsize: 4
1324: suffix: bddc_unsym_deluxe
1325: args: -elas_ksp_monitor_short -no_view -elas_ksp_converged_reason -elas_ksp_type cg -elas_ksp_norm_type natural -mx 22 -my 22 -dm_mat_type is -elas_pc_type bddc -elas_pc_bddc_monolithic -use_nonsymbc -elas_pc_bddc_symmetric 0 -elas_pc_bddc_use_deluxe_scaling -elas_sub_schurs_symmetric 0
1327: test:
1328: nsize: 4
1329: suffix: fetidp_unsym_deluxe
1330: args: -elas_ksp_monitor_short -no_view -elas_ksp_converged_reason -elas_ksp_type fetidp -elas_fetidp_ksp_type cg -elas_ksp_norm_type natural -mx 22 -my 22 -dm_mat_type is -elas_fetidp_bddc_pc_bddc_monolithic -use_nonsymbc -elas_fetidp_bddc_pc_bddc_use_deluxe_scaling -elas_fetidp_bddc_sub_schurs_symmetric 0 -elas_fetidp_bddc_pc_bddc_deluxe_singlemat
1332: test:
1333: nsize: 4
1334: suffix: bddc_layerjump
1335: args: -mx 40 -my 40 -elas_ksp_monitor_short -no_view -c_str 3 -sponge_E0 1 -sponge_E1 1000 -sponge_nu0 0.4 -sponge_nu1 0.2 -sponge_t 1 -sponge_w 8 -elas_ksp_type cg -elas_pc_type bddc -elas_pc_bddc_monolithic -dm_mat_type is -elas_ksp_norm_type natural
1337: test:
1338: nsize: 4
1339: suffix: bddc_subdomainjump
1340: args: -mx 40 -my 40 -elas_ksp_monitor_short -no_view -c_str 2 -brick_E 1,1000 -brick_nu 0.4,0.2 -brick_span 20 -elas_ksp_type cg -elas_pc_type bddc -elas_pc_bddc_monolithic -dm_mat_type is -elas_pc_is_use_stiffness_scaling -elas_ksp_norm_type natural
1342: test:
1343: nsize: 9
1344: suffix: bddc_subdomainjump_deluxe
1345: args: -mx 30 -my 30 -elas_ksp_monitor_short -no_view -c_str 2 -brick_E 1,1000 -brick_nu 0.4,0.2 -brick_span 10 -elas_ksp_type cg -elas_pc_type bddc -elas_pc_bddc_monolithic -dm_mat_type is -elas_pc_bddc_use_deluxe_scaling -elas_ksp_norm_type natural -elas_pc_bddc_schur_layers 1
1346: TEST*/