xref: /petsc/src/dm/impls/da/daview.c (revision 030f984af8d8bb4c203755d35bded3c05b3d83ce)
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 {
13   PetscErrorCode   ierr;
14   PetscMPIInt      rank;
15   PetscInt         dim,m,n,p,dof,swidth;
16   DMDAStencilType  stencil;
17   DMBoundaryType   bx,by,bz;
18   mxArray          *mx;
19   const char       *fnames[] = {"dimension","m","n","p","dof","stencil_width","bx","by","bz","stencil_type"};
20 
21   PetscFunctionBegin;
22   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)da),&rank);CHKERRMPI(ierr);
23   if (!rank) {
24     ierr = DMDAGetInfo(da,&dim,&m,&n,&p,0,0,0,&dof,&swidth,&bx,&by,&bz,&stencil);CHKERRQ(ierr);
25     mx   = mxCreateStructMatrix(1,1,8,(const char**)fnames);
26     if (!mx) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Unable to generate MATLAB struct array to hold DMDA informations");
27     mxSetFieldByNumber(mx,0,0,mxCreateDoubleScalar((double)dim));
28     mxSetFieldByNumber(mx,0,1,mxCreateDoubleScalar((double)m));
29     mxSetFieldByNumber(mx,0,2,mxCreateDoubleScalar((double)n));
30     mxSetFieldByNumber(mx,0,3,mxCreateDoubleScalar((double)p));
31     mxSetFieldByNumber(mx,0,4,mxCreateDoubleScalar((double)dof));
32     mxSetFieldByNumber(mx,0,5,mxCreateDoubleScalar((double)swidth));
33     mxSetFieldByNumber(mx,0,6,mxCreateDoubleScalar((double)bx));
34     mxSetFieldByNumber(mx,0,7,mxCreateDoubleScalar((double)by));
35     mxSetFieldByNumber(mx,0,8,mxCreateDoubleScalar((double)bz));
36     mxSetFieldByNumber(mx,0,9,mxCreateDoubleScalar((double)stencil));
37     ierr = PetscObjectName((PetscObject)da);CHKERRQ(ierr);
38     ierr = PetscViewerMatlabPutVariable(viewer,((PetscObject)da)->name,mx);CHKERRQ(ierr);
39   }
40   PetscFunctionReturn(0);
41 }
42 #endif
43 
44 PetscErrorCode DMView_DA_Binary(DM da,PetscViewer viewer)
45 {
46   PetscErrorCode   ierr;
47   PetscMPIInt      rank;
48   PetscInt         dim,m,n,p,dof,swidth,M,N,P;
49   DMDAStencilType  stencil;
50   DMBoundaryType   bx,by,bz;
51   MPI_Comm         comm;
52   PetscBool        coors = PETSC_FALSE;
53 
54   PetscFunctionBegin;
55   ierr = PetscObjectGetComm((PetscObject)da,&comm);CHKERRQ(ierr);
56 
57   ierr = DMDAGetInfo(da,&dim,&m,&n,&p,&M,&N,&P,&dof,&swidth,&bx,&by,&bz,&stencil);CHKERRQ(ierr);
58   ierr = MPI_Comm_rank(comm,&rank);CHKERRMPI(ierr);
59   if (!rank) {
60     ierr = PetscViewerBinaryWrite(viewer,&dim,1,PETSC_INT);CHKERRQ(ierr);
61     ierr = PetscViewerBinaryWrite(viewer,&m,1,PETSC_INT);CHKERRQ(ierr);
62     ierr = PetscViewerBinaryWrite(viewer,&n,1,PETSC_INT);CHKERRQ(ierr);
63     ierr = PetscViewerBinaryWrite(viewer,&p,1,PETSC_INT);CHKERRQ(ierr);
64     ierr = PetscViewerBinaryWrite(viewer,&dof,1,PETSC_INT);CHKERRQ(ierr);
65     ierr = PetscViewerBinaryWrite(viewer,&swidth,1,PETSC_INT);CHKERRQ(ierr);
66     ierr = PetscViewerBinaryWrite(viewer,&bx,1,PETSC_ENUM);CHKERRQ(ierr);
67     ierr = PetscViewerBinaryWrite(viewer,&by,1,PETSC_ENUM);CHKERRQ(ierr);
68     ierr = PetscViewerBinaryWrite(viewer,&bz,1,PETSC_ENUM);CHKERRQ(ierr);
69     ierr = PetscViewerBinaryWrite(viewer,&stencil,1,PETSC_ENUM);CHKERRQ(ierr);
70     if (da->coordinates) coors = PETSC_TRUE;
71     ierr = PetscViewerBinaryWrite(viewer,&coors,1,PETSC_BOOL);CHKERRQ(ierr);
72   }
73 
74   /* save the coordinates if they exist to disk (in the natural ordering) */
75   if (da->coordinates) {
76     ierr = VecView(da->coordinates,viewer);CHKERRQ(ierr);
77   }
78   PetscFunctionReturn(0);
79 }
80 
81 PetscErrorCode DMView_DA_VTK(DM da, PetscViewer viewer)
82 {
83   PetscInt       dim, dof, M = 0, N = 0, P = 0;
84   PetscErrorCode ierr;
85 
86   PetscFunctionBegin;
87   ierr = DMDAGetInfo(da, &dim, &M, &N, &P, NULL, NULL, NULL, &dof, NULL, NULL, NULL, NULL, NULL);CHKERRQ(ierr);
88   if (!da->coordinates) SETERRQ(PetscObjectComm((PetscObject)da),PETSC_ERR_SUP, "VTK output requires DMDA coordinates.");
89   /* Write Header */
90   ierr = PetscViewerASCIIPrintf(viewer,"# vtk DataFile Version 2.0\n");CHKERRQ(ierr);
91   ierr = PetscViewerASCIIPrintf(viewer,"Structured Mesh Example\n");CHKERRQ(ierr);
92   ierr = PetscViewerASCIIPrintf(viewer,"ASCII\n");CHKERRQ(ierr);
93   ierr = PetscViewerASCIIPrintf(viewer,"DATASET STRUCTURED_GRID\n");CHKERRQ(ierr);
94   ierr = PetscViewerASCIIPrintf(viewer,"DIMENSIONS %d %d %d\n", M, N, P);CHKERRQ(ierr);
95   ierr = PetscViewerASCIIPrintf(viewer,"POINTS %d double\n", M*N*P);CHKERRQ(ierr);
96   if (da->coordinates) {
97     DM  dac;
98     Vec natural;
99 
100     ierr = DMGetCoordinateDM(da, &dac);CHKERRQ(ierr);
101     ierr = DMDACreateNaturalVector(dac, &natural);CHKERRQ(ierr);
102     ierr = PetscObjectSetOptionsPrefix((PetscObject) natural, "coor_");CHKERRQ(ierr);
103     ierr = DMDAGlobalToNaturalBegin(dac, da->coordinates, INSERT_VALUES, natural);CHKERRQ(ierr);
104     ierr = DMDAGlobalToNaturalEnd(dac, da->coordinates, INSERT_VALUES, natural);CHKERRQ(ierr);
105     ierr = PetscViewerPushFormat(viewer, PETSC_VIEWER_ASCII_VTK_COORDS_DEPRECATED);CHKERRQ(ierr);
106     ierr = VecView(natural, viewer);CHKERRQ(ierr);
107     ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
108     ierr = VecDestroy(&natural);CHKERRQ(ierr);
109   }
110   PetscFunctionReturn(0);
111 }
112 
113 /*@C
114    DMDAGetInfo - Gets information about a given distributed array.
115 
116    Not Collective
117 
118    Input Parameter:
119 .  da - the distributed array
120 
121    Output Parameters:
122 +  dim      - dimension of the distributed array (1, 2, or 3)
123 .  M, N, P  - global dimension in each direction of the array
124 .  m, n, p  - corresponding number of procs in each dimension
125 .  dof      - number of degrees of freedom per node
126 .  s        - stencil width
127 .  bx,by,bz - type of ghost nodes at boundary, one of DM_BOUNDARY_NONE, DM_BOUNDARY_GHOSTED,
128               DM_BOUNDARY_MIRROR, DM_BOUNDARY_PERIODIC
129 -  st       - stencil type, either DMDA_STENCIL_STAR or DMDA_STENCIL_BOX
130 
131    Level: beginner
132 
133    Note:
134    Use NULL (NULL_INTEGER in Fortran) in place of any output parameter that is not of interest.
135 
136 .seealso: DMView(), DMDAGetCorners(), DMDAGetLocalInfo()
137 @*/
138 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)
139 {
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 {
189   PetscInt w;
190   DM_DA    *dd = (DM_DA*)da->data;
191 
192   PetscFunctionBegin;
193   PetscValidHeaderSpecificType(da,DM_CLASSID,1,DMDA);
194   PetscValidPointer(info,2);
195   info->da  = da;
196   info->dim = da->dim;
197   if (dd->Mo < 0) info->mx = dd->M;
198   else info->mx = dd->Mo;
199   if (dd->No < 0) info->my = dd->N;
200   else info->my = dd->No;
201   if (dd->Po < 0) info->mz = dd->P;
202   else info->mz = dd->Po;
203   info->dof = dd->w;
204   info->sw  = dd->s;
205   info->bx  = dd->bx;
206   info->by  = dd->by;
207   info->bz  = dd->bz;
208   info->st  = dd->stencil_type;
209 
210   /* since the xs, xe ... have all been multiplied by the number of degrees
211      of freedom per cell, w = dd->w, we divide that out before returning.*/
212   w        = dd->w;
213   info->xs = dd->xs/w + dd->xo;
214   info->xm = (dd->xe - dd->xs)/w;
215   /* the y and z have NOT been multiplied by w */
216   info->ys = dd->ys + dd->yo;
217   info->ym = (dd->ye - dd->ys);
218   info->zs = dd->zs + dd->zo;
219   info->zm = (dd->ze - dd->zs);
220 
221   info->gxs = dd->Xs/w + dd->xo;
222   info->gxm = (dd->Xe - dd->Xs)/w;
223   /* the y and z have NOT been multiplied by w */
224   info->gys = dd->Ys + dd->yo;
225   info->gym = (dd->Ye - dd->Ys);
226   info->gzs = dd->Zs + dd->zo;
227   info->gzm = (dd->Ze - dd->Zs);
228   PetscFunctionReturn(0);
229 }
230