Actual source code: daview.c


  2: /*
  3:   Code for manipulating distributed regular arrays in parallel.
  4: */

  6: #include <petsc/private/dmdaimpl.h>

  8: #if defined(PETSC_HAVE_MATLAB_ENGINE)
  9: #include <mat.h>   /* MATLAB include file */

 11: PetscErrorCode DMView_DA_Matlab(DM da,PetscViewer viewer)
 12: {
 13:   PetscMPIInt      rank;
 14:   PetscInt         dim,m,n,p,dof,swidth;
 15:   DMDAStencilType  stencil;
 16:   DMBoundaryType   bx,by,bz;
 17:   mxArray          *mx;
 18:   const char       *fnames[] = {"dimension","m","n","p","dof","stencil_width","bx","by","bz","stencil_type"};

 20:   MPI_Comm_rank(PetscObjectComm((PetscObject)da),&rank);
 21:   if (rank == 0) {
 22:     DMDAGetInfo(da,&dim,&m,&n,&p,0,0,0,&dof,&swidth,&bx,&by,&bz,&stencil);
 23:     mx   = mxCreateStructMatrix(1,1,8,(const char**)fnames);
 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:     PetscObjectName((PetscObject)da);
 36:     PetscViewerMatlabPutVariable(viewer,((PetscObject)da)->name,mx);
 37:   }
 38:   return 0;
 39: }
 40: #endif

 42: PetscErrorCode DMView_DA_Binary(DM da,PetscViewer viewer)
 43: {
 44:   PetscMPIInt      rank;
 45:   PetscInt         dim,m,n,p,dof,swidth,M,N,P;
 46:   DMDAStencilType  stencil;
 47:   DMBoundaryType   bx,by,bz;
 48:   MPI_Comm         comm;
 49:   PetscBool        coors = PETSC_FALSE;

 51:   PetscObjectGetComm((PetscObject)da,&comm);

 53:   DMDAGetInfo(da,&dim,&m,&n,&p,&M,&N,&P,&dof,&swidth,&bx,&by,&bz,&stencil);
 54:   MPI_Comm_rank(comm,&rank);
 55:   if (rank == 0) {
 56:     PetscViewerBinaryWrite(viewer,&dim,1,PETSC_INT);
 57:     PetscViewerBinaryWrite(viewer,&m,1,PETSC_INT);
 58:     PetscViewerBinaryWrite(viewer,&n,1,PETSC_INT);
 59:     PetscViewerBinaryWrite(viewer,&p,1,PETSC_INT);
 60:     PetscViewerBinaryWrite(viewer,&dof,1,PETSC_INT);
 61:     PetscViewerBinaryWrite(viewer,&swidth,1,PETSC_INT);
 62:     PetscViewerBinaryWrite(viewer,&bx,1,PETSC_ENUM);
 63:     PetscViewerBinaryWrite(viewer,&by,1,PETSC_ENUM);
 64:     PetscViewerBinaryWrite(viewer,&bz,1,PETSC_ENUM);
 65:     PetscViewerBinaryWrite(viewer,&stencil,1,PETSC_ENUM);
 66:     if (da->coordinates) coors = PETSC_TRUE;
 67:     PetscViewerBinaryWrite(viewer,&coors,1,PETSC_BOOL);
 68:   }

 70:   /* save the coordinates if they exist to disk (in the natural ordering) */
 71:   if (da->coordinates) {
 72:     VecView(da->coordinates,viewer);
 73:   }
 74:   return 0;
 75: }

 77: PetscErrorCode DMView_DA_VTK(DM da, PetscViewer viewer)
 78: {
 79:   PetscInt       dim, dof, M = 0, N = 0, P = 0;

 81:   DMDAGetInfo(da, &dim, &M, &N, &P, NULL, NULL, NULL, &dof, NULL, NULL, NULL, NULL, NULL);
 83:   /* Write Header */
 84:   PetscViewerASCIIPrintf(viewer,"# vtk DataFile Version 2.0\n");
 85:   PetscViewerASCIIPrintf(viewer,"Structured Mesh Example\n");
 86:   PetscViewerASCIIPrintf(viewer,"ASCII\n");
 87:   PetscViewerASCIIPrintf(viewer,"DATASET STRUCTURED_GRID\n");
 88:   PetscViewerASCIIPrintf(viewer,"DIMENSIONS %d %d %d\n", M, N, P);
 89:   PetscViewerASCIIPrintf(viewer,"POINTS %d double\n", M*N*P);
 90:   if (da->coordinates) {
 91:     DM  dac;
 92:     Vec natural;

 94:     DMGetCoordinateDM(da, &dac);
 95:     DMDACreateNaturalVector(dac, &natural);
 96:     PetscObjectSetOptionsPrefix((PetscObject) natural, "coor_");
 97:     DMDAGlobalToNaturalBegin(dac, da->coordinates, INSERT_VALUES, natural);
 98:     DMDAGlobalToNaturalEnd(dac, da->coordinates, INSERT_VALUES, natural);
 99:     PetscViewerPushFormat(viewer, PETSC_VIEWER_ASCII_VTK_COORDS_DEPRECATED);
100:     VecView(natural, viewer);
101:     PetscViewerPopFormat(viewer);
102:     VecDestroy(&natural);
103:   }
104:   return 0;
105: }

