Actual source code: dainterp.c

  1: #define PETSCDM_DLL

  3: /*
  4:   Code for interpolating between grids represented by DAs
  5: */

 7:  #include src/dm/da/daimpl.h
 8:  #include petscmg.h

 12: PetscErrorCode PETSCDM_DLLEXPORT DMGetInterpolationScale(DM dac,DM daf,Mat mat,Vec *scale)
 13: {
 15:   Vec            fine;
 16:   PetscScalar    one = 1.0;

 19:   DMCreateGlobalVector(daf,&fine);
 20:   DMCreateGlobalVector(dac,scale);
 21:   VecSet(fine,one);
 22:   MatRestrict(mat,fine,*scale);
 23:   VecDestroy(fine);
 24:   VecReciprocal(*scale);
 25:   return(0);
 26: }

 30: PetscErrorCode DAGetInterpolation_1D_Q1(DA dac,DA daf,Mat *A)
 31: {
 33:   PetscInt       i,i_start,m_f,Mx,*idx_f;
 34:   PetscInt       m_ghost,*idx_c,m_ghost_c;
 35:   PetscInt       row,col,i_start_ghost,mx,m_c,nc,ratio;
 36:   PetscInt       i_c,i_start_c,i_start_ghost_c,cols[2],dof;
 37:   PetscScalar    v[2],x,*coors = 0,*ccoors;
 38:   Mat            mat;
 39:   DAPeriodicType pt;
 40:   Vec            vcoors,cvcoors;

 43:   DAGetInfo(dac,0,&Mx,0,0,0,0,0,0,0,&pt,0);
 44:   DAGetInfo(daf,0,&mx,0,0,0,0,0,&dof,0,0,0);
 45:   if (pt == DA_XPERIODIC) {
 46:     ratio = mx/Mx;
 47:     if (ratio*Mx != mx) SETERRQ2(PETSC_ERR_ARG_INCOMP,"Ratio between levels: mx/Mx  must be integer: mx %D Mx %D",mx,Mx);
 48:   } else {
 49:     ratio = (mx-1)/(Mx-1);
 50:     if (ratio*(Mx-1) != mx-1) SETERRQ2(PETSC_ERR_ARG_INCOMP,"Ratio between levels: (mx - 1)/(Mx - 1) must be integer: mx %D Mx %D",mx,Mx);
 51:   }

 53:   DAGetCorners(daf,&i_start,0,0,&m_f,0,0);
 54:   DAGetGhostCorners(daf,&i_start_ghost,0,0,&m_ghost,0,0);
 55:   DAGetGlobalIndices(daf,PETSC_NULL,&idx_f);

 57:   DAGetCorners(dac,&i_start_c,0,0,&m_c,0,0);
 58:   DAGetGhostCorners(dac,&i_start_ghost_c,0,0,&m_ghost_c,0,0);
 59:   DAGetGlobalIndices(dac,PETSC_NULL,&idx_c);

 61:   /* create interpolation matrix */
 62:   MatCreate(dac->comm,&mat);
 63:   MatSetSizes(mat,m_f,m_c,mx,Mx);
 64:   MatSetType(mat,MATAIJ);
 65:   MatSeqAIJSetPreallocation(mat,2,PETSC_NULL);
 66:   MatMPIAIJSetPreallocation(mat,2,PETSC_NULL,0,PETSC_NULL);
 67:   if (!DAXPeriodic(pt)){MatSetOption(mat,MAT_COLUMNS_SORTED);}

 69:   DAGetCoordinates(daf,&vcoors);
 70:   if (vcoors) {
 71:     DAGetGhostedCoordinates(dac,&cvcoors);
 72:     DAVecGetArray(daf->da_coordinates,vcoors,&coors);
 73:     DAVecGetArray(dac->da_coordinates,cvcoors,&ccoors);
 74:   }
 75:   /* loop over local fine grid nodes setting interpolation for those*/
 76:   for (i=i_start; i<i_start+m_f; i++) {
 77:     /* convert to local "natural" numbering and then to PETSc global numbering */
 78:     row    = idx_f[dof*(i-i_start_ghost)]/dof;

 80:     i_c = (i/ratio);    /* coarse grid node to left of fine grid node */
 81:     if (i_c < i_start_ghost_c) SETERRQ3(PETSC_ERR_ARG_INCOMP,"Processor's coarse DA must lie over fine DA\n\
 82:     i_start %D i_c %D i_start_ghost_c %D",i_start,i_c,i_start_ghost_c);

 84:     /* 
 85:          Only include those interpolation points that are truly 
 86:          nonzero. Note this is very important for final grid lines
 87:          in x direction; since they have no right neighbor
 88:     */
 89:     if (coors) {
 90:       x = (coors[i] - ccoors[i_c]);
 91:       /* only access the next coors point if we know there is one */
 92:       /* note this is dangerous because x may not exactly equal ZERO */
 93:       if (PetscAbsScalar(x) != 0.0) x = x/(ccoors[i_c+1] - ccoors[i_c]);
 94:     } else {
 95:       x  = ((double)(i - i_c*ratio))/((double)ratio);
 96:     }
 97:     nc = 0;
 98:       /* one left and below; or we are right on it */
 99:     col      = dof*(i_c-i_start_ghost_c);
100:     cols[nc] = idx_c[col]/dof;
101:     v[nc++]  = - x + 1.0;
102:     /* one right? */
103:     if (i_c*ratio != i) {
104:       cols[nc] = idx_c[col+dof]/dof;
105:       v[nc++]  = x;
106:     }
107:     MatSetValues(mat,1,&row,nc,cols,v,INSERT_VALUES);
108:   }
109:   if (vcoors) {
110:     DAVecRestoreArray(daf->da_coordinates,vcoors,&coors);
111:     DAVecRestoreArray(dac->da_coordinates,cvcoors,&ccoors);
112:   }
113:   MatAssemblyBegin(mat,MAT_FINAL_ASSEMBLY);
114:   MatAssemblyEnd(mat,MAT_FINAL_ASSEMBLY);
115:   MatCreateMAIJ(mat,dof,A);
116:   MatDestroy(mat);
117:   PetscLogFlops(5*m_f);
118:   return(0);
119: }

