Actual source code: swarm_ex1.c

  1: #include "petscis.h"
  2: #include "petscvec.h"
  3: static char help[] = "Tests DMSwarm\n\n";

  5: #include <petscdm.h>
  6: #include <petscdmda.h>
  7: #include <petscdmswarm.h>

  9: PETSC_EXTERN PetscErrorCode DMSwarmCollect_General(DM, PetscErrorCode (*)(DM, void *, PetscInt *, PetscInt **), size_t, void *, PetscInt *);
 10: PETSC_EXTERN PetscErrorCode DMSwarmCollect_DMDABoundingBox(DM, PetscInt *);

 12: PetscErrorCode ex1_1(void)
 13: {
 14:   DM          dms;
 15:   Vec         x;
 16:   PetscMPIInt rank, size;
 17:   PetscInt    p;

 19:   PetscFunctionBegin;
 20:   PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD, &size));
 21:   PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank));
 22:   PetscCheck(!(size > 1) || !(size != 4), PETSC_COMM_WORLD, PETSC_ERR_SUP, "Must be run with 4 MPI ranks");

 24:   PetscCall(DMCreate(PETSC_COMM_WORLD, &dms));
 25:   PetscCall(DMSetType(dms, DMSWARM));
 26:   PetscCall(PetscObjectSetName((PetscObject)dms, "Particles"));

 28:   PetscCall(DMSwarmInitializeFieldRegister(dms));
 29:   PetscCall(DMSwarmRegisterPetscDatatypeField(dms, "viscosity", 1, PETSC_REAL));
 30:   PetscCall(DMSwarmRegisterPetscDatatypeField(dms, "strain", 1, PETSC_REAL));
 31:   PetscCall(DMSwarmFinalizeFieldRegister(dms));
 32:   PetscCall(DMSwarmSetLocalSizes(dms, 5 + rank, 4));
 33:   PetscCall(DMView(dms, PETSC_VIEWER_STDOUT_WORLD));

 35:   {
 36:     PetscReal *array;
 37:     PetscCall(DMSwarmGetField(dms, "viscosity", NULL, NULL, (void **)&array));
 38:     for (p = 0; p < 5 + rank; p++) array[p] = 11.1 + p * 0.1 + rank * 100.0;
 39:     PetscCall(DMSwarmRestoreField(dms, "viscosity", NULL, NULL, (void **)&array));
 40:   }

 42:   {
 43:     PetscReal *array;
 44:     PetscCall(DMSwarmGetField(dms, "strain", NULL, NULL, (void **)&array));
 45:     for (p = 0; p < 5 + rank; p++) array[p] = 2.0e-2 + p * 0.001 + rank * 1.0;
 46:     PetscCall(DMSwarmRestoreField(dms, "strain", NULL, NULL, (void **)&array));
 47:   }

 49:   PetscCall(DMSwarmCreateGlobalVectorFromField(dms, "viscosity", &x));
 50:   PetscCall(DMSwarmDestroyGlobalVectorFromField(dms, "viscosity", &x));

 52:   PetscCall(DMSwarmVectorDefineField(dms, "strain"));
 53:   PetscCall(DMCreateGlobalVector(dms, &x));
 54:   PetscCall(VecDestroy(&x));

 56:   {
 57:     PetscInt *rankval;
 58:     PetscInt  npoints[2], npoints_orig[2];

 60:     PetscCall(DMSwarmGetLocalSize(dms, &npoints_orig[0]));
 61:     PetscCall(DMSwarmGetSize(dms, &npoints_orig[1]));
 62:     PetscCall(DMSwarmGetField(dms, DMSwarmField_rank, NULL, NULL, (void **)&rankval));
 63:     if (rank == 0 && size > 1) {
 64:       rankval[0] = 1;
 65:       rankval[3] = 1;
 66:     }
 67:     if (rank == 3) rankval[2] = 1;
 68:     PetscCall(DMSwarmRestoreField(dms, DMSwarmField_rank, NULL, NULL, (void **)&rankval));
 69:     PetscCall(DMSwarmMigrate(dms, PETSC_TRUE));
 70:     PetscCall(DMSwarmGetLocalSize(dms, &npoints[0]));
 71:     PetscCall(DMSwarmGetSize(dms, &npoints[1]));
 72:     PetscCall(PetscViewerASCIIPushSynchronized(PETSC_VIEWER_STDOUT_WORLD));
 73:     PetscCall(PetscViewerASCIISynchronizedPrintf(PETSC_VIEWER_STDOUT_WORLD, "rank[%d] before(%" PetscInt_FMT ",%" PetscInt_FMT ") after(%" PetscInt_FMT ",%" PetscInt_FMT ")\n", rank, npoints_orig[0], npoints_orig[1], npoints[0], npoints[1]));
 74:     PetscCall(PetscViewerFlush(PETSC_VIEWER_STDOUT_WORLD));
 75:     PetscCall(PetscViewerASCIIPopSynchronized(PETSC_VIEWER_STDOUT_WORLD));
 76:   }
 77:   {
 78:     PetscCall(DMSwarmCreateGlobalVectorFromField(dms, "viscosity", &x));
 79:     PetscCall(VecView(x, PETSC_VIEWER_STDOUT_WORLD));
 80:     PetscCall(DMSwarmDestroyGlobalVectorFromField(dms, "viscosity", &x));
 81:   }
 82:   {
 83:     PetscCall(DMSwarmCreateGlobalVectorFromField(dms, "strain", &x));
 84:     PetscCall(VecView(x, PETSC_VIEWER_STDOUT_WORLD));
 85:     PetscCall(DMSwarmDestroyGlobalVectorFromField(dms, "strain", &x));
 86:   }

 88:   PetscCall(DMDestroy(&dms));
 89:   PetscFunctionReturn(PETSC_SUCCESS);
 90: }

 92: PetscErrorCode ex1_2(void)
 93: {
 94:   DM          dms;
 95:   Vec         x;
 96:   PetscMPIInt rank, size;
 97:   PetscInt    p;

 99:   PetscFunctionBegin;
100:   PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD, &size));
101:   PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank));
102:   PetscCall(DMCreate(PETSC_COMM_WORLD, &dms));
103:   PetscCall(DMSetType(dms, DMSWARM));
104:   PetscCall(PetscObjectSetName((PetscObject)dms, "Particles"));
105:   PetscCall(DMSwarmInitializeFieldRegister(dms));

