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*/ 7b2fff234SMatthew G. Knepley #include <petscbt.h> 80c312b8eSJed Brown #include <petscsf.h> 92764a2aaSMatthew G. Knepley #include <petscds.h> 109a800dd8SMatthew G. Knepley #include <petscfe.h> 1147c6ae99SBarry Smith 1247c6ae99SBarry Smith /* 13e3c5b3baSBarry Smith This allows the DMDA vectors to properly tell MATLAB their dimensions 1447c6ae99SBarry Smith */ 1547c6ae99SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE) 16c6db04a5SJed Brown #include <engine.h> /* MATLAB include file */ 17c6db04a5SJed Brown #include <mex.h> /* MATLAB include file */ 181e6b0712SBarry Smith static PetscErrorCode VecMatlabEnginePut_DA2d(PetscObject obj,void *mengine) 1947c6ae99SBarry Smith { 2047c6ae99SBarry Smith PetscErrorCode ierr; 2147c6ae99SBarry Smith PetscInt n,m; 2247c6ae99SBarry Smith Vec vec = (Vec)obj; 2347c6ae99SBarry Smith PetscScalar *array; 2447c6ae99SBarry Smith mxArray *mat; 259a42bb27SBarry Smith DM da; 2647c6ae99SBarry Smith 2747c6ae99SBarry Smith PetscFunctionBegin; 28c688c046SMatthew G Knepley ierr = VecGetDM(vec, &da);CHKERRQ(ierr); 29ce94432eSBarry Smith if (!da) SETERRQ(PetscObjectComm((PetscObject)vec),PETSC_ERR_ARG_WRONGSTATE,"Vector not associated with a DMDA"); 30aa219208SBarry Smith ierr = DMDAGetGhostCorners(da,0,0,0,&m,&n,0);CHKERRQ(ierr); 3147c6ae99SBarry Smith 3247c6ae99SBarry Smith ierr = VecGetArray(vec,&array);CHKERRQ(ierr); 3347c6ae99SBarry Smith #if !defined(PETSC_USE_COMPLEX) 3447c6ae99SBarry Smith mat = mxCreateDoubleMatrix(m,n,mxREAL); 3547c6ae99SBarry Smith #else 3647c6ae99SBarry Smith mat = mxCreateDoubleMatrix(m,n,mxCOMPLEX); 3747c6ae99SBarry Smith #endif 38580bdb30SBarry Smith ierr = PetscArraycpy(mxGetPr(mat),array,n*m);CHKERRQ(ierr); 3947c6ae99SBarry Smith ierr = PetscObjectName(obj);CHKERRQ(ierr); 4047c6ae99SBarry Smith engPutVariable((Engine*)mengine,obj->name,mat); 4147c6ae99SBarry Smith 4247c6ae99SBarry Smith ierr = VecRestoreArray(vec,&array);CHKERRQ(ierr); 4347c6ae99SBarry Smith PetscFunctionReturn(0); 4447c6ae99SBarry Smith } 4547c6ae99SBarry Smith #endif 4647c6ae99SBarry Smith 477087cfbeSBarry Smith PetscErrorCode DMCreateLocalVector_DA(DM da,Vec *g) 4847c6ae99SBarry Smith { 4947c6ae99SBarry Smith PetscErrorCode ierr; 5047c6ae99SBarry Smith DM_DA *dd = (DM_DA*)da->data; 5147c6ae99SBarry Smith 5247c6ae99SBarry Smith PetscFunctionBegin; 5347c6ae99SBarry Smith PetscValidHeaderSpecific(da,DM_CLASSID,1); 5447c6ae99SBarry Smith PetscValidPointer(g,2); 5547c6ae99SBarry Smith ierr = VecCreate(PETSC_COMM_SELF,g);CHKERRQ(ierr); 5647c6ae99SBarry Smith ierr = VecSetSizes(*g,dd->nlocal,PETSC_DETERMINE);CHKERRQ(ierr); 5747c6ae99SBarry Smith ierr = VecSetBlockSize(*g,dd->w);CHKERRQ(ierr); 58401ddaa8SBarry Smith ierr = VecSetType(*g,da->vectype);CHKERRQ(ierr); 59c688c046SMatthew G Knepley ierr = VecSetDM(*g, da);CHKERRQ(ierr); 6047c6ae99SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE) 617bc9226cSBarry Smith if (dd->w == 1 && da->dim == 2) { 62bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)*g,"PetscMatlabEnginePut_C",VecMatlabEnginePut_DA2d);CHKERRQ(ierr); 6347c6ae99SBarry Smith } 6447c6ae99SBarry Smith #endif 6547c6ae99SBarry Smith PetscFunctionReturn(0); 6647c6ae99SBarry Smith } 6747c6ae99SBarry Smith 68939f6067SMatthew G. Knepley /*@ 69939f6067SMatthew G. Knepley DMDAGetNumCells - Get the number of cells in the local piece of the DMDA. This includes ghost cells. 70939f6067SMatthew G. Knepley 71939f6067SMatthew G. Knepley Input Parameter: 72939f6067SMatthew G. Knepley . dm - The DM object 73939f6067SMatthew G. Knepley 74939f6067SMatthew G. Knepley Output Parameters: 75939f6067SMatthew G. Knepley + numCellsX - The number of local cells in the x-direction 76939f6067SMatthew G. Knepley . numCellsY - The number of local cells in the y-direction 77939f6067SMatthew G. Knepley . numCellsZ - The number of local cells in the z-direction 78939f6067SMatthew G. Knepley - numCells - The number of local cells 79939f6067SMatthew G. Knepley 80939f6067SMatthew G. Knepley Level: developer 81939f6067SMatthew G. Knepley 82939f6067SMatthew G. Knepley .seealso: DMDAGetCellPoint() 83939f6067SMatthew G. Knepley @*/ 843582350cSMatthew G. Knepley PetscErrorCode DMDAGetNumCells(DM dm, PetscInt *numCellsX, PetscInt *numCellsY, PetscInt *numCellsZ, PetscInt *numCells) 8557459e9aSMatthew G Knepley { 8657459e9aSMatthew G Knepley DM_DA *da = (DM_DA*) dm->data; 87c73cfb54SMatthew G. Knepley const PetscInt dim = dm->dim; 8857459e9aSMatthew G Knepley const PetscInt mx = (da->Xe - da->Xs)/da->w, my = da->Ye - da->Ys, mz = da->Ze - da->Zs; 8957459e9aSMatthew G Knepley const PetscInt nC = (mx)*(dim > 1 ? (my)*(dim > 2 ? (mz) : 1) : 1); 9057459e9aSMatthew G Knepley 9157459e9aSMatthew G Knepley PetscFunctionBegin; 92a9a02de4SBarry Smith PetscValidHeaderSpecificType(dm, DM_CLASSID, 1,DMDA); 933582350cSMatthew G. Knepley if (numCellsX) { 943582350cSMatthew G. Knepley PetscValidIntPointer(numCellsX,2); 953582350cSMatthew G. Knepley *numCellsX = mx; 963582350cSMatthew G. Knepley } 973582350cSMatthew G. Knepley if (numCellsY) { 988135c375SStefano Zampini PetscValidIntPointer(numCellsY,3); 993582350cSMatthew G. Knepley *numCellsY = my; 1003582350cSMatthew G. Knepley } 1013582350cSMatthew G. Knepley if (numCellsZ) { 1028135c375SStefano Zampini PetscValidIntPointer(numCellsZ,4); 1033582350cSMatthew G. Knepley *numCellsZ = mz; 1043582350cSMatthew G. Knepley } 10557459e9aSMatthew G Knepley if (numCells) { 1063582350cSMatthew G. Knepley PetscValidIntPointer(numCells,5); 10757459e9aSMatthew G Knepley *numCells = nC; 10857459e9aSMatthew G Knepley } 10957459e9aSMatthew G Knepley PetscFunctionReturn(0); 11057459e9aSMatthew G Knepley } 11157459e9aSMatthew G Knepley 112d6401b93SMatthew G. Knepley /*@ 113d6401b93SMatthew G. Knepley DMDAGetCellPoint - Get the DM point corresponding to the tuple (i, j, k) in the DMDA 114d6401b93SMatthew G. Knepley 115d6401b93SMatthew G. Knepley Input Parameters: 116d6401b93SMatthew G. Knepley + dm - The DM object 117d6401b93SMatthew G. Knepley - i,j,k - The global indices for the cell 118d6401b93SMatthew G. Knepley 119d6401b93SMatthew G. Knepley Output Parameters: 120d6401b93SMatthew G. Knepley . point - The local DM point 121d6401b93SMatthew G. Knepley 122d6401b93SMatthew G. Knepley Level: developer 123d6401b93SMatthew G. Knepley 124d6401b93SMatthew G. Knepley .seealso: DMDAGetNumCells() 125d6401b93SMatthew G. Knepley @*/ 126d6401b93SMatthew G. Knepley PetscErrorCode DMDAGetCellPoint(DM dm, PetscInt i, PetscInt j, PetscInt k, PetscInt *point) 127d6401b93SMatthew G. Knepley { 128c73cfb54SMatthew G. Knepley const PetscInt dim = dm->dim; 129d6401b93SMatthew G. Knepley DMDALocalInfo info; 130d6401b93SMatthew G. Knepley PetscErrorCode ierr; 131d6401b93SMatthew G. Knepley 132d6401b93SMatthew G. Knepley PetscFunctionBegin; 133a9a02de4SBarry Smith PetscValidHeaderSpecificType(dm, DM_CLASSID, 1,DMDA); 134d6401b93SMatthew G. Knepley PetscValidIntPointer(point,5); 135d6401b93SMatthew G. Knepley ierr = DMDAGetLocalInfo(dm, &info);CHKERRQ(ierr); 136d6401b93SMatthew G. Knepley if (dim > 0) {if ((i < info.gxs) || (i >= info.gxs+info.gxm)) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "X index %d not in [%d, %d)", i, info.gxs, info.gxs+info.gxm);} 13763b6bffdSJed Brown if (dim > 1) {if ((j < info.gys) || (j >= info.gys+info.gym)) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Y index %d not in [%d, %d)", j, info.gys, info.gys+info.gym);} 13863b6bffdSJed Brown if (dim > 2) {if ((k < info.gzs) || (k >= info.gzs+info.gzm)) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Z index %d not in [%d, %d)", k, info.gzs, info.gzs+info.gzm);} 139d6401b93SMatthew G. Knepley *point = i + (dim > 1 ? (j + (dim > 2 ? k*info.gym : 0))*info.gxm : 0); 140d6401b93SMatthew G. Knepley PetscFunctionReturn(0); 141d6401b93SMatthew G. Knepley } 142d6401b93SMatthew G. Knepley 14357459e9aSMatthew G Knepley PetscErrorCode DMDAGetNumVertices(DM dm, PetscInt *numVerticesX, PetscInt *numVerticesY, PetscInt *numVerticesZ, PetscInt *numVertices) 14457459e9aSMatthew G Knepley { 14557459e9aSMatthew G Knepley DM_DA *da = (DM_DA*) dm->data; 146c73cfb54SMatthew G. Knepley const PetscInt dim = dm->dim; 14757459e9aSMatthew G Knepley const PetscInt mx = (da->Xe - da->Xs)/da->w, my = da->Ye - da->Ys, mz = da->Ze - da->Zs; 14857459e9aSMatthew G Knepley const PetscInt nVx = mx+1; 14957459e9aSMatthew G Knepley const PetscInt nVy = dim > 1 ? (my+1) : 1; 15057459e9aSMatthew G Knepley const PetscInt nVz = dim > 2 ? (mz+1) : 1; 15157459e9aSMatthew G Knepley const PetscInt nV = nVx*nVy*nVz; 15257459e9aSMatthew G Knepley 15357459e9aSMatthew G Knepley PetscFunctionBegin; 15457459e9aSMatthew G Knepley if (numVerticesX) { 15557459e9aSMatthew G Knepley PetscValidIntPointer(numVerticesX,2); 15657459e9aSMatthew G Knepley *numVerticesX = nVx; 15757459e9aSMatthew G Knepley } 15857459e9aSMatthew G Knepley if (numVerticesY) { 15957459e9aSMatthew G Knepley PetscValidIntPointer(numVerticesY,3); 16057459e9aSMatthew G Knepley *numVerticesY = nVy; 16157459e9aSMatthew G Knepley } 16257459e9aSMatthew G Knepley if (numVerticesZ) { 16357459e9aSMatthew G Knepley PetscValidIntPointer(numVerticesZ,4); 16457459e9aSMatthew G Knepley *numVerticesZ = nVz; 16557459e9aSMatthew G Knepley } 16657459e9aSMatthew G Knepley if (numVertices) { 16757459e9aSMatthew G Knepley PetscValidIntPointer(numVertices,5); 16857459e9aSMatthew G Knepley *numVertices = nV; 16957459e9aSMatthew G Knepley } 17057459e9aSMatthew G Knepley PetscFunctionReturn(0); 17157459e9aSMatthew G Knepley } 17257459e9aSMatthew G Knepley 17357459e9aSMatthew G Knepley PetscErrorCode DMDAGetNumFaces(DM dm, PetscInt *numXFacesX, PetscInt *numXFaces, PetscInt *numYFacesY, PetscInt *numYFaces, PetscInt *numZFacesZ, PetscInt *numZFaces) 17457459e9aSMatthew G Knepley { 17557459e9aSMatthew G Knepley DM_DA *da = (DM_DA*) dm->data; 176c73cfb54SMatthew G. Knepley const PetscInt dim = dm->dim; 17757459e9aSMatthew G Knepley const PetscInt mx = (da->Xe - da->Xs)/da->w, my = da->Ye - da->Ys, mz = da->Ze - da->Zs; 17857459e9aSMatthew G Knepley const PetscInt nxF = (dim > 1 ? (my)*(dim > 2 ? (mz) : 1) : 1); 17957459e9aSMatthew G Knepley const PetscInt nXF = (mx+1)*nxF; 18057459e9aSMatthew G Knepley const PetscInt nyF = mx*(dim > 2 ? mz : 1); 18157459e9aSMatthew G Knepley const PetscInt nYF = dim > 1 ? (my+1)*nyF : 0; 18257459e9aSMatthew G Knepley const PetscInt nzF = mx*(dim > 1 ? my : 0); 18357459e9aSMatthew G Knepley const PetscInt nZF = dim > 2 ? (mz+1)*nzF : 0; 18457459e9aSMatthew G Knepley 18557459e9aSMatthew G Knepley PetscFunctionBegin; 18657459e9aSMatthew G Knepley if (numXFacesX) { 18757459e9aSMatthew G Knepley PetscValidIntPointer(numXFacesX,2); 18857459e9aSMatthew G Knepley *numXFacesX = nxF; 18957459e9aSMatthew G Knepley } 19057459e9aSMatthew G Knepley if (numXFaces) { 19157459e9aSMatthew G Knepley PetscValidIntPointer(numXFaces,3); 19257459e9aSMatthew G Knepley *numXFaces = nXF; 19357459e9aSMatthew G Knepley } 19457459e9aSMatthew G Knepley if (numYFacesY) { 19557459e9aSMatthew G Knepley PetscValidIntPointer(numYFacesY,4); 19657459e9aSMatthew G Knepley *numYFacesY = nyF; 19757459e9aSMatthew G Knepley } 19857459e9aSMatthew G Knepley if (numYFaces) { 19957459e9aSMatthew G Knepley PetscValidIntPointer(numYFaces,5); 20057459e9aSMatthew G Knepley *numYFaces = nYF; 20157459e9aSMatthew G Knepley } 20257459e9aSMatthew G Knepley if (numZFacesZ) { 20357459e9aSMatthew G Knepley PetscValidIntPointer(numZFacesZ,6); 20457459e9aSMatthew G Knepley *numZFacesZ = nzF; 20557459e9aSMatthew G Knepley } 20657459e9aSMatthew G Knepley if (numZFaces) { 20757459e9aSMatthew G Knepley PetscValidIntPointer(numZFaces,7); 20857459e9aSMatthew G Knepley *numZFaces = nZF; 20957459e9aSMatthew G Knepley } 21057459e9aSMatthew G Knepley PetscFunctionReturn(0); 21157459e9aSMatthew G Knepley } 21257459e9aSMatthew G Knepley 21357459e9aSMatthew G Knepley PetscErrorCode DMDAGetHeightStratum(DM dm, PetscInt height, PetscInt *pStart, PetscInt *pEnd) 21457459e9aSMatthew G Knepley { 215c73cfb54SMatthew G. Knepley const PetscInt dim = dm->dim; 21657459e9aSMatthew G Knepley PetscInt nC, nV, nXF, nYF, nZF; 21757459e9aSMatthew G Knepley PetscErrorCode ierr; 21857459e9aSMatthew G Knepley 21957459e9aSMatthew G Knepley PetscFunctionBegin; 2208865f1eaSKarl Rupp if (pStart) PetscValidIntPointer(pStart,3); 2218865f1eaSKarl Rupp if (pEnd) PetscValidIntPointer(pEnd,4); 2223582350cSMatthew G. Knepley ierr = DMDAGetNumCells(dm, NULL, NULL, NULL, &nC);CHKERRQ(ierr); 2230298fd71SBarry Smith ierr = DMDAGetNumVertices(dm, NULL, NULL, NULL, &nV);CHKERRQ(ierr); 2240298fd71SBarry Smith ierr = DMDAGetNumFaces(dm, NULL, &nXF, NULL, &nYF, NULL, &nZF);CHKERRQ(ierr); 22557459e9aSMatthew G Knepley if (height == 0) { 22657459e9aSMatthew G Knepley /* Cells */ 2278865f1eaSKarl Rupp if (pStart) *pStart = 0; 2288865f1eaSKarl Rupp if (pEnd) *pEnd = nC; 22957459e9aSMatthew G Knepley } else if (height == 1) { 23057459e9aSMatthew G Knepley /* Faces */ 2318865f1eaSKarl Rupp if (pStart) *pStart = nC+nV; 2328865f1eaSKarl Rupp if (pEnd) *pEnd = nC+nV+nXF+nYF+nZF; 23357459e9aSMatthew G Knepley } else if (height == dim) { 23457459e9aSMatthew G Knepley /* Vertices */ 2358865f1eaSKarl Rupp if (pStart) *pStart = nC; 2368865f1eaSKarl Rupp if (pEnd) *pEnd = nC+nV; 23757459e9aSMatthew G Knepley } else if (height < 0) { 23857459e9aSMatthew G Knepley /* All points */ 2398865f1eaSKarl Rupp if (pStart) *pStart = 0; 2408865f1eaSKarl Rupp if (pEnd) *pEnd = nC+nV+nXF+nYF+nZF; 24182f516ccSBarry Smith } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "No points of height %d in the DA", height); 24257459e9aSMatthew G Knepley PetscFunctionReturn(0); 24357459e9aSMatthew G Knepley } 24457459e9aSMatthew G Knepley 2453385ff7eSMatthew G. Knepley PetscErrorCode DMDAGetDepthStratum(DM dm, PetscInt depth, PetscInt *pStart, PetscInt *pEnd) 2463385ff7eSMatthew G. Knepley { 247c73cfb54SMatthew G. Knepley const PetscInt dim = dm->dim; 2483385ff7eSMatthew G. Knepley PetscInt nC, nV, nXF, nYF, nZF; 2493385ff7eSMatthew G. Knepley PetscErrorCode ierr; 2503385ff7eSMatthew G. Knepley 2513385ff7eSMatthew G. Knepley PetscFunctionBegin; 2523385ff7eSMatthew G. Knepley if (pStart) PetscValidIntPointer(pStart,3); 2533385ff7eSMatthew G. Knepley if (pEnd) PetscValidIntPointer(pEnd,4); 2543385ff7eSMatthew G. Knepley ierr = DMDAGetNumCells(dm, NULL, NULL, NULL, &nC);CHKERRQ(ierr); 2553385ff7eSMatthew G. Knepley ierr = DMDAGetNumVertices(dm, NULL, NULL, NULL, &nV);CHKERRQ(ierr); 2563385ff7eSMatthew G. Knepley ierr = DMDAGetNumFaces(dm, NULL, &nXF, NULL, &nYF, NULL, &nZF);CHKERRQ(ierr); 2573385ff7eSMatthew G. Knepley if (depth == dim) { 2583385ff7eSMatthew G. Knepley /* Cells */ 2593385ff7eSMatthew G. Knepley if (pStart) *pStart = 0; 2603385ff7eSMatthew G. Knepley if (pEnd) *pEnd = nC; 2613385ff7eSMatthew G. Knepley } else if (depth == dim-1) { 2623385ff7eSMatthew G. Knepley /* Faces */ 2633385ff7eSMatthew G. Knepley if (pStart) *pStart = nC+nV; 2643385ff7eSMatthew G. Knepley if (pEnd) *pEnd = nC+nV+nXF+nYF+nZF; 2653385ff7eSMatthew G. Knepley } else if (depth == 0) { 2663385ff7eSMatthew G. Knepley /* Vertices */ 2673385ff7eSMatthew G. Knepley if (pStart) *pStart = nC; 2683385ff7eSMatthew G. Knepley if (pEnd) *pEnd = nC+nV; 2693385ff7eSMatthew G. Knepley } else if (depth < 0) { 2703385ff7eSMatthew G. Knepley /* All points */ 2713385ff7eSMatthew G. Knepley if (pStart) *pStart = 0; 2723385ff7eSMatthew G. Knepley if (pEnd) *pEnd = nC+nV+nXF+nYF+nZF; 2733385ff7eSMatthew G. Knepley } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "No points of depth %d in the DA", depth); 2743385ff7eSMatthew G. Knepley PetscFunctionReturn(0); 2753385ff7eSMatthew G. Knepley } 2763385ff7eSMatthew G. Knepley 2773385ff7eSMatthew G. Knepley PetscErrorCode DMDAGetConeSize(DM dm, PetscInt p, PetscInt *coneSize) 2783385ff7eSMatthew G. Knepley { 279c73cfb54SMatthew G. Knepley const PetscInt dim = dm->dim; 2803385ff7eSMatthew G. Knepley PetscInt nC, nV, nXF, nYF, nZF; 2813385ff7eSMatthew G. Knepley PetscErrorCode ierr; 2823385ff7eSMatthew G. Knepley 2833385ff7eSMatthew G. Knepley PetscFunctionBegin; 2843385ff7eSMatthew G. Knepley *coneSize = 0; 2853385ff7eSMatthew G. Knepley ierr = DMDAGetNumCells(dm, NULL, NULL, NULL, &nC);CHKERRQ(ierr); 2863385ff7eSMatthew G. Knepley ierr = DMDAGetNumVertices(dm, NULL, NULL, NULL, &nV);CHKERRQ(ierr); 2873385ff7eSMatthew G. Knepley ierr = DMDAGetNumFaces(dm, NULL, &nXF, NULL, &nYF, NULL, &nZF);CHKERRQ(ierr); 2883385ff7eSMatthew G. Knepley switch (dim) { 2893385ff7eSMatthew G. Knepley case 2: 2903385ff7eSMatthew G. Knepley if (p >= 0) { 2913385ff7eSMatthew G. Knepley if (p < nC) { 2923385ff7eSMatthew G. Knepley *coneSize = 4; 2933385ff7eSMatthew G. Knepley } else if (p < nC+nV) { 2943385ff7eSMatthew G. Knepley *coneSize = 0; 2953385ff7eSMatthew G. Knepley } else if (p < nC+nV+nXF+nYF+nZF) { 2963385ff7eSMatthew G. Knepley *coneSize = 2; 2973385ff7eSMatthew G. Knepley } else SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Point %d should be in [0, %d)", p, nC+nV+nXF+nYF+nZF); 2983385ff7eSMatthew G. Knepley } else SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Negative point %d is invalid", p); 2993385ff7eSMatthew G. Knepley break; 3003385ff7eSMatthew G. Knepley case 3: 3013385ff7eSMatthew G. Knepley SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Too lazy to do 3D"); 3023385ff7eSMatthew G. Knepley break; 3033385ff7eSMatthew G. Knepley } 3043385ff7eSMatthew G. Knepley PetscFunctionReturn(0); 3053385ff7eSMatthew G. Knepley } 3063385ff7eSMatthew G. Knepley 3073385ff7eSMatthew G. Knepley PetscErrorCode DMDAGetCone(DM dm, PetscInt p, PetscInt *cone[]) 3083385ff7eSMatthew G. Knepley { 309c73cfb54SMatthew G. Knepley const PetscInt dim = dm->dim; 3103385ff7eSMatthew G. Knepley PetscInt nCx, nCy, nCz, nC, nVx, nVy, nVz, nV, nxF, nyF, nzF, nXF, nYF, nZF; 3113385ff7eSMatthew G. Knepley PetscErrorCode ierr; 3123385ff7eSMatthew G. Knepley 3133385ff7eSMatthew G. Knepley PetscFunctionBegin; 31469291d52SBarry Smith if (!cone) {ierr = DMGetWorkArray(dm, 6, MPIU_INT, cone);CHKERRQ(ierr);} 3153385ff7eSMatthew G. Knepley ierr = DMDAGetNumCells(dm, &nCx, &nCy, &nCz, &nC);CHKERRQ(ierr); 3163385ff7eSMatthew G. Knepley ierr = DMDAGetNumVertices(dm, &nVx, &nVy, &nVz, &nV);CHKERRQ(ierr); 3173385ff7eSMatthew G. Knepley ierr = DMDAGetNumFaces(dm, &nxF, &nXF, &nyF, &nYF, &nzF, &nZF);CHKERRQ(ierr); 3183385ff7eSMatthew G. Knepley switch (dim) { 3193385ff7eSMatthew G. Knepley case 2: 3203385ff7eSMatthew G. Knepley if (p >= 0) { 3213385ff7eSMatthew G. Knepley if (p < nC) { 3223385ff7eSMatthew G. Knepley const PetscInt cy = p / nCx; 3233385ff7eSMatthew G. Knepley const PetscInt cx = p % nCx; 3243385ff7eSMatthew G. Knepley 3253385ff7eSMatthew G. Knepley (*cone)[0] = cy*nxF + cx + nC+nV; 3263385ff7eSMatthew G. Knepley (*cone)[1] = cx*nyF + cy + nyF + nC+nV+nXF; 3273385ff7eSMatthew G. Knepley (*cone)[2] = cy*nxF + cx + nxF + nC+nV; 3283385ff7eSMatthew G. Knepley (*cone)[3] = cx*nyF + cy + nC+nV+nXF; 3293385ff7eSMatthew G. Knepley SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Too lazy to do cell cones"); 3303385ff7eSMatthew G. Knepley } else if (p < nC+nV) { 3313385ff7eSMatthew G. Knepley } else if (p < nC+nV+nXF) { 3323385ff7eSMatthew G. Knepley const PetscInt fy = (p - nC+nV) / nxF; 3333385ff7eSMatthew G. Knepley const PetscInt fx = (p - nC+nV) % nxF; 3343385ff7eSMatthew G. Knepley 3353385ff7eSMatthew G. Knepley (*cone)[0] = fy*nVx + fx + nC; 3363385ff7eSMatthew G. Knepley (*cone)[1] = fy*nVx + fx + 1 + nC; 3373385ff7eSMatthew G. Knepley } else if (p < nC+nV+nXF+nYF) { 3383385ff7eSMatthew G. Knepley const PetscInt fx = (p - nC+nV+nXF) / nyF; 3393385ff7eSMatthew G. Knepley const PetscInt fy = (p - nC+nV+nXF) % nyF; 3403385ff7eSMatthew G. Knepley 3413385ff7eSMatthew G. Knepley (*cone)[0] = fy*nVx + fx + nC; 3423385ff7eSMatthew G. Knepley (*cone)[1] = fy*nVx + fx + nVx + nC; 3433385ff7eSMatthew G. Knepley } else SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Point %d should be in [0, %d)", p, nC+nV+nXF+nYF+nZF); 3443385ff7eSMatthew G. Knepley } else SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Negative point %d is invalid", p); 3453385ff7eSMatthew G. Knepley break; 3463385ff7eSMatthew G. Knepley case 3: 3473385ff7eSMatthew G. Knepley SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Too lazy to do 3D"); 3483385ff7eSMatthew G. Knepley break; 3493385ff7eSMatthew G. Knepley } 3503385ff7eSMatthew G. Knepley PetscFunctionReturn(0); 3513385ff7eSMatthew G. Knepley } 3523385ff7eSMatthew G. Knepley 3533385ff7eSMatthew G. Knepley PetscErrorCode DMDARestoreCone(DM dm, PetscInt p, PetscInt *cone[]) 3543385ff7eSMatthew G. Knepley { 3553385ff7eSMatthew G. Knepley PetscErrorCode ierr; 3563385ff7eSMatthew G. Knepley 3573385ff7eSMatthew G. Knepley PetscFunctionBegin; 35869291d52SBarry Smith ierr = DMGetWorkArray(dm, 6, MPIU_INT, cone);CHKERRQ(ierr); 3593385ff7eSMatthew G. Knepley PetscFunctionReturn(0); 3603385ff7eSMatthew G. Knepley } 3613385ff7eSMatthew G. Knepley 3623385ff7eSMatthew G. Knepley PetscErrorCode DMDASetVertexCoordinates(DM dm, PetscReal xl, PetscReal xu, PetscReal yl, PetscReal yu, PetscReal zl, PetscReal zu) 3633385ff7eSMatthew G. Knepley { 3643385ff7eSMatthew G. Knepley DM_DA *da = (DM_DA *) dm->data; 3653385ff7eSMatthew G. Knepley Vec coordinates; 3663385ff7eSMatthew G. Knepley PetscSection section; 3673385ff7eSMatthew G. Knepley PetscScalar *coords; 3683385ff7eSMatthew G. Knepley PetscReal h[3]; 3693385ff7eSMatthew G. Knepley PetscInt dim, size, M, N, P, nVx, nVy, nVz, nV, vStart, vEnd, v, i, j, k; 3703385ff7eSMatthew G. Knepley PetscErrorCode ierr; 3713385ff7eSMatthew G. Knepley 3723385ff7eSMatthew G. Knepley PetscFunctionBegin; 373a9a02de4SBarry Smith PetscValidHeaderSpecificType(dm, DM_CLASSID, 1,DMDA); 374*ea78f98cSLisandro Dalcin ierr = DMDAGetInfo(dm, &dim, &M, &N, &P, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);CHKERRQ(ierr); 3754a2f8832SBarry Smith if (dim > 3) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_PLIB,"The following code only works for dim <= 3"); 3763385ff7eSMatthew G. Knepley h[0] = (xu - xl)/M; 3773385ff7eSMatthew G. Knepley h[1] = (yu - yl)/N; 3783385ff7eSMatthew G. Knepley h[2] = (zu - zl)/P; 3793385ff7eSMatthew G. Knepley ierr = DMDAGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr); 3803385ff7eSMatthew G. Knepley ierr = DMDAGetNumVertices(dm, &nVx, &nVy, &nVz, &nV);CHKERRQ(ierr); 3813385ff7eSMatthew G. Knepley ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), §ion);CHKERRQ(ierr); 3823385ff7eSMatthew G. Knepley ierr = PetscSectionSetNumFields(section, 1);CHKERRQ(ierr); 3833385ff7eSMatthew G. Knepley ierr = PetscSectionSetFieldComponents(section, 0, dim);CHKERRQ(ierr); 3843385ff7eSMatthew G. Knepley ierr = PetscSectionSetChart(section, vStart, vEnd);CHKERRQ(ierr); 3853385ff7eSMatthew G. Knepley for (v = vStart; v < vEnd; ++v) { 3863385ff7eSMatthew G. Knepley ierr = PetscSectionSetDof(section, v, dim);CHKERRQ(ierr); 3873385ff7eSMatthew G. Knepley } 3883385ff7eSMatthew G. Knepley ierr = PetscSectionSetUp(section);CHKERRQ(ierr); 3893385ff7eSMatthew G. Knepley ierr = PetscSectionGetStorageSize(section, &size);CHKERRQ(ierr); 3903385ff7eSMatthew G. Knepley ierr = VecCreateSeq(PETSC_COMM_SELF, size, &coordinates);CHKERRQ(ierr); 39124640c55SToby Isaac ierr = PetscObjectSetName((PetscObject)coordinates,"coordinates");CHKERRQ(ierr); 3923385ff7eSMatthew G. Knepley ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr); 3933385ff7eSMatthew G. Knepley for (k = 0; k < nVz; ++k) { 394e4d69e08SBarry Smith PetscInt ind[3], d, off; 3953385ff7eSMatthew G. Knepley 396e4d69e08SBarry Smith ind[0] = 0; 397e4d69e08SBarry Smith ind[1] = 0; 398e4d69e08SBarry Smith ind[2] = k + da->zs; 3993385ff7eSMatthew G. Knepley for (j = 0; j < nVy; ++j) { 4003385ff7eSMatthew G. Knepley ind[1] = j + da->ys; 4013385ff7eSMatthew G. Knepley for (i = 0; i < nVx; ++i) { 4023385ff7eSMatthew G. Knepley const PetscInt vertex = (k*nVy + j)*nVx + i + vStart; 4033385ff7eSMatthew G. Knepley 4043385ff7eSMatthew G. Knepley ierr = PetscSectionGetOffset(section, vertex, &off);CHKERRQ(ierr); 4053385ff7eSMatthew G. Knepley ind[0] = i + da->xs; 4063385ff7eSMatthew G. Knepley for (d = 0; d < dim; ++d) { 4073385ff7eSMatthew G. Knepley coords[off+d] = h[d]*ind[d]; 4083385ff7eSMatthew G. Knepley } 4093385ff7eSMatthew G. Knepley } 4103385ff7eSMatthew G. Knepley } 4113385ff7eSMatthew G. Knepley } 4123385ff7eSMatthew G. Knepley ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr); 41346e270d4SMatthew G. Knepley ierr = DMSetCoordinateSection(dm, PETSC_DETERMINE, section);CHKERRQ(ierr); 4143385ff7eSMatthew G. Knepley ierr = DMSetCoordinatesLocal(dm, coordinates);CHKERRQ(ierr); 415a4b60ecfSMatthew G. Knepley ierr = PetscSectionDestroy(§ion);CHKERRQ(ierr); 4163385ff7eSMatthew G. Knepley ierr = VecDestroy(&coordinates);CHKERRQ(ierr); 4173385ff7eSMatthew G. Knepley PetscFunctionReturn(0); 4183385ff7eSMatthew G. Knepley } 4199a800dd8SMatthew G. Knepley 42047c6ae99SBarry Smith /* ------------------------------------------------------------------- */ 42147c6ae99SBarry Smith 42247c6ae99SBarry Smith /*@C 423aa219208SBarry Smith DMDAGetArray - Gets a work array for a DMDA 42447c6ae99SBarry Smith 42547c6ae99SBarry Smith Input Parameter: 42647c6ae99SBarry Smith + da - information about my local patch 42747c6ae99SBarry Smith - ghosted - do you want arrays for the ghosted or nonghosted patch 42847c6ae99SBarry Smith 42947c6ae99SBarry Smith Output Parameters: 43047c6ae99SBarry Smith . vptr - array data structured 43147c6ae99SBarry Smith 43247c6ae99SBarry Smith Note: The vector values are NOT initialized and may have garbage in them, so you may need 43347c6ae99SBarry Smith to zero them. 43447c6ae99SBarry Smith 43547c6ae99SBarry Smith Level: advanced 43647c6ae99SBarry Smith 437bcaeba4dSBarry Smith .seealso: DMDARestoreArray() 43847c6ae99SBarry Smith 43947c6ae99SBarry Smith @*/ 4407087cfbeSBarry Smith PetscErrorCode DMDAGetArray(DM da,PetscBool ghosted,void *vptr) 44147c6ae99SBarry Smith { 44247c6ae99SBarry Smith PetscErrorCode ierr; 44347c6ae99SBarry Smith PetscInt j,i,xs,ys,xm,ym,zs,zm; 44447c6ae99SBarry Smith char *iarray_start; 44547c6ae99SBarry Smith void **iptr = (void**)vptr; 44647c6ae99SBarry Smith DM_DA *dd = (DM_DA*)da->data; 44747c6ae99SBarry Smith 44847c6ae99SBarry Smith PetscFunctionBegin; 449a9a02de4SBarry Smith PetscValidHeaderSpecificType(da,DM_CLASSID,1,DMDA); 45047c6ae99SBarry Smith if (ghosted) { 451aa219208SBarry Smith for (i=0; i<DMDA_MAX_WORK_ARRAYS; i++) { 45247c6ae99SBarry Smith if (dd->arrayghostedin[i]) { 45347c6ae99SBarry Smith *iptr = dd->arrayghostedin[i]; 45447c6ae99SBarry Smith iarray_start = (char*)dd->startghostedin[i]; 4550298fd71SBarry Smith dd->arrayghostedin[i] = NULL; 4560298fd71SBarry Smith dd->startghostedin[i] = NULL; 45747c6ae99SBarry Smith 45847c6ae99SBarry Smith goto done; 45947c6ae99SBarry Smith } 46047c6ae99SBarry Smith } 46147c6ae99SBarry Smith xs = dd->Xs; 46247c6ae99SBarry Smith ys = dd->Ys; 46347c6ae99SBarry Smith zs = dd->Zs; 46447c6ae99SBarry Smith xm = dd->Xe-dd->Xs; 46547c6ae99SBarry Smith ym = dd->Ye-dd->Ys; 46647c6ae99SBarry Smith zm = dd->Ze-dd->Zs; 46747c6ae99SBarry Smith } else { 468aa219208SBarry Smith for (i=0; i<DMDA_MAX_WORK_ARRAYS; i++) { 46947c6ae99SBarry Smith if (dd->arrayin[i]) { 47047c6ae99SBarry Smith *iptr = dd->arrayin[i]; 47147c6ae99SBarry Smith iarray_start = (char*)dd->startin[i]; 4720298fd71SBarry Smith dd->arrayin[i] = NULL; 4730298fd71SBarry Smith dd->startin[i] = NULL; 47447c6ae99SBarry Smith 47547c6ae99SBarry Smith goto done; 47647c6ae99SBarry Smith } 47747c6ae99SBarry Smith } 47847c6ae99SBarry Smith xs = dd->xs; 47947c6ae99SBarry Smith ys = dd->ys; 48047c6ae99SBarry Smith zs = dd->zs; 48147c6ae99SBarry Smith xm = dd->xe-dd->xs; 48247c6ae99SBarry Smith ym = dd->ye-dd->ys; 48347c6ae99SBarry Smith zm = dd->ze-dd->zs; 48447c6ae99SBarry Smith } 48547c6ae99SBarry Smith 486c73cfb54SMatthew G. Knepley switch (da->dim) { 48747c6ae99SBarry Smith case 1: { 48847c6ae99SBarry Smith void *ptr; 48947c6ae99SBarry Smith 490901f1932SJed Brown ierr = PetscMalloc(xm*sizeof(PetscScalar),&iarray_start);CHKERRQ(ierr); 49147c6ae99SBarry Smith 49247c6ae99SBarry Smith ptr = (void*)(iarray_start - xs*sizeof(PetscScalar)); 49347c6ae99SBarry Smith *iptr = (void*)ptr; 4948865f1eaSKarl Rupp break; 4958865f1eaSKarl Rupp } 49647c6ae99SBarry Smith case 2: { 49747c6ae99SBarry Smith void **ptr; 49847c6ae99SBarry Smith 499901f1932SJed Brown ierr = PetscMalloc((ym+1)*sizeof(void*)+xm*ym*sizeof(PetscScalar),&iarray_start);CHKERRQ(ierr); 50047c6ae99SBarry Smith 50147c6ae99SBarry Smith ptr = (void**)(iarray_start + xm*ym*sizeof(PetscScalar) - ys*sizeof(void*)); 5028865f1eaSKarl Rupp for (j=ys; j<ys+ym; j++) ptr[j] = iarray_start + sizeof(PetscScalar)*(xm*(j-ys) - xs); 50347c6ae99SBarry Smith *iptr = (void*)ptr; 5048865f1eaSKarl Rupp break; 5058865f1eaSKarl Rupp } 50647c6ae99SBarry Smith case 3: { 50747c6ae99SBarry Smith void ***ptr,**bptr; 50847c6ae99SBarry Smith 509901f1932SJed Brown ierr = PetscMalloc((zm+1)*sizeof(void**)+(ym*zm+1)*sizeof(void*)+xm*ym*zm*sizeof(PetscScalar),&iarray_start);CHKERRQ(ierr); 51047c6ae99SBarry Smith 51147c6ae99SBarry Smith ptr = (void***)(iarray_start + xm*ym*zm*sizeof(PetscScalar) - zs*sizeof(void*)); 51247c6ae99SBarry Smith bptr = (void**)(iarray_start + xm*ym*zm*sizeof(PetscScalar) + zm*sizeof(void**)); 5138865f1eaSKarl Rupp for (i=zs; i<zs+zm; i++) ptr[i] = bptr + ((i-zs)*ym - ys); 51447c6ae99SBarry Smith for (i=zs; i<zs+zm; i++) { 51547c6ae99SBarry Smith for (j=ys; j<ys+ym; j++) { 51647c6ae99SBarry Smith ptr[i][j] = iarray_start + sizeof(PetscScalar)*(xm*ym*(i-zs) + xm*(j-ys) - xs); 51747c6ae99SBarry Smith } 51847c6ae99SBarry Smith } 51947c6ae99SBarry Smith *iptr = (void*)ptr; 5208865f1eaSKarl Rupp break; 5218865f1eaSKarl Rupp } 52247c6ae99SBarry Smith default: 523c73cfb54SMatthew G. Knepley SETERRQ1(PetscObjectComm((PetscObject)da),PETSC_ERR_SUP,"Dimension %D not supported",da->dim); 52447c6ae99SBarry Smith } 52547c6ae99SBarry Smith 52647c6ae99SBarry Smith done: 52747c6ae99SBarry Smith /* add arrays to the checked out list */ 52847c6ae99SBarry Smith if (ghosted) { 529aa219208SBarry Smith for (i=0; i<DMDA_MAX_WORK_ARRAYS; i++) { 53047c6ae99SBarry Smith if (!dd->arrayghostedout[i]) { 53147c6ae99SBarry Smith dd->arrayghostedout[i] = *iptr; 53247c6ae99SBarry Smith dd->startghostedout[i] = iarray_start; 53347c6ae99SBarry Smith break; 53447c6ae99SBarry Smith } 53547c6ae99SBarry Smith } 53647c6ae99SBarry Smith } else { 537aa219208SBarry Smith for (i=0; i<DMDA_MAX_WORK_ARRAYS; i++) { 53847c6ae99SBarry Smith if (!dd->arrayout[i]) { 53947c6ae99SBarry Smith dd->arrayout[i] = *iptr; 54047c6ae99SBarry Smith dd->startout[i] = iarray_start; 54147c6ae99SBarry Smith break; 54247c6ae99SBarry Smith } 54347c6ae99SBarry Smith } 54447c6ae99SBarry Smith } 54547c6ae99SBarry Smith PetscFunctionReturn(0); 54647c6ae99SBarry Smith } 54747c6ae99SBarry Smith 54847c6ae99SBarry Smith /*@C 549aa219208SBarry Smith DMDARestoreArray - Restores an array of derivative types for a DMDA 55047c6ae99SBarry Smith 55147c6ae99SBarry Smith Input Parameter: 55247c6ae99SBarry Smith + da - information about my local patch 55347c6ae99SBarry Smith . ghosted - do you want arrays for the ghosted or nonghosted patch 554cb004a26SBarry Smith - vptr - array data structured 55547c6ae99SBarry Smith 55647c6ae99SBarry Smith Level: advanced 55747c6ae99SBarry Smith 558bcaeba4dSBarry Smith .seealso: DMDAGetArray() 55947c6ae99SBarry Smith 56047c6ae99SBarry Smith @*/ 5617087cfbeSBarry Smith PetscErrorCode DMDARestoreArray(DM da,PetscBool ghosted,void *vptr) 56247c6ae99SBarry Smith { 56347c6ae99SBarry Smith PetscInt i; 564*ea78f98cSLisandro Dalcin void **iptr = (void**)vptr,*iarray_start = NULL; 56547c6ae99SBarry Smith DM_DA *dd = (DM_DA*)da->data; 56647c6ae99SBarry Smith 56747c6ae99SBarry Smith PetscFunctionBegin; 568a9a02de4SBarry Smith PetscValidHeaderSpecificType(da,DM_CLASSID,1,DMDA); 56947c6ae99SBarry Smith if (ghosted) { 570aa219208SBarry Smith for (i=0; i<DMDA_MAX_WORK_ARRAYS; i++) { 57147c6ae99SBarry Smith if (dd->arrayghostedout[i] == *iptr) { 57247c6ae99SBarry Smith iarray_start = dd->startghostedout[i]; 5730298fd71SBarry Smith dd->arrayghostedout[i] = NULL; 5740298fd71SBarry Smith dd->startghostedout[i] = NULL; 57547c6ae99SBarry Smith break; 57647c6ae99SBarry Smith } 57747c6ae99SBarry Smith } 578aa219208SBarry Smith for (i=0; i<DMDA_MAX_WORK_ARRAYS; i++) { 57947c6ae99SBarry Smith if (!dd->arrayghostedin[i]) { 58047c6ae99SBarry Smith dd->arrayghostedin[i] = *iptr; 58147c6ae99SBarry Smith dd->startghostedin[i] = iarray_start; 58247c6ae99SBarry Smith break; 58347c6ae99SBarry Smith } 58447c6ae99SBarry Smith } 58547c6ae99SBarry Smith } else { 586aa219208SBarry Smith for (i=0; i<DMDA_MAX_WORK_ARRAYS; i++) { 58747c6ae99SBarry Smith if (dd->arrayout[i] == *iptr) { 58847c6ae99SBarry Smith iarray_start = dd->startout[i]; 5890298fd71SBarry Smith dd->arrayout[i] = NULL; 5900298fd71SBarry Smith dd->startout[i] = NULL; 59147c6ae99SBarry Smith break; 59247c6ae99SBarry Smith } 59347c6ae99SBarry Smith } 594aa219208SBarry Smith for (i=0; i<DMDA_MAX_WORK_ARRAYS; i++) { 59547c6ae99SBarry Smith if (!dd->arrayin[i]) { 59647c6ae99SBarry Smith dd->arrayin[i] = *iptr; 59747c6ae99SBarry Smith dd->startin[i] = iarray_start; 59847c6ae99SBarry Smith break; 59947c6ae99SBarry Smith } 60047c6ae99SBarry Smith } 60147c6ae99SBarry Smith } 60247c6ae99SBarry Smith PetscFunctionReturn(0); 60347c6ae99SBarry Smith } 60447c6ae99SBarry Smith 605