xref: /petsc/src/dm/impls/da/daview.c (revision 9371c9d470a9602b6d10a8bf50c9b2280a79e45a)
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 
11*9371c9d4SSatish Balay PetscErrorCode DMView_DA_Matlab(DM da, PetscViewer viewer) {
1247c6ae99SBarry Smith   PetscMPIInt     rank;
1347c6ae99SBarry Smith   PetscInt        dim, m, n, p, dof, swidth;
14aa219208SBarry Smith   DMDAStencilType stencil;
15bff4a2f0SMatthew G. Knepley   DMBoundaryType  bx, by, bz;
1647c6ae99SBarry Smith   mxArray        *mx;
171321219cSEthan Coon   const char     *fnames[] = {"dimension", "m", "n", "p", "dof", "stencil_width", "bx", "by", "bz", "stencil_type"};
1847c6ae99SBarry Smith 
1947c6ae99SBarry Smith   PetscFunctionBegin;
209566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)da), &rank));
21dd400576SPatrick Sanan   if (rank == 0) {
229566063dSJacob Faibussowitsch     PetscCall(DMDAGetInfo(da, &dim, &m, &n, &p, 0, 0, 0, &dof, &swidth, &bx, &by, &bz, &stencil));
2347c6ae99SBarry Smith     mx = mxCreateStructMatrix(1, 1, 8, (const char **)fnames);
247a8be351SBarry Smith     PetscCheck(mx, PETSC_COMM_SELF, PETSC_ERR_LIB, "Unable to generate MATLAB struct array to hold DMDA information");
2547c6ae99SBarry Smith     mxSetFieldByNumber(mx, 0, 0, mxCreateDoubleScalar((double)dim));
2647c6ae99SBarry Smith     mxSetFieldByNumber(mx, 0, 1, mxCreateDoubleScalar((double)m));
2747c6ae99SBarry Smith     mxSetFieldByNumber(mx, 0, 2, mxCreateDoubleScalar((double)n));
2847c6ae99SBarry Smith     mxSetFieldByNumber(mx, 0, 3, mxCreateDoubleScalar((double)p));
2947c6ae99SBarry Smith     mxSetFieldByNumber(mx, 0, 4, mxCreateDoubleScalar((double)dof));
3047c6ae99SBarry Smith     mxSetFieldByNumber(mx, 0, 5, mxCreateDoubleScalar((double)swidth));
311321219cSEthan Coon     mxSetFieldByNumber(mx, 0, 6, mxCreateDoubleScalar((double)bx));
321321219cSEthan Coon     mxSetFieldByNumber(mx, 0, 7, mxCreateDoubleScalar((double)by));
331321219cSEthan Coon     mxSetFieldByNumber(mx, 0, 8, mxCreateDoubleScalar((double)bz));
341321219cSEthan Coon     mxSetFieldByNumber(mx, 0, 9, mxCreateDoubleScalar((double)stencil));
359566063dSJacob Faibussowitsch     PetscCall(PetscObjectName((PetscObject)da));
369566063dSJacob Faibussowitsch     PetscCall(PetscViewerMatlabPutVariable(viewer, ((PetscObject)da)->name, mx));
3747c6ae99SBarry Smith   }
3847c6ae99SBarry Smith   PetscFunctionReturn(0);
3947c6ae99SBarry Smith }
4047c6ae99SBarry Smith #endif
4147c6ae99SBarry Smith 
42*9371c9d4SSatish Balay PetscErrorCode DMView_DA_Binary(DM da, PetscViewer viewer) {
4347c6ae99SBarry Smith   PetscMPIInt     rank;
44b859378eSBarry Smith   PetscInt        dim, m, n, p, dof, swidth, M, N, P;
45aa219208SBarry Smith   DMDAStencilType stencil;
46bff4a2f0SMatthew G. Knepley   DMBoundaryType  bx, by, bz;
4747c6ae99SBarry Smith   MPI_Comm        comm;
48bc2bf880SBarry Smith   PetscBool       coors = PETSC_FALSE;
496858538eSMatthew G. Knepley   Vec             coordinates;
5047c6ae99SBarry Smith 
5147c6ae99SBarry Smith   PetscFunctionBegin;
529566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)da, &comm));
5347c6ae99SBarry Smith 
549566063dSJacob Faibussowitsch   PetscCall(DMDAGetInfo(da, &dim, &m, &n, &p, &M, &N, &P, &dof, &swidth, &bx, &by, &bz, &stencil));
559566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
566858538eSMatthew G. Knepley   PetscCall(DMGetCoordinates(da, &coordinates));
57dd400576SPatrick Sanan   if (rank == 0) {
589566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryWrite(viewer, &dim, 1, PETSC_INT));
599566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryWrite(viewer, &m, 1, PETSC_INT));
609566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryWrite(viewer, &n, 1, PETSC_INT));
619566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryWrite(viewer, &p, 1, PETSC_INT));
629566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryWrite(viewer, &dof, 1, PETSC_INT));
639566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryWrite(viewer, &swidth, 1, PETSC_INT));
649566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryWrite(viewer, &bx, 1, PETSC_ENUM));
659566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryWrite(viewer, &by, 1, PETSC_ENUM));
669566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryWrite(viewer, &bz, 1, PETSC_ENUM));
679566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryWrite(viewer, &stencil, 1, PETSC_ENUM));
686858538eSMatthew G. Knepley     if (coordinates) coors = PETSC_TRUE;
699566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryWrite(viewer, &coors, 1, PETSC_BOOL));
7047c6ae99SBarry Smith   }
7147c6ae99SBarry Smith 
7247c6ae99SBarry Smith   /* save the coordinates if they exist to disk (in the natural ordering) */
736858538eSMatthew G. Knepley   if (coordinates) PetscCall(VecView(coordinates, viewer));
7447c6ae99SBarry Smith   PetscFunctionReturn(0);
7547c6ae99SBarry Smith }
7647c6ae99SBarry Smith 
77*9371c9d4SSatish Balay PetscErrorCode DMView_DA_VTK(DM da, PetscViewer viewer) {
786858538eSMatthew G. Knepley   Vec      coordinates;
7947c6ae99SBarry Smith   PetscInt dim, dof, M = 0, N = 0, P = 0;
8047c6ae99SBarry Smith 
8147c6ae99SBarry Smith   PetscFunctionBegin;
826858538eSMatthew G. Knepley   PetscCall(DMGetCoordinates(da, &coordinates));
839566063dSJacob Faibussowitsch   PetscCall(DMDAGetInfo(da, &dim, &M, &N, &P, NULL, NULL, NULL, &dof, NULL, NULL, NULL, NULL, NULL));
846858538eSMatthew G. Knepley   PetscCheck(coordinates, PetscObjectComm((PetscObject)da), PETSC_ERR_SUP, "VTK output requires DMDA coordinates.");
8547c6ae99SBarry Smith   /* Write Header */
869566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPrintf(viewer, "# vtk DataFile Version 2.0\n"));
879566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPrintf(viewer, "Structured Mesh Example\n"));
889566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPrintf(viewer, "ASCII\n"));
899566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPrintf(viewer, "DATASET STRUCTURED_GRID\n"));
9063a3b9bcSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPrintf(viewer, "DIMENSIONS %" PetscInt_FMT " %" PetscInt_FMT " %" PetscInt_FMT "\n", M, N, P));
9163a3b9bcSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPrintf(viewer, "POINTS %" PetscInt_FMT " double\n", M * N * P));
926858538eSMatthew G. Knepley   if (coordinates) {
939a42bb27SBarry Smith     DM  dac;
9447c6ae99SBarry Smith     Vec natural;
9547c6ae99SBarry Smith 
969566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateDM(da, &dac));
979566063dSJacob Faibussowitsch     PetscCall(DMDACreateNaturalVector(dac, &natural));
989566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)natural, "coor_"));
996858538eSMatthew G. Knepley     PetscCall(DMDAGlobalToNaturalBegin(dac, coordinates, INSERT_VALUES, natural));
1006858538eSMatthew G. Knepley     PetscCall(DMDAGlobalToNaturalEnd(dac, coordinates, INSERT_VALUES, natural));
1019566063dSJacob Faibussowitsch     PetscCall(PetscViewerPushFormat(viewer, PETSC_VIEWER_ASCII_VTK_COORDS_DEPRECATED));
1029566063dSJacob Faibussowitsch     PetscCall(VecView(natural, viewer));
1039566063dSJacob Faibussowitsch     PetscCall(PetscViewerPopFormat(viewer));
1049566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&natural));
10547c6ae99SBarry Smith   }
10647c6ae99SBarry Smith   PetscFunctionReturn(0);
10747c6ae99SBarry Smith }
10847c6ae99SBarry Smith 
10947c6ae99SBarry Smith /*@C
110aa219208SBarry Smith    DMDAGetInfo - Gets information about a given distributed array.
11147c6ae99SBarry Smith 
11247c6ae99SBarry Smith    Not Collective
11347c6ae99SBarry Smith 
11447c6ae99SBarry Smith    Input Parameter:
11547c6ae99SBarry Smith .  da - the distributed array
11647c6ae99SBarry Smith 
11747c6ae99SBarry Smith    Output Parameters:
11847c6ae99SBarry Smith +  dim      - dimension of the distributed array (1, 2, or 3)
1196b867d5aSJose E. Roman .  M        - global dimension in first direction of the array
1206b867d5aSJose E. Roman .  N        - global dimension in second direction of the array
1216b867d5aSJose E. Roman .  P        - global dimension in third direction of the array
1226b867d5aSJose E. Roman .  m        - corresponding number of procs in first dimension
1236b867d5aSJose E. Roman .  n        - corresponding number of procs in second dimension
1246b867d5aSJose E. Roman .  p        - corresponding number of procs in third dimension
12547c6ae99SBarry Smith .  dof      - number of degrees of freedom per node
12647c6ae99SBarry Smith .  s        - stencil width
1276b867d5aSJose E. Roman .  bx       - type of ghost nodes at boundary in first dimension
1286b867d5aSJose E. Roman .  by       - type of ghost nodes at boundary in second dimension
1296b867d5aSJose E. Roman .  bz       - type of ghost nodes at boundary in third dimension
130aa219208SBarry Smith -  st       - stencil type, either DMDA_STENCIL_STAR or DMDA_STENCIL_BOX
13147c6ae99SBarry Smith 
13247c6ae99SBarry Smith    Level: beginner
13347c6ae99SBarry Smith 
13447c6ae99SBarry Smith    Note:
1350298fd71SBarry Smith    Use NULL (NULL_INTEGER in Fortran) in place of any output parameter that is not of interest.
13647c6ae99SBarry Smith 
137db781477SPatrick Sanan .seealso: `DMView()`, `DMDAGetCorners()`, `DMDAGetLocalInfo()`
13847c6ae99SBarry Smith @*/
139*9371c9d4SSatish Balay 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) {
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 
185db781477SPatrick Sanan .seealso: `DMDAGetInfo()`, `DMDAGetCorners()`, `DMDALocalInfo`
18647c6ae99SBarry Smith @*/
187*9371c9d4SSatish Balay PetscErrorCode DMDAGetLocalInfo(DM da, DMDALocalInfo *info) {
18847c6ae99SBarry Smith   PetscInt w;
18947c6ae99SBarry Smith   DM_DA   *dd = (DM_DA *)da->data;
19047c6ae99SBarry Smith 
19147c6ae99SBarry Smith   PetscFunctionBegin;
192a9a02de4SBarry Smith   PetscValidHeaderSpecificType(da, DM_CLASSID, 1, DMDA);
19347c6ae99SBarry Smith   PetscValidPointer(info, 2);
19447c6ae99SBarry Smith   info->da  = da;
195c73cfb54SMatthew G. Knepley   info->dim = da->dim;
1968865f1eaSKarl Rupp   if (dd->Mo < 0) info->mx = dd->M;
1978865f1eaSKarl Rupp   else info->mx = dd->Mo;
1988865f1eaSKarl Rupp   if (dd->No < 0) info->my = dd->N;
1998865f1eaSKarl Rupp   else info->my = dd->No;
2008865f1eaSKarl Rupp   if (dd->Po < 0) info->mz = dd->P;
2018865f1eaSKarl Rupp   else info->mz = dd->Po;
20247c6ae99SBarry Smith   info->dof = dd->w;
20347c6ae99SBarry Smith   info->sw  = dd->s;
2041321219cSEthan Coon   info->bx  = dd->bx;
2051321219cSEthan Coon   info->by  = dd->by;
2061321219cSEthan Coon   info->bz  = dd->bz;
20747c6ae99SBarry Smith   info->st  = dd->stencil_type;
20847c6ae99SBarry Smith 
20947c6ae99SBarry Smith   /* since the xs, xe ... have all been multiplied by the number of degrees
21047c6ae99SBarry Smith      of freedom per cell, w = dd->w, we divide that out before returning.*/
21147c6ae99SBarry Smith   w        = dd->w;
212d886c4f4SPeter Brune   info->xs = dd->xs / w + dd->xo;
21347c6ae99SBarry Smith   info->xm = (dd->xe - dd->xs) / w;
21447c6ae99SBarry Smith   /* the y and z have NOT been multiplied by w */
215d886c4f4SPeter Brune   info->ys = dd->ys + dd->yo;
21647c6ae99SBarry Smith   info->ym = (dd->ye - dd->ys);
217d886c4f4SPeter Brune   info->zs = dd->zs + dd->zo;
21847c6ae99SBarry Smith   info->zm = (dd->ze - dd->zs);
21947c6ae99SBarry Smith 
220d886c4f4SPeter Brune   info->gxs = dd->Xs / w + dd->xo;
22147c6ae99SBarry Smith   info->gxm = (dd->Xe - dd->Xs) / w;
22247c6ae99SBarry Smith   /* the y and z have NOT been multiplied by w */
223d886c4f4SPeter Brune   info->gys = dd->Ys + dd->yo;
22447c6ae99SBarry Smith   info->gym = (dd->Ye - dd->Ys);
225d886c4f4SPeter Brune   info->gzs = dd->Zs + dd->zo;
22647c6ae99SBarry Smith   info->gzm = (dd->Ze - dd->Zs);
22747c6ae99SBarry Smith   PetscFunctionReturn(0);
22847c6ae99SBarry Smith }
229