147c6ae99SBarry Smith #define PETSCDM_DLL 247c6ae99SBarry Smith 347c6ae99SBarry Smith /* 447c6ae99SBarry Smith Code for manipulating distributed regular arrays in parallel. 547c6ae99SBarry Smith */ 647c6ae99SBarry Smith 747c6ae99SBarry Smith #include "private/daimpl.h" /*I "petscda.h" I*/ 847c6ae99SBarry Smith 947c6ae99SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE) 1047c6ae99SBarry Smith #include "mat.h" /* Matlab include file */ 1147c6ae99SBarry Smith 1247c6ae99SBarry Smith #undef __FUNCT__ 13*9a42bb27SBarry Smith #define __FUNCT__ "DMView_DA_Matlab" 14*9a42bb27SBarry Smith PetscErrorCode DMView_DA_Matlab(DM da,PetscViewer viewer) 1547c6ae99SBarry Smith { 1647c6ae99SBarry Smith PetscErrorCode ierr; 1747c6ae99SBarry Smith PetscMPIInt rank; 1847c6ae99SBarry Smith PetscInt dim,m,n,p,dof,swidth; 1947c6ae99SBarry Smith DAStencilType stencil; 2047c6ae99SBarry Smith DAPeriodicType periodic; 2147c6ae99SBarry Smith mxArray *mx; 2247c6ae99SBarry Smith const char *fnames[] = {"dimension","m","n","p","dof","stencil_width","periodicity","stencil_type"}; 2347c6ae99SBarry Smith 2447c6ae99SBarry Smith PetscFunctionBegin; 2547c6ae99SBarry Smith ierr = MPI_Comm_rank(((PetscObject)da)->comm,&rank);CHKERRQ(ierr); 2647c6ae99SBarry Smith if (!rank) { 2747c6ae99SBarry Smith ierr = DAGetInfo(da,&dim,&m,&n,&p,0,0,0,&dof,&swidth,&periodic,&stencil);CHKERRQ(ierr); 2847c6ae99SBarry Smith mx = mxCreateStructMatrix(1,1,8,(const char **)fnames); 2947c6ae99SBarry Smith if (!mx) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Unable to generate Matlab struct array to hold DA informations"); 3047c6ae99SBarry Smith mxSetFieldByNumber(mx,0,0,mxCreateDoubleScalar((double)dim)); 3147c6ae99SBarry Smith mxSetFieldByNumber(mx,0,1,mxCreateDoubleScalar((double)m)); 3247c6ae99SBarry Smith mxSetFieldByNumber(mx,0,2,mxCreateDoubleScalar((double)n)); 3347c6ae99SBarry Smith mxSetFieldByNumber(mx,0,3,mxCreateDoubleScalar((double)p)); 3447c6ae99SBarry Smith mxSetFieldByNumber(mx,0,4,mxCreateDoubleScalar((double)dof)); 3547c6ae99SBarry Smith mxSetFieldByNumber(mx,0,5,mxCreateDoubleScalar((double)swidth)); 3647c6ae99SBarry Smith mxSetFieldByNumber(mx,0,6,mxCreateDoubleScalar((double)periodic)); 3747c6ae99SBarry Smith mxSetFieldByNumber(mx,0,7,mxCreateDoubleScalar((double)stencil)); 3847c6ae99SBarry Smith ierr = PetscObjectName((PetscObject)da);CHKERRQ(ierr); 3947c6ae99SBarry Smith ierr = PetscViewerMatlabPutVariable(viewer,((PetscObject)da)->name,mx);CHKERRQ(ierr); 4047c6ae99SBarry Smith } 4147c6ae99SBarry Smith PetscFunctionReturn(0); 4247c6ae99SBarry Smith } 4347c6ae99SBarry Smith #endif 4447c6ae99SBarry Smith 4547c6ae99SBarry Smith #undef __FUNCT__ 46*9a42bb27SBarry Smith #define __FUNCT__ "DMView_DA_Binary" 47*9a42bb27SBarry Smith PetscErrorCode DMView_DA_Binary(DM da,PetscViewer viewer) 4847c6ae99SBarry Smith { 4947c6ae99SBarry Smith PetscErrorCode ierr; 5047c6ae99SBarry Smith PetscMPIInt rank; 5147c6ae99SBarry Smith PetscInt i,dim,m,n,p,dof,swidth,M,N,P; 5247c6ae99SBarry Smith size_t j,len; 5347c6ae99SBarry Smith DAStencilType stencil; 5447c6ae99SBarry Smith DAPeriodicType periodic; 5547c6ae99SBarry Smith MPI_Comm comm; 5647c6ae99SBarry Smith DM_DA *dd = (DM_DA*)da->data; 5747c6ae99SBarry Smith 5847c6ae99SBarry Smith PetscFunctionBegin; 5947c6ae99SBarry Smith ierr = PetscObjectGetComm((PetscObject)da,&comm);CHKERRQ(ierr); 6047c6ae99SBarry Smith 6147c6ae99SBarry Smith ierr = DAGetInfo(da,&dim,&m,&n,&p,&M,&N,&P,&dof,&swidth,&periodic,&stencil);CHKERRQ(ierr); 6247c6ae99SBarry Smith ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 6347c6ae99SBarry Smith if (!rank) { 6447c6ae99SBarry Smith FILE *file; 6547c6ae99SBarry Smith 6647c6ae99SBarry Smith ierr = PetscViewerBinaryGetInfoPointer(viewer,&file);CHKERRQ(ierr); 6747c6ae99SBarry Smith if (file) { 6847c6ae99SBarry Smith char fieldname[PETSC_MAX_PATH_LEN]; 6947c6ae99SBarry Smith 7047c6ae99SBarry Smith ierr = PetscFPrintf(PETSC_COMM_SELF,file,"-daload_info %D,%D,%D,%D,%D,%D,%D,%D\n",dim,m,n,p,dof,swidth,stencil,periodic);CHKERRQ(ierr); 7147c6ae99SBarry Smith for (i=0; i<dof; i++) { 7247c6ae99SBarry Smith if (dd->fieldname[i]) { 7347c6ae99SBarry Smith ierr = PetscStrncpy(fieldname,dd->fieldname[i],PETSC_MAX_PATH_LEN);CHKERRQ(ierr); 7447c6ae99SBarry Smith ierr = PetscStrlen(fieldname,&len);CHKERRQ(ierr); 7547c6ae99SBarry Smith len = PetscMin(PETSC_MAX_PATH_LEN,len);CHKERRQ(ierr); 7647c6ae99SBarry Smith for (j=0; j<len; j++) { 7747c6ae99SBarry Smith if (fieldname[j] == ' ') fieldname[j] = '_'; 7847c6ae99SBarry Smith } 7947c6ae99SBarry Smith ierr = PetscFPrintf(PETSC_COMM_SELF,file,"-daload_fieldname_%D %s\n",i,fieldname);CHKERRQ(ierr); 8047c6ae99SBarry Smith } 8147c6ae99SBarry Smith } 8247c6ae99SBarry Smith if (dd->coordinates) { /* save the DA's coordinates */ 8347c6ae99SBarry Smith ierr = PetscFPrintf(PETSC_COMM_SELF,file,"-daload_coordinates\n");CHKERRQ(ierr); 8447c6ae99SBarry Smith } 8547c6ae99SBarry Smith } 8647c6ae99SBarry Smith } 8747c6ae99SBarry Smith 8847c6ae99SBarry Smith /* save the coordinates if they exist to disk (in the natural ordering) */ 8947c6ae99SBarry Smith if (dd->coordinates) { 90*9a42bb27SBarry Smith DM dac; 9147c6ae99SBarry Smith const PetscInt *lx,*ly,*lz; 9247c6ae99SBarry Smith Vec natural; 9347c6ae99SBarry Smith 9447c6ae99SBarry Smith /* create the appropriate DA to map to natural ordering */ 9547c6ae99SBarry Smith ierr = DAGetOwnershipRanges(da,&lx,&ly,&lz);CHKERRQ(ierr); 9647c6ae99SBarry Smith if (dim == 1) { 9747c6ae99SBarry Smith ierr = DACreate1d(comm,DA_NONPERIODIC,m,dim,0,lx,&dac);CHKERRQ(ierr); 9847c6ae99SBarry Smith } else if (dim == 2) { 9947c6ae99SBarry Smith ierr = DACreate2d(comm,DA_NONPERIODIC,DA_STENCIL_BOX,m,n,M,N,dim,0,lx,ly,&dac);CHKERRQ(ierr); 10047c6ae99SBarry Smith } else if (dim == 3) { 10147c6ae99SBarry Smith ierr = DACreate3d(comm,DA_NONPERIODIC,DA_STENCIL_BOX,m,n,p,M,N,P,dim,0,lx,ly,lz,&dac);CHKERRQ(ierr); 10247c6ae99SBarry Smith } else { 10347c6ae99SBarry Smith SETERRQ1(comm,PETSC_ERR_ARG_CORRUPT,"Dimension is not 1 2 or 3: %D\n",dim); 10447c6ae99SBarry Smith } 10547c6ae99SBarry Smith ierr = DACreateNaturalVector(dac,&natural);CHKERRQ(ierr); 10647c6ae99SBarry Smith ierr = PetscObjectSetOptionsPrefix((PetscObject)natural,"coor_");CHKERRQ(ierr); 10747c6ae99SBarry Smith ierr = DAGlobalToNaturalBegin(dac,dd->coordinates,INSERT_VALUES,natural);CHKERRQ(ierr); 10847c6ae99SBarry Smith ierr = DAGlobalToNaturalEnd(dac,dd->coordinates,INSERT_VALUES,natural);CHKERRQ(ierr); 10947c6ae99SBarry Smith ierr = VecView(natural,viewer);CHKERRQ(ierr); 11047c6ae99SBarry Smith ierr = VecDestroy(natural);CHKERRQ(ierr); 111*9a42bb27SBarry Smith ierr = DMDestroy(dac);CHKERRQ(ierr); 11247c6ae99SBarry Smith } 11347c6ae99SBarry Smith 11447c6ae99SBarry Smith PetscFunctionReturn(0); 11547c6ae99SBarry Smith } 11647c6ae99SBarry Smith 11747c6ae99SBarry Smith #undef __FUNCT__ 118*9a42bb27SBarry Smith #define __FUNCT__ "DMView_DA_VTK" 119*9a42bb27SBarry Smith PetscErrorCode DMView_DA_VTK(DM da, PetscViewer viewer) 12047c6ae99SBarry Smith { 12147c6ae99SBarry Smith PetscInt dim, dof, M = 0, N = 0, P = 0; 12247c6ae99SBarry Smith PetscErrorCode ierr; 12347c6ae99SBarry Smith DM_DA *dd = (DM_DA*)da->data; 12447c6ae99SBarry Smith 12547c6ae99SBarry Smith PetscFunctionBegin; 12647c6ae99SBarry Smith ierr = DAGetInfo(da, &dim, &M, &N, &P, PETSC_NULL, PETSC_NULL, PETSC_NULL, &dof, PETSC_NULL, PETSC_NULL, PETSC_NULL);CHKERRQ(ierr); 12747c6ae99SBarry Smith /* if (dim != 3) {SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP, "VTK output only works for three dimensional DAs.");} */ 12847c6ae99SBarry Smith if (!dd->coordinates) SETERRQ(((PetscObject)da)->comm,PETSC_ERR_SUP, "VTK output requires DA coordinates."); 12947c6ae99SBarry Smith /* Write Header */ 13047c6ae99SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"# vtk DataFile Version 2.0\n");CHKERRQ(ierr); 13147c6ae99SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"Structured Mesh Example\n");CHKERRQ(ierr); 13247c6ae99SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"ASCII\n");CHKERRQ(ierr); 13347c6ae99SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"DATASET STRUCTURED_GRID\n");CHKERRQ(ierr); 13447c6ae99SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"DIMENSIONS %d %d %d\n", M, N, P);CHKERRQ(ierr); 13547c6ae99SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"POINTS %d double\n", M*N*P);CHKERRQ(ierr); 13647c6ae99SBarry Smith if (dd->coordinates) { 137*9a42bb27SBarry Smith DM dac; 13847c6ae99SBarry Smith Vec natural; 13947c6ae99SBarry Smith 14047c6ae99SBarry Smith ierr = DAGetCoordinateDA(da, &dac);CHKERRQ(ierr); 14147c6ae99SBarry Smith ierr = DACreateNaturalVector(dac, &natural);CHKERRQ(ierr); 14247c6ae99SBarry Smith ierr = PetscObjectSetOptionsPrefix((PetscObject) natural, "coor_");CHKERRQ(ierr); 14347c6ae99SBarry Smith ierr = DAGlobalToNaturalBegin(dac, dd->coordinates, INSERT_VALUES, natural);CHKERRQ(ierr); 14447c6ae99SBarry Smith ierr = DAGlobalToNaturalEnd(dac, dd->coordinates, INSERT_VALUES, natural);CHKERRQ(ierr); 14547c6ae99SBarry Smith ierr = PetscViewerPushFormat(viewer, PETSC_VIEWER_ASCII_VTK_COORDS);CHKERRQ(ierr); 14647c6ae99SBarry Smith ierr = VecView(natural, viewer);CHKERRQ(ierr); 14747c6ae99SBarry Smith ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr); 14847c6ae99SBarry Smith ierr = VecDestroy(natural);CHKERRQ(ierr); 14947c6ae99SBarry Smith } 15047c6ae99SBarry Smith PetscFunctionReturn(0); 15147c6ae99SBarry Smith } 15247c6ae99SBarry Smith 15347c6ae99SBarry Smith #undef __FUNCT__ 15447c6ae99SBarry Smith #define __FUNCT__ "DAGetInfo" 15547c6ae99SBarry Smith /*@C 15647c6ae99SBarry Smith DAGetInfo - Gets information about a given distributed array. 15747c6ae99SBarry Smith 15847c6ae99SBarry Smith Not Collective 15947c6ae99SBarry Smith 16047c6ae99SBarry Smith Input Parameter: 16147c6ae99SBarry Smith . da - the distributed array 16247c6ae99SBarry Smith 16347c6ae99SBarry Smith Output Parameters: 16447c6ae99SBarry Smith + dim - dimension of the distributed array (1, 2, or 3) 16547c6ae99SBarry Smith . M, N, P - global dimension in each direction of the array 16647c6ae99SBarry Smith . m, n, p - corresponding number of procs in each dimension 16747c6ae99SBarry Smith . dof - number of degrees of freedom per node 16847c6ae99SBarry Smith . s - stencil width 16947c6ae99SBarry Smith . wrap - type of periodicity, one of DA_NONPERIODIC, DA_XPERIODIC, DA_YPERIODIC, 17047c6ae99SBarry Smith DA_XYPERIODIC, DA_XYZPERIODIC, DA_XZPERIODIC, DA_YZPERIODIC,DA_ZPERIODIC 17147c6ae99SBarry Smith - st - stencil type, either DA_STENCIL_STAR or DA_STENCIL_BOX 17247c6ae99SBarry Smith 17347c6ae99SBarry Smith Level: beginner 17447c6ae99SBarry Smith 17547c6ae99SBarry Smith Note: 17647c6ae99SBarry Smith Use PETSC_NULL (PETSC_NULL_INTEGER in Fortran) in place of any output parameter that is not of interest. 17747c6ae99SBarry Smith 17847c6ae99SBarry Smith .keywords: distributed array, get, information 17947c6ae99SBarry Smith 180*9a42bb27SBarry Smith .seealso: DMView(), DAGetCorners(), DAGetLocalInfo() 18147c6ae99SBarry Smith @*/ 182*9a42bb27SBarry Smith PetscErrorCode PETSCDM_DLLEXPORT DAGetInfo(DM da,PetscInt *dim,PetscInt *M,PetscInt *N,PetscInt *P,PetscInt *m,PetscInt *n,PetscInt *p,PetscInt *dof,PetscInt *s,DAPeriodicType *wrap,DAStencilType *st) 18347c6ae99SBarry Smith { 18447c6ae99SBarry Smith DM_DA *dd = (DM_DA*)da->data; 18547c6ae99SBarry Smith 18647c6ae99SBarry Smith PetscFunctionBegin; 18747c6ae99SBarry Smith PetscValidHeaderSpecific(da,DM_CLASSID,1); 18847c6ae99SBarry Smith if (dim) *dim = dd->dim; 18947c6ae99SBarry Smith if (M) *M = dd->M; 19047c6ae99SBarry Smith if (N) *N = dd->N; 19147c6ae99SBarry Smith if (P) *P = dd->P; 19247c6ae99SBarry Smith if (m) *m = dd->m; 19347c6ae99SBarry Smith if (n) *n = dd->n; 19447c6ae99SBarry Smith if (p) *p = dd->p; 19547c6ae99SBarry Smith if (dof) *dof = dd->w; 19647c6ae99SBarry Smith if (s) *s = dd->s; 19747c6ae99SBarry Smith if (wrap) *wrap = dd->wrap; 19847c6ae99SBarry Smith if (st) *st = dd->stencil_type; 19947c6ae99SBarry Smith PetscFunctionReturn(0); 20047c6ae99SBarry Smith } 20147c6ae99SBarry Smith 20247c6ae99SBarry Smith #undef __FUNCT__ 20347c6ae99SBarry Smith #define __FUNCT__ "DAGetLocalInfo" 20447c6ae99SBarry Smith /*@C 20547c6ae99SBarry Smith DAGetLocalInfo - Gets information about a given distributed array and this processors location in it 20647c6ae99SBarry Smith 20747c6ae99SBarry Smith Not Collective 20847c6ae99SBarry Smith 20947c6ae99SBarry Smith Input Parameter: 21047c6ae99SBarry Smith . da - the distributed array 21147c6ae99SBarry Smith 21247c6ae99SBarry Smith Output Parameters: 21347c6ae99SBarry Smith . dainfo - structure containing the information 21447c6ae99SBarry Smith 21547c6ae99SBarry Smith Level: beginner 21647c6ae99SBarry Smith 21747c6ae99SBarry Smith .keywords: distributed array, get, information 21847c6ae99SBarry Smith 21947c6ae99SBarry Smith .seealso: DAGetInfo(), DAGetCorners() 22047c6ae99SBarry Smith @*/ 221*9a42bb27SBarry Smith PetscErrorCode PETSCDM_DLLEXPORT DAGetLocalInfo(DM da,DALocalInfo *info) 22247c6ae99SBarry Smith { 22347c6ae99SBarry Smith PetscInt w; 22447c6ae99SBarry Smith DM_DA *dd = (DM_DA*)da->data; 22547c6ae99SBarry Smith 22647c6ae99SBarry Smith PetscFunctionBegin; 22747c6ae99SBarry Smith PetscValidHeaderSpecific(da,DM_CLASSID,1); 22847c6ae99SBarry Smith PetscValidPointer(info,2); 22947c6ae99SBarry Smith info->da = da; 23047c6ae99SBarry Smith info->dim = dd->dim; 23147c6ae99SBarry Smith info->mx = dd->M; 23247c6ae99SBarry Smith info->my = dd->N; 23347c6ae99SBarry Smith info->mz = dd->P; 23447c6ae99SBarry Smith info->dof = dd->w; 23547c6ae99SBarry Smith info->sw = dd->s; 23647c6ae99SBarry Smith info->pt = dd->wrap; 23747c6ae99SBarry Smith info->st = dd->stencil_type; 23847c6ae99SBarry Smith 23947c6ae99SBarry Smith /* since the xs, xe ... have all been multiplied by the number of degrees 24047c6ae99SBarry Smith of freedom per cell, w = dd->w, we divide that out before returning.*/ 24147c6ae99SBarry Smith w = dd->w; 24247c6ae99SBarry Smith info->xs = dd->xs/w; 24347c6ae99SBarry Smith info->xm = (dd->xe - dd->xs)/w; 24447c6ae99SBarry Smith /* the y and z have NOT been multiplied by w */ 24547c6ae99SBarry Smith info->ys = dd->ys; 24647c6ae99SBarry Smith info->ym = (dd->ye - dd->ys); 24747c6ae99SBarry Smith info->zs = dd->zs; 24847c6ae99SBarry Smith info->zm = (dd->ze - dd->zs); 24947c6ae99SBarry Smith 25047c6ae99SBarry Smith info->gxs = dd->Xs/w; 25147c6ae99SBarry Smith info->gxm = (dd->Xe - dd->Xs)/w; 25247c6ae99SBarry Smith /* the y and z have NOT been multiplied by w */ 25347c6ae99SBarry Smith info->gys = dd->Ys; 25447c6ae99SBarry Smith info->gym = (dd->Ye - dd->Ys); 25547c6ae99SBarry Smith info->gzs = dd->Zs; 25647c6ae99SBarry Smith info->gzm = (dd->Ze - dd->Zs); 25747c6ae99SBarry Smith PetscFunctionReturn(0); 25847c6ae99SBarry Smith } 25947c6ae99SBarry Smith 260