xref: /petsc/src/vec/vec/utils/tagger/impls/simple.c (revision bcda9346efad4e5ba2d553af84eb238771ba1e25)
1 #include <petsc/private/vecimpl.h> /*I "petscvec.h" I*/
2 #include "../src/vec/vec/utils/tagger/impls/simple.h"
3 
VecTaggerDestroy_Simple(VecTagger tagger)4 static PetscErrorCode VecTaggerDestroy_Simple(VecTagger tagger)
5 {
6   VecTagger_Simple *smpl = (VecTagger_Simple *)tagger->data;
7 
8   PetscFunctionBegin;
9   PetscCall(PetscFree(smpl->box));
10   PetscCall(PetscFree(tagger->data));
11   PetscFunctionReturn(PETSC_SUCCESS);
12 }
13 
VecTaggerSetFromOptions_Simple(VecTagger tagger,PetscOptionItems PetscOptionsObject)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;
22 
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 }
40 
VecTaggerSetUp_Simple(VecTagger tagger)41 static PetscErrorCode VecTaggerSetUp_Simple(VecTagger tagger)
42 {
43   VecTagger_Simple *smpl = (VecTagger_Simple *)tagger->data;
44 
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 }
49 
VecTaggerView_Simple(VecTagger tagger,PetscViewer viewer)50 PetscErrorCode VecTaggerView_Simple(VecTagger tagger, PetscViewer viewer)
51 {
52   VecTagger_Simple *smpl = (VecTagger_Simple *)tagger->data;
53   PetscBool         isascii;
54 
55   PetscFunctionBegin;
56   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii));
57   if (isascii) {
58     PetscInt    bs, i;
59     const char *name;
60 
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 }
81 
VecTaggerSetBox_Simple(VecTagger tagger,VecTaggerBox * box)82 PetscErrorCode VecTaggerSetBox_Simple(VecTagger tagger, VecTaggerBox *box)
83 {
84   VecTagger_Simple *smpl = (VecTagger_Simple *)tagger->data;
85 
86   PetscFunctionBegin;
87   PetscValidHeaderSpecific(tagger, VEC_TAGGER_CLASSID, 1);
88   PetscAssertPointer(box, 2);
89   if (box != smpl->box) {
90     PetscInt bs, i;
91 
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 }
99 
VecTaggerGetBox_Simple(VecTagger tagger,const VecTaggerBox ** box)100 PetscErrorCode VecTaggerGetBox_Simple(VecTagger tagger, const VecTaggerBox **box)
101 {
102   VecTagger_Simple *smpl = (VecTagger_Simple *)tagger->data;
103 
104   PetscFunctionBegin;
105   PetscValidHeaderSpecific(tagger, VEC_TAGGER_CLASSID, 1);
106   PetscAssertPointer(box, 2);
107   *box = smpl->box;
108   PetscFunctionReturn(PETSC_SUCCESS);
109 }
110 
VecTaggerCreate_Simple(VecTagger tagger)111 PetscErrorCode VecTaggerCreate_Simple(VecTagger tagger)
112 {
113   VecTagger_Simple *smpl;
114 
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 }
125