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 iascii;
55: PetscFunctionBegin;
56: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &iascii));
57: if (iascii) {
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: 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)));
70: #endif
71: }
72: PetscCall(PetscViewerASCIIPrintf(viewer, "]\n"));
73: }
74: PetscFunctionReturn(PETSC_SUCCESS);
75: }
77: PetscErrorCode VecTaggerSetBox_Simple(VecTagger tagger, VecTaggerBox *box)
78: {
79: VecTagger_Simple *smpl = (VecTagger_Simple *)tagger->data;
81: PetscFunctionBegin;
83: PetscAssertPointer(box, 2);
84: if (box != smpl->box) {
85: PetscInt bs, i;
87: PetscCall(VecTaggerGetBlockSize(tagger, &bs));
88: PetscCall(PetscFree(smpl->box));
89: PetscCall(PetscMalloc1(bs, &smpl->box));
90: for (i = 0; i < bs; i++) smpl->box[i] = box[i];
91: }
92: PetscFunctionReturn(PETSC_SUCCESS);
93: }
95: PetscErrorCode VecTaggerGetBox_Simple(VecTagger tagger, const VecTaggerBox **box)
96: {
97: VecTagger_Simple *smpl = (VecTagger_Simple *)tagger->data;
99: PetscFunctionBegin;
101: PetscAssertPointer(box, 2);
102: *box = smpl->box;
103: PetscFunctionReturn(PETSC_SUCCESS);
104: }
106: PetscErrorCode VecTaggerCreate_Simple(VecTagger tagger)
107: {
108: VecTagger_Simple *smpl;
110: PetscFunctionBegin;
111: tagger->ops->destroy = VecTaggerDestroy_Simple;
112: tagger->ops->setfromoptions = VecTaggerSetFromOptions_Simple;
113: tagger->ops->setup = VecTaggerSetUp_Simple;
114: tagger->ops->view = VecTaggerView_Simple;
115: tagger->ops->computeis = VecTaggerComputeIS_FromBoxes;
116: PetscCall(PetscNew(&smpl));
117: tagger->data = smpl;
118: PetscFunctionReturn(PETSC_SUCCESS);
119: }