/*@ MatColoringView - Output details about the MatColoring. Collective on MatColoring Input Parameters: - mc - the MatColoring context + viewer - the Viewer context Level: beginner .keywords: Coloring, view .seealso: MatColoring, MatColoringApply() @*/ PetscErrorCode MatColoringView(MatColoring mc,PetscViewer viewer) { PetscErrorCode ierr; PetscBool iascii; PetscFunctionBegin; PetscValidHeaderSpecific(mc,MAT_COLORING_CLASSID,1); if (!viewer) { ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)mc),&viewer);CHKERRQ(ierr); } PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); PetscCheckSameComm(mc,1,viewer,2); ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); if (iascii) { ierr = PetscObjectPrintClassNamePrefixType((PetscObject)mc,viewer);CHKERRQ(ierr); ierr = PetscViewerASCIIPrintf(viewer," Weight type: %s\n",MatColoringWeightTypes[mc->weight_type]);CHKERRQ(ierr); if (mc->maxcolors > 0) { ierr = PetscViewerASCIIPrintf(viewer," Distance %d, Max. Colors %d\n",mc->dist,mc->maxcolors);CHKERRQ(ierr); } else { ierr = PetscViewerASCIIPrintf(viewer," Distance %d\n",mc->dist);CHKERRQ(ierr); } } PetscFunctionReturn(0); }
/*@C MatPartitioningView - Prints the partitioning data structure. Collective on MatPartitioning Input Parameters: . part - the partitioning context . viewer - optional visualization context Level: intermediate Note: The available visualization contexts include + PETSC_VIEWER_STDOUT_SELF - standard output (default) - PETSC_VIEWER_STDOUT_WORLD - synchronized standard output where only the first processor opens the file. All other processors send their data to the first processor to print. The user can open alternative visualization contexts with . PetscViewerASCIIOpen() - output to a specified file .keywords: Partitioning, view .seealso: PetscViewerASCIIOpen() @*/ PetscErrorCode MatPartitioningView(MatPartitioning part,PetscViewer viewer) { PetscErrorCode ierr; PetscBool iascii; PetscFunctionBegin; PetscValidHeaderSpecific(part,MAT_PARTITIONING_CLASSID,1); if (!viewer) { ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)part),&viewer);CHKERRQ(ierr); } PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); PetscCheckSameComm(part,1,viewer,2); ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); if (iascii) { ierr = PetscObjectPrintClassNamePrefixType((PetscObject)part,viewer);CHKERRQ(ierr); if (part->vertex_weights) { ierr = PetscViewerASCIIPrintf(viewer," Using vertex weights\n");CHKERRQ(ierr); } } if (part->ops->view) { ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); ierr = (*part->ops->view)(part,viewer);CHKERRQ(ierr); ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); } PetscFunctionReturn(0); }
/*@C MatPartitioningView - Prints the partitioning data structure. Collective on MatPartitioning Input Parameters: . part - the partitioning context . viewer - optional visualization context Level: intermediate Note: The available visualization contexts include + PETSC_VIEWER_STDOUT_SELF - standard output (default) - PETSC_VIEWER_STDOUT_WORLD - synchronized standard output where only the first processor opens the file. All other processors send their data to the first processor to print. The user can open alternative visualization contexts with . PetscViewerASCIIOpen() - output to a specified file .keywords: Partitioning, view .seealso: PetscViewerASCIIOpen() @*/ PetscErrorCode MatPartitioningView(MatPartitioning part,PetscViewer viewer) { PetscErrorCode ierr; PetscBool iascii; PetscFunctionBegin; PetscValidHeaderSpecific(part,MAT_PARTITIONING_CLASSID,1); if (!viewer) { ierr = PetscViewerASCIIGetStdout(((PetscObject)part)->comm,&viewer);CHKERRQ(ierr); } PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); PetscCheckSameComm(part,1,viewer,2); ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); if (iascii) { ierr = PetscObjectPrintClassNamePrefixType((PetscObject)part,viewer,"MatPartitioning Object");CHKERRQ(ierr); if (part->vertex_weights) { ierr = PetscViewerASCIIPrintf(viewer," Using vertex weights\n");CHKERRQ(ierr); } } else { SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Viewer type %s not supported for this MatParitioning",((PetscObject)viewer)->type_name); } if (part->ops->view) { ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); ierr = (*part->ops->view)(part,viewer);CHKERRQ(ierr); ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); } PetscFunctionReturn(0); }
/*@ 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); }
/*@ 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); }
/*@C TSTrajectoryView - Prints information about the trajectory object Collective on TSTrajectory Input Parameters: + tj - the TSTrajectory context obtained from TSTrajectoryCreate() - viewer - visualization context Options Database Key: . -ts_trajectory_view - calls TSTrajectoryView() at end of TSAdjointStep() Notes: The available visualization contexts include + PETSC_VIEWER_STDOUT_SELF - standard output (default) - PETSC_VIEWER_STDOUT_WORLD - synchronized standard output where only the first processor opens the file. All other processors send their data to the first processor to print. The user can open an alternative visualization context with PetscViewerASCIIOpen() - output to a specified file. Level: beginner .keywords: TS, trajectory, timestep, view .seealso: PetscViewerASCIIOpen() @*/ PetscErrorCode TSTrajectoryView(TSTrajectory tj,PetscViewer viewer) { PetscErrorCode ierr; PetscBool iascii; PetscFunctionBegin; PetscValidHeaderSpecific(tj,TSTRAJECTORY_CLASSID,1); if (!viewer) { ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)tj),&viewer);CHKERRQ(ierr); } PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); PetscCheckSameComm(tj,1,viewer,2); ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); if (iascii) { ierr = PetscObjectPrintClassNamePrefixType((PetscObject)tj,viewer);CHKERRQ(ierr); ierr = PetscViewerASCIIPrintf(viewer," total number of recomputations for adjoint calculation = %D\n",tj->recomps);CHKERRQ(ierr); ierr = PetscViewerASCIIPrintf(viewer," disk checkpoint reads = %D\n",tj->diskreads);CHKERRQ(ierr); ierr = PetscViewerASCIIPrintf(viewer," disk checkpoint writes = %D\n",tj->diskwrites);CHKERRQ(ierr); if (tj->ops->view) { ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); ierr = (*tj->ops->view)(tj,viewer);CHKERRQ(ierr); ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); } } PetscFunctionReturn(0); }
PETSC_EXTERN PetscErrorCode MatHeaderReplace(Mat A,Mat C) { PetscErrorCode ierr; PetscInt refct; PetscObjectState state; PetscFunctionBegin; PetscValidHeaderSpecific(A,MAT_CLASSID,1); PetscValidHeaderSpecific(C,MAT_CLASSID,2); if (A == C) PetscFunctionReturn(0); PetscCheckSameComm(A,1,C,2); if (((PetscObject)C)->refct != 1) SETERRQ1(PetscObjectComm((PetscObject)C),PETSC_ERR_ARG_WRONGSTATE,"Object C has refct %D > 1, would leave hanging reference",((PetscObject)C)->refct); /* free all the interior data structures from mat */ ierr = (*A->ops->destroy)(A);CHKERRQ(ierr); ierr = PetscHeaderDestroy_Private((PetscObject)A);CHKERRQ(ierr); ierr = PetscLayoutDestroy(&A->rmap);CHKERRQ(ierr); ierr = PetscLayoutDestroy(&A->cmap);CHKERRQ(ierr); ierr = PetscFree(A->spptr);CHKERRQ(ierr); /* copy C over to A */ refct = ((PetscObject)A)->refct; state = ((PetscObject)A)->state; ierr = PetscMemcpy(A,C,sizeof(struct _p_Mat));CHKERRQ(ierr); ((PetscObject)A)->refct = refct; ((PetscObject)A)->state = state + 1; ierr = PetscFree(C);CHKERRQ(ierr); PetscFunctionReturn(0); }
PETSC_EXTERN PetscErrorCode MatHeaderReplace(Mat A,Mat *C) { PetscErrorCode ierr; PetscInt refct; PetscObjectState state; struct _p_Mat buffer; PetscFunctionBegin; PetscValidHeaderSpecific(A,MAT_CLASSID,1); PetscValidHeaderSpecific(*C,MAT_CLASSID,2); if (A == *C) PetscFunctionReturn(0); PetscCheckSameComm(A,1,*C,2); if (((PetscObject)*C)->refct != 1) SETERRQ1(PetscObjectComm((PetscObject)C),PETSC_ERR_ARG_WRONGSTATE,"Object C has refct %D > 1, would leave hanging reference",((PetscObject)*C)->refct); /* swap C and A */ refct = ((PetscObject)A)->refct; state = ((PetscObject)A)->state; ierr = PetscMemcpy(&buffer,A,sizeof(struct _p_Mat));CHKERRQ(ierr); ierr = PetscMemcpy(A,*C,sizeof(struct _p_Mat));CHKERRQ(ierr); ierr = PetscMemcpy(*C,&buffer,sizeof(struct _p_Mat));CHKERRQ(ierr); ((PetscObject)A)->refct = refct; ((PetscObject)A)->state = state + 1; ((PetscObject)*C)->refct = 1; ierr = MatDestroy(C);CHKERRQ(ierr); PetscFunctionReturn(0); }
/*@ BVOrthogonalizeVec - Orthogonalize a given vector with respect to all active columns. Collective on BV Input Parameters: + bv - the basis vectors context - v - the vector Output Parameters: + H - (optional) coefficients computed during orthogonalization . norm - (optional) norm of the vector after being orthogonalized - lindep - (optional) flag indicating that refinement did not improve the quality of orthogonalization Notes: This function is equivalent to BVOrthogonalizeColumn() but orthogonalizes a vector as an argument rather than taking one of the BV columns. The vector is orthogonalized against all active columns. Level: advanced .seealso: BVOrthogonalizeColumn(), BVSetOrthogonalization(), BVSetActiveColumns() @*/ PetscErrorCode BVOrthogonalizeVec(BV bv,Vec v,PetscScalar *H,PetscReal *norm,PetscBool *lindep) { PetscErrorCode ierr; PetscInt i,ksave,lsave; PetscFunctionBegin; PetscValidHeaderSpecific(bv,BV_CLASSID,1); PetscValidHeaderSpecific(v,VEC_CLASSID,2); PetscValidType(bv,1); BVCheckSizes(bv,1); PetscValidType(v,2); PetscCheckSameComm(bv,1,v,2); ierr = PetscLogEventBegin(BV_Orthogonalize,bv,0,0,0);CHKERRQ(ierr); ksave = bv->k; lsave = bv->l; bv->l = -bv->nc; /* must also orthogonalize against constraints and leading columns */ ierr = BV_AllocateCoeffs(bv);CHKERRQ(ierr); ierr = BV_AllocateSignature(bv);CHKERRQ(ierr); switch (bv->orthog_type) { case BV_ORTHOG_CGS: ierr = BVOrthogonalizeCGS(bv,0,v,H,norm,lindep);CHKERRQ(ierr); break; case BV_ORTHOG_MGS: ierr = BVOrthogonalizeMGS(bv,0,v,NULL,H,norm,lindep);CHKERRQ(ierr); break; } bv->k = ksave; bv->l = lsave; if (H) for (i=bv->l;i<bv->k;i++) H[i-bv->l] = bv->h[bv->nc+i]; ierr = PetscLogEventEnd(BV_Orthogonalize,bv,0,0,0);CHKERRQ(ierr); PetscFunctionReturn(0); }
/*@ PFView - Prints information about a mathematical function Collective on PF unless PetscViewer is PETSC_VIEWER_STDOUT_SELF Input Parameters: + PF - the PF context - viewer - optional visualization context Note: The available visualization contexts include + PETSC_VIEWER_STDOUT_SELF - standard output (default) - PETSC_VIEWER_STDOUT_WORLD - synchronized standard output where only the first processor opens the file. All other processors send their data to the first processor to print. The user can open an alternative visualization contexts with PetscViewerASCIIOpen() (output to a specified file). Level: developer .keywords: PF, view .seealso: PetscViewerCreate(), PetscViewerASCIIOpen() @*/ PetscErrorCode PFView(PF pf,PetscViewer viewer) { PetscErrorCode ierr; PetscBool iascii; PetscViewerFormat format; PetscFunctionBegin; PetscValidHeaderSpecific(pf,PF_CLASSID,1); if (!viewer) { ierr = PetscViewerASCIIGetStdout(((PetscObject)pf)->comm,&viewer);CHKERRQ(ierr); } PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); PetscCheckSameComm(pf,1,viewer,2); ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); if (iascii) { ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr); ierr = PetscObjectPrintClassNamePrefixType((PetscObject)pf,viewer,"PF Object");CHKERRQ(ierr); if (pf->ops->view) { ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); ierr = (*pf->ops->view)(pf->data,viewer);CHKERRQ(ierr); ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); } } else { SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Viewer type %s not supported by PF",((PetscObject)viewer)->type_name); } PetscFunctionReturn(0); }
PetscErrorCode MatSetLocalToGlobalMapping_IS(Mat A,ISLocalToGlobalMapping rmapping,ISLocalToGlobalMapping cmapping) { PetscErrorCode ierr; PetscInt n,bs; Mat_IS *is = (Mat_IS*)A->data; IS from,to; Vec global; PetscFunctionBegin; PetscCheckSameComm(A,1,rmapping,2); if (rmapping != cmapping) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_INCOMP,"MATIS requires the row and column mappings to be identical"); if (is->mapping) { /* Currenly destroys the objects that will be created by this routine. Is there anything else that should be checked? */ ierr = ISLocalToGlobalMappingDestroy(&is->mapping);CHKERRQ(ierr); ierr = VecDestroy(&is->x);CHKERRQ(ierr); ierr = VecDestroy(&is->y);CHKERRQ(ierr); ierr = VecScatterDestroy(&is->ctx);CHKERRQ(ierr); ierr = MatDestroy(&is->A);CHKERRQ(ierr); } ierr = PetscObjectReference((PetscObject)rmapping);CHKERRQ(ierr); ierr = ISLocalToGlobalMappingDestroy(&is->mapping);CHKERRQ(ierr); is->mapping = rmapping; /* ierr = PetscLayoutSetISLocalToGlobalMapping(A->rmap,rmapping);CHKERRQ(ierr); ierr = PetscLayoutSetISLocalToGlobalMapping(A->cmap,cmapping);CHKERRQ(ierr); */ /* Create the local matrix A */ ierr = ISLocalToGlobalMappingGetSize(rmapping,&n);CHKERRQ(ierr); ierr = ISLocalToGlobalMappingGetBlockSize(rmapping,&bs);CHKERRQ(ierr); ierr = MatCreate(PETSC_COMM_SELF,&is->A);CHKERRQ(ierr); if (bs > 1) { ierr = MatSetType(is->A,MATSEQBAIJ);CHKERRQ(ierr); } else { ierr = MatSetType(is->A,MATSEQAIJ);CHKERRQ(ierr); } ierr = MatSetSizes(is->A,n,n,n,n);CHKERRQ(ierr); ierr = MatSetBlockSize(is->A,bs);CHKERRQ(ierr); ierr = MatSetOptionsPrefix(is->A,((PetscObject)A)->prefix);CHKERRQ(ierr); ierr = MatAppendOptionsPrefix(is->A,"is_");CHKERRQ(ierr); ierr = MatSetFromOptions(is->A);CHKERRQ(ierr); /* Create the local work vectors */ ierr = VecCreate(PETSC_COMM_SELF,&is->x);CHKERRQ(ierr); ierr = VecSetBlockSize(is->x,bs);CHKERRQ(ierr); ierr = VecSetSizes(is->x,n,n);CHKERRQ(ierr); ierr = VecSetOptionsPrefix(is->x,((PetscObject)A)->prefix);CHKERRQ(ierr); ierr = VecAppendOptionsPrefix(is->x,"is_");CHKERRQ(ierr); ierr = VecSetFromOptions(is->x);CHKERRQ(ierr); ierr = VecDuplicate(is->x,&is->y);CHKERRQ(ierr); /* setup the global to local scatter */ ierr = ISCreateStride(PETSC_COMM_SELF,n,0,1,&to);CHKERRQ(ierr); ierr = ISLocalToGlobalMappingApplyIS(rmapping,to,&from);CHKERRQ(ierr); ierr = MatCreateVecs(A,&global,NULL);CHKERRQ(ierr); ierr = VecScatterCreate(global,from,is->x,to,&is->ctx);CHKERRQ(ierr); ierr = VecDestroy(&global);CHKERRQ(ierr); ierr = ISDestroy(&to);CHKERRQ(ierr); ierr = ISDestroy(&from);CHKERRQ(ierr); PetscFunctionReturn(0); }
dErr dUnitsView(dUnits un,dViewer viewer) { dBool iascii; dErr err; dFunctionBegin; dValidHeader(un,dUNITS_CLASSID,1); if (!viewer) {err = PetscViewerASCIIGetStdout(((dObject)un)->comm,&viewer);dCHK(err);} dValidHeader(viewer,PETSC_VIEWER_CLASSID,2); PetscCheckSameComm(un,1,viewer,2); err = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);dCHK(err); if (iascii) { err = PetscObjectPrintClassNamePrefixType((PetscObject)un,viewer,"Units Manager");dCHK(err); err = PetscViewerASCIIPushTab(viewer);dCHK(err); for (dInt i=0; i<un->nalloc && un->list[i]; i++) { dUnit u = un->list[i]; err = PetscViewerASCIIPrintf(viewer,"%-12s: 1 internal unit = %10.4e %s (%s) = %10.4e %s\n", dUnitQuantityName(u),dUnitDimensionalize(u,1.0),dUnitName(u),dUnitShortName(u),dUnitDimensionalizeSI(u,1.0),dUnitSIName(u));dCHK(err); err = PetscViewerASCIIPrintf(viewer,"%-12s 1 %s = %10.4e %s\n","",dUnitShortName(u),dUnitDimensionalizeSI(u,dUnitNonDimensionalize(u,1.0)),dUnitSIName(u));dCHK(err); } err = PetscViewerASCIIPopTab(viewer);dCHK(err); } else dERROR(((dObject)un)->comm,PETSC_ERR_SUP,"Viewer type %s not supported",((PetscObject)viewer)->type_name); dFunctionReturn(0); }
/*@C TaoSetJacobianDesignRoutine - Sets the function to compute the Jacobian of the constraint function with respect to the design variables. Used only for pde-constrained optimization. Logically collective on Tao Input Parameters: + tao - the Tao context . J - Matrix used for the jacobian . jac - Jacobian evaluation routine - ctx - [optional] user-defined context for private data for the Jacobian evaluation routine (may be NULL) Calling sequence of jac: $ jac (Tao tao,Vec x,Mat *J,void *ctx); + tao - the Tao context . x - input vector . J - Jacobian matrix - ctx - [optional] user-defined Jacobian context Notes: The function jac() takes Mat * as the matrix arguments rather than Mat. This allows the Jacobian evaluation routine to replace A and/or B with a completely new new matrix structure (not just different matrix elements) when appropriate, for instance, if the nonzero structure is changing throughout the global iterations. Level: intermediate .seealso: TaoComputeJacobianDesign(), TaoSetJacobianStateRoutine(), TaoSetStateDesignIS() @*/ PetscErrorCode TaoSetJacobianDesignRoutine(Tao tao, Mat J, PetscErrorCode (*func)(Tao, Vec, Mat, void*), void *ctx) { PetscErrorCode ierr; PetscFunctionBegin; PetscValidHeaderSpecific(tao,TAO_CLASSID,1); if (J) { PetscValidHeaderSpecific(J,MAT_CLASSID,2); PetscCheckSameComm(tao,1,J,2); } if (ctx) { tao->user_jac_designP = ctx; } if (func) { tao->ops->computejacobiandesign = func; } if (J) { ierr = PetscObjectReference((PetscObject)J); CHKERRQ(ierr); ierr = MatDestroy(&tao->jacobian_design); CHKERRQ(ierr); tao->jacobian_design = J; } PetscFunctionReturn(0); }
/*@ TaoLineSearchComputeObjective - Computes the objective function value at a given point Collective on TaoLineSearch Input Parameters: + ls - the TaoLineSearch context - x - input vector Output Parameter: . f - Objective value at X Notes: TaoLineSearchComputeObjective() is typically used within line searches so most users would not generally call this routine themselves. Level: developer .seealso: TaoLineSearchComputeGradient(), TaoLineSearchComputeObjectiveAndGradient(), TaoLineSearchSetObjectiveRoutine() @*/ PetscErrorCode TaoLineSearchComputeObjective(TaoLineSearch ls, Vec x, PetscReal *f) { PetscErrorCode ierr; Vec gdummy; PetscReal gts; PetscFunctionBegin; PetscValidHeaderSpecific(ls,TAOLINESEARCH_CLASSID,1); PetscValidHeaderSpecific(x,VEC_CLASSID,2); PetscValidPointer(f,3); PetscCheckSameComm(ls,1,x,2); if (ls->usetaoroutines) { ierr = TaoComputeObjective(ls->tao,x,f);CHKERRQ(ierr); } else { ierr = PetscLogEventBegin(TaoLineSearch_EvalEvent,ls,0,0,0);CHKERRQ(ierr); if (!ls->ops->computeobjective && !ls->ops->computeobjectiveandgradient && !ls->ops->computeobjectiveandgts) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Line Search does not have objective function set"); PetscStackPush("TaoLineSearch user objective routine"); if (ls->ops->computeobjective) { ierr = (*ls->ops->computeobjective)(ls,x,f,ls->userctx_func);CHKERRQ(ierr); } else if (ls->ops->computeobjectiveandgradient) { ierr = VecDuplicate(x,&gdummy);CHKERRQ(ierr); ierr = (*ls->ops->computeobjectiveandgradient)(ls,x,f,gdummy,ls->userctx_funcgrad);CHKERRQ(ierr); ierr = VecDestroy(&gdummy);CHKERRQ(ierr); } else { ierr = (*ls->ops->computeobjectiveandgts)(ls,x,ls->stepdirection,f,>s,ls->userctx_funcgts);CHKERRQ(ierr); } PetscStackPop; ierr = PetscLogEventEnd(TaoLineSearch_EvalEvent,ls,0,0,0);CHKERRQ(ierr); } ls->nfeval++; PetscFunctionReturn(0); }
/*@ BVMultVec - Computes y = beta*y + alpha*X*q. Logically Collective on BV and Vec Input Parameters: + X - a basis vectors object . alpha,beta - scalars . y - a vector - q - an array of scalars Output Parameter: . y - the modified vector Notes: This operation is the analogue of BVMult() but with a BV and a Vec, instead of two BV. Note that arguments are listed in different order with respect to BVMult(). If X has leading columns specified, then these columns do not participate in the computation. The length of array q must be equal to the number of active columns of X minus the number of leading columns, i.e. the first entry of q multiplies the first non-leading column. Level: intermediate .seealso: BVMult(), BVMultColumn(), BVMultInPlace(), BVSetActiveColumns() @*/ PetscErrorCode BVMultVec(BV X,PetscScalar alpha,PetscScalar beta,Vec y,PetscScalar *q) { PetscErrorCode ierr; PetscInt n,N; PetscFunctionBegin; PetscValidHeaderSpecific(X,BV_CLASSID,1); PetscValidLogicalCollectiveScalar(X,alpha,2); PetscValidLogicalCollectiveScalar(X,beta,3); PetscValidHeaderSpecific(y,VEC_CLASSID,4); PetscValidPointer(q,5); PetscValidType(X,1); BVCheckSizes(X,1); PetscValidType(y,4); PetscCheckSameComm(X,1,y,4); ierr = VecGetSize(y,&N);CHKERRQ(ierr); ierr = VecGetLocalSize(y,&n);CHKERRQ(ierr); if (N!=X->N || n!=X->n) SETERRQ4(PetscObjectComm((PetscObject)X),PETSC_ERR_ARG_INCOMP,"Vec sizes (global %D, local %D) do not match BV sizes (global %D, local %D)",N,n,X->N,X->n); if (!X->n) PetscFunctionReturn(0); ierr = PetscLogEventBegin(BV_Mult,X,y,0,0);CHKERRQ(ierr); ierr = (*X->ops->multvec)(X,alpha,beta,y,q);CHKERRQ(ierr); ierr = PetscLogEventEnd(BV_Mult,X,y,0,0);CHKERRQ(ierr); PetscFunctionReturn(0); }
PetscErrorCode TSAdaptView(TSAdapt adapt,PetscViewer viewer) { PetscErrorCode ierr; PetscBool iascii,isbinary; PetscFunctionBegin; PetscValidHeaderSpecific(adapt,TSADAPT_CLASSID,1); if (!viewer) {ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)adapt),&viewer);CHKERRQ(ierr);} PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); PetscCheckSameComm(adapt,1,viewer,2); ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr); if (iascii) { ierr = PetscObjectPrintClassNamePrefixType((PetscObject)adapt,viewer);CHKERRQ(ierr); ierr = PetscViewerASCIIPrintf(viewer," number of candidates %D\n",adapt->candidates.n);CHKERRQ(ierr); if (adapt->ops->view) { ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); ierr = (*adapt->ops->view)(adapt,viewer);CHKERRQ(ierr); ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); } } else if (isbinary) { char type[256]; /* need to save FILE_CLASS_ID for adapt class */ ierr = PetscStrncpy(type,((PetscObject)adapt)->type_name,256);CHKERRQ(ierr); ierr = PetscViewerBinaryWrite(viewer,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr); } else if (adapt->ops->view) { ierr = (*adapt->ops->view)(adapt,viewer);CHKERRQ(ierr); } PetscFunctionReturn(0); }
/*@ TaoComputeObjective - Computes the objective function value at a given point Collective on Tao Input Parameters: + tao - the Tao context - X - input vector Output Parameter: . f - Objective value at X Notes: TaoComputeObjective() is typically used within minimization implementations, so most users would not generally call this routine themselves. Level: advanced .seealso: TaoComputeGradient(), TaoComputeObjectiveAndGradient(), TaoSetObjectiveRoutine() @*/ PetscErrorCode TaoComputeObjective(Tao tao, Vec X, PetscReal *f) { PetscErrorCode ierr; Vec temp; PetscFunctionBegin; PetscValidHeaderSpecific(tao,TAO_CLASSID,1); PetscValidHeaderSpecific(X,VEC_CLASSID,2); PetscCheckSameComm(tao,1,X,2); if (tao->ops->computeobjective) { ierr = PetscLogEventBegin(Tao_ObjectiveEval,tao,X,NULL,NULL);CHKERRQ(ierr); PetscStackPush("Tao user objective evaluation routine"); ierr = (*tao->ops->computeobjective)(tao,X,f,tao->user_objP);CHKERRQ(ierr); PetscStackPop; ierr = PetscLogEventEnd(Tao_ObjectiveEval,tao,X,NULL,NULL);CHKERRQ(ierr); tao->nfuncs++; } else if (tao->ops->computeobjectiveandgradient) { ierr = PetscInfo(tao,"Duplicating variable vector in order to call func/grad routine\n");CHKERRQ(ierr); ierr = VecDuplicate(X,&temp);CHKERRQ(ierr); ierr = PetscLogEventBegin(Tao_ObjGradientEval,tao,X,NULL,NULL);CHKERRQ(ierr); PetscStackPush("Tao user objective/gradient evaluation routine"); ierr = (*tao->ops->computeobjectiveandgradient)(tao,X,f,temp,tao->user_objgradP);CHKERRQ(ierr); PetscStackPop; ierr = PetscLogEventEnd(Tao_ObjGradientEval,tao,X,NULL,NULL);CHKERRQ(ierr); ierr = VecDestroy(&temp);CHKERRQ(ierr); tao->nfuncgrads++; } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"TaoSetObjectiveRoutine() has not been called"); ierr = PetscInfo1(tao,"TAO Function evaluation: %14.12e\n",(double)(*f));CHKERRQ(ierr); PetscFunctionReturn(0); }
/*@C MatNullSpaceView - Visualizes a null space object. Collective on MatNullSpace Input Parameters: + matnull - the null space - viewer - visualization context Level: advanced Fortran Note: This routine is not supported in Fortran. .seealso: MatNullSpaceCreate(), PetscViewerASCIIOpen() @*/ PetscErrorCode MatNullSpaceView(MatNullSpace sp,PetscViewer viewer) { PetscErrorCode ierr; PetscBool iascii; PetscFunctionBegin; PetscValidHeaderSpecific(sp,MAT_NULLSPACE_CLASSID,1); if (!viewer) { ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)sp),&viewer);CHKERRQ(ierr); } PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); PetscCheckSameComm(sp,1,viewer,2); ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); if (iascii) { PetscViewerFormat format; PetscInt i; ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr); ierr = PetscObjectPrintClassNamePrefixType((PetscObject)sp,viewer);CHKERRQ(ierr); ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); ierr = PetscViewerASCIIPrintf(viewer,"Contains %D vector%s%s\n",sp->n,sp->n==1 ? "" : "s",sp->has_cnst ? " and the constant" : "");CHKERRQ(ierr); if (sp->remove) {ierr = PetscViewerASCIIPrintf(viewer,"Has user-provided removal function\n");CHKERRQ(ierr);} if (!(format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_DETAIL)) { for (i=0; i<sp->n; i++) { ierr = VecView(sp->vecs[i],viewer);CHKERRQ(ierr); } } ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); } PetscFunctionReturn(0); }
/*@ PEPGetEigenpair - Gets the i-th solution of the eigenproblem as computed by PEPSolve(). The solution consists in both the eigenvalue and the eigenvector. Logically Collective on EPS Input Parameters: + pep - polynomial eigensolver context - i - index of the solution Output Parameters: + eigr - real part of eigenvalue . eigi - imaginary part of eigenvalue . Vr - real part of eigenvector - Vi - imaginary part of eigenvector Notes: If the eigenvalue is real, then eigi and Vi are set to zero. If PETSc is configured with complex scalars the eigenvalue is stored directly in eigr (eigi is set to zero) and the eigenvector in Vr (Vi is set to zero). The index i should be a value between 0 and nconv-1 (see PEPGetConverged()). Eigenpairs are indexed according to the ordering criterion established with PEPSetWhichEigenpairs(). Level: beginner .seealso: PEPSolve(), PEPGetConverged(), PEPSetWhichEigenpairs() @*/ PetscErrorCode PEPGetEigenpair(PEP pep,PetscInt i,PetscScalar *eigr,PetscScalar *eigi,Vec Vr,Vec Vi) { PetscInt k; PetscErrorCode ierr; PetscFunctionBegin; PetscValidHeaderSpecific(pep,PEP_CLASSID,1); PetscValidLogicalCollectiveInt(pep,i,2); if (Vr) { PetscValidHeaderSpecific(Vr,VEC_CLASSID,5); PetscCheckSameComm(pep,1,Vr,5); } if (Vi) { PetscValidHeaderSpecific(Vi,VEC_CLASSID,6); PetscCheckSameComm(pep,1,Vi,6); } PEPCheckSolved(pep,1); if (i<0 || i>=pep->nconv) SETERRQ(PetscObjectComm((PetscObject)pep),PETSC_ERR_ARG_OUTOFRANGE,"Argument 2 out of range"); ierr = PEPComputeVectors(pep);CHKERRQ(ierr); if (!pep->perm) k = i; else k = pep->perm[i]; /* eigenvalue */ #if defined(PETSC_USE_COMPLEX) if (eigr) *eigr = pep->eigr[k]; if (eigi) *eigi = 0; #else if (eigr) *eigr = pep->eigr[k]; if (eigi) *eigi = pep->eigi[k]; #endif /* eigenvector */ #if defined(PETSC_USE_COMPLEX) if (Vr) { ierr = BVCopyVec(pep->V,k,Vr);CHKERRQ(ierr); } if (Vi) { ierr = VecSet(Vi,0.0);CHKERRQ(ierr); } #else if (pep->eigi[k]>0) { /* first value of conjugate pair */ if (Vr) { ierr = BVCopyVec(pep->V,k,Vr);CHKERRQ(ierr); } if (Vi) { ierr = BVCopyVec(pep->V,k+1,Vi);CHKERRQ(ierr); } } else if (pep->eigi[k]<0) { /* second value of conjugate pair */ if (Vr) { ierr = BVCopyVec(pep->V,k-1,Vr);CHKERRQ(ierr); } if (Vi) { ierr = BVCopyVec(pep->V,k,Vi);CHKERRQ(ierr); ierr = VecScale(Vi,-1.0);CHKERRQ(ierr); } } else { /* real eigenvalue */ if (Vr) { ierr = BVCopyVec(pep->V,k,Vr);CHKERRQ(ierr); } if (Vi) { ierr = VecSet(Vi,0.0);CHKERRQ(ierr); } } #endif PetscFunctionReturn(0); }
/*@ MatSchurComplementUpdateSubMatrices - Updates the Schur complement matrix object with new submatrices Collective on Mat Input Parameters: + S - matrix obtained with MatCreateSchurComplement() (or equivalent) and implementing the action of A11 - A10 ksp(A00,Ap00) A01 . A00,A01,A10,A11 - the four parts of A = [A00 A01; A10 A11] (A11 is optional) - Ap00 - preconditioning matrix for use in ksp(A00,Ap00) to approximate the action of A^{-1}. Level: intermediate Notes: All four matrices must have the same MPI communicator A00 and A11 must be square matrices All of the matrices provided must have the same sizes as was used with MatCreateSchurComplement() or MatSchurComplementSetSubMatrices() though they need not be the same matrices. .seealso: MatCreateNormal(), MatMult(), MatCreate(), MatSchurComplementGetKSP(), MatCreateSchurComplement() @*/ PetscErrorCode MatSchurComplementUpdateSubMatrices(Mat S,Mat A00,Mat Ap00,Mat A01,Mat A10,Mat A11) { PetscErrorCode ierr; Mat_SchurComplement *Na = (Mat_SchurComplement*)S->data; PetscFunctionBegin; if (!S->assembled) SETERRQ(PetscObjectComm((PetscObject)S),PETSC_ERR_ARG_WRONGSTATE,"Use MatSchurComplementSetSubMatrices() for a new matrix"); PetscValidHeaderSpecific(A00,MAT_CLASSID,1); PetscValidHeaderSpecific(A01,MAT_CLASSID,2); PetscValidHeaderSpecific(A10,MAT_CLASSID,3); PetscCheckSameComm(A00,1,Ap00,2); PetscCheckSameComm(A00,1,A01,3); PetscCheckSameComm(A00,1,A10,4); if (A00->rmap->n != A00->cmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Local rows of A00 %D do not equal local columns %D",A00->rmap->n,A00->cmap->n); if (A00->rmap->n != Ap00->rmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Local rows of A00 %D do not equal local rows of Ap00 %D",A00->rmap->n,Ap00->rmap->n); if (Ap00->rmap->n != Ap00->cmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Local rows of Ap00 %D do not equal local columns %D",Ap00->rmap->n,Ap00->cmap->n); if (A00->cmap->n != A01->rmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Local columns of A00 %D do not equal local rows of A01 %D",A00->cmap->n,A01->rmap->n); if (A10->cmap->n != A00->rmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Local columns of A10 %D do not equal local rows of A00 %D",A10->cmap->n,A00->rmap->n); if (A11) { PetscValidHeaderSpecific(A11,MAT_CLASSID,5); PetscCheckSameComm(A00,1,A11,5); if (A10->rmap->n != A11->rmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Local rows of A10 %D do not equal local rows A11 %D",A10->rmap->n,A11->rmap->n); } ierr = PetscObjectReference((PetscObject)A00);CHKERRQ(ierr); ierr = PetscObjectReference((PetscObject)Ap00);CHKERRQ(ierr); ierr = PetscObjectReference((PetscObject)A01);CHKERRQ(ierr); ierr = PetscObjectReference((PetscObject)A10);CHKERRQ(ierr); if (A11) { ierr = PetscObjectReference((PetscObject)A11);CHKERRQ(ierr); } ierr = MatDestroy(&Na->A);CHKERRQ(ierr); ierr = MatDestroy(&Na->Ap);CHKERRQ(ierr); ierr = MatDestroy(&Na->B);CHKERRQ(ierr); ierr = MatDestroy(&Na->C);CHKERRQ(ierr); ierr = MatDestroy(&Na->D);CHKERRQ(ierr); Na->A = A00; Na->Ap = Ap00; Na->B = A01; Na->C = A10; Na->D = A11; ierr = KSPSetOperators(Na->ksp,A00,Ap00);CHKERRQ(ierr); PetscFunctionReturn(0); }
/*@C PetscRandomView - Views a random number generator object. Collective on PetscRandom Input Parameters: + rnd - The random number generator context - viewer - an optional visualization context Notes: The available visualization contexts include + PETSC_VIEWER_STDOUT_SELF - standard output (default) - PETSC_VIEWER_STDOUT_WORLD - synchronized standard output where only the first processor opens the file. All other processors send their data to the first processor to print. You can change the format the vector is printed using the option PetscViewerSetFormat(). Level: beginner .seealso: PetscRealView(), PetscScalarView(), PetscIntView() @*/ PetscErrorCode PetscRandomView(PetscRandom rnd,PetscViewer viewer) { PetscErrorCode ierr; PetscBool iascii; #if defined(PETSC_HAVE_SAWS) PetscBool issaws; #endif PetscFunctionBegin; PetscValidHeaderSpecific(rnd,PETSC_RANDOM_CLASSID,1); PetscValidType(rnd,1); if (!viewer) { ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)rnd),&viewer); CHKERRQ(ierr); } PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); PetscCheckSameComm(rnd,1,viewer,2); ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii); CHKERRQ(ierr); #if defined(PETSC_HAVE_SAWS) ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSAWS,&issaws); CHKERRQ(ierr); #endif if (iascii) { PetscMPIInt rank; ierr = PetscObjectPrintClassNamePrefixType((PetscObject)rnd,viewer); CHKERRQ(ierr); ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)rnd),&rank); CHKERRQ(ierr); ierr = PetscViewerASCIIPushSynchronized(viewer); CHKERRQ(ierr); ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] Random type %s, seed %D\n",rank,((PetscObject)rnd)->type_name,rnd->seed); CHKERRQ(ierr); ierr = PetscViewerFlush(viewer); CHKERRQ(ierr); ierr = PetscViewerASCIIPopSynchronized(viewer); CHKERRQ(ierr); #if defined(PETSC_HAVE_SAWS) } else if (issaws) { PetscMPIInt rank; const char *name; ierr = PetscObjectGetName((PetscObject)rnd,&name); CHKERRQ(ierr); ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank); CHKERRQ(ierr); if (!((PetscObject)rnd)->amsmem && !rank) { char dir[1024]; ierr = PetscObjectViewSAWs((PetscObject)rnd,viewer); CHKERRQ(ierr); ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/Low",name); CHKERRQ(ierr); PetscStackCallSAWs(SAWs_Register,(dir,&rnd->low,1,SAWs_READ,SAWs_DOUBLE)); } #endif } PetscFunctionReturn(0); }
/*@C TaoSetJacobianStateRoutine - Sets the function to compute the Jacobian (and its inverse) of the constraint function with respect to the state variables. Used only for pde-constrained optimization. Logically collective on Tao Input Parameters: + tao - the Tao context . J - Matrix used for the jacobian . Jpre - Matrix that will be used operated on by PETSc preconditioner, can be same as J. Only used if Jinv is NULL . Jinv - [optional] Matrix used to apply the inverse of the state jacobian. Use NULL to default to PETSc KSP solvers to apply the inverse. . jac - Jacobian evaluation routine - ctx - [optional] user-defined context for private data for the Jacobian evaluation routine (may be NULL) Calling sequence of jac: $ jac (Tao tao,Vec x,Mat *J,Mat *Jpre,void *ctx); + tao - the Tao context . x - input vector . J - Jacobian matrix . Jpre - preconditioner matrix, usually the same as J . Jinv - inverse of J - ctx - [optional] user-defined Jacobian context Level: intermediate .seealse: TaoComputeJacobianState(), TaoSetJacobianDesignRoutine(), TaoSetStateDesignIS() @*/ PetscErrorCode TaoSetJacobianStateRoutine(Tao tao, Mat J, Mat Jpre, Mat Jinv, PetscErrorCode (*func)(Tao, Vec, Mat, Mat, Mat,void*), void *ctx) { PetscErrorCode ierr; PetscFunctionBegin; PetscValidHeaderSpecific(tao,TAO_CLASSID,1); if (J) { PetscValidHeaderSpecific(J,MAT_CLASSID,2); PetscCheckSameComm(tao,1,J,2); } if (Jpre) { PetscValidHeaderSpecific(Jpre,MAT_CLASSID,3); PetscCheckSameComm(tao,1,Jpre,3); } if (Jinv) { PetscValidHeaderSpecific(Jinv,MAT_CLASSID,4); PetscCheckSameComm(tao,1,Jinv,4); } if (ctx) { tao->user_jac_stateP = ctx; } if (func) { tao->ops->computejacobianstate = func; } if (J) { ierr = PetscObjectReference((PetscObject)J); CHKERRQ(ierr); ierr = MatDestroy(&tao->jacobian_state); CHKERRQ(ierr); tao->jacobian_state = J; } if (Jpre) { ierr = PetscObjectReference((PetscObject)Jpre); CHKERRQ(ierr); ierr = MatDestroy(&tao->jacobian_state_pre); CHKERRQ(ierr); tao->jacobian_state_pre=Jpre; } if (Jinv) { ierr = PetscObjectReference((PetscObject)Jinv); CHKERRQ(ierr); ierr = MatDestroy(&tao->jacobian_state_inv); CHKERRQ(ierr); tao->jacobian_state_inv=Jinv; } PetscFunctionReturn(0); }
/*@ TaoComputeDualVariables - Computes the dual vectors corresponding to the bounds of the variables Collective on Tao Input Parameters: . tao - the Tao context Output Parameter: + DL - dual variable vector for the lower bounds - DU - dual variable vector for the upper bounds Level: advanced Note: DL and DU should be created before calling this routine. If calling this routine after using an unconstrained solver, DL and DU are set to all zeros. Level: advanced .seealso: TaoComputeObjective(), TaoSetVariableBounds() @*/ PetscErrorCode TaoComputeDualVariables(Tao tao, Vec DL, Vec DU) { PetscErrorCode ierr; PetscFunctionBegin; PetscValidHeaderSpecific(tao,TAO_CLASSID,1); PetscValidHeaderSpecific(DL,VEC_CLASSID,2); PetscValidHeaderSpecific(DU,VEC_CLASSID,2); PetscCheckSameComm(tao,1,DL,2); PetscCheckSameComm(tao,1,DU,3); if (tao->ops->computedual) { ierr = (*tao->ops->computedual)(tao,DL,DU);CHKERRQ(ierr); } else { ierr = VecSet(DL,0.0);CHKERRQ(ierr); ierr = VecSet(DU,0.0);CHKERRQ(ierr); } PetscFunctionReturn(0); }
PetscErrorCode TaoLineSearchView(TaoLineSearch ls, PetscViewer viewer) { PetscErrorCode ierr; PetscBool isascii, isstring; const TaoLineSearchType type; PetscBool destroyviewer = PETSC_FALSE; PetscFunctionBegin; PetscValidHeaderSpecific(ls,TAOLINESEARCH_CLASSID,1); if (!viewer) { destroyviewer = PETSC_TRUE; ierr = PetscViewerASCIIGetStdout(((PetscObject)ls)->comm, &viewer);CHKERRQ(ierr); } PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); PetscCheckSameComm(ls,1,viewer,2); ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&isascii);CHKERRQ(ierr); ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr); if (isascii) { if (((PetscObject)ls)->prefix) { ierr = PetscViewerASCIIPrintf(viewer,"TaoLineSearch Object:(%s)\n",((PetscObject)ls)->prefix);CHKERRQ(ierr); } else { ierr = PetscViewerASCIIPrintf(viewer,"TaoLineSearch Object:\n");CHKERRQ(ierr); } ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); ierr = TaoLineSearchGetType(ls,&type);CHKERRQ(ierr); if (type) { ierr = PetscViewerASCIIPrintf(viewer,"type: %s\n",type);CHKERRQ(ierr); } else { ierr = PetscViewerASCIIPrintf(viewer,"type: not set yet\n");CHKERRQ(ierr); } if (ls->ops->view) { ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); ierr = (*ls->ops->view)(ls,viewer);CHKERRQ(ierr); ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); } ierr = PetscViewerASCIIPrintf(viewer,"maximum function evaluations=%D\n",ls->max_funcs);CHKERRQ(ierr); ierr = PetscViewerASCIIPrintf(viewer,"tolerances: ftol=%g, rtol=%g, gtol=%g\n",(double)ls->ftol,(double)ls->rtol,(double)ls->gtol);CHKERRQ(ierr); ierr = PetscViewerASCIIPrintf(viewer,"total number of function evaluations=%D\n",ls->nfeval);CHKERRQ(ierr); ierr = PetscViewerASCIIPrintf(viewer,"total number of gradient evaluations=%D\n",ls->ngeval);CHKERRQ(ierr); ierr = PetscViewerASCIIPrintf(viewer,"total number of function/gradient evaluations=%D\n",ls->nfgeval);CHKERRQ(ierr); if (ls->bounded) { ierr = PetscViewerASCIIPrintf(viewer,"using variable bounds\n");CHKERRQ(ierr); } ierr = PetscViewerASCIIPrintf(viewer,"Termination reason: %D\n",(int)ls->reason);CHKERRQ(ierr); ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); } else if (isstring) { ierr = TaoLineSearchGetType(ls,&type);CHKERRQ(ierr); ierr = PetscViewerStringSPrintf(viewer," %-3.3s",type);CHKERRQ(ierr); } if (destroyviewer) { ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); } PetscFunctionReturn(0); }
/*@ KSPSetOperators - Sets the matrix associated with the linear system and a (possibly) different one associated with the preconditioner. Collective on KSP and Mat Input Parameters: + ksp - the KSP context . Amat - the matrix that defines the linear system - Pmat - the matrix to be used in constructing the preconditioner, usually the same as Amat. Notes: If you know the operator Amat has a null space you can use MatSetNullSpace() and MatSetTransposeNullSpace() to supply the null space to Amat and the KSP solvers will automatically use that null space as needed during the solution process. All future calls to KSPSetOperators() must use the same size matrices! Passing a NULL for Amat or Pmat removes the matrix that is currently used. If you wish to replace either Amat or Pmat but leave the other one untouched then first call KSPGetOperators() to get the one you wish to keep, call PetscObjectReference() on it and then pass it back in in your call to KSPSetOperators(). Level: beginner Alternative usage: If the operators have NOT been set with KSP/PCSetOperators() then the operators are created in PC and returned to the user. In this case, if both operators mat and pmat are requested, two DIFFERENT operators will be returned. If only one is requested both operators in the PC will be the same (i.e. as if one had called KSP/PCSetOperators() with the same argument for both Mats). The user must set the sizes of the returned matrices and their type etc just as if the user created them with MatCreate(). For example, $ KSP/PCGetOperators(ksp/pc,&mat,NULL); is equivalent to $ set size, type, etc of mat $ MatCreate(comm,&mat); $ KSP/PCSetOperators(ksp/pc,mat,mat); $ PetscObjectDereference((PetscObject)mat); $ set size, type, etc of mat and $ KSP/PCGetOperators(ksp/pc,&mat,&pmat); is equivalent to $ set size, type, etc of mat and pmat $ MatCreate(comm,&mat); $ MatCreate(comm,&pmat); $ KSP/PCSetOperators(ksp/pc,mat,pmat); $ PetscObjectDereference((PetscObject)mat); $ PetscObjectDereference((PetscObject)pmat); $ set size, type, etc of mat and pmat The rational for this support is so that when creating a TS, SNES, or KSP the hierarchy of underlying objects (i.e. SNES, KSP, PC, Mat) and their livespans can be completely managed by the top most level object (i.e. the TS, SNES, or KSP). Another way to look at this is when you create a SNES you do not NEED to create a KSP and attach it to the SNES object (the SNES object manages it for you). Similarly when you create a KSP you do not need to attach a PC to it (the KSP object manages the PC object for you). Thus, why should YOU have to create the Mat and attach it to the SNES/KSP/PC, when it can be created for you? .keywords: KSP, set, operators, matrix, preconditioner, linear system .seealso: KSPSolve(), KSPGetPC(), PCGetOperators(), PCSetOperators(), KSPGetOperators(), KSPSetComputeOperators(), KSPSetComputeInitialGuess(), KSPSetComputeRHS() @*/ PetscErrorCode KSPSetOperators(KSP ksp,Mat Amat,Mat Pmat) { PetscErrorCode ierr; PetscFunctionBegin; PetscValidHeaderSpecific(ksp,KSP_CLASSID,1); if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2); if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3); if (Amat) PetscCheckSameComm(ksp,1,Amat,2); if (Pmat) PetscCheckSameComm(ksp,1,Pmat,3); if (!ksp->pc) {ierr = KSPGetPC(ksp,&ksp->pc);CHKERRQ(ierr);} ierr = PCSetOperators(ksp->pc,Amat,Pmat);CHKERRQ(ierr); if (ksp->setupstage == KSP_SETUP_NEWRHS) ksp->setupstage = KSP_SETUP_NEWMATRIX; /* so that next solve call will call PCSetUp() on new matrix */ if (ksp->guess) { ierr = KSPFischerGuessReset(ksp->guess);CHKERRQ(ierr); } PetscFunctionReturn(0); }
/*@ MatMultEqual - Compares matrix-vector products of two matrices. Collective on Mat Input Parameters: + A - the first matrix - B - the second matrix - n - number of random vectors to be tested Output Parameter: . flg - PETSC_TRUE if the products are equal; PETSC_FALSE otherwise. Level: intermediate Concepts: matrices^equality between @*/ PetscErrorCode MatMultEqual(Mat A,Mat B,PetscInt n,PetscBool *flg) { PetscErrorCode ierr; Vec x,s1,s2; PetscRandom rctx; PetscReal r1,r2,tol=1.e-10; PetscInt am,an,bm,bn,k; PetscScalar none = -1.0; PetscFunctionBegin; PetscValidHeaderSpecific(A,MAT_CLASSID,1); PetscValidHeaderSpecific(B,MAT_CLASSID,2); ierr = MatGetLocalSize(A,&am,&an);CHKERRQ(ierr); ierr = MatGetLocalSize(B,&bm,&bn);CHKERRQ(ierr); if (am != bm || an != bn) SETERRQ4(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Mat A,Mat B: local dim %D %D %D %D",am,bm,an,bn); PetscCheckSameComm(A,1,B,2); #if defined(PETSC_USE_REAL_SINGLE) tol = 1.e-5; #endif ierr = PetscRandomCreate(((PetscObject)A)->comm,&rctx);CHKERRQ(ierr); ierr = PetscRandomSetFromOptions(rctx);CHKERRQ(ierr); ierr = VecCreate(((PetscObject)A)->comm,&x);CHKERRQ(ierr); ierr = VecSetSizes(x,an,PETSC_DECIDE);CHKERRQ(ierr); ierr = VecSetFromOptions(x);CHKERRQ(ierr); ierr = VecCreate(((PetscObject)A)->comm,&s1);CHKERRQ(ierr); ierr = VecSetSizes(s1,am,PETSC_DECIDE);CHKERRQ(ierr); ierr = VecSetFromOptions(s1);CHKERRQ(ierr); ierr = VecDuplicate(s1,&s2);CHKERRQ(ierr); *flg = PETSC_TRUE; for (k=0; k<n; k++) { ierr = VecSetRandom(x,rctx);CHKERRQ(ierr); ierr = MatMult(A,x,s1);CHKERRQ(ierr); ierr = MatMult(B,x,s2);CHKERRQ(ierr); ierr = VecNorm(s2,NORM_INFINITY,&r2);CHKERRQ(ierr); if (r2 < tol){ ierr = VecNorm(s1,NORM_INFINITY,&r1);CHKERRQ(ierr); } else { ierr = VecAXPY(s2,none,s1);CHKERRQ(ierr); ierr = VecNorm(s2,NORM_INFINITY,&r1);CHKERRQ(ierr); r1 /= r2; } if (r1 > tol) { *flg = PETSC_FALSE; ierr = PetscInfo2(A,"Error: %D-th MatMult() %G\n",k,r1);CHKERRQ(ierr); break; } } ierr = PetscRandomDestroy(&rctx);CHKERRQ(ierr); ierr = VecDestroy(&x);CHKERRQ(ierr); ierr = VecDestroy(&s1);CHKERRQ(ierr); ierr = VecDestroy(&s2);CHKERRQ(ierr); PetscFunctionReturn(0); }
/*@ MFNSolve - Solves the matrix function problem. Given a vector b, the vector x = f(alpha*A)*b is returned. Collective on MFN Input Parameters: + mfn - matrix function context obtained from MFNCreate() - b - the right hand side vector Output Parameter: . x - the solution (this may be the same vector as b, then b will be overwritten with the answer) Options Database Keys: + -mfn_view - print information about the solver used . -mfn_view_mat binary - save the matrix to the default binary viewer . -mfn_view_rhs binary - save right hand side vector to the default binary viewer - -mfn_view_solution binary - save computed solution vector to the default binary viewer Notes: The matrix A is specified with MFNSetOperator(). The function f is specified with MFNSetFunction(). The scalar alpha is specified with MFNSetScaleFactor(). Level: beginner .seealso: MFNCreate(), MFNSetUp(), MFNDestroy(), MFNSetTolerances(), MFNSetOperator(), MFNSetFunction(), MFNSetScaleFactor() @*/ PetscErrorCode MFNSolve(MFN mfn,Vec b,Vec x) { PetscErrorCode ierr; PetscBool flg; PetscViewer viewer; PetscViewerFormat format; PetscFunctionBegin; PetscValidHeaderSpecific(mfn,MFN_CLASSID,1); PetscValidHeaderSpecific(b,VEC_CLASSID,2); PetscCheckSameComm(mfn,1,b,2); if (b!=x) PetscValidHeaderSpecific(x,VEC_CLASSID,3); if (b!=x) PetscCheckSameComm(mfn,1,x,3); /* call setup */ ierr = MFNSetUp(mfn);CHKERRQ(ierr); mfn->its = 0; ierr = MFNMonitor(mfn,mfn->its,0);CHKERRQ(ierr); /* call solver */ ierr = PetscLogEventBegin(MFN_Solve,mfn,b,x,0);CHKERRQ(ierr); ierr = (*mfn->ops->solve)(mfn,b,x);CHKERRQ(ierr); ierr = PetscLogEventEnd(MFN_Solve,mfn,b,x,0);CHKERRQ(ierr); if (!mfn->reason) SETERRQ(PetscObjectComm((PetscObject)mfn),PETSC_ERR_PLIB,"Internal error, solver returned without setting converged reason"); if (mfn->errorifnotconverged && mfn->reason < 0) SETERRQ(PetscObjectComm((PetscObject)mfn),PETSC_ERR_NOT_CONVERGED,"MFNSolve has not converged"); /* various viewers */ ierr = MatViewFromOptions(mfn->A,((PetscObject)mfn)->prefix,"-mfn_view_mat");CHKERRQ(ierr); ierr = VecViewFromOptions(b,((PetscObject)mfn)->prefix,"-mfn_view_rhs");CHKERRQ(ierr); ierr = VecViewFromOptions(x,((PetscObject)mfn)->prefix,"-mfn_view_solution");CHKERRQ(ierr); ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject)mfn),((PetscObject)mfn)->prefix,"-mfn_view",&viewer,&format,&flg);CHKERRQ(ierr); if (flg && !PetscPreLoadingOn) { ierr = PetscViewerPushFormat(viewer,format);CHKERRQ(ierr); ierr = MFNView(mfn,viewer);CHKERRQ(ierr); ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr); ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); } PetscFunctionReturn(0); }
/*@ NEPInterpolSetPEP - Associate a polynomial eigensolver object (PEP) to the nonlinear eigenvalue solver. Collective on NEP Input Parameters: + nep - nonlinear eigenvalue solver - pep - the polynomial eigensolver object Level: advanced .seealso: NEPInterpolGetPEP() @*/ PetscErrorCode NEPInterpolSetPEP(NEP nep,PEP pep) { PetscErrorCode ierr; PetscFunctionBegin; PetscValidHeaderSpecific(nep,NEP_CLASSID,1); PetscValidHeaderSpecific(pep,PEP_CLASSID,2); PetscCheckSameComm(nep,1,pep,2); ierr = PetscTryMethod(nep,"NEPInterpolSetPEP_C",(NEP,PEP),(nep,pep));CHKERRQ(ierr); PetscFunctionReturn(0); }
/*@ PEPLinearSetEPS - Associate an eigensolver object (EPS) to the polynomial eigenvalue solver. Collective on PEP Input Parameters: + pep - polynomial eigenvalue solver - eps - the eigensolver object Level: advanced .seealso: PEPLinearGetEPS() @*/ PetscErrorCode PEPLinearSetEPS(PEP pep,EPS eps) { PetscErrorCode ierr; PetscFunctionBegin; PetscValidHeaderSpecific(pep,PEP_CLASSID,1); PetscValidHeaderSpecific(eps,EPS_CLASSID,2); PetscCheckSameComm(pep,1,eps,2); ierr = PetscTryMethod(pep,"PEPLinearSetEPS_C",(PEP,EPS),(pep,eps));CHKERRQ(ierr); PetscFunctionReturn(0); }
/*@ NEPSLPSetEPS - Associate a linear eigensolver object (EPS) to the nonlinear eigenvalue solver. Collective on NEP Input Parameters: + nep - nonlinear eigenvalue solver - eps - the eigensolver object Level: advanced .seealso: NEPSLPGetEPS() @*/ PetscErrorCode NEPSLPSetEPS(NEP nep,EPS eps) { PetscErrorCode ierr; PetscFunctionBegin; PetscValidHeaderSpecific(nep,NEP_CLASSID,1); PetscValidHeaderSpecific(eps,EPS_CLASSID,2); PetscCheckSameComm(nep,1,eps,2); ierr = PetscTryMethod(nep,"NEPSLPSetEPS_C",(NEP,EPS),(nep,eps));CHKERRQ(ierr); PetscFunctionReturn(0); }