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