xref: /petsc/src/dm/impls/da/dagetarray.c (revision 9a42bb27a39f0cdf3306a1e22d33cd9809484eaa)
147c6ae99SBarry Smith #define PETSCDM_DLL
247c6ae99SBarry Smith 
347c6ae99SBarry Smith #include "petscda.h"    /*I   "petscda.h"   I*/
447c6ae99SBarry Smith 
547c6ae99SBarry Smith #undef __FUNCT__
647c6ae99SBarry Smith #define __FUNCT__ "DAVecGetArray"
747c6ae99SBarry Smith /*@C
847c6ae99SBarry Smith    DAVecGetArray - Returns a multiple dimension array that shares data with
947c6ae99SBarry Smith       the underlying vector and is indexed using the global dimensions.
1047c6ae99SBarry Smith 
1147c6ae99SBarry Smith    Not Collective
1247c6ae99SBarry Smith 
1347c6ae99SBarry Smith    Input Parameter:
1447c6ae99SBarry Smith +  da - the distributed array
1547c6ae99SBarry Smith -  vec - the vector, either a vector the same size as one obtained with
1647c6ae99SBarry Smith          DACreateGlobalVector() or DACreateLocalVector()
1747c6ae99SBarry Smith 
1847c6ae99SBarry Smith    Output Parameter:
1947c6ae99SBarry Smith .  array - the array
2047c6ae99SBarry Smith 
2147c6ae99SBarry Smith    Notes:
2247c6ae99SBarry Smith     Call DAVecRestoreArray() once you have finished accessing the vector entries.
2347c6ae99SBarry Smith 
2447c6ae99SBarry Smith     In C, the indexing is "backwards" from what expects: array[k][j][i] NOT array[i][j][k]!
2547c6ae99SBarry Smith 
2647c6ae99SBarry Smith     If vec is a local vector (obtained with DACreateLocalVector() etc) then they ghost point locations are accessable. If it is
2747c6ae99SBarry Smith     a global vector then the ghost points are not accessable. Of course with the local vector you will have had to do the
2847c6ae99SBarry Smith 
29*9a42bb27SBarry Smith     appropriate DMLocalToGlobalBegin() and DMLocalToGlobalEnd() to have correct values in the ghost locations.
3047c6ae99SBarry Smith 
3147c6ae99SBarry Smith   Fortran Notes: From Fortran use DAVecGetArrayF90() and pass for the array type PetscScalar,pointer :: array(:,...,:) of the appropriate
3247c6ae99SBarry Smith        dimension. For a DA created with a dof of 1 use the dimension of the DA, for a DA created with a dof greater than 1 use one more than the
3347c6ae99SBarry Smith        dimension of the DA. The order of the indices is array(xs:xs+xm-1,ys:ys+ym-1,zs:zs+zm-1) (when dof is 1) otherwise
3447c6ae99SBarry Smith        array(1:dof,xs:xs+xm-1,ys:ys+ym-1,zs:zs+zm-1) where the values are obtained from
3547c6ae99SBarry Smith        DAGetCorners() for a global array or DAGetGhostCorners() for a local array. Include finclude/petscda.h90 to access this routine.
3647c6ae99SBarry Smith 
3747c6ae99SBarry Smith   Due to bugs in the compiler DAVecGetArrayF90() does not work with gfortran versions before 2.5
3847c6ae99SBarry Smith 
3947c6ae99SBarry Smith   Level: intermediate
4047c6ae99SBarry Smith 
4147c6ae99SBarry Smith .keywords: distributed array, get, corners, nodes, local indices, coordinates
4247c6ae99SBarry Smith 
4347c6ae99SBarry Smith .seealso: DAGetGhostCorners(), DAGetCorners(), VecGetArray(), VecRestoreArray(), DAVecRestoreArray(), DAVecRestoreArrayDOF()
4447c6ae99SBarry Smith           DAVecGetarrayDOF()
4547c6ae99SBarry Smith @*/
46*9a42bb27SBarry Smith PetscErrorCode PETSCDM_DLLEXPORT DAVecGetArray(DM da,Vec vec,void *array)
4747c6ae99SBarry Smith {
4847c6ae99SBarry Smith   PetscErrorCode ierr;
4947c6ae99SBarry Smith   PetscInt       xs,ys,zs,xm,ym,zm,gxs,gys,gzs,gxm,gym,gzm,N,dim,dof;
5047c6ae99SBarry Smith 
5147c6ae99SBarry Smith   PetscFunctionBegin;
5247c6ae99SBarry Smith   ierr = DAGetCorners(da,&xs,&ys,&zs,&xm,&ym,&zm);CHKERRQ(ierr);
5347c6ae99SBarry Smith   ierr = DAGetGhostCorners(da,&gxs,&gys,&gzs,&gxm,&gym,&gzm);CHKERRQ(ierr);
5447c6ae99SBarry Smith   ierr = DAGetInfo(da,&dim,0,0,0,0,0,0,&dof,0,0,0);CHKERRQ(ierr);
5547c6ae99SBarry Smith 
5647c6ae99SBarry Smith   /* Handle case where user passes in global vector as opposed to local */
5747c6ae99SBarry Smith   ierr = VecGetLocalSize(vec,&N);CHKERRQ(ierr);
5847c6ae99SBarry Smith   if (N == xm*ym*zm*dof) {
5947c6ae99SBarry Smith     gxm = xm;
6047c6ae99SBarry Smith     gym = ym;
6147c6ae99SBarry Smith     gzm = zm;
6247c6ae99SBarry Smith     gxs = xs;
6347c6ae99SBarry Smith     gys = ys;
6447c6ae99SBarry Smith     gzs = zs;
6547c6ae99SBarry Smith   } else if (N != gxm*gym*gzm*dof) {
6647c6ae99SBarry Smith     SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Vector local size %D is not compatible with DA local sizes %D %D\n",N,xm*ym*zm*dof,gxm*gym*gzm*dof);
6747c6ae99SBarry Smith   }
6847c6ae99SBarry Smith 
6947c6ae99SBarry Smith   if (dim == 1) {
7047c6ae99SBarry Smith     ierr = VecGetArray1d(vec,gxm*dof,gxs*dof,(PetscScalar **)array);CHKERRQ(ierr);
7147c6ae99SBarry Smith   } else if (dim == 2) {
7247c6ae99SBarry Smith     ierr = VecGetArray2d(vec,gym,gxm*dof,gys,gxs*dof,(PetscScalar***)array);CHKERRQ(ierr);
7347c6ae99SBarry Smith   } else if (dim == 3) {
7447c6ae99SBarry Smith     ierr = VecGetArray3d(vec,gzm,gym,gxm*dof,gzs,gys,gxs*dof,(PetscScalar****)array);CHKERRQ(ierr);
7547c6ae99SBarry Smith   } else {
7647c6ae99SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_CORRUPT,"DA dimension not 1, 2, or 3, it is %D\n",dim);
7747c6ae99SBarry Smith   }
7847c6ae99SBarry Smith 
7947c6ae99SBarry Smith   PetscFunctionReturn(0);
8047c6ae99SBarry Smith }
8147c6ae99SBarry Smith 
8247c6ae99SBarry Smith #undef __FUNCT__
8347c6ae99SBarry Smith #define __FUNCT__ "DAVecRestoreArray"
8447c6ae99SBarry Smith /*@
8547c6ae99SBarry Smith    DAVecRestoreArray - Restores a multiple dimension array obtained with DAVecGetArray()
8647c6ae99SBarry Smith 
8747c6ae99SBarry Smith    Not Collective
8847c6ae99SBarry Smith 
8947c6ae99SBarry Smith    Input Parameter:
9047c6ae99SBarry Smith +  da - the distributed array
9147c6ae99SBarry Smith .  vec - the vector, either a vector the same size as one obtained with
9247c6ae99SBarry Smith          DACreateGlobalVector() or DACreateLocalVector()
9347c6ae99SBarry Smith -  array - the array
9447c6ae99SBarry Smith 
9547c6ae99SBarry Smith   Level: intermediate
9647c6ae99SBarry Smith 
9747c6ae99SBarry Smith   Fortran Notes: From Fortran use DAVecRestoreArrayF90()
9847c6ae99SBarry Smith 
9947c6ae99SBarry Smith .keywords: distributed array, get, corners, nodes, local indices, coordinates
10047c6ae99SBarry Smith 
10147c6ae99SBarry Smith .seealso: DAGetGhostCorners(), DAGetCorners(), VecGetArray(), VecRestoreArray(), DAVecGetArray()
10247c6ae99SBarry Smith @*/
103*9a42bb27SBarry Smith PetscErrorCode PETSCDM_DLLEXPORT DAVecRestoreArray(DM da,Vec vec,void *array)
10447c6ae99SBarry Smith {
10547c6ae99SBarry Smith   PetscErrorCode ierr;
10647c6ae99SBarry Smith   PetscInt       xs,ys,zs,xm,ym,zm,gxs,gys,gzs,gxm,gym,gzm,N,dim,dof;
10747c6ae99SBarry Smith 
10847c6ae99SBarry Smith   PetscFunctionBegin;
10947c6ae99SBarry Smith   ierr = DAGetCorners(da,&xs,&ys,&zs,&xm,&ym,&zm);CHKERRQ(ierr);
11047c6ae99SBarry Smith   ierr = DAGetGhostCorners(da,&gxs,&gys,&gzs,&gxm,&gym,&gzm);CHKERRQ(ierr);
11147c6ae99SBarry Smith   ierr = DAGetInfo(da,&dim,0,0,0,0,0,0,&dof,0,0,0);CHKERRQ(ierr);
11247c6ae99SBarry Smith 
11347c6ae99SBarry Smith   /* Handle case where user passes in global vector as opposed to local */
11447c6ae99SBarry Smith   ierr = VecGetLocalSize(vec,&N);CHKERRQ(ierr);
11547c6ae99SBarry Smith   if (N == xm*ym*zm*dof) {
11647c6ae99SBarry Smith     gxm = xm;
11747c6ae99SBarry Smith     gym = ym;
11847c6ae99SBarry Smith     gzm = zm;
11947c6ae99SBarry Smith     gxs = xs;
12047c6ae99SBarry Smith     gys = ys;
12147c6ae99SBarry Smith     gzs = zs;
12247c6ae99SBarry Smith   } else if (N != gxm*gym*gzm*dof) {
12347c6ae99SBarry Smith     SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Vector local size %D is not compatible with DA local sizes %D %D\n",N,xm*ym*zm*dof,gxm*gym*gzm*dof);
12447c6ae99SBarry Smith   }
12547c6ae99SBarry Smith 
12647c6ae99SBarry Smith   if (dim == 1) {
12747c6ae99SBarry Smith     ierr = VecRestoreArray1d(vec,gxm*dof,gxs*dof,(PetscScalar **)array);CHKERRQ(ierr);
12847c6ae99SBarry Smith   } else if (dim == 2) {
12947c6ae99SBarry Smith     ierr = VecRestoreArray2d(vec,gym,gxm*dof,gys,gxs*dof,(PetscScalar***)array);CHKERRQ(ierr);
13047c6ae99SBarry Smith   } else if (dim == 3) {
13147c6ae99SBarry Smith     ierr = VecRestoreArray3d(vec,gzm,gym,gxm*dof,gzs,gys,gxs*dof,(PetscScalar****)array);CHKERRQ(ierr);
13247c6ae99SBarry Smith   } else {
13347c6ae99SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_CORRUPT,"DA dimension not 1, 2, or 3, it is %D\n",dim);
13447c6ae99SBarry Smith   }
13547c6ae99SBarry Smith   PetscFunctionReturn(0);
13647c6ae99SBarry Smith }
13747c6ae99SBarry Smith 
13847c6ae99SBarry Smith #undef __FUNCT__
13947c6ae99SBarry Smith #define __FUNCT__ "DAVecGetArrayDOF"
14047c6ae99SBarry Smith /*@C
14147c6ae99SBarry Smith    DAVecGetArrayDOF - Returns a multiple dimension array that shares data with
14247c6ae99SBarry Smith       the underlying vector and is indexed using the global dimensions.
14347c6ae99SBarry Smith 
14447c6ae99SBarry Smith    Not Collective
14547c6ae99SBarry Smith 
14647c6ae99SBarry Smith    Input Parameter:
14747c6ae99SBarry Smith +  da - the distributed array
14847c6ae99SBarry Smith -  vec - the vector, either a vector the same size as one obtained with
14947c6ae99SBarry Smith          DACreateGlobalVector() or DACreateLocalVector()
15047c6ae99SBarry Smith 
15147c6ae99SBarry Smith    Output Parameter:
15247c6ae99SBarry Smith .  array - the array
15347c6ae99SBarry Smith 
15447c6ae99SBarry Smith    Notes:
15547c6ae99SBarry Smith     Call DAVecRestoreArrayDOF() once you have finished accessing the vector entries.
15647c6ae99SBarry Smith 
15747c6ae99SBarry Smith     In C, the indexing is "backwards" from what expects: array[k][j][i][DOF] NOT array[i][j][k][DOF]!
15847c6ae99SBarry Smith 
15947c6ae99SBarry Smith   Level: intermediate
16047c6ae99SBarry Smith 
16147c6ae99SBarry Smith .keywords: distributed array, get, corners, nodes, local indices, coordinates
16247c6ae99SBarry Smith 
16347c6ae99SBarry Smith .seealso: DAGetGhostCorners(), DAGetCorners(), VecGetArray(), VecRestoreArray(), DAVecRestoreArray(), DAVecGetArray(), DAVecRestoreArrayDOF()
16447c6ae99SBarry Smith @*/
165*9a42bb27SBarry Smith PetscErrorCode PETSCDM_DLLEXPORT DAVecGetArrayDOF(DM da,Vec vec,void *array)
16647c6ae99SBarry Smith {
16747c6ae99SBarry Smith   PetscErrorCode ierr;
16847c6ae99SBarry Smith   PetscInt       xs,ys,zs,xm,ym,zm,gxs,gys,gzs,gxm,gym,gzm,N,dim,dof;
16947c6ae99SBarry Smith 
17047c6ae99SBarry Smith   PetscFunctionBegin;
17147c6ae99SBarry Smith   ierr = DAGetCorners(da,&xs,&ys,&zs,&xm,&ym,&zm);CHKERRQ(ierr);
17247c6ae99SBarry Smith   ierr = DAGetGhostCorners(da,&gxs,&gys,&gzs,&gxm,&gym,&gzm);CHKERRQ(ierr);
17347c6ae99SBarry Smith   ierr = DAGetInfo(da,&dim,0,0,0,0,0,0,&dof,0,0,0);CHKERRQ(ierr);
17447c6ae99SBarry Smith 
17547c6ae99SBarry Smith   /* Handle case where user passes in global vector as opposed to local */
17647c6ae99SBarry Smith   ierr = VecGetLocalSize(vec,&N);CHKERRQ(ierr);
17747c6ae99SBarry Smith   if (N == xm*ym*zm*dof) {
17847c6ae99SBarry Smith     gxm = xm;
17947c6ae99SBarry Smith     gym = ym;
18047c6ae99SBarry Smith     gzm = zm;
18147c6ae99SBarry Smith     gxs = xs;
18247c6ae99SBarry Smith     gys = ys;
18347c6ae99SBarry Smith     gzs = zs;
18447c6ae99SBarry Smith   } else if (N != gxm*gym*gzm*dof) {
18547c6ae99SBarry Smith     SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Vector local size %D is not compatible with DA local sizes %D %D\n",N,xm*ym*zm*dof,gxm*gym*gzm*dof);
18647c6ae99SBarry Smith   }
18747c6ae99SBarry Smith 
18847c6ae99SBarry Smith   if (dim == 1) {
18947c6ae99SBarry Smith     ierr = VecGetArray2d(vec,gxm,dof,gxs,0,(PetscScalar ***)array);CHKERRQ(ierr);
19047c6ae99SBarry Smith   } else if (dim == 2) {
19147c6ae99SBarry Smith     ierr = VecGetArray3d(vec,gym,gxm,dof,gys,gxs,0,(PetscScalar****)array);CHKERRQ(ierr);
19247c6ae99SBarry Smith   } else if (dim == 3) {
19347c6ae99SBarry Smith     ierr = VecGetArray4d(vec,gzm,gym,gxm,dof,gzs,gys,gxs,0,(PetscScalar*****)array);CHKERRQ(ierr);
19447c6ae99SBarry Smith   } else {
19547c6ae99SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_CORRUPT,"DA dimension not 1, 2, or 3, it is %D\n",dim);
19647c6ae99SBarry Smith   }
19747c6ae99SBarry Smith   PetscFunctionReturn(0);
19847c6ae99SBarry Smith }
19947c6ae99SBarry Smith 
20047c6ae99SBarry Smith #undef __FUNCT__
20147c6ae99SBarry Smith #define __FUNCT__ "DAVecRestoreArrayDOF"
20247c6ae99SBarry Smith /*@
20347c6ae99SBarry Smith    DAVecRestoreArrayDOF - Restores a multiple dimension array obtained with DAVecGetArrayDOF()
20447c6ae99SBarry Smith 
20547c6ae99SBarry Smith    Not Collective
20647c6ae99SBarry Smith 
20747c6ae99SBarry Smith    Input Parameter:
20847c6ae99SBarry Smith +  da - the distributed array
20947c6ae99SBarry Smith .  vec - the vector, either a vector the same size as one obtained with
21047c6ae99SBarry Smith          DACreateGlobalVector() or DACreateLocalVector()
21147c6ae99SBarry Smith -  array - the array
21247c6ae99SBarry Smith 
21347c6ae99SBarry Smith   Level: intermediate
21447c6ae99SBarry Smith 
21547c6ae99SBarry Smith .keywords: distributed array, get, corners, nodes, local indices, coordinates
21647c6ae99SBarry Smith 
21747c6ae99SBarry Smith .seealso: DAGetGhostCorners(), DAGetCorners(), VecGetArray(), VecRestoreArray(), DAVecGetArray(), DAVecGetArrayDOF(), DAVecRestoreArrayDOF()
21847c6ae99SBarry Smith @*/
219*9a42bb27SBarry Smith PetscErrorCode PETSCDM_DLLEXPORT DAVecRestoreArrayDOF(DM da,Vec vec,void *array)
22047c6ae99SBarry Smith {
22147c6ae99SBarry Smith   PetscErrorCode ierr;
22247c6ae99SBarry Smith   PetscInt       xs,ys,zs,xm,ym,zm,gxs,gys,gzs,gxm,gym,gzm,N,dim,dof;
22347c6ae99SBarry Smith 
22447c6ae99SBarry Smith   PetscFunctionBegin;
22547c6ae99SBarry Smith   ierr = DAGetCorners(da,&xs,&ys,&zs,&xm,&ym,&zm);CHKERRQ(ierr);
22647c6ae99SBarry Smith   ierr = DAGetGhostCorners(da,&gxs,&gys,&gzs,&gxm,&gym,&gzm);CHKERRQ(ierr);
22747c6ae99SBarry Smith   ierr = DAGetInfo(da,&dim,0,0,0,0,0,0,&dof,0,0,0);CHKERRQ(ierr);
22847c6ae99SBarry Smith 
22947c6ae99SBarry Smith   /* Handle case where user passes in global vector as opposed to local */
23047c6ae99SBarry Smith   ierr = VecGetLocalSize(vec,&N);CHKERRQ(ierr);
23147c6ae99SBarry Smith   if (N == xm*ym*zm*dof) {
23247c6ae99SBarry Smith     gxm = xm;
23347c6ae99SBarry Smith     gym = ym;
23447c6ae99SBarry Smith     gzm = zm;
23547c6ae99SBarry Smith     gxs = xs;
23647c6ae99SBarry Smith     gys = ys;
23747c6ae99SBarry Smith     gzs = zs;
23847c6ae99SBarry Smith   }
23947c6ae99SBarry Smith 
24047c6ae99SBarry Smith   if (dim == 1) {
24147c6ae99SBarry Smith     ierr = VecRestoreArray2d(vec,gxm,dof,gxs,0,(PetscScalar***)array);CHKERRQ(ierr);
24247c6ae99SBarry Smith   } else if (dim == 2) {
24347c6ae99SBarry Smith     ierr = VecRestoreArray3d(vec,gym,gxm,dof,gys,gxs,0,(PetscScalar****)array);CHKERRQ(ierr);
24447c6ae99SBarry Smith   } else if (dim == 3) {
24547c6ae99SBarry Smith     ierr = VecRestoreArray4d(vec,gzm,gym,gxm,dof,gzs,gys,gxs,0,(PetscScalar*****)array);CHKERRQ(ierr);
24647c6ae99SBarry Smith   } else {
24747c6ae99SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_CORRUPT,"DA dimension not 1, 2, or 3, it is %D\n",dim);
24847c6ae99SBarry Smith   }
24947c6ae99SBarry Smith   PetscFunctionReturn(0);
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 
26347c6ae99SBarry Smith 
264