123: PetscErrorCode DAGetInterpolation_1D_Q0(DA dac,DA daf,Mat *A)
124: {
126:   PetscInt       i,i_start,m_f,Mx,*idx_f;
127:   PetscInt       m_ghost,*idx_c,m_ghost_c;
128:   PetscInt       row,col,i_start_ghost,mx,m_c,nc,ratio;
129:   PetscInt       i_c,i_start_c,i_start_ghost_c,cols[2],dof;
130:   PetscScalar    v[2],x;
131:   Mat            mat;
132:   DAPeriodicType pt;

135:   DAGetInfo(dac,0,&Mx,0,0,0,0,0,0,0,&pt,0);
136:   DAGetInfo(daf,0,&mx,0,0,0,0,0,&dof,0,0,0);
137:   if (pt == DA_XPERIODIC) {
138:     ratio = mx/Mx;
139:     if (ratio*Mx != mx) SETERRQ2(PETSC_ERR_ARG_INCOMP,"Ratio between levels: mx/Mx  must be integer: mx %D Mx %D",mx,Mx);
140:   } else {
141:     ratio = (mx-1)/(Mx-1);
142:     if (ratio*(Mx-1) != mx-1) SETERRQ2(PETSC_ERR_ARG_INCOMP,"Ratio between levels: (mx - 1)/(Mx - 1) must be integer: mx %D Mx %D",mx,Mx);
143:   }

145:   DAGetCorners(daf,&i_start,0,0,&m_f,0,0);
146:   DAGetGhostCorners(daf,&i_start_ghost,0,0,&m_ghost,0,0);
147:   DAGetGlobalIndices(daf,PETSC_NULL,&idx_f);

149:   DAGetCorners(dac,&i_start_c,0,0,&m_c,0,0);
150:   DAGetGhostCorners(dac,&i_start_ghost_c,0,0,&m_ghost_c,0,0);
151:   DAGetGlobalIndices(dac,PETSC_NULL,&idx_c);

153:   /* create interpolation matrix */
154:   MatCreate(dac->comm,&mat);
155:   MatSetSizes(mat,m_f,m_c,mx,Mx);
156:   MatSetType(mat,MATAIJ);
157:   MatSeqAIJSetPreallocation(mat,2,PETSC_NULL);
158:   MatMPIAIJSetPreallocation(mat,2,PETSC_NULL,0,PETSC_NULL);
159:   if (!DAXPeriodic(pt)) {MatSetOption(mat,MAT_COLUMNS_SORTED);}

161:   /* loop over local fine grid nodes setting interpolation for those*/
162:   for (i=i_start; i<i_start+m_f; i++) {
163:     /* convert to local "natural" numbering and then to PETSc global numbering */
164:     row    = idx_f[dof*(i-i_start_ghost)]/dof;

166:     i_c = (i/ratio);    /* coarse grid node to left of fine grid node */

168:     /* 
169:          Only include those interpolation points that are truly 
170:          nonzero. Note this is very important for final grid lines
171:          in x direction; since they have no right neighbor
172:     */
173:     x  = ((double)(i - i_c*ratio))/((double)ratio);
174:     nc = 0;
175:       /* one left and below; or we are right on it */
176:     col      = dof*(i_c-i_start_ghost_c);
177:     cols[nc] = idx_c[col]/dof;
178:     v[nc++]  = - x + 1.0;
179:     /* one right? */
180:     if (i_c*ratio != i) {
181:       cols[nc] = idx_c[col+dof]/dof;
182:       v[nc++]  = x;
183:     }
184:     MatSetValues(mat,1,&row,nc,cols,v,INSERT_VALUES);
185:   }
186:   MatAssemblyBegin(mat,MAT_FINAL_ASSEMBLY);
187:   MatAssemblyEnd(mat,MAT_FINAL_ASSEMBLY);
188:   MatCreateMAIJ(mat,dof,A);
189:   MatDestroy(mat);
190:   PetscLogFlops(5*m_f);
191:   return(0);
192: }

