Actual source code: ex10.c
2: static char help[] = "Linear elastiticty with dimensions using 20 node serendipity elements.\n\
3: This also demonstrates use of block\n\
4: diagonal data structure. Input arguments are:\n\
5: -m : problem size\n\n";
7: #include <petscksp.h>
9: /* This code is not intended as an efficient implementation, it is only
10: here to produce an interesting sparse matrix quickly.
12: PLEASE DO NOT BASE ANY OF YOUR CODES ON CODE LIKE THIS, THERE ARE MUCH
13: BETTER WAYS TO DO THIS. */
15: extern PetscErrorCode GetElasticityMatrix(PetscInt,Mat*);
16: extern PetscErrorCode Elastic20Stiff(PetscReal**);
17: extern PetscErrorCode AddElement(Mat,PetscInt,PetscInt,PetscReal**,PetscInt,PetscInt);
18: extern PetscErrorCode paulsetup20(void);
19: extern PetscErrorCode paulintegrate20(PetscReal K[60][60]);
21: int main(int argc,char **args)
22: {
23: Mat mat;
24: PetscInt i,its,m = 3,rdim,cdim,rstart,rend;
25: PetscMPIInt rank,size;
26: PetscScalar v,neg1 = -1.0;
27: Vec u,x,b;
28: KSP ksp;
29: PetscReal norm;
31: PetscInitialize(&argc,&args,(char*)0,help);
32: PetscOptionsGetInt(NULL,NULL,"-m",&m,NULL);
33: MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
34: MPI_Comm_size(PETSC_COMM_WORLD,&size);
36: /* Form matrix */
37: GetElasticityMatrix(m,&mat);
39: /* Generate vectors */
40: MatGetSize(mat,&rdim,&cdim);
41: MatGetOwnershipRange(mat,&rstart,&rend);
42: VecCreate(PETSC_COMM_WORLD,&u);
43: VecSetSizes(u,PETSC_DECIDE,rdim);
44: VecSetFromOptions(u);
45: VecDuplicate(u,&b);
46: VecDuplicate(b,&x);
47: for (i=rstart; i<rend; i++) {
48: v = (PetscScalar)(i-rstart + 100*rank);
49: VecSetValues(u,1,&i,&v,INSERT_VALUES);
50: }
51: VecAssemblyBegin(u);
52: VecAssemblyEnd(u);
54: /* Compute right-hand-side */
55: MatMult(mat,u,b);
57: /* Solve linear system */
58: KSPCreate(PETSC_COMM_WORLD,&ksp);
59: KSPSetOperators(ksp,mat,mat);
60: KSPSetFromOptions(ksp);
61: KSPSolve(ksp,b,x);
62: KSPGetIterationNumber(ksp,&its);
63: /* Check error */
64: VecAXPY(x,neg1,u);
65: VecNorm(x,NORM_2,&norm);
67: PetscPrintf(PETSC_COMM_WORLD,"Norm of residual %g Number of iterations %D\n",(double)norm,its);
69: /* Free work space */
70: KSPDestroy(&ksp);
71: VecDestroy(&u);
72: VecDestroy(&x);
73: VecDestroy(&b);
74: MatDestroy(&mat);
76: PetscFinalize();
77: return 0;
78: }
79: /* -------------------------------------------------------------------- */
80: /*
81: GetElasticityMatrix - Forms 3D linear elasticity matrix.
82: */
83: PetscErrorCode GetElasticityMatrix(PetscInt m,Mat *newmat)
84: {
85: PetscInt i,j,k,i1,i2,j_1,j2,k1,k2,h1,h2,shiftx,shifty,shiftz;
86: PetscInt ict,nz,base,r1,r2,N,*rowkeep,nstart;
87: IS iskeep;
88: PetscReal **K,norm;
89: Mat mat,submat = 0,*submatb;
90: MatType type = MATSEQBAIJ;
92: m /= 2; /* This is done just to be consistent with the old example */
93: N = 3*(2*m+1)*(2*m+1)*(2*m+1);
94: PetscPrintf(PETSC_COMM_SELF,"m = %D, N=%D\n",m,N);
95: MatCreateSeqAIJ(PETSC_COMM_SELF,N,N,80,NULL,&mat);
97: /* Form stiffness for element */
98: PetscMalloc1(81,&K);
99: for (i=0; i<81; i++) {
100: PetscMalloc1(81,&K[i]);
101: }
102: Elastic20Stiff(K);
104: /* Loop over elements and add contribution to stiffness */
105: shiftx = 3; shifty = 3*(2*m+1); shiftz = 3*(2*m+1)*(2*m+1);
106: for (k=0; k<m; k++) {
107: for (j=0; j<m; j++) {
108: for (i=0; i<m; i++) {
109: h1 = 0;
110: base = 2*k*shiftz + 2*j*shifty + 2*i*shiftx;
111: for (k1=0; k1<3; k1++) {
112: for (j_1=0; j_1<3; j_1++) {
113: for (i1=0; i1<3; i1++) {
114: h2 = 0;
115: r1 = base + i1*shiftx + j_1*shifty + k1*shiftz;
116: for (k2=0; k2<3; k2++) {
117: for (j2=0; j2<3; j2++) {
118: for (i2=0; i2<3; i2++) {
119: r2 = base + i2*shiftx + j2*shifty + k2*shiftz;
120: AddElement(mat,r1,r2,K,h1,h2);
121: h2 += 3;
122: }
123: }
124: }
125: h1 += 3;
126: }
127: }
128: }
129: }
130: }
131: }
133: for (i=0; i<81; i++) {
134: PetscFree(K[i]);
135: }
136: PetscFree(K);
138: MatAssemblyBegin(mat,MAT_FINAL_ASSEMBLY);
139: MatAssemblyEnd(mat,MAT_FINAL_ASSEMBLY);
141: /* Exclude any superfluous rows and columns */
142: nstart = 3*(2*m+1)*(2*m+1);
143: ict = 0;
144: PetscMalloc1(N-nstart,&rowkeep);
145: for (i=nstart; i<N; i++) {
146: MatGetRow(mat,i,&nz,0,0);
147: if (nz) rowkeep[ict++] = i;
148: MatRestoreRow(mat,i,&nz,0,0);
149: }
150: ISCreateGeneral(PETSC_COMM_SELF,ict,rowkeep,PETSC_COPY_VALUES,&iskeep);
151: MatCreateSubMatrices(mat,1,&iskeep,&iskeep,MAT_INITIAL_MATRIX,&submatb);
152: submat = *submatb;
153: PetscFree(submatb);
154: PetscFree(rowkeep);
155: ISDestroy(&iskeep);
156: MatDestroy(&mat);
158: /* Convert storage formats -- just to demonstrate conversion to various
159: formats (in particular, block diagonal storage). This is NOT the
160: recommended means to solve such a problem. */
161: MatConvert(submat,type,MAT_INITIAL_MATRIX,newmat);
162: MatDestroy(&submat);
164: MatNorm(*newmat,NORM_1,&norm);
165: PetscPrintf(PETSC_COMM_WORLD,"matrix 1 norm = %g\n",(double)norm);
167: return 0;
168: }
169: /* -------------------------------------------------------------------- */
170: PetscErrorCode AddElement(Mat mat,PetscInt r1,PetscInt r2,PetscReal **K,PetscInt h1,PetscInt h2)
171: {
172: PetscScalar val;
173: PetscInt l1,l2,row,col;
175: for (l1=0; l1<3; l1++) {
176: for (l2=0; l2<3; l2++) {
177: /*
178: NOTE you should never do this! Inserting values 1 at a time is
179: just too expensive!
180: */
181: if (K[h1+l1][h2+l2] != 0.0) {
182: row = r1+l1; col = r2+l2; val = K[h1+l1][h2+l2];
183: MatSetValues(mat,1,&row,1,&col,&val,ADD_VALUES);
184: row = r2+l2; col = r1+l1;
185: MatSetValues(mat,1,&row,1,&col,&val,ADD_VALUES);
186: }
187: }
188: }
189: return 0;
190: }
191: /* -------------------------------------------------------------------- */
192: PetscReal N[20][64]; /* Interpolation function. */
193: PetscReal part_N[3][20][64]; /* Partials of interpolation function. */
194: PetscReal rst[3][64]; /* Location of integration pts in (r,s,t) */
195: PetscReal weight[64]; /* Gaussian quadrature weights. */
196: PetscReal xyz[20][3]; /* (x,y,z) coordinates of nodes */
197: PetscReal E,nu; /* Physcial constants. */
198: PetscInt n_int,N_int; /* N_int = n_int^3, number of int. pts. */
199: /* Ordering of the vertices, (r,s,t) coordinates, of the canonical cell. */
200: PetscReal r2[20] = {-1.0,0.0,1.0,-1.0,1.0,-1.0,0.0,1.0,
201: -1.0,1.0,-1.0,1.0,
202: -1.0,0.0,1.0,-1.0,1.0,-1.0,0.0,1.0};
203: PetscReal s2[20] = {-1.0,-1.0, -1.0,0.0,0.0,1.0, 1.0, 1.0,
204: -1.0,-1.0,1.0,1.0,
205: -1.0,-1.0, -1.0,0.0,0.0,1.0, 1.0, 1.0};
206: PetscReal t2[20] = {-1.0,-1.0,-1.0,-1.0,-1.0,-1.0,-1.0,-1.0,
207: 0.0,0.0,0.0,0.0,
208: 1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0};
209: PetscInt rmap[20] = {0,1,2,3,5,6,7,8,9,11,15,17,18,19,20,21,23,24,25,26};
210: /* -------------------------------------------------------------------- */
211: /*
212: Elastic20Stiff - Forms 20 node elastic stiffness for element.
213: */
214: PetscErrorCode Elastic20Stiff(PetscReal **Ke)
215: {
216: PetscReal K[60][60],x,y,z,dx,dy,dz;
217: PetscInt i,j,k,l,Ii,J;
219: paulsetup20();
221: x = -1.0; y = -1.0; z = -1.0; dx = 2.0; dy = 2.0; dz = 2.0;
222: xyz[0][0] = x; xyz[0][1] = y; xyz[0][2] = z;
223: xyz[1][0] = x + dx; xyz[1][1] = y; xyz[1][2] = z;
224: xyz[2][0] = x + 2.*dx; xyz[2][1] = y; xyz[2][2] = z;
225: xyz[3][0] = x; xyz[3][1] = y + dy; xyz[3][2] = z;
226: xyz[4][0] = x + 2.*dx; xyz[4][1] = y + dy; xyz[4][2] = z;
227: xyz[5][0] = x; xyz[5][1] = y + 2.*dy; xyz[5][2] = z;
228: xyz[6][0] = x + dx; xyz[6][1] = y + 2.*dy; xyz[6][2] = z;
229: xyz[7][0] = x + 2.*dx; xyz[7][1] = y + 2.*dy; xyz[7][2] = z;
230: xyz[8][0] = x; xyz[8][1] = y; xyz[8][2] = z + dz;
231: xyz[9][0] = x + 2.*dx; xyz[9][1] = y; xyz[9][2] = z + dz;
232: xyz[10][0] = x; xyz[10][1] = y + 2.*dy; xyz[10][2] = z + dz;
233: xyz[11][0] = x + 2.*dx; xyz[11][1] = y + 2.*dy; xyz[11][2] = z + dz;
234: xyz[12][0] = x; xyz[12][1] = y; xyz[12][2] = z + 2.*dz;
235: xyz[13][0] = x + dx; xyz[13][1] = y; xyz[13][2] = z + 2.*dz;
236: xyz[14][0] = x + 2.*dx; xyz[14][1] = y; xyz[14][2] = z + 2.*dz;
237: xyz[15][0] = x; xyz[15][1] = y + dy; xyz[15][2] = z + 2.*dz;
238: xyz[16][0] = x + 2.*dx; xyz[16][1] = y + dy; xyz[16][2] = z + 2.*dz;
239: xyz[17][0] = x; xyz[17][1] = y + 2.*dy; xyz[17][2] = z + 2.*dz;
240: xyz[18][0] = x + dx; xyz[18][1] = y + 2.*dy; xyz[18][2] = z + 2.*dz;
241: xyz[19][0] = x + 2.*dx; xyz[19][1] = y + 2.*dy; xyz[19][2] = z + 2.*dz;
242: paulintegrate20(K);
244: /* copy the stiffness from K into format used by Ke */
245: for (i=0; i<81; i++) {
246: for (j=0; j<81; j++) {
247: Ke[i][j] = 0.0;
248: }
249: }
250: Ii = 0;
251: for (i=0; i<20; i++) {
252: J = 0;
253: for (j=0; j<20; j++) {
254: for (k=0; k<3; k++) {
255: for (l=0; l<3; l++) {
256: Ke[3*rmap[i]+k][3*rmap[j]+l] = K[Ii+k][J+l];
257: }
258: }
259: J += 3;
260: }
261: Ii += 3;
262: }
264: /* force the matrix to be exactly symmetric */
265: for (i=0; i<81; i++) {
266: for (j=0; j<i; j++) {
267: Ke[i][j] = (Ke[i][j] + Ke[j][i])/2.0;
268: }
269: }
270: return 0;
271: }
272: /* -------------------------------------------------------------------- */
273: /*
274: paulsetup20 - Sets up data structure for forming local elastic stiffness.
275: */
276: PetscErrorCode paulsetup20(void)
277: {
278: PetscInt i,j,k,cnt;
279: PetscReal x[4],w[4];
280: PetscReal c;
282: n_int = 3;
283: nu = 0.3;
284: E = 1.0;
286: /* Assign integration points and weights for
287: Gaussian quadrature formulae. */
288: if (n_int == 2) {
289: x[0] = (-0.577350269189626);
290: x[1] = (0.577350269189626);
291: w[0] = 1.0000000;
292: w[1] = 1.0000000;
293: } else if (n_int == 3) {
294: x[0] = (-0.774596669241483);
295: x[1] = 0.0000000;
296: x[2] = 0.774596669241483;
297: w[0] = 0.555555555555555;
298: w[1] = 0.888888888888888;
299: w[2] = 0.555555555555555;
300: } else if (n_int == 4) {
301: x[0] = (-0.861136311594053);
302: x[1] = (-0.339981043584856);
303: x[2] = 0.339981043584856;
304: x[3] = 0.861136311594053;
305: w[0] = 0.347854845137454;
306: w[1] = 0.652145154862546;
307: w[2] = 0.652145154862546;
308: w[3] = 0.347854845137454;
309: } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Value for n_int not supported");
311: /* rst[][i] contains the location of the i-th integration point
312: in the canonical (r,s,t) coordinate system. weight[i] contains
313: the Gaussian weighting factor. */
315: cnt = 0;
316: for (i=0; i<n_int; i++) {
317: for (j=0; j<n_int; j++) {
318: for (k=0; k<n_int; k++) {
319: rst[0][cnt] =x[i];
320: rst[1][cnt] =x[j];
321: rst[2][cnt] =x[k];
322: weight[cnt] = w[i]*w[j]*w[k];
323: ++cnt;
324: }
325: }
326: }
327: N_int = cnt;
329: /* N[][j] is the interpolation vector, N[][j] .* xyz[] */
330: /* yields the (x,y,z) locations of the integration point. */
331: /* part_N[][][j] is the partials of the N function */
332: /* w.r.t. (r,s,t). */
334: c = 1.0/8.0;
335: for (j=0; j<N_int; j++) {
336: for (i=0; i<20; i++) {
337: if (i==0 || i==2 || i==5 || i==7 || i==12 || i==14 || i== 17 || i==19) {
338: N[i][j] = c*(1.0 + r2[i]*rst[0][j])*
339: (1.0 + s2[i]*rst[1][j])*(1.0 + t2[i]*rst[2][j])*
340: (-2.0 + r2[i]*rst[0][j] + s2[i]*rst[1][j] + t2[i]*rst[2][j]);
341: part_N[0][i][j] = c*r2[i]*(1 + s2[i]*rst[1][j])*(1 + t2[i]*rst[2][j])*
342: (-1.0 + 2.0*r2[i]*rst[0][j] + s2[i]*rst[1][j] +
343: t2[i]*rst[2][j]);
344: part_N[1][i][j] = c*s2[i]*(1 + r2[i]*rst[0][j])*(1 + t2[i]*rst[2][j])*
345: (-1.0 + r2[i]*rst[0][j] + 2.0*s2[i]*rst[1][j] +
346: t2[i]*rst[2][j]);
347: part_N[2][i][j] = c*t2[i]*(1 + r2[i]*rst[0][j])*(1 + s2[i]*rst[1][j])*
348: (-1.0 + r2[i]*rst[0][j] + s2[i]*rst[1][j] +
349: 2.0*t2[i]*rst[2][j]);
350: } else if (i==1 || i==6 || i==13 || i==18) {
351: N[i][j] = .25*(1.0 - rst[0][j]*rst[0][j])*
352: (1.0 + s2[i]*rst[1][j])*(1.0 + t2[i]*rst[2][j]);
353: part_N[0][i][j] = -.5*rst[0][j]*(1 + s2[i]*rst[1][j])*
354: (1 + t2[i]*rst[2][j]);
355: part_N[1][i][j] = .25*s2[i]*(1 + t2[i]*rst[2][j])*
356: (1.0 - rst[0][j]*rst[0][j]);
357: part_N[2][i][j] = .25*t2[i]*(1.0 - rst[0][j]*rst[0][j])*
358: (1 + s2[i]*rst[1][j]);
359: } else if (i==3 || i==4 || i==15 || i==16) {
360: N[i][j] = .25*(1.0 - rst[1][j]*rst[1][j])*
361: (1.0 + r2[i]*rst[0][j])*(1.0 + t2[i]*rst[2][j]);
362: part_N[0][i][j] = .25*r2[i]*(1 + t2[i]*rst[2][j])*
363: (1.0 - rst[1][j]*rst[1][j]);
364: part_N[1][i][j] = -.5*rst[1][j]*(1 + r2[i]*rst[0][j])*
365: (1 + t2[i]*rst[2][j]);
366: part_N[2][i][j] = .25*t2[i]*(1.0 - rst[1][j]*rst[1][j])*
367: (1 + r2[i]*rst[0][j]);
368: } else if (i==8 || i==9 || i==10 || i==11) {
369: N[i][j] = .25*(1.0 - rst[2][j]*rst[2][j])*
370: (1.0 + r2[i]*rst[0][j])*(1.0 + s2[i]*rst[1][j]);
371: part_N[0][i][j] = .25*r2[i]*(1 + s2[i]*rst[1][j])*
372: (1.0 - rst[2][j]*rst[2][j]);
373: part_N[1][i][j] = .25*s2[i]*(1.0 - rst[2][j]*rst[2][j])*
374: (1 + r2[i]*rst[0][j]);
375: part_N[2][i][j] = -.5*rst[2][j]*(1 + r2[i]*rst[0][j])*
376: (1 + s2[i]*rst[1][j]);
377: }
378: }
379: }
380: return 0;
381: }
382: /* -------------------------------------------------------------------- */
383: /*
384: paulintegrate20 - Does actual numerical integration on 20 node element.
385: */
386: PetscErrorCode paulintegrate20(PetscReal K[60][60])
387: {
388: PetscReal det_jac,jac[3][3],inv_jac[3][3];
389: PetscReal B[6][60],B_temp[6][60],C[6][6];
390: PetscReal temp;
391: PetscInt i,j,k,step;
393: /* Zero out K, since we will accumulate the result here */
394: for (i=0; i<60; i++) {
395: for (j=0; j<60; j++) {
396: K[i][j] = 0.0;
397: }
398: }
400: /* Loop over integration points ... */
401: for (step=0; step<N_int; step++) {
403: /* Compute the Jacobian, its determinant, and inverse. */
404: for (i=0; i<3; i++) {
405: for (j=0; j<3; j++) {
406: jac[i][j] = 0;
407: for (k=0; k<20; k++) {
408: jac[i][j] += part_N[i][k][step]*xyz[k][j];
409: }
410: }
411: }
412: det_jac = jac[0][0]*(jac[1][1]*jac[2][2]-jac[1][2]*jac[2][1])
413: + jac[0][1]*(jac[1][2]*jac[2][0]-jac[1][0]*jac[2][2])
414: + jac[0][2]*(jac[1][0]*jac[2][1]-jac[1][1]*jac[2][0]);
415: inv_jac[0][0] = (jac[1][1]*jac[2][2]-jac[1][2]*jac[2][1])/det_jac;
416: inv_jac[0][1] = (jac[0][2]*jac[2][1]-jac[0][1]*jac[2][2])/det_jac;
417: inv_jac[0][2] = (jac[0][1]*jac[1][2]-jac[1][1]*jac[0][2])/det_jac;
418: inv_jac[1][0] = (jac[1][2]*jac[2][0]-jac[1][0]*jac[2][2])/det_jac;
419: inv_jac[1][1] = (jac[0][0]*jac[2][2]-jac[2][0]*jac[0][2])/det_jac;
420: inv_jac[1][2] = (jac[0][2]*jac[1][0]-jac[0][0]*jac[1][2])/det_jac;
421: inv_jac[2][0] = (jac[1][0]*jac[2][1]-jac[1][1]*jac[2][0])/det_jac;
422: inv_jac[2][1] = (jac[0][1]*jac[2][0]-jac[0][0]*jac[2][1])/det_jac;
423: inv_jac[2][2] = (jac[0][0]*jac[1][1]-jac[1][0]*jac[0][1])/det_jac;
425: /* Compute the B matrix. */
426: for (i=0; i<3; i++) {
427: for (j=0; j<20; j++) {
428: B_temp[i][j] = 0.0;
429: for (k=0; k<3; k++) {
430: B_temp[i][j] += inv_jac[i][k]*part_N[k][j][step];
431: }
432: }
433: }
434: for (i=0; i<6; i++) {
435: for (j=0; j<60; j++) {
436: B[i][j] = 0.0;
437: }
438: }
440: /* Put values in correct places in B. */
441: for (k=0; k<20; k++) {
442: B[0][3*k] = B_temp[0][k];
443: B[1][3*k+1] = B_temp[1][k];
444: B[2][3*k+2] = B_temp[2][k];
445: B[3][3*k] = B_temp[1][k];
446: B[3][3*k+1] = B_temp[0][k];
447: B[4][3*k+1] = B_temp[2][k];
448: B[4][3*k+2] = B_temp[1][k];
449: B[5][3*k] = B_temp[2][k];
450: B[5][3*k+2] = B_temp[0][k];
451: }
453: /* Construct the C matrix, uses the constants "nu" and "E". */
454: for (i=0; i<6; i++) {
455: for (j=0; j<6; j++) {
456: C[i][j] = 0.0;
457: }
458: }
459: temp = (1.0 + nu)*(1.0 - 2.0*nu);
460: temp = E/temp;
461: C[0][0] = temp*(1.0 - nu);
462: C[1][1] = C[0][0];
463: C[2][2] = C[0][0];
464: C[3][3] = temp*(0.5 - nu);
465: C[4][4] = C[3][3];
466: C[5][5] = C[3][3];
467: C[0][1] = temp*nu;
468: C[0][2] = C[0][1];
469: C[1][0] = C[0][1];
470: C[1][2] = C[0][1];
471: C[2][0] = C[0][1];
472: C[2][1] = C[0][1];
474: for (i=0; i<6; i++) {
475: for (j=0; j<60; j++) {
476: B_temp[i][j] = 0.0;
477: for (k=0; k<6; k++) {
478: B_temp[i][j] += C[i][k]*B[k][j];
479: }
480: B_temp[i][j] *= det_jac;
481: }
482: }
484: /* Accumulate B'*C*B*det(J)*weight, as a function of (r,s,t), in K. */
485: for (i=0; i<60; i++) {
486: for (j=0; j<60; j++) {
487: temp = 0.0;
488: for (k=0; k<6; k++) {
489: temp += B[k][i]*B_temp[k][j];
490: }
491: K[i][j] += temp*weight[step];
492: }
493: }
494: } /* end of loop over integration points */
495: return 0;
496: }
498: /*TEST
500: test:
501: args: -matconvert_type seqaij -ksp_monitor_short -ksp_rtol 1.e-2 -pc_type jacobi
502: requires: x
504: TEST*/