1c4762a1bSJed Brown static char help[] = "Tests for DMLabel lookup\n\n"; 2c4762a1bSJed Brown 3c4762a1bSJed Brown #include <petscdmplex.h> 4c4762a1bSJed Brown 5c4762a1bSJed Brown typedef struct { 6c4762a1bSJed Brown PetscInt debug; /* The debugging level */ 7c4762a1bSJed Brown PetscInt pStart, pEnd; /* The label chart */ 8c4762a1bSJed Brown PetscInt numStrata; /* The number of label strata */ 9c4762a1bSJed Brown PetscReal fill; /* Percentage of label to fill */ 10c4762a1bSJed Brown PetscInt size; /* The number of set values */ 11c4762a1bSJed Brown } AppCtx; 12c4762a1bSJed Brown 13c4762a1bSJed Brown PetscErrorCode ProcessOptions(MPI_Comm comm, AppCtx *options) 14c4762a1bSJed Brown { 15c4762a1bSJed Brown PetscFunctionBegin; 16c4762a1bSJed Brown options->debug = 0; 17c4762a1bSJed Brown options->pStart = 0; 18c4762a1bSJed Brown options->pEnd = 1000; 19c4762a1bSJed Brown options->numStrata = 5; 20c4762a1bSJed Brown options->fill = 0.10; 21c4762a1bSJed Brown 22d0609cedSBarry Smith PetscOptionsBegin(comm, "", "Meshing Problem Options", "DMPLEX"); 239566063dSJacob Faibussowitsch PetscCall(PetscOptionsBoundedInt("-debug", "The debugging level", "ex6.c", options->debug, &options->debug, NULL,0)); 249566063dSJacob Faibussowitsch PetscCall(PetscOptionsBoundedInt("-num_strata", "The number of label values", "ex6.c", options->numStrata, &options->numStrata, NULL,0)); 259566063dSJacob Faibussowitsch PetscCall(PetscOptionsBoundedInt("-pend", "The label point limit", "ex6.c", options->pEnd, &options->pEnd, NULL,0)); 269566063dSJacob Faibussowitsch PetscCall(PetscOptionsReal("-fill", "The percentage of label chart to set", "ex6.c", options->fill, &options->fill, NULL)); 27d0609cedSBarry Smith PetscOptionsEnd(); 28c4762a1bSJed Brown PetscFunctionReturn(0); 29c4762a1bSJed Brown } 30c4762a1bSJed Brown 31c4762a1bSJed Brown PetscErrorCode TestSetup(DMLabel label, AppCtx *user) 32c4762a1bSJed Brown { 33c4762a1bSJed Brown PetscRandom r; 34c4762a1bSJed Brown PetscInt n = (PetscInt) (user->fill*(user->pEnd - user->pStart)), i; 35c4762a1bSJed Brown 36c4762a1bSJed Brown PetscFunctionBegin; 379566063dSJacob Faibussowitsch PetscCall(PetscRandomCreate(PETSC_COMM_SELF, &r)); 389566063dSJacob Faibussowitsch PetscCall(PetscRandomSetFromOptions(r));/* -random_type <> */ 399566063dSJacob Faibussowitsch PetscCall(PetscRandomSetInterval(r, user->pStart, user->pEnd)); 409566063dSJacob Faibussowitsch PetscCall(PetscRandomSetSeed(r, 123456789L)); 419566063dSJacob Faibussowitsch PetscCall(PetscRandomSeed(r)); 42c4762a1bSJed Brown user->size = 0; 43c4762a1bSJed Brown for (i = 0; i < n; ++i) { 44c4762a1bSJed Brown PetscReal p; 45c4762a1bSJed Brown PetscInt val; 46c4762a1bSJed Brown 479566063dSJacob Faibussowitsch PetscCall(PetscRandomGetValueReal(r, &p)); 489566063dSJacob Faibussowitsch PetscCall(DMLabelGetValue(label, (PetscInt) p, &val)); 49c4762a1bSJed Brown if (val < 0) { 50c4762a1bSJed Brown ++user->size; 519566063dSJacob Faibussowitsch PetscCall(DMLabelSetValue(label, (PetscInt) p, i % user->numStrata)); 52c4762a1bSJed Brown } 53c4762a1bSJed Brown } 549566063dSJacob Faibussowitsch PetscCall(PetscRandomDestroy(&r)); 559566063dSJacob Faibussowitsch PetscCall(DMLabelCreateIndex(label, user->pStart, user->pEnd)); 5663a3b9bcSJacob Faibussowitsch 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)); 57c4762a1bSJed Brown PetscFunctionReturn(0); 58c4762a1bSJed Brown } 59c4762a1bSJed Brown 60c4762a1bSJed Brown PetscErrorCode TestLookup(DMLabel label, AppCtx *user) 61c4762a1bSJed Brown { 62c4762a1bSJed Brown const PetscInt pStart = user->pStart; 63c4762a1bSJed Brown const PetscInt pEnd = user->pEnd; 64c4762a1bSJed Brown PetscInt p, n = 0; 65c4762a1bSJed Brown 66c4762a1bSJed Brown PetscFunctionBegin; 67c4762a1bSJed Brown for (p = pStart; p < pEnd; ++p) { 68c4762a1bSJed Brown PetscInt val; 69c4762a1bSJed Brown PetscBool has; 70c4762a1bSJed Brown 719566063dSJacob Faibussowitsch PetscCall(DMLabelGetValue(label, p, &val)); 729566063dSJacob Faibussowitsch PetscCall(DMLabelHasPoint(label, p, &has)); 73*1dca8a05SBarry Smith 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); 74c4762a1bSJed Brown if (has) ++n; 75c4762a1bSJed Brown } 7663a3b9bcSJacob Faibussowitsch 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); 77c4762a1bSJed Brown /* Also put in timing code */ 78c4762a1bSJed Brown PetscFunctionReturn(0); 79c4762a1bSJed Brown } 80c4762a1bSJed Brown 81c4762a1bSJed Brown PetscErrorCode TestClear(DMLabel label, AppCtx *user) 82c4762a1bSJed Brown { 83c4762a1bSJed Brown PetscInt pStart = user->pStart, pEnd = user->pEnd, p; 84c4762a1bSJed Brown PetscInt defaultValue; 85c4762a1bSJed Brown 86c4762a1bSJed Brown PetscFunctionBegin; 879566063dSJacob Faibussowitsch PetscCall(DMLabelGetDefaultValue(label,&defaultValue)); 88c4762a1bSJed Brown for (p = pStart; p < pEnd; p++) { 89c4762a1bSJed Brown PetscInt val; 90c4762a1bSJed Brown PetscBool hasPoint; 91c4762a1bSJed Brown 929566063dSJacob Faibussowitsch PetscCall(DMLabelGetValue(label,p,&val)); 93c4762a1bSJed Brown if (val != defaultValue) { 949566063dSJacob Faibussowitsch PetscCall(DMLabelClearValue(label,p,val)); 95c4762a1bSJed Brown } 969566063dSJacob Faibussowitsch PetscCall(DMLabelGetValue(label,p,&val)); 979566063dSJacob Faibussowitsch PetscCall(DMLabelHasPoint(label,p,&hasPoint)); 9863a3b9bcSJacob Faibussowitsch 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); 9963a3b9bcSJacob Faibussowitsch PetscCheck(!hasPoint,PETSC_COMM_SELF,PETSC_ERR_PLIB,"Label contains %" PetscInt_FMT " after clearing",p); 100c4762a1bSJed Brown } 101c4762a1bSJed Brown PetscFunctionReturn(0); 102c4762a1bSJed Brown } 103c4762a1bSJed Brown 104c4762a1bSJed Brown int main(int argc, char **argv) 105c4762a1bSJed Brown { 106c4762a1bSJed Brown DMLabel label; 107c4762a1bSJed Brown AppCtx user; /* user-defined work context */ 108c4762a1bSJed Brown 1099566063dSJacob Faibussowitsch PetscCall(PetscInitialize(&argc, &argv, NULL,help)); 1109566063dSJacob Faibussowitsch PetscCall(ProcessOptions(PETSC_COMM_WORLD, &user)); 1119566063dSJacob Faibussowitsch PetscCall(DMLabelCreate(PETSC_COMM_SELF, "Test Label", &label)); 1129566063dSJacob Faibussowitsch PetscCall(TestSetup(label, &user)); 1139566063dSJacob Faibussowitsch PetscCall(TestLookup(label, &user)); 1149566063dSJacob Faibussowitsch PetscCall(TestClear(label,&user)); 1159566063dSJacob Faibussowitsch PetscCall(DMLabelDestroy(&label)); 1169566063dSJacob Faibussowitsch PetscCall(PetscFinalize()); 117b122ec5aSJacob Faibussowitsch return 0; 118c4762a1bSJed Brown } 119c4762a1bSJed Brown 120c4762a1bSJed Brown /*TEST 121c4762a1bSJed Brown 122c4762a1bSJed Brown test: 123c4762a1bSJed Brown suffix: 0 124c4762a1bSJed Brown args: -malloc_dump 125c4762a1bSJed Brown test: 126c4762a1bSJed Brown suffix: 1 127c4762a1bSJed Brown args: -malloc_dump -pend 10000 128c4762a1bSJed Brown test: 129c4762a1bSJed Brown suffix: 2 130c4762a1bSJed Brown args: -malloc_dump -pend 10000 -fill 0.05 131c4762a1bSJed Brown test: 132c4762a1bSJed Brown suffix: 3 133c4762a1bSJed Brown args: -malloc_dump -pend 10000 -fill 0.25 134c4762a1bSJed Brown 135c4762a1bSJed Brown TEST*/ 136