Actual source code: ex1.c

petsc-master 2018-08-15
Report Typos and Errors

  2: static char help[] = "VecTagger interface routines.\n\n";

  4:  #include <petscvec.h>

  6: static PetscErrorCode ISGetBlockGlobalIS(IS is, Vec vec, PetscInt bs, IS *isBlockGlobal)
  7: {
  8:   const PetscInt *idxin;
  9:   PetscInt       *idxout, i, n, rstart;
 10:   PetscLayout    map;


 15:   VecGetLayout(vec,&map);
 16:   rstart = map->rstart / bs;
 17:   ISGetLocalSize(is, &n);
 18:   PetscMalloc1(n, &idxout);
 19:   ISGetIndices(is, &idxin);
 20:   for (i = 0; i < n; i++) idxout[i] = rstart + idxin[i];
 21:   ISRestoreIndices(is, &idxin);
 22:   ISCreateBlock(PetscObjectComm((PetscObject)vec),bs,n,idxout,PETSC_OWN_POINTER,isBlockGlobal);
 23:   return(0);
 24: }

 26: int main(int argc, char **argv)
 27: {
 28:   Vec            vec, tagged, untagged;
 29:   VecScatter     taggedScatter, untaggedScatter;
 30:   PetscInt       bs;
 31:   PetscInt       n, nloc, nint, i, j, k, localStart, localEnd, ntagged, nuntagged;
 32:   MPI_Comm       comm;
 33:   VecTagger      tagger;
 34:   PetscScalar    *array;
 35:   PetscRandom    rand;
 36:   VecTaggerBox   *defaultBox;
 37:   VecTaggerBox   *boxes;
 38:   IS             is, isBlockGlobal, isComp;

 41:   PetscInitialize(&argc,&argv,NULL,help);if (ierr) return ierr;
 42:   n    = 10.;
 43:   bs   = 1;
 44:   comm = PETSC_COMM_WORLD;
 45:   PetscOptionsBegin(comm, "" , "VecTagger Test Options", "Vec");
 46:   PetscOptionsInt("-bs","The block size of the vector","ex1.c",bs,&bs,NULL);
 47:   PetscOptionsInt("-n","The size of the vector (in blocks)","ex1.c",n,&n,NULL);
 48:   PetscOptionsEnd();

 50:   PetscRandomCreate(comm,&rand);
 51:   PetscRandomSetFromOptions(rand);

 53:   VecCreate(comm,&vec);
 54:   PetscObjectSetName((PetscObject)vec,"Vec to Tag");
 55:   VecSetBlockSize(vec,bs);
 56:   VecSetSizes(vec,PETSC_DECIDE,n);
 57:   VecSetUp(vec);
 58:   VecGetLocalSize(vec,&nloc);
 59:   VecGetArray(vec,&array);
 60:   for (i = 0; i < nloc; i++) {
 61:     PetscScalar val;

 63:     PetscRandomGetValue(rand,&val);
 64:     array[i] = val;
 65:   }
 66:   VecRestoreArray(vec,&array);
 67:   PetscRandomDestroy(&rand);
 68:   VecViewFromOptions(vec,NULL,"-vec_view");

 70:   VecTaggerCreate(comm,&tagger);
 71:   VecTaggerSetBlockSize(tagger,bs);
 72:   VecTaggerSetType(tagger,VECTAGGERABSOLUTE);
 73:   PetscMalloc1(bs,&defaultBox);
 74:   for (i = 0; i < bs; i++) {
 75: #if !defined(PETSC_USE_COMPLEX)
 76:     defaultBox[i].min = 0.1;
 77:     defaultBox[i].max = 1.5;
 78: #else
 79:     defaultBox[i].min = PetscCMPLX(0.1,0.1);
 80:     defaultBox[i].max = PetscCMPLX(1.5,1.5);
 81: #endif
 82:   }
 83:   VecTaggerAbsoluteSetBox(tagger,defaultBox);
 84:   PetscFree(defaultBox);
 85:   VecTaggerSetFromOptions(tagger);
 86:   VecTaggerSetUp(tagger);
 87:   PetscObjectViewFromOptions((PetscObject)tagger,NULL,"-vec_tagger_view");
 88:   VecTaggerGetBlockSize(tagger,&bs);

 90:   VecTaggerComputeBoxes(tagger,vec,&nint,&boxes);
 91:   if (ierr && ierr != PETSC_ERR_SUP)
 92:   else {
 93:     PetscViewer viewer = NULL;

 95:     PetscOptionsGetViewer(comm,NULL,"-vec_tagger_boxes_view",&viewer,NULL,NULL);
 96:     if (viewer) {
 97:       PetscBool iascii;

 99:       PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
100:       if (iascii) {
101:         PetscViewerASCIIPrintf(viewer,"Num boxes: %D\n",nint);
102:         PetscViewerASCIIPushTab(viewer);
103:         for (i = 0, k = 0; i < nint; i++) {
104:           PetscViewerASCIIPrintf(viewer,"%D: ",i);
105:           for (j = 0; j < bs; j++, k++) {
106:             if (j) {PetscViewerASCIIPrintf(viewer," x ");}
107: #if !defined(PETSC_USE_COMPLEX)
108:             PetscViewerASCIIPrintf(viewer,"[%g,%g]",(double)boxes[k].min,(double)boxes[k].max);
109: #else
110:             PetscViewerASCIIPrintf(viewer,"[%g+%gi,%g+%gi]",(double)PetscRealPart(boxes[k].min),(double)PetscImaginaryPart(boxes[k].min),(double)PetscRealPart(boxes[k].max),(double)PetscImaginaryPart(boxes[k].max));
111: #endif
112:           }
113:           PetscViewerASCIIPrintf(viewer,"\n");
114:         }
115:         PetscViewerASCIIPopTab(viewer);
116:       }
117:     }
118:     PetscViewerDestroy(&viewer);
119:     PetscFree(boxes);
120:   }

122:   VecTaggerComputeIS(tagger,vec,&is);
123:   ISGetBlockGlobalIS(is,vec,bs,&isBlockGlobal);
124:   PetscObjectSetName((PetscObject)isBlockGlobal,"Tagged IS (block global)");
125:   ISViewFromOptions(isBlockGlobal,NULL,"-tagged_is_view");

127:   VecGetOwnershipRange(vec,&localStart,&localEnd);
128:   ISComplement(isBlockGlobal,localStart,localEnd,&isComp);
129:   PetscObjectSetName((PetscObject)isComp,"Untagged IS (global)");
130:   ISViewFromOptions(isComp,NULL,"-untagged_is_view");

132:   ISGetLocalSize(isBlockGlobal,&ntagged);
133:   ISGetLocalSize(isComp,&nuntagged);

135:   VecCreate(comm,&tagged);
136:   PetscObjectSetName((PetscObject)tagged,"Tagged selection");
137:   VecSetSizes(tagged,ntagged,PETSC_DETERMINE);
138:   VecSetUp(tagged);

140:   VecCreate(comm,&untagged);
141:   PetscObjectSetName((PetscObject)untagged,"Untagged selection");
142:   VecSetSizes(untagged,nuntagged,PETSC_DETERMINE);
143:   VecSetUp(untagged);

145:   VecScatterCreate(vec,isBlockGlobal,tagged,NULL,&taggedScatter);
146:   VecScatterCreate(vec,isComp,untagged,NULL,&untaggedScatter);

148:   VecScatterBegin(taggedScatter,vec,tagged,INSERT_VALUES,SCATTER_FORWARD);
149:   VecScatterEnd(taggedScatter,vec,tagged,INSERT_VALUES,SCATTER_FORWARD);
150:   VecScatterBegin(untaggedScatter,vec,untagged,INSERT_VALUES,SCATTER_FORWARD);
151:   VecScatterEnd(untaggedScatter,vec,untagged,INSERT_VALUES,SCATTER_FORWARD);

153:   VecViewFromOptions(tagged,NULL,"-tagged_vec_view");
154:   VecViewFromOptions(untagged,NULL,"-untagged_vec_view");

156:   VecScatterDestroy(&untaggedScatter);
157:   VecScatterDestroy(&taggedScatter);

159:   VecDestroy(&untagged);
160:   VecDestroy(&tagged);
161:   ISDestroy(&isComp);
162:   ISDestroy(&isBlockGlobal);
163:   ISDestroy(&is);

165:   VecTaggerDestroy(&tagger);
166:   VecDestroy(&vec);
167:   PetscFinalize();
168:   return ierr;
169: }