196: PetscErrorCode DAGetInterpolation_2D_Q1(DA dac,DA daf,Mat *A)
197: {
199:   PetscInt       i,j,i_start,j_start,m_f,n_f,Mx,My,*idx_f,dof;
200:   PetscInt       m_ghost,n_ghost,*idx_c,m_ghost_c,n_ghost_c,*dnz,*onz;
201:   PetscInt       row,col,i_start_ghost,j_start_ghost,cols[4],mx,m_c,my,nc,ratioi,ratioj;
202:   PetscInt       i_c,j_c,i_start_c,j_start_c,n_c,i_start_ghost_c,j_start_ghost_c,col_shift,col_scale;
203:   PetscMPIInt    size_c,size_f,rank_f;
204:   PetscScalar    v[4],x,y;
205:   Mat            mat;
206:   DAPeriodicType pt;
207:   DACoor2d       **coors = 0,**ccoors;
208:   Vec            vcoors,cvcoors;

211:   DAGetInfo(dac,0,&Mx,&My,0,0,0,0,0,0,&pt,0);
212:   DAGetInfo(daf,0,&mx,&my,0,0,0,0,&dof,0,0,0);
213:   if (DAXPeriodic(pt)){
214:     ratioi = mx/Mx;
215:     if (ratioi*Mx != mx) SETERRQ2(PETSC_ERR_ARG_INCOMP,"Ratio between levels: mx/Mx  must be integer: mx %D Mx %D",mx,Mx);
216:   } else {
217:     ratioi = (mx-1)/(Mx-1);
218:     if (ratioi*(Mx-1) != mx-1) SETERRQ2(PETSC_ERR_ARG_INCOMP,"Ratio between levels: (mx - 1)/(Mx - 1) must be integer: mx %D Mx %D",mx,Mx);
219:   }
220:   if (DAYPeriodic(pt)){
221:     ratioj = my/My;
222:     if (ratioj*My != my) SETERRQ2(PETSC_ERR_ARG_INCOMP,"Ratio between levels: my/My  must be integer: my %D My %D",my,My);
223:   } else {
224:     ratioj = (my-1)/(My-1);
225:     if (ratioj*(My-1) != my-1) SETERRQ2(PETSC_ERR_ARG_INCOMP,"Ratio between levels: (my - 1)/(My - 1) must be integer: my %D My %D",my,My);
226:   }


229:   DAGetCorners(daf,&i_start,&j_start,0,&m_f,&n_f,0);
230:   DAGetGhostCorners(daf,&i_start_ghost,&j_start_ghost,0,&m_ghost,&n_ghost,0);
231:   DAGetGlobalIndices(daf,PETSC_NULL,&idx_f);

233:   DAGetCorners(dac,&i_start_c,&j_start_c,0,&m_c,&n_c,0);
234:   DAGetGhostCorners(dac,&i_start_ghost_c,&j_start_ghost_c,0,&m_ghost_c,&n_ghost_c,0);
235:   DAGetGlobalIndices(dac,PETSC_NULL,&idx_c);

237:   /*
238:      Used for handling a coarse DA that lives on 1/4 the processors of the fine DA.
239:      The coarse vector is then duplicated 4 times (each time it lives on 1/4 of the 
240:      processors). It's effective length is hence 4 times its normal length, this is
241:      why the col_scale is multiplied by the interpolation matrix column sizes.
242:      sol_shift allows each set of 1/4 processors do its own interpolation using ITS
243:      copy of the coarse vector. A bit of a hack but you do better.

245:      In the standard case when size_f == size_c col_scale == 1 and col_shift == 0
246:   */
247:   MPI_Comm_size(dac->comm,&size_c);
248:   MPI_Comm_size(daf->comm,&size_f);
249:   MPI_Comm_rank(daf->comm,&rank_f);
250:   col_scale = size_f/size_c;
251:   col_shift = Mx*My*(rank_f/size_c);

253:   MatPreallocateInitialize(daf->comm,m_f*n_f,col_scale*m_c*n_c,dnz,onz);
254:   for (j=j_start; j<j_start+n_f; j++) {
255:     for (i=i_start; i<i_start+m_f; i++) {
256:       /* convert to local "natural" numbering and then to PETSc global numbering */
257:       row    = idx_f[dof*(m_ghost*(j-j_start_ghost) + (i-i_start_ghost))]/dof;

259:       i_c = (i/ratioi);    /* coarse grid node to left of fine grid node */
260:       j_c = (j/ratioj);    /* coarse grid node below fine grid node */

262:       if (j_c < j_start_ghost_c) SETERRQ3(PETSC_ERR_ARG_INCOMP,"Processor's coarse DA must lie over fine DA\n\
263:     j_start %D j_c %D j_start_ghost_c %D",j_start,j_c,j_start_ghost_c);
264:       if (i_c < i_start_ghost_c) SETERRQ3(PETSC_ERR_ARG_INCOMP,"Processor's coarse DA must lie over fine DA\n\
265:     i_start %D i_c %D i_start_ghost_c %D",i_start,i_c,i_start_ghost_c);

267:       /* 
268:          Only include those interpolation points that are truly 
269:          nonzero. Note this is very important for final grid lines
270:          in x and y directions; since they have no right/top neighbors
271:       */
272:       nc = 0;
273:       /* one left and below; or we are right on it */
274:       col        = dof*(m_ghost_c*(j_c-j_start_ghost_c) + (i_c-i_start_ghost_c));
275:       cols[nc++] = col_shift + idx_c[col]/dof;
276:       /* one right and below */
277:       if (i_c*ratioi != i) {
278:         cols[nc++] = col_shift + idx_c[col+dof]/dof;
279:       }
280:       /* one left and above */
281:       if (j_c*ratioj != j) {
282:         cols[nc++] = col_shift + idx_c[col+m_ghost_c*dof]/dof;
283:       }
284:       /* one right and above */
285:       if (j_c*ratioi != j && i_c*ratioj != i) {
286:         cols[nc++] = col_shift + idx_c[col+(m_ghost_c+1)*dof]/dof;
287:       }
288:       MatPreallocateSet(row,nc,cols,dnz,onz);
289:     }
290:   }
291:   MatCreate(daf->comm,&mat);
292:   MatSetSizes(mat,m_f*n_f,col_scale*m_c*n_c,mx*my,col_scale*Mx*My);
293:   MatSetType(mat,MATAIJ);
294:   MatSeqAIJSetPreallocation(mat,0,dnz);
295:   MatMPIAIJSetPreallocation(mat,0,dnz,0,onz);
296:   MatPreallocateFinalize(dnz,onz);
297:   if (!DAXPeriodic(pt) && !DAYPeriodic(pt)) {MatSetOption(mat,MAT_COLUMNS_SORTED);}

299:   DAGetCoordinates(daf,&vcoors);
300:   if (vcoors) {
301:     DAGetGhostedCoordinates(dac,&cvcoors);
302:     DAVecGetArray(daf->da_coordinates,vcoors,&coors);
303:     DAVecGetArray(dac->da_coordinates,cvcoors,&ccoors);
304:   }

306:   /* loop over local fine grid nodes setting interpolation for those*/
307:   for (j=j_start; j<j_start+n_f; j++) {
308:     for (i=i_start; i<i_start+m_f; i++) {
309:       /* convert to local "natural" numbering and then to PETSc global numbering */
310:       row    = idx_f[dof*(m_ghost*(j-j_start_ghost) + (i-i_start_ghost))]/dof;

312:       i_c = (i/ratioi);    /* coarse grid node to left of fine grid node */
313:       j_c = (j/ratioj);    /* coarse grid node below fine grid node */

315:       /* 
316:          Only include those interpolation points that are truly 
317:          nonzero. Note this is very important for final grid lines
318:          in x and y directions; since they have no right/top neighbors
319:       */
320:       if (coors) {
321:         /* only access the next coors point if we know there is one */
322:         /* note this is dangerous because x may not exactly equal ZERO */
323:         x = (coors[j][i].x - ccoors[j_c][i_c].x);
324:         if (PetscAbsScalar(x) != 0.0) x = x/(ccoors[j_c][i_c+1].x - ccoors[j_c][i_c].x);
325:         y = (coors[j][i].y - ccoors[j_c][i_c].y);
326:         if (PetscAbsScalar(y) != 0.0) y = y/(ccoors[j_c+1][i_c].y - ccoors[j_c][i_c].y);
327:       } else {
328:         x  = ((double)(i - i_c*ratioi))/((double)ratioi);
329:         y  = ((double)(j - j_c*ratioj))/((double)ratioj);
330:       }
331:       nc = 0;
332:       /* one left and below; or we are right on it */
333:       col      = dof*(m_ghost_c*(j_c-j_start_ghost_c) + (i_c-i_start_ghost_c));
334:       cols[nc] = col_shift + idx_c[col]/dof;
335:       v[nc++]  = x*y - x - y + 1.0;
336:       /* one right and below */
337:       if (i_c*ratioi != i) {
338:         cols[nc] = col_shift + idx_c[col+dof]/dof;
339:         v[nc++]  = -x*y + x;
340:       }
341:       /* one left and above */
342:       if (j_c*ratioj != j) {
343:         cols[nc] = col_shift + idx_c[col+m_ghost_c*dof]/dof;
344:         v[nc++]  = -x*y + y;
345:       }
346:       /* one right and above */
347:       if (j_c*ratioj != j && i_c*ratioi != i) {
348:         cols[nc] = col_shift + idx_c[col+(m_ghost_c+1)*dof]/dof;
349:         v[nc++]  = x*y;
350:       }
351:       MatSetValues(mat,1,&row,nc,cols,v,INSERT_VALUES);
352:     }
353:   }
354:   if (vcoors) {
355:     DAVecRestoreArray(daf->da_coordinates,vcoors,&coors);
356:     DAVecRestoreArray(dac->da_coordinates,cvcoors,&ccoors);
357:   }
358:   MatAssemblyBegin(mat,MAT_FINAL_ASSEMBLY);
359:   MatAssemblyEnd(mat,MAT_FINAL_ASSEMBLY);
360:   MatCreateMAIJ(mat,dof,A);
361:   MatDestroy(mat);
362:   PetscLogFlops(13*m_f*n_f);
363:   return(0);
364: }

