Actual source code: ex54.c

petsc-3.4.5 2014-06-29
  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>

 10: int main(int argc,char **args)
 11: {
 12:   Mat            Amat,Pmat;
 14:   PetscInt       i,m,M,its,Istart,Iend,j,Ii,bs,ix,ne=4;
 15:   PetscReal      x,y,h;
 16:   Vec            xx,bb;
 17:   KSP            ksp;
 18:   PetscReal      soft_alpha = 1.e-3;
 19:   MPI_Comm       comm;
 20:   PetscMPIInt    npe,mype;
 21:   PC             pc;
 22:   PetscScalar    DD[4][4],DD2[4][4];
 23: #if defined(PETSC_USE_LOG)
 24:   PetscLogStage stage;
 25: #endif
 26: #define DIAG_S 0.0
 27:   PetscScalar DD1[4][4] = { {5.0+DIAG_S, -2.0, -1.0, -2.0},
 28:                             {-2.0, 5.0+DIAG_S, -2.0, -1.0},
 29:                             {-1.0, -2.0, 5.0+DIAG_S, -2.0},
 30:                             {-2.0, -1.0, -2.0, 5.0+DIAG_S} };

 32:   PetscInitialize(&argc,&args,(char*)0,help);
 33:   comm = PETSC_COMM_WORLD;
 34:   MPI_Comm_rank(comm, &mype);
 35:   MPI_Comm_size(comm, &npe);
 36:   PetscOptionsGetInt(NULL,"-ne",&ne,NULL);
 37:   h     = 1./ne;
 38:   /* ne*ne; number of global elements */
 39:   PetscOptionsGetReal(NULL,"-alpha",&soft_alpha,NULL);
 40:   M    = (ne+1)*(ne+1); /* global number of nodes */
 41:   /* create stiffness matrix */
 42:   MatCreateAIJ(comm,PETSC_DECIDE,PETSC_DECIDE,M,M,
 43:                       18,NULL,6,NULL,&Amat);
 44:   MatCreateAIJ(comm,PETSC_DECIDE,PETSC_DECIDE,M,M,
 45:                       18,NULL,6,NULL,&Pmat);
 46:   MatGetOwnershipRange(Amat,&Istart,&Iend);
 47:   m    = Iend-Istart;
 48:   bs   = 1;
 49:   /* Generate vectors */
 50:   VecCreate(comm,&xx);
 51:   VecSetSizes(xx,m,M);
 52:   VecSetFromOptions(xx);
 53:   VecDuplicate(xx,&bb);
 54:   VecSet(bb,.0);
 55:   /* generate element matrices */
 56:   {
 57:     FILE *file;
 58:     char fname[] = "data/elem_2d_therm.txt";
 59:     file = fopen(fname, "r");
 60:     if (file == 0) {
 61:       DD1[0][0] =  0.66666666666666663;
 62:       DD1[0][1] = -0.16666666666666669;
 63:       DD1[0][2] = -0.33333333333333343;
 64:       DD1[0][3] = -0.16666666666666666;
 65:       DD1[1][0] = -0.16666666666666669;
 66:       DD1[1][1] =  0.66666666666666663;
 67:       DD1[1][2] = -0.16666666666666666;
 68:       DD1[1][3] = -0.33333333333333343;
 69:       DD1[2][0] = -0.33333333333333343;
 70:       DD1[2][1] = -0.16666666666666666;
 71:       DD1[2][2] =  0.66666666666666663;
 72:       DD1[2][3] = -0.16666666666666663;
 73:       DD1[3][0] = -0.16666666666666666;
 74:       DD1[3][1] = -0.33333333333333343;
 75:       DD1[3][2] = -0.16666666666666663;
 76:       DD1[3][3] =  0.66666666666666663;
 77:     } else {
 78:       for (i=0;i<4;i++) {
 79:         for (j=0;j<4;j++) {
 80:           fscanf(file, "%le", &DD1[i][j]);
 81:         }
 82:       }
 83:     }
 84:     /* BC version of element */
 85:     for (i=0;i<4;i++) {
 86:       for (j=0;j<4;j++) {
 87:         if (i<2 || j < 2) {
 88:           if (i==j) DD2[i][j] = .1*DD1[i][j];
 89:           else DD2[i][j] = 0.0;
 90:         } else DD2[i][j] = DD1[i][j];
 91:       }
 92:     }
 93:   }
 94:   {
 95:     PetscReal coords[2*m];
 96:     /* forms the element stiffness for the Laplacian and coordinates */
 97:     for (Ii=Istart,ix=0; Ii<Iend; Ii++,ix++) {
 98:       j = Ii/(ne+1); i = Ii%(ne+1);
 99:       /* coords */
100:       x            = h*(Ii % (ne+1)); y = h*(Ii/(ne+1));
101:       coords[2*ix] = x; coords[2*ix+1] = y;
102:       if (i<ne && j<ne) {
103:         PetscInt jj,ii,idx[4] = {Ii, Ii+1, Ii + (ne+1) + 1, Ii + (ne+1)};
104:         /* radius */
105:         PetscReal radius = PetscSqrtScalar((x-.5+h/2)*(x-.5+h/2) + (y-.5+h/2)*(y-.5+h/2));
106:         PetscReal alpha  = 1.0;
107:         if (radius < 0.25) alpha = soft_alpha;


110:         for (ii=0; ii<4; ii++) {
111:           for (jj=0; jj<4; jj++) DD[ii][jj] = alpha*DD1[ii][jj];
112:         }
113:         MatSetValues(Pmat,4,idx,4,idx,(const PetscScalar*)DD,ADD_VALUES);
114:         if (j>0) {
115:           MatSetValues(Amat,4,idx,4,idx,(const PetscScalar*)DD,ADD_VALUES);
116:         } else {
117:           /* a BC */
118:           for (ii=0;ii<4;ii++) {
119:             for (jj=0;jj<4;jj++) DD[ii][jj] = alpha*DD2[ii][jj];
120:           }
121:           MatSetValues(Amat,4,idx,4,idx,(const PetscScalar*)DD,ADD_VALUES);
122:         }
123:       }
124:       if (j>0) {
125:         PetscScalar v  = h*h;
126:         PetscInt    jj = Ii;
127:         VecSetValues(bb,1,&jj,&v,INSERT_VALUES);
128:       }
129:     }
130:     MatAssemblyBegin(Amat,MAT_FINAL_ASSEMBLY);
131:     MatAssemblyEnd(Amat,MAT_FINAL_ASSEMBLY);
132:     MatAssemblyBegin(Pmat,MAT_FINAL_ASSEMBLY);
133:     MatAssemblyEnd(Pmat,MAT_FINAL_ASSEMBLY);
134:     VecAssemblyBegin(bb);
135:     VecAssemblyEnd(bb);

137:     /* Setup solver */
138:     KSPCreate(PETSC_COMM_WORLD,&ksp);
139:     KSPSetType(ksp, KSPCG);
140:     KSPGetPC(ksp,&pc);
141:     PCSetType(pc,PCGAMG);
142:     KSPSetFromOptions(ksp);

144:     /* PCGAMGSetType(pc,"agg"); */

146:     /* finish KSP/PC setup */
147:     KSPSetOperators(ksp, Amat, Amat, SAME_NONZERO_PATTERN);
148:     PCSetCoordinates(pc, 2, m, coords);
149:   }

151:   if (!PETSC_TRUE) {
152:     PetscViewer viewer;
153:     PetscViewerASCIIOpen(comm, "Amat.m", &viewer);
154:     PetscViewerSetFormat(viewer, PETSC_VIEWER_ASCII_MATLAB);
155:     MatView(Amat,viewer);
156:     PetscViewerDestroy(&viewer);
157:   }

159:   /* solve */
160: #if defined(PETSC_USE_LOG)
161:   PetscLogStageRegister("Solve", &stage);
162:   PetscLogStagePush(stage);
163: #endif
164:   VecSet(xx,.0);

166:   KSPSolve(ksp,bb,xx);

168: #if defined(PETSC_USE_LOG)
169:   PetscLogStagePop();
170: #endif

172:   KSPGetIterationNumber(ksp,&its);

174:   if (!PETSC_TRUE) {
175:     PetscReal   norm,norm2;
176:     PetscViewer viewer;
177:     Vec         res;
178:     PetscViewerASCIIOpen(comm, "rhs.m", &viewer);
179:     PetscViewerSetFormat(viewer, PETSC_VIEWER_ASCII_MATLAB);
180:     VecView(bb,viewer);
181:     PetscViewerDestroy(&viewer);
182:     VecNorm(bb, NORM_2, &norm2);

184:     PetscViewerASCIIOpen(comm, "solution.m", &viewer);
185:     PetscViewerSetFormat(viewer, PETSC_VIEWER_ASCII_MATLAB);
186:     VecView(xx,viewer);
187:     PetscViewerDestroy(&viewer);

189:     VecDuplicate(xx, &res);
190:     MatMult(Amat, xx, res);
191:     VecAXPY(bb, -1.0, res);
192:     VecDestroy(&res);
193:     VecNorm(bb,NORM_2,&norm);
194:     PetscPrintf(PETSC_COMM_WORLD,"[%d]%s |b-Ax|/|b|=%e, |b|=%e\n",0,__FUNCT__,norm/norm2,norm2);

196:     PetscViewerASCIIOpen(comm, "residual.m", &viewer);
197:     PetscViewerSetFormat(viewer, PETSC_VIEWER_ASCII_MATLAB);
198:     VecView(bb,viewer);
199:     PetscViewerDestroy(&viewer);
200:   }

202:   /* Free work space */
203:   KSPDestroy(&ksp);
204:   VecDestroy(&xx);
205:   VecDestroy(&bb);
206:   MatDestroy(&Amat);
207:   MatDestroy(&Pmat);

209:   PetscFinalize();
210:   return 0;
211: }