171: /*TEST

173:   test:
174:     suffix: 0
175:     requires: !complex
176:     args: -n 12 -vec_view -vec_tagger_view -vec_tagger_boxes_view -tagged_is_view -untagged_is_view -tagged_vec_view -untagged_vec_view

178:   test:
179:     suffix: 1
180:     requires: !complex
181:     nsize: 3
182:     args: -n 12 -vec_view -vec_tagger_view -vec_tagger_boxes_view -tagged_is_view -untagged_is_view -tagged_vec_view -untagged_vec_view

184:   test:
185:     suffix: 2
186:     requires: !complex
187:     args: -n 12 -vec_view -vec_tagger_view -vec_tagger_boxes_view -tagged_is_view -untagged_is_view -tagged_vec_view -untagged_vec_view -bs 2

189:   test:
190:     suffix: 3
191:     requires: !complex
192:     args: -n 12 -vec_view -vec_tagger_view -vec_tagger_boxes_view -tagged_is_view -untagged_is_view -tagged_vec_view -untagged_vec_view -vec_tagger_block_size 2 -vec_tagger_box 0.1,1.5,0.1,1.5

194:   test:
195:     suffix: 4
196:     requires: !complex
197:     args: -n 12 -vec_view -vec_tagger_view -vec_tagger_boxes_view -tagged_is_view -untagged_is_view -tagged_vec_view -untagged_vec_view -vec_tagger_invert

199:   test:
200:     suffix: 5
201:     requires: !complex
202:     args: -n 12 -vec_view -vec_tagger_view -vec_tagger_boxes_view -tagged_is_view -untagged_is_view -tagged_vec_view -untagged_vec_view -vec_tagger_type relative -vec_tagger_box 0.25,0.75

204:   test:
205:     suffix: 6
206:     requires: !complex
207:     nsize: 3
208:     args: -n 12 -vec_view -vec_tagger_view -vec_tagger_boxes_view -tagged_is_view -untagged_is_view -tagged_vec_view -untagged_vec_view -vec_tagger_type cdf -vec_tagger_box 0.25,0.75

210:   test:
211:     suffix: 7
212:     requires: !complex
213:     nsize: 3
214:     args: -n 12 -vec_view -vec_tagger_view -vec_tagger_boxes_view -tagged_is_view -untagged_is_view -tagged_vec_view -untagged_vec_view -vec_tagger_type cdf -vec_tagger_box 0.25,0.75 -vec_tagger_cdf_method iterative -vec_tagger_cdf_max_it 10

216:   test:
217:     suffix: 8
218:     requires: !complex
219:     nsize: 3
220:     args: -n 12 -vec_view -vec_tagger_view -vec_tagger_boxes_view -tagged_is_view -untagged_is_view -tagged_vec_view -untagged_vec_view -vec_tagger_type or -vec_tagger_num_subs 2 -sub_0_vec_tagger_type absolute -sub_0_vec_tagger_box 0.0,0.25 -sub_1_vec_tagger_type relative -sub_1_vec_tagger_box 0.75,inf
221:     filter: sed -e s~Inf~inf~g

223:   test:
224:     suffix: 9
225:     requires: !complex
226:     nsize: 3
227:     args: -n 12 -vec_view -vec_tagger_view -vec_tagger_boxes_view -tagged_is_view -untagged_is_view -tagged_vec_view -untagged_vec_view -vec_tagger_type and -vec_tagger_num_subs 2 -sub_0_vec_tagger_type absolute -sub_0_vec_tagger_box -inf,0.5 -sub_1_vec_tagger_type relative -sub_1_vec_tagger_box 0.25,0.75
228:     filter: sed -e s~Inf~inf~g

230:   test:
231:     suffix: 10
232:     requires: complex
233:     args: -n 12 -vec_view -vec_tagger_view -vec_tagger_boxes_view -tagged_is_view -untagged_is_view -tagged_vec_view -untagged_vec_view

235:   test:
236:     suffix: 11
237:     requires: complex
238:     nsize: 3
239:     args: -n 12 -vec_view -vec_tagger_view -vec_tagger_boxes_view -tagged_is_view -untagged_is_view -tagged_vec_view -untagged_vec_view

241:   test:
242:     suffix: 12
243:     requires: complex
244:     args: -n 12 -vec_view -vec_tagger_view -vec_tagger_boxes_view -tagged_is_view -untagged_is_view -tagged_vec_view -untagged_vec_view -bs 2

246:   test:
247:     suffix: 13
248:     requires: complex
249:     args: -n 12 -vec_view -vec_tagger_view -vec_tagger_boxes_view -tagged_is_view -untagged_is_view -tagged_vec_view -untagged_vec_view -vec_tagger_block_size 2 -vec_tagger_box 0.1+0.1i,1.5+1.5i,0.1+0.1i,1.5+1.5i

251:   test:
252:     suffix: 14
253:     requires: complex
254:     args: -n 12 -vec_view -vec_tagger_view -vec_tagger_boxes_view -tagged_is_view -untagged_is_view -tagged_vec_view -untagged_vec_view -vec_tagger_invert

256:   test:
257:     suffix: 15
258:     requires: complex
259:     args: -n 12 -vec_view -vec_tagger_view -vec_tagger_boxes_view -tagged_is_view -untagged_is_view -tagged_vec_view -untagged_vec_view -vec_tagger_type relative -vec_tagger_box 0.25+0.25i,0.75+0.75i

261:   test:
262:     suffix: 16
263:     requires: complex
264:     nsize: 3
265:     args: -n 12 -vec_view -vec_tagger_view -vec_tagger_boxes_view -tagged_is_view -untagged_is_view -tagged_vec_view -untagged_vec_view -vec_tagger_type cdf -vec_tagger_box 0.25+0.25i,0.75+0.75i

267:   test:
268:     suffix: 17
269:     requires: complex
270:     nsize: 3
271:     args: -n 12 -vec_view -vec_tagger_view -vec_tagger_boxes_view -tagged_is_view -untagged_is_view -tagged_vec_view -untagged_vec_view -vec_tagger_type cdf -vec_tagger_box 0.25+0.25i,0.75+0.75i -vec_tagger_cdf_method iterative -vec_tagger_cdf_max_it 10

273:   test:
274:     suffix: 18
275:     requires: complex
276:     nsize: 3
277:     args: -n 12 -vec_view -vec_tagger_view -vec_tagger_boxes_view -tagged_is_view -untagged_is_view -tagged_vec_view -untagged_vec_view -vec_tagger_type or -vec_tagger_num_subs 2 -sub_0_vec_tagger_type absolute -sub_0_vec_tagger_box 0.0+0.0i,0.25+0.25i -sub_1_vec_tagger_type relative -sub_1_vec_tagger_box 0.75+0.75i,inf+infi
278:     filter: sed -e s~Inf~inf~g

280:   test:
281:     suffix: 19
282:     requires: complex
283:     nsize: 3
284:     args: -n 12 -vec_view -vec_tagger_view -vec_tagger_boxes_view -tagged_is_view -untagged_is_view -tagged_vec_view -untagged_vec_view -vec_tagger_type and -vec_tagger_num_subs 2 -sub_0_vec_tagger_type absolute -sub_0_vec_tagger_box -inf-infi,0.75+0.75i -sub_1_vec_tagger_type relative -sub_1_vec_tagger_box 0.25+0.25i,1.+1.i
285:     filter: sed -e s~Inf~inf~g

287: TEST*/