366: /* 
367:        Contributed by Andrei Draganescu <aidraga@sandia.gov>
368: */
371: PetscErrorCode DAGetInterpolation_2D_Q0(DA dac,DA daf,Mat *A)
372: {
374:   PetscInt       i,j,i_start,j_start,m_f,n_f,Mx,My,*idx_f,dof;
375:   PetscInt       m_ghost,n_ghost,*idx_c,m_ghost_c,n_ghost_c,*dnz,*onz;
376:   PetscInt       row,col,i_start_ghost,j_start_ghost,cols[4],mx,m_c,my,nc,ratioi,ratioj;
377:   PetscInt       i_c,j_c,i_start_c,j_start_c,n_c,i_start_ghost_c,j_start_ghost_c,col_shift,col_scale;
378:   PetscMPIInt    size_c,size_f,rank_f;
379:   PetscScalar    v[4];
380:   Mat            mat;
381:   DAPeriodicType pt;

384:   DAGetInfo(dac,0,&Mx,&My,0,0,0,0,0,0,&pt,0);
385:   DAGetInfo(daf,0,&mx,&my,0,0,0,0,&dof,0,0,0);
386:   if (DAXPeriodic(pt)) SETERRQ(PETSC_ERR_ARG_WRONG,"Cannot handle periodic grid in x");
387:   if (DAYPeriodic(pt)) SETERRQ(PETSC_ERR_ARG_WRONG,"Cannot handle periodic grid in y");
388:   ratioi = mx/Mx;
389:   ratioj = my/My;
390:   if (ratioi*Mx != mx) SETERRQ(PETSC_ERR_ARG_WRONG,"Fine grid points must be multiple of coarse grid points in x");
391:   if (ratioj*My != my) SETERRQ(PETSC_ERR_ARG_WRONG,"Fine grid points must be multiple of coarse grid points in y");
392:   if (ratioi != 2) SETERRQ(PETSC_ERR_ARG_WRONG,"Coarsening factor in x must be 2");
393:   if (ratioj != 2) SETERRQ(PETSC_ERR_ARG_WRONG,"Coarsening factor in y must be 2");

395:   DAGetCorners(daf,&i_start,&j_start,0,&m_f,&n_f,0);
396:   DAGetGhostCorners(daf,&i_start_ghost,&j_start_ghost,0,&m_ghost,&n_ghost,0);
397:   DAGetGlobalIndices(daf,PETSC_NULL,&idx_f);

399:   DAGetCorners(dac,&i_start_c,&j_start_c,0,&m_c,&n_c,0);
400:   DAGetGhostCorners(dac,&i_start_ghost_c,&j_start_ghost_c,0,&m_ghost_c,&n_ghost_c,0);
401:   DAGetGlobalIndices(dac,PETSC_NULL,&idx_c);

403:   /*
404:      Used for handling a coarse DA that lives on 1/4 the processors of the fine DA.
405:      The coarse vector is then duplicated 4 times (each time it lives on 1/4 of the 
406:      processors). It's effective length is hence 4 times its normal length, this is
407:      why the col_scale is multiplied by the interpolation matrix column sizes.
408:      sol_shift allows each set of 1/4 processors do its own interpolation using ITS
409:      copy of the coarse vector. A bit of a hack but you do better.

411:      In the standard case when size_f == size_c col_scale == 1 and col_shift == 0
412:   */
413:   MPI_Comm_size(dac->comm,&size_c);
414:   MPI_Comm_size(daf->comm,&size_f);
415:   MPI_Comm_rank(daf->comm,&rank_f);
416:   col_scale = size_f/size_c;
417:   col_shift = Mx*My*(rank_f/size_c);

419:   MatPreallocateInitialize(daf->comm,m_f*n_f,col_scale*m_c*n_c,dnz,onz);
420:   for (j=j_start; j<j_start+n_f; j++) {
421:     for (i=i_start; i<i_start+m_f; i++) {
422:       /* convert to local "natural" numbering and then to PETSc global numbering */
423:       row    = idx_f[dof*(m_ghost*(j-j_start_ghost) + (i-i_start_ghost))]/dof;

425:       i_c = (i/ratioi);    /* coarse grid node to left of fine grid node */
426:       j_c = (j/ratioj);    /* coarse grid node below fine grid node */

428:       if (j_c < j_start_ghost_c) SETERRQ3(PETSC_ERR_ARG_INCOMP,"Processor's coarse DA must lie over fine DA\n\
429:     j_start %D j_c %D j_start_ghost_c %D",j_start,j_c,j_start_ghost_c);
430:       if (i_c < i_start_ghost_c) SETERRQ3(PETSC_ERR_ARG_INCOMP,"Processor's coarse DA must lie over fine DA\n\
431:     i_start %D i_c %D i_start_ghost_c %D",i_start,i_c,i_start_ghost_c);

433:       /* 
434:          Only include those interpolation points that are truly 
435:          nonzero. Note this is very important for final grid lines
436:          in x and y directions; since they have no right/top neighbors
437:       */
438:       nc = 0;
439:       /* one left and below; or we are right on it */
440:       col        = dof*(m_ghost_c*(j_c-j_start_ghost_c) + (i_c-i_start_ghost_c));
441:       cols[nc++] = col_shift + idx_c[col]/dof;
442:       MatPreallocateSet(row,nc,cols,dnz,onz);
443:     }
444:   }
445:   MatCreate(daf->comm,&mat);
446:   MatSetSizes(mat,m_f*n_f,col_scale*m_c*n_c,mx*my,col_scale*Mx*My);
447:   MatSetType(mat,MATAIJ);
448:   MatSeqAIJSetPreallocation(mat,0,dnz);
449:   MatMPIAIJSetPreallocation(mat,0,dnz,0,onz);
450:   MatPreallocateFinalize(dnz,onz);
451:   if (!DAXPeriodic(pt) && !DAYPeriodic(pt)) {MatSetOption(mat,MAT_COLUMNS_SORTED);}

453:   /* loop over local fine grid nodes setting interpolation for those*/
454:   for (j=j_start; j<j_start+n_f; j++) {
455:     for (i=i_start; i<i_start+m_f; i++) {
456:       /* convert to local "natural" numbering and then to PETSc global numbering */
457:       row    = idx_f[dof*(m_ghost*(j-j_start_ghost) + (i-i_start_ghost))]/dof;

459:       i_c = (i/ratioi);    /* coarse grid node to left of fine grid node */
460:       j_c = (j/ratioj);    /* coarse grid node below fine grid node */
461:       nc = 0;
462:       /* one left and below; or we are right on it */
463:       col      = dof*(m_ghost_c*(j_c-j_start_ghost_c) + (i_c-i_start_ghost_c));
464:       cols[nc] = col_shift + idx_c[col]/dof;
465:       v[nc++]  = 1.0;
466: 
467:       MatSetValues(mat,1,&row,nc,cols,v,INSERT_VALUES);
468:     }
469:   }
470:   MatAssemblyBegin(mat,MAT_FINAL_ASSEMBLY);
471:   MatAssemblyEnd(mat,MAT_FINAL_ASSEMBLY);
472:   MatCreateMAIJ(mat,dof,A);
473:   MatDestroy(mat);
474:   PetscLogFlops(13*m_f*n_f);
475:   return(0);
476: }