107:   PetscCall(DMSwarmRegisterPetscDatatypeField(dms, "viscosity", 1, PETSC_REAL));
108:   PetscCall(DMSwarmRegisterPetscDatatypeField(dms, "strain", 1, PETSC_REAL));
109:   PetscCall(DMSwarmFinalizeFieldRegister(dms));
110:   PetscCall(DMSwarmSetLocalSizes(dms, 5 + rank, 4));
111:   PetscCall(DMView(dms, PETSC_VIEWER_STDOUT_WORLD));
112:   {
113:     PetscReal *array;
114:     PetscCall(DMSwarmGetField(dms, "viscosity", NULL, NULL, (void **)&array));
115:     for (p = 0; p < 5 + rank; p++) array[p] = 11.1 + p * 0.1 + rank * 100.0;
116:     PetscCall(DMSwarmRestoreField(dms, "viscosity", NULL, NULL, (void **)&array));
117:   }
118:   {
119:     PetscReal *array;
120:     PetscCall(DMSwarmGetField(dms, "strain", NULL, NULL, (void **)&array));
121:     for (p = 0; p < 5 + rank; p++) array[p] = 2.0e-2 + p * 0.001 + rank * 1.0;
122:     PetscCall(DMSwarmRestoreField(dms, "strain", NULL, NULL, (void **)&array));
123:   }
124:   {
125:     PetscInt *rankval;
126:     PetscInt  npoints[2], npoints_orig[2];

128:     PetscCall(DMSwarmGetLocalSize(dms, &npoints_orig[0]));
129:     PetscCall(DMSwarmGetSize(dms, &npoints_orig[1]));
130:     PetscCall(PetscViewerASCIIPushSynchronized(PETSC_VIEWER_STDOUT_WORLD));
131:     PetscCall(PetscViewerASCIISynchronizedPrintf(PETSC_VIEWER_STDOUT_WORLD, "rank[%d] before(%" PetscInt_FMT ",%" PetscInt_FMT ")\n", rank, npoints_orig[0], npoints_orig[1]));
132:     PetscCall(PetscViewerFlush(PETSC_VIEWER_STDOUT_WORLD));
133:     PetscCall(PetscViewerASCIIPopSynchronized(PETSC_VIEWER_STDOUT_WORLD));

135:     PetscCall(DMSwarmGetField(dms, "DMSwarm_rank", NULL, NULL, (void **)&rankval));

137:     if (rank == 1) rankval[0] = -1;
138:     if (rank == 2) rankval[1] = -1;
139:     if (rank == 3) {
140:       rankval[3] = -1;
141:       rankval[4] = -1;
142:     }
143:     PetscCall(DMSwarmRestoreField(dms, "DMSwarm_rank", NULL, NULL, (void **)&rankval));
144:     PetscCall(DMSwarmCollectViewCreate(dms));
145:     PetscCall(DMSwarmGetLocalSize(dms, &npoints[0]));
146:     PetscCall(DMSwarmGetSize(dms, &npoints[1]));
147:     PetscCall(PetscViewerASCIIPushSynchronized(PETSC_VIEWER_STDOUT_WORLD));
148:     PetscCall(PetscViewerASCIISynchronizedPrintf(PETSC_VIEWER_STDOUT_WORLD, "rank[%d] after(%" PetscInt_FMT ",%" PetscInt_FMT ")\n", rank, npoints[0], npoints[1]));
149:     PetscCall(PetscViewerFlush(PETSC_VIEWER_STDOUT_WORLD));
150:     PetscCall(PetscViewerASCIIPopSynchronized(PETSC_VIEWER_STDOUT_WORLD));

152:     PetscCall(DMSwarmCreateGlobalVectorFromField(dms, "viscosity", &x));
153:     PetscCall(VecView(x, PETSC_VIEWER_STDOUT_WORLD));
154:     PetscCall(DMSwarmDestroyGlobalVectorFromField(dms, "viscosity", &x));

156:     PetscCall(DMSwarmCollectViewDestroy(dms));
157:     PetscCall(DMSwarmGetLocalSize(dms, &npoints[0]));
158:     PetscCall(DMSwarmGetSize(dms, &npoints[1]));
159:     PetscCall(PetscViewerASCIIPushSynchronized(PETSC_VIEWER_STDOUT_WORLD));
160:     PetscCall(PetscViewerASCIISynchronizedPrintf(PETSC_VIEWER_STDOUT_WORLD, "rank[%d] after_v(%" PetscInt_FMT ",%" PetscInt_FMT ")\n", rank, npoints[0], npoints[1]));
161:     PetscCall(PetscViewerFlush(PETSC_VIEWER_STDOUT_WORLD));
162:     PetscCall(PetscViewerASCIIPopSynchronized(PETSC_VIEWER_STDOUT_WORLD));

164:     PetscCall(DMSwarmCreateGlobalVectorFromField(dms, "viscosity", &x));
165:     PetscCall(VecView(x, PETSC_VIEWER_STDOUT_WORLD));
166:     PetscCall(DMSwarmDestroyGlobalVectorFromField(dms, "viscosity", &x));
167:   }
168:   PetscCall(DMDestroy(&dms));
169:   PetscFunctionReturn(PETSC_SUCCESS);
170: }

