xref: /petsc/src/dm/impls/da/daview.c (revision b45d2f2cb7e031d9c0de5873eca80614ca7b863b)
147c6ae99SBarry Smith 
247c6ae99SBarry Smith /*
347c6ae99SBarry Smith   Code for manipulating distributed regular arrays in parallel.
447c6ae99SBarry Smith */
547c6ae99SBarry Smith 
6*b45d2f2cSJed Brown #include <petsc-private/daimpl.h>    /*I   "petscdmda.h"   I*/
747c6ae99SBarry Smith 
847c6ae99SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
9c6db04a5SJed Brown #include <mat.h>   /* MATLAB include file */
1047c6ae99SBarry Smith 
1147c6ae99SBarry Smith #undef __FUNCT__
129a42bb27SBarry Smith #define __FUNCT__ "DMView_DA_Matlab"
139a42bb27SBarry Smith PetscErrorCode DMView_DA_Matlab(DM da,PetscViewer viewer)
1447c6ae99SBarry Smith {
1547c6ae99SBarry Smith   PetscErrorCode   ierr;
1647c6ae99SBarry Smith   PetscMPIInt      rank;
1747c6ae99SBarry Smith   PetscInt         dim,m,n,p,dof,swidth;
18aa219208SBarry Smith   DMDAStencilType  stencil;
191321219cSEthan Coon   DMDABoundaryType bx,by,bz;
2047c6ae99SBarry Smith   mxArray          *mx;
211321219cSEthan Coon   const char       *fnames[] = {"dimension","m","n","p","dof","stencil_width","bx","by","bz","stencil_type"};
2247c6ae99SBarry Smith 
2347c6ae99SBarry Smith   PetscFunctionBegin;
2447c6ae99SBarry Smith   ierr = MPI_Comm_rank(((PetscObject)da)->comm,&rank);CHKERRQ(ierr);
2547c6ae99SBarry Smith   if (!rank) {
261321219cSEthan Coon     ierr = DMDAGetInfo(da,&dim,&m,&n,&p,0,0,0,&dof,&swidth,&bx,&by,&bz,&stencil);CHKERRQ(ierr);
2747c6ae99SBarry Smith     mx = mxCreateStructMatrix(1,1,8,(const char **)fnames);
28e3c5b3baSBarry Smith     if (!mx) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Unable to generate MATLAB struct array to hold DMDA informations");
2947c6ae99SBarry Smith     mxSetFieldByNumber(mx,0,0,mxCreateDoubleScalar((double)dim));
3047c6ae99SBarry Smith     mxSetFieldByNumber(mx,0,1,mxCreateDoubleScalar((double)m));
3147c6ae99SBarry Smith     mxSetFieldByNumber(mx,0,2,mxCreateDoubleScalar((double)n));
3247c6ae99SBarry Smith     mxSetFieldByNumber(mx,0,3,mxCreateDoubleScalar((double)p));
3347c6ae99SBarry Smith     mxSetFieldByNumber(mx,0,4,mxCreateDoubleScalar((double)dof));
3447c6ae99SBarry Smith     mxSetFieldByNumber(mx,0,5,mxCreateDoubleScalar((double)swidth));
351321219cSEthan Coon     mxSetFieldByNumber(mx,0,6,mxCreateDoubleScalar((double)bx));
361321219cSEthan Coon     mxSetFieldByNumber(mx,0,7,mxCreateDoubleScalar((double)by));
371321219cSEthan Coon     mxSetFieldByNumber(mx,0,8,mxCreateDoubleScalar((double)bz));
381321219cSEthan Coon     mxSetFieldByNumber(mx,0,9,mxCreateDoubleScalar((double)stencil));
3947c6ae99SBarry Smith     ierr = PetscObjectName((PetscObject)da);CHKERRQ(ierr);
4047c6ae99SBarry Smith     ierr = PetscViewerMatlabPutVariable(viewer,((PetscObject)da)->name,mx);CHKERRQ(ierr);
4147c6ae99SBarry Smith   }
4247c6ae99SBarry Smith   PetscFunctionReturn(0);
4347c6ae99SBarry Smith }
4447c6ae99SBarry Smith #endif
4547c6ae99SBarry Smith 
4647c6ae99SBarry Smith #undef __FUNCT__
479a42bb27SBarry Smith #define __FUNCT__ "DMView_DA_Binary"
489a42bb27SBarry Smith PetscErrorCode DMView_DA_Binary(DM da,PetscViewer viewer)
4947c6ae99SBarry Smith {
5047c6ae99SBarry Smith   PetscErrorCode   ierr;
5147c6ae99SBarry Smith   PetscMPIInt      rank;
52b859378eSBarry Smith   PetscInt         dim,m,n,p,dof,swidth,M,N,P;
53aa219208SBarry Smith   DMDAStencilType  stencil;
541321219cSEthan Coon   DMDABoundaryType bx,by,bz;
5547c6ae99SBarry Smith   MPI_Comm         comm;
5647c6ae99SBarry Smith   DM_DA            *dd = (DM_DA*)da->data;
57b859378eSBarry Smith   PetscInt         classid = DM_FILE_CLASSID,subclassid = DMDA_FILE_CLASSID ;
58bc2bf880SBarry Smith   PetscBool        coors = PETSC_FALSE;
5947c6ae99SBarry Smith 
6047c6ae99SBarry Smith   PetscFunctionBegin;
6147c6ae99SBarry Smith   ierr = PetscObjectGetComm((PetscObject)da,&comm);CHKERRQ(ierr);
6247c6ae99SBarry Smith 
631321219cSEthan Coon   ierr = DMDAGetInfo(da,&dim,&m,&n,&p,&M,&N,&P,&dof,&swidth,&bx,&by,&bz,&stencil);CHKERRQ(ierr);
6447c6ae99SBarry Smith   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
6547c6ae99SBarry Smith   if (!rank) {
6647c6ae99SBarry Smith 
67b859378eSBarry Smith     ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
68b859378eSBarry Smith     ierr = PetscViewerBinaryWrite(viewer,&subclassid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
69b859378eSBarry Smith     ierr = PetscViewerBinaryWrite(viewer,&dim,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
70b859378eSBarry Smith     ierr = PetscViewerBinaryWrite(viewer,&m,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
71b859378eSBarry Smith     ierr = PetscViewerBinaryWrite(viewer,&n,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
72b859378eSBarry Smith     ierr = PetscViewerBinaryWrite(viewer,&p,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
73b859378eSBarry Smith     ierr = PetscViewerBinaryWrite(viewer,&dof,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
74b859378eSBarry Smith     ierr = PetscViewerBinaryWrite(viewer,&swidth,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
75b859378eSBarry Smith     ierr = PetscViewerBinaryWrite(viewer,&bx,1,PETSC_ENUM,PETSC_FALSE);CHKERRQ(ierr);
76b859378eSBarry Smith     ierr = PetscViewerBinaryWrite(viewer,&by,1,PETSC_ENUM,PETSC_FALSE);CHKERRQ(ierr);
77b859378eSBarry Smith     ierr = PetscViewerBinaryWrite(viewer,&bz,1,PETSC_ENUM,PETSC_FALSE);CHKERRQ(ierr);
78b859378eSBarry Smith     ierr = PetscViewerBinaryWrite(viewer,&stencil,1,PETSC_ENUM,PETSC_FALSE);CHKERRQ(ierr);
79bc2bf880SBarry Smith     if (dd->coordinates) coors = PETSC_TRUE;
80bc2bf880SBarry Smith     ierr = PetscViewerBinaryWrite(viewer,&coors,1,PETSC_BOOL,PETSC_FALSE);CHKERRQ(ierr);
8147c6ae99SBarry Smith   }
8247c6ae99SBarry Smith 
8347c6ae99SBarry Smith   /* save the coordinates if they exist to disk (in the natural ordering) */
8447c6ae99SBarry Smith   if (dd->coordinates) {
85b859378eSBarry Smith     ierr = VecView(dd->coordinates,viewer);CHKERRQ(ierr);
8647c6ae99SBarry Smith   }
8747c6ae99SBarry Smith   PetscFunctionReturn(0);
8847c6ae99SBarry Smith }
8947c6ae99SBarry Smith 
9047c6ae99SBarry Smith #undef __FUNCT__
919a42bb27SBarry Smith #define __FUNCT__ "DMView_DA_VTK"
929a42bb27SBarry Smith PetscErrorCode DMView_DA_VTK(DM da, PetscViewer viewer)
9347c6ae99SBarry Smith {
9447c6ae99SBarry Smith   PetscInt       dim, dof, M = 0, N = 0, P = 0;
9547c6ae99SBarry Smith   PetscErrorCode ierr;
9647c6ae99SBarry Smith   DM_DA          *dd = (DM_DA*)da->data;
9747c6ae99SBarry Smith 
9847c6ae99SBarry Smith   PetscFunctionBegin;
991321219cSEthan Coon   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);
100aa219208SBarry Smith   /* if (dim != 3) {SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP, "VTK output only works for three dimensional DMDAs.");} */
101aa219208SBarry Smith   if (!dd->coordinates) SETERRQ(((PetscObject)da)->comm,PETSC_ERR_SUP, "VTK output requires DMDA coordinates.");
10247c6ae99SBarry Smith   /* Write Header */
10347c6ae99SBarry Smith   ierr = PetscViewerASCIIPrintf(viewer,"# vtk DataFile Version 2.0\n");CHKERRQ(ierr);
10447c6ae99SBarry Smith   ierr = PetscViewerASCIIPrintf(viewer,"Structured Mesh Example\n");CHKERRQ(ierr);
10547c6ae99SBarry Smith   ierr = PetscViewerASCIIPrintf(viewer,"ASCII\n");CHKERRQ(ierr);
10647c6ae99SBarry Smith   ierr = PetscViewerASCIIPrintf(viewer,"DATASET STRUCTURED_GRID\n");CHKERRQ(ierr);
10747c6ae99SBarry Smith   ierr = PetscViewerASCIIPrintf(viewer,"DIMENSIONS %d %d %d\n", M, N, P);CHKERRQ(ierr);
10847c6ae99SBarry Smith   ierr = PetscViewerASCIIPrintf(viewer,"POINTS %d double\n", M*N*P);CHKERRQ(ierr);
10947c6ae99SBarry Smith   if (dd->coordinates) {
1109a42bb27SBarry Smith     DM  dac;
11147c6ae99SBarry Smith     Vec natural;
11247c6ae99SBarry Smith 
113aa219208SBarry Smith     ierr = DMDAGetCoordinateDA(da, &dac);CHKERRQ(ierr);
114aa219208SBarry Smith     ierr = DMDACreateNaturalVector(dac, &natural);CHKERRQ(ierr);
11547c6ae99SBarry Smith     ierr = PetscObjectSetOptionsPrefix((PetscObject) natural, "coor_");CHKERRQ(ierr);
116aa219208SBarry Smith     ierr = DMDAGlobalToNaturalBegin(dac, dd->coordinates, INSERT_VALUES, natural);CHKERRQ(ierr);
117aa219208SBarry Smith     ierr = DMDAGlobalToNaturalEnd(dac, dd->coordinates, INSERT_VALUES, natural);CHKERRQ(ierr);
11847c6ae99SBarry Smith     ierr = PetscViewerPushFormat(viewer, PETSC_VIEWER_ASCII_VTK_COORDS);CHKERRQ(ierr);
11947c6ae99SBarry Smith     ierr = VecView(natural, viewer);CHKERRQ(ierr);
12047c6ae99SBarry Smith     ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
121fcfd50ebSBarry Smith     ierr = VecDestroy(&natural);CHKERRQ(ierr);
12247c6ae99SBarry Smith   }
12347c6ae99SBarry Smith   PetscFunctionReturn(0);
12447c6ae99SBarry Smith }
12547c6ae99SBarry Smith 
12647c6ae99SBarry Smith #undef __FUNCT__
127aa219208SBarry Smith #define __FUNCT__ "DMDAGetInfo"
12847c6ae99SBarry Smith /*@C
129aa219208SBarry Smith    DMDAGetInfo - Gets information about a given distributed array.
13047c6ae99SBarry Smith 
13147c6ae99SBarry Smith    Not Collective
13247c6ae99SBarry Smith 
13347c6ae99SBarry Smith    Input Parameter:
13447c6ae99SBarry Smith .  da - the distributed array
13547c6ae99SBarry Smith 
13647c6ae99SBarry Smith    Output Parameters:
13747c6ae99SBarry Smith +  dim      - dimension of the distributed array (1, 2, or 3)
13847c6ae99SBarry Smith .  M, N, P  - global dimension in each direction of the array
13947c6ae99SBarry Smith .  m, n, p  - corresponding number of procs in each dimension
14047c6ae99SBarry Smith .  dof      - number of degrees of freedom per node
14147c6ae99SBarry Smith .  s        - stencil width
1421321219cSEthan Coon .  bx,by,bz - type of ghost nodes at boundary, one of DMDA_BOUNDARY_NONE, DMDA_BOUNDARY_GHOSTED,
1431321219cSEthan Coon               DMDA_BOUNDARY_MIRROR, DMDA_BOUNDARY_PERIODIC
144aa219208SBarry Smith -  st       - stencil type, either DMDA_STENCIL_STAR or DMDA_STENCIL_BOX
14547c6ae99SBarry Smith 
14647c6ae99SBarry Smith    Level: beginner
14747c6ae99SBarry Smith 
14847c6ae99SBarry Smith    Note:
14947c6ae99SBarry Smith    Use PETSC_NULL (PETSC_NULL_INTEGER in Fortran) in place of any output parameter that is not of interest.
15047c6ae99SBarry Smith 
15147c6ae99SBarry Smith .keywords: distributed array, get, information
15247c6ae99SBarry Smith 
153aa219208SBarry Smith .seealso: DMView(), DMDAGetCorners(), DMDAGetLocalInfo()
15447c6ae99SBarry Smith @*/
1551321219cSEthan Coon 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)
15647c6ae99SBarry Smith {
15747c6ae99SBarry Smith   DM_DA *dd = (DM_DA*)da->data;
15847c6ae99SBarry Smith 
15947c6ae99SBarry Smith   PetscFunctionBegin;
16047c6ae99SBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
16147c6ae99SBarry Smith   if (dim)  *dim  = dd->dim;
16247c6ae99SBarry Smith   if (M)    *M    = dd->M;
16347c6ae99SBarry Smith   if (N)    *N    = dd->N;
16447c6ae99SBarry Smith   if (P)    *P    = dd->P;
16547c6ae99SBarry Smith   if (m)    *m    = dd->m;
16647c6ae99SBarry Smith   if (n)    *n    = dd->n;
16747c6ae99SBarry Smith   if (p)    *p    = dd->p;
16847c6ae99SBarry Smith   if (dof)  *dof  = dd->w;
16947c6ae99SBarry Smith   if (s)    *s    = dd->s;
1701321219cSEthan Coon   if (bx) *bx = dd->bx;
1711321219cSEthan Coon   if (by) *by = dd->by;
1721321219cSEthan Coon   if (bz) *bz = dd->bz;
17347c6ae99SBarry Smith   if (st)   *st   = dd->stencil_type;
17447c6ae99SBarry Smith   PetscFunctionReturn(0);
17547c6ae99SBarry Smith }
17647c6ae99SBarry Smith 
17747c6ae99SBarry Smith #undef __FUNCT__
178aa219208SBarry Smith #define __FUNCT__ "DMDAGetLocalInfo"
17947c6ae99SBarry Smith /*@C
180aa219208SBarry Smith    DMDAGetLocalInfo - Gets information about a given distributed array and this processors location in it
18147c6ae99SBarry Smith 
18247c6ae99SBarry Smith    Not Collective
18347c6ae99SBarry Smith 
18447c6ae99SBarry Smith    Input Parameter:
18547c6ae99SBarry Smith .  da - the distributed array
18647c6ae99SBarry Smith 
18747c6ae99SBarry Smith    Output Parameters:
18847c6ae99SBarry Smith .  dainfo - structure containing the information
18947c6ae99SBarry Smith 
19047c6ae99SBarry Smith    Level: beginner
19147c6ae99SBarry Smith 
19247c6ae99SBarry Smith .keywords: distributed array, get, information
19347c6ae99SBarry Smith 
194aa219208SBarry Smith .seealso: DMDAGetInfo(), DMDAGetCorners()
19547c6ae99SBarry Smith @*/
1967087cfbeSBarry Smith PetscErrorCode  DMDAGetLocalInfo(DM da,DMDALocalInfo *info)
19747c6ae99SBarry Smith {
19847c6ae99SBarry Smith   PetscInt w;
19947c6ae99SBarry Smith   DM_DA    *dd = (DM_DA*)da->data;
20047c6ae99SBarry Smith 
20147c6ae99SBarry Smith   PetscFunctionBegin;
20247c6ae99SBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
20347c6ae99SBarry Smith   PetscValidPointer(info,2);
20447c6ae99SBarry Smith   info->da   = da;
20547c6ae99SBarry Smith   info->dim  = dd->dim;
20647c6ae99SBarry Smith   info->mx   = dd->M;
20747c6ae99SBarry Smith   info->my   = dd->N;
20847c6ae99SBarry Smith   info->mz   = dd->P;
20947c6ae99SBarry Smith   info->dof  = dd->w;
21047c6ae99SBarry Smith   info->sw   = dd->s;
2111321219cSEthan Coon   info->bx   = dd->bx;
2121321219cSEthan Coon   info->by   = dd->by;
2131321219cSEthan Coon   info->bz   = dd->bz;
21447c6ae99SBarry Smith   info->st   = dd->stencil_type;
21547c6ae99SBarry Smith 
21647c6ae99SBarry Smith   /* since the xs, xe ... have all been multiplied by the number of degrees
21747c6ae99SBarry Smith      of freedom per cell, w = dd->w, we divide that out before returning.*/
21847c6ae99SBarry Smith   w = dd->w;
21947c6ae99SBarry Smith   info->xs = dd->xs/w;
22047c6ae99SBarry Smith   info->xm = (dd->xe - dd->xs)/w;
22147c6ae99SBarry Smith   /* the y and z have NOT been multiplied by w */
22247c6ae99SBarry Smith   info->ys = dd->ys;
22347c6ae99SBarry Smith   info->ym = (dd->ye - dd->ys);
22447c6ae99SBarry Smith   info->zs = dd->zs;
22547c6ae99SBarry Smith   info->zm = (dd->ze - dd->zs);
22647c6ae99SBarry Smith 
22747c6ae99SBarry Smith   info->gxs = dd->Xs/w;
22847c6ae99SBarry Smith   info->gxm = (dd->Xe - dd->Xs)/w;
22947c6ae99SBarry Smith   /* the y and z have NOT been multiplied by w */
23047c6ae99SBarry Smith   info->gys = dd->Ys;
23147c6ae99SBarry Smith   info->gym = (dd->Ye - dd->Ys);
23247c6ae99SBarry Smith   info->gzs = dd->Zs;
23347c6ae99SBarry Smith   info->gzm = (dd->Ze - dd->Zs);
23447c6ae99SBarry Smith   PetscFunctionReturn(0);
23547c6ae99SBarry Smith }
23647c6ae99SBarry Smith 
237