480: PetscErrorCode DAGetInterpolation_3D_Q1(DA dac,DA daf,Mat *A)
481: {
483:   PetscInt       i,j,i_start,j_start,m_f,n_f,Mx,My,*idx_f,dof,l;
484:   PetscInt       m_ghost,n_ghost,*idx_c,m_ghost_c,n_ghost_c,Mz,mz;
485:   PetscInt       row,col,i_start_ghost,j_start_ghost,cols[8],mx,m_c,my,nc,ratioi,ratioj,ratiok;
486:   PetscInt       i_c,j_c,i_start_c,j_start_c,n_c,i_start_ghost_c,j_start_ghost_c;
487:   PetscInt       l_start,p_f,l_start_ghost,p_ghost,l_start_c,p_c;
488:   PetscInt       l_start_ghost_c,p_ghost_c,l_c,*dnz,*onz;
489:   PetscScalar    v[8],x,y,z;
490:   Mat            mat;
491:   DAPeriodicType pt;
492:   DACoor3d       ***coors = 0,***ccoors;
493:   Vec            vcoors,cvcoors;

496:   DAGetInfo(dac,0,&Mx,&My,&Mz,0,0,0,0,0,&pt,0);
497:   DAGetInfo(daf,0,&mx,&my,&mz,0,0,0,&dof,0,0,0);
498:   if (mx == Mx) {
499:     ratioi = 1;
500:   } else if (DAXPeriodic(pt)){
501:     ratioi = mx/Mx;
502:     if (ratioi*Mx != mx) SETERRQ2(PETSC_ERR_ARG_INCOMP,"Ratio between levels: mx/Mx  must be integer: mx %D Mx %D",mx,Mx);
503:   } else {
504:     ratioi = (mx-1)/(Mx-1);
505:     if (ratioi*(Mx-1) != mx-1) SETERRQ2(PETSC_ERR_ARG_INCOMP,"Ratio between levels: (mx - 1)/(Mx - 1) must be integer: mx %D Mx %D",mx,Mx);
506:   }
507:   if (my == My) {
508:     ratioj = 1;
509:   } else if (DAYPeriodic(pt)){
510:     ratioj = my/My;
511:     if (ratioj*My != my) SETERRQ2(PETSC_ERR_ARG_INCOMP,"Ratio between levels: my/My  must be integer: my %D My %D",my,My);
512:   } else {
513:     ratioj = (my-1)/(My-1);
514:     if (ratioj*(My-1) != my-1) SETERRQ2(PETSC_ERR_ARG_INCOMP,"Ratio between levels: (my - 1)/(My - 1) must be integer: my %D My %D",my,My);
515:   }
516:   if (mz == Mz) {
517:     ratiok = 1;
518:   } else if (DAZPeriodic(pt)){
519:     ratiok = mz/Mz;
520:     if (ratiok*Mz != mz) SETERRQ2(PETSC_ERR_ARG_INCOMP,"Ratio between levels: mz/Mz  must be integer: mz %D Mz %D",mz,Mz);
521:   } else {
522:     ratiok = (mz-1)/(Mz-1);
523:     if (ratiok*(Mz-1) != mz-1) SETERRQ2(PETSC_ERR_ARG_INCOMP,"Ratio between levels: (mz - 1)/(Mz - 1) must be integer: mz %D Mz %D",mz,Mz);
524:   }

526:   DAGetCorners(daf,&i_start,&j_start,&l_start,&m_f,&n_f,&p_f);
527:   DAGetGhostCorners(daf,&i_start_ghost,&j_start_ghost,&l_start_ghost,&m_ghost,&n_ghost,&p_ghost);
528:   DAGetGlobalIndices(daf,PETSC_NULL,&idx_f);

530:   DAGetCorners(dac,&i_start_c,&j_start_c,&l_start_c,&m_c,&n_c,&p_c);
531:   DAGetGhostCorners(dac,&i_start_ghost_c,&j_start_ghost_c,&l_start_ghost_c,&m_ghost_c,&n_ghost_c,&p_ghost_c);
532:   DAGetGlobalIndices(dac,PETSC_NULL,&idx_c);

534:   /* create interpolation matrix, determining exact preallocation */
535:   MatPreallocateInitialize(dac->comm,m_f*n_f*p_f,m_c*n_c*p_c,dnz,onz);
536:   /* loop over local fine grid nodes counting interpolating points */
537:   for (l=l_start; l<l_start+p_f; l++) {
538:     for (j=j_start; j<j_start+n_f; j++) {
539:       for (i=i_start; i<i_start+m_f; i++) {
540:         /* convert to local "natural" numbering and then to PETSc global numbering */
541:         row = idx_f[dof*(m_ghost*n_ghost*(l-l_start_ghost) + m_ghost*(j-j_start_ghost) + (i-i_start_ghost))]/dof;
542:         i_c = (i/ratioi);
543:         j_c = (j/ratioj);
544:         l_c = (l/ratiok);
545:         if (l_c < l_start_ghost_c) SETERRQ3(PETSC_ERR_ARG_INCOMP,"Processor's coarse DA must lie over fine DA\n\
546:           l_start %D l_c %D l_start_ghost_c %D",l_start,l_c,l_start_ghost_c);
547:         if (j_c < j_start_ghost_c) SETERRQ3(PETSC_ERR_ARG_INCOMP,"Processor's coarse DA must lie over fine DA\n\
548:           j_start %D j_c %D j_start_ghost_c %D",j_start,j_c,j_start_ghost_c);
549:         if (i_c < i_start_ghost_c) SETERRQ3(PETSC_ERR_ARG_INCOMP,"Processor's coarse DA must lie over fine DA\n\
550:           i_start %D i_c %D i_start_ghost_c %D",i_start,i_c,i_start_ghost_c);

552:         /* 
553:          Only include those interpolation points that are truly 
554:          nonzero. Note this is very important for final grid lines
555:          in x and y directions; since they have no right/top neighbors
556:         */
557:         nc       = 0;
558:         col      = dof*(m_ghost_c*n_ghost_c*(l_c-l_start_ghost_c) + m_ghost_c*(j_c-j_start_ghost_c) + (i_c-i_start_ghost_c));
559:         cols[nc++] = idx_c[col]/dof;
560:         if (i_c*ratioi != i) {
561:           cols[nc++] = idx_c[col+dof]/dof;
562:         }
563:         if (j_c*ratioj != j) {
564:           cols[nc++] = idx_c[col+m_ghost_c*dof]/dof;
565:         }
566:         if (l_c*ratiok != l) {
567:           cols[nc++] = idx_c[col+m_ghost_c*n_ghost_c*dof]/dof;
568:         }
569:         if (j_c*ratioj != j && i_c*ratioi != i) {
570:           cols[nc++] = idx_c[col+(m_ghost_c+1)*dof]/dof;
571:         }
572:         if (j_c*ratioj != j && l_c*ratiok != l) {
573:           cols[nc++] = idx_c[col+(m_ghost_c*n_ghost_c+m_ghost_c)*dof]/dof;
574:         }
575:         if (i_c*ratioi != i && l_c*ratiok != l) {
576:           cols[nc++] = idx_c[col+(m_ghost_c*n_ghost_c+1)*dof]/dof;
577:         }
578:         if (i_c*ratioi != i && l_c*ratiok != l && j_c*ratioj != j) {
579:           cols[nc++] = idx_c[col+(m_ghost_c*n_ghost_c+m_ghost_c+1)*dof]/dof;
580:         }
581:         MatPreallocateSet(row,nc,cols,dnz,onz);
582:       }
583:     }
584:   }
585:   MatCreate(dac->comm,&mat);
586:   MatSetSizes(mat,m_f*n_f*p_f,m_c*n_c*p_c,mx*my*mz,Mx*My*Mz);
587:   MatSetType(mat,MATAIJ);
588:   MatSeqAIJSetPreallocation(mat,0,dnz);
589:   MatMPIAIJSetPreallocation(mat,0,dnz,0,onz);
590:   MatPreallocateFinalize(dnz,onz);
591:   if (!DAXPeriodic(pt) && !DAYPeriodic(pt) && !DAZPeriodic(pt)) {MatSetOption(mat,MAT_COLUMNS_SORTED);}

593:   DAGetCoordinates(daf,&vcoors);
594:   if (vcoors) {
595:     DAGetGhostedCoordinates(dac,&cvcoors);
596:     DAVecGetArray(daf->da_coordinates,vcoors,&coors);
597:     DAVecGetArray(dac->da_coordinates,cvcoors,&ccoors);
598:   }

600:   /* loop over local fine grid nodes setting interpolation for those*/
601:   for (l=l_start; l<l_start+p_f; l++) {
602:     for (j=j_start; j<j_start+n_f; j++) {
603:       for (i=i_start; i<i_start+m_f; i++) {
604:         /* convert to local "natural" numbering and then to PETSc global numbering */
605:         row = idx_f[dof*(m_ghost*n_ghost*(l-l_start_ghost) + m_ghost*(j-j_start_ghost) + (i-i_start_ghost))]/dof;

607:         i_c = (i/ratioi);
608:         j_c = (j/ratioj);
609:         l_c = (l/ratiok);

611:         /* 
612:            Only include those interpolation points that are truly 
613:            nonzero. Note this is very important for final grid lines
614:            in x and y directions; since they have no right/top neighbors
615:         */
616:         if (coors) {
617:           /* only access the next coors point if we know there is one */
618:           /* note this is dangerous because x may not exactly equal ZERO */
619:           x = (coors[l][j][i].x - ccoors[l_c][j_c][i_c].x);
620:           if (PetscAbsScalar(x) != 0.0) x = x/(ccoors[l_c][j_c][i_c+1].x - ccoors[l_c][j_c][i_c].x);
621:           y = (coors[l][j][i].y - ccoors[l_c][j_c][i_c].y);
622:           if (PetscAbsScalar(y) != 0.0) y = y/(ccoors[l_c][j_c+1][i_c].y - ccoors[l_c][j_c][i_c].y);
623:           z = (coors[l][j][i].z - ccoors[l_c][j_c][i_c].z);
624:           if (PetscAbsScalar(z) != 0.0) z = z/(ccoors[l_c+1][j_c][i_c].z - ccoors[l_c][j_c][i_c].z);
625:         } else {
626:           x  = ((double)(i - i_c*ratioi))/((double)ratioi);
627:           y  = ((double)(j - j_c*ratioj))/((double)ratioj);
628:           z  = ((double)(l - l_c*ratiok))/((double)ratiok);
629:         }
630:         nc = 0;
631:         /* one left and below; or we are right on it */
632:         col      = dof*(m_ghost_c*n_ghost_c*(l_c-l_start_ghost_c)+m_ghost_c*(j_c-j_start_ghost_c)+(i_c-i_start_ghost_c));

634:         cols[nc] = idx_c[col]/dof;
635:         v[nc++]  = .125*(1. - (2.0*x-1.))*(1. - (2.0*y-1.))*(1. - (2.0*z-1.));

637:         if (i_c*ratioi != i) {
638:           cols[nc] = idx_c[col+dof]/dof;
639:           v[nc++]  = .125*(1. + (2.0*x-1.))*(1. - (2.0*y-1.))*(1. - (2.0*z-1.));
640:         }

642:         if (j_c*ratioj != j) {
643:           cols[nc] = idx_c[col+m_ghost_c*dof]/dof;
644:           v[nc++]  = .125*(1. - (2.0*x-1.))*(1. + (2.0*y-1.))*(1. - (2.0*z-1.));
645:         }

647:         if (l_c*ratiok != l) {
648:           cols[nc] = idx_c[col+m_ghost_c*n_ghost_c*dof]/dof;
649:           v[nc++]  = .125*(1. - (2.0*x-1.))*(1. - (2.0*y-1.))*(1. + (2.0*z-1.));
650:         }

652:         if (j_c*ratioj != j && i_c*ratioi != i) {
653:           cols[nc] = idx_c[col+(m_ghost_c+1)*dof]/dof;
654:           v[nc++]  = .125*(1. + (2.0*x-1.))*(1. + (2.0*y-1.))*(1. - (2.0*z-1.));
655:         }

657:         if (j_c*ratioj != j && l_c*ratiok != l) {
658:           cols[nc] = idx_c[col+(m_ghost_c*n_ghost_c+m_ghost_c)*dof]/dof;
659:           v[nc++]  = .125*(1. - (2.0*x-1.))*(1. + (2.0*y-1.))*(1. + (2.0*z-1.));
660:         }

662:         if (i_c*ratioi != i && l_c*ratiok != l) {
663:           cols[nc] = idx_c[col+(m_ghost_c*n_ghost_c+1)*dof]/dof;
664:           v[nc++]  = .125*(1. + (2.0*x-1.))*(1. - (2.0*y-1.))*(1. + (2.0*z-1.));
665:         }

667:         if (i_c*ratioi != i && l_c*ratiok != l && j_c*ratioj != j) {
668:           cols[nc] = idx_c[col+(m_ghost_c*n_ghost_c+m_ghost_c+1)*dof]/dof;
669:           v[nc++]  = .125*(1. + (2.0*x-1.))*(1. + (2.0*y-1.))*(1. + (2.0*z-1.));
670:         }
671:         MatSetValues(mat,1,&row,nc,cols,v,INSERT_VALUES);
672:       }
673:     }
674:   }
675:   if (vcoors) {
676:     DAVecRestoreArray(daf->da_coordinates,vcoors,&coors);
677:     DAVecRestoreArray(dac->da_coordinates,cvcoors,&ccoors);
678:   }
679:   MatAssemblyBegin(mat,MAT_FINAL_ASSEMBLY);
680:   MatAssemblyEnd(mat,MAT_FINAL_ASSEMBLY);

682:   MatCreateMAIJ(mat,dof,A);
683:   MatDestroy(mat);
684:   PetscLogFlops(13*m_f*n_f);
685:   return(0);
686: }

