Actual source code: ex21.c

petsc-3.14.0 2020-09-29
Report Typos and Errors
  1: static const char help[] = "Test DMCreateInjection() for mapping coordinates in 3D";

  3: #include <petscvec.h>
  4: #include <petscmat.h>
  5: #include <petscdm.h>
  6: #include <petscdmda.h>

  8: PetscErrorCode test1_DAInjection3d(PetscInt mx, PetscInt my, PetscInt mz)
  9: {
 10:   PetscErrorCode   ierr;
 11:   DM               dac,daf;
 12:   PetscViewer      vv;
 13:   Vec              ac,af;
 14:   PetscInt         periodicity;
 15:   DMBoundaryType   bx,by,bz;

 18:   bx = DM_BOUNDARY_NONE;
 19:   by = DM_BOUNDARY_NONE;
 20:   bz = DM_BOUNDARY_NONE;

 22:   periodicity = 0;

 24:   PetscOptionsGetInt(NULL,NULL,"-periodic", &periodicity, NULL);
 25:   if (periodicity==1) {
 26:     bx = DM_BOUNDARY_PERIODIC;
 27:   } else if (periodicity==2) {
 28:     by = DM_BOUNDARY_PERIODIC;
 29:   } else if (periodicity==3) {
 30:     bz = DM_BOUNDARY_PERIODIC;
 31:   }

 33:   DMDACreate3d(PETSC_COMM_WORLD, bx,by,bz, DMDA_STENCIL_BOX,mx+1, my+1,mz+1,PETSC_DECIDE, PETSC_DECIDE,PETSC_DECIDE,1, /* 1 dof */
 34:                       1, /* stencil = 1 */NULL,NULL,NULL,&daf);
 35:   DMSetFromOptions(daf);
 36:   DMSetUp(daf);

 38:   DMCoarsen(daf,MPI_COMM_NULL,&dac);

 40:   DMDASetUniformCoordinates(dac, -1.0,1.0, -1.0,1.0, -1.0,1.0);
 41:   DMDASetUniformCoordinates(daf, -1.0,1.0, -1.0,1.0, -1.0,1.0);

 43:   {
 44:     DM         cdaf,cdac;
 45:     Vec        coordsc,coordsf,coordsf2;
 46:     Mat        inject;
 47:     VecScatter vscat;
 48:     Mat        interp;
 49:     PetscReal  norm;

 51:     DMGetCoordinateDM(dac,&cdac);
 52:     DMGetCoordinateDM(daf,&cdaf);

 54:     DMGetCoordinates(dac,&coordsc);
 55:     DMGetCoordinates(daf,&coordsf);

 57:     DMCreateInjection(cdac,cdaf,&inject);
 58:     MatScatterGetVecScatter(inject,&vscat);
 59:     VecScatterBegin(vscat,coordsf,coordsc,INSERT_VALUES,SCATTER_FORWARD);
 60:     VecScatterEnd(vscat  ,coordsf,coordsc,INSERT_VALUES,SCATTER_FORWARD);
 61:     MatDestroy(&inject);

 63:     DMCreateInterpolation(cdac,cdaf,&interp,NULL);
 64:     VecDuplicate(coordsf,&coordsf2);
 65:     MatInterpolate(interp,coordsc,coordsf2);
 66:     VecAXPY(coordsf2,-1.0,coordsf);
 67:     VecNorm(coordsf2,NORM_MAX,&norm);
 68:     /* The fine coordinates are only reproduced in certain cases */
 69:     if (!bx && !by && !bz && norm > PETSC_SQRT_MACHINE_EPSILON) {PetscPrintf(PETSC_COMM_WORLD,"Norm %g\n",(double)norm);}
 70:     VecDestroy(&coordsf2);
 71:     MatDestroy(&interp);
 72:   }

 74:   if (0) {
 75:     DMCreateGlobalVector(dac,&ac);
 76:     VecZeroEntries(ac);

 78:     DMCreateGlobalVector(daf,&af);
 79:     VecZeroEntries(af);

 81:     PetscViewerASCIIOpen(PETSC_COMM_WORLD, "dac_7.vtu", &vv);
 82:     VecView(ac, vv);
 83:     PetscViewerDestroy(&vv);

 85:     PetscViewerASCIIOpen(PETSC_COMM_WORLD, "daf_7.vtu", &vv);
 86:     VecView(af, vv);
 87:     PetscViewerDestroy(&vv);
 88:     VecDestroy(&ac);
 89:     VecDestroy(&af);
 90:   }
 91:   DMDestroy(&dac);
 92:   DMDestroy(&daf);
 93:   return(0);
 94: }

 96: int main(int argc,char **argv)
 97: {
 99:   PetscInt       mx,my,mz;

101:   PetscInitialize(&argc,&argv,0,help);if (ierr) return ierr;
102:   mx   = 2;
103:   my   = 2;
104:   mz   = 2;
105:   PetscOptionsGetInt(NULL,NULL,"-mx", &mx, 0);
106:   PetscOptionsGetInt(NULL,NULL,"-my", &my, 0);
107:   PetscOptionsGetInt(NULL,NULL,"-mz", &mz, 0);
108:   test1_DAInjection3d(mx,my,mz);
109:   PetscFinalize();
110:   return ierr;
111: }


