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