690: /*@C
691:    DAGetInterpolation - Gets an interpolation matrix that maps between 
692:    grids associated with two DAs.

694:    Collective on DA

696:    Input Parameters:
697: +  dac - the coarse grid DA
698: -  daf - the fine grid DA

700:    Output Parameters:
701: +  A - the interpolation matrix
702: -  scale - a scaling vector used to scale the coarse grid restricted vector before applying the 
703:            grid function or grid Jacobian to it.

705:    Level: intermediate

707: .keywords: interpolation, restriction, multigrid 

709: .seealso: DARefine(), DAGetInjection()
710: @*/
711: PetscErrorCode PETSCDM_DLLEXPORT DAGetInterpolation(DA dac,DA daf,Mat *A,Vec *scale)
712: {
714:   PetscInt       dimc,Mc,Nc,Pc,mc,nc,pc,dofc,sc,dimf,Mf,Nf,Pf,mf,nf,pf,doff,sf;
715:   DAPeriodicType wrapc,wrapf;
716:   DAStencilType  stc,stf;


724:   DAGetInfo(dac,&dimc,&Mc,&Nc,&Pc,&mc,&nc,&pc,&dofc,&sc,&wrapc,&stc);
725:   DAGetInfo(daf,&dimf,&Mf,&Nf,&Pf,&mf,&nf,&pf,&doff,&sf,&wrapf,&stf);
726:   if (dimc != dimf) SETERRQ2(PETSC_ERR_ARG_INCOMP,"Dimensions of DA do not match %D %D",dimc,dimf);
727:   if (dofc != doff) SETERRQ2(PETSC_ERR_ARG_INCOMP,"DOF of DA do not match %D %D",dofc,doff);
728:   if (sc != sf) SETERRQ2(PETSC_ERR_ARG_INCOMP,"Stencil width of DA do not match %D %D",sc,sf);
729:   if (wrapc != wrapf) SETERRQ(PETSC_ERR_ARG_INCOMP,"Periodic type different in two DAs");
730:   if (stc != stf) SETERRQ(PETSC_ERR_ARG_INCOMP,"Stencil type different in two DAs");
731:   if (Mc < 2 && Mf > 1) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Coarse grid requires at least 2 points in x direction");
732:   if (dimc > 1 && Nc < 2 && Nf > 1) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Coarse grid requires at least 2 points in y direction");
733:   if (dimc > 2 && Pc < 2 && Pf > 1) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Coarse grid requires at least 2 points in z direction");

735:   if (dac->interptype == DA_Q1){
736:     if (dimc == 1){
737:       DAGetInterpolation_1D_Q1(dac,daf,A);
738:     } else if (dimc == 2){
739:       DAGetInterpolation_2D_Q1(dac,daf,A);
740:     } else if (dimc == 3){
741:       DAGetInterpolation_3D_Q1(dac,daf,A);
742:     } else {
743:       SETERRQ2(PETSC_ERR_SUP,"No support for this DA dimension %D for interpolation type %d",dimc,(int)dac->interptype);
744:     }
745:   } else if (dac->interptype == DA_Q0){
746:     if (dimc == 1){
747:       DAGetInterpolation_1D_Q0(dac,daf,A);
748:     } if (dimc == 2){
749:        DAGetInterpolation_2D_Q0(dac,daf,A);
750:     } else {
751:       SETERRQ2(PETSC_ERR_SUP,"No support for this DA dimension %D for interpolation type %d",dimc,(int)dac->interptype);
752:     }
753:   }
754:   if (scale) {
755:     DMGetInterpolationScale((DM)dac,(DM)daf,*A,scale);
756:   }
757:   return(0);
758: }

