Actual source code: ex54.c

petsc-master 2015-03-03
Report Typos and Errors
  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,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:   /* Generate vectors */
 49:   VecCreate(comm,&xx);
 50:   VecSetSizes(xx,m,M);
 51:   VecSetFromOptions(xx);
 52:   VecDuplicate(xx,&bb);
 53:   VecSet(bb,.0);
 54:   /* generate element matrices */
 55:   {
 56:     FILE *file;
 57:     char fname[] = "data/elem_2d_therm.txt";
 58:     file = fopen(fname, "r");
 59:     if (file == 0) {
 60:       DD1[0][0] =  0.66666666666666663;
 61:       DD1[0][1] = -0.16666666666666669;
 62:       DD1[0][2] = -0.33333333333333343;
 63:       DD1[0][3] = -0.16666666666666666;
 64:       DD1[1][0] = -0.16666666666666669;
 65:       DD1[1][1] =  0.66666666666666663;
 66:       DD1[1][2] = -0.16666666666666666;
 67:       DD1[1][3] = -0.33333333333333343;
 68:       DD1[2][0] = -0.33333333333333343;
 69:       DD1[2][1] = -0.16666666666666666;
 70:       DD1[2][2] =  0.66666666666666663;
 71:       DD1[2][3] = -0.16666666666666663;
 72:       DD1[3][0] = -0.16666666666666666;
 73:       DD1[3][1] = -0.33333333333333343;
 74:       DD1[3][2] = -0.16666666666666663;
 75:       DD1[3][3] =  0.66666666666666663;
 76:     } else {
 77:       for (i=0;i<4;i++) {
 78:         for (j=0;j<4;j++) {
 79:           double dtmp;
 80:           fscanf(file, "%le", &dtmp);
 81:           DD1[i][j] = dtmp;
 82:         }
 83:       }
 84:     }
 85:     /* BC version of element */
 86:     for (i=0;i<4;i++) {
 87:       for (j=0;j<4;j++) {
 88:         if (i<2 || j < 2) {
 89:           if (i==j) DD2[i][j] = .1*DD1[i][j];
 90:           else DD2[i][j] = 0.0;
 91:         } else DD2[i][j] = DD1[i][j];
 92:       }
 93:     }
 94:   }
 95:   {
 96:     PetscReal *coords;
 97:     PetscMalloc1(2*m,&coords);
 98:     /* forms the element stiffness for the Laplacian and coordinates */
 99:     for (Ii=Istart,ix=0; Ii<Iend; Ii++,ix++) {
100:       j = Ii/(ne+1); i = Ii%(ne+1);
101:       /* coords */
102:       x            = h*(Ii % (ne+1)); y = h*(Ii/(ne+1));
103:       coords[2*ix] = x; coords[2*ix+1] = y;
104:       if (i<ne && j<ne) {
105:         PetscInt jj,ii,idx[4];
106:         /* radius */
107:         PetscReal radius = PetscSqrtScalar((x-.5+h/2)*(x-.5+h/2) + (y-.5+h/2)*(y-.5+h/2));
108:         PetscReal alpha  = 1.0;

110:         idx[0] = Ii; idx[1] = Ii+1; idx[2] = Ii + (ne+1) + 1; idx[3] =  Ii + (ne+1);
111:         if (radius < 0.25) alpha = soft_alpha;


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

141:     /* Setup solver */
142:     KSPCreate(PETSC_COMM_WORLD,&ksp);
143:     KSPSetType(ksp, KSPCG);
144:     KSPGetPC(ksp,&pc);
145:     PCSetType(pc,PCGAMG);
146:     KSPSetFromOptions(ksp);

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

150:     /* finish KSP/PC setup */
151:     KSPSetOperators(ksp, Amat, Amat);
152:     PCSetCoordinates(pc, 2, m, coords);
153:     PetscFree(coords);
154:   }

156:   if (!PETSC_TRUE) {
157:     PetscViewer viewer;
158:     PetscViewerASCIIOpen(comm, "Amat.m", &viewer);
159:     PetscViewerSetFormat(viewer, PETSC_VIEWER_ASCII_MATLAB);
160:     MatView(Amat,viewer);
161:     PetscViewerDestroy(&viewer);
162:   }

164:   /* solve */
165: #if defined(PETSC_USE_LOG)
166:   PetscLogStageRegister("Solve", &stage);
167:   PetscLogStagePush(stage);
168: #endif
169:   VecSet(xx,.0);

171:   KSPSetUp(ksp);

173:   KSPSolve(ksp,bb,xx);

175: #if defined(PETSC_USE_LOG)
176:   PetscLogStagePop();
177: #endif

179:   KSPGetIterationNumber(ksp,&its);

181:   if (!PETSC_TRUE) {
182:     PetscReal   norm,norm2;
183:     PetscViewer viewer;
184:     Vec         res;
185:     PetscViewerASCIIOpen(comm, "rhs.m", &viewer);
186:     PetscViewerSetFormat(viewer, PETSC_VIEWER_ASCII_MATLAB);
187:     VecView(bb,viewer);
188:     PetscViewerDestroy(&viewer);
189:     VecNorm(bb, NORM_2, &norm2);

191:     PetscViewerASCIIOpen(comm, "solution.m", &viewer);
192:     PetscViewerSetFormat(viewer, PETSC_VIEWER_ASCII_MATLAB);
193:     VecView(xx,viewer);
194:     PetscViewerDestroy(&viewer);

196:     VecDuplicate(xx, &res);
197:     MatMult(Amat, xx, res);
198:     VecAXPY(bb, -1.0, res);
199:     VecDestroy(&res);
200:     VecNorm(bb,NORM_2,&norm);
201:     PetscPrintf(PETSC_COMM_WORLD,"[%d]%s |b-Ax|/|b|=%e, |b|=%e\n",0,__FUNCT__,norm/norm2,norm2);

203:     PetscViewerASCIIOpen(comm, "residual.m", &viewer);
204:     PetscViewerSetFormat(viewer, PETSC_VIEWER_ASCII_MATLAB);
205:     VecView(bb,viewer);
206:     PetscViewerDestroy(&viewer);
207:   }

209:   /* Free work space */
210:   KSPDestroy(&ksp);
211:   VecDestroy(&xx);
212:   VecDestroy(&bb);
213:   MatDestroy(&Amat);
214:   MatDestroy(&Pmat);

216:   PetscFinalize();
217:   return 0;
218: }