static char help[] = "Tests DMSwarm with DMShell\n\n"; #include #include #include #include #include #include PetscErrorCode _DMLocatePoints_DMDARegular_IS(DM dm,Vec pos,IS *iscell) { PetscInt p,n,bs,npoints,si,sj,milocal,mjlocal,mx,my; DM dmregular; PetscInt *cellidx; const PetscScalar *coor; PetscReal dx,dy; PetscErrorCode ierr; PetscMPIInt rank; PetscFunctionBegin; ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRMPI(ierr); ierr = VecGetLocalSize(pos,&n);CHKERRQ(ierr); ierr = VecGetBlockSize(pos,&bs);CHKERRQ(ierr); npoints = n/bs; ierr = PetscMalloc1(npoints,&cellidx);CHKERRQ(ierr); ierr = DMGetApplicationContext(dm,&dmregular);CHKERRQ(ierr); ierr = DMDAGetCorners(dmregular,&si,&sj,NULL,&milocal,&mjlocal,NULL);CHKERRQ(ierr); ierr = DMDAGetInfo(dmregular,NULL,&mx,&my,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL);CHKERRQ(ierr); dx = 2.0/((PetscReal)mx); dy = 2.0/((PetscReal)my); ierr = VecGetArrayRead(pos,&coor);CHKERRQ(ierr); for (p=0; p= sj) && (mj < sj + mjlocal)) { if ((mi >= si) && (mi < si + milocal)) { cellidx[p] = (mi-si) + (mj-sj) * milocal; } } if (coorx < -1.0) cellidx[p] = DMLOCATEPOINT_POINT_NOT_FOUND; if (coorx > 1.0) cellidx[p] = DMLOCATEPOINT_POINT_NOT_FOUND; if (coory < -1.0) cellidx[p] = DMLOCATEPOINT_POINT_NOT_FOUND; if (coory > 1.0) cellidx[p] = DMLOCATEPOINT_POINT_NOT_FOUND; } ierr = VecRestoreArrayRead(pos,&coor);CHKERRQ(ierr); ierr = ISCreateGeneral(PETSC_COMM_SELF,npoints,cellidx,PETSC_OWN_POINTER,iscell);CHKERRQ(ierr); PetscFunctionReturn(0); } PetscErrorCode DMLocatePoints_DMDARegular(DM dm,Vec pos,DMPointLocationType ltype, PetscSF cellSF) { IS iscell; PetscSFNode *cells; PetscInt p,bs,npoints,nfound; const PetscInt *boxCells; PetscErrorCode ierr; PetscFunctionBegin; ierr = _DMLocatePoints_DMDARegular_IS(dm,pos,&iscell);CHKERRQ(ierr); ierr = VecGetLocalSize(pos,&npoints);CHKERRQ(ierr); ierr = VecGetBlockSize(pos,&bs);CHKERRQ(ierr); npoints = npoints / bs; ierr = PetscMalloc1(npoints, &cells);CHKERRQ(ierr); ierr = ISGetIndices(iscell, &boxCells);CHKERRQ(ierr); for (p=0; pops->locatepoints = DMLocatePoints_DMDARegular; dmcell->ops->getneighbors = DMGetNeighbors_DMDARegular; /* Create the swarm */ ierr = DMCreate(PETSC_COMM_WORLD,&dms);CHKERRQ(ierr); ierr = DMSetType(dms,DMSWARM);CHKERRQ(ierr); ierr = DMSetDimension(dms,2);CHKERRQ(ierr); ierr = DMSwarmSetType(dms,DMSWARM_PIC);CHKERRQ(ierr); ierr = DMSwarmSetCellDM(dms,dmcell);CHKERRQ(ierr); ierr = DMSwarmRegisterPetscDatatypeField(dms,"itag",1,PETSC_INT);CHKERRQ(ierr); ierr = DMSwarmFinalizeFieldRegister(dms);CHKERRQ(ierr); { PetscInt si,sj,milocal,mjlocal; const PetscScalar *LA_coors; Vec coors; PetscInt cnt; ierr = DMDAGetCorners(dmregular,&si,&sj,NULL,&milocal,&mjlocal,NULL);CHKERRQ(ierr); ierr = DMGetCoordinates(dmregular,&coors);CHKERRQ(ierr); ierr = VecGetArrayRead(coors,&LA_coors);CHKERRQ(ierr); ierr = DMSwarmSetLocalSizes(dms,milocal*mjlocal,4);CHKERRQ(ierr); ierr = DMSwarmGetLocalSize(dms,&nlocal);CHKERRQ(ierr); ierr = DMSwarmGetField(dms,DMSwarmPICField_coor,&bs,NULL,(void**)&array);CHKERRQ(ierr); cnt = 0; ierr = PetscRandomCreate(PETSC_COMM_SELF,&rand);CHKERRQ(ierr); ierr = PetscRandomSetInterval(rand,-1.0,1.0);CHKERRQ(ierr); for (p=0; p