172: /*
173:  splot "c-rank0.gp","c-rank1.gp","c-rank2.gp","c-rank3.gp"
174: */
175: PetscErrorCode ex1_3(void)
176: {
177:   DM          dms;
178:   PetscMPIInt rank, size;
179:   PetscInt    is, js, ni, nj, overlap;
180:   DM          dmcell;

182:   PetscFunctionBegin;
183:   PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD, &size));
184:   PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank));
185:   overlap = 2;
186:   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));
187:   PetscCall(DMSetFromOptions(dmcell));
188:   PetscCall(DMSetUp(dmcell));
189:   PetscCall(DMDASetUniformCoordinates(dmcell, -1.0, 1.0, -1.0, 1.0, -1.0, 1.0));
190:   PetscCall(DMDAGetCorners(dmcell, &is, &js, NULL, &ni, &nj, NULL));
191:   PetscCall(DMCreate(PETSC_COMM_WORLD, &dms));
192:   PetscCall(DMSetType(dms, DMSWARM));
193:   PetscCall(PetscObjectSetName((PetscObject)dms, "Particles"));
194:   PetscCall(DMSwarmSetCellDM(dms, dmcell));

196:   /* load in data types */
197:   PetscCall(DMSwarmInitializeFieldRegister(dms));
198:   PetscCall(DMSwarmRegisterPetscDatatypeField(dms, "viscosity", 1, PETSC_REAL));
199:   PetscCall(DMSwarmRegisterPetscDatatypeField(dms, "coorx", 1, PETSC_REAL));
200:   PetscCall(DMSwarmRegisterPetscDatatypeField(dms, "coory", 1, PETSC_REAL));
201:   PetscCall(DMSwarmFinalizeFieldRegister(dms));
202:   PetscCall(DMSwarmSetLocalSizes(dms, ni * nj * 4, 4));
203:   PetscCall(DMView(dms, PETSC_VIEWER_STDOUT_WORLD));

