Actual source code: swarm_ex2.c

petsc-3.12.5 2020-03-29
Report Typos and Errors

  2: static char help[] = "Tests DMSwarm\n\n";

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

  8: /*
  9:  Checks for variable blocksize
 10: */
 11: PetscErrorCode ex2_1(void)
 12: {
 13:   DM             dms;
 15:   Vec            x;
 16:   PetscMPIInt    rank;
 17:   PetscInt       p,bs,nlocal;

 19:   MPI_Comm_rank(PETSC_COMM_WORLD,&rank);

 21:   DMCreate(PETSC_COMM_WORLD,&dms);
 22:   DMSetType(dms,DMSWARM);
 23:   DMSwarmInitializeFieldRegister(dms);
 24:   DMSwarmRegisterPetscDatatypeField(dms,"viscosity",1,PETSC_REAL);
 25:   DMSwarmRegisterPetscDatatypeField(dms,"strain",3,PETSC_REAL);
 26:   DMSwarmFinalizeFieldRegister(dms);
 27:   DMSwarmSetLocalSizes(dms,5+rank,4);
 28:   DMView(dms,PETSC_VIEWER_STDOUT_WORLD);
 29:   DMSwarmGetLocalSize(dms,&nlocal);

 31:   {
 32:     PetscReal *array;
 33:     DMSwarmGetField(dms,"viscosity",&bs,NULL,(void**)&array);
 34:     for (p=0; p<nlocal; p++) {
 35:       array[p] = 11.1 + p*0.1 + rank*100.0;
 36:     }
 37:     DMSwarmRestoreField(dms,"viscosity",&bs,NULL,(void**)&array);
 38:   }

 40:   {
 41:     PetscReal *array;
 42:     DMSwarmGetField(dms,"strain",&bs,NULL,(void**)&array);
 43:     for (p=0; p<nlocal; p++) {
 44:       array[bs*p+0] = 2.0e-2 + p*0.001 + rank*1.0;
 45:       array[bs*p+1] = 2.0e-2 + p*0.002 + rank*1.0;
 46:       array[bs*p+2] = 2.0e-2 + p*0.003 + rank*1.0;
 47:     }
 48:     DMSwarmRestoreField(dms,"strain",&bs,NULL,(void**)&array);
 49:   }

 51:   DMSwarmCreateGlobalVectorFromField(dms,"viscosity",&x);
 52:   VecView(x,PETSC_VIEWER_STDOUT_WORLD);
 53:   DMSwarmDestroyGlobalVectorFromField(dms,"viscosity",&x);

 55:   DMSwarmCreateGlobalVectorFromField(dms,"strain",&x);
 56:   VecView(x,PETSC_VIEWER_STDOUT_WORLD);
 57:   DMSwarmDestroyGlobalVectorFromField(dms,"strain",&x);

 59:   DMSwarmVectorDefineField(dms,"strain");
 60:   DMCreateGlobalVector(dms,&x);
 61:   VecView(x,PETSC_VIEWER_STDOUT_WORLD);
 62:   VecDestroy(&x);
 63:   DMDestroy(&dms);
 64:   return(0);
 65: }


 68: int main(int argc,char **argv)
 69: {

 72:   PetscInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;
 73:   ex2_1();
 74:   PetscFinalize();
 75:   return ierr;
 76: }

 78: /*TEST

 80:    test:
 81:       requires: !complex double
 82:       nsize: 3
 83:       filter: grep -v atomic
 84:       filter_output: grep -v atomic

 86: TEST*/