Actual source code: ex56.c
petsc-3.4.2 2013-07-02
1: static char help[] = "3D, bi-linear quadrilateral (Q1), displacement finite element formulation\n\
2: of linear elasticity. E=1.0, nu=0.25.\n\
3: Unit square domain with Dirichelet boundary condition on the y=0 side only.\n\
4: Load of 1.0 in x + 2y direction on all nodes (not a true uniform load).\n\
5: -ne <size> : number of (square) quadrilateral elements in each dimension\n\
6: -alpha <v> : scaling of material coeficient in embedded circle\n\n";
8: #include <petscksp.h>
10: static PetscBool log_stages = PETSC_TRUE;
11: static PetscErrorCode MaybeLogStagePush(PetscLogStage stage) { return log_stages ? PetscLogStagePush(stage) : 0; }
12: static PetscErrorCode MaybeLogStagePop() { return log_stages ? PetscLogStagePop() : 0; }
16: int main(int argc,char **args)
17: {
18: Mat Amat;
20: PetscInt m,nn,M,Istart,Iend,i,j,k,ii,jj,kk,ic,ne=4,id;
21: PetscReal x,y,z,h,*coords,soft_alpha=1.e-3;
22: PetscBool two_solves = PETSC_FALSE,test_nonzero_cols = PETSC_FALSE;
23: Vec xx,bb;
24: KSP ksp;
25: MPI_Comm comm;
26: PetscMPIInt npe,mype;
27: PC pc;
28: PetscScalar DD[24][24],DD2[24][24];
29: PetscLogStage stage[6];
30: PetscScalar DD1[24][24];
31: PCType type;
33: PetscInitialize(&argc,&args,(char*)0,help);
34: comm = PETSC_COMM_WORLD;
35: MPI_Comm_rank(comm, &mype);
36: MPI_Comm_size(comm, &npe);
38: PetscOptionsBegin(comm,NULL,"3D bilinear Q1 elasticity options","");
39: {
40: char nestring[256];
41: PetscSNPrintf(nestring,sizeof nestring,"number of elements in each direction, ne+1 must be a multiple of %D (nprocs^{1/3})",(PetscInt)(PetscPowReal((PetscReal)npe,1./3.) + .5));
42: PetscOptionsInt("-ne",nestring,"",ne,&ne,NULL);
43: PetscOptionsBool("-log_stages","Log stages of solve separately","",log_stages,&log_stages,NULL);
44: PetscOptionsReal("-alpha","material coefficient inside circle","",soft_alpha,&soft_alpha,NULL);
45: PetscOptionsBool("-two_solves","solve additional variant of the problem","",two_solves,&two_solves,NULL);
46: PetscOptionsBool("-test_nonzero_cols","nonzero test","",test_nonzero_cols,&test_nonzero_cols,NULL);
47: }
48: PetscOptionsEnd();
50: if (log_stages) {
51: PetscLogStageRegister("Setup", &stage[0]);
52: PetscLogStageRegister("Solve", &stage[1]);
53: PetscLogStageRegister("2nd Setup", &stage[2]);
54: PetscLogStageRegister("2nd Solve", &stage[3]);
55: PetscLogStageRegister("3rd Setup", &stage[4]);
56: PetscLogStageRegister("3rd Solve", &stage[5]);
57: } else {
58: for (i=0; i<sizeof(stage)/sizeof(stage[0]); i++) stage[i] = -1;
59: }
61: h = 1./ne; nn = ne+1;
62: /* ne*ne; number of global elements */
63: M = 3*nn*nn*nn; /* global number of equations */
64: if (npe==2) {
65: if (mype==1) m=0;
66: else m = nn*nn*nn;
67: npe = 1;
68: } else {
69: m = nn*nn*nn/npe;
70: if (mype==npe-1) m = nn*nn*nn - (npe-1)*m;
71: }
72: m *= 3; /* number of equations local*/
73: /* Setup solver, get PC type and pc */
74: KSPCreate(PETSC_COMM_WORLD,&ksp);
75: KSPSetType(ksp, KSPCG);
76: KSPSetComputeSingularValues(ksp, PETSC_TRUE);
77: KSPGetPC(ksp, &pc);
78: PCSetType(pc, PCGAMG); /* default */
79: KSPSetFromOptions(ksp);
80: PCGetType(pc, &type);
82: {
83: /* configureation */
84: const PetscInt NP = (PetscInt)(PetscPowReal((PetscReal)npe,1./3.) + .5);
85: if (npe!=NP*NP*NP) SETERRQ1(comm,PETSC_ERR_ARG_WRONG, "npe=%d: npe^{1/3} must be integer",npe);
86: if (nn!=NP*(nn/NP)) SETERRQ1(comm,PETSC_ERR_ARG_WRONG, "-ne %d: (ne+1)%(npe^{1/3}) must equal zero",ne);
87: const PetscInt ipx = mype%NP, ipy = (mype%(NP*NP))/NP, ipz = mype/(NP*NP);
88: const PetscInt Ni0 = ipx*(nn/NP), Nj0 = ipy*(nn/NP), Nk0 = ipz*(nn/NP);
89: const PetscInt Ni1 = Ni0 + (m>0 ? (nn/NP) : 0), Nj1 = Nj0 + (nn/NP), Nk1 = Nk0 + (nn/NP);
90: const PetscInt NN = nn/NP, id0 = ipz*nn*nn*NN + ipy*nn*NN*NN + ipx*NN*NN*NN;
91: PetscInt *d_nnz, *o_nnz,osz[4]={0,9,15,19},nbc;
92: PetscScalar vv[24], v2[24];
94: /* count nnz */
95: PetscMalloc((m+1)*sizeof(PetscInt), &d_nnz);
96: PetscMalloc((m+1)*sizeof(PetscInt), &o_nnz);
97: for (i=Ni0,ic=0; i<Ni1; i++) {
98: for (j=Nj0; j<Nj1; j++) {
99: for (k=Nk0; k<Nk1; k++) {
100: nbc = 0;
101: if (i==Ni0 || i==Ni1-1) nbc++;
102: if (j==Nj0 || j==Nj1-1) nbc++;
103: if (k==Nk0 || k==Nk1-1) nbc++;
104: for (jj=0; jj<3; jj++,ic++) {
105: d_nnz[ic] = 3*(27-osz[nbc]);
106: o_nnz[ic] = 3*osz[nbc];
107: }
108: }
109: }
110: }
111: if (ic != m) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"ic %D does not equal m %D",ic,m);
113: /* create stiffness matrix */
114: MatCreate(comm,&Amat);
115: MatSetSizes(Amat,m,m,M,M);
116: MatSetBlockSize(Amat,3);
117: MatSetType(Amat,MATAIJ);
118: MatSeqAIJSetPreallocation(Amat,0,d_nnz);
119: MatMPIAIJSetPreallocation(Amat,0,d_nnz,0,o_nnz);
121: PetscFree(d_nnz);
122: PetscFree(o_nnz);
124: MatGetOwnershipRange(Amat,&Istart,&Iend);
126: if (m != Iend - Istart) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_PLIB,"m %D does not equal Iend %D - Istart %D",m,Iend,Istart);
127: /* Generate vectors */
128: VecCreate(comm,&xx);
129: VecSetSizes(xx,m,M);
130: VecSetBlockSize(xx,3);
131: VecSetFromOptions(xx);
132: VecDuplicate(xx,&bb);
133: VecSet(bb,.0);
134: /* generate element matrices */
135: {
136: FILE *file;
137: char fname[] = "data/elem_3d_elast_v_25.txt";
138: file = fopen(fname, "r");
139: if (file == 0) {
140: PetscPrintf(PETSC_COMM_WORLD,"\t%s failed to open input file '%s'\n",__FUNCT__,fname);
141: for (i=0; i<24; i++) {
142: for (j=0; j<24; j++) {
143: if (i==j) DD1[i][j] = 1.0;
144: else DD1[i][j] = -.25;
145: }
146: }
147: } else {
148: for (i=0; i<24; i++) {
149: for (j=0; j<24; j++) {
150: fscanf(file, "%le", &DD1[i][j]);
151: }
152: }
153: }
154: fclose(file);
155: /* BC version of element */
156: for (i=0; i<24; i++) {
157: for (j=0; j<24; j++) {
158: if (i<12 || (j < 12 && !test_nonzero_cols)) {
159: if (i==j) DD2[i][j] = 0.1*DD1[i][j];
160: else DD2[i][j] = 0.0;
161: } else DD2[i][j] = DD1[i][j];
162: }
163: }
164: /* element residual/load vector */
165: for (i=0; i<24; i++) {
166: if (i%3==0) vv[i] = h*h;
167: else if (i%3==1) vv[i] = 2.0*h*h;
168: else vv[i] = .0;
169: }
170: for (i=0; i<24; i++) {
171: if (i%3==0 && i>=12) v2[i] = h*h;
172: else if (i%3==1 && i>=12) v2[i] = 2.0*h*h;
173: else v2[i] = .0;
174: }
175: }
177: PetscMalloc((m+1)*sizeof(PetscReal), &coords);
178: coords[m] = -99.0;
180: /* forms the element stiffness and coordinates */
181: for (i=Ni0,ic=0,ii=0; i<Ni1; i++,ii++) {
182: for (j=Nj0,jj=0; j<Nj1; j++,jj++) {
183: for (k=Nk0,kk=0; k<Nk1; k++,kk++,ic++) {
185: /* coords */
186: x = coords[3*ic] = h*(PetscReal)i;
187: y = coords[3*ic+1] = h*(PetscReal)j;
188: z = coords[3*ic+2] = h*(PetscReal)k;
189: /* matrix */
190: id = id0 + ii + NN*jj + NN*NN*kk;
192: if (i<ne && j<ne && k<ne) {
193: /* radius */
194: PetscReal radius = PetscSqrtScalar((x-.5+h/2)*(x-.5+h/2)+(y-.5+h/2)*(y-.5+h/2)+
195: (z-.5+h/2)*(z-.5+h/2));
196: PetscReal alpha = 1.0;
197: PetscInt jx,ix,idx[8] = { id, id+1, id+NN+1, id+NN,
198: id + NN*NN, id+1 + NN*NN,
199: id+NN+1 + NN*NN, id+NN + NN*NN };
201: /* correct indices */
202: if (i==Ni1-1 && Ni1!=nn) {
203: idx[1] += NN*(NN*NN-1);
204: idx[2] += NN*(NN*NN-1);
205: idx[5] += NN*(NN*NN-1);
206: idx[6] += NN*(NN*NN-1);
207: }
208: if (j==Nj1-1 && Nj1!=nn) {
209: idx[2] += NN*NN*(nn-1);
210: idx[3] += NN*NN*(nn-1);
211: idx[6] += NN*NN*(nn-1);
212: idx[7] += NN*NN*(nn-1);
213: }
214: if (k==Nk1-1 && Nk1!=nn) {
215: idx[4] += NN*(nn*nn-NN*NN);
216: idx[5] += NN*(nn*nn-NN*NN);
217: idx[6] += NN*(nn*nn-NN*NN);
218: idx[7] += NN*(nn*nn-NN*NN);
219: }
221: if (radius < 0.25) alpha = soft_alpha;
223: for (ix=0; ix<24; ix++) {
224: for (jx=0;jx<24;jx++) DD[ix][jx] = alpha*DD1[ix][jx];
225: }
226: if (k>0) {
227: MatSetValuesBlocked(Amat,8,idx,8,idx,(const PetscScalar*)DD,ADD_VALUES);
228: VecSetValuesBlocked(bb,8,idx,(const PetscScalar*)vv,ADD_VALUES);
229: } else {
230: /* a BC */
231: for (ix=0;ix<24;ix++) {
232: for (jx=0;jx<24;jx++) DD[ix][jx] = alpha*DD2[ix][jx];
233: }
234: MatSetValuesBlocked(Amat,8,idx,8,idx,(const PetscScalar*)DD,ADD_VALUES);
235: VecSetValuesBlocked(bb,8,idx,(const PetscScalar*)v2,ADD_VALUES);
236: }
237: }
238: }
239: }
241: }
242: MatAssemblyBegin(Amat,MAT_FINAL_ASSEMBLY);
243: MatAssemblyEnd(Amat,MAT_FINAL_ASSEMBLY);
244: VecAssemblyBegin(bb);
245: VecAssemblyEnd(bb);
246: }
248: if (!PETSC_TRUE) {
249: PetscViewer viewer;
250: PetscViewerASCIIOpen(comm, "Amat.m", &viewer);
251: PetscViewerSetFormat(viewer, PETSC_VIEWER_ASCII_MATLAB);
252: MatView(Amat,viewer);
253: PetscViewerDestroy(&viewer);
254: }
256: /* finish KSP/PC setup */
257: KSPSetOperators(ksp, Amat, Amat, SAME_NONZERO_PATTERN);
258: PCSetCoordinates(pc, 3, m/3, coords);
260: MaybeLogStagePush(stage[0]);
262: /* PC setup basically */
263: KSPSetUp(ksp);
265: MaybeLogStagePop();
266: MaybeLogStagePush(stage[1]);
268: /* test BCs */
269: if (test_nonzero_cols) {
270: VecZeroEntries(xx);
271: if (mype==0) VecSetValue(xx,0,1.0,INSERT_VALUES);
272: VecAssemblyBegin(xx);
273: VecAssemblyEnd(xx);
274: KSPSetInitialGuessNonzero(ksp,PETSC_TRUE);
275: }
277: /* 1st solve */
278: KSPSolve(ksp, bb, xx);
280: MaybeLogStagePop();
282: /* 2nd solve */
283: if (two_solves) {
284: PetscReal emax, emin;
285: MaybeLogStagePush(stage[2]);
286: /* PC setup basically */
287: MatScale(Amat, 100000.0);
288: KSPSetOperators(ksp, Amat, Amat, SAME_NONZERO_PATTERN);
289: KSPSetUp(ksp);
291: MaybeLogStagePop();
292: MaybeLogStagePush(stage[3]);
293: KSPSolve(ksp, bb, xx);
294: KSPComputeExtremeSingularValues(ksp, &emax, &emin);
296: MaybeLogStagePop();
297: MaybeLogStagePush(stage[4]);
299: /* 3rd solve */
300: MatScale(Amat, 100000.0);
301: KSPSetOperators(ksp, Amat, Amat, SAME_NONZERO_PATTERN);
302: KSPSetUp(ksp);
304: MaybeLogStagePop();
305: MaybeLogStagePush(stage[5]);
307: KSPSolve(ksp, bb, xx);
309: MaybeLogStagePop();
311: PetscReal norm,norm2;
312: /* PetscViewer viewer; */
313: Vec res;
315: VecNorm(bb, NORM_2, &norm2);
317: VecDuplicate(xx, &res);
318: MatMult(Amat, xx, res);
319: VecAXPY(bb, -1.0, res);
320: VecDestroy(&res);
321: VecNorm(bb, NORM_2, &norm);
322: PetscPrintf(PETSC_COMM_WORLD,"[%d]%s |b-Ax|/|b|=%e, |b|=%e, emax=%e\n",0,__FUNCT__,norm/norm2,norm2,emax);
323: /*PetscViewerASCIIOpen(comm, "residual.m", &viewer);
324: PetscViewerSetFormat(viewer, PETSC_VIEWER_ASCII_MATLAB);
325: VecView(bb,viewer);
326: PetscViewerDestroy(&viewer);*/
329: /* PetscViewerASCIIOpen(comm, "rhs.m", &viewer); */
330: /* PetscViewerSetFormat(viewer, PETSC_VIEWER_ASCII_MATLAB); */
331: /* */
332: /* VecView(bb,viewer); */
333: /* PetscViewerDestroy(&viewer); */
335: /* PetscViewerASCIIOpen(comm, "solution.m", &viewer); */
336: /* PetscViewerSetFormat(viewer, PETSC_VIEWER_ASCII_MATLAB); */
337: /* */
338: /* VecView(xx, viewer); */
339: /* PetscViewerDestroy(&viewer); */
340: }
342: /* Free work space */
343: KSPDestroy(&ksp);
344: VecDestroy(&xx);
345: VecDestroy(&bb);
346: MatDestroy(&Amat);
347: PetscFree(coords);
349: PetscFinalize();
350: return 0;
351: }