205:   /* set values within the swarm */
206:   {
207:     PetscReal   *array_x, *array_y;
208:     PetscInt     npoints, i, j, cnt;
209:     DMDACoor2d **LA_coor;
210:     Vec          coor;
211:     DM           dmcellcdm;

213:     PetscCall(DMGetCoordinateDM(dmcell, &dmcellcdm));
214:     PetscCall(DMGetCoordinates(dmcell, &coor));
215:     PetscCall(DMDAVecGetArray(dmcellcdm, coor, &LA_coor));
216:     PetscCall(DMSwarmGetLocalSize(dms, &npoints));
217:     PetscCall(DMSwarmGetField(dms, "coorx", NULL, NULL, (void **)&array_x));
218:     PetscCall(DMSwarmGetField(dms, "coory", NULL, NULL, (void **)&array_y));
219:     cnt = 0;
220:     for (j = js; j < js + nj; j++) {
221:       for (i = is; i < is + ni; i++) {
222:         PetscReal xp, yp;
223:         xp                   = PetscRealPart(LA_coor[j][i].x);
224:         yp                   = PetscRealPart(LA_coor[j][i].y);
225:         array_x[4 * cnt + 0] = xp - 0.05;
226:         if (array_x[4 * cnt + 0] < -1.0) array_x[4 * cnt + 0] = -1.0 + 1.0e-12;
227:         array_x[4 * cnt + 1] = xp + 0.05;
228:         if (array_x[4 * cnt + 1] > 1.0) array_x[4 * cnt + 1] = 1.0 - 1.0e-12;
229:         array_x[4 * cnt + 2] = xp - 0.05;
230:         if (array_x[4 * cnt + 2] < -1.0) array_x[4 * cnt + 2] = -1.0 + 1.0e-12;
231:         array_x[4 * cnt + 3] = xp + 0.05;
232:         if (array_x[4 * cnt + 3] > 1.0) array_x[4 * cnt + 3] = 1.0 - 1.0e-12;

234:         array_y[4 * cnt + 0] = yp - 0.05;
235:         if (array_y[4 * cnt + 0] < -1.0) array_y[4 * cnt + 0] = -1.0 + 1.0e-12;
236:         array_y[4 * cnt + 1] = yp - 0.05;
237:         if (array_y[4 * cnt + 1] < -1.0) array_y[4 * cnt + 1] = -1.0 + 1.0e-12;
238:         array_y[4 * cnt + 2] = yp + 0.05;
239:         if (array_y[4 * cnt + 2] > 1.0) array_y[4 * cnt + 2] = 1.0 - 1.0e-12;
240:         array_y[4 * cnt + 3] = yp + 0.05;
241:         if (array_y[4 * cnt + 3] > 1.0) array_y[4 * cnt + 3] = 1.0 - 1.0e-12;
242:         cnt++;
243:       }
244:     }
245:     PetscCall(DMSwarmRestoreField(dms, "coory", NULL, NULL, (void **)&array_y));
246:     PetscCall(DMSwarmRestoreField(dms, "coorx", NULL, NULL, (void **)&array_x));
247:     PetscCall(DMDAVecRestoreArray(dmcellcdm, coor, &LA_coor));
248:   }
249:   {
250:     PetscInt npoints[2], npoints_orig[2], ng;

252:     PetscCall(DMSwarmGetLocalSize(dms, &npoints_orig[0]));
253:     PetscCall(DMSwarmGetSize(dms, &npoints_orig[1]));
254:     PetscCall(PetscViewerASCIIPushSynchronized(PETSC_VIEWER_STDOUT_WORLD));
255:     PetscCall(PetscViewerASCIISynchronizedPrintf(PETSC_VIEWER_STDOUT_WORLD, "rank[%d] before(%" PetscInt_FMT ",%" PetscInt_FMT ")\n", rank, npoints_orig[0], npoints_orig[1]));
256:     PetscCall(PetscViewerFlush(PETSC_VIEWER_STDOUT_WORLD));
257:     PetscCall(PetscViewerASCIIPopSynchronized(PETSC_VIEWER_STDOUT_WORLD));
258:     PetscCall(DMSwarmCollect_DMDABoundingBox(dms, &ng));

260:     PetscCall(DMSwarmGetLocalSize(dms, &npoints[0]));
261:     PetscCall(DMSwarmGetSize(dms, &npoints[1]));
262:     PetscCall(PetscViewerASCIIPushSynchronized(PETSC_VIEWER_STDOUT_WORLD));
263:     PetscCall(PetscViewerASCIISynchronizedPrintf(PETSC_VIEWER_STDOUT_WORLD, "rank[%d] after(%" PetscInt_FMT ",%" PetscInt_FMT ")\n", rank, npoints[0], npoints[1]));
264:     PetscCall(PetscViewerFlush(PETSC_VIEWER_STDOUT_WORLD));
265:     PetscCall(PetscViewerASCIIPopSynchronized(PETSC_VIEWER_STDOUT_WORLD));
266:   }
267:   {
268:     PetscReal *array_x, *array_y;
269:     PetscInt   npoints;
270:     FILE      *fp = NULL;
271:     char       name[PETSC_MAX_PATH_LEN];

273:     PetscCall(PetscSNPrintf(name, PETSC_MAX_PATH_LEN - 1, "c-rank%d.gp", rank));
274:     fp = fopen(name, "w");
275:     PetscCheck(fp, PETSC_COMM_SELF, PETSC_ERR_FILE_OPEN, "Cannot open file %s", name);
276:     PetscCall(DMSwarmGetLocalSize(dms, &npoints));
277:     PetscCall(DMSwarmGetField(dms, "coorx", NULL, NULL, (void **)&array_x));
278:     PetscCall(DMSwarmGetField(dms, "coory", NULL, NULL, (void **)&array_y));
279:     for (PetscInt p = 0; p < npoints; p++) fprintf(fp, "%+1.4e %+1.4e %1.4e\n", array_x[p], array_y[p], (double)rank);
280:     PetscCall(DMSwarmRestoreField(dms, "coory", NULL, NULL, (void **)&array_y));
281:     PetscCall(DMSwarmRestoreField(dms, "coorx", NULL, NULL, (void **)&array_x));
282:     fclose(fp);
283:   }
284:   PetscCall(DMDestroy(&dmcell));
285:   PetscCall(DMDestroy(&dms));
286:   PetscFunctionReturn(PETSC_SUCCESS);
287: }

