PetscErrorCode VecSwap_MultiVec(Vec x, Vec y) { #if !defined(NDEBUG) TBOX_ASSERT(x); TBOX_ASSERT(y); #endif Vec_MultiVec* mx = static_cast<Vec_MultiVec*>(x->data); Vec_MultiVec* my = static_cast<Vec_MultiVec*>(y->data); #if !defined(NDEBUG) TBOX_ASSERT(mx); TBOX_ASSERT(my); TBOX_ASSERT(mx->n == my->n); #endif PetscErrorCode ierr; for (PetscInt k = 0; k < mx->n; ++k) { ierr = VecSwap(mx->array[k], my->array[k]); CHKERRQ(ierr); } ierr = PetscObjectStateIncrease(reinterpret_cast<PetscObject>(x)); CHKERRQ(ierr); ierr = PetscObjectStateIncrease(reinterpret_cast<PetscObject>(y)); CHKERRQ(ierr); PetscFunctionReturn(0); } // VecSwap_MultiVec
PetscErrorCode IBImplicitStaggeredHierarchyIntegrator::compositeIBJacobianApply(Vec x, Vec f) { PetscErrorCode ierr; const double half_time = d_integrator_time + 0.5 * d_current_dt; Vec* component_sol_vecs; Vec* component_rhs_vecs; ierr = VecMultiVecGetSubVecs(x, &component_sol_vecs); IBTK_CHKERRQ(ierr); ierr = VecMultiVecGetSubVecs(f, &component_rhs_vecs); IBTK_CHKERRQ(ierr); Pointer<SAMRAIVectorReal<NDIM, double> > u = PETScSAMRAIVectorReal::getSAMRAIVector(component_sol_vecs[0]); Pointer<SAMRAIVectorReal<NDIM, double> > f_u = PETScSAMRAIVectorReal::getSAMRAIVector(component_rhs_vecs[0]); Pointer<Variable<NDIM> > u_var = d_ins_hier_integrator->getVelocityVariable(); const int u_idx = u->getComponentDescriptorIndex(0); const int f_u_idx = f_u->getComponentDescriptorIndex(0); Vec X = component_sol_vecs[1]; Vec R = component_rhs_vecs[1]; // Evaluate the Eulerian terms. d_stokes_op->setHomogeneousBc(true); d_stokes_op->apply(*u, *f_u); d_ib_implicit_ops->computeLinearizedLagrangianForce(X, half_time); if (d_enable_logging) plog << d_object_name << "::integrateHierarchy(): spreading Lagrangian force to the Eulerian grid\n"; d_hier_velocity_data_ops->setToScalar(d_f_idx, 0.0); d_u_phys_bdry_op->setPatchDataIndex(d_f_idx); d_ib_implicit_ops->spreadLinearizedForce(d_f_idx, d_u_phys_bdry_op, getProlongRefineSchedules(d_object_name + "::f"), half_time); d_hier_velocity_data_ops->subtract(f_u_idx, f_u_idx, d_f_idx); ierr = PetscObjectStateIncrease(reinterpret_cast<PetscObject>(component_rhs_vecs[0])); IBTK_CHKERRQ(ierr); // Evaluate the Lagrangian terms. d_hier_velocity_data_ops->scale(d_u_idx, 0.5, u_idx); d_u_phys_bdry_op->setPatchDataIndex(d_u_idx); d_ib_implicit_ops->interpolateLinearizedVelocity( d_u_idx, getCoarsenSchedules(d_object_name + "::u::CONSERVATIVE_COARSEN"), getGhostfillRefineSchedules(d_object_name + "::u"), half_time); d_ib_implicit_ops->computeLinearizedResidual(X, R); // Ensure that PETSc sees that the state of the RHS vector has changed. // This is a nasty hack. ierr = PetscObjectStateIncrease(reinterpret_cast<PetscObject>(f)); IBTK_CHKERRQ(ierr); return ierr; } // compositeIBJacobianApply
/*@ BVInsertConstraints - Insert a set of vectors as constraints. Collective on BV Input Parameters: + V - basis vectors - C - set of vectors to be inserted as constraints Input/Output Parameter: . nc - number of input vectors, on output the number of linearly independent vectors Notes: The constraints are relevant only during orthogonalization. Constraint vectors span a subspace that is deflated in every orthogonalization operation, so they are intended for removing those directions from the orthogonal basis computed in regular BV columns. Constraints are not stored in regular BV colums, but in a special part of the storage. They can be accessed with negative indices in BVGetColumn(). This operation is DESTRUCTIVE, meaning that all data contained in the columns of V is lost. This is typically invoked just after creating the BV. Once a set of constraints has been set, it is not allowed to call this function again. The vectors are copied one by one and then orthogonalized against the previous ones. If any of them is linearly dependent then it is discarded and the value of nc is decreased. The behaviour is similar to BVInsertVecs(). Level: advanced .seealso: BVInsertVecs(), BVOrthogonalizeColumn(), BVGetColumn(), BVGetNumConstraints() @*/ PetscErrorCode BVInsertConstraints(BV V,PetscInt *nc,Vec *C) { PetscErrorCode ierr; PetscInt msave; PetscFunctionBegin; PetscValidHeaderSpecific(V,BV_CLASSID,1); PetscValidPointer(nc,2); PetscValidLogicalCollectiveInt(V,*nc,2); if (!*nc) PetscFunctionReturn(0); if (*nc<0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Number of constraints (given %D) cannot be negative",*nc); PetscValidPointer(C,3); PetscValidHeaderSpecific(*C,VEC_CLASSID,3); PetscValidType(V,1); BVCheckSizes(V,1); PetscCheckSameComm(V,1,*C,3); if (V->nc) SETERRQ(PetscObjectComm((PetscObject)V),PETSC_ERR_ARG_WRONGSTATE,"Constraints already present in this BV object"); if (V->ci[0]!=-1 || V->ci[1]!=-1) SETERRQ(PetscObjectComm((PetscObject)V),PETSC_ERR_SUP,"Cannot call BVInsertConstraints after BVGetColumn"); msave = V->m; ierr = BVResize(V,*nc+V->m,PETSC_FALSE);CHKERRQ(ierr); ierr = BVInsertVecs(V,0,nc,C,PETSC_TRUE);CHKERRQ(ierr); V->nc = *nc; V->m = msave; V->ci[0] = -V->nc-1; V->ci[1] = -V->nc-1; ierr = PetscObjectStateIncrease((PetscObject)V);CHKERRQ(ierr); PetscFunctionReturn(0); }
PetscErrorCode VecAYPX_SAMRAI(Vec y, const PetscScalar alpha, Vec x) { IBTK_TIMER_START(t_vec_aypx); #if !defined(NDEBUG) TBOX_ASSERT(x); TBOX_ASSERT(y); #endif static const bool interior_only = false; if (MathUtilities<double>::equalEps(alpha, 1.0)) { PSVR_CAST2(y)->add(PSVR_CAST2(x), PSVR_CAST2(y), interior_only); } else if (MathUtilities<double>::equalEps(alpha, -1.0)) { PSVR_CAST2(y)->subtract(PSVR_CAST2(x), PSVR_CAST2(y), interior_only); } else { PSVR_CAST2(y)->axpy(alpha, PSVR_CAST2(y), PSVR_CAST2(x), interior_only); } int ierr = PetscObjectStateIncrease(reinterpret_cast<PetscObject>(y)); IBTK_CHKERRQ(ierr); IBTK_TIMER_STOP(t_vec_aypx); PetscFunctionReturn(0); } // VecAYPX
/*@ BVInsertVec - Insert a vector into the specified column. Collective on BV Input Parameters: + V - basis vectors . j - the column of V to be overwritten - w - the vector to be copied Level: intermediate .seealso: BVInsertVecs() @*/ PetscErrorCode BVInsertVec(BV V,PetscInt j,Vec w) { PetscErrorCode ierr; PetscInt n,N; Vec v; PetscFunctionBegin; PetscValidHeaderSpecific(V,BV_CLASSID,1); PetscValidLogicalCollectiveInt(V,j,2); PetscValidHeaderSpecific(w,VEC_CLASSID,3); PetscValidType(V,1); BVCheckSizes(V,1); PetscCheckSameComm(V,1,w,3); ierr = VecGetSize(w,&N);CHKERRQ(ierr); ierr = VecGetLocalSize(w,&n);CHKERRQ(ierr); if (N!=V->N || n!=V->n) SETERRQ4(PetscObjectComm((PetscObject)V),PETSC_ERR_ARG_INCOMP,"Vec sizes (global %D, local %D) do not match BV sizes (global %D, local %D)",N,n,V->N,V->n); if (j<-V->nc || j>=V->m) SETERRQ3(PetscObjectComm((PetscObject)V),PETSC_ERR_ARG_OUTOFRANGE,"Argument j has wrong value %D, should be between %D and %D",j,-V->nc,V->m-1); ierr = BVGetColumn(V,j,&v);CHKERRQ(ierr); ierr = VecCopy(w,v);CHKERRQ(ierr); ierr = BVRestoreColumn(V,j,&v);CHKERRQ(ierr); ierr = PetscObjectStateIncrease((PetscObject)V);CHKERRQ(ierr); PetscFunctionReturn(0); }
PetscErrorCode VecSwap_SAMRAI(Vec x, Vec y) { IBTK_TIMER_START(t_vec_swap); #if !defined(NDEBUG) TBOX_ASSERT(x); TBOX_ASSERT(y); #endif PSVR_CAST2(x)->swapVectors(PSVR_CAST2(y)); int ierr; ierr = PetscObjectStateIncrease(reinterpret_cast<PetscObject>(x)); IBTK_CHKERRQ(ierr); ierr = PetscObjectStateIncrease(reinterpret_cast<PetscObject>(y)); IBTK_CHKERRQ(ierr); IBTK_TIMER_STOP(t_vec_swap); PetscFunctionReturn(0); } // VecSwap
/*@ BVMultInPlaceTranspose - Update a set of vectors as V(:,s:e-1) = V*Q'(:,s:e-1). Logically Collective on BV Input Parameters: + Q - a sequential dense matrix . s - first column of V to be overwritten - e - first column of V not to be overwritten Input/Output Parameter: + V - basis vectors Notes: This is a variant of BVMultInPlace() where the conjugate transpose of Q is used. Level: intermediate .seealso: BVMultInPlace() @*/ PetscErrorCode BVMultInPlaceTranspose(BV V,Mat Q,PetscInt s,PetscInt e) { PetscErrorCode ierr; PetscBool match; PetscInt m,n; PetscFunctionBegin; PetscValidHeaderSpecific(V,BV_CLASSID,1); PetscValidHeaderSpecific(Q,MAT_CLASSID,2); PetscValidLogicalCollectiveInt(V,s,3); PetscValidLogicalCollectiveInt(V,e,4); PetscValidType(V,1); BVCheckSizes(V,1); PetscValidType(Q,2); ierr = PetscObjectTypeCompare((PetscObject)Q,MATSEQDENSE,&match);CHKERRQ(ierr); if (!match) SETERRQ(PetscObjectComm((PetscObject)V),PETSC_ERR_SUP,"Mat argument must be of type seqdense"); if (s<V->l || s>V->m) SETERRQ3(PetscObjectComm((PetscObject)V),PETSC_ERR_ARG_OUTOFRANGE,"Argument s has wrong value %D, should be between %D and %D",s,V->l,V->m); if (e<V->l || e>V->m) SETERRQ3(PetscObjectComm((PetscObject)V),PETSC_ERR_ARG_OUTOFRANGE,"Argument e has wrong value %D, should be between %D and %D",e,V->l,V->m); ierr = MatGetSize(Q,&m,&n);CHKERRQ(ierr); if (n<V->k) SETERRQ2(PetscObjectComm((PetscObject)V),PETSC_ERR_ARG_SIZ,"Mat argument has %D columns, should have at least %D",n,V->k); if (e>m) SETERRQ2(PetscObjectComm((PetscObject)V),PETSC_ERR_ARG_SIZ,"Mat argument only has %D rows, the requested value of e is larger: %D",m,e); if (s>=e || !V->n) PetscFunctionReturn(0); ierr = PetscLogEventBegin(BV_Mult,V,Q,0,0);CHKERRQ(ierr); ierr = (*V->ops->multinplacetrans)(V,Q,s,e);CHKERRQ(ierr); ierr = PetscLogEventEnd(BV_Mult,V,Q,0,0);CHKERRQ(ierr); ierr = PetscObjectStateIncrease((PetscObject)V);CHKERRQ(ierr); PetscFunctionReturn(0); }
PetscErrorCode VecMAXPY_SAMRAI( Vec y, PetscInt nv, const PetscScalar* alpha, Vec* x) { IBTK_TIMER_START(t_vec_maxpy); #ifdef DEBUG_CHECK_ASSERTIONS TBOX_ASSERT(y != PETSC_NULL); for (PetscInt i = 0; i < nv; ++i) { TBOX_ASSERT(x[i] != PETSC_NULL); } #endif static const bool interior_only = false; for (PetscInt i = 0; i < nv; ++i) { if (MathUtilities<double>::equalEps(alpha[i],1.0)) { PSVR_CAST2(y)->add(PSVR_CAST2(x[i]), PSVR_CAST2(y), interior_only); } else if (MathUtilities<double>::equalEps(alpha[i],-1.0)) { PSVR_CAST2(y)->subtract(PSVR_CAST2(y), PSVR_CAST2(x[i]), interior_only); } else { PSVR_CAST2(y)->axpy(alpha[i], PSVR_CAST2(x[i]), PSVR_CAST2(y), interior_only); } } int ierr = PetscObjectStateIncrease(reinterpret_cast<PetscObject>(y)); IBTK_CHKERRQ(ierr); IBTK_TIMER_STOP(t_vec_maxpy); PetscFunctionReturn(0); }// VecMAXPY
/*@ BVMult - Computes Y = beta*Y + alpha*X*Q. Logically Collective on BV Input Parameters: + Y,X - basis vectors . alpha,beta - scalars - Q - a sequential dense matrix Output Parameter: . Y - the modified basis vectors Notes: X and Y must be different objects. The case X=Y can be addressed with BVMultInPlace(). The matrix Q must be a sequential dense Mat, with all entries equal on all processes (otherwise each process will compute a different update). The dimensions of Q must be at least m,n where m is the number of active columns of X and n is the number of active columns of Y. The leading columns of Y are not modified. Also, if X has leading columns specified, then these columns do not participate in the computation. Hence, only rows (resp. columns) of Q starting from lx (resp. ly) are used, where lx (resp. ly) is the number of leading columns of X (resp. Y). Level: intermediate .seealso: BVMultVec(), BVMultColumn(), BVMultInPlace(), BVSetActiveColumns() @*/ PetscErrorCode BVMult(BV Y,PetscScalar alpha,PetscScalar beta,BV X,Mat Q) { PetscErrorCode ierr; PetscBool match; PetscInt m,n; PetscFunctionBegin; PetscValidHeaderSpecific(Y,BV_CLASSID,1); PetscValidLogicalCollectiveScalar(Y,alpha,2); PetscValidLogicalCollectiveScalar(Y,beta,3); PetscValidHeaderSpecific(X,BV_CLASSID,4); PetscValidHeaderSpecific(Q,MAT_CLASSID,5); PetscValidType(Y,1); BVCheckSizes(Y,1); PetscValidType(X,4); BVCheckSizes(X,4); PetscValidType(Q,5); PetscCheckSameTypeAndComm(Y,1,X,4); if (X==Y) SETERRQ(PetscObjectComm((PetscObject)Y),PETSC_ERR_ARG_WRONG,"X and Y arguments must be different"); ierr = PetscObjectTypeCompare((PetscObject)Q,MATSEQDENSE,&match);CHKERRQ(ierr); if (!match) SETERRQ(PetscObjectComm((PetscObject)Y),PETSC_ERR_SUP,"Mat argument must be of type seqdense"); ierr = MatGetSize(Q,&m,&n);CHKERRQ(ierr); if (m<X->k) SETERRQ2(PetscObjectComm((PetscObject)Y),PETSC_ERR_ARG_SIZ,"Mat argument has %D rows, should have at least %D",m,X->k); if (n<Y->k) SETERRQ2(PetscObjectComm((PetscObject)Y),PETSC_ERR_ARG_SIZ,"Mat argument has %D columns, should have at least %D",n,Y->k); if (X->n!=Y->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Mismatching local dimension X %D, Y %D",X->n,Y->n); if (!X->n) PetscFunctionReturn(0); ierr = PetscLogEventBegin(BV_Mult,X,Y,0,0);CHKERRQ(ierr); ierr = (*Y->ops->mult)(Y,alpha,beta,X,Q);CHKERRQ(ierr); ierr = PetscLogEventEnd(BV_Mult,X,Y,0,0);CHKERRQ(ierr); ierr = PetscObjectStateIncrease((PetscObject)Y);CHKERRQ(ierr); PetscFunctionReturn(0); }
PetscErrorCode VecAXPBYPCZ_MultiVec(Vec w, PetscScalar alpha, PetscScalar beta, PetscScalar gamma, Vec x, Vec y) { #if !defined(NDEBUG) TBOX_ASSERT(w); TBOX_ASSERT(x); TBOX_ASSERT(y); #endif Vec_MultiVec* mw = static_cast<Vec_MultiVec*>(w->data); Vec_MultiVec* mx = static_cast<Vec_MultiVec*>(x->data); Vec_MultiVec* my = static_cast<Vec_MultiVec*>(y->data); #if !defined(NDEBUG) TBOX_ASSERT(mw); TBOX_ASSERT(mx); TBOX_ASSERT(my); TBOX_ASSERT(mw->n == mx->n); TBOX_ASSERT(mw->n == my->n); #endif PetscErrorCode ierr; for (PetscInt k = 0; k < mw->n; ++k) { ierr = VecAXPBYPCZ(mw->array[k], alpha, beta, gamma, mx->array[k], my->array[k]); CHKERRQ(ierr); } ierr = PetscObjectStateIncrease(reinterpret_cast<PetscObject>(w)); CHKERRQ(ierr); PetscFunctionReturn(0); } // VecAXPBYPCZ_MultiVec
/*@ BVOrthogonalize - Orthogonalize all columns (except leading ones), that is, compute the QR decomposition. Collective on BV Input Parameter: . V - basis vectors Output Parameters: + V - the modified basis vectors - R - a sequential dense matrix (or NULL) Notes: On input, matrix R must be a sequential dense Mat, with at least as many rows and columns as the number of active columns of V. The output satisfies V0 = V*R (where V0 represent the input V) and V'*V = I. If V has leading columns, then they are not modified (are assumed to be already orthonormal) and the corresponding part of R is not referenced. Can pass NULL if R is not required. Level: intermediate .seealso: BVOrthogonalizeColumn(), BVOrthogonalizeVec(), BVSetActiveColumns() @*/ PetscErrorCode BVOrthogonalize(BV V,Mat R) { PetscErrorCode ierr; PetscBool match; PetscInt m,n; PetscFunctionBegin; PetscValidHeaderSpecific(V,BV_CLASSID,1); PetscValidType(V,1); BVCheckSizes(V,1); if (R) { PetscValidHeaderSpecific(R,MAT_CLASSID,2); PetscValidType(R,2); ierr = PetscObjectTypeCompare((PetscObject)R,MATSEQDENSE,&match);CHKERRQ(ierr); if (!match) SETERRQ(PetscObjectComm((PetscObject)V),PETSC_ERR_SUP,"Mat argument must be of type seqdense"); ierr = MatGetSize(R,&m,&n);CHKERRQ(ierr); if (m!=n) SETERRQ2(PetscObjectComm((PetscObject)V),PETSC_ERR_ARG_SIZ,"Mat argument is not square, it has %D rows and %D columns",m,n); if (n<V->k) SETERRQ2(PetscObjectComm((PetscObject)V),PETSC_ERR_ARG_SIZ,"Mat size %D is smaller than the number of BV active columns %D",n,V->k); } if (V->matrix) SETERRQ(PetscObjectComm((PetscObject)V),PETSC_ERR_SUP,"Not implemented for non-standard inner product, use BVOrthogonalizeColumn() instead"); if (V->nc) SETERRQ(PetscObjectComm((PetscObject)V),PETSC_ERR_SUP,"Not implemented for BV with constraints, use BVOrthogonalizeColumn() instead"); ierr = PetscLogEventBegin(BV_Orthogonalize,V,R,0,0);CHKERRQ(ierr); if (V->ops->orthogonalize) { ierr = (*V->ops->orthogonalize)(V,R);CHKERRQ(ierr); } else { /* no specific QR function available, so proceed column by column with Gram-Schmidt */ ierr = BVOrthogonalize_GS(V,R);CHKERRQ(ierr); } ierr = PetscLogEventEnd(BV_Orthogonalize,V,R,0,0);CHKERRQ(ierr); ierr = PetscObjectStateIncrease((PetscObject)V);CHKERRQ(ierr); PetscFunctionReturn(0); }
PetscErrorCode VecWAXPY_SAMRAI( Vec w, PetscScalar alpha, Vec x, Vec y) { IBTK_TIMER_START(t_vec_waxpy); #ifdef DEBUG_CHECK_ASSERTIONS TBOX_ASSERT(x != PETSC_NULL); TBOX_ASSERT(y != PETSC_NULL); TBOX_ASSERT(w != PETSC_NULL); #endif static const bool interior_only = false; if (MathUtilities<double>::equalEps(alpha,1.0)) { PSVR_CAST2(w)->add(PSVR_CAST2(x), PSVR_CAST2(y), interior_only); } else if (MathUtilities<double>::equalEps(alpha,-1.0)) { PSVR_CAST2(w)->subtract(PSVR_CAST2(y), PSVR_CAST2(x), interior_only); } else { PSVR_CAST2(w)->axpy(alpha, PSVR_CAST2(x), PSVR_CAST2(y), interior_only); } int ierr = PetscObjectStateIncrease(reinterpret_cast<PetscObject>(w)); IBTK_CHKERRQ(ierr); IBTK_TIMER_STOP(t_vec_waxpy); PetscFunctionReturn(0); }// VecWAXPY
/*@ PetscRandomSeed - Seed the generator. Not collective Input Parameters: . r - The random number generator context Level: intermediate Usage: PetscRandomSetSeed(r,a positive integer); PetscRandomSeed(r); PetscRandomGetValue() will now start with the new seed. PetscRandomSeed(r) without a call to PetscRandomSetSeed() re-initializes the seed. The random numbers generated will be the same as before. Concepts: random numbers^seed .seealso: PetscRandomCreate(), PetscRandomGetSeed(), PetscRandomSetSeed() @*/ PetscErrorCode PetscRandomSeed(PetscRandom r) { PetscErrorCode ierr; PetscFunctionBegin; PetscValidHeaderSpecific(r,PETSC_RANDOM_CLASSID,1); PetscValidType(r,1); ierr = (*r->ops->seed)(r);CHKERRQ(ierr); ierr = PetscObjectStateIncrease((PetscObject)r);CHKERRQ(ierr); PetscFunctionReturn(0); }
PetscErrorCode VecSet_SAMRAI(Vec x, PetscScalar alpha) { IBTK_TIMER_START(t_vec_set); #if !defined(NDEBUG) TBOX_ASSERT(x); #endif static const bool interior_only = false; PSVR_CAST2(x)->setToScalar(alpha, interior_only); int ierr = PetscObjectStateIncrease(reinterpret_cast<PetscObject>(x)); IBTK_CHKERRQ(ierr); IBTK_TIMER_STOP(t_vec_set); PetscFunctionReturn(0); } // VecSet
PetscErrorCode VecCopy_SAMRAI(Vec x, Vec y) { IBTK_TIMER_START(t_vec_copy); #if !defined(NDEBUG) TBOX_ASSERT(x); TBOX_ASSERT(y); #endif static const bool interior_only = false; PSVR_CAST2(y)->copyVector(PSVR_CAST2(x), interior_only); int ierr = PetscObjectStateIncrease(reinterpret_cast<PetscObject>(y)); IBTK_CHKERRQ(ierr); IBTK_TIMER_STOP(t_vec_copy); PetscFunctionReturn(0); } // VecCopy
PetscErrorCode VecScale_SAMRAI( Vec x, PetscScalar alpha) { IBTK_TIMER_START(t_vec_scale); #ifdef DEBUG_CHECK_ASSERTIONS TBOX_ASSERT(x != PETSC_NULL); #endif static const bool interior_only = false; PSVR_CAST2(x)->scale(alpha,PSVR_CAST2(x), interior_only); int ierr = PetscObjectStateIncrease(reinterpret_cast<PetscObject>(x)); IBTK_CHKERRQ(ierr); IBTK_TIMER_STOP(t_vec_scale); PetscFunctionReturn(0); }// VecScale
PetscErrorCode VecPointwiseDivide_SAMRAI(Vec w, Vec x, Vec y) { IBTK_TIMER_START(t_vec_pointwise_divide); #if !defined(NDEBUG) TBOX_ASSERT(x); TBOX_ASSERT(y); TBOX_ASSERT(w); #endif static const bool interior_only = false; PSVR_CAST2(w)->divide(PSVR_CAST2(x), PSVR_CAST2(y), interior_only); int ierr = PetscObjectStateIncrease(reinterpret_cast<PetscObject>(w)); IBTK_CHKERRQ(ierr); IBTK_TIMER_STOP(t_vec_pointwise_divide); PetscFunctionReturn(0); } // VecPointwiseDivide
PetscErrorCode VecSetRandom_SAMRAI(Vec x, PetscRandom rctx) { IBTK_TIMER_START(t_vec_set_random); #if !defined(NDEBUG) TBOX_ASSERT(x); #endif PetscScalar lo, hi; int ierr; ierr = PetscRandomGetInterval(rctx, &lo, &hi); IBTK_CHKERRQ(ierr); PSVR_CAST2(x)->setRandomValues(hi - lo, lo); ierr = PetscObjectStateIncrease(reinterpret_cast<PetscObject>(x)); IBTK_CHKERRQ(ierr); IBTK_TIMER_STOP(t_vec_set_random); PetscFunctionReturn(0); } // VecSetRandom
/*@ BVInsertVecs - Insert a set of vectors into the specified columns. Collective on BV Input Parameters: + V - basis vectors . s - first column of V to be overwritten . W - set of vectors to be copied - orth - flag indicating if the vectors must be orthogonalized Input/Output Parameter: . m - number of input vectors, on output the number of linearly independent vectors Notes: Copies the contents of vectors W to V(:,s:s+n). If the orthogonalization flag is set, then the vectors are copied one by one and then orthogonalized against the previous ones. If any of them is linearly dependent then it is discarded and the value of m is decreased. Level: intermediate .seealso: BVInsertVec(), BVOrthogonalizeColumn() @*/ PetscErrorCode BVInsertVecs(BV V,PetscInt s,PetscInt *m,Vec *W,PetscBool orth) { PetscErrorCode ierr; PetscInt n,N,i,ndep; PetscBool lindep; PetscReal norm; Vec v; PetscFunctionBegin; PetscValidHeaderSpecific(V,BV_CLASSID,1); PetscValidLogicalCollectiveInt(V,s,2); PetscValidPointer(m,3); PetscValidLogicalCollectiveInt(V,*m,3); if (!*m) PetscFunctionReturn(0); if (*m<0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Number of vectors (given %D) cannot be negative",*m); PetscValidPointer(W,4); PetscValidHeaderSpecific(*W,VEC_CLASSID,4); PetscValidLogicalCollectiveBool(V,orth,5); PetscValidType(V,1); BVCheckSizes(V,1); PetscCheckSameComm(V,1,*W,4); ierr = VecGetSize(*W,&N);CHKERRQ(ierr); ierr = VecGetLocalSize(*W,&n);CHKERRQ(ierr); if (N!=V->N || n!=V->n) SETERRQ4(PetscObjectComm((PetscObject)V),PETSC_ERR_ARG_INCOMP,"Vec sizes (global %D, local %D) do not match BV sizes (global %D, local %D)",N,n,V->N,V->n); if (s<0 || s>=V->m) SETERRQ2(PetscObjectComm((PetscObject)V),PETSC_ERR_ARG_OUTOFRANGE,"Argument s has wrong value %D, should be between 0 and %D",s,V->m-1); if (s+(*m)>V->m) SETERRQ1(PetscObjectComm((PetscObject)V),PETSC_ERR_ARG_OUTOFRANGE,"Too many vectors provided, there is only room for %D",V->m); ndep = 0; for (i=0;i<*m;i++) { ierr = BVGetColumn(V,s+i-ndep,&v);CHKERRQ(ierr); ierr = VecCopy(W[i],v);CHKERRQ(ierr); ierr = BVRestoreColumn(V,s+i-ndep,&v);CHKERRQ(ierr); if (orth) { ierr = BVOrthogonalizeColumn(V,s+i-ndep,NULL,&norm,&lindep);CHKERRQ(ierr); if (norm==0.0 || lindep) { ierr = PetscInfo1(V,"Removing linearly dependent vector %D\n",i);CHKERRQ(ierr); ndep++; } else { ierr = BVScaleColumn(V,s+i-ndep,1.0/norm);CHKERRQ(ierr); } } } *m -= ndep; ierr = PetscObjectStateIncrease((PetscObject)V);CHKERRQ(ierr); PetscFunctionReturn(0); }
/*@ BVScale - Multiply the BV entries by a scalar value. Logically Collective on BV Input Parameters: + bv - basis vectors - alpha - scaling factor Note: All active columns (except the leading ones) are scaled. Level: intermediate .seealso: BVScaleColumn(), BVSetActiveColumns() @*/ PetscErrorCode BVScale(BV bv,PetscScalar alpha) { PetscErrorCode ierr; PetscFunctionBegin; PetscValidHeaderSpecific(bv,BV_CLASSID,1); PetscValidLogicalCollectiveScalar(bv,alpha,2); PetscValidType(bv,1); BVCheckSizes(bv,1); if (!bv->n || alpha == (PetscScalar)1.0) PetscFunctionReturn(0); ierr = PetscLogEventBegin(BV_Scale,bv,0,0,0);CHKERRQ(ierr); ierr = (*bv->ops->scale)(bv,-1,alpha);CHKERRQ(ierr); ierr = PetscLogEventEnd(BV_Scale,bv,0,0,0);CHKERRQ(ierr); ierr = PetscObjectStateIncrease((PetscObject)bv);CHKERRQ(ierr); PetscFunctionReturn(0); }
PetscErrorCode VecAXPBYPCZ_SAMRAI(Vec z, PetscScalar alpha, PetscScalar beta, PetscScalar gamma, Vec x, Vec y) { IBTK_TIMER_START(t_vec_axpbypcz); #if !defined(NDEBUG) TBOX_ASSERT(x); TBOX_ASSERT(y); TBOX_ASSERT(z); #endif static const bool interior_only = false; PSVR_CAST2(z)->linearSum(alpha, PSVR_CAST2(x), gamma, PSVR_CAST2(z), interior_only); PSVR_CAST2(z)->axpy(beta, PSVR_CAST2(y), PSVR_CAST2(z), interior_only); int ierr = PetscObjectStateIncrease(reinterpret_cast<PetscObject>(z)); IBTK_CHKERRQ(ierr); IBTK_TIMER_STOP(t_vec_axpbypcz); PetscFunctionReturn(0); } // VecAXPBYPCZ
PetscErrorCode VecPointwiseMult_SAMRAI( Vec w, Vec x, Vec y) { IBTK_TIMER_START(t_vec_pointwise_mult); #ifdef DEBUG_CHECK_ASSERTIONS TBOX_ASSERT(x != PETSC_NULL); TBOX_ASSERT(y != PETSC_NULL); TBOX_ASSERT(w != PETSC_NULL); #endif static const bool interior_only = false; PSVR_CAST2(w)->multiply(PSVR_CAST2(x), PSVR_CAST2(y), interior_only); int ierr = PetscObjectStateIncrease(reinterpret_cast<PetscObject>(w)); IBTK_CHKERRQ(ierr); IBTK_TIMER_STOP(t_vec_pointwise_mult); PetscFunctionReturn(0); }// VecPointwiseMult
PetscErrorCode VecMAXPY_MultiVec(Vec y, PetscInt nv, const PetscScalar* alpha, Vec* x) { #if !defined(NDEBUG) TBOX_ASSERT(y); for (PetscInt i = 0; i < nv; ++i) { TBOX_ASSERT(x[i]); } #endif PetscErrorCode ierr; for (PetscInt i = 0; i < nv; ++i) { ierr = VecAXPY_MultiVec(y, alpha[i], x[i]); CHKERRQ(ierr); } ierr = PetscObjectStateIncrease(reinterpret_cast<PetscObject>(y)); CHKERRQ(ierr); PetscFunctionReturn(0); } // VecMAXPY_MultiVec
PetscErrorCode VecSetRandom_MultiVec(Vec x, PetscRandom rctx) { #if !defined(NDEBUG) TBOX_ASSERT(x); #endif Vec_MultiVec* mx = static_cast<Vec_MultiVec*>(x->data); #if !defined(NDEBUG) TBOX_ASSERT(mx); #endif PetscErrorCode ierr; for (PetscInt k = 0; k < mx->n; ++k) { ierr = VecSetRandom(mx->array[k], rctx); CHKERRQ(ierr); } ierr = PetscObjectStateIncrease(reinterpret_cast<PetscObject>(x)); CHKERRQ(ierr); PetscFunctionReturn(0); } // VecSetRandom_MultiVec
/*@ MatDiagonalSet - Computes Y = Y + D, where D is a diagonal matrix that is represented as a vector. Or Y[i,i] = D[i] if InsertMode is INSERT_VALUES. Input Parameters: + Y - the input matrix . D - the diagonal matrix, represented as a vector - i - INSERT_VALUES or ADD_VALUES Neighbor-wise Collective on Mat and Vec Notes: If the matrix Y is missing some diagonal entries this routine can be very slow. To make it fast one should initially fill the matrix so that all diagonal entries have a value (with a value of zero for those locations that would not have an entry). Level: intermediate .keywords: matrix, add, shift, diagonal .seealso: MatShift(), MatScale(), MatDiagonalScale() @*/ PetscErrorCode MatDiagonalSet(Mat Y,Vec D,InsertMode is) { PetscErrorCode ierr; PetscInt matlocal,veclocal; PetscFunctionBegin; PetscValidHeaderSpecific(Y,MAT_CLASSID,1); PetscValidHeaderSpecific(D,VEC_CLASSID,2); ierr = MatGetLocalSize(Y,&matlocal,NULL);CHKERRQ(ierr); ierr = VecGetLocalSize(D,&veclocal);CHKERRQ(ierr); if (matlocal != veclocal) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Number local rows of matrix %D does not match that of vector for diagonal %D",matlocal,veclocal); if (Y->ops->diagonalset) { ierr = (*Y->ops->diagonalset)(Y,D,is);CHKERRQ(ierr); } else { ierr = MatDiagonalSet_Default(Y,D,is);CHKERRQ(ierr); } ierr = PetscObjectStateIncrease((PetscObject)Y);CHKERRQ(ierr); PetscFunctionReturn(0); }
/*@ BVScaleColumn - Scale one column of a BV. Logically Collective on BV Input Parameters: + bv - basis vectors . j - column number to be scaled - alpha - scaling factor Level: intermediate .seealso: BVScale(), BVSetActiveColumns() @*/ PetscErrorCode BVScaleColumn(BV bv,PetscInt j,PetscScalar alpha) { PetscErrorCode ierr; PetscFunctionBegin; PetscValidHeaderSpecific(bv,BV_CLASSID,1); PetscValidLogicalCollectiveInt(bv,j,2); PetscValidLogicalCollectiveScalar(bv,alpha,3); PetscValidType(bv,1); BVCheckSizes(bv,1); if (j<0 || j>=bv->m) SETERRQ2(PetscObjectComm((PetscObject)bv),PETSC_ERR_ARG_OUTOFRANGE,"Argument j has wrong value %D, the number of columns is %D",j,bv->m); if (!bv->n || alpha == (PetscScalar)1.0) PetscFunctionReturn(0); ierr = PetscLogEventBegin(BV_Scale,bv,0,0,0);CHKERRQ(ierr); ierr = (*bv->ops->scale)(bv,j,alpha);CHKERRQ(ierr); ierr = PetscLogEventEnd(BV_Scale,bv,0,0,0);CHKERRQ(ierr); ierr = PetscObjectStateIncrease((PetscObject)bv);CHKERRQ(ierr); PetscFunctionReturn(0); }
/*@ BVSetRandomColumn - Set one column of a BV to random numbers. Logically Collective on BV Input Parameters: + bv - basis vectors . j - column number to be set - rctx - the random number context, formed by PetscRandomCreate(), or NULL and it will create one internally. Note: This operation is analogue to VecSetRandom - the difference is that the generated random vector is the same irrespective of the size of the communicator (if all processes pass a PetscRandom context initialized with the same seed). Level: advanced .seealso: BVSetRandom(), BVSetActiveColumns() @*/ PetscErrorCode BVSetRandomColumn(BV bv,PetscInt j,PetscRandom rctx) { PetscErrorCode ierr; PetscRandom rand=NULL; PetscInt i,low,high; PetscScalar *px,t; Vec x; PetscFunctionBegin; PetscValidHeaderSpecific(bv,BV_CLASSID,1); PetscValidLogicalCollectiveInt(bv,j,2); if (rctx) PetscValidHeaderSpecific(rctx,PETSC_RANDOM_CLASSID,3); else { ierr = PetscRandomCreate(PetscObjectComm((PetscObject)bv),&rand);CHKERRQ(ierr); ierr = PetscRandomSetSeed(rand,0x12345678);CHKERRQ(ierr); ierr = PetscRandomSetFromOptions(rand);CHKERRQ(ierr); rctx = rand; } PetscValidType(bv,1); BVCheckSizes(bv,1); if (j<0 || j>=bv->m) SETERRQ2(PetscObjectComm((PetscObject)bv),PETSC_ERR_ARG_OUTOFRANGE,"Argument j has wrong value %D, the number of columns is %D",j,bv->m); ierr = PetscLogEventBegin(BV_SetRandom,bv,rctx,0,0);CHKERRQ(ierr); ierr = BVGetColumn(bv,j,&x);CHKERRQ(ierr); ierr = VecGetOwnershipRange(x,&low,&high);CHKERRQ(ierr); ierr = VecGetArray(x,&px);CHKERRQ(ierr); for (i=0;i<bv->N;i++) { ierr = PetscRandomGetValue(rctx,&t);CHKERRQ(ierr); if (i>=low && i<high) px[i-low] = t; } ierr = VecRestoreArray(x,&px);CHKERRQ(ierr); ierr = BVRestoreColumn(bv,j,&x);CHKERRQ(ierr); ierr = PetscLogEventEnd(BV_SetRandom,bv,rctx,0,0);CHKERRQ(ierr); ierr = PetscRandomDestroy(&rand);CHKERRQ(ierr); ierr = PetscObjectStateIncrease((PetscObject)bv);CHKERRQ(ierr); PetscFunctionReturn(0); }
/*@ BVSetRandom - Set the columns of a BV to random numbers. Logically Collective on BV Input Parameters: + bv - basis vectors - rctx - the random number context, formed by PetscRandomCreate(), or NULL and it will create one internally. Note: All active columns (except the leading ones) are modified. Level: advanced .seealso: BVSetRandomColumn(), BVSetActiveColumns() @*/ PetscErrorCode BVSetRandom(BV bv,PetscRandom rctx) { PetscErrorCode ierr; PetscRandom rand=NULL; PetscInt i,low,high,k; PetscScalar *px,t; Vec x; PetscFunctionBegin; PetscValidHeaderSpecific(bv,BV_CLASSID,1); if (rctx) PetscValidHeaderSpecific(rctx,PETSC_RANDOM_CLASSID,2); else { ierr = PetscRandomCreate(PetscObjectComm((PetscObject)bv),&rand);CHKERRQ(ierr); ierr = PetscRandomSetSeed(rand,0x12345678);CHKERRQ(ierr); ierr = PetscRandomSetFromOptions(rand);CHKERRQ(ierr); rctx = rand; } PetscValidType(bv,1); BVCheckSizes(bv,1); ierr = PetscLogEventBegin(BV_SetRandom,bv,rctx,0,0);CHKERRQ(ierr); for (k=bv->l;k<bv->k;k++) { ierr = BVGetColumn(bv,k,&x);CHKERRQ(ierr); ierr = VecGetOwnershipRange(x,&low,&high);CHKERRQ(ierr); ierr = VecGetArray(x,&px);CHKERRQ(ierr); for (i=0;i<bv->N;i++) { ierr = PetscRandomGetValue(rctx,&t);CHKERRQ(ierr); if (i>=low && i<high) px[i-low] = t; } ierr = VecRestoreArray(x,&px);CHKERRQ(ierr); ierr = BVRestoreColumn(bv,k,&x);CHKERRQ(ierr); } ierr = PetscLogEventEnd(BV_SetRandom,bv,rctx,0,0);CHKERRQ(ierr); ierr = PetscRandomDestroy(&rand);CHKERRQ(ierr); ierr = PetscObjectStateIncrease((PetscObject)bv);CHKERRQ(ierr); PetscFunctionReturn(0); }
/*@C BVAXPY - Computes Y = Y + alpha*X. Logically Collective on BV Input Parameters: + Y,X - basis vectors - alpha - scalar Output Parameter: . Y - the modified basis vectors Notes: X and Y must be different objects, with compatible dimensions. The effect is the same as doing a VecAXPY for each of the active columns (excluding the leading ones). Level: intermediate .seealso: BVMult(), BVSetActiveColumns() @*/ PetscErrorCode BVAXPY(BV Y,PetscScalar alpha,BV X) { PetscErrorCode ierr; PetscFunctionBegin; PetscValidHeaderSpecific(Y,BV_CLASSID,1); PetscValidLogicalCollectiveScalar(Y,alpha,2); PetscValidHeaderSpecific(X,BV_CLASSID,3); PetscValidType(Y,1); BVCheckSizes(Y,1); PetscValidType(X,3); BVCheckSizes(X,3); PetscCheckSameTypeAndComm(Y,1,X,3); if (X==Y) SETERRQ(PetscObjectComm((PetscObject)Y),PETSC_ERR_ARG_WRONG,"X and Y arguments must be different"); if (X->n!=Y->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Mismatching local dimension X %D, Y %D",X->n,Y->n); if (X->k-X->l!=Y->k-Y->l) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Y has %D non-leading columns, while X has %D",Y->m-Y->l,X->k-X->l); if (!X->n) PetscFunctionReturn(0); ierr = PetscLogEventBegin(BV_AXPY,X,Y,0,0);CHKERRQ(ierr); ierr = (*Y->ops->axpy)(Y,alpha,X);CHKERRQ(ierr); ierr = PetscLogEventEnd(BV_AXPY,X,Y,0,0);CHKERRQ(ierr); ierr = PetscObjectStateIncrease((PetscObject)Y);CHKERRQ(ierr); PetscFunctionReturn(0); }
/*@ MatShift - Computes Y = Y + a I, where a is a PetscScalar and I is the identity matrix. Neighbor-wise Collective on Mat Input Parameters: + Y - the matrices - a - the PetscScalar Level: intermediate Notes: If the matrix Y is missing some diagonal entries this routine can be very slow. To make it fast one should initially fill the matrix so that all diagonal entries have a value (with a value of zero for those locations that would not have an entry). To form Y = Y + diag(V) use MatDiagonalSet() Developers Note: If the local "diagonal part" of the matrix Y has no entries then the local diagonal part is preallocated with 1 nonzero per row for the to be added values. This allows for fast shifting of an empty matrix. .keywords: matrix, add, shift .seealso: MatDiagonalSet(), MatScale(), MatDiagonalScale() @*/ PetscErrorCode MatShift(Mat Y,PetscScalar a) { PetscErrorCode ierr; PetscFunctionBegin; PetscValidHeaderSpecific(Y,MAT_CLASSID,1); if (!Y->assembled) SETERRQ(PetscObjectComm((PetscObject)Y),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); if (Y->factortype) SETERRQ(PetscObjectComm((PetscObject)Y),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); MatCheckPreallocated(Y,1); if (Y->ops->shift) { ierr = (*Y->ops->shift)(Y,a);CHKERRQ(ierr); } else { ierr = MatShift_Basic(Y,a);CHKERRQ(ierr); } ierr = PetscObjectStateIncrease((PetscObject)Y);CHKERRQ(ierr); #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA) if (Y->valid_GPU_matrix != PETSC_OFFLOAD_UNALLOCATED) { Y->valid_GPU_matrix = PETSC_OFFLOAD_CPU; } #endif PetscFunctionReturn(0); }