xref: /petsc/src/dm/impls/da/dagetarray.c (revision 3c48a1e8da19189ff2402a4e41a2fc082d52c349)
147c6ae99SBarry Smith 
2*3c48a1e8SJed Brown #include "petscdmda.h"    /*I   "petscdmda.h"   I*/
347c6ae99SBarry Smith 
447c6ae99SBarry Smith #undef __FUNCT__
5aa219208SBarry Smith #define __FUNCT__ "DMDAVecGetArray"
647c6ae99SBarry Smith /*@C
7aa219208SBarry Smith    DMDAVecGetArray - Returns a multiple dimension array that shares data with
847c6ae99SBarry Smith       the underlying vector and is indexed using the global dimensions.
947c6ae99SBarry Smith 
1047c6ae99SBarry Smith    Not Collective
1147c6ae99SBarry Smith 
1247c6ae99SBarry Smith    Input Parameter:
1347c6ae99SBarry Smith +  da - the distributed array
1447c6ae99SBarry Smith -  vec - the vector, either a vector the same size as one obtained with
15564755cdSBarry Smith          DMCreateGlobalVector() or DMCreateLocalVector()
1647c6ae99SBarry Smith 
1747c6ae99SBarry Smith    Output Parameter:
1847c6ae99SBarry Smith .  array - the array
1947c6ae99SBarry Smith 
2047c6ae99SBarry Smith    Notes:
21aa219208SBarry Smith     Call DMDAVecRestoreArray() once you have finished accessing the vector entries.
2247c6ae99SBarry Smith 
2347c6ae99SBarry Smith     In C, the indexing is "backwards" from what expects: array[k][j][i] NOT array[i][j][k]!
2447c6ae99SBarry Smith 
25564755cdSBarry Smith     If vec is a local vector (obtained with DMCreateLocalVector() etc) then they ghost point locations are accessable. If it is
2647c6ae99SBarry Smith     a global vector then the ghost points are not accessable. Of course with the local vector you will have had to do the
2747c6ae99SBarry Smith 
289a42bb27SBarry Smith     appropriate DMLocalToGlobalBegin() and DMLocalToGlobalEnd() to have correct values in the ghost locations.
2947c6ae99SBarry Smith 
30aa219208SBarry Smith   Fortran Notes: From Fortran use DMDAVecGetArrayF90() and pass for the array type PetscScalar,pointer :: array(:,...,:) of the appropriate
31aa219208SBarry Smith        dimension. For a DMDA created with a dof of 1 use the dimension of the DMDA, for a DMDA created with a dof greater than 1 use one more than the
32aa219208SBarry Smith        dimension of the DMDA. The order of the indices is array(xs:xs+xm-1,ys:ys+ym-1,zs:zs+zm-1) (when dof is 1) otherwise
3347c6ae99SBarry Smith        array(1:dof,xs:xs+xm-1,ys:ys+ym-1,zs:zs+zm-1) where the values are obtained from
34*3c48a1e8SJed Brown        DMDAGetCorners() for a global array or DMDAGetGhostCorners() for a local array. Include finclude/petscdmda.h90 to access this routine.
3547c6ae99SBarry Smith 
36aa219208SBarry Smith   Due to bugs in the compiler DMDAVecGetArrayF90() does not work with gfortran versions before 2.5
3747c6ae99SBarry Smith 
3847c6ae99SBarry Smith   Level: intermediate
3947c6ae99SBarry Smith 
4047c6ae99SBarry Smith .keywords: distributed array, get, corners, nodes, local indices, coordinates
4147c6ae99SBarry Smith 
42aa219208SBarry Smith .seealso: DMDAGetGhostCorners(), DMDAGetCorners(), VecGetArray(), VecRestoreArray(), DMDAVecRestoreArray(), DMDAVecRestoreArrayDOF()
43aa219208SBarry Smith           DMDAVecGetarrayDOF()
4447c6ae99SBarry Smith @*/
457087cfbeSBarry Smith PetscErrorCode  DMDAVecGetArray(DM da,Vec vec,void *array)
4647c6ae99SBarry Smith {
4747c6ae99SBarry Smith   PetscErrorCode ierr;
4847c6ae99SBarry Smith   PetscInt       xs,ys,zs,xm,ym,zm,gxs,gys,gzs,gxm,gym,gzm,N,dim,dof;
4947c6ae99SBarry Smith 
5047c6ae99SBarry Smith   PetscFunctionBegin;
51aa219208SBarry Smith   ierr = DMDAGetCorners(da,&xs,&ys,&zs,&xm,&ym,&zm);CHKERRQ(ierr);
52aa219208SBarry Smith   ierr = DMDAGetGhostCorners(da,&gxs,&gys,&gzs,&gxm,&gym,&gzm);CHKERRQ(ierr);
531321219cSEthan Coon   ierr = DMDAGetInfo(da,&dim,0,0,0,0,0,0,&dof,0,0,0,0,0);CHKERRQ(ierr);
5447c6ae99SBarry Smith 
5547c6ae99SBarry Smith   /* Handle case where user passes in global vector as opposed to local */
5647c6ae99SBarry Smith   ierr = VecGetLocalSize(vec,&N);CHKERRQ(ierr);
5747c6ae99SBarry Smith   if (N == xm*ym*zm*dof) {
5847c6ae99SBarry Smith     gxm = xm;
5947c6ae99SBarry Smith     gym = ym;
6047c6ae99SBarry Smith     gzm = zm;
6147c6ae99SBarry Smith     gxs = xs;
6247c6ae99SBarry Smith     gys = ys;
6347c6ae99SBarry Smith     gzs = zs;
6447c6ae99SBarry Smith   } else if (N != gxm*gym*gzm*dof) {
65aa219208SBarry Smith     SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Vector local size %D is not compatible with DMDA local sizes %D %D\n",N,xm*ym*zm*dof,gxm*gym*gzm*dof);
6647c6ae99SBarry Smith   }
6747c6ae99SBarry Smith 
6847c6ae99SBarry Smith   if (dim == 1) {
6947c6ae99SBarry Smith     ierr = VecGetArray1d(vec,gxm*dof,gxs*dof,(PetscScalar **)array);CHKERRQ(ierr);
7047c6ae99SBarry Smith   } else if (dim == 2) {
7147c6ae99SBarry Smith     ierr = VecGetArray2d(vec,gym,gxm*dof,gys,gxs*dof,(PetscScalar***)array);CHKERRQ(ierr);
7247c6ae99SBarry Smith   } else if (dim == 3) {
7347c6ae99SBarry Smith     ierr = VecGetArray3d(vec,gzm,gym,gxm*dof,gzs,gys,gxs*dof,(PetscScalar****)array);CHKERRQ(ierr);
7447c6ae99SBarry Smith   } else {
75aa219208SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_CORRUPT,"DMDA dimension not 1, 2, or 3, it is %D\n",dim);
7647c6ae99SBarry Smith   }
7747c6ae99SBarry Smith 
7847c6ae99SBarry Smith   PetscFunctionReturn(0);
7947c6ae99SBarry Smith }
8047c6ae99SBarry Smith 
8147c6ae99SBarry Smith #undef __FUNCT__
82aa219208SBarry Smith #define __FUNCT__ "DMDAVecRestoreArray"
8347c6ae99SBarry Smith /*@
84aa219208SBarry Smith    DMDAVecRestoreArray - Restores a multiple dimension array obtained with DMDAVecGetArray()
8547c6ae99SBarry Smith 
8647c6ae99SBarry Smith    Not Collective
8747c6ae99SBarry Smith 
8847c6ae99SBarry Smith    Input Parameter:
8947c6ae99SBarry Smith +  da - the distributed array
9047c6ae99SBarry Smith .  vec - the vector, either a vector the same size as one obtained with
91564755cdSBarry Smith          DMCreateGlobalVector() or DMCreateLocalVector()
9247c6ae99SBarry Smith -  array - the array
9347c6ae99SBarry Smith 
9447c6ae99SBarry Smith   Level: intermediate
9547c6ae99SBarry Smith 
96aa219208SBarry Smith   Fortran Notes: From Fortran use DMDAVecRestoreArrayF90()
9747c6ae99SBarry Smith 
9847c6ae99SBarry Smith .keywords: distributed array, get, corners, nodes, local indices, coordinates
9947c6ae99SBarry Smith 
100aa219208SBarry Smith .seealso: DMDAGetGhostCorners(), DMDAGetCorners(), VecGetArray(), VecRestoreArray(), DMDAVecGetArray()
10147c6ae99SBarry Smith @*/
1027087cfbeSBarry Smith PetscErrorCode  DMDAVecRestoreArray(DM da,Vec vec,void *array)
10347c6ae99SBarry Smith {
10447c6ae99SBarry Smith   PetscErrorCode ierr;
10547c6ae99SBarry Smith   PetscInt       xs,ys,zs,xm,ym,zm,gxs,gys,gzs,gxm,gym,gzm,N,dim,dof;
10647c6ae99SBarry Smith 
10747c6ae99SBarry Smith   PetscFunctionBegin;
108aa219208SBarry Smith   ierr = DMDAGetCorners(da,&xs,&ys,&zs,&xm,&ym,&zm);CHKERRQ(ierr);
109aa219208SBarry Smith   ierr = DMDAGetGhostCorners(da,&gxs,&gys,&gzs,&gxm,&gym,&gzm);CHKERRQ(ierr);
1101321219cSEthan Coon   ierr = DMDAGetInfo(da,&dim,0,0,0,0,0,0,&dof,0,0,0,0,0);CHKERRQ(ierr);
11147c6ae99SBarry Smith 
11247c6ae99SBarry Smith   /* Handle case where user passes in global vector as opposed to local */
11347c6ae99SBarry Smith   ierr = VecGetLocalSize(vec,&N);CHKERRQ(ierr);
11447c6ae99SBarry Smith   if (N == xm*ym*zm*dof) {
11547c6ae99SBarry Smith     gxm = xm;
11647c6ae99SBarry Smith     gym = ym;
11747c6ae99SBarry Smith     gzm = zm;
11847c6ae99SBarry Smith     gxs = xs;
11947c6ae99SBarry Smith     gys = ys;
12047c6ae99SBarry Smith     gzs = zs;
12147c6ae99SBarry Smith   } else if (N != gxm*gym*gzm*dof) {
122aa219208SBarry Smith     SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Vector local size %D is not compatible with DMDA local sizes %D %D\n",N,xm*ym*zm*dof,gxm*gym*gzm*dof);
12347c6ae99SBarry Smith   }
12447c6ae99SBarry Smith 
12547c6ae99SBarry Smith   if (dim == 1) {
12647c6ae99SBarry Smith     ierr = VecRestoreArray1d(vec,gxm*dof,gxs*dof,(PetscScalar **)array);CHKERRQ(ierr);
12747c6ae99SBarry Smith   } else if (dim == 2) {
12847c6ae99SBarry Smith     ierr = VecRestoreArray2d(vec,gym,gxm*dof,gys,gxs*dof,(PetscScalar***)array);CHKERRQ(ierr);
12947c6ae99SBarry Smith   } else if (dim == 3) {
13047c6ae99SBarry Smith     ierr = VecRestoreArray3d(vec,gzm,gym,gxm*dof,gzs,gys,gxs*dof,(PetscScalar****)array);CHKERRQ(ierr);
13147c6ae99SBarry Smith   } else {
132aa219208SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_CORRUPT,"DMDA dimension not 1, 2, or 3, it is %D\n",dim);
13347c6ae99SBarry Smith   }
13447c6ae99SBarry Smith   PetscFunctionReturn(0);
13547c6ae99SBarry Smith }
13647c6ae99SBarry Smith 
13747c6ae99SBarry Smith #undef __FUNCT__
138aa219208SBarry Smith #define __FUNCT__ "DMDAVecGetArrayDOF"
13947c6ae99SBarry Smith /*@C
140aa219208SBarry Smith    DMDAVecGetArrayDOF - Returns a multiple dimension array that shares data with
14147c6ae99SBarry Smith       the underlying vector and is indexed using the global dimensions.
14247c6ae99SBarry Smith 
14347c6ae99SBarry Smith    Not Collective
14447c6ae99SBarry Smith 
14547c6ae99SBarry Smith    Input Parameter:
14647c6ae99SBarry Smith +  da - the distributed array
14747c6ae99SBarry Smith -  vec - the vector, either a vector the same size as one obtained with
148564755cdSBarry Smith          DMCreateGlobalVector() or DMCreateLocalVector()
14947c6ae99SBarry Smith 
15047c6ae99SBarry Smith    Output Parameter:
15147c6ae99SBarry Smith .  array - the array
15247c6ae99SBarry Smith 
15347c6ae99SBarry Smith    Notes:
154aa219208SBarry Smith     Call DMDAVecRestoreArrayDOF() once you have finished accessing the vector entries.
15547c6ae99SBarry Smith 
15647c6ae99SBarry Smith     In C, the indexing is "backwards" from what expects: array[k][j][i][DOF] NOT array[i][j][k][DOF]!
15747c6ae99SBarry Smith 
15847c6ae99SBarry Smith   Level: intermediate
15947c6ae99SBarry Smith 
16047c6ae99SBarry Smith .keywords: distributed array, get, corners, nodes, local indices, coordinates
16147c6ae99SBarry Smith 
162aa219208SBarry Smith .seealso: DMDAGetGhostCorners(), DMDAGetCorners(), VecGetArray(), VecRestoreArray(), DMDAVecRestoreArray(), DMDAVecGetArray(), DMDAVecRestoreArrayDOF()
16347c6ae99SBarry Smith @*/
1647087cfbeSBarry Smith PetscErrorCode  DMDAVecGetArrayDOF(DM da,Vec vec,void *array)
16547c6ae99SBarry Smith {
16647c6ae99SBarry Smith   PetscErrorCode ierr;
16747c6ae99SBarry Smith   PetscInt       xs,ys,zs,xm,ym,zm,gxs,gys,gzs,gxm,gym,gzm,N,dim,dof;
16847c6ae99SBarry Smith 
16947c6ae99SBarry Smith   PetscFunctionBegin;
170aa219208SBarry Smith   ierr = DMDAGetCorners(da,&xs,&ys,&zs,&xm,&ym,&zm);CHKERRQ(ierr);
171aa219208SBarry Smith   ierr = DMDAGetGhostCorners(da,&gxs,&gys,&gzs,&gxm,&gym,&gzm);CHKERRQ(ierr);
1721321219cSEthan Coon   ierr = DMDAGetInfo(da,&dim,0,0,0,0,0,0,&dof,0,0,0,0,0);CHKERRQ(ierr);
17347c6ae99SBarry Smith 
17447c6ae99SBarry Smith   /* Handle case where user passes in global vector as opposed to local */
17547c6ae99SBarry Smith   ierr = VecGetLocalSize(vec,&N);CHKERRQ(ierr);
17647c6ae99SBarry Smith   if (N == xm*ym*zm*dof) {
17747c6ae99SBarry Smith     gxm = xm;
17847c6ae99SBarry Smith     gym = ym;
17947c6ae99SBarry Smith     gzm = zm;
18047c6ae99SBarry Smith     gxs = xs;
18147c6ae99SBarry Smith     gys = ys;
18247c6ae99SBarry Smith     gzs = zs;
18347c6ae99SBarry Smith   } else if (N != gxm*gym*gzm*dof) {
184aa219208SBarry Smith     SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Vector local size %D is not compatible with DMDA local sizes %D %D\n",N,xm*ym*zm*dof,gxm*gym*gzm*dof);
18547c6ae99SBarry Smith   }
18647c6ae99SBarry Smith 
18747c6ae99SBarry Smith   if (dim == 1) {
18847c6ae99SBarry Smith     ierr = VecGetArray2d(vec,gxm,dof,gxs,0,(PetscScalar ***)array);CHKERRQ(ierr);
18947c6ae99SBarry Smith   } else if (dim == 2) {
19047c6ae99SBarry Smith     ierr = VecGetArray3d(vec,gym,gxm,dof,gys,gxs,0,(PetscScalar****)array);CHKERRQ(ierr);
19147c6ae99SBarry Smith   } else if (dim == 3) {
19247c6ae99SBarry Smith     ierr = VecGetArray4d(vec,gzm,gym,gxm,dof,gzs,gys,gxs,0,(PetscScalar*****)array);CHKERRQ(ierr);
19347c6ae99SBarry Smith   } else {
194aa219208SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_CORRUPT,"DMDA dimension not 1, 2, or 3, it is %D\n",dim);
19547c6ae99SBarry Smith   }
19647c6ae99SBarry Smith   PetscFunctionReturn(0);
19747c6ae99SBarry Smith }
19847c6ae99SBarry Smith 
19947c6ae99SBarry Smith #undef __FUNCT__
200aa219208SBarry Smith #define __FUNCT__ "DMDAVecRestoreArrayDOF"
20147c6ae99SBarry Smith /*@
202aa219208SBarry Smith    DMDAVecRestoreArrayDOF - Restores a multiple dimension array obtained with DMDAVecGetArrayDOF()
20347c6ae99SBarry Smith 
20447c6ae99SBarry Smith    Not Collective
20547c6ae99SBarry Smith 
20647c6ae99SBarry Smith    Input Parameter:
20747c6ae99SBarry Smith +  da - the distributed array
20847c6ae99SBarry Smith .  vec - the vector, either a vector the same size as one obtained with
209564755cdSBarry Smith          DMCreateGlobalVector() or DMCreateLocalVector()
21047c6ae99SBarry Smith -  array - the array
21147c6ae99SBarry Smith 
21247c6ae99SBarry Smith   Level: intermediate
21347c6ae99SBarry Smith 
21447c6ae99SBarry Smith .keywords: distributed array, get, corners, nodes, local indices, coordinates
21547c6ae99SBarry Smith 
216aa219208SBarry Smith .seealso: DMDAGetGhostCorners(), DMDAGetCorners(), VecGetArray(), VecRestoreArray(), DMDAVecGetArray(), DMDAVecGetArrayDOF(), DMDAVecRestoreArrayDOF()
21747c6ae99SBarry Smith @*/
2187087cfbeSBarry Smith PetscErrorCode  DMDAVecRestoreArrayDOF(DM da,Vec vec,void *array)
21947c6ae99SBarry Smith {
22047c6ae99SBarry Smith   PetscErrorCode ierr;
22147c6ae99SBarry Smith   PetscInt       xs,ys,zs,xm,ym,zm,gxs,gys,gzs,gxm,gym,gzm,N,dim,dof;
22247c6ae99SBarry Smith 
22347c6ae99SBarry Smith   PetscFunctionBegin;
224aa219208SBarry Smith   ierr = DMDAGetCorners(da,&xs,&ys,&zs,&xm,&ym,&zm);CHKERRQ(ierr);
225aa219208SBarry Smith   ierr = DMDAGetGhostCorners(da,&gxs,&gys,&gzs,&gxm,&gym,&gzm);CHKERRQ(ierr);
2261321219cSEthan Coon   ierr = DMDAGetInfo(da,&dim,0,0,0,0,0,0,&dof,0,0,0,0,0);CHKERRQ(ierr);
22747c6ae99SBarry Smith 
22847c6ae99SBarry Smith   /* Handle case where user passes in global vector as opposed to local */
22947c6ae99SBarry Smith   ierr = VecGetLocalSize(vec,&N);CHKERRQ(ierr);
23047c6ae99SBarry Smith   if (N == xm*ym*zm*dof) {
23147c6ae99SBarry Smith     gxm = xm;
23247c6ae99SBarry Smith     gym = ym;
23347c6ae99SBarry Smith     gzm = zm;
23447c6ae99SBarry Smith     gxs = xs;
23547c6ae99SBarry Smith     gys = ys;
23647c6ae99SBarry Smith     gzs = zs;
23747c6ae99SBarry Smith   }
23847c6ae99SBarry Smith 
23947c6ae99SBarry Smith   if (dim == 1) {
24047c6ae99SBarry Smith     ierr = VecRestoreArray2d(vec,gxm,dof,gxs,0,(PetscScalar***)array);CHKERRQ(ierr);
24147c6ae99SBarry Smith   } else if (dim == 2) {
24247c6ae99SBarry Smith     ierr = VecRestoreArray3d(vec,gym,gxm,dof,gys,gxs,0,(PetscScalar****)array);CHKERRQ(ierr);
24347c6ae99SBarry Smith   } else if (dim == 3) {
24447c6ae99SBarry Smith     ierr = VecRestoreArray4d(vec,gzm,gym,gxm,dof,gzs,gys,gxs,0,(PetscScalar*****)array);CHKERRQ(ierr);
24547c6ae99SBarry Smith   } else {
246aa219208SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_CORRUPT,"DMDA dimension not 1, 2, or 3, it is %D\n",dim);
24747c6ae99SBarry Smith   }
24847c6ae99SBarry Smith   PetscFunctionReturn(0);
24947c6ae99SBarry Smith }
25047c6ae99SBarry Smith 
25147c6ae99SBarry Smith 
25247c6ae99SBarry Smith 
25347c6ae99SBarry Smith 
25447c6ae99SBarry Smith 
25547c6ae99SBarry Smith 
25647c6ae99SBarry Smith 
25747c6ae99SBarry Smith 
25847c6ae99SBarry Smith 
25947c6ae99SBarry Smith 
26047c6ae99SBarry Smith 
26147c6ae99SBarry Smith 
26247c6ae99SBarry Smith 
263