114: /*TEST

116:       test:
117:          nsize: 5
118:          args: -mx 30 -my 30 -mz 30 -periodic 0 -da_processors_x 5

120:       test:
121:          suffix: 2
122:          nsize: 5
123:          args: -mx 29 -my 30 -mz 30 -periodic 1 -da_processors_x 5

125:       test:
126:          suffix: 3
127:          nsize: 5
128:          args: -mx 30 -my 29 -mz 30 -periodic 2 -da_processors_x 5

130:       test:
131:          suffix: 4
132:          nsize: 5
133:          args: -mx 30 -my 30 -mz 29 -periodic 3 -da_processors_x 5

135:       test:
136:          suffix: 5
137:          nsize: 5
138:          args: -mx 30 -my 30 -mz 30 -periodic 0 -da_processors_y 5

140:       test:
141:          suffix: 6
142:          nsize: 5
143:          args: -mx 29 -my 30 -mz 30 -periodic 1 -da_processors_y 5

145:       test:
146:          suffix: 7
147:          nsize: 5
148:          args: -mx 30 -my 29 -mz 30 -periodic 2 -da_processors_y 5

150:       test:
151:          suffix: 8
152:          nsize: 5
153:          args: -mx 30 -my 30 -mz 29 -periodic 3 -da_processors_y 5

155:       test:
156:          suffix: 9
157:          nsize: 5
158:          args: -mx 30 -my 30 -mz 30 -periodic 0 -da_processors_z 5

160:       test:
161:          suffix: 10
162:          nsize: 5
163:          args: -mx 29 -my 30 -mz 30 -periodic 1 -da_processors_z 5

165:       test:
166:          suffix: 11
167:          nsize: 5
168:          args: -mx 30 -my 29 -mz 30 -periodic 2 -da_processors_z 5

170:       test:
171:          suffix: 12
172:          nsize: 5
173:          args: -mx 30 -my 30 -mz 29 -periodic 3 -da_processors_z 5

175:       test:
176:          suffix: 13
177:          nsize: 5
178:          args: -mx 30 -my 30 -mz 30 -periodic 0

180:       test:
181:          suffix: 14
182:          nsize: 5
183:          args: -mx 29 -my 30 -mz 30 -periodic 1

185:       test:
186:          suffix: 15
187:          nsize: 5
188:          args: -mx 30 -my 29 -mz 30 -periodic 2

190:       test:
191:          suffix: 16
192:          nsize: 5
193:          args: -mx 30 -my 30 -mz 29 -periodic 3

195: TEST*/