289: typedef struct {
290:   PetscReal cx[2];
291:   PetscReal radius;
292: } CollectZoneCtx;

294: PetscErrorCode collect_zone(DM dm, PetscCtx ctx, PetscInt *nfound, PetscInt **foundlist)
295: {
296:   CollectZoneCtx *zone = (CollectZoneCtx *)ctx;
297:   PetscInt        npoints;
298:   PetscReal      *array_x, *array_y, r2;
299:   PetscInt        p2collect, *plist;
300:   PetscMPIInt     rank;

302:   PetscFunctionBegin;
303:   PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank));
304:   PetscCall(DMSwarmGetLocalSize(dm, &npoints));
305:   PetscCall(DMSwarmGetField(dm, "coorx", NULL, NULL, (void **)&array_x));
306:   PetscCall(DMSwarmGetField(dm, "coory", NULL, NULL, (void **)&array_y));

308:   r2        = zone->radius * zone->radius;
309:   p2collect = 0;
310:   for (PetscInt p = 0; p < npoints; p++) {
311:     PetscReal sep2;

313:     sep2 = (array_x[p] - zone->cx[0]) * (array_x[p] - zone->cx[0]);
314:     sep2 += (array_y[p] - zone->cx[1]) * (array_y[p] - zone->cx[1]);
315:     if (sep2 < r2) p2collect++;
316:   }