107: /*@C
108:    DMDAGetInfo - Gets information about a given distributed array.

110:    Not Collective

112:    Input Parameter:
113: .  da - the distributed array

115:    Output Parameters:
116: +  dim      - dimension of the distributed array (1, 2, or 3)
117: .  M        - global dimension in first direction of the array
118: .  N        - global dimension in second direction of the array
119: .  P        - global dimension in third direction of the array
120: .  m        - corresponding number of procs in first dimension
121: .  n        - corresponding number of procs in second dimension
122: .  p        - corresponding number of procs in third dimension
123: .  dof      - number of degrees of freedom per node
124: .  s        - stencil width
125: .  bx       - type of ghost nodes at boundary in first dimension
126: .  by       - type of ghost nodes at boundary in second dimension
127: .  bz       - type of ghost nodes at boundary in third dimension
128: -  st       - stencil type, either DMDA_STENCIL_STAR or DMDA_STENCIL_BOX

130:    Level: beginner

132:    Note:
133:    Use NULL (NULL_INTEGER in Fortran) in place of any output parameter that is not of interest.

135: .seealso: DMView(), DMDAGetCorners(), DMDAGetLocalInfo()
136: @*/
137: 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)
138: {
139:   DM_DA *dd = (DM_DA*)da->data;

142:   if (dim) *dim = da->dim;
143:   if (M) {
144:     if (dd->Mo < 0) *M = dd->M;
145:     else *M = dd->Mo;
146:   }
147:   if (N) {
148:     if (dd->No < 0) *N = dd->N;
149:     else *N = dd->No;
150:   }
151:   if (P) {
152:     if (dd->Po < 0) *P = dd->P;
153:     else *P = dd->Po;
154:   }
155:   if (m) *m = dd->m;
156:   if (n) *n = dd->n;
157:   if (p) *p = dd->p;
158:   if (dof) *dof = dd->w;
159:   if (s) *s = dd->s;
160:   if (bx) *bx = dd->bx;
161:   if (by) *by = dd->by;
162:   if (bz) *bz = dd->bz;
163:   if (st) *st = dd->stencil_type;
164:   return 0;
165: }

167: /*@C
168:    DMDAGetLocalInfo - Gets information about a given distributed array and this processors location in it

170:    Not Collective

172:    Input Parameter:
173: .  da - the distributed array

175:    Output Parameters:
176: .  dainfo - structure containing the information

178:    Level: beginner

180:    Notes:
181:     See DMDALocalInfo for the information that is returned

183: .seealso: DMDAGetInfo(), DMDAGetCorners(), DMDALocalInfo
184: @*/
185: PetscErrorCode  DMDAGetLocalInfo(DM da,DMDALocalInfo *info)
186: {
187:   PetscInt w;
188:   DM_DA    *dd = (DM_DA*)da->data;

192:   info->da  = da;
193:   info->dim = da->dim;
194:   if (dd->Mo < 0) info->mx = dd->M;
195:   else info->mx = dd->Mo;
196:   if (dd->No < 0) info->my = dd->N;
197:   else info->my = dd->No;
198:   if (dd->Po < 0) info->mz = dd->P;
199:   else info->mz = dd->Po;
200:   info->dof = dd->w;
201:   info->sw  = dd->s;
202:   info->bx  = dd->bx;
203:   info->by  = dd->by;
204:   info->bz  = dd->bz;
205:   info->st  = dd->stencil_type;

207:   /* since the xs, xe ... have all been multiplied by the number of degrees
208:      of freedom per cell, w = dd->w, we divide that out before returning.*/
209:   w        = dd->w;
210:   info->xs = dd->xs/w + dd->xo;
211:   info->xm = (dd->xe - dd->xs)/w;
212:   /* the y and z have NOT been multiplied by w */
213:   info->ys = dd->ys + dd->yo;
214:   info->ym = (dd->ye - dd->ys);
215:   info->zs = dd->zs + dd->zo;
216:   info->zm = (dd->ze - dd->zs);

218:   info->gxs = dd->Xs/w + dd->xo;
219:   info->gxm = (dd->Xe - dd->Xs)/w;
220:   /* the y and z have NOT been multiplied by w */
221:   info->gys = dd->Ys + dd->yo;
222:   info->gym = (dd->Ye - dd->Ys);
223:   info->gzs = dd->Zs + dd->zo;
224:   info->gzm = (dd->Ze - dd->Zs);
225:   return 0;
226: }