762: PetscErrorCode DAGetInjection_2D(DA dac,DA daf,VecScatter *inject)
763: {
765:   PetscInt       i,j,i_start,j_start,m_f,n_f,Mx,My,*idx_f,dof;
766:   PetscInt       m_ghost,n_ghost,*idx_c,m_ghost_c,n_ghost_c;
767:   PetscInt       row,i_start_ghost,j_start_ghost,mx,m_c,my,nc,ratioi,ratioj;
768:   PetscInt       i_c,j_c,i_start_c,j_start_c,n_c,i_start_ghost_c,j_start_ghost_c;
769:   PetscInt       *cols;
770:   DAPeriodicType pt;
771:   Vec            vecf,vecc;
772:   IS             isf;


776:   DAGetInfo(dac,0,&Mx,&My,0,0,0,0,0,0,&pt,0);
777:   DAGetInfo(daf,0,&mx,&my,0,0,0,0,&dof,0,0,0);
778:   if (DAXPeriodic(pt)){
779:     ratioi = mx/Mx;
780:     if (ratioi*Mx != mx) SETERRQ2(PETSC_ERR_ARG_INCOMP,"Ratio between levels: mx/Mx  must be integer: mx %D Mx %D",mx,Mx);
781:   } else {
782:     ratioi = (mx-1)/(Mx-1);
783:     if (ratioi*(Mx-1) != mx-1) SETERRQ2(PETSC_ERR_ARG_INCOMP,"Ratio between levels: (mx - 1)/(Mx - 1) must be integer: mx %D Mx %D",mx,Mx);
784:   }
785:   if (DAYPeriodic(pt)){
786:     ratioj = my/My;
787:     if (ratioj*My != my) SETERRQ2(PETSC_ERR_ARG_INCOMP,"Ratio between levels: my/My  must be integer: my %D My %D",my,My);
788:   } else {
789:     ratioj = (my-1)/(My-1);
790:     if (ratioj*(My-1) != my-1) SETERRQ2(PETSC_ERR_ARG_INCOMP,"Ratio between levels: (my - 1)/(My - 1) must be integer: my %D My %D",my,My);
791:   }


794:   DAGetCorners(daf,&i_start,&j_start,0,&m_f,&n_f,0);
795:   DAGetGhostCorners(daf,&i_start_ghost,&j_start_ghost,0,&m_ghost,&n_ghost,0);
796:   DAGetGlobalIndices(daf,PETSC_NULL,&idx_f);

798:   DAGetCorners(dac,&i_start_c,&j_start_c,0,&m_c,&n_c,0);
799:   DAGetGhostCorners(dac,&i_start_ghost_c,&j_start_ghost_c,0,&m_ghost_c,&n_ghost_c,0);
800:   DAGetGlobalIndices(dac,PETSC_NULL,&idx_c);


803:   /* loop over local fine grid nodes setting interpolation for those*/
804:   nc = 0;
805:   PetscMalloc(n_f*m_f*sizeof(PetscInt),&cols);
806:   for (j=j_start; j<j_start+n_f; j++) {
807:     for (i=i_start; i<i_start+m_f; i++) {

809:       i_c = (i/ratioi);    /* coarse grid node to left of fine grid node */
810:       j_c = (j/ratioj);    /* coarse grid node below fine grid node */

812:       if (j_c < j_start_ghost_c) SETERRQ3(PETSC_ERR_ARG_INCOMP,"Processor's coarse DA must lie over fine DA\n\
813:     j_start %D j_c %D j_start_ghost_c %D",j_start,j_c,j_start_ghost_c);
814:       if (i_c < i_start_ghost_c) SETERRQ3(PETSC_ERR_ARG_INCOMP,"Processor's coarse DA must lie over fine DA\n\
815:     i_start %D i_c %D i_start_ghost_c %D",i_start,i_c,i_start_ghost_c);

817:       if (i_c*ratioi == i && j_c*ratioj == j) {
818:         /* convert to local "natural" numbering and then to PETSc global numbering */
819:         row    = idx_f[dof*(m_ghost*(j-j_start_ghost) + (i-i_start_ghost))];
820:         cols[nc++] = row;
821:       }
822:     }
823:   }

825:   ISCreateBlock(daf->comm,dof,nc,cols,&isf);
826:   DAGetGlobalVector(dac,&vecc);
827:   DAGetGlobalVector(daf,&vecf);
828:   VecScatterCreate(vecf,isf,vecc,PETSC_NULL,inject);
829:   DARestoreGlobalVector(dac,&vecc);
830:   DARestoreGlobalVector(daf,&vecf);
831:   ISDestroy(isf);
832:   PetscFree(cols);
833:   return(0);
834: }

