Actual source code: ex6.c

  1: static char help[] = "Tests for DMLabel lookup\n\n";

  3: #include <petscdmplex.h>

  5: typedef struct {
  6:   PetscInt  debug;        /* The debugging level */
  7:   PetscInt  pStart, pEnd; /* The label chart */
  8:   PetscInt  numStrata;    /* The number of label strata */
  9:   PetscReal fill;         /* Percentage of label to fill */
 10:   PetscInt  size;         /* The number of set values */
 11: } AppCtx;

 13: PetscErrorCode ProcessOptions(MPI_Comm comm, AppCtx *options)
 14: {
 15:   options->debug     = 0;
 16:   options->pStart    = 0;
 17:   options->pEnd      = 1000;
 18:   options->numStrata = 5;
 19:   options->fill      = 0.10;

 21:   PetscOptionsBegin(comm, "", "Meshing Problem Options", "DMPLEX");
 22:   PetscOptionsBoundedInt("-debug", "The debugging level", "ex6.c", options->debug, &options->debug, NULL, 0);
 23:   PetscOptionsBoundedInt("-num_strata", "The number of label values", "ex6.c", options->numStrata, &options->numStrata, NULL, 0);
 24:   PetscOptionsBoundedInt("-pend", "The label point limit", "ex6.c", options->pEnd, &options->pEnd, NULL, 0);
 25:   PetscOptionsReal("-fill", "The percentage of label chart to set", "ex6.c", options->fill, &options->fill, NULL);
 26:   PetscOptionsEnd();
 27:   return 0;
 28: }

 30: PetscErrorCode TestSetup(DMLabel label, AppCtx *user)
 31: {
 32:   PetscRandom r;
 33:   PetscInt    n = (PetscInt)(user->fill * (user->pEnd - user->pStart)), i;

 35:   PetscRandomCreate(PETSC_COMM_SELF, &r);
 36:   PetscRandomSetFromOptions(r); /* -random_type <> */
 37:   PetscRandomSetInterval(r, user->pStart, user->pEnd);
 38:   PetscRandomSetSeed(r, 123456789L);
 39:   PetscRandomSeed(r);
 40:   user->size = 0;
 41:   for (i = 0; i < n; ++i) {
 42:     PetscReal p;
 43:     PetscInt  val;

 45:     PetscRandomGetValueReal(r, &p);
 46:     DMLabelGetValue(label, (PetscInt)p, &val);
 47:     if (val < 0) {
 48:       ++user->size;
 49:       DMLabelSetValue(label, (PetscInt)p, i % user->numStrata);
 50:     }
 51:   }
 52:   PetscRandomDestroy(&r);
 53:   DMLabelCreateIndex(label, user->pStart, user->pEnd);
 54:   PetscPrintf(PETSC_COMM_SELF, "Created label with chart [%" PetscInt_FMT ", %" PetscInt_FMT ") and set %" PetscInt_FMT " values\n", user->pStart, user->pEnd, user->size);
 55:   return 0;
 56: }

 58: PetscErrorCode TestLookup(DMLabel label, AppCtx *user)
 59: {
 60:   const PetscInt pStart = user->pStart;
 61:   const PetscInt pEnd   = user->pEnd;
 62:   PetscInt       p, n = 0;

 64:   for (p = pStart; p < pEnd; ++p) {
 65:     PetscInt  val;
 66:     PetscBool has;

 68:     DMLabelGetValue(label, p, &val);
 69:     DMLabelHasPoint(label, p, &has);
 71:     if (has) ++n;
 72:   }
 74:   /* Also put in timing code */
 75:   return 0;
 76: }

 78: PetscErrorCode TestClear(DMLabel label, AppCtx *user)
 79: {
 80:   PetscInt pStart = user->pStart, pEnd = user->pEnd, p;
 81:   PetscInt defaultValue;

 83:   DMLabelGetDefaultValue(label, &defaultValue);
 84:   for (p = pStart; p < pEnd; p++) {
 85:     PetscInt  val;
 86:     PetscBool hasPoint;

 88:     DMLabelGetValue(label, p, &val);
 89:     if (val != defaultValue) DMLabelClearValue(label, p, val);
 90:     DMLabelGetValue(label, p, &val);
 91:     DMLabelHasPoint(label, p, &hasPoint);
 94:   }
 95:   return 0;
 96: }

 98: int main(int argc, char **argv)
 99: {
100:   DMLabel label;
101:   AppCtx  user; /* user-defined work context */

104:   PetscInitialize(&argc, &argv, NULL, help);
105:   ProcessOptions(PETSC_COMM_WORLD, &user);
106:   DMLabelCreate(PETSC_COMM_SELF, "Test Label", &label);
107:   TestSetup(label, &user);
108:   TestLookup(label, &user);
109:   TestClear(label, &user);
110:   DMLabelDestroy(&label);
111:   PetscFinalize();
112:   return 0;
113: }

115: /*TEST

117:   test:
118:     suffix: 0
119:     args: -malloc_dump
120:   test:
121:     suffix: 1
122:     args: -malloc_dump -pend 10000
123:   test:
124:     suffix: 2
125:     args: -malloc_dump -pend 10000 -fill 0.05
126:   test:
127:     suffix: 3
128:     args: -malloc_dump -pend 10000 -fill 0.25

130: TEST*/