xref: /petsc/src/dm/tutorials/swarm_ex1.c (revision 356ed81403a8ddb9cbcae868d64486ea275d004c)
1 
2 static char help[] = "Tests DMSwarm\n\n";
3 
4 #include <petscdm.h>
5 #include <petscdmda.h>
6 #include <petscdmswarm.h>
7 
8 PETSC_EXTERN PetscErrorCode DMSwarmCollect_General(DM,PetscErrorCode (*)(DM,void*,PetscInt*,PetscInt**),size_t,void*,PetscInt*);
9 PETSC_EXTERN PetscErrorCode DMSwarmCollect_DMDABoundingBox(DM,PetscInt*);
10 
11 PetscErrorCode ex1_1(void)
12 {
13   DM             dms;
14   Vec            x;
15   PetscMPIInt    rank,size;
16   PetscInt       p;
17 
18   PetscFunctionBegin;
19   PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD,&size));
20   PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD,&rank));
21   PetscCheckFalse((size > 1) && (size != 4),PETSC_COMM_WORLD,PETSC_ERR_SUP,"Must be run wuth 4 MPI ranks");
22 
23   PetscCall(DMCreate(PETSC_COMM_WORLD,&dms));
24   PetscCall(DMSetType(dms,DMSWARM));
25 
26   PetscCall(DMSwarmInitializeFieldRegister(dms));
27   PetscCall(DMSwarmRegisterPetscDatatypeField(dms,"viscosity",1,PETSC_REAL));
28   PetscCall(DMSwarmRegisterPetscDatatypeField(dms,"strain",1,PETSC_REAL));
29   PetscCall(DMSwarmFinalizeFieldRegister(dms));
30   PetscCall(DMSwarmSetLocalSizes(dms,5+rank,4));
31   PetscCall(DMView(dms,PETSC_VIEWER_STDOUT_WORLD));
32 
33   {
34     PetscReal *array;
35     PetscCall(DMSwarmGetField(dms,"viscosity",NULL,NULL,(void**)&array));
36     for (p=0; p<5+rank; p++) {
37       array[p] = 11.1 + p*0.1 + rank*100.0;
38     }
39     PetscCall(DMSwarmRestoreField(dms,"viscosity",NULL,NULL,(void**)&array));
40   }
41 
42   {
43     PetscReal *array;
44     PetscCall(DMSwarmGetField(dms,"strain",NULL,NULL,(void**)&array));
45     for (p=0; p<5+rank; p++) {
46       array[p] = 2.0e-2 + p*0.001 + rank*1.0;
47     }
48     PetscCall(DMSwarmRestoreField(dms,"strain",NULL,NULL,(void**)&array));
49   }
50 
51   PetscCall(DMSwarmCreateGlobalVectorFromField(dms,"viscosity",&x));
52   PetscCall(DMSwarmDestroyGlobalVectorFromField(dms,"viscosity",&x));
53 
54   PetscCall(DMSwarmVectorDefineField(dms,"strain"));
55   PetscCall(DMCreateGlobalVector(dms,&x));
56   PetscCall(VecDestroy(&x));
57 
58   {
59     PetscInt    *rankval;
60     PetscInt    npoints[2],npoints_orig[2];
61 
62     PetscCall(DMSwarmGetLocalSize(dms,&npoints_orig[0]));
63     PetscCall(DMSwarmGetSize(dms,&npoints_orig[1]));
64     PetscCall(DMSwarmGetField(dms,"DMSwarm_rank",NULL,NULL,(void**)&rankval));
65     if ((rank == 0) && (size > 1)) {
66       rankval[0] = 1;
67       rankval[3] = 1;
68     }
69     if (rank == 3) {
70       rankval[2] = 1;
71     }
72     PetscCall(DMSwarmRestoreField(dms,"DMSwarm_rank",NULL,NULL,(void**)&rankval));
73     PetscCall(DMSwarmMigrate(dms,PETSC_TRUE));
74     PetscCall(DMSwarmGetLocalSize(dms,&npoints[0]));
75     PetscCall(DMSwarmGetSize(dms,&npoints[1]));
76     PetscCall(PetscViewerASCIIPushSynchronized(PETSC_VIEWER_STDOUT_WORLD));
77     PetscCall(PetscViewerASCIISynchronizedPrintf(PETSC_VIEWER_STDOUT_WORLD,"rank[%d] before(%D,%D) after(%D,%D)\n",rank,npoints_orig[0],npoints_orig[1],npoints[0],npoints[1]));
78     PetscCall(PetscViewerFlush(PETSC_VIEWER_STDOUT_WORLD));
79     PetscCall(PetscViewerASCIIPopSynchronized(PETSC_VIEWER_STDOUT_WORLD));
80   }
81   {
82     PetscCall(DMSwarmCreateGlobalVectorFromField(dms,"viscosity",&x));
83     PetscCall(VecView(x,PETSC_VIEWER_STDOUT_WORLD));
84     PetscCall(DMSwarmDestroyGlobalVectorFromField(dms,"viscosity",&x));
85   }
86   {
87     PetscCall(DMSwarmCreateGlobalVectorFromField(dms,"strain",&x));
88     PetscCall(VecView(x,PETSC_VIEWER_STDOUT_WORLD));
89     PetscCall(DMSwarmDestroyGlobalVectorFromField(dms,"strain",&x));
90   }
91 
92   PetscCall(DMDestroy(&dms));
93   PetscFunctionReturn(0);
94 }
95 
96 PetscErrorCode ex1_2(void)
97 {
98   DM             dms;
99   Vec            x;
100   PetscMPIInt    rank,size;
101   PetscInt       p;
102 
103   PetscFunctionBegin;
104   PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD,&size));
105   PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD,&rank));
106   PetscCall(DMCreate(PETSC_COMM_WORLD,&dms));
107   PetscCall(DMSetType(dms,DMSWARM));
108   PetscCall(DMSwarmInitializeFieldRegister(dms));
109 
110   PetscCall(DMSwarmRegisterPetscDatatypeField(dms,"viscosity",1,PETSC_REAL));
111   PetscCall(DMSwarmRegisterPetscDatatypeField(dms,"strain",1,PETSC_REAL));
112   PetscCall(DMSwarmFinalizeFieldRegister(dms));
113   PetscCall(DMSwarmSetLocalSizes(dms,5+rank,4));
114   PetscCall(DMView(dms,PETSC_VIEWER_STDOUT_WORLD));
115   {
116     PetscReal *array;
117     PetscCall(DMSwarmGetField(dms,"viscosity",NULL,NULL,(void**)&array));
118     for (p=0; p<5+rank; p++) {
119       array[p] = 11.1 + p*0.1 + rank*100.0;
120     }
121     PetscCall(DMSwarmRestoreField(dms,"viscosity",NULL,NULL,(void**)&array));
122   }
123   {
124     PetscReal *array;
125     PetscCall(DMSwarmGetField(dms,"strain",NULL,NULL,(void**)&array));
126     for (p=0; p<5+rank; p++) {
127       array[p] = 2.0e-2 + p*0.001 + rank*1.0;
128     }
129     PetscCall(DMSwarmRestoreField(dms,"strain",NULL,NULL,(void**)&array));
130   }
131   {
132     PetscInt    *rankval;
133     PetscInt    npoints[2],npoints_orig[2];
134 
135     PetscCall(DMSwarmGetLocalSize(dms,&npoints_orig[0]));
136     PetscCall(DMSwarmGetSize(dms,&npoints_orig[1]));
137     PetscCall(PetscViewerASCIIPushSynchronized(PETSC_VIEWER_STDOUT_WORLD));
138     PetscCall(PetscViewerASCIISynchronizedPrintf(PETSC_VIEWER_STDOUT_WORLD,"rank[%d] before(%D,%D)\n",rank,npoints_orig[0],npoints_orig[1]));
139     PetscCall(PetscViewerFlush(PETSC_VIEWER_STDOUT_WORLD));
140     PetscCall(PetscViewerASCIIPopSynchronized(PETSC_VIEWER_STDOUT_WORLD));
141 
142     PetscCall(DMSwarmGetField(dms,"DMSwarm_rank",NULL,NULL,(void**)&rankval));
143 
144     if (rank == 1) {
145       rankval[0] = -1;
146     }
147     if (rank == 2) {
148       rankval[1] = -1;
149     }
150     if (rank == 3) {
151       rankval[3] = -1;
152       rankval[4] = -1;
153     }
154     PetscCall(DMSwarmRestoreField(dms,"DMSwarm_rank",NULL,NULL,(void**)&rankval));
155     PetscCall(DMSwarmCollectViewCreate(dms));
156     PetscCall(DMSwarmGetLocalSize(dms,&npoints[0]));
157     PetscCall(DMSwarmGetSize(dms,&npoints[1]));
158     PetscCall(PetscViewerASCIIPushSynchronized(PETSC_VIEWER_STDOUT_WORLD));
159     PetscCall(PetscViewerASCIISynchronizedPrintf(PETSC_VIEWER_STDOUT_WORLD,"rank[%d] after(%D,%D)\n",rank,npoints[0],npoints[1]));
160     PetscCall(PetscViewerFlush(PETSC_VIEWER_STDOUT_WORLD));
161     PetscCall(PetscViewerASCIIPopSynchronized(PETSC_VIEWER_STDOUT_WORLD));
162 
163     PetscCall(DMSwarmCreateGlobalVectorFromField(dms,"viscosity",&x));
164     PetscCall(VecView(x,PETSC_VIEWER_STDOUT_WORLD));
165     PetscCall(DMSwarmDestroyGlobalVectorFromField(dms,"viscosity",&x));
166 
167     PetscCall(DMSwarmCollectViewDestroy(dms));
168     PetscCall(DMSwarmGetLocalSize(dms,&npoints[0]));
169     PetscCall(DMSwarmGetSize(dms,&npoints[1]));
170     PetscCall(PetscViewerASCIIPushSynchronized(PETSC_VIEWER_STDOUT_WORLD));
171     PetscCall(PetscViewerASCIISynchronizedPrintf(PETSC_VIEWER_STDOUT_WORLD,"rank[%d] after_v(%D,%D)\n",rank,npoints[0],npoints[1]));
172     PetscCall(PetscViewerFlush(PETSC_VIEWER_STDOUT_WORLD));
173     PetscCall(PetscViewerASCIIPopSynchronized(PETSC_VIEWER_STDOUT_WORLD));
174 
175     PetscCall(DMSwarmCreateGlobalVectorFromField(dms,"viscosity",&x));
176     PetscCall(VecView(x,PETSC_VIEWER_STDOUT_WORLD));
177     PetscCall(DMSwarmDestroyGlobalVectorFromField(dms,"viscosity",&x));
178   }
179   PetscCall(DMDestroy(&dms));
180   PetscFunctionReturn(0);
181 }
182 
183 /*
184  splot "c-rank0.gp","c-rank1.gp","c-rank2.gp","c-rank3.gp"
185 */
186 PetscErrorCode ex1_3(void)
187 {
188   DM             dms;
189   PetscMPIInt    rank,size;
190   PetscInt       is,js,ni,nj,overlap;
191   DM             dmcell;
192 
193   PetscFunctionBegin;
194   PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD,&size));
195   PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD,&rank));
196   overlap = 2;
197   PetscCall(DMDACreate2d(PETSC_COMM_WORLD,DM_BOUNDARY_NONE,DM_BOUNDARY_NONE,DMDA_STENCIL_BOX,13,13,PETSC_DECIDE,PETSC_DECIDE,1,overlap,NULL,NULL,&dmcell));
198   PetscCall(DMSetFromOptions(dmcell));
199   PetscCall(DMSetUp(dmcell));
200   PetscCall(DMDASetUniformCoordinates(dmcell,-1.0,1.0,-1.0,1.0,-1.0,1.0));
201   PetscCall(DMDAGetCorners(dmcell,&is,&js,NULL,&ni,&nj,NULL));
202   PetscCall(DMCreate(PETSC_COMM_WORLD,&dms));
203   PetscCall(DMSetType(dms,DMSWARM));
204   PetscCall(DMSwarmSetCellDM(dms,dmcell));
205 
206   /* load in data types */
207   PetscCall(DMSwarmInitializeFieldRegister(dms));
208   PetscCall(DMSwarmRegisterPetscDatatypeField(dms,"viscosity",1,PETSC_REAL));
209   PetscCall(DMSwarmRegisterPetscDatatypeField(dms,"coorx",1,PETSC_REAL));
210   PetscCall(DMSwarmRegisterPetscDatatypeField(dms,"coory",1,PETSC_REAL));
211   PetscCall(DMSwarmFinalizeFieldRegister(dms));
212   PetscCall(DMSwarmSetLocalSizes(dms,ni*nj*4,4));
213   PetscCall(DMView(dms,PETSC_VIEWER_STDOUT_WORLD));
214 
215   /* set values within the swarm */
216   {
217     PetscReal  *array_x,*array_y;
218     PetscInt   npoints,i,j,cnt;
219     DMDACoor2d **LA_coor;
220     Vec        coor;
221     DM         dmcellcdm;
222 
223     PetscCall(DMGetCoordinateDM(dmcell,&dmcellcdm));
224     PetscCall(DMGetCoordinates(dmcell,&coor));
225     PetscCall(DMDAVecGetArray(dmcellcdm,coor,&LA_coor));
226     PetscCall(DMSwarmGetLocalSize(dms,&npoints));
227     PetscCall(DMSwarmGetField(dms,"coorx",NULL,NULL,(void**)&array_x));
228     PetscCall(DMSwarmGetField(dms,"coory",NULL,NULL,(void**)&array_y));
229     cnt = 0;
230     for (j=js; j<js+nj; j++) {
231       for (i=is; i<is+ni; i++) {
232         PetscReal xp,yp;
233         xp = PetscRealPart(LA_coor[j][i].x);
234         yp = PetscRealPart(LA_coor[j][i].y);
235         array_x[4*cnt+0] = xp - 0.05; if (array_x[4*cnt+0] < -1.0) { array_x[4*cnt+0] = -1.0+1.0e-12; }
236         array_x[4*cnt+1] = xp + 0.05; if (array_x[4*cnt+1] > 1.0)  { array_x[4*cnt+1] =  1.0-1.0e-12; }
237         array_x[4*cnt+2] = xp - 0.05; if (array_x[4*cnt+2] < -1.0) { array_x[4*cnt+2] = -1.0+1.0e-12; }
238         array_x[4*cnt+3] = xp + 0.05; if (array_x[4*cnt+3] > 1.0)  { array_x[4*cnt+3] =  1.0-1.0e-12; }
239 
240         array_y[4*cnt+0] = yp - 0.05; if (array_y[4*cnt+0] < -1.0) { array_y[4*cnt+0] = -1.0+1.0e-12; }
241         array_y[4*cnt+1] = yp - 0.05; if (array_y[4*cnt+1] < -1.0) { array_y[4*cnt+1] = -1.0+1.0e-12; }
242         array_y[4*cnt+2] = yp + 0.05; if (array_y[4*cnt+2] > 1.0)  { array_y[4*cnt+2] =  1.0-1.0e-12; }
243         array_y[4*cnt+3] = yp + 0.05; if (array_y[4*cnt+3] > 1.0)  { array_y[4*cnt+3] =  1.0-1.0e-12; }
244         cnt++;
245       }
246     }
247     PetscCall(DMSwarmRestoreField(dms,"coory",NULL,NULL,(void**)&array_y));
248     PetscCall(DMSwarmRestoreField(dms,"coorx",NULL,NULL,(void**)&array_x));
249     PetscCall(DMDAVecRestoreArray(dmcellcdm,coor,&LA_coor));
250   }
251   {
252     PetscInt    npoints[2],npoints_orig[2],ng;
253 
254     PetscCall(DMSwarmGetLocalSize(dms,&npoints_orig[0]));
255     PetscCall(DMSwarmGetSize(dms,&npoints_orig[1]));
256     PetscCall(PetscViewerASCIIPushSynchronized(PETSC_VIEWER_STDOUT_WORLD));
257     PetscCall(PetscViewerASCIISynchronizedPrintf(PETSC_VIEWER_STDOUT_WORLD,"rank[%d] before(%D,%D)\n",rank,npoints_orig[0],npoints_orig[1]));
258     PetscCall(PetscViewerFlush(PETSC_VIEWER_STDOUT_WORLD));
259     PetscCall(PetscViewerASCIIPopSynchronized(PETSC_VIEWER_STDOUT_WORLD));
260     PetscCall(DMSwarmCollect_DMDABoundingBox(dms,&ng));
261 
262     PetscCall(DMSwarmGetLocalSize(dms,&npoints[0]));
263     PetscCall(DMSwarmGetSize(dms,&npoints[1]));
264     PetscCall(PetscViewerASCIIPushSynchronized(PETSC_VIEWER_STDOUT_WORLD));
265     PetscCall(PetscViewerASCIISynchronizedPrintf(PETSC_VIEWER_STDOUT_WORLD,"rank[%d] after(%D,%D)\n",rank,npoints[0],npoints[1]));
266     PetscCall(PetscViewerFlush(PETSC_VIEWER_STDOUT_WORLD));
267     PetscCall(PetscViewerASCIIPopSynchronized(PETSC_VIEWER_STDOUT_WORLD));
268   }
269   {
270     PetscReal *array_x,*array_y;
271     PetscInt  npoints,p;
272     FILE      *fp = NULL;
273     char      name[PETSC_MAX_PATH_LEN];
274 
275     PetscCall(PetscSNPrintf(name,PETSC_MAX_PATH_LEN-1,"c-rank%d.gp",rank));
276     fp = fopen(name,"w");
277     PetscCheck(fp,PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Cannot open file %s",name);
278     PetscCall(DMSwarmGetLocalSize(dms,&npoints));
279     PetscCall(DMSwarmGetField(dms,"coorx",NULL,NULL,(void**)&array_x));
280     PetscCall(DMSwarmGetField(dms,"coory",NULL,NULL,(void**)&array_y));
281     for (p=0; p<npoints; p++) {
282       fprintf(fp,"%+1.4e %+1.4e %1.4e\n",array_x[p],array_y[p],(double)rank);
283     }
284     PetscCall(DMSwarmRestoreField(dms,"coory",NULL,NULL,(void**)&array_y));
285     PetscCall(DMSwarmRestoreField(dms,"coorx",NULL,NULL,(void**)&array_x));
286     fclose(fp);
287   }
288   PetscCall(DMDestroy(&dmcell));
289   PetscCall(DMDestroy(&dms));
290   PetscFunctionReturn(0);
291 }
292 
293 typedef struct {
294   PetscReal cx[2];
295   PetscReal radius;
296 } CollectZoneCtx;
297 
298 PetscErrorCode collect_zone(DM dm,void *ctx,PetscInt *nfound,PetscInt **foundlist)
299 {
300   CollectZoneCtx *zone = (CollectZoneCtx*)ctx;
301   PetscInt       p,npoints;
302   PetscReal      *array_x,*array_y,r2;
303   PetscInt       p2collect,*plist;
304   PetscMPIInt    rank;
305 
306   PetscFunctionBegin;
307   PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD,&rank));
308   PetscCall(DMSwarmGetLocalSize(dm,&npoints));
309   PetscCall(DMSwarmGetField(dm,"coorx",NULL,NULL,(void**)&array_x));
310   PetscCall(DMSwarmGetField(dm,"coory",NULL,NULL,(void**)&array_y));
311 
312   r2 = zone->radius * zone->radius;
313   p2collect = 0;
314   for (p=0; p<npoints; p++) {
315     PetscReal sep2;
316 
317     sep2  = (array_x[p] - zone->cx[0])*(array_x[p] - zone->cx[0]);
318     sep2 += (array_y[p] - zone->cx[1])*(array_y[p] - zone->cx[1]);
319     if (sep2 < r2) {
320       p2collect++;
321     }
322   }
323 
324   PetscCall(PetscMalloc1(p2collect+1,&plist));
325   p2collect = 0;
326   for (p=0; p<npoints; p++) {
327     PetscReal sep2;
328 
329     sep2  = (array_x[p] - zone->cx[0])*(array_x[p] - zone->cx[0]);
330     sep2 += (array_y[p] - zone->cx[1])*(array_y[p] - zone->cx[1]);
331     if (sep2 < r2) {
332       plist[p2collect] = p;
333       p2collect++;
334     }
335   }
336   PetscCall(DMSwarmRestoreField(dm,"coory",NULL,NULL,(void**)&array_y));
337   PetscCall(DMSwarmRestoreField(dm,"coorx",NULL,NULL,(void**)&array_x));
338 
339   *nfound = p2collect;
340   *foundlist = plist;
341   PetscFunctionReturn(0);
342 }
343 
344 PetscErrorCode ex1_4(void)
345 {
346   DM             dms;
347   PetscMPIInt    rank,size;
348   PetscInt       is,js,ni,nj,overlap,nn;
349   DM             dmcell;
350   CollectZoneCtx *zone;
351   PetscReal      dx;
352 
353   PetscFunctionBegin;
354   PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD,&size));
355   PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD,&rank));
356   nn = 101;
357   dx = 2.0/ (PetscReal)(nn-1);
358   overlap = 0;
359   PetscCall(DMDACreate2d(PETSC_COMM_WORLD,DM_BOUNDARY_NONE,DM_BOUNDARY_NONE,DMDA_STENCIL_BOX,nn,nn,PETSC_DECIDE,PETSC_DECIDE,1,overlap,NULL,NULL,&dmcell));
360   PetscCall(DMSetFromOptions(dmcell));
361   PetscCall(DMSetUp(dmcell));
362   PetscCall(DMDASetUniformCoordinates(dmcell,-1.0,1.0,-1.0,1.0,-1.0,1.0));
363   PetscCall(DMDAGetCorners(dmcell,&is,&js,NULL,&ni,&nj,NULL));
364   PetscCall(DMCreate(PETSC_COMM_WORLD,&dms));
365   PetscCall(DMSetType(dms,DMSWARM));
366 
367   /* load in data types */
368   PetscCall(DMSwarmInitializeFieldRegister(dms));
369   PetscCall(DMSwarmRegisterPetscDatatypeField(dms,"viscosity",1,PETSC_REAL));
370   PetscCall(DMSwarmRegisterPetscDatatypeField(dms,"coorx",1,PETSC_REAL));
371   PetscCall(DMSwarmRegisterPetscDatatypeField(dms,"coory",1,PETSC_REAL));
372   PetscCall(DMSwarmFinalizeFieldRegister(dms));
373   PetscCall(DMSwarmSetLocalSizes(dms,ni*nj*4,4));
374   PetscCall(DMView(dms,PETSC_VIEWER_STDOUT_WORLD));
375 
376   /* set values within the swarm */
377   {
378     PetscReal  *array_x,*array_y;
379     PetscInt   npoints,i,j,cnt;
380     DMDACoor2d **LA_coor;
381     Vec        coor;
382     DM         dmcellcdm;
383 
384     PetscCall(DMGetCoordinateDM(dmcell,&dmcellcdm));
385     PetscCall(DMGetCoordinates(dmcell,&coor));
386     PetscCall(DMDAVecGetArray(dmcellcdm,coor,&LA_coor));
387     PetscCall(DMSwarmGetLocalSize(dms,&npoints));
388     PetscCall(DMSwarmGetField(dms,"coorx",NULL,NULL,(void**)&array_x));
389     PetscCall(DMSwarmGetField(dms,"coory",NULL,NULL,(void**)&array_y));
390     cnt = 0;
391     for (j=js; j<js+nj; j++) {
392       for (i=is; i<is+ni; i++) {
393         PetscReal xp,yp;
394 
395         xp = PetscRealPart(LA_coor[j][i].x);
396         yp = PetscRealPart(LA_coor[j][i].y);
397         array_x[4*cnt+0] = xp - dx*0.1; /*if (array_x[4*cnt+0] < -1.0) { array_x[4*cnt+0] = -1.0+1.0e-12; }*/
398         array_x[4*cnt+1] = xp + dx*0.1; /*if (array_x[4*cnt+1] > 1.0)  { array_x[4*cnt+1] =  1.0-1.0e-12; }*/
399         array_x[4*cnt+2] = xp - dx*0.1; /*if (array_x[4*cnt+2] < -1.0) { array_x[4*cnt+2] = -1.0+1.0e-12; }*/
400         array_x[4*cnt+3] = xp + dx*0.1; /*if (array_x[4*cnt+3] > 1.0)  { array_x[4*cnt+3] =  1.0-1.0e-12; }*/
401         array_y[4*cnt+0] = yp - dx*0.1; /*if (array_y[4*cnt+0] < -1.0) { array_y[4*cnt+0] = -1.0+1.0e-12; }*/
402         array_y[4*cnt+1] = yp - dx*0.1; /*if (array_y[4*cnt+1] < -1.0) { array_y[4*cnt+1] = -1.0+1.0e-12; }*/
403         array_y[4*cnt+2] = yp + dx*0.1; /*if (array_y[4*cnt+2] > 1.0)  { array_y[4*cnt+2] =  1.0-1.0e-12; }*/
404         array_y[4*cnt+3] = yp + dx*0.1; /*if (array_y[4*cnt+3] > 1.0)  { array_y[4*cnt+3] =  1.0-1.0e-12; }*/
405         cnt++;
406       }
407     }
408     PetscCall(DMSwarmRestoreField(dms,"coory",NULL,NULL,(void**)&array_y));
409     PetscCall(DMSwarmRestoreField(dms,"coorx",NULL,NULL,(void**)&array_x));
410     PetscCall(DMDAVecRestoreArray(dmcellcdm,coor,&LA_coor));
411   }
412   PetscCall(PetscMalloc1(1,&zone));
413   if (size == 4) {
414     if (rank == 0) {
415       zone->cx[0] = 0.5;
416       zone->cx[1] = 0.5;
417       zone->radius = 0.3;
418     }
419     if (rank == 1) {
420       zone->cx[0] = -0.5;
421       zone->cx[1] = 0.5;
422       zone->radius = 0.25;
423     }
424     if (rank == 2) {
425       zone->cx[0] = 0.5;
426       zone->cx[1] = -0.5;
427       zone->radius = 0.2;
428     }
429     if (rank == 3) {
430       zone->cx[0] = -0.5;
431       zone->cx[1] = -0.5;
432       zone->radius = 0.1;
433     }
434   } else {
435     if (rank == 0) {
436       zone->cx[0] = 0.5;
437       zone->cx[1] = 0.5;
438       zone->radius = 0.8;
439     } else {
440       zone->cx[0] = 10.0;
441       zone->cx[1] = 10.0;
442       zone->radius = 0.0;
443     }
444   }
445   {
446     PetscInt    npoints[2],npoints_orig[2],ng;
447 
448     PetscCall(DMSwarmGetLocalSize(dms,&npoints_orig[0]));
449     PetscCall(DMSwarmGetSize(dms,&npoints_orig[1]));
450     PetscCall(PetscViewerASCIIPushSynchronized(PETSC_VIEWER_STDOUT_WORLD));
451     PetscCall(PetscViewerASCIISynchronizedPrintf(PETSC_VIEWER_STDOUT_WORLD,"rank[%d] before(%D,%D)\n",rank,npoints_orig[0],npoints_orig[1]));
452     PetscCall(PetscViewerFlush(PETSC_VIEWER_STDOUT_WORLD));
453     PetscCall(PetscViewerASCIIPopSynchronized(PETSC_VIEWER_STDOUT_WORLD));
454     PetscCall(DMSwarmCollect_General(dms,collect_zone,sizeof(CollectZoneCtx),zone,&ng));
455     PetscCall(DMSwarmGetLocalSize(dms,&npoints[0]));
456     PetscCall(DMSwarmGetSize(dms,&npoints[1]));
457     PetscCall(PetscViewerASCIIPushSynchronized(PETSC_VIEWER_STDOUT_WORLD));
458     PetscCall(PetscViewerASCIISynchronizedPrintf(PETSC_VIEWER_STDOUT_WORLD,"rank[%d] after(%D,%D)\n",rank,npoints[0],npoints[1]));
459     PetscCall(PetscViewerFlush(PETSC_VIEWER_STDOUT_WORLD));
460     PetscCall(PetscViewerASCIIPopSynchronized(PETSC_VIEWER_STDOUT_WORLD));
461   }
462   {
463     PetscReal *array_x,*array_y;
464     PetscInt  npoints,p;
465     FILE      *fp = NULL;
466     char      name[PETSC_MAX_PATH_LEN];
467 
468     PetscCall(PetscSNPrintf(name,PETSC_MAX_PATH_LEN-1,"c-rank%d.gp",rank));
469     fp = fopen(name,"w");
470     PetscCheck(fp,PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Cannot open file %s",name);
471     PetscCall(DMSwarmGetLocalSize(dms,&npoints));
472     PetscCall(DMSwarmGetField(dms,"coorx",NULL,NULL,(void**)&array_x));
473     PetscCall(DMSwarmGetField(dms,"coory",NULL,NULL,(void**)&array_y));
474     for (p=0; p<npoints; p++) {
475       fprintf(fp,"%+1.4e %+1.4e %1.4e\n",array_x[p],array_y[p],(double)rank);
476     }
477     PetscCall(DMSwarmRestoreField(dms,"coory",NULL,NULL,(void**)&array_y));
478     PetscCall(DMSwarmRestoreField(dms,"coorx",NULL,NULL,(void**)&array_x));
479     fclose(fp);
480   }
481   PetscCall(DMDestroy(&dmcell));
482   PetscCall(DMDestroy(&dms));
483   PetscCall(PetscFree(zone));
484   PetscFunctionReturn(0);
485 }
486 
487 int main(int argc,char **argv)
488 {
489   PetscInt       test_mode = 4;
490 
491   PetscCall(PetscInitialize(&argc,&argv,(char*)0,help));
492   PetscCall(PetscOptionsGetInt(NULL,NULL,"-test_mode",&test_mode,NULL));
493   if (test_mode == 1) {
494     PetscCall(ex1_1());
495   } else if (test_mode == 2) {
496     PetscCall(ex1_2());
497   } else if (test_mode == 3) {
498     PetscCall(ex1_3());
499   } else if (test_mode == 4) {
500     PetscCall(ex1_4());
501   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_USER,"Unknown test_mode value, should be 1,2,3,4");
502   PetscCall(PetscFinalize());
503   return 0;
504 }
505 
506 /*TEST
507 
508    build:
509       requires: !complex double
510 
511    test:
512       args: -test_mode 1
513       filter: grep -v atomic
514       filter_output: grep -v atomic
515 
516    test:
517       suffix: 2
518       args: -test_mode 2
519       filter: grep -v atomic
520       filter_output: grep -v atomic
521 
522    test:
523       suffix: 3
524       args: -test_mode 3
525       filter: grep -v atomic
526       filter_output: grep -v atomic
527       TODO: broken
528 
529    test:
530       suffix: 4
531       args: -test_mode 4
532       filter: grep -v atomic
533       filter_output: grep -v atomic
534 
535    test:
536       suffix: 5
537       nsize: 4
538       args: -test_mode 1
539       filter: grep -v atomic
540       filter_output: grep -v atomic
541 
542    test:
543       suffix: 6
544       nsize: 4
545       args: -test_mode 2
546       filter: grep -v atomic
547       filter_output: grep -v atomic
548 
549    test:
550       suffix: 7
551       nsize: 4
552       args: -test_mode 3
553       filter: grep -v atomic
554       filter_output: grep -v atomic
555       TODO: broken
556 
557    test:
558       suffix: 8
559       nsize: 4
560       args: -test_mode 4
561       filter: grep -v atomic
562       filter_output: grep -v atomic
563 
564 TEST*/
565