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 CHKERRMPI(MPI_Comm_size(PETSC_COMM_WORLD,&size)); 20 CHKERRMPI(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 CHKERRQ(DMCreate(PETSC_COMM_WORLD,&dms)); 24 CHKERRQ(DMSetType(dms,DMSWARM)); 25 26 CHKERRQ(DMSwarmInitializeFieldRegister(dms)); 27 CHKERRQ(DMSwarmRegisterPetscDatatypeField(dms,"viscosity",1,PETSC_REAL)); 28 CHKERRQ(DMSwarmRegisterPetscDatatypeField(dms,"strain",1,PETSC_REAL)); 29 CHKERRQ(DMSwarmFinalizeFieldRegister(dms)); 30 CHKERRQ(DMSwarmSetLocalSizes(dms,5+rank,4)); 31 CHKERRQ(DMView(dms,PETSC_VIEWER_STDOUT_WORLD)); 32 33 { 34 PetscReal *array; 35 CHKERRQ(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 CHKERRQ(DMSwarmRestoreField(dms,"viscosity",NULL,NULL,(void**)&array)); 40 } 41 42 { 43 PetscReal *array; 44 CHKERRQ(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 CHKERRQ(DMSwarmRestoreField(dms,"strain",NULL,NULL,(void**)&array)); 49 } 50 51 CHKERRQ(DMSwarmCreateGlobalVectorFromField(dms,"viscosity",&x)); 52 CHKERRQ(DMSwarmDestroyGlobalVectorFromField(dms,"viscosity",&x)); 53 54 CHKERRQ(DMSwarmVectorDefineField(dms,"strain")); 55 CHKERRQ(DMCreateGlobalVector(dms,&x)); 56 CHKERRQ(VecDestroy(&x)); 57 58 { 59 PetscInt *rankval; 60 PetscInt npoints[2],npoints_orig[2]; 61 62 CHKERRQ(DMSwarmGetLocalSize(dms,&npoints_orig[0])); 63 CHKERRQ(DMSwarmGetSize(dms,&npoints_orig[1])); 64 CHKERRQ(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 CHKERRQ(DMSwarmRestoreField(dms,"DMSwarm_rank",NULL,NULL,(void**)&rankval)); 73 CHKERRQ(DMSwarmMigrate(dms,PETSC_TRUE)); 74 CHKERRQ(DMSwarmGetLocalSize(dms,&npoints[0])); 75 CHKERRQ(DMSwarmGetSize(dms,&npoints[1])); 76 CHKERRQ(PetscViewerASCIIPushSynchronized(PETSC_VIEWER_STDOUT_WORLD)); 77 CHKERRQ(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 CHKERRQ(PetscViewerFlush(PETSC_VIEWER_STDOUT_WORLD)); 79 CHKERRQ(PetscViewerASCIIPopSynchronized(PETSC_VIEWER_STDOUT_WORLD)); 80 } 81 { 82 CHKERRQ(DMSwarmCreateGlobalVectorFromField(dms,"viscosity",&x)); 83 CHKERRQ(VecView(x,PETSC_VIEWER_STDOUT_WORLD)); 84 CHKERRQ(DMSwarmDestroyGlobalVectorFromField(dms,"viscosity",&x)); 85 } 86 { 87 CHKERRQ(DMSwarmCreateGlobalVectorFromField(dms,"strain",&x)); 88 CHKERRQ(VecView(x,PETSC_VIEWER_STDOUT_WORLD)); 89 CHKERRQ(DMSwarmDestroyGlobalVectorFromField(dms,"strain",&x)); 90 } 91 92 CHKERRQ(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 CHKERRMPI(MPI_Comm_size(PETSC_COMM_WORLD,&size)); 105 CHKERRMPI(MPI_Comm_rank(PETSC_COMM_WORLD,&rank)); 106 CHKERRQ(DMCreate(PETSC_COMM_WORLD,&dms)); 107 CHKERRQ(DMSetType(dms,DMSWARM)); 108 CHKERRQ(DMSwarmInitializeFieldRegister(dms)); 109 110 CHKERRQ(DMSwarmRegisterPetscDatatypeField(dms,"viscosity",1,PETSC_REAL)); 111 CHKERRQ(DMSwarmRegisterPetscDatatypeField(dms,"strain",1,PETSC_REAL)); 112 CHKERRQ(DMSwarmFinalizeFieldRegister(dms)); 113 CHKERRQ(DMSwarmSetLocalSizes(dms,5+rank,4)); 114 CHKERRQ(DMView(dms,PETSC_VIEWER_STDOUT_WORLD)); 115 { 116 PetscReal *array; 117 CHKERRQ(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 CHKERRQ(DMSwarmRestoreField(dms,"viscosity",NULL,NULL,(void**)&array)); 122 } 123 { 124 PetscReal *array; 125 CHKERRQ(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 CHKERRQ(DMSwarmRestoreField(dms,"strain",NULL,NULL,(void**)&array)); 130 } 131 { 132 PetscInt *rankval; 133 PetscInt npoints[2],npoints_orig[2]; 134 135 CHKERRQ(DMSwarmGetLocalSize(dms,&npoints_orig[0])); 136 CHKERRQ(DMSwarmGetSize(dms,&npoints_orig[1])); 137 CHKERRQ(PetscViewerASCIIPushSynchronized(PETSC_VIEWER_STDOUT_WORLD)); 138 CHKERRQ(PetscViewerASCIISynchronizedPrintf(PETSC_VIEWER_STDOUT_WORLD,"rank[%d] before(%D,%D)\n",rank,npoints_orig[0],npoints_orig[1])); 139 CHKERRQ(PetscViewerFlush(PETSC_VIEWER_STDOUT_WORLD)); 140 CHKERRQ(PetscViewerASCIIPopSynchronized(PETSC_VIEWER_STDOUT_WORLD)); 141 142 CHKERRQ(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 CHKERRQ(DMSwarmRestoreField(dms,"DMSwarm_rank",NULL,NULL,(void**)&rankval)); 155 CHKERRQ(DMSwarmCollectViewCreate(dms)); 156 CHKERRQ(DMSwarmGetLocalSize(dms,&npoints[0])); 157 CHKERRQ(DMSwarmGetSize(dms,&npoints[1])); 158 CHKERRQ(PetscViewerASCIIPushSynchronized(PETSC_VIEWER_STDOUT_WORLD)); 159 CHKERRQ(PetscViewerASCIISynchronizedPrintf(PETSC_VIEWER_STDOUT_WORLD,"rank[%d] after(%D,%D)\n",rank,npoints[0],npoints[1])); 160 CHKERRQ(PetscViewerFlush(PETSC_VIEWER_STDOUT_WORLD)); 161 CHKERRQ(PetscViewerASCIIPopSynchronized(PETSC_VIEWER_STDOUT_WORLD)); 162 163 CHKERRQ(DMSwarmCreateGlobalVectorFromField(dms,"viscosity",&x)); 164 CHKERRQ(VecView(x,PETSC_VIEWER_STDOUT_WORLD)); 165 CHKERRQ(DMSwarmDestroyGlobalVectorFromField(dms,"viscosity",&x)); 166 167 CHKERRQ(DMSwarmCollectViewDestroy(dms)); 168 CHKERRQ(DMSwarmGetLocalSize(dms,&npoints[0])); 169 CHKERRQ(DMSwarmGetSize(dms,&npoints[1])); 170 CHKERRQ(PetscViewerASCIIPushSynchronized(PETSC_VIEWER_STDOUT_WORLD)); 171 CHKERRQ(PetscViewerASCIISynchronizedPrintf(PETSC_VIEWER_STDOUT_WORLD,"rank[%d] after_v(%D,%D)\n",rank,npoints[0],npoints[1])); 172 CHKERRQ(PetscViewerFlush(PETSC_VIEWER_STDOUT_WORLD)); 173 CHKERRQ(PetscViewerASCIIPopSynchronized(PETSC_VIEWER_STDOUT_WORLD)); 174 175 CHKERRQ(DMSwarmCreateGlobalVectorFromField(dms,"viscosity",&x)); 176 CHKERRQ(VecView(x,PETSC_VIEWER_STDOUT_WORLD)); 177 CHKERRQ(DMSwarmDestroyGlobalVectorFromField(dms,"viscosity",&x)); 178 } 179 CHKERRQ(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 CHKERRMPI(MPI_Comm_size(PETSC_COMM_WORLD,&size)); 195 CHKERRMPI(MPI_Comm_rank(PETSC_COMM_WORLD,&rank)); 196 overlap = 2; 197 CHKERRQ(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 CHKERRQ(DMSetFromOptions(dmcell)); 199 CHKERRQ(DMSetUp(dmcell)); 200 CHKERRQ(DMDASetUniformCoordinates(dmcell,-1.0,1.0,-1.0,1.0,-1.0,1.0)); 201 CHKERRQ(DMDAGetCorners(dmcell,&is,&js,NULL,&ni,&nj,NULL)); 202 CHKERRQ(DMCreate(PETSC_COMM_WORLD,&dms)); 203 CHKERRQ(DMSetType(dms,DMSWARM)); 204 CHKERRQ(DMSwarmSetCellDM(dms,dmcell)); 205 206 /* load in data types */ 207 CHKERRQ(DMSwarmInitializeFieldRegister(dms)); 208 CHKERRQ(DMSwarmRegisterPetscDatatypeField(dms,"viscosity",1,PETSC_REAL)); 209 CHKERRQ(DMSwarmRegisterPetscDatatypeField(dms,"coorx",1,PETSC_REAL)); 210 CHKERRQ(DMSwarmRegisterPetscDatatypeField(dms,"coory",1,PETSC_REAL)); 211 CHKERRQ(DMSwarmFinalizeFieldRegister(dms)); 212 CHKERRQ(DMSwarmSetLocalSizes(dms,ni*nj*4,4)); 213 CHKERRQ(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 CHKERRQ(DMGetCoordinateDM(dmcell,&dmcellcdm)); 224 CHKERRQ(DMGetCoordinates(dmcell,&coor)); 225 CHKERRQ(DMDAVecGetArray(dmcellcdm,coor,&LA_coor)); 226 CHKERRQ(DMSwarmGetLocalSize(dms,&npoints)); 227 CHKERRQ(DMSwarmGetField(dms,"coorx",NULL,NULL,(void**)&array_x)); 228 CHKERRQ(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 CHKERRQ(DMSwarmRestoreField(dms,"coory",NULL,NULL,(void**)&array_y)); 248 CHKERRQ(DMSwarmRestoreField(dms,"coorx",NULL,NULL,(void**)&array_x)); 249 CHKERRQ(DMDAVecRestoreArray(dmcellcdm,coor,&LA_coor)); 250 } 251 { 252 PetscInt npoints[2],npoints_orig[2],ng; 253 254 CHKERRQ(DMSwarmGetLocalSize(dms,&npoints_orig[0])); 255 CHKERRQ(DMSwarmGetSize(dms,&npoints_orig[1])); 256 CHKERRQ(PetscViewerASCIIPushSynchronized(PETSC_VIEWER_STDOUT_WORLD)); 257 CHKERRQ(PetscViewerASCIISynchronizedPrintf(PETSC_VIEWER_STDOUT_WORLD,"rank[%d] before(%D,%D)\n",rank,npoints_orig[0],npoints_orig[1])); 258 CHKERRQ(PetscViewerFlush(PETSC_VIEWER_STDOUT_WORLD)); 259 CHKERRQ(PetscViewerASCIIPopSynchronized(PETSC_VIEWER_STDOUT_WORLD)); 260 CHKERRQ(DMSwarmCollect_DMDABoundingBox(dms,&ng)); 261 262 CHKERRQ(DMSwarmGetLocalSize(dms,&npoints[0])); 263 CHKERRQ(DMSwarmGetSize(dms,&npoints[1])); 264 CHKERRQ(PetscViewerASCIIPushSynchronized(PETSC_VIEWER_STDOUT_WORLD)); 265 CHKERRQ(PetscViewerASCIISynchronizedPrintf(PETSC_VIEWER_STDOUT_WORLD,"rank[%d] after(%D,%D)\n",rank,npoints[0],npoints[1])); 266 CHKERRQ(PetscViewerFlush(PETSC_VIEWER_STDOUT_WORLD)); 267 CHKERRQ(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 CHKERRQ(PetscSNPrintf(name,PETSC_MAX_PATH_LEN-1,"c-rank%d.gp",rank)); 276 fp = fopen(name,"w"); 277 PetscCheckFalse(!fp,PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Cannot open file %s",name); 278 CHKERRQ(DMSwarmGetLocalSize(dms,&npoints)); 279 CHKERRQ(DMSwarmGetField(dms,"coorx",NULL,NULL,(void**)&array_x)); 280 CHKERRQ(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 CHKERRQ(DMSwarmRestoreField(dms,"coory",NULL,NULL,(void**)&array_y)); 285 CHKERRQ(DMSwarmRestoreField(dms,"coorx",NULL,NULL,(void**)&array_x)); 286 fclose(fp); 287 } 288 CHKERRQ(DMDestroy(&dmcell)); 289 CHKERRQ(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 CHKERRMPI(MPI_Comm_rank(PETSC_COMM_WORLD,&rank)); 308 CHKERRQ(DMSwarmGetLocalSize(dm,&npoints)); 309 CHKERRQ(DMSwarmGetField(dm,"coorx",NULL,NULL,(void**)&array_x)); 310 CHKERRQ(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 CHKERRQ(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 CHKERRQ(DMSwarmRestoreField(dm,"coory",NULL,NULL,(void**)&array_y)); 337 CHKERRQ(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 CHKERRMPI(MPI_Comm_size(PETSC_COMM_WORLD,&size)); 355 CHKERRMPI(MPI_Comm_rank(PETSC_COMM_WORLD,&rank)); 356 nn = 101; 357 dx = 2.0/ (PetscReal)(nn-1); 358 overlap = 0; 359 CHKERRQ(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 CHKERRQ(DMSetFromOptions(dmcell)); 361 CHKERRQ(DMSetUp(dmcell)); 362 CHKERRQ(DMDASetUniformCoordinates(dmcell,-1.0,1.0,-1.0,1.0,-1.0,1.0)); 363 CHKERRQ(DMDAGetCorners(dmcell,&is,&js,NULL,&ni,&nj,NULL)); 364 CHKERRQ(DMCreate(PETSC_COMM_WORLD,&dms)); 365 CHKERRQ(DMSetType(dms,DMSWARM)); 366 367 /* load in data types */ 368 CHKERRQ(DMSwarmInitializeFieldRegister(dms)); 369 CHKERRQ(DMSwarmRegisterPetscDatatypeField(dms,"viscosity",1,PETSC_REAL)); 370 CHKERRQ(DMSwarmRegisterPetscDatatypeField(dms,"coorx",1,PETSC_REAL)); 371 CHKERRQ(DMSwarmRegisterPetscDatatypeField(dms,"coory",1,PETSC_REAL)); 372 CHKERRQ(DMSwarmFinalizeFieldRegister(dms)); 373 CHKERRQ(DMSwarmSetLocalSizes(dms,ni*nj*4,4)); 374 CHKERRQ(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 CHKERRQ(DMGetCoordinateDM(dmcell,&dmcellcdm)); 385 CHKERRQ(DMGetCoordinates(dmcell,&coor)); 386 CHKERRQ(DMDAVecGetArray(dmcellcdm,coor,&LA_coor)); 387 CHKERRQ(DMSwarmGetLocalSize(dms,&npoints)); 388 CHKERRQ(DMSwarmGetField(dms,"coorx",NULL,NULL,(void**)&array_x)); 389 CHKERRQ(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 CHKERRQ(DMSwarmRestoreField(dms,"coory",NULL,NULL,(void**)&array_y)); 409 CHKERRQ(DMSwarmRestoreField(dms,"coorx",NULL,NULL,(void**)&array_x)); 410 CHKERRQ(DMDAVecRestoreArray(dmcellcdm,coor,&LA_coor)); 411 } 412 CHKERRQ(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 CHKERRQ(DMSwarmGetLocalSize(dms,&npoints_orig[0])); 449 CHKERRQ(DMSwarmGetSize(dms,&npoints_orig[1])); 450 CHKERRQ(PetscViewerASCIIPushSynchronized(PETSC_VIEWER_STDOUT_WORLD)); 451 CHKERRQ(PetscViewerASCIISynchronizedPrintf(PETSC_VIEWER_STDOUT_WORLD,"rank[%d] before(%D,%D)\n",rank,npoints_orig[0],npoints_orig[1])); 452 CHKERRQ(PetscViewerFlush(PETSC_VIEWER_STDOUT_WORLD)); 453 CHKERRQ(PetscViewerASCIIPopSynchronized(PETSC_VIEWER_STDOUT_WORLD)); 454 CHKERRQ(DMSwarmCollect_General(dms,collect_zone,sizeof(CollectZoneCtx),zone,&ng)); 455 CHKERRQ(DMSwarmGetLocalSize(dms,&npoints[0])); 456 CHKERRQ(DMSwarmGetSize(dms,&npoints[1])); 457 CHKERRQ(PetscViewerASCIIPushSynchronized(PETSC_VIEWER_STDOUT_WORLD)); 458 CHKERRQ(PetscViewerASCIISynchronizedPrintf(PETSC_VIEWER_STDOUT_WORLD,"rank[%d] after(%D,%D)\n",rank,npoints[0],npoints[1])); 459 CHKERRQ(PetscViewerFlush(PETSC_VIEWER_STDOUT_WORLD)); 460 CHKERRQ(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 CHKERRQ(PetscSNPrintf(name,PETSC_MAX_PATH_LEN-1,"c-rank%d.gp",rank)); 469 fp = fopen(name,"w"); 470 PetscCheckFalse(!fp,PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Cannot open file %s",name); 471 CHKERRQ(DMSwarmGetLocalSize(dms,&npoints)); 472 CHKERRQ(DMSwarmGetField(dms,"coorx",NULL,NULL,(void**)&array_x)); 473 CHKERRQ(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 CHKERRQ(DMSwarmRestoreField(dms,"coory",NULL,NULL,(void**)&array_y)); 478 CHKERRQ(DMSwarmRestoreField(dms,"coorx",NULL,NULL,(void**)&array_x)); 479 fclose(fp); 480 } 481 CHKERRQ(DMDestroy(&dmcell)); 482 CHKERRQ(DMDestroy(&dms)); 483 CHKERRQ(PetscFree(zone)); 484 PetscFunctionReturn(0); 485 } 486 487 int main(int argc,char **argv) 488 { 489 PetscErrorCode ierr; 490 PetscInt test_mode = 4; 491 492 ierr = PetscInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr; 493 CHKERRQ(PetscOptionsGetInt(NULL,NULL,"-test_mode",&test_mode,NULL)); 494 if (test_mode == 1) { 495 CHKERRQ(ex1_1()); 496 } else if (test_mode == 2) { 497 CHKERRQ(ex1_2()); 498 } else if (test_mode == 3) { 499 CHKERRQ(ex1_3()); 500 } else if (test_mode == 4) { 501 CHKERRQ(ex1_4()); 502 } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_USER,"Unknown test_mode value, should be 1,2,3,4"); 503 ierr = PetscFinalize(); 504 return ierr; 505 } 506 507 /*TEST 508 509 build: 510 requires: !complex double 511 512 test: 513 args: -test_mode 1 514 filter: grep -v atomic 515 filter_output: grep -v atomic 516 517 test: 518 suffix: 2 519 args: -test_mode 2 520 filter: grep -v atomic 521 filter_output: grep -v atomic 522 523 test: 524 suffix: 3 525 args: -test_mode 3 526 filter: grep -v atomic 527 filter_output: grep -v atomic 528 TODO: broken 529 530 test: 531 suffix: 4 532 args: -test_mode 4 533 filter: grep -v atomic 534 filter_output: grep -v atomic 535 536 test: 537 suffix: 5 538 nsize: 4 539 args: -test_mode 1 540 filter: grep -v atomic 541 filter_output: grep -v atomic 542 543 test: 544 suffix: 6 545 nsize: 4 546 args: -test_mode 2 547 filter: grep -v atomic 548 filter_output: grep -v atomic 549 550 test: 551 suffix: 7 552 nsize: 4 553 args: -test_mode 3 554 filter: grep -v atomic 555 filter_output: grep -v atomic 556 TODO: broken 557 558 test: 559 suffix: 8 560 nsize: 4 561 args: -test_mode 4 562 filter: grep -v atomic 563 filter_output: grep -v atomic 564 565 TEST*/ 566