147c6ae99SBarry Smith 247c6ae99SBarry Smith /* 347c6ae99SBarry Smith Code for manipulating distributed regular arrays in parallel. 447c6ae99SBarry Smith */ 547c6ae99SBarry Smith 6af0996ceSBarry Smith #include <petsc/private/dmdaimpl.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 119a42bb27SBarry Smith PetscErrorCode DMView_DA_Matlab(DM da,PetscViewer viewer) 1247c6ae99SBarry Smith { 1347c6ae99SBarry Smith PetscErrorCode ierr; 1447c6ae99SBarry Smith PetscMPIInt rank; 1547c6ae99SBarry Smith PetscInt dim,m,n,p,dof,swidth; 16aa219208SBarry Smith DMDAStencilType stencil; 17bff4a2f0SMatthew G. Knepley DMBoundaryType bx,by,bz; 1847c6ae99SBarry Smith mxArray *mx; 191321219cSEthan Coon const char *fnames[] = {"dimension","m","n","p","dof","stencil_width","bx","by","bz","stencil_type"}; 2047c6ae99SBarry Smith 2147c6ae99SBarry Smith PetscFunctionBegin; 22ce94432eSBarry Smith ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)da),&rank);CHKERRQ(ierr); 2347c6ae99SBarry Smith if (!rank) { 241321219cSEthan Coon ierr = DMDAGetInfo(da,&dim,&m,&n,&p,0,0,0,&dof,&swidth,&bx,&by,&bz,&stencil);CHKERRQ(ierr); 2547c6ae99SBarry Smith mx = mxCreateStructMatrix(1,1,8,(const char**)fnames); 26e3c5b3baSBarry Smith if (!mx) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Unable to generate MATLAB struct array to hold DMDA informations"); 2747c6ae99SBarry Smith mxSetFieldByNumber(mx,0,0,mxCreateDoubleScalar((double)dim)); 2847c6ae99SBarry Smith mxSetFieldByNumber(mx,0,1,mxCreateDoubleScalar((double)m)); 2947c6ae99SBarry Smith mxSetFieldByNumber(mx,0,2,mxCreateDoubleScalar((double)n)); 3047c6ae99SBarry Smith mxSetFieldByNumber(mx,0,3,mxCreateDoubleScalar((double)p)); 3147c6ae99SBarry Smith mxSetFieldByNumber(mx,0,4,mxCreateDoubleScalar((double)dof)); 3247c6ae99SBarry Smith mxSetFieldByNumber(mx,0,5,mxCreateDoubleScalar((double)swidth)); 331321219cSEthan Coon mxSetFieldByNumber(mx,0,6,mxCreateDoubleScalar((double)bx)); 341321219cSEthan Coon mxSetFieldByNumber(mx,0,7,mxCreateDoubleScalar((double)by)); 351321219cSEthan Coon mxSetFieldByNumber(mx,0,8,mxCreateDoubleScalar((double)bz)); 361321219cSEthan Coon mxSetFieldByNumber(mx,0,9,mxCreateDoubleScalar((double)stencil)); 3747c6ae99SBarry Smith ierr = PetscObjectName((PetscObject)da);CHKERRQ(ierr); 3847c6ae99SBarry Smith ierr = PetscViewerMatlabPutVariable(viewer,((PetscObject)da)->name,mx);CHKERRQ(ierr); 3947c6ae99SBarry Smith } 4047c6ae99SBarry Smith PetscFunctionReturn(0); 4147c6ae99SBarry Smith } 4247c6ae99SBarry Smith #endif 4347c6ae99SBarry Smith 449a42bb27SBarry Smith PetscErrorCode DMView_DA_Binary(DM da,PetscViewer viewer) 4547c6ae99SBarry Smith { 4647c6ae99SBarry Smith PetscErrorCode ierr; 4747c6ae99SBarry Smith PetscMPIInt rank; 48b859378eSBarry Smith PetscInt dim,m,n,p,dof,swidth,M,N,P; 49aa219208SBarry Smith DMDAStencilType stencil; 50bff4a2f0SMatthew G. Knepley DMBoundaryType bx,by,bz; 5147c6ae99SBarry Smith MPI_Comm comm; 52bc2bf880SBarry Smith PetscBool coors = PETSC_FALSE; 5347c6ae99SBarry Smith 5447c6ae99SBarry Smith PetscFunctionBegin; 5547c6ae99SBarry Smith ierr = PetscObjectGetComm((PetscObject)da,&comm);CHKERRQ(ierr); 5647c6ae99SBarry Smith 571321219cSEthan Coon ierr = DMDAGetInfo(da,&dim,&m,&n,&p,&M,&N,&P,&dof,&swidth,&bx,&by,&bz,&stencil);CHKERRQ(ierr); 5847c6ae99SBarry Smith ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 5947c6ae99SBarry Smith if (!rank) { 60*f253e43cSLisandro Dalcin ierr = PetscViewerBinaryWrite(viewer,&dim,1,PETSC_INT);CHKERRQ(ierr); 61*f253e43cSLisandro Dalcin ierr = PetscViewerBinaryWrite(viewer,&m,1,PETSC_INT);CHKERRQ(ierr); 62*f253e43cSLisandro Dalcin ierr = PetscViewerBinaryWrite(viewer,&n,1,PETSC_INT);CHKERRQ(ierr); 63*f253e43cSLisandro Dalcin ierr = PetscViewerBinaryWrite(viewer,&p,1,PETSC_INT);CHKERRQ(ierr); 64*f253e43cSLisandro Dalcin ierr = PetscViewerBinaryWrite(viewer,&dof,1,PETSC_INT);CHKERRQ(ierr); 65*f253e43cSLisandro Dalcin ierr = PetscViewerBinaryWrite(viewer,&swidth,1,PETSC_INT);CHKERRQ(ierr); 66*f253e43cSLisandro Dalcin ierr = PetscViewerBinaryWrite(viewer,&bx,1,PETSC_ENUM);CHKERRQ(ierr); 67*f253e43cSLisandro Dalcin ierr = PetscViewerBinaryWrite(viewer,&by,1,PETSC_ENUM);CHKERRQ(ierr); 68*f253e43cSLisandro Dalcin ierr = PetscViewerBinaryWrite(viewer,&bz,1,PETSC_ENUM);CHKERRQ(ierr); 69*f253e43cSLisandro Dalcin ierr = PetscViewerBinaryWrite(viewer,&stencil,1,PETSC_ENUM);CHKERRQ(ierr); 706636e97aSMatthew G Knepley if (da->coordinates) coors = PETSC_TRUE; 71*f253e43cSLisandro Dalcin ierr = PetscViewerBinaryWrite(viewer,&coors,1,PETSC_BOOL);CHKERRQ(ierr); 7247c6ae99SBarry Smith } 7347c6ae99SBarry Smith 7447c6ae99SBarry Smith /* save the coordinates if they exist to disk (in the natural ordering) */ 756636e97aSMatthew G Knepley if (da->coordinates) { 766636e97aSMatthew G Knepley ierr = VecView(da->coordinates,viewer);CHKERRQ(ierr); 7747c6ae99SBarry Smith } 7847c6ae99SBarry Smith PetscFunctionReturn(0); 7947c6ae99SBarry Smith } 8047c6ae99SBarry Smith 819a42bb27SBarry Smith PetscErrorCode DMView_DA_VTK(DM da, PetscViewer viewer) 8247c6ae99SBarry Smith { 8347c6ae99SBarry Smith PetscInt dim, dof, M = 0, N = 0, P = 0; 8447c6ae99SBarry Smith PetscErrorCode ierr; 8547c6ae99SBarry Smith 8647c6ae99SBarry Smith PetscFunctionBegin; 870298fd71SBarry Smith ierr = DMDAGetInfo(da, &dim, &M, &N, &P, NULL, NULL, NULL, &dof, NULL, NULL, NULL, NULL, NULL);CHKERRQ(ierr); 88ce94432eSBarry Smith if (!da->coordinates) SETERRQ(PetscObjectComm((PetscObject)da),PETSC_ERR_SUP, "VTK output requires DMDA coordinates."); 8947c6ae99SBarry Smith /* Write Header */ 9047c6ae99SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"# vtk DataFile Version 2.0\n");CHKERRQ(ierr); 9147c6ae99SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"Structured Mesh Example\n");CHKERRQ(ierr); 9247c6ae99SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"ASCII\n");CHKERRQ(ierr); 9347c6ae99SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"DATASET STRUCTURED_GRID\n");CHKERRQ(ierr); 9447c6ae99SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"DIMENSIONS %d %d %d\n", M, N, P);CHKERRQ(ierr); 9547c6ae99SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"POINTS %d double\n", M*N*P);CHKERRQ(ierr); 966636e97aSMatthew G Knepley if (da->coordinates) { 979a42bb27SBarry Smith DM dac; 9847c6ae99SBarry Smith Vec natural; 9947c6ae99SBarry Smith 1006636e97aSMatthew G Knepley ierr = DMGetCoordinateDM(da, &dac);CHKERRQ(ierr); 101aa219208SBarry Smith ierr = DMDACreateNaturalVector(dac, &natural);CHKERRQ(ierr); 10247c6ae99SBarry Smith ierr = PetscObjectSetOptionsPrefix((PetscObject) natural, "coor_");CHKERRQ(ierr); 1036636e97aSMatthew G Knepley ierr = DMDAGlobalToNaturalBegin(dac, da->coordinates, INSERT_VALUES, natural);CHKERRQ(ierr); 1046636e97aSMatthew G Knepley ierr = DMDAGlobalToNaturalEnd(dac, da->coordinates, INSERT_VALUES, natural);CHKERRQ(ierr); 10547c6ae99SBarry Smith ierr = PetscViewerPushFormat(viewer, PETSC_VIEWER_ASCII_VTK_COORDS);CHKERRQ(ierr); 10647c6ae99SBarry Smith ierr = VecView(natural, viewer);CHKERRQ(ierr); 10747c6ae99SBarry Smith ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr); 108fcfd50ebSBarry Smith ierr = VecDestroy(&natural);CHKERRQ(ierr); 10947c6ae99SBarry Smith } 11047c6ae99SBarry Smith PetscFunctionReturn(0); 11147c6ae99SBarry Smith } 11247c6ae99SBarry Smith 11347c6ae99SBarry Smith /*@C 114aa219208SBarry Smith DMDAGetInfo - Gets information about a given distributed array. 11547c6ae99SBarry Smith 11647c6ae99SBarry Smith Not Collective 11747c6ae99SBarry Smith 11847c6ae99SBarry Smith Input Parameter: 11947c6ae99SBarry Smith . da - the distributed array 12047c6ae99SBarry Smith 12147c6ae99SBarry Smith Output Parameters: 12247c6ae99SBarry Smith + dim - dimension of the distributed array (1, 2, or 3) 12347c6ae99SBarry Smith . M, N, P - global dimension in each direction of the array 12447c6ae99SBarry Smith . m, n, p - corresponding number of procs in each dimension 12547c6ae99SBarry Smith . dof - number of degrees of freedom per node 12647c6ae99SBarry Smith . s - stencil width 127bff4a2f0SMatthew G. Knepley . bx,by,bz - type of ghost nodes at boundary, one of DM_BOUNDARY_NONE, DM_BOUNDARY_GHOSTED, 128bff4a2f0SMatthew G. Knepley DM_BOUNDARY_MIRROR, DM_BOUNDARY_PERIODIC 129aa219208SBarry Smith - st - stencil type, either DMDA_STENCIL_STAR or DMDA_STENCIL_BOX 13047c6ae99SBarry Smith 13147c6ae99SBarry Smith Level: beginner 13247c6ae99SBarry Smith 13347c6ae99SBarry Smith Note: 1340298fd71SBarry Smith Use NULL (NULL_INTEGER in Fortran) in place of any output parameter that is not of interest. 13547c6ae99SBarry Smith 136aa219208SBarry Smith .seealso: DMView(), DMDAGetCorners(), DMDAGetLocalInfo() 13747c6ae99SBarry Smith @*/ 138bff4a2f0SMatthew G. Knepley 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) 13947c6ae99SBarry Smith { 14047c6ae99SBarry Smith DM_DA *dd = (DM_DA*)da->data; 14147c6ae99SBarry Smith 14247c6ae99SBarry Smith PetscFunctionBegin; 143a9a02de4SBarry Smith PetscValidHeaderSpecificType(da,DM_CLASSID,1,DMDA); 144c73cfb54SMatthew G. Knepley if (dim) *dim = da->dim; 145e30e807fSPeter Brune if (M) { 1468865f1eaSKarl Rupp if (dd->Mo < 0) *M = dd->M; 1478865f1eaSKarl Rupp else *M = dd->Mo; 148e30e807fSPeter Brune } 149e30e807fSPeter Brune if (N) { 1508865f1eaSKarl Rupp if (dd->No < 0) *N = dd->N; 1518865f1eaSKarl Rupp else *N = dd->No; 152e30e807fSPeter Brune } 153e30e807fSPeter Brune if (P) { 1548865f1eaSKarl Rupp if (dd->Po < 0) *P = dd->P; 1558865f1eaSKarl Rupp else *P = dd->Po; 156e30e807fSPeter Brune } 15747c6ae99SBarry Smith if (m) *m = dd->m; 15847c6ae99SBarry Smith if (n) *n = dd->n; 15947c6ae99SBarry Smith if (p) *p = dd->p; 16047c6ae99SBarry Smith if (dof) *dof = dd->w; 16147c6ae99SBarry Smith if (s) *s = dd->s; 1621321219cSEthan Coon if (bx) *bx = dd->bx; 1631321219cSEthan Coon if (by) *by = dd->by; 1641321219cSEthan Coon if (bz) *bz = dd->bz; 16547c6ae99SBarry Smith if (st) *st = dd->stencil_type; 16647c6ae99SBarry Smith PetscFunctionReturn(0); 16747c6ae99SBarry Smith } 16847c6ae99SBarry Smith 16947c6ae99SBarry Smith /*@C 170aa219208SBarry Smith DMDAGetLocalInfo - Gets information about a given distributed array and this processors location in it 17147c6ae99SBarry Smith 17247c6ae99SBarry Smith Not Collective 17347c6ae99SBarry Smith 17447c6ae99SBarry Smith Input Parameter: 17547c6ae99SBarry Smith . da - the distributed array 17647c6ae99SBarry Smith 17747c6ae99SBarry Smith Output Parameters: 17847c6ae99SBarry Smith . dainfo - structure containing the information 17947c6ae99SBarry Smith 18047c6ae99SBarry Smith Level: beginner 18147c6ae99SBarry Smith 18295452b02SPatrick Sanan Notes: 18395452b02SPatrick Sanan See DMDALocalInfo for the information that is returned 184d3187782SBarry Smith 18595452b02SPatrick Sanan Fortran Notes: 18695452b02SPatrick Sanan In Fortran the routine is DMDAGetLocalInfoF90(), see DMDALocalInfo for how to access the values 187d3187782SBarry Smith 18895452b02SPatrick Sanan Developer Notes: 18995452b02SPatrick Sanan Not sure why the Fortran function has a F90() in the name since it does not utilize F90 constructs. 190d3187782SBarry Smith 191d3187782SBarry Smith .seealso: DMDAGetInfo(), DMDAGetCorners(), DMDALocalInfo 19247c6ae99SBarry Smith @*/ 1937087cfbeSBarry Smith PetscErrorCode DMDAGetLocalInfo(DM da,DMDALocalInfo *info) 19447c6ae99SBarry Smith { 19547c6ae99SBarry Smith PetscInt w; 19647c6ae99SBarry Smith DM_DA *dd = (DM_DA*)da->data; 19747c6ae99SBarry Smith 19847c6ae99SBarry Smith PetscFunctionBegin; 199a9a02de4SBarry Smith PetscValidHeaderSpecificType(da,DM_CLASSID,1,DMDA); 20047c6ae99SBarry Smith PetscValidPointer(info,2); 20147c6ae99SBarry Smith info->da = da; 202c73cfb54SMatthew G. Knepley info->dim = da->dim; 2038865f1eaSKarl Rupp if (dd->Mo < 0) info->mx = dd->M; 2048865f1eaSKarl Rupp else info->mx = dd->Mo; 2058865f1eaSKarl Rupp if (dd->No < 0) info->my = dd->N; 2068865f1eaSKarl Rupp else info->my = dd->No; 2078865f1eaSKarl Rupp if (dd->Po < 0) info->mz = dd->P; 2088865f1eaSKarl Rupp else info->mz = dd->Po; 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; 219d886c4f4SPeter Brune info->xs = dd->xs/w + dd->xo; 22047c6ae99SBarry Smith info->xm = (dd->xe - dd->xs)/w; 22147c6ae99SBarry Smith /* the y and z have NOT been multiplied by w */ 222d886c4f4SPeter Brune info->ys = dd->ys + dd->yo; 22347c6ae99SBarry Smith info->ym = (dd->ye - dd->ys); 224d886c4f4SPeter Brune info->zs = dd->zs + dd->zo; 22547c6ae99SBarry Smith info->zm = (dd->ze - dd->zs); 22647c6ae99SBarry Smith 227d886c4f4SPeter Brune info->gxs = dd->Xs/w + dd->xo; 22847c6ae99SBarry Smith info->gxm = (dd->Xe - dd->Xs)/w; 22947c6ae99SBarry Smith /* the y and z have NOT been multiplied by w */ 230d886c4f4SPeter Brune info->gys = dd->Ys + dd->yo; 23147c6ae99SBarry Smith info->gym = (dd->Ye - dd->Ys); 232d886c4f4SPeter Brune info->gzs = dd->Zs + dd->zo; 23347c6ae99SBarry Smith info->gzm = (dd->Ze - dd->Zs); 23447c6ae99SBarry Smith PetscFunctionReturn(0); 23547c6ae99SBarry Smith } 236