838: /*@C
839:    DAGetInjection - Gets an injection matrix that maps between 
840:    grids associated with two DAs.

842:    Collective on DA

844:    Input Parameters:
845: +  dac - the coarse grid DA
846: -  daf - the fine grid DA

848:    Output Parameters:
849: .  inject - the injection scatter

851:    Level: intermediate

853: .keywords: interpolation, restriction, multigrid, injection 

855: .seealso: DARefine(), DAGetInterpolation()
856: @*/
857: PetscErrorCode PETSCDM_DLLEXPORT DAGetInjection(DA dac,DA daf,VecScatter *inject)
858: {
860:   PetscInt       dimc,Mc,Nc,Pc,mc,nc,pc,dofc,sc,dimf,Mf,Nf,Pf,mf,nf,pf,doff,sf;
861:   DAPeriodicType wrapc,wrapf;
862:   DAStencilType  stc,stf;


869:   DAGetInfo(dac,&dimc,&Mc,&Nc,&Pc,&mc,&nc,&pc,&dofc,&sc,&wrapc,&stc);
870:   DAGetInfo(daf,&dimf,&Mf,&Nf,&Pf,&mf,&nf,&pf,&doff,&sf,&wrapf,&stf);
871:   if (dimc != dimf) SETERRQ2(PETSC_ERR_ARG_INCOMP,"Dimensions of DA do not match %D %D",dimc,dimf);
872:   if (dofc != doff) SETERRQ2(PETSC_ERR_ARG_INCOMP,"DOF of DA do not match %D %D",dofc,doff);
873:   if (sc != sf) SETERRQ2(PETSC_ERR_ARG_INCOMP,"Stencil width of DA do not match %D %D",sc,sf);
874:   if (wrapc != wrapf) SETERRQ(PETSC_ERR_ARG_INCOMP,"Periodic type different in two DAs");
875:   if (stc != stf) SETERRQ(PETSC_ERR_ARG_INCOMP,"Stencil type different in two DAs");
876:   if (Mc < 2) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Coarse grid requires at least 2 points in x direction");
877:   if (dimc > 1 && Nc < 2) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Coarse grid requires at least 2 points in y direction");
878:   if (dimc > 2 && Pc < 2) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Coarse grid requires at least 2 points in z direction");

880:   if (dimc == 2){
881:     DAGetInjection_2D(dac,daf,inject);
882:   } else {
883:     SETERRQ1(PETSC_ERR_SUP,"No support for this DA dimension %D",dimc);
884:   }
885:   return(0);
886: }