Actual source code: daview.c
1: #define PETSCDM_DLL
3: /*
4: Code for manipulating distributed regular arrays in parallel.
5: */
7: #include src/dm/da/daimpl.h
8: #if defined(PETSC_HAVE_PNETCDF)
10: #include "pnetcdf.h"
12: #endif
15: #if defined(PETSC_HAVE_MATLAB_ENGINE)
16: #include "mat.h" /* Matlab include file */
20: PetscErrorCode DAView_Matlab(DA da,PetscViewer viewer)
21: {
23: PetscMPIInt rank;
24: PetscInt dim,m,n,p,dof,swidth;
25: DAStencilType stencil;
26: DAPeriodicType periodic;
27: mxArray *mx;
28: const char *fnames[] = {"dimension","m","n","p","dof","stencil_width","periodicity","stencil_type"};
31: MPI_Comm_rank(da->comm,&rank);
32: if (!rank) {
33: DAGetInfo(da,&dim,&m,&n,&p,0,0,0,&dof,&swidth,&periodic,&stencil);
34: mx = mxCreateStructMatrix(1,1,8,(const char **)fnames);
35: if (!mx) SETERRQ(PETSC_ERR_LIB,"Unable to generate Matlab struct array to hold DA informations");
36: mxSetFieldByNumber(mx,0,0,mxCreateDoubleScalar((double)dim));
37: mxSetFieldByNumber(mx,0,1,mxCreateDoubleScalar((double)m));
38: mxSetFieldByNumber(mx,0,2,mxCreateDoubleScalar((double)n));
39: mxSetFieldByNumber(mx,0,3,mxCreateDoubleScalar((double)p));
40: mxSetFieldByNumber(mx,0,4,mxCreateDoubleScalar((double)dof));
41: mxSetFieldByNumber(mx,0,5,mxCreateDoubleScalar((double)swidth));
42: mxSetFieldByNumber(mx,0,6,mxCreateDoubleScalar((double)periodic));
43: mxSetFieldByNumber(mx,0,7,mxCreateDoubleScalar((double)stencil));
44: PetscObjectName((PetscObject)da);
45: PetscViewerMatlabPutVariable(viewer,da->name,mx);
46: }
47: return(0);
48: }
49: #endif
53: PetscErrorCode DAView_Binary(DA da,PetscViewer viewer)
54: {
56: PetscMPIInt rank;
57: PetscInt i,dim,m,n,p,dof,swidth,M,N,P;
58: size_t j,len;
59: DAStencilType stencil;
60: DAPeriodicType periodic;
61: MPI_Comm comm;
64: PetscObjectGetComm((PetscObject)da,&comm);
66: DAGetInfo(da,&dim,&m,&n,&p,&M,&N,&P,&dof,&swidth,&periodic,&stencil);
67: MPI_Comm_rank(comm,&rank);
68: if (!rank) {
69: FILE *file;
71: PetscViewerBinaryGetInfoPointer(viewer,&file);
72: if (file) {
73: char fieldname[PETSC_MAX_PATH_LEN];
75: PetscFPrintf(PETSC_COMM_SELF,file,"-daload_info %D,%D,%D,%D,%D,%D,%D,%D\n",dim,m,n,p,dof,swidth,stencil,periodic);
76: for (i=0; i<dof; i++) {
77: if (da->fieldname[i]) {
78: PetscStrncpy(fieldname,da->fieldname[i],PETSC_MAX_PATH_LEN);
79: PetscStrlen(fieldname,&len);
80: len = PetscMin(PETSC_MAX_PATH_LEN,len);
81: for (j=0; j<len; j++) {
82: if (fieldname[j] == ' ') fieldname[j] = '_';
83: }
84: PetscFPrintf(PETSC_COMM_SELF,file,"-daload_fieldname_%D %s\n",i,fieldname);
85: }
86: }
87: if (da->coordinates) { /* save the DA's coordinates */
88: PetscFPrintf(PETSC_COMM_SELF,file,"-daload_coordinates\n");
89: }
90: }
91: }
93: /* save the coordinates if they exist to disk (in the natural ordering) */
94: if (da->coordinates) {
95: DA dac;
96: PetscInt *lx,*ly,*lz;
97: Vec natural;
99: /* create the appropriate DA to map to natural ordering */
100: DAGetOwnershipRange(da,&lx,&ly,&lz);
101: if (dim == 1) {
102: DACreate1d(comm,DA_NONPERIODIC,m,dim,0,lx,&dac);
103: } else if (dim == 2) {
104: DACreate2d(comm,DA_NONPERIODIC,DA_STENCIL_BOX,m,n,M,N,dim,0,lx,ly,&dac);
105: } else if (dim == 3) {
106: DACreate3d(comm,DA_NONPERIODIC,DA_STENCIL_BOX,m,n,p,M,N,P,dim,0,lx,ly,lz,&dac);
107: } else {
108: SETERRQ1(PETSC_ERR_ARG_CORRUPT,"Dimension is not 1 2 or 3: %D\n",dim);
109: }
110: DACreateNaturalVector(dac,&natural);
111: PetscObjectSetOptionsPrefix((PetscObject)natural,"coor_");
112: DAGlobalToNaturalBegin(dac,da->coordinates,INSERT_VALUES,natural);
113: DAGlobalToNaturalEnd(dac,da->coordinates,INSERT_VALUES,natural);
114: VecView(natural,viewer);
115: VecDestroy(natural);
116: DADestroy(dac);
117: }
119: return(0);
120: }
124: /*@C
125: DAView - Visualizes a distributed array object.
127: Collective on DA
129: Input Parameters:
130: + da - the distributed array
131: - ptr - an optional visualization context
133: Notes:
134: The available visualization contexts include
135: + PETSC_VIEWER_STDOUT_SELF - standard output (default)
136: . PETSC_VIEWER_STDOUT_WORLD - synchronized standard
137: output where only the first processor opens
138: the file. All other processors send their
139: data to the first processor to print.
140: - PETSC_VIEWER_DRAW_WORLD - to default window
142: The user can open alternative visualization contexts with
143: + PetscViewerASCIIOpen() - Outputs vector to a specified file
144: - PetscViewerDrawOpen() - Outputs vector to an X window display
146: Default Output Format:
147: (for 3d arrays)
148: .vb
149: Processor [proc] M N P m n p w s
150: X range: xs xe, Y range: ys, ye, Z range: zs, ze
152: where
153: M,N,P - global dimension in each direction of the array
154: m,n,p - corresponding number of procs in each dimension
155: w - number of degrees of freedom per node
156: s - stencil width
157: xs, xe - internal local starting/ending grid points
158: in x-direction, (augmented to handle multiple
159: degrees of freedom per node)
160: ys, ye - local starting/ending grid points in y-direction
161: zs, ze - local starting/ending grid points in z-direction
162: .ve
164: Options Database Key:
165: . -da_view - Calls DAView() at the conclusion of DACreate1d(),
166: DACreate2d(), and DACreate3d()
168: Level: beginner
170: Notes:
171: Use DAGetCorners() and DAGetGhostCorners() to get the starting
172: and ending grid points (ghost points) in each direction.
174: When drawing the DA grid it only draws the logical grid and does not
175: respect the grid coordinates set with DASetCoordinates()
177: .keywords: distributed array, view, visualize
179: .seealso: PetscViewerASCIIOpen(), PetscViewerDrawOpen(), DAGetInfo(), DAGetCorners(),
180: DAGetGhostCorners()
181: @*/
182: PetscErrorCode DAView(DA da,PetscViewer viewer)
183: {
185: PetscInt i,dof = da->w;
186: PetscTruth iascii,fieldsnamed = PETSC_FALSE,isbinary;
187: #if defined(PETSC_HAVE_MATLAB_ENGINE)
188: PetscTruth ismatlab;
189: #endif
193: if (!viewer) {
194: PetscViewerASCIIGetStdout(da->comm,&viewer);
195: }
198: PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&iascii);
199: PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_BINARY,&isbinary);
200: #if defined(PETSC_HAVE_MATLAB_ENGINE)
201: PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_MATLAB,&ismatlab);
202: #endif
203: if (iascii) {
204: for (i=0; i<dof; i++) {
205: if (da->fieldname[i]) {
206: fieldsnamed = PETSC_TRUE;
207: break;
208: }
209: }
210: if (fieldsnamed) {
211: PetscViewerASCIIPrintf(viewer,"FieldNames: ");
212: for (i=0; i<dof; i++) {
213: if (da->fieldname[i]) {
214: PetscViewerASCIIPrintf(viewer,"%s ",da->fieldname[i]);
215: } else {
216: PetscViewerASCIIPrintf(viewer,"(not named) ");
217: }
218: }
219: PetscViewerASCIIPrintf(viewer,"\n");
220: }
221: }
222: if (isbinary){
223: DAView_Binary(da,viewer);
224: #if defined(PETSC_HAVE_MATLAB_ENGINE)
225: } else if (ismatlab) {
226: DAView_Matlab(da,viewer);
227: #endif
228: } else {
229: (*da->ops->view)(da,viewer);
230: }
231: return(0);
232: }
236: /*@C
237: DAGetInfo - Gets information about a given distributed array.
239: Not Collective
241: Input Parameter:
242: . da - the distributed array
244: Output Parameters:
245: + dim - dimension of the distributed array (1, 2, or 3)
246: . M, N, P - global dimension in each direction of the array
247: . m, n, p - corresponding number of procs in each dimension
248: . dof - number of degrees of freedom per node
249: . s - stencil width
250: . wrap - type of periodicity, one of DA_NONPERIODIC, DA_XPERIODIC, DA_YPERIODIC,
251: DA_XYPERIODIC, DA_XYZPERIODIC, DA_XZPERIODIC, DA_YZPERIODIC,DA_ZPERIODIC
252: - st - stencil type, either DA_STENCIL_STAR or DA_STENCIL_BOX
254: Level: beginner
255:
256: Note:
257: Use PETSC_NULL (PETSC_NULL_INTEGER in Fortran) in place of any output parameter that is not of interest.
259: .keywords: distributed array, get, information
261: .seealso: DAView(), DAGetCorners(), DAGetLocalInfo()
262: @*/
263: PetscErrorCode DAGetInfo(DA da,PetscInt *dim,PetscInt *M,PetscInt *N,PetscInt *P,PetscInt *m,PetscInt *n,PetscInt *p,PetscInt *dof,PetscInt *s,DAPeriodicType *wrap,DAStencilType *st)
264: {
267: if (dim) *dim = da->dim;
268: if (M) *M = da->M;
269: if (N) *N = da->N;
270: if (P) *P = da->P;
271: if (m) *m = da->m;
272: if (n) *n = da->n;
273: if (p) *p = da->p;
274: if (dof) *dof = da->w;
275: if (s) *s = da->s;
276: if (wrap) *wrap = da->wrap;
277: if (st) *st = da->stencil_type;
278: return(0);
279: }
283: /*@C
284: DAGetLocalInfo - Gets information about a given distributed array and this processors location in it
286: Not Collective
288: Input Parameter:
289: . da - the distributed array
291: Output Parameters:
292: . dainfo - structure containing the information
294: Level: beginner
295:
296: .keywords: distributed array, get, information
298: .seealso: DAGetInfo(), DAGetCorners()
299: @*/
300: PetscErrorCode DAGetLocalInfo(DA da,DALocalInfo *info)
301: {
302: PetscInt w;
307: info->da = da;
308: info->dim = da->dim;
309: info->mx = da->M;
310: info->my = da->N;
311: info->mz = da->P;
312: info->dof = da->w;
313: info->sw = da->s;
314: info->pt = da->wrap;
315: info->st = da->stencil_type;
317: /* since the xs, xe ... have all been multiplied by the number of degrees
318: of freedom per cell, w = da->w, we divide that out before returning.*/
319: w = da->w;
320: info->xs = da->xs/w;
321: info->xm = (da->xe - da->xs)/w;
322: /* the y and z have NOT been multiplied by w */
323: info->ys = da->ys;
324: info->ym = (da->ye - da->ys);
325: info->zs = da->zs;
326: info->zm = (da->ze - da->zs);
328: info->gxs = da->Xs/w;
329: info->gxm = (da->Xe - da->Xs)/w;
330: /* the y and z have NOT been multiplied by w */
331: info->gys = da->Ys;
332: info->gym = (da->Ye - da->Ys);
333: info->gzs = da->Zs;
334: info->gzm = (da->Ze - da->Zs);
335: return(0);
336: }