#include "tao.h" /*I "tao.h" I*/ #include "petsc-private/matimpl.h" #include "../src/tao/matrix/submatfree.h" #include "tao_util.h" /*I "tao_util.h" I*/ #undef __FUNCT__ #define __FUNCT__ "VecWhichEqual" /*@ VecWhichEqual - Creates an index set containing the indices where the vectors Vec1 and Vec2 have identical elements. Collective on S Input Parameters: . Vec1, Vec2 - the two vectors to compare OutputParameter: . S - The index set containing the indices i where vec1[i] == vec2[i] Level: advanced @*/ PetscErrorCode VecWhichEqual(Vec Vec1, Vec Vec2, IS * S) { PetscErrorCode ierr; PetscInt i,n_same=0; PetscInt n,low,high,low2,high2; PetscInt *same; PetscReal *v1,*v2; MPI_Comm comm; PetscFunctionBegin; PetscValidHeaderSpecific(Vec1,VEC_CLASSID,1); PetscValidHeaderSpecific(Vec2,VEC_CLASSID,2); PetscCheckSameComm(Vec1,1,Vec2,2); ierr = VecGetOwnershipRange(Vec1, &low, &high); CHKERRQ(ierr); ierr = VecGetOwnershipRange(Vec2, &low2, &high2); CHKERRQ(ierr); if ( low != low2 || high != high2 ) SETERRQ(PETSC_COMM_SELF,1,"Vectors must be identically loaded over processors"); ierr = VecGetLocalSize(Vec1,&n); CHKERRQ(ierr); if (n>0){ if (Vec1 == Vec2){ ierr = VecGetArray(Vec1,&v1); CHKERRQ(ierr); v2=v1; } else { ierr = VecGetArray(Vec1,&v1); CHKERRQ(ierr); ierr = VecGetArray(Vec2,&v2); CHKERRQ(ierr); } ierr = PetscMalloc( n*sizeof(PetscInt),&same ); CHKERRQ(ierr); for (i=0; i0){ if (Vec1 == Vec2){ ierr = VecGetArray(Vec1,&v1); CHKERRQ(ierr); v2=v1; } else { ierr = VecGetArray(Vec1,&v1); CHKERRQ(ierr); ierr = VecGetArray(Vec2,&v2); CHKERRQ(ierr); } ierr = PetscMalloc( n*sizeof(PetscInt),< ); CHKERRQ(ierr); for (i=0; i Vec2 Collective on S Input Parameters: . Vec1, Vec2 - the two vectors to compare OutputParameter: . S - The index set containing the indices i where vec1[i] > vec2[i] Level: advanced @*/ PetscErrorCode VecWhichGreaterThan(Vec Vec1, Vec Vec2, IS * S) { int ierr; PetscInt n,low,high,low2,high2,n_gt=0,i; PetscInt *gt=NULL; PetscReal *v1,*v2; MPI_Comm comm; PetscFunctionBegin; PetscValidHeaderSpecific(Vec1,VEC_CLASSID,1); PetscValidHeaderSpecific(Vec2,VEC_CLASSID,2); PetscCheckSameComm(Vec1,1,Vec2,2); ierr = VecGetOwnershipRange(Vec1, &low, &high); CHKERRQ(ierr); ierr = VecGetOwnershipRange(Vec2, &low2, &high2); CHKERRQ(ierr); if ( low != low2 || high != high2 ) SETERRQ(PETSC_COMM_SELF,1,"Vectors must be identically loaded over processors"); ierr = VecGetLocalSize(Vec1,&n); CHKERRQ(ierr); if (n>0){ if (Vec1 == Vec2){ ierr = VecGetArray(Vec1,&v1); CHKERRQ(ierr); v2=v1; } else { ierr = VecGetArray(Vec1,&v1); CHKERRQ(ierr); ierr = VecGetArray(Vec2,&v2); CHKERRQ(ierr); } ierr = PetscMalloc( n*sizeof(PetscInt), > ); CHKERRQ(ierr); for (i=0; i v2[i]) {gt[n_gt]=low+i; n_gt++;} } if (Vec1 == Vec2){ ierr = VecRestoreArray(Vec1,&v1); CHKERRQ(ierr); } else { ierr = VecRestoreArray(Vec1,&v1); CHKERRQ(ierr); ierr = VecRestoreArray(Vec2,&v2); CHKERRQ(ierr); } } else{ n_gt=0; gt=NULL; } ierr = PetscObjectGetComm((PetscObject)Vec1,&comm);CHKERRQ(ierr); ierr = ISCreateGeneral(comm,n_gt,gt,PETSC_COPY_VALUES,S);CHKERRQ(ierr); if (gt) { ierr = PetscFree(gt); CHKERRQ(ierr); } PetscFunctionReturn(0); } #undef __FUNCT__ #define __FUNCT__ "VecWhichBetween" /*@ VecWhichBetween - Creates an index set containing the indices where VecLow < V < VecHigh Collective on S Input Parameters: + VecLow - lower bound . V - Vector to compare - VecHigh - higher bound OutputParameter: . S - The index set containing the indices i where veclow[i] < v[i] < vechigh[i] Level: advanced @*/ PetscErrorCode VecWhichBetween(Vec VecLow, Vec V, Vec VecHigh, IS *S) { PetscErrorCode ierr; PetscInt n,low,high,low2,high2,low3,high3,n_vm=0; PetscInt *vm,i; PetscReal *v1,*v2,*vmiddle; MPI_Comm comm; PetscValidHeaderSpecific(V,VEC_CLASSID,2); PetscCheckSameComm(V,2,VecLow,1); PetscCheckSameComm(V,2,VecHigh,3); ierr = VecGetOwnershipRange(VecLow, &low, &high); CHKERRQ(ierr); ierr = VecGetOwnershipRange(VecHigh, &low2, &high2); CHKERRQ(ierr); ierr = VecGetOwnershipRange(V, &low3, &high3); CHKERRQ(ierr); if ( low!=low2 || high!=high2 || low!=low3 || high!=high3 ) SETERRQ(PETSC_COMM_SELF,1,"Vectors must be identically loaded over processors"); ierr = VecGetLocalSize(VecLow,&n); CHKERRQ(ierr); if (n>0){ ierr = VecGetArray(VecLow,&v1); CHKERRQ(ierr); if (VecLow != VecHigh){ ierr = VecGetArray(VecHigh,&v2); CHKERRQ(ierr); } else { v2=v1; } if ( V != VecLow && V != VecHigh){ ierr = VecGetArray(V,&vmiddle); CHKERRQ(ierr); } else if ( V==VecLow ){ vmiddle=v1; } else { vmiddle =v2; } ierr = PetscMalloc( n*sizeof(PetscInt), &vm ); CHKERRQ(ierr); for (i=0; i0){ ierr = VecGetArray(VecLow,&v1); CHKERRQ(ierr); if (VecLow != VecHigh){ ierr = VecGetArray(VecHigh,&v2); CHKERRQ(ierr); } else { v2=v1; } if ( V != VecLow && V != VecHigh){ ierr = VecGetArray(V,&vmiddle); CHKERRQ(ierr); } else if ( V==VecLow ){ vmiddle=v1; } else { vmiddle =v2; } ierr = PetscMalloc( n*sizeof(PetscInt), &vm ); CHKERRQ(ierr); for (i=0; i (fhigh-flow)) { SETERRQ2(PETSC_COMM_WORLD,1,"IS local size %d > Vec local size %d",nlocal,fhigh-flow); } for (i=0;i-1){ ss[n]=tt[i]; n++; } } ierr = ISRestoreIndices(S, &s); CHKERRQ(ierr); ierr = PetscObjectGetComm((PetscObject)S,&comm);CHKERRQ(ierr); ierr = ISCreateGeneral(comm,n,ss,PETSC_COPY_VALUES,T);CHKERRQ(ierr); if (tt) { ierr = PetscFree(tt); CHKERRQ(ierr); } if (ss) { ierr = PetscFree(ss); CHKERRQ(ierr); } PetscFunctionReturn(0); } #undef __FUNCT__ #define __FUNCT__ "VecISSetToConstant" /*@ VecISSetToConstant - Sets the elements of a vector, specified by an index set, to a constant Input Parameter: + S - a PETSc IS . c - the constant - V - a Vec .seealso VecSet() Level: advanced @*/ PetscErrorCode VecISSetToConstant(IS S, PetscReal c, Vec V){ PetscErrorCode ierr; PetscInt nloc,low,high,i; const PetscInt *s; PetscReal *v; PetscFunctionBegin; PetscValidHeaderSpecific(V,VEC_CLASSID,3); PetscValidHeaderSpecific(S,IS_CLASSID,1); PetscValidType(V,3); PetscCheckSameComm(V,3,S,1); ierr = VecGetOwnershipRange(V, &low, &high); CHKERRQ(ierr); ierr = ISGetLocalSize(S,&nloc);CHKERRQ(ierr); ierr = ISGetIndices(S, &s); CHKERRQ(ierr); ierr = VecGetArray(V,&v); CHKERRQ(ierr); for (i=0; i