318:   PetscCall(PetscMalloc1(p2collect + 1, &plist));
319:   p2collect = 0;
320:   for (PetscInt p = 0; p < npoints; p++) {
321:     PetscReal sep2;

323:     sep2 = (array_x[p] - zone->cx[0]) * (array_x[p] - zone->cx[0]);
324:     sep2 += (array_y[p] - zone->cx[1]) * (array_y[p] - zone->cx[1]);
325:     if (sep2 < r2) {
326:       plist[p2collect] = p;
327:       p2collect++;
328:     }
329:   }
330:   PetscCall(DMSwarmRestoreField(dm, "coory", NULL, NULL, (void **)&array_y));
331:   PetscCall(DMSwarmRestoreField(dm, "coorx", NULL, NULL, (void **)&array_x));

333:   *nfound    = p2collect;
334:   *foundlist = plist;
335:   PetscFunctionReturn(PETSC_SUCCESS);
336: }

338: PetscErrorCode ex1_4(void)
339: {
340:   DM              dms;
341:   PetscMPIInt     rank, size;
342:   PetscInt        is, js, ni, nj, overlap, nn;
343:   DM              dmcell;
344:   CollectZoneCtx *zone;
345:   PetscReal       dx;

347:   PetscFunctionBegin;
348:   PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD, &size));
349:   PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank));
350:   nn      = 101;
351:   dx      = 2.0 / (PetscReal)(nn - 1);
352:   overlap = 0;
353:   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));
354:   PetscCall(DMSetFromOptions(dmcell));
355:   PetscCall(DMSetUp(dmcell));
356:   PetscCall(DMDASetUniformCoordinates(dmcell, -1.0, 1.0, -1.0, 1.0, -1.0, 1.0));
357:   PetscCall(DMDAGetCorners(dmcell, &is, &js, NULL, &ni, &nj, NULL));
358:   PetscCall(DMCreate(PETSC_COMM_WORLD, &dms));
359:   PetscCall(DMSetType(dms, DMSWARM));
360:   PetscCall(PetscObjectSetName((PetscObject)dms, "Particles"));

362:   /* load in data types */
363:   PetscCall(DMSwarmInitializeFieldRegister(dms));
364:   PetscCall(DMSwarmRegisterPetscDatatypeField(dms, "viscosity", 1, PETSC_REAL));
365:   PetscCall(DMSwarmRegisterPetscDatatypeField(dms, "coorx", 1, PETSC_REAL));
366:   PetscCall(DMSwarmRegisterPetscDatatypeField(dms, "coory", 1, PETSC_REAL));
367:   PetscCall(DMSwarmFinalizeFieldRegister(dms));
368:   PetscCall(DMSwarmSetLocalSizes(dms, ni * nj * 4, 4));
369:   PetscCall(DMView(dms, PETSC_VIEWER_STDOUT_WORLD));

