Actual source code: ex54.c
petsc-3.9.4 2018-09-11
2: static char help[] = "Creates a matrix from quadrilateral finite elements in 2D, Laplacian \n\
3: -ne <size> : problem size in number of elements (eg, -ne 31 gives 32^2 grid)\n\
4: -alpha <v> : scaling of material coeficient in embedded circle\n\n";
6: #include <petscksp.h>
8: int main(int argc,char **args)
9: {
10: Mat Amat,Pmat;
12: PetscInt i,m,M,its,Istart,Iend,j,Ii,ix,ne=4;
13: PetscReal x,y,h;
14: Vec xx,bb;
15: KSP ksp;
16: PetscReal soft_alpha = 1.e-3;
17: MPI_Comm comm;
18: PetscMPIInt npe,mype;
19: PetscScalar DD[4][4],DD2[4][4];
20: #if defined(PETSC_USE_LOG)
21: PetscLogStage stage;
22: #endif
23: #define DIAG_S 0.0
24: PetscScalar DD1[4][4] = { {5.0+DIAG_S, -2.0, -1.0, -2.0},
25: {-2.0, 5.0+DIAG_S, -2.0, -1.0},
26: {-1.0, -2.0, 5.0+DIAG_S, -2.0},
27: {-2.0, -1.0, -2.0, 5.0+DIAG_S} };
29: PetscInitialize(&argc,&args,(char*)0,help);if (ierr) return ierr;
30: comm = PETSC_COMM_WORLD;
31: MPI_Comm_rank(comm, &mype);
32: MPI_Comm_size(comm, &npe);
33: PetscOptionsGetInt(NULL,NULL,"-ne",&ne,NULL);
34: h = 1./ne;
35: /* ne*ne; number of global elements */
36: PetscOptionsGetReal(NULL,NULL,"-alpha",&soft_alpha,NULL);
37: M = (ne+1)*(ne+1); /* global number of nodes */
39: /* create stiffness matrix (2) */
40: MatCreate(comm,&Amat);
41: MatSetSizes(Amat,PETSC_DECIDE,PETSC_DECIDE,M,M);
42: MatSetType(Amat,MATMPIAIJ);
43: MatMPIAIJSetPreallocation(Amat,81,NULL,57,NULL);
45: MatCreate(comm,&Pmat);
46: MatSetSizes(Pmat,PETSC_DECIDE,PETSC_DECIDE,M,M);
47: MatSetType(Pmat,MATMPIAIJ);
48: MatMPIAIJSetPreallocation(Pmat,81,NULL,57,NULL);
50: MatSetFromOptions(Amat);
51: MatSetFromOptions(Pmat);
53: /* vectors */
54: MatCreateVecs(Amat,&bb,&xx);
55: VecSet(bb,.0);
56: /* generate element matrices -- see ex56.c on how to use different data set */
57: {
58: DD1[0][0] = 0.66666666666666663;
59: DD1[0][1] = -0.16666666666666669;
60: DD1[0][2] = -0.33333333333333343;
61: DD1[0][3] = -0.16666666666666666;
62: DD1[1][0] = -0.16666666666666669;
63: DD1[1][1] = 0.66666666666666663;
64: DD1[1][2] = -0.16666666666666666;
65: DD1[1][3] = -0.33333333333333343;
66: DD1[2][0] = -0.33333333333333343;
67: DD1[2][1] = -0.16666666666666666;
68: DD1[2][2] = 0.66666666666666663;
69: DD1[2][3] = -0.16666666666666663;
70: DD1[3][0] = -0.16666666666666666;
71: DD1[3][1] = -0.33333333333333343;
72: DD1[3][2] = -0.16666666666666663;
73: DD1[3][3] = 0.66666666666666663;
75: /* BC version of element */
76: for (i=0;i<4;i++) {
77: for (j=0;j<4;j++) {
78: if (i<2 || j < 2) {
79: if (i==j) DD2[i][j] = .1*DD1[i][j];
80: else DD2[i][j] = 0.0;
81: } else DD2[i][j] = DD1[i][j];
82: }
83: }
84: }
85: {
86: PetscReal *coords;
87: PC pc;
88: /* forms the element stiffness for the Laplacian and coordinates */
89: MatGetOwnershipRange(Amat,&Istart,&Iend);
90: m = Iend-Istart;
91: PetscMalloc1(2*m,&coords);
92: for (Ii=Istart,ix=0; Ii<Iend; Ii++,ix++) {
93: j = Ii/(ne+1); i = Ii%(ne+1);
94: /* coords */
95: x = h*(Ii % (ne+1)); y = h*(Ii/(ne+1));
96: coords[2*ix] = x; coords[2*ix+1] = y;
97: if (i<ne && j<ne) {
98: PetscInt jj,ii,idx[4];
99: /* radius */
100: PetscReal radius = PetscSqrtReal((x-.5+h/2)*(x-.5+h/2) + (y-.5+h/2)*(y-.5+h/2));
101: PetscReal alpha = 1.0;
102: idx[0] = Ii; idx[1] = Ii+1; idx[2] = Ii + (ne+1) + 1; idx[3] = Ii + (ne+1);
103: if (radius < 0.25) alpha = soft_alpha;
104: for (ii=0; ii<4; ii++) {
105: for (jj=0; jj<4; jj++) DD[ii][jj] = alpha*DD1[ii][jj];
106: }
107: MatSetValues(Pmat,4,idx,4,idx,(const PetscScalar*)DD,ADD_VALUES);
108: if (j>0) {
109: MatSetValues(Amat,4,idx,4,idx,(const PetscScalar*)DD,ADD_VALUES);
110: } else {
111: /* a BC */
112: for (ii=0;ii<4;ii++) {
113: for (jj=0;jj<4;jj++) DD[ii][jj] = alpha*DD2[ii][jj];
114: }
115: MatSetValues(Amat,4,idx,4,idx,(const PetscScalar*)DD,ADD_VALUES);
116: }
117: }
118: if (j>0) {
119: PetscScalar v = h*h;
120: PetscInt jj = Ii;
121: VecSetValues(bb,1,&jj,&v,INSERT_VALUES);
122: }
123: }
124: MatAssemblyBegin(Amat,MAT_FINAL_ASSEMBLY);
125: MatAssemblyEnd(Amat,MAT_FINAL_ASSEMBLY);
126: MatAssemblyBegin(Pmat,MAT_FINAL_ASSEMBLY);
127: MatAssemblyEnd(Pmat,MAT_FINAL_ASSEMBLY);
128: VecAssemblyBegin(bb);
129: VecAssemblyEnd(bb);
131: /* Setup solver */
132: KSPCreate(PETSC_COMM_WORLD,&ksp);
133: KSPSetFromOptions(ksp);
135: /* finish KSP/PC setup */
136: KSPSetOperators(ksp, Amat, Amat);
138: KSPGetPC(ksp,&pc);
139: PCSetCoordinates(pc, 2, m, coords);
140: PetscFree(coords);
141: }
143: if (!PETSC_TRUE) {
144: PetscViewer viewer;
145: PetscViewerASCIIOpen(comm, "Amat.m", &viewer);
146: PetscViewerPushFormat(viewer, PETSC_VIEWER_ASCII_MATLAB);
147: MatView(Amat,viewer);
148: PetscViewerPopFormat(viewer);
149: PetscViewerDestroy(&viewer);
150: }
152: /* solve */
153: #if defined(PETSC_USE_LOG)
154: PetscLogStageRegister("Solve", &stage);
155: PetscLogStagePush(stage);
156: #endif
157: VecSet(xx,.0);
159: KSPSetUp(ksp);
161: KSPSolve(ksp,bb,xx);
163: #if defined(PETSC_USE_LOG)
164: PetscLogStagePop();
165: #endif
167: KSPGetIterationNumber(ksp,&its);
169: if (!PETSC_TRUE) {
170: PetscReal norm,norm2;
171: PetscViewer viewer;
172: Vec res;
173: PetscViewerASCIIOpen(comm, "rhs.m", &viewer);
174: PetscViewerPushFormat(viewer, PETSC_VIEWER_ASCII_MATLAB);
175: VecView(bb,viewer);
176: PetscViewerPopFormat(viewer);
177: PetscViewerDestroy(&viewer);
178: VecNorm(bb, NORM_2, &norm2);
180: PetscViewerASCIIOpen(comm, "solution.m", &viewer);
181: PetscViewerPushFormat(viewer, PETSC_VIEWER_ASCII_MATLAB);
182: VecView(xx,viewer);
183: PetscViewerPopFormat(viewer);
184: PetscViewerDestroy(&viewer);
186: VecDuplicate(xx, &res);
187: MatMult(Amat, xx, res);
188: VecAXPY(bb, -1.0, res);
189: VecDestroy(&res);
190: VecNorm(bb,NORM_2,&norm);
191: PetscPrintf(PETSC_COMM_WORLD,"[%d]%s |b-Ax|/|b|=%e, |b|=%e\n",0,PETSC_FUNCTION_NAME,norm/norm2,norm2);
193: PetscViewerASCIIOpen(comm, "residual.m", &viewer);
194: PetscViewerPushFormat(viewer, PETSC_VIEWER_ASCII_MATLAB);
195: VecView(bb,viewer);
196: PetscViewerPopFormat(viewer);
197: PetscViewerDestroy(&viewer);
198: }
200: /* Free work space */
201: KSPDestroy(&ksp);
202: VecDestroy(&xx);
203: VecDestroy(&bb);
204: MatDestroy(&Amat);
205: MatDestroy(&Pmat);
207: PetscFinalize();
208: return ierr;
209: }
213: /*TEST
215: build:
216: requires: !complex
218: test:
219: nsize: 4
220: args: -ne 49 -alpha 1.e-3 -ksp_type cg -pc_type gamg -pc_gamg_type agg -pc_gamg_agg_nsmooths 1 -ksp_converged_reason -mg_levels_esteig_ksp_type cg
222: test:
223: suffix: seqaijmkl
224: nsize: 4
225: requires: mkl_sparse
226: args: -ne 19 -alpha 1.e-3 -pc_type gamg -pc_gamg_agg_nsmooths 1 -ksp_monitor -ksp_converged_reason -ksp_type cg -mat_seqaij_type seqaijmkl
228: test:
229: suffix: Classical
230: args: -ne 49 -alpha 1.e-3 -ksp_type cg -pc_type gamg -pc_gamg_type classical -mg_levels_ksp_chebyshev_esteig 0,0.05,0,1.05 -ksp_converged_reason -mg_levels_esteig_ksp_type cg
231: output_file: output/ex54_classical.out
233: test:
234: suffix: geo
235: nsize: 4
236: args: -ne 49 -alpha 1.e-3 -ksp_type cg -pc_type gamg -pc_gamg_type geo -pc_gamg_coarse_eq_limit 200 -mg_levels_pc_type jacobi -mg_levels_ksp_chebyshev_esteig 0,0.05,0,1.05 -ksp_monitor_short -mg_levels_esteig_ksp_type cg
237: requires: triangle
238: output_file: output/ex54_0.out
240: TEST*/