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:   PetscFunctionBegin;
 16:   options->debug     = 0;
 17:   options->pStart    = 0;
 18:   options->pEnd      = 1000;
 19:   options->numStrata = 5;
 20:   options->fill      = 0.10;

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

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

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

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

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

 66:   PetscFunctionBegin;
 67:   for (p = pStart; p < pEnd; ++p) {
 68:     PetscInt  val;
 69:     PetscBool has;

 71:     PetscCall(DMLabelGetValue(label, p, &val));
 72:     PetscCall(DMLabelHasPoint(label, p, &has));
 73:     PetscCheck((val < 0 || has) || (val >= 0 || has), PETSC_COMM_SELF, PETSC_ERR_PLIB, "Label value %" PetscInt_FMT " does not match contains check %" PetscInt_FMT " for point %" PetscInt_FMT, val, (PetscInt)has, p);
 74:     if (has) ++n;
 75:   }
 76:   PetscCheck(n == user->size, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid number of label points detected %" PetscInt_FMT " does not match number set %" PetscInt_FMT, n, user->size);
 77:   /* Also put in timing code */
 78:   PetscFunctionReturn(PETSC_SUCCESS);
 79: }

 81: PetscErrorCode TestClear(DMLabel label, AppCtx *user)
 82: {
 83:   PetscInt pStart = user->pStart, pEnd = user->pEnd, p;
 84:   PetscInt defaultValue;

 86:   PetscFunctionBegin;
 87:   PetscCall(DMLabelGetDefaultValue(label, &defaultValue));
 88:   for (p = pStart; p < pEnd; p++) {
 89:     PetscInt  val;
 90:     PetscBool hasPoint;

 92:     PetscCall(DMLabelGetValue(label, p, &val));
 93:     if (val != defaultValue) PetscCall(DMLabelClearValue(label, p, val));
 94:     PetscCall(DMLabelGetValue(label, p, &val));
 95:     PetscCall(DMLabelHasPoint(label, p, &hasPoint));
 96:     PetscCheck(val == defaultValue, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Expected default value %" PetscInt_FMT " after clearing point %" PetscInt_FMT ", got %" PetscInt_FMT, defaultValue, p, val);
 97:     PetscCheck(!hasPoint, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Label contains %" PetscInt_FMT " after clearing", p);
 98:   }
 99:   PetscFunctionReturn(PETSC_SUCCESS);
100: }

102: int main(int argc, char **argv)
103: {
104:   DMLabel label;
105:   AppCtx  user; /* user-defined work context */

107:   PetscFunctionBeginUser;
108:   PetscCall(PetscInitialize(&argc, &argv, NULL, help));
109:   PetscCall(ProcessOptions(PETSC_COMM_WORLD, &user));
110:   PetscCall(DMLabelCreate(PETSC_COMM_SELF, "Test Label", &label));
111:   PetscCall(TestSetup(label, &user));
112:   PetscCall(TestLookup(label, &user));
113:   PetscCall(TestClear(label, &user));
114:   PetscCall(DMLabelDestroy(&label));
115:   PetscCall(PetscFinalize());
116:   return 0;
117: }

119: /*TEST

121:   test:
122:     suffix: 0
123:     args: -malloc_dump
124:   test:
125:     suffix: 1
126:     args: -malloc_dump -pend 10000
127:   test:
128:     suffix: 2
129:     args: -malloc_dump -pend 10000 -fill 0.05
130:   test:
131:     suffix: 3
132:     args: -malloc_dump -pend 10000 -fill 0.25

134: TEST*/