Actual source code: vsection.c
petsc-dev 2014-02-02
1: /*
2: This file contains routines for section object operations on Vecs
3: */
4: #include <petsc-private/isimpl.h> /*I "petscvec.h" I*/
5: #include <petsc-private/vecimpl.h> /*I "petscvec.h" I*/
9: PetscErrorCode PetscSectionVecView_ASCII(PetscSection s, Vec v, PetscViewer viewer)
10: {
11: PetscScalar *array;
12: PetscInt p, i;
13: PetscMPIInt rank;
17: if (s->atlasLayout.numDof != 1) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot handle %d dof in a uniform section", s->atlasLayout.numDof);
18: MPI_Comm_rank(PetscObjectComm((PetscObject)viewer), &rank);
19: VecGetArray(v, &array);
20: PetscViewerASCIISynchronizedAllow(viewer, PETSC_TRUE);
21: PetscViewerASCIISynchronizedPrintf(viewer, "Process %d:\n", rank);
22: for (p = 0; p < s->atlasLayout.pEnd - s->atlasLayout.pStart; ++p) {
23: if ((s->bc) && (s->bc->atlasDof[p] > 0)) {
24: PetscInt b;
26: PetscViewerASCIISynchronizedPrintf(viewer, " (%4d) dim %2d offset %3d", p+s->atlasLayout.pStart, s->atlasDof[p], s->atlasOff[p]);
27: for (i = s->atlasOff[p]; i < s->atlasOff[p]+s->atlasDof[p]; ++i) {
28: PetscScalar v = array[i];
29: #if defined(PETSC_USE_COMPLEX)
30: if (PetscImaginaryPart(v) > 0.0) {
31: PetscViewerASCIISynchronizedPrintf(viewer," %g + %g i", (double)PetscRealPart(v), (double)PetscImaginaryPart(v));
32: } else if (PetscImaginaryPart(v) < 0.0) {
33: PetscViewerASCIISynchronizedPrintf(viewer," %g - %g i", (double)PetscRealPart(v),(double)(-PetscImaginaryPart(v)));
34: } else {
35: PetscViewerASCIISynchronizedPrintf(viewer, " %g", (double)PetscRealPart(v));
36: }
37: #else
38: PetscViewerASCIISynchronizedPrintf(viewer, " %g", (double)v);
39: #endif
40: }
41: PetscViewerASCIISynchronizedPrintf(viewer, " constrained");
42: for (b = 0; b < s->bc->atlasDof[p]; ++b) {
43: PetscViewerASCIISynchronizedPrintf(viewer, " %d", s->bcIndices[s->bc->atlasOff[p]+b]);
44: }
45: PetscViewerASCIISynchronizedPrintf(viewer, "\n");
46: } else {
47: PetscViewerASCIISynchronizedPrintf(viewer, " (%4d) dim %2d offset %3d", p+s->atlasLayout.pStart, s->atlasDof[p], s->atlasOff[p]);
48: for (i = s->atlasOff[p]; i < s->atlasOff[p]+s->atlasDof[p]; ++i) {
49: PetscScalar v = array[i];
50: #if defined(PETSC_USE_COMPLEX)
51: if (PetscImaginaryPart(v) > 0.0) {
52: PetscViewerASCIISynchronizedPrintf(viewer," %g + %g i", (double)PetscRealPart(v), (double)PetscImaginaryPart(v));
53: } else if (PetscImaginaryPart(v) < 0.0) {
54: PetscViewerASCIISynchronizedPrintf(viewer," %g - %g i", (double)PetscRealPart(v),(double)(-PetscImaginaryPart(v)));
55: } else {
56: PetscViewerASCIISynchronizedPrintf(viewer, " %g", (double)PetscRealPart(v));
57: }
58: #else
59: PetscViewerASCIISynchronizedPrintf(viewer, " %g", (double)v);
60: #endif
61: }
62: PetscViewerASCIISynchronizedPrintf(viewer, "\n");
63: }
64: }
65: PetscViewerFlush(viewer);
66: VecRestoreArray(v, &array);
67: return(0);
68: }
72: PetscErrorCode PetscSectionVecView(PetscSection s, Vec v, PetscViewer viewer)
73: {
74: PetscBool isascii;
75: PetscInt f;
79: if (!viewer) {PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)v), &viewer);}
82: PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &isascii);
83: if (isascii) {
84: const char *name;
86: PetscObjectGetName((PetscObject) v, &name);
87: if (s->numFields) {
88: PetscViewerASCIIPrintf(viewer, "%s with %d fields\n", name, s->numFields);
89: for (f = 0; f < s->numFields; ++f) {
90: PetscViewerASCIIPrintf(viewer, " field %d with %d components\n", f, s->numFieldComponents[f]);
91: PetscSectionVecView_ASCII(s->field[f], v, viewer);
92: }
93: } else {
94: PetscViewerASCIIPrintf(viewer, "%s\n", name);
95: PetscSectionVecView_ASCII(s, v, viewer);
96: }
97: }
98: return(0);
99: }
103: PetscErrorCode VecGetValuesSection(Vec v, PetscSection s, PetscInt point, PetscScalar **values)
104: {
105: PetscScalar *baseArray;
106: const PetscInt p = point - s->atlasLayout.pStart;
110: VecGetArray(v, &baseArray);
111: *values = &baseArray[s->atlasOff[p]];
112: VecRestoreArray(v, &baseArray);
113: return(0);
114: }
118: /*@C
119: VecSetValuesSection - Sets all the values associated with a given point, accoridng to the section, in the given Vec
121: Not collective
123: Input Parameters:
124: + v - the Vec
125: . s - the organizing PetscSection
126: . point - the point
127: . values - the array of input values
128: - mode - the insertion mode, either ADD_VALUES or INSERT_VALUES
130: Level: developer
132: Note: This is similar to MatSetValuesStencil(). The Fortran binding is
133: $
134: $ VecSetValuesSectionF90(vec, section, point, values, mode, ierr)
135: $
137: .seealso: PetscSection, PetscSectionCreate()
138: @*/
139: PetscErrorCode VecSetValuesSection(Vec v, PetscSection s, PetscInt point, PetscScalar values[], InsertMode mode)
140: {
141: PetscScalar *baseArray, *array;
142: const PetscBool doInsert = mode == INSERT_VALUES || mode == INSERT_ALL_VALUES || mode == INSERT_BC_VALUES ? PETSC_TRUE : PETSC_FALSE;
143: const PetscBool doInterior = mode == INSERT_ALL_VALUES || mode == ADD_ALL_VALUES || mode == INSERT_VALUES || mode == ADD_VALUES ? PETSC_TRUE : PETSC_FALSE;
144: const PetscBool doBC = mode == INSERT_ALL_VALUES || mode == ADD_ALL_VALUES || mode == INSERT_BC_VALUES || mode == ADD_BC_VALUES ? PETSC_TRUE : PETSC_FALSE;
145: const PetscInt p = point - s->atlasLayout.pStart;
146: const PetscInt orientation = 0; /* Needs to be included for use in closure operations */
147: PetscInt cDim = 0;
148: PetscErrorCode ierr;
151: PetscSectionGetConstraintDof(s, point, &cDim);
152: VecGetArray(v, &baseArray);
153: array = &baseArray[s->atlasOff[p]];
154: if (!cDim && doInterior) {
155: if (orientation >= 0) {
156: const PetscInt dim = s->atlasDof[p];
157: PetscInt i;
159: if (doInsert) {
160: for (i = 0; i < dim; ++i) array[i] = values[i];
161: } else {
162: for (i = 0; i < dim; ++i) array[i] += values[i];
163: }
164: } else {
165: PetscInt offset = 0;
166: PetscInt j = -1, field, i;
168: for (field = 0; field < s->numFields; ++field) {
169: const PetscInt dim = s->field[field]->atlasDof[p]; /* PetscSectionGetFieldDof() */
171: for (i = dim-1; i >= 0; --i) array[++j] = values[i+offset];
172: offset += dim;
173: }
174: }
175: } else if (cDim) {
176: if (orientation >= 0) {
177: const PetscInt dim = s->atlasDof[p];
178: PetscInt cInd = 0, i;
179: const PetscInt *cDof;
181: PetscSectionGetConstraintIndices(s, point, &cDof);
182: if (doInsert) {
183: for (i = 0; i < dim; ++i) {
184: if ((cInd < cDim) && (i == cDof[cInd])) {
185: if (doBC) array[i] = values[i]; /* Constrained update */
186: ++cInd;
187: continue;
188: }
189: if (doInterior) array[i] = values[i]; /* Unconstrained update */
190: }
191: } else {
192: for (i = 0; i < dim; ++i) {
193: if ((cInd < cDim) && (i == cDof[cInd])) {
194: if (doBC) array[i] += values[i]; /* Constrained update */
195: ++cInd;
196: continue;
197: }
198: if (doInterior) array[i] += values[i]; /* Unconstrained update */
199: }
200: }
201: } else {
202: /* TODO This is broken for add and constrained update */
203: const PetscInt *cDof;
204: PetscInt offset = 0;
205: PetscInt cOffset = 0;
206: PetscInt j = 0, field;
208: PetscSectionGetConstraintIndices(s, point, &cDof);
209: for (field = 0; field < s->numFields; ++field) {
210: const PetscInt dim = s->field[field]->atlasDof[p]; /* PetscSectionGetFieldDof() */
211: const PetscInt tDim = s->field[field]->bc->atlasDof[p]; /* PetscSectionGetFieldConstraintDof() */
212: const PetscInt sDim = dim - tDim;
213: PetscInt cInd = 0, i ,k;
215: for (i = 0, k = dim+offset-1; i < dim; ++i, ++j, --k) {
216: if ((cInd < sDim) && (j == cDof[cInd+cOffset])) {++cInd; continue;}
217: if (doInterior) array[j] = values[k]; /* Unconstrained update */
218: }
219: offset += dim;
220: cOffset += dim - tDim;
221: }
222: }
223: }
224: VecRestoreArray(v, &baseArray);
225: return(0);
226: }