371:   /* set values within the swarm */
372:   {
373:     PetscReal   *array_x, *array_y;
374:     PetscInt     npoints, i, j, cnt;
375:     DMDACoor2d **LA_coor;
376:     Vec          coor;
377:     DM           dmcellcdm;

379:     PetscCall(DMGetCoordinateDM(dmcell, &dmcellcdm));
380:     PetscCall(DMGetCoordinates(dmcell, &coor));
381:     PetscCall(DMDAVecGetArray(dmcellcdm, coor, &LA_coor));
382:     PetscCall(DMSwarmGetLocalSize(dms, &npoints));
383:     PetscCall(DMSwarmGetField(dms, "coorx", NULL, NULL, (void **)&array_x));
384:     PetscCall(DMSwarmGetField(dms, "coory", NULL, NULL, (void **)&array_y));
385:     cnt = 0;
386:     for (j = js; j < js + nj; j++) {
387:       for (i = is; i < is + ni; i++) {
388:         PetscReal xp, yp;

390:         xp                   = PetscRealPart(LA_coor[j][i].x);
391:         yp                   = PetscRealPart(LA_coor[j][i].y);
392:         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;*/
393:         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; }*/
394:         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;*/
395:         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; }*/
396:         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;*/
397:         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;*/
398:         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; }*/
399:         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; }*/
400:         cnt++;
401:       }
402:     }
403:     PetscCall(DMSwarmRestoreField(dms, "coory", NULL, NULL, (void **)&array_y));
404:     PetscCall(DMSwarmRestoreField(dms, "coorx", NULL, NULL, (void **)&array_x));
405:     PetscCall(DMDAVecRestoreArray(dmcellcdm, coor, &LA_coor));
406:   }
407:   PetscCall(PetscMalloc1(1, &zone));
408:   if (size == 4) {
409:     if (rank == 0) {
410:       zone->cx[0]  = 0.5;
411:       zone->cx[1]  = 0.5;
412:       zone->radius = 0.3;
413:     }
414:     if (rank == 1) {
415:       zone->cx[0]  = -0.5;
416:       zone->cx[1]  = 0.5;
417:       zone->radius = 0.25;
418:     }
419:     if (rank == 2) {
420:       zone->cx[0]  = 0.5;
421:       zone->cx[1]  = -0.5;
422:       zone->radius = 0.2;
423:     }
424:     if (rank == 3) {
425:       zone->cx[0]  = -0.5;
426:       zone->cx[1]  = -0.5;
427:       zone->radius = 0.1;
428:     }
429:   } else {
430:     if (rank == 0) {
431:       zone->cx[0]  = 0.5;
432:       zone->cx[1]  = 0.5;
433:       zone->radius = 0.8;
434:     } else {
435:       zone->cx[0]  = 10.0;
436:       zone->cx[1]  = 10.0;
437:       zone->radius = 0.0;
438:     }
439:   }
440:   {
441:     PetscInt npoints[2], npoints_orig[2], ng;

443:     PetscCall(DMSwarmGetLocalSize(dms, &npoints_orig[0]));
444:     PetscCall(DMSwarmGetSize(dms, &npoints_orig[1]));
445:     PetscCall(PetscViewerASCIIPushSynchronized(PETSC_VIEWER_STDOUT_WORLD));
446:     PetscCall(PetscViewerASCIISynchronizedPrintf(PETSC_VIEWER_STDOUT_WORLD, "rank[%d] before(%" PetscInt_FMT ",%" PetscInt_FMT ")\n", rank, npoints_orig[0], npoints_orig[1]));
447:     PetscCall(PetscViewerFlush(PETSC_VIEWER_STDOUT_WORLD));
448:     PetscCall(PetscViewerASCIIPopSynchronized(PETSC_VIEWER_STDOUT_WORLD));
449:     PetscCall(DMSwarmCollect_General(dms, collect_zone, sizeof(CollectZoneCtx), zone, &ng));
450:     PetscCall(DMSwarmGetLocalSize(dms, &npoints[0]));
451:     PetscCall(DMSwarmGetSize(dms, &npoints[1]));
452:     PetscCall(PetscViewerASCIIPushSynchronized(PETSC_VIEWER_STDOUT_WORLD));
453:     PetscCall(PetscViewerASCIISynchronizedPrintf(PETSC_VIEWER_STDOUT_WORLD, "rank[%d] after(%" PetscInt_FMT ",%" PetscInt_FMT ")\n", rank, npoints[0], npoints[1]));
454:     PetscCall(PetscViewerFlush(PETSC_VIEWER_STDOUT_WORLD));
455:     PetscCall(PetscViewerASCIIPopSynchronized(PETSC_VIEWER_STDOUT_WORLD));
456:   }
457:   {
458:     PetscReal *array_x, *array_y;
459:     PetscInt   npoints;
460:     FILE      *fp = NULL;
461:     char       name[PETSC_MAX_PATH_LEN];

463:     PetscCall(PetscSNPrintf(name, PETSC_MAX_PATH_LEN - 1, "c-rank%d.gp", rank));
464:     fp = fopen(name, "w");
465:     PetscCheck(fp, PETSC_COMM_SELF, PETSC_ERR_FILE_OPEN, "Cannot open file %s", name);
466:     PetscCall(DMSwarmGetLocalSize(dms, &npoints));
467:     PetscCall(DMSwarmGetField(dms, "coorx", NULL, NULL, (void **)&array_x));
468:     PetscCall(DMSwarmGetField(dms, "coory", NULL, NULL, (void **)&array_y));
469:     for (PetscInt p = 0; p < npoints; p++) fprintf(fp, "%+1.4e %+1.4e %1.4e\n", array_x[p], array_y[p], (double)rank);
470:     PetscCall(DMSwarmRestoreField(dms, "coory", NULL, NULL, (void **)&array_y));
471:     PetscCall(DMSwarmRestoreField(dms, "coorx", NULL, NULL, (void **)&array_x));
472:     fclose(fp);
473:   }
474:   PetscCall(DMDestroy(&dmcell));
475:   PetscCall(DMDestroy(&dms));
476:   PetscCall(PetscFree(zone));
477:   PetscFunctionReturn(PETSC_SUCCESS);
478: }

