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 i,dim,m,n,p,dof,swidth,M,N,P; 53 size_t j,len; 54 DMDAStencilType stencil; 55 DMDABoundaryType bx,by,bz; 56 MPI_Comm comm; 57 DM_DA *dd = (DM_DA*)da->data; 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 FILE *file; 66 67 ierr = PetscViewerBinaryGetInfoPointer(viewer,&file);CHKERRQ(ierr); 68 if (file) { 69 char fieldname[PETSC_MAX_PATH_LEN]; 70 71 ierr = PetscFPrintf(PETSC_COMM_SELF,file,"-daload_info %D,%D,%D,%D,%D,%D,%D,%D,%D,%D\n",dim,m,n,p,dof,swidth,stencil,bx,by,bz);CHKERRQ(ierr); 72 for (i=0; i<dof; i++) { 73 if (dd->fieldname[i]) { 74 ierr = PetscStrncpy(fieldname,dd->fieldname[i],PETSC_MAX_PATH_LEN);CHKERRQ(ierr); 75 ierr = PetscStrlen(fieldname,&len);CHKERRQ(ierr); 76 len = PetscMin(PETSC_MAX_PATH_LEN,len);CHKERRQ(ierr); 77 for (j=0; j<len; j++) { 78 if (fieldname[j] == ' ') fieldname[j] = '_'; 79 } 80 ierr = PetscFPrintf(PETSC_COMM_SELF,file,"-daload_fieldname_%D %s\n",i,fieldname);CHKERRQ(ierr); 81 } 82 } 83 if (dd->coordinates) { /* save the DMDA's coordinates */ 84 ierr = PetscFPrintf(PETSC_COMM_SELF,file,"-daload_coordinates\n");CHKERRQ(ierr); 85 } 86 } 87 } 88 89 /* save the coordinates if they exist to disk (in the natural ordering) */ 90 if (dd->coordinates) { 91 DM dac; 92 const PetscInt *lx,*ly,*lz; 93 Vec natural; 94 95 /* create the appropriate DMDA to map to natural ordering */ 96 ierr = DMDAGetOwnershipRanges(da,&lx,&ly,&lz);CHKERRQ(ierr); 97 if (dim == 1) { 98 ierr = DMDACreate1d(comm,DMDA_BOUNDARY_NONE,m,dim,0,lx,&dac);CHKERRQ(ierr); 99 } else if (dim == 2) { 100 ierr = DMDACreate2d(comm,DMDA_BOUNDARY_NONE,DMDA_BOUNDARY_NONE,DMDA_STENCIL_BOX,m,n,M,N,dim,0,lx,ly,&dac);CHKERRQ(ierr); 101 } else if (dim == 3) { 102 ierr = DMDACreate3d(comm,DMDA_BOUNDARY_NONE,DMDA_BOUNDARY_NONE,DMDA_BOUNDARY_NONE,DMDA_STENCIL_BOX,m,n,p,M,N,P,dim,0,lx,ly,lz,&dac);CHKERRQ(ierr); 103 } else SETERRQ1(comm,PETSC_ERR_ARG_CORRUPT,"Dimension is not 1 2 or 3: %D\n",dim); 104 ierr = DMDACreateNaturalVector(dac,&natural);CHKERRQ(ierr); 105 ierr = PetscObjectSetOptionsPrefix((PetscObject)natural,"coor_");CHKERRQ(ierr); 106 ierr = DMDAGlobalToNaturalBegin(dac,dd->coordinates,INSERT_VALUES,natural);CHKERRQ(ierr); 107 ierr = DMDAGlobalToNaturalEnd(dac,dd->coordinates,INSERT_VALUES,natural);CHKERRQ(ierr); 108 ierr = VecView(natural,viewer);CHKERRQ(ierr); 109 ierr = VecDestroy(&natural);CHKERRQ(ierr); 110 ierr = DMDestroy(&dac);CHKERRQ(ierr); 111 } 112 PetscFunctionReturn(0); 113 } 114 115 #undef __FUNCT__ 116 #define __FUNCT__ "DMView_DA_VTK" 117 PetscErrorCode DMView_DA_VTK(DM da, PetscViewer viewer) 118 { 119 PetscInt dim, dof, M = 0, N = 0, P = 0; 120 PetscErrorCode ierr; 121 DM_DA *dd = (DM_DA*)da->data; 122 123 PetscFunctionBegin; 124 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); 125 /* if (dim != 3) {SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP, "VTK output only works for three dimensional DMDAs.");} */ 126 if (!dd->coordinates) SETERRQ(((PetscObject)da)->comm,PETSC_ERR_SUP, "VTK output requires DMDA coordinates."); 127 /* Write Header */ 128 ierr = PetscViewerASCIIPrintf(viewer,"# vtk DataFile Version 2.0\n");CHKERRQ(ierr); 129 ierr = PetscViewerASCIIPrintf(viewer,"Structured Mesh Example\n");CHKERRQ(ierr); 130 ierr = PetscViewerASCIIPrintf(viewer,"ASCII\n");CHKERRQ(ierr); 131 ierr = PetscViewerASCIIPrintf(viewer,"DATASET STRUCTURED_GRID\n");CHKERRQ(ierr); 132 ierr = PetscViewerASCIIPrintf(viewer,"DIMENSIONS %d %d %d\n", M, N, P);CHKERRQ(ierr); 133 ierr = PetscViewerASCIIPrintf(viewer,"POINTS %d double\n", M*N*P);CHKERRQ(ierr); 134 if (dd->coordinates) { 135 DM dac; 136 Vec natural; 137 138 ierr = DMDAGetCoordinateDA(da, &dac);CHKERRQ(ierr); 139 ierr = DMDACreateNaturalVector(dac, &natural);CHKERRQ(ierr); 140 ierr = PetscObjectSetOptionsPrefix((PetscObject) natural, "coor_");CHKERRQ(ierr); 141 ierr = DMDAGlobalToNaturalBegin(dac, dd->coordinates, INSERT_VALUES, natural);CHKERRQ(ierr); 142 ierr = DMDAGlobalToNaturalEnd(dac, dd->coordinates, INSERT_VALUES, natural);CHKERRQ(ierr); 143 ierr = PetscViewerPushFormat(viewer, PETSC_VIEWER_ASCII_VTK_COORDS);CHKERRQ(ierr); 144 ierr = VecView(natural, viewer);CHKERRQ(ierr); 145 ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr); 146 ierr = VecDestroy(&natural);CHKERRQ(ierr); 147 } 148 PetscFunctionReturn(0); 149 } 150 151 #undef __FUNCT__ 152 #define __FUNCT__ "DMDAGetInfo" 153 /*@C 154 DMDAGetInfo - Gets information about a given distributed array. 155 156 Not Collective 157 158 Input Parameter: 159 . da - the distributed array 160 161 Output Parameters: 162 + dim - dimension of the distributed array (1, 2, or 3) 163 . M, N, P - global dimension in each direction of the array 164 . m, n, p - corresponding number of procs in each dimension 165 . dof - number of degrees of freedom per node 166 . s - stencil width 167 . bx,by,bz - type of ghost nodes at boundary, one of DMDA_BOUNDARY_NONE, DMDA_BOUNDARY_GHOSTED, 168 DMDA_BOUNDARY_MIRROR, DMDA_BOUNDARY_PERIODIC 169 - st - stencil type, either DMDA_STENCIL_STAR or DMDA_STENCIL_BOX 170 171 Level: beginner 172 173 Note: 174 Use PETSC_NULL (PETSC_NULL_INTEGER in Fortran) in place of any output parameter that is not of interest. 175 176 .keywords: distributed array, get, information 177 178 .seealso: DMView(), DMDAGetCorners(), DMDAGetLocalInfo() 179 @*/ 180 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) 181 { 182 DM_DA *dd = (DM_DA*)da->data; 183 184 PetscFunctionBegin; 185 PetscValidHeaderSpecific(da,DM_CLASSID,1); 186 if (dim) *dim = dd->dim; 187 if (M) *M = dd->M; 188 if (N) *N = dd->N; 189 if (P) *P = dd->P; 190 if (m) *m = dd->m; 191 if (n) *n = dd->n; 192 if (p) *p = dd->p; 193 if (dof) *dof = dd->w; 194 if (s) *s = dd->s; 195 if (bx) *bx = dd->bx; 196 if (by) *by = dd->by; 197 if (bz) *bz = dd->bz; 198 if (st) *st = dd->stencil_type; 199 PetscFunctionReturn(0); 200 } 201 202 #undef __FUNCT__ 203 #define __FUNCT__ "DMDAGetLocalInfo" 204 /*@C 205 DMDAGetLocalInfo - Gets information about a given distributed array and this processors location in it 206 207 Not Collective 208 209 Input Parameter: 210 . da - the distributed array 211 212 Output Parameters: 213 . dainfo - structure containing the information 214 215 Level: beginner 216 217 .keywords: distributed array, get, information 218 219 .seealso: DMDAGetInfo(), DMDAGetCorners() 220 @*/ 221 PetscErrorCode DMDAGetLocalInfo(DM da,DMDALocalInfo *info) 222 { 223 PetscInt w; 224 DM_DA *dd = (DM_DA*)da->data; 225 226 PetscFunctionBegin; 227 PetscValidHeaderSpecific(da,DM_CLASSID,1); 228 PetscValidPointer(info,2); 229 info->da = da; 230 info->dim = dd->dim; 231 info->mx = dd->M; 232 info->my = dd->N; 233 info->mz = dd->P; 234 info->dof = dd->w; 235 info->sw = dd->s; 236 info->bx = dd->bx; 237 info->by = dd->by; 238 info->bz = dd->bz; 239 info->st = dd->stencil_type; 240 241 /* since the xs, xe ... have all been multiplied by the number of degrees 242 of freedom per cell, w = dd->w, we divide that out before returning.*/ 243 w = dd->w; 244 info->xs = dd->xs/w; 245 info->xm = (dd->xe - dd->xs)/w; 246 /* the y and z have NOT been multiplied by w */ 247 info->ys = dd->ys; 248 info->ym = (dd->ye - dd->ys); 249 info->zs = dd->zs; 250 info->zm = (dd->ze - dd->zs); 251 252 info->gxs = dd->Xs/w; 253 info->gxm = (dd->Xe - dd->Xs)/w; 254 /* the y and z have NOT been multiplied by w */ 255 info->gys = dd->Ys; 256 info->gym = (dd->Ye - dd->Ys); 257 info->gzs = dd->Zs; 258 info->gzm = (dd->Ze - dd->Zs); 259 PetscFunctionReturn(0); 260 } 261 262