1 2 /* 3 Code for manipulating distributed regular arrays in parallel. 4 */ 5 6 #include <petsc/private/dmdaimpl.h> /*I "petscdmda.h" I*/ 7 8 #if defined(PETSC_HAVE_MATLAB_ENGINE) 9 #include <mat.h> /* MATLAB include file */ 10 11 PetscErrorCode DMView_DA_Matlab(DM da, PetscViewer viewer) { 12 PetscMPIInt rank; 13 PetscInt dim, m, n, p, dof, swidth; 14 DMDAStencilType stencil; 15 DMBoundaryType bx, by, bz; 16 mxArray *mx; 17 const char *fnames[] = {"dimension", "m", "n", "p", "dof", "stencil_width", "bx", "by", "bz", "stencil_type"}; 18 19 PetscFunctionBegin; 20 PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)da), &rank)); 21 if (rank == 0) { 22 PetscCall(DMDAGetInfo(da, &dim, &m, &n, &p, 0, 0, 0, &dof, &swidth, &bx, &by, &bz, &stencil)); 23 mx = mxCreateStructMatrix(1, 1, 8, (const char **)fnames); 24 PetscCheck(mx, PETSC_COMM_SELF, PETSC_ERR_LIB, "Unable to generate MATLAB struct array to hold DMDA information"); 25 mxSetFieldByNumber(mx, 0, 0, mxCreateDoubleScalar((double)dim)); 26 mxSetFieldByNumber(mx, 0, 1, mxCreateDoubleScalar((double)m)); 27 mxSetFieldByNumber(mx, 0, 2, mxCreateDoubleScalar((double)n)); 28 mxSetFieldByNumber(mx, 0, 3, mxCreateDoubleScalar((double)p)); 29 mxSetFieldByNumber(mx, 0, 4, mxCreateDoubleScalar((double)dof)); 30 mxSetFieldByNumber(mx, 0, 5, mxCreateDoubleScalar((double)swidth)); 31 mxSetFieldByNumber(mx, 0, 6, mxCreateDoubleScalar((double)bx)); 32 mxSetFieldByNumber(mx, 0, 7, mxCreateDoubleScalar((double)by)); 33 mxSetFieldByNumber(mx, 0, 8, mxCreateDoubleScalar((double)bz)); 34 mxSetFieldByNumber(mx, 0, 9, mxCreateDoubleScalar((double)stencil)); 35 PetscCall(PetscObjectName((PetscObject)da)); 36 PetscCall(PetscViewerMatlabPutVariable(viewer, ((PetscObject)da)->name, mx)); 37 } 38 PetscFunctionReturn(0); 39 } 40 #endif 41 42 PetscErrorCode DMView_DA_Binary(DM da, PetscViewer viewer) { 43 PetscMPIInt rank; 44 PetscInt dim, m, n, p, dof, swidth, M, N, P; 45 DMDAStencilType stencil; 46 DMBoundaryType bx, by, bz; 47 MPI_Comm comm; 48 PetscBool coors = PETSC_FALSE; 49 Vec coordinates; 50 51 PetscFunctionBegin; 52 PetscCall(PetscObjectGetComm((PetscObject)da, &comm)); 53 54 PetscCall(DMDAGetInfo(da, &dim, &m, &n, &p, &M, &N, &P, &dof, &swidth, &bx, &by, &bz, &stencil)); 55 PetscCallMPI(MPI_Comm_rank(comm, &rank)); 56 PetscCall(DMGetCoordinates(da, &coordinates)); 57 if (rank == 0) { 58 PetscCall(PetscViewerBinaryWrite(viewer, &dim, 1, PETSC_INT)); 59 PetscCall(PetscViewerBinaryWrite(viewer, &m, 1, PETSC_INT)); 60 PetscCall(PetscViewerBinaryWrite(viewer, &n, 1, PETSC_INT)); 61 PetscCall(PetscViewerBinaryWrite(viewer, &p, 1, PETSC_INT)); 62 PetscCall(PetscViewerBinaryWrite(viewer, &dof, 1, PETSC_INT)); 63 PetscCall(PetscViewerBinaryWrite(viewer, &swidth, 1, PETSC_INT)); 64 PetscCall(PetscViewerBinaryWrite(viewer, &bx, 1, PETSC_ENUM)); 65 PetscCall(PetscViewerBinaryWrite(viewer, &by, 1, PETSC_ENUM)); 66 PetscCall(PetscViewerBinaryWrite(viewer, &bz, 1, PETSC_ENUM)); 67 PetscCall(PetscViewerBinaryWrite(viewer, &stencil, 1, PETSC_ENUM)); 68 if (coordinates) coors = PETSC_TRUE; 69 PetscCall(PetscViewerBinaryWrite(viewer, &coors, 1, PETSC_BOOL)); 70 } 71 72 /* save the coordinates if they exist to disk (in the natural ordering) */ 73 if (coordinates) PetscCall(VecView(coordinates, viewer)); 74 PetscFunctionReturn(0); 75 } 76 77 PetscErrorCode DMView_DA_VTK(DM da, PetscViewer viewer) { 78 Vec coordinates; 79 PetscInt dim, dof, M = 0, N = 0, P = 0; 80 81 PetscFunctionBegin; 82 PetscCall(DMGetCoordinates(da, &coordinates)); 83 PetscCall(DMDAGetInfo(da, &dim, &M, &N, &P, NULL, NULL, NULL, &dof, NULL, NULL, NULL, NULL, NULL)); 84 PetscCheck(coordinates, PetscObjectComm((PetscObject)da), PETSC_ERR_SUP, "VTK output requires DMDA coordinates."); 85 /* Write Header */ 86 PetscCall(PetscViewerASCIIPrintf(viewer, "# vtk DataFile Version 2.0\n")); 87 PetscCall(PetscViewerASCIIPrintf(viewer, "Structured Mesh Example\n")); 88 PetscCall(PetscViewerASCIIPrintf(viewer, "ASCII\n")); 89 PetscCall(PetscViewerASCIIPrintf(viewer, "DATASET STRUCTURED_GRID\n")); 90 PetscCall(PetscViewerASCIIPrintf(viewer, "DIMENSIONS %" PetscInt_FMT " %" PetscInt_FMT " %" PetscInt_FMT "\n", M, N, P)); 91 PetscCall(PetscViewerASCIIPrintf(viewer, "POINTS %" PetscInt_FMT " double\n", M * N * P)); 92 if (coordinates) { 93 DM dac; 94 Vec natural; 95 96 PetscCall(DMGetCoordinateDM(da, &dac)); 97 PetscCall(DMDACreateNaturalVector(dac, &natural)); 98 PetscCall(PetscObjectSetOptionsPrefix((PetscObject)natural, "coor_")); 99 PetscCall(DMDAGlobalToNaturalBegin(dac, coordinates, INSERT_VALUES, natural)); 100 PetscCall(DMDAGlobalToNaturalEnd(dac, coordinates, INSERT_VALUES, natural)); 101 PetscCall(PetscViewerPushFormat(viewer, PETSC_VIEWER_ASCII_VTK_COORDS_DEPRECATED)); 102 PetscCall(VecView(natural, viewer)); 103 PetscCall(PetscViewerPopFormat(viewer)); 104 PetscCall(VecDestroy(&natural)); 105 } 106 PetscFunctionReturn(0); 107 } 108 109 /*@C 110 DMDAGetInfo - Gets information about a given distributed array. 111 112 Not Collective 113 114 Input Parameter: 115 . da - the distributed array 116 117 Output Parameters: 118 + dim - dimension of the distributed array (1, 2, or 3) 119 . M - global dimension in first direction of the array 120 . N - global dimension in second direction of the array 121 . P - global dimension in third direction of the array 122 . m - corresponding number of procs in first dimension 123 . n - corresponding number of procs in second dimension 124 . p - corresponding number of procs in third dimension 125 . dof - number of degrees of freedom per node 126 . s - stencil width 127 . bx - type of ghost nodes at boundary in first dimension 128 . by - type of ghost nodes at boundary in second dimension 129 . bz - type of ghost nodes at boundary in third dimension 130 - st - stencil type, either DMDA_STENCIL_STAR or DMDA_STENCIL_BOX 131 132 Level: beginner 133 134 Note: 135 Use NULL (NULL_INTEGER in Fortran) in place of any output parameter that is not of interest. 136 137 .seealso: `DMView()`, `DMDAGetCorners()`, `DMDAGetLocalInfo()` 138 @*/ 139 PetscErrorCode DMDAGetInfo(DM da, PetscInt *dim, PetscInt *M, PetscInt *N, PetscInt *P, PetscInt *m, PetscInt *n, PetscInt *p, PetscInt *dof, PetscInt *s, DMBoundaryType *bx, DMBoundaryType *by, DMBoundaryType *bz, DMDAStencilType *st) { 140 DM_DA *dd = (DM_DA *)da->data; 141 142 PetscFunctionBegin; 143 PetscValidHeaderSpecificType(da, DM_CLASSID, 1, DMDA); 144 if (dim) *dim = da->dim; 145 if (M) { 146 if (dd->Mo < 0) *M = dd->M; 147 else *M = dd->Mo; 148 } 149 if (N) { 150 if (dd->No < 0) *N = dd->N; 151 else *N = dd->No; 152 } 153 if (P) { 154 if (dd->Po < 0) *P = dd->P; 155 else *P = dd->Po; 156 } 157 if (m) *m = dd->m; 158 if (n) *n = dd->n; 159 if (p) *p = dd->p; 160 if (dof) *dof = dd->w; 161 if (s) *s = dd->s; 162 if (bx) *bx = dd->bx; 163 if (by) *by = dd->by; 164 if (bz) *bz = dd->bz; 165 if (st) *st = dd->stencil_type; 166 PetscFunctionReturn(0); 167 } 168 169 /*@C 170 DMDAGetLocalInfo - Gets information about a given distributed array and this processors location in it 171 172 Not Collective 173 174 Input Parameter: 175 . da - the distributed array 176 177 Output Parameters: 178 . dainfo - structure containing the information 179 180 Level: beginner 181 182 Notes: 183 See DMDALocalInfo for the information that is returned 184 185 .seealso: `DMDAGetInfo()`, `DMDAGetCorners()`, `DMDALocalInfo` 186 @*/ 187 PetscErrorCode DMDAGetLocalInfo(DM da, DMDALocalInfo *info) { 188 PetscInt w; 189 DM_DA *dd = (DM_DA *)da->data; 190 191 PetscFunctionBegin; 192 PetscValidHeaderSpecificType(da, DM_CLASSID, 1, DMDA); 193 PetscValidPointer(info, 2); 194 info->da = da; 195 info->dim = da->dim; 196 if (dd->Mo < 0) info->mx = dd->M; 197 else info->mx = dd->Mo; 198 if (dd->No < 0) info->my = dd->N; 199 else info->my = dd->No; 200 if (dd->Po < 0) info->mz = dd->P; 201 else info->mz = dd->Po; 202 info->dof = dd->w; 203 info->sw = dd->s; 204 info->bx = dd->bx; 205 info->by = dd->by; 206 info->bz = dd->bz; 207 info->st = dd->stencil_type; 208 209 /* since the xs, xe ... have all been multiplied by the number of degrees 210 of freedom per cell, w = dd->w, we divide that out before returning.*/ 211 w = dd->w; 212 info->xs = dd->xs / w + dd->xo; 213 info->xm = (dd->xe - dd->xs) / w; 214 /* the y and z have NOT been multiplied by w */ 215 info->ys = dd->ys + dd->yo; 216 info->ym = (dd->ye - dd->ys); 217 info->zs = dd->zs + dd->zo; 218 info->zm = (dd->ze - dd->zs); 219 220 info->gxs = dd->Xs / w + dd->xo; 221 info->gxm = (dd->Xe - dd->Xs) / w; 222 /* the y and z have NOT been multiplied by w */ 223 info->gys = dd->Ys + dd->yo; 224 info->gym = (dd->Ye - dd->Ys); 225 info->gzs = dd->Zs + dd->zo; 226 info->gzm = (dd->Ze - dd->Zs); 227 PetscFunctionReturn(0); 228 } 229