Actual source code: simple.c

      1: #include <petsc/private/vecimpl.h>
      2: #include "../src/vec/vec/utils/tagger/impls/simple.h"

      4: static PetscErrorCode VecTaggerDestroy_Simple(VecTagger tagger)
      5: {
      6:   VecTagger_Simple *smpl = (VecTagger_Simple *)tagger->data;

      8:   PetscFunctionBegin;
      9:   PetscCall(PetscFree(smpl->box));
     10:   PetscCall(PetscFree(tagger->data));
     11:   PetscFunctionReturn(PETSC_SUCCESS);
     12: }

     14: PetscErrorCode VecTaggerSetFromOptions_Simple(VecTagger tagger, PetscOptionItems PetscOptionsObject)
     15: {
     16:   PetscInt     nvals, bs;
     17:   char         headstring[BUFSIZ];
     18:   char         funcstring[BUFSIZ];
     19:   const char  *name;
     20:   PetscBool    set;
     21:   PetscScalar *inBoxVals;

     23:   PetscFunctionBegin;
     24:   PetscCall(PetscObjectGetType((PetscObject)tagger, &name));
     25:   PetscCall(VecTaggerGetBlockSize(tagger, &bs));
     26:   nvals = 2 * bs;
     27:   PetscCall(PetscMalloc1(nvals, &inBoxVals));
     28:   PetscCall(PetscSNPrintf(headstring, BUFSIZ, "VecTagger %s options", name));
     29:   PetscCall(PetscSNPrintf(funcstring, BUFSIZ, "VecTagger%sSetBox()", name));
     30:   PetscOptionsHeadBegin(PetscOptionsObject, headstring);
     31:   PetscCall(PetscOptionsScalarArray("-vec_tagger_box", "lower and upper bounds of the box", funcstring, inBoxVals, &nvals, &set));
     32:   PetscOptionsHeadEnd();
     33:   if (set) {
     34:     PetscCheck(nvals == 2 * bs, PetscObjectComm((PetscObject)tagger), PETSC_ERR_ARG_INCOMP, "Expect array of %" PetscInt_FMT " values for -vec_tagger_box, got %" PetscInt_FMT, 2 * bs, nvals);
     35:     PetscCall(VecTaggerSetBox_Simple(tagger, (VecTaggerBox *)inBoxVals));
     36:   }
     37:   PetscCall(PetscFree(inBoxVals));
     38:   PetscFunctionReturn(PETSC_SUCCESS);
     39: }

     41: static PetscErrorCode VecTaggerSetUp_Simple(VecTagger tagger)
     42: {
     43:   VecTagger_Simple *smpl = (VecTagger_Simple *)tagger->data;

     45:   PetscFunctionBegin;
     46:   PetscCheck(smpl->box, PetscObjectComm((PetscObject)tagger), PETSC_ERR_ARG_WRONGSTATE, "Must set a box before calling setup.");
     47:   PetscFunctionReturn(PETSC_SUCCESS);
     48: }

     50: PetscErrorCode VecTaggerView_Simple(VecTagger tagger, PetscViewer viewer)
     51: {
     52:   VecTagger_Simple *smpl = (VecTagger_Simple *)tagger->data;
     53:   PetscBool         isascii;

     55:   PetscFunctionBegin;
     56:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii));
     57:   if (isascii) {
     58:     PetscInt    bs, i;
     59:     const char *name;

     61:     PetscCall(PetscObjectGetType((PetscObject)tagger, &name));
     62:     PetscCall(VecTaggerGetBlockSize(tagger, &bs));
     63:     PetscCall(PetscViewerASCIIPrintf(viewer, " %s box=[", name));
     64:     for (i = 0; i < bs; i++) {
     65:       if (i) PetscCall(PetscViewerASCIIPrintf(viewer, "; "));
     66: #if !defined(PETSC_USE_COMPLEX)
     67:       PetscCall(PetscViewerASCIIPrintf(viewer, "%g,%g", (double)smpl->box[i].min, (double)smpl->box[i].max));
     68: #else
     69:       // If the imaginary parts coincide, this is intended to be a real interval
     70:       if (PetscImaginaryPart(smpl->box[i].min) == PetscImaginaryPart(smpl->box[i].max)) {
     71:         PetscCall(PetscViewerASCIIPrintf(viewer, "%g,%g", (double)PetscRealPart(smpl->box[i].min), (double)PetscRealPart(smpl->box[i].max)));
     72:       } else {
     73:         PetscCall(PetscViewerASCIIPrintf(viewer, "%g+%gi,%g+%gi", (double)PetscRealPart(smpl->box[i].min), (double)PetscImaginaryPart(smpl->box[i].min), (double)PetscRealPart(smpl->box[i].max), (double)PetscImaginaryPart(smpl->box[i].max)));
     74:       }
     75: #endif
     76:     }
     77:     PetscCall(PetscViewerASCIIPrintf(viewer, "]\n"));
     78:   }
     79:   PetscFunctionReturn(PETSC_SUCCESS);
     80: }

     82: PetscErrorCode VecTaggerSetBox_Simple(VecTagger tagger, VecTaggerBox *box)
     83: {
     84:   VecTagger_Simple *smpl = (VecTagger_Simple *)tagger->data;

     86:   PetscFunctionBegin;
     88:   PetscAssertPointer(box, 2);
     89:   if (box != smpl->box) {
     90:     PetscInt bs, i;

     92:     PetscCall(VecTaggerGetBlockSize(tagger, &bs));
     93:     PetscCall(PetscFree(smpl->box));
     94:     PetscCall(PetscMalloc1(bs, &smpl->box));
     95:     for (i = 0; i < bs; i++) smpl->box[i] = box[i];
     96:   }
     97:   PetscFunctionReturn(PETSC_SUCCESS);
     98: }

    100: PetscErrorCode VecTaggerGetBox_Simple(VecTagger tagger, const VecTaggerBox **box)
    101: {
    102:   VecTagger_Simple *smpl = (VecTagger_Simple *)tagger->data;

    104:   PetscFunctionBegin;
    106:   PetscAssertPointer(box, 2);
    107:   *box = smpl->box;
    108:   PetscFunctionReturn(PETSC_SUCCESS);
    109: }

    111: PetscErrorCode VecTaggerCreate_Simple(VecTagger tagger)
    112: {
    113:   VecTagger_Simple *smpl;

    115:   PetscFunctionBegin;
    116:   tagger->ops->destroy        = VecTaggerDestroy_Simple;
    117:   tagger->ops->setfromoptions = VecTaggerSetFromOptions_Simple;
    118:   tagger->ops->setup          = VecTaggerSetUp_Simple;
    119:   tagger->ops->view           = VecTaggerView_Simple;
    120:   tagger->ops->computeis      = VecTaggerComputeIS_FromBoxes;
    121:   PetscCall(PetscNew(&smpl));
    122:   tagger->data = smpl;
    123:   PetscFunctionReturn(PETSC_SUCCESS);
    124: }