480: int main(int argc, char **argv)
481: {
482:   PetscInt test_mode = 4;

484:   PetscFunctionBeginUser;
485:   PetscCall(PetscInitialize(&argc, &argv, NULL, help));
486:   PetscCall(PetscOptionsGetInt(NULL, NULL, "-test_mode", &test_mode, NULL));
487:   if (test_mode == 1) PetscCall(ex1_1());
488:   else if (test_mode == 2) PetscCall(ex1_2());
489:   else if (test_mode == 3) PetscCall(ex1_3());
490:   else {
491:     PetscCheck(test_mode == 4, PETSC_COMM_SELF, PETSC_ERR_USER, "Unknown test_mode value, should be 1,2,3,4");
492:     PetscCall(ex1_4());
493:   }
494:   PetscCall(PetscFinalize());
495:   return 0;
496: }

498: /*TEST

500:    build:
501:       requires: !complex double

503:    test:
504:       args: -test_mode 1
505:       filter: grep -v atomic
506:       filter_output: grep -v atomic

508:    test:
509:       suffix: 2
510:       args: -test_mode 2
511:       filter: grep -v atomic
512:       filter_output: grep -v atomic

514:    test:
515:       suffix: 3
516:       args: -test_mode 3
517:       filter: grep -v atomic
518:       filter_output: grep -v atomic
519:       TODO: broken

521:    test:
522:       suffix: 4
523:       args: -test_mode 4
524:       filter: grep -v atomic
525:       filter_output: grep -v atomic

527:    test:
528:       suffix: 5
529:       nsize: 4
530:       args: -test_mode 1
531:       filter: grep -v atomic
532:       filter_output: grep -v atomic

534:    test:
535:       suffix: 6
536:       nsize: 4
537:       args: -test_mode 2
538:       filter: grep -v atomic
539:       filter_output: grep -v atomic

541:    test:
542:       suffix: 7
543:       nsize: 4
544:       args: -test_mode 3
545:       filter: grep -v atomic
546:       filter_output: grep -v atomic
547:       TODO: broken

549:    test:
550:       suffix: 8
551:       nsize: 4
552:       args: -test_mode 4
553:       filter: grep -v atomic
554:       filter_output: grep -v atomic

556: TEST*/