LCOV - code coverage report
Current view: top level - svd/interface - svdview.c (source / functions) Hit Total Coverage
Test: SLEPc Lines: 313 345 90.7 %
Date: 2021-08-02 00:32:28 Functions: 17 17 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
       3             :    SLEPc - Scalable Library for Eigenvalue Problem Computations
       4             :    Copyright (c) 2002-2021, Universitat Politecnica de Valencia, Spain
       5             : 
       6             :    This file is part of SLEPc.
       7             :    SLEPc is distributed under a 2-clause BSD license (see LICENSE).
       8             :    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
       9             : */
      10             : /*
      11             :    The SVD routines related to various viewers
      12             : */
      13             : 
      14             : #include <slepc/private/svdimpl.h>      /*I "slepcsvd.h" I*/
      15             : #include <petscdraw.h>
      16             : 
      17             : /*@C
      18             :    SVDView - Prints the SVD data structure.
      19             : 
      20             :    Collective on svd
      21             : 
      22             :    Input Parameters:
      23             : +  svd - the singular value solver context
      24             : -  viewer - optional visualization context
      25             : 
      26             :    Options Database Key:
      27             : .  -svd_view -  Calls SVDView() at end of SVDSolve()
      28             : 
      29             :    Note:
      30             :    The available visualization contexts include
      31             : +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
      32             : -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
      33             :          output where only the first processor opens
      34             :          the file.  All other processors send their
      35             :          data to the first processor to print.
      36             : 
      37             :    The user can open an alternative visualization context with
      38             :    PetscViewerASCIIOpen() - output to a specified file.
      39             : 
      40             :    Level: beginner
      41             : @*/
      42           3 : PetscErrorCode SVDView(SVD svd,PetscViewer viewer)
      43             : {
      44           3 :   PetscErrorCode ierr;
      45           3 :   const char     *type=NULL;
      46           3 :   PetscBool      isascii,isshell;
      47             : 
      48           3 :   PetscFunctionBegin;
      49           3 :   PetscValidHeaderSpecific(svd,SVD_CLASSID,1);
      50           3 :   if (!viewer) {
      51           0 :     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)svd),&viewer);CHKERRQ(ierr);
      52             :   }
      53           3 :   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
      54           3 :   PetscCheckSameComm(svd,1,viewer,2);
      55             : 
      56           3 :   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&isascii);CHKERRQ(ierr);
      57           3 :   if (isascii) {
      58           3 :     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)svd,viewer);CHKERRQ(ierr);
      59           3 :     if (svd->ops->view) {
      60           3 :       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
      61           3 :       ierr = (*svd->ops->view)(svd,viewer);CHKERRQ(ierr);
      62           3 :       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
      63             :     }
      64           3 :     if (svd->problem_type) {
      65           3 :       switch (svd->problem_type) {
      66           3 :         case SVD_STANDARD:    type = "(standard) singular value problem"; break;
      67           0 :         case SVD_GENERALIZED: type = "generalized singular value problem"; break;
      68             :       }
      69             :     } else type = "not yet set";
      70           3 :     ierr = PetscViewerASCIIPrintf(viewer,"  problem type: %s\n",type);CHKERRQ(ierr);
      71           6 :     ierr = PetscViewerASCIIPrintf(viewer,"  transpose mode: %s\n",svd->impltrans?"implicit":"explicit");CHKERRQ(ierr);
      72           3 :     if (svd->which == SVD_LARGEST) {
      73           3 :       ierr = PetscViewerASCIIPrintf(viewer,"  selected portion of the spectrum: largest\n");CHKERRQ(ierr);
      74             :     } else {
      75           0 :       ierr = PetscViewerASCIIPrintf(viewer,"  selected portion of the spectrum: smallest\n");CHKERRQ(ierr);
      76             :     }
      77           3 :     ierr = PetscViewerASCIIPrintf(viewer,"  number of singular values (nsv): %D\n",svd->nsv);CHKERRQ(ierr);
      78           3 :     ierr = PetscViewerASCIIPrintf(viewer,"  number of column vectors (ncv): %D\n",svd->ncv);CHKERRQ(ierr);
      79           3 :     ierr = PetscViewerASCIIPrintf(viewer,"  maximum dimension of projected problem (mpd): %D\n",svd->mpd);CHKERRQ(ierr);
      80           3 :     ierr = PetscViewerASCIIPrintf(viewer,"  maximum number of iterations: %D\n",svd->max_it);CHKERRQ(ierr);
      81           3 :     ierr = PetscViewerASCIIPrintf(viewer,"  tolerance: %g\n",(double)svd->tol);CHKERRQ(ierr);
      82           3 :     ierr = PetscViewerASCIIPrintf(viewer,"  convergence test: ");CHKERRQ(ierr);
      83           3 :     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
      84           3 :     switch (svd->conv) {
      85           0 :     case SVD_CONV_ABS:
      86           0 :       ierr = PetscViewerASCIIPrintf(viewer,"absolute\n");CHKERRQ(ierr);break;
      87           3 :     case SVD_CONV_REL:
      88           3 :       ierr = PetscViewerASCIIPrintf(viewer,"relative to the singular value\n");CHKERRQ(ierr);break;
      89           0 :     case EPS_CONV_NORM:
      90           0 :       ierr = PetscViewerASCIIPrintf(viewer,"relative to the matrix norms\n");CHKERRQ(ierr);
      91           0 :       ierr = PetscViewerASCIIPrintf(viewer,"  computed matrix norms: norm(A)=%g",(double)svd->nrma);CHKERRQ(ierr);
      92           0 :       if (svd->isgeneralized) {
      93           0 :         ierr = PetscViewerASCIIPrintf(viewer,", norm(B)=%g",(double)svd->nrmb);CHKERRQ(ierr);
      94             :       }
      95           0 :       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
      96             :       break;
      97           0 :     case SVD_CONV_MAXIT:
      98           0 :       ierr = PetscViewerASCIIPrintf(viewer,"maximum number of iterations\n");CHKERRQ(ierr);break;
      99           0 :     case SVD_CONV_USER:
     100           0 :       ierr = PetscViewerASCIIPrintf(viewer,"user-defined\n");CHKERRQ(ierr);break;
     101             :     }
     102           3 :     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
     103           3 :     if (svd->nini) {
     104           0 :       ierr = PetscViewerASCIIPrintf(viewer,"  dimension of user-provided initial space: %D\n",PetscAbs(svd->nini));CHKERRQ(ierr);
     105             :     }
     106           3 :     if (svd->ninil) {
     107           0 :       ierr = PetscViewerASCIIPrintf(viewer,"  dimension of user-provided initial left space: %D\n",PetscAbs(svd->ninil));CHKERRQ(ierr);
     108             :     }
     109             :   } else {
     110           0 :     if (svd->ops->view) {
     111           0 :       ierr = (*svd->ops->view)(svd,viewer);CHKERRQ(ierr);
     112             :     }
     113             :   }
     114           3 :   ierr = PetscObjectTypeCompareAny((PetscObject)svd,&isshell,SVDCROSS,SVDCYCLIC,SVDSCALAPACK,SVDELEMENTAL,SVDPRIMME,"");CHKERRQ(ierr);
     115           3 :   if (!isshell) {
     116           1 :     ierr = PetscViewerPushFormat(viewer,PETSC_VIEWER_ASCII_INFO);CHKERRQ(ierr);
     117           1 :     if (!svd->V) { ierr = SVDGetBV(svd,&svd->V,NULL);CHKERRQ(ierr); }
     118           1 :     ierr = BVView(svd->V,viewer);CHKERRQ(ierr);
     119           1 :     if (!svd->ds) { ierr = SVDGetDS(svd,&svd->ds);CHKERRQ(ierr); }
     120           1 :     ierr = DSView(svd->ds,viewer);CHKERRQ(ierr);
     121           1 :     ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
     122             :   }
     123           3 :   PetscFunctionReturn(0);
     124             : }
     125             : 
     126             : /*@C
     127             :    SVDViewFromOptions - View from options
     128             : 
     129             :    Collective on SVD
     130             : 
     131             :    Input Parameters:
     132             : +  svd  - the singular value solver context
     133             : .  obj  - optional object
     134             : -  name - command line option
     135             : 
     136             :    Level: intermediate
     137             : 
     138             : .seealso: SVDView(), SVDCreate()
     139             : @*/
     140         374 : PetscErrorCode SVDViewFromOptions(SVD svd,PetscObject obj,const char name[])
     141             : {
     142         374 :   PetscErrorCode ierr;
     143             : 
     144         374 :   PetscFunctionBegin;
     145         374 :   PetscValidHeaderSpecific(svd,SVD_CLASSID,1);
     146         374 :   ierr = PetscObjectViewFromOptions((PetscObject)svd,obj,name);CHKERRQ(ierr);
     147         374 :   PetscFunctionReturn(0);
     148             : }
     149             : 
     150             : /*@C
     151             :    SVDConvergedReasonView - Displays the reason an SVD solve converged or diverged.
     152             : 
     153             :    Collective on svd
     154             : 
     155             :    Input Parameters:
     156             : +  svd - the singular value solver context
     157             : -  viewer - the viewer to display the reason
     158             : 
     159             :    Options Database Keys:
     160             : .  -svd_converged_reason - print reason for convergence, and number of iterations
     161             : 
     162             :    Note:
     163             :    To change the format of the output call PetscViewerPushFormat(viewer,format) before
     164             :    this call. Use PETSC_VIEWER_DEFAULT for the default, use PETSC_VIEWER_FAILED to only
     165             :    display a reason if it fails. The latter can be set in the command line with
     166             :    -svd_converged_reason ::failed
     167             : 
     168             :    Level: intermediate
     169             : 
     170             : .seealso: SVDSetTolerances(), SVDGetIterationNumber(), SVDConvergedReasonViewFromOptions()
     171             : @*/
     172           3 : PetscErrorCode SVDConvergedReasonView(SVD svd,PetscViewer viewer)
     173             : {
     174           3 :   PetscErrorCode    ierr;
     175           3 :   PetscBool         isAscii;
     176           3 :   PetscViewerFormat format;
     177             : 
     178           3 :   PetscFunctionBegin;
     179           3 :   if (!viewer) viewer = PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject)svd));
     180           3 :   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&isAscii);CHKERRQ(ierr);
     181           3 :   if (isAscii) {
     182           3 :     ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
     183           3 :     ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)svd)->tablevel);CHKERRQ(ierr);
     184           3 :     if (svd->reason > 0 && format != PETSC_VIEWER_FAILED) {
     185           3 :       ierr = PetscViewerASCIIPrintf(viewer,"%s SVD solve converged (%D singular triplet%s) due to %s; iterations %D\n",((PetscObject)svd)->prefix?((PetscObject)svd)->prefix:"",svd->nconv,(svd->nconv>1)?"s":"",SVDConvergedReasons[svd->reason],svd->its);CHKERRQ(ierr);
     186           0 :     } else if (svd->reason <= 0) {
     187           0 :       ierr = PetscViewerASCIIPrintf(viewer,"%s SVD solve did not converge due to %s; iterations %D\n",((PetscObject)svd)->prefix?((PetscObject)svd)->prefix:"",SVDConvergedReasons[svd->reason],svd->its);CHKERRQ(ierr);
     188             :     }
     189           3 :     ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)svd)->tablevel);CHKERRQ(ierr);
     190             :   }
     191           3 :   PetscFunctionReturn(0);
     192             : }
     193             : 
     194             : /*@
     195             :    SVDConvergedReasonViewFromOptions - Processes command line options to determine if/how
     196             :    the SVD converged reason is to be viewed.
     197             : 
     198             :    Collective on svd
     199             : 
     200             :    Input Parameter:
     201             : .  svd - the singular value solver context
     202             : 
     203             :    Level: developer
     204             : 
     205             : .seealso: SVDConvergedReasonView()
     206             : @*/
     207         187 : PetscErrorCode SVDConvergedReasonViewFromOptions(SVD svd)
     208             : {
     209         187 :   PetscErrorCode    ierr;
     210         187 :   PetscViewer       viewer;
     211         187 :   PetscBool         flg;
     212         187 :   static PetscBool  incall = PETSC_FALSE;
     213         187 :   PetscViewerFormat format;
     214             : 
     215         187 :   PetscFunctionBegin;
     216         187 :   if (incall) PetscFunctionReturn(0);
     217         187 :   incall = PETSC_TRUE;
     218         187 :   ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject)svd),((PetscObject)svd)->options,((PetscObject)svd)->prefix,"-svd_converged_reason",&viewer,&format,&flg);CHKERRQ(ierr);
     219         187 :   if (flg) {
     220           2 :     ierr = PetscViewerPushFormat(viewer,format);CHKERRQ(ierr);
     221           2 :     ierr = SVDConvergedReasonView(svd,viewer);CHKERRQ(ierr);
     222           2 :     ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
     223           2 :     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
     224             :   }
     225         187 :   incall = PETSC_FALSE;
     226         187 :   PetscFunctionReturn(0);
     227             : }
     228             : 
     229         137 : static PetscErrorCode SVDErrorView_ASCII(SVD svd,SVDErrorType etype,PetscViewer viewer)
     230             : {
     231         137 :   PetscReal      error,sigma;
     232         137 :   PetscInt       i,j;
     233         137 :   PetscErrorCode ierr;
     234             : 
     235         137 :   PetscFunctionBegin;
     236         137 :   if (svd->nconv<svd->nsv) {
     237           0 :     ierr = PetscViewerASCIIPrintf(viewer," Problem: less than %D singular values converged\n\n",svd->nsv);CHKERRQ(ierr);
     238           0 :     PetscFunctionReturn(0);
     239             :   }
     240         580 :   for (i=0;i<svd->nsv;i++) {
     241         443 :     ierr = SVDComputeError(svd,i,etype,&error);CHKERRQ(ierr);
     242         443 :     if (error>=5.0*svd->tol) {
     243           0 :       ierr = PetscViewerASCIIPrintf(viewer," Problem: some of the first %D relative errors are higher than the tolerance\n\n",svd->nsv);CHKERRQ(ierr);
     244         443 :       PetscFunctionReturn(0);
     245             :     }
     246             :   }
     247         137 :   ierr = PetscViewerASCIIPrintf(viewer," All requested singular values computed up to the required tolerance:");CHKERRQ(ierr);
     248         274 :   for (i=0;i<=(svd->nsv-1)/8;i++) {
     249         137 :     ierr = PetscViewerASCIIPrintf(viewer,"\n     ");CHKERRQ(ierr);
     250         580 :     for (j=0;j<PetscMin(8,svd->nsv-8*i);j++) {
     251         443 :       ierr = SVDGetSingularTriplet(svd,8*i+j,&sigma,NULL,NULL);CHKERRQ(ierr);
     252         443 :       ierr = PetscViewerASCIIPrintf(viewer,"%.5f",(double)sigma);CHKERRQ(ierr);
     253         443 :       if (8*i+j+1<svd->nsv) { ierr = PetscViewerASCIIPrintf(viewer,", ");CHKERRQ(ierr); }
     254             :     }
     255             :   }
     256         137 :   ierr = PetscViewerASCIIPrintf(viewer,"\n\n");CHKERRQ(ierr);
     257         137 :   PetscFunctionReturn(0);
     258             : }
     259             : 
     260           3 : static PetscErrorCode SVDErrorView_DETAIL(SVD svd,SVDErrorType etype,PetscViewer viewer)
     261             : {
     262           3 :   PetscErrorCode ierr;
     263           3 :   PetscReal      error,sigma;
     264           3 :   PetscInt       i;
     265           3 :   char           ex[30],sep[]=" ---------------------- --------------------\n";
     266             : 
     267           3 :   PetscFunctionBegin;
     268           3 :   if (!svd->nconv) PetscFunctionReturn(0);
     269           3 :   switch (etype) {
     270           0 :     case SVD_ERROR_ABSOLUTE:
     271           0 :       ierr = PetscSNPrintf(ex,sizeof(ex)," absolute error");CHKERRQ(ierr);
     272             :       break;
     273           3 :     case SVD_ERROR_RELATIVE:
     274           3 :       ierr = PetscSNPrintf(ex,sizeof(ex)," relative error");CHKERRQ(ierr);
     275             :       break;
     276             :   }
     277           3 :   ierr = PetscViewerASCIIPrintf(viewer,"%s          sigma           %s\n%s",sep,ex,sep);CHKERRQ(ierr);
     278          20 :   for (i=0;i<svd->nconv;i++) {
     279          17 :     ierr = SVDGetSingularTriplet(svd,i,&sigma,NULL,NULL);CHKERRQ(ierr);
     280          17 :     ierr = SVDComputeError(svd,i,etype,&error);CHKERRQ(ierr);
     281          17 :     ierr = PetscViewerASCIIPrintf(viewer,"       % 6f          %12g\n",(double)sigma,(double)error);CHKERRQ(ierr);
     282             :   }
     283           3 :   ierr = PetscViewerASCIIPrintf(viewer,sep);CHKERRQ(ierr);
     284           3 :   PetscFunctionReturn(0);
     285             : }
     286             : 
     287           2 : static PetscErrorCode SVDErrorView_MATLAB(SVD svd,SVDErrorType etype,PetscViewer viewer)
     288             : {
     289           2 :   PetscErrorCode ierr;
     290           2 :   PetscReal      error;
     291           2 :   PetscInt       i;
     292           2 :   const char     *name;
     293             : 
     294           2 :   PetscFunctionBegin;
     295           2 :   ierr = PetscObjectGetName((PetscObject)svd,&name);CHKERRQ(ierr);
     296           2 :   ierr = PetscViewerASCIIPrintf(viewer,"Error_%s = [\n",name);CHKERRQ(ierr);
     297          14 :   for (i=0;i<svd->nconv;i++) {
     298          12 :     ierr = SVDComputeError(svd,i,etype,&error);CHKERRQ(ierr);
     299          12 :     ierr = PetscViewerASCIIPrintf(viewer,"%18.16e\n",(double)error);CHKERRQ(ierr);
     300             :   }
     301           2 :   ierr = PetscViewerASCIIPrintf(viewer,"];\n");CHKERRQ(ierr);
     302           2 :   PetscFunctionReturn(0);
     303             : }
     304             : 
     305             : /*@C
     306             :    SVDErrorView - Displays the errors associated with the computed solution
     307             :    (as well as the singular values).
     308             : 
     309             :    Collective on svd
     310             : 
     311             :    Input Parameters:
     312             : +  svd    - the singular value solver context
     313             : .  etype  - error type
     314             : -  viewer - optional visualization context
     315             : 
     316             :    Options Database Key:
     317             : +  -svd_error_absolute - print absolute errors of each singular triplet
     318             : -  -svd_error_relative - print relative errors of each singular triplet
     319             : 
     320             :    Notes:
     321             :    By default, this function checks the error of all singular triplets and prints
     322             :    the singular values if all of them are below the requested tolerance.
     323             :    If the viewer has format=PETSC_VIEWER_ASCII_INFO_DETAIL then a table with
     324             :    singular values and corresponding errors is printed.
     325             : 
     326             :    Level: intermediate
     327             : 
     328             : .seealso: SVDSolve(), SVDValuesView(), SVDVectorsView()
     329             : @*/
     330         142 : PetscErrorCode SVDErrorView(SVD svd,SVDErrorType etype,PetscViewer viewer)
     331             : {
     332         142 :   PetscBool         isascii;
     333         142 :   PetscViewerFormat format;
     334         142 :   PetscErrorCode    ierr;
     335             : 
     336         142 :   PetscFunctionBegin;
     337         142 :   PetscValidHeaderSpecific(svd,SVD_CLASSID,1);
     338         142 :   if (!viewer) {
     339         118 :     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)svd),&viewer);CHKERRQ(ierr);
     340             :   }
     341         142 :   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,3);
     342         142 :   PetscCheckSameComm(svd,1,viewer,3);
     343         142 :   SVDCheckSolved(svd,1);
     344         142 :   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&isascii);CHKERRQ(ierr);
     345         142 :   if (!isascii) PetscFunctionReturn(0);
     346             : 
     347         142 :   ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
     348         142 :   switch (format) {
     349         137 :     case PETSC_VIEWER_DEFAULT:
     350             :     case PETSC_VIEWER_ASCII_INFO:
     351         137 :       ierr = SVDErrorView_ASCII(svd,etype,viewer);CHKERRQ(ierr);
     352             :       break;
     353           3 :     case PETSC_VIEWER_ASCII_INFO_DETAIL:
     354           3 :       ierr = SVDErrorView_DETAIL(svd,etype,viewer);CHKERRQ(ierr);
     355             :       break;
     356           2 :     case PETSC_VIEWER_ASCII_MATLAB:
     357           2 :       ierr = SVDErrorView_MATLAB(svd,etype,viewer);CHKERRQ(ierr);
     358             :       break;
     359           0 :     default:
     360           0 :       ierr = PetscInfo1(svd,"Unsupported viewer format %s\n",PetscViewerFormats[format]);CHKERRQ(ierr);
     361             :   }
     362         142 :   PetscFunctionReturn(0);
     363             : }
     364             : 
     365             : /*@
     366             :    SVDErrorViewFromOptions - Processes command line options to determine if/how
     367             :    the errors of the computed solution are to be viewed.
     368             : 
     369             :    Collective on svd
     370             : 
     371             :    Input Parameter:
     372             : .  svd - the singular value solver context
     373             : 
     374             :    Level: developer
     375             : @*/
     376         187 : PetscErrorCode SVDErrorViewFromOptions(SVD svd)
     377             : {
     378         187 :   PetscErrorCode    ierr;
     379         187 :   PetscViewer       viewer;
     380         187 :   PetscBool         flg;
     381         187 :   static PetscBool  incall = PETSC_FALSE;
     382         187 :   PetscViewerFormat format;
     383             : 
     384         187 :   PetscFunctionBegin;
     385         187 :   if (incall) PetscFunctionReturn(0);
     386         187 :   incall = PETSC_TRUE;
     387         187 :   ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject)svd),((PetscObject)svd)->options,((PetscObject)svd)->prefix,"-svd_error_absolute",&viewer,&format,&flg);CHKERRQ(ierr);
     388         187 :   if (flg) {
     389           2 :     ierr = PetscViewerPushFormat(viewer,format);CHKERRQ(ierr);
     390           2 :     ierr = SVDErrorView(svd,SVD_ERROR_ABSOLUTE,viewer);CHKERRQ(ierr);
     391           2 :     ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
     392           2 :     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
     393             :   }
     394         187 :   ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject)svd),((PetscObject)svd)->options,((PetscObject)svd)->prefix,"-svd_error_relative",&viewer,&format,&flg);CHKERRQ(ierr);
     395         187 :   if (flg) {
     396           2 :     ierr = PetscViewerPushFormat(viewer,format);CHKERRQ(ierr);
     397           2 :     ierr = SVDErrorView(svd,SVD_ERROR_RELATIVE,viewer);CHKERRQ(ierr);
     398           2 :     ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
     399           2 :     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
     400             :   }
     401         187 :   incall = PETSC_FALSE;
     402         187 :   PetscFunctionReturn(0);
     403             : }
     404             : 
     405           2 : static PetscErrorCode SVDValuesView_DRAW(SVD svd,PetscViewer viewer)
     406             : {
     407           2 :   PetscErrorCode ierr;
     408           2 :   PetscDraw      draw;
     409           2 :   PetscDrawSP    drawsp;
     410           2 :   PetscReal      re,im=0.0;
     411           2 :   PetscInt       i;
     412             : 
     413           2 :   PetscFunctionBegin;
     414           2 :   if (!svd->nconv) PetscFunctionReturn(0);
     415           2 :   ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
     416           2 :   ierr = PetscDrawSetTitle(draw,"Computed singular values");CHKERRQ(ierr);
     417           2 :   ierr = PetscDrawSPCreate(draw,1,&drawsp);CHKERRQ(ierr);
     418          18 :   for (i=0;i<svd->nconv;i++) {
     419          16 :     re = svd->sigma[svd->perm[i]];
     420          16 :     ierr = PetscDrawSPAddPoint(drawsp,&re,&im);CHKERRQ(ierr);
     421             :   }
     422           2 :   ierr = PetscDrawSPDraw(drawsp,PETSC_TRUE);CHKERRQ(ierr);
     423           2 :   ierr = PetscDrawSPSave(drawsp);CHKERRQ(ierr);
     424           2 :   ierr = PetscDrawSPDestroy(&drawsp);CHKERRQ(ierr);
     425           2 :   PetscFunctionReturn(0);
     426             : }
     427             : 
     428           1 : static PetscErrorCode SVDValuesView_BINARY(SVD svd,PetscViewer viewer)
     429             : {
     430           1 :   PetscInt       i,k;
     431           1 :   PetscReal      *sv;
     432           1 :   PetscErrorCode ierr;
     433             : 
     434           1 :   PetscFunctionBegin;
     435           1 :   ierr = PetscMalloc1(svd->nconv,&sv);CHKERRQ(ierr);
     436           7 :   for (i=0;i<svd->nconv;i++) {
     437           6 :     k = svd->perm[i];
     438           6 :     sv[i] = svd->sigma[k];
     439             :   }
     440           1 :   ierr = PetscViewerBinaryWrite(viewer,sv,svd->nconv,PETSC_REAL);CHKERRQ(ierr);
     441           1 :   ierr = PetscFree(sv);CHKERRQ(ierr);
     442           1 :   PetscFunctionReturn(0);
     443             : }
     444             : 
     445             : #if defined(PETSC_HAVE_HDF5)
     446             : static PetscErrorCode SVDValuesView_HDF5(SVD svd,PetscViewer viewer)
     447             : {
     448             :   PetscErrorCode ierr;
     449             :   PetscInt       i,k,n,N;
     450             :   PetscMPIInt    rank;
     451             :   Vec            v;
     452             :   char           vname[30];
     453             :   const char     *ename;
     454             : 
     455             :   PetscFunctionBegin;
     456             :   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)svd),&rank);CHKERRMPI(ierr);
     457             :   N = svd->nconv;
     458             :   n = rank? 0: N;
     459             :   /* create a vector containing the singular values */
     460             :   ierr = VecCreateMPI(PetscObjectComm((PetscObject)svd),n,N,&v);CHKERRQ(ierr);
     461             :   ierr = PetscObjectGetName((PetscObject)svd,&ename);CHKERRQ(ierr);
     462             :   ierr = PetscSNPrintf(vname,sizeof(vname),"sigma_%s",ename);CHKERRQ(ierr);
     463             :   ierr = PetscObjectSetName((PetscObject)v,vname);CHKERRQ(ierr);
     464             :   if (!rank) {
     465             :     for (i=0;i<svd->nconv;i++) {
     466             :       k = svd->perm[i];
     467             :       ierr = VecSetValue(v,i,svd->sigma[k],INSERT_VALUES);CHKERRQ(ierr);
     468             :     }
     469             :   }
     470             :   ierr = VecAssemblyBegin(v);CHKERRQ(ierr);
     471             :   ierr = VecAssemblyEnd(v);CHKERRQ(ierr);
     472             :   ierr = VecView(v,viewer);CHKERRQ(ierr);
     473             :   ierr = VecDestroy(&v);CHKERRQ(ierr);
     474             :   PetscFunctionReturn(0);
     475             : }
     476             : #endif
     477             : 
     478           2 : static PetscErrorCode SVDValuesView_ASCII(SVD svd,PetscViewer viewer)
     479             : {
     480           2 :   PetscInt       i;
     481           2 :   PetscErrorCode ierr;
     482             : 
     483           2 :   PetscFunctionBegin;
     484           2 :   ierr = PetscViewerASCIIPrintf(viewer,"Singular values = \n");CHKERRQ(ierr);
     485          14 :   for (i=0;i<svd->nconv;i++) {
     486          12 :     ierr = PetscViewerASCIIPrintf(viewer,"   %.5f\n",(double)svd->sigma[svd->perm[i]]);CHKERRQ(ierr);
     487             :   }
     488           2 :   ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
     489           2 :   PetscFunctionReturn(0);
     490             : }
     491             : 
     492           1 : static PetscErrorCode SVDValuesView_MATLAB(SVD svd,PetscViewer viewer)
     493             : {
     494           1 :   PetscErrorCode ierr;
     495           1 :   PetscInt       i;
     496           1 :   const char     *name;
     497             : 
     498           1 :   PetscFunctionBegin;
     499           1 :   ierr = PetscObjectGetName((PetscObject)svd,&name);CHKERRQ(ierr);
     500           1 :   ierr = PetscViewerASCIIPrintf(viewer,"Sigma_%s = [\n",name);CHKERRQ(ierr);
     501           4 :   for (i=0;i<svd->nconv;i++) {
     502           3 :     ierr = PetscViewerASCIIPrintf(viewer,"%18.16e\n",(double)svd->sigma[svd->perm[i]]);CHKERRQ(ierr);
     503             :   }
     504           1 :   ierr = PetscViewerASCIIPrintf(viewer,"];\n");CHKERRQ(ierr);
     505           1 :   PetscFunctionReturn(0);
     506             : }
     507             : 
     508             : /*@C
     509             :    SVDValuesView - Displays the computed singular values in a viewer.
     510             : 
     511             :    Collective on svd
     512             : 
     513             :    Input Parameters:
     514             : +  svd    - the singular value solver context
     515             : -  viewer - the viewer
     516             : 
     517             :    Options Database Key:
     518             : .  -svd_view_values - print computed singular values
     519             : 
     520             :    Level: intermediate
     521             : 
     522             : .seealso: SVDSolve(), SVDVectorsView(), SVDErrorView()
     523             : @*/
     524           6 : PetscErrorCode SVDValuesView(SVD svd,PetscViewer viewer)
     525             : {
     526           6 :   PetscBool         isascii,isdraw,isbinary;
     527           6 :   PetscViewerFormat format;
     528           6 :   PetscErrorCode    ierr;
     529             : #if defined(PETSC_HAVE_HDF5)
     530             :   PetscBool         ishdf5;
     531             : #endif
     532             : 
     533           6 :   PetscFunctionBegin;
     534           6 :   PetscValidHeaderSpecific(svd,SVD_CLASSID,1);
     535           6 :   if (!viewer) {
     536           0 :     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)svd),&viewer);CHKERRQ(ierr);
     537             :   }
     538           6 :   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
     539           6 :   PetscCheckSameComm(svd,1,viewer,2);
     540           6 :   SVDCheckSolved(svd,1);
     541           6 :   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
     542           6 :   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
     543             : #if defined(PETSC_HAVE_HDF5)
     544             :   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERHDF5,&ishdf5);CHKERRQ(ierr);
     545             : #endif
     546           6 :   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&isascii);CHKERRQ(ierr);
     547           6 :   if (isdraw) {
     548           2 :     ierr = SVDValuesView_DRAW(svd,viewer);CHKERRQ(ierr);
     549           4 :   } else if (isbinary) {
     550           1 :     ierr = SVDValuesView_BINARY(svd,viewer);CHKERRQ(ierr);
     551             : #if defined(PETSC_HAVE_HDF5)
     552             :   } else if (ishdf5) {
     553             :     ierr = SVDValuesView_HDF5(svd,viewer);CHKERRQ(ierr);
     554             : #endif
     555           3 :   } else if (isascii) {
     556           3 :     ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
     557           3 :     switch (format) {
     558           2 :       case PETSC_VIEWER_DEFAULT:
     559             :       case PETSC_VIEWER_ASCII_INFO:
     560             :       case PETSC_VIEWER_ASCII_INFO_DETAIL:
     561           2 :         ierr = SVDValuesView_ASCII(svd,viewer);CHKERRQ(ierr);
     562             :         break;
     563           1 :       case PETSC_VIEWER_ASCII_MATLAB:
     564           1 :         ierr = SVDValuesView_MATLAB(svd,viewer);CHKERRQ(ierr);
     565             :         break;
     566           0 :       default:
     567           0 :         ierr = PetscInfo1(svd,"Unsupported viewer format %s\n",PetscViewerFormats[format]);CHKERRQ(ierr);
     568             :     }
     569             :   }
     570           6 :   PetscFunctionReturn(0);
     571             : }
     572             : 
     573             : /*@
     574             :    SVDValuesViewFromOptions - Processes command line options to determine if/how
     575             :    the computed singular values are to be viewed.
     576             : 
     577             :    Collective on svd
     578             : 
     579             :    Input Parameter:
     580             : .  svd - the singular value solver context
     581             : 
     582             :    Level: developer
     583             : @*/
     584         187 : PetscErrorCode SVDValuesViewFromOptions(SVD svd)
     585             : {
     586         187 :   PetscErrorCode    ierr;
     587         187 :   PetscViewer       viewer;
     588         187 :   PetscBool         flg;
     589         187 :   static PetscBool  incall = PETSC_FALSE;
     590         187 :   PetscViewerFormat format;
     591             : 
     592         187 :   PetscFunctionBegin;
     593         187 :   if (incall) PetscFunctionReturn(0);
     594         187 :   incall = PETSC_TRUE;
     595         187 :   ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject)svd),((PetscObject)svd)->options,((PetscObject)svd)->prefix,"-svd_view_values",&viewer,&format,&flg);CHKERRQ(ierr);
     596         187 :   if (flg) {
     597           6 :     ierr = PetscViewerPushFormat(viewer,format);CHKERRQ(ierr);
     598           6 :     ierr = SVDValuesView(svd,viewer);CHKERRQ(ierr);
     599           6 :     ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
     600           6 :     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
     601             :   }
     602         187 :   incall = PETSC_FALSE;
     603         187 :   PetscFunctionReturn(0);
     604             : }
     605             : 
     606             : /*@C
     607             :    SVDVectorsView - Outputs computed singular vectors to a viewer.
     608             : 
     609             :    Collective on svd
     610             : 
     611             :    Input Parameters:
     612             : +  svd    - the singular value solver context
     613             : -  viewer - the viewer
     614             : 
     615             :    Options Database Keys:
     616             : .  -svd_view_vectors - output singular vectors
     617             : 
     618             :    Note:
     619             :    Right and left singular vectors are interleaved, that is, the vectors are
     620             :    output in the following order: V0, U0, V1, U1, V2, U2, ...
     621             : 
     622             :    Level: intermediate
     623             : 
     624             : .seealso: SVDSolve(), SVDValuesView(), SVDErrorView()
     625             : @*/
     626           1 : PetscErrorCode SVDVectorsView(SVD svd,PetscViewer viewer)
     627             : {
     628           1 :   PetscErrorCode ierr;
     629           1 :   PetscInt       i,k;
     630           1 :   Vec            x;
     631           1 :   char           vname[30];
     632           1 :   const char     *ename;
     633             : 
     634           1 :   PetscFunctionBegin;
     635           1 :   PetscValidHeaderSpecific(svd,SVD_CLASSID,1);
     636           1 :   if (!viewer) {
     637           0 :     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)svd),&viewer);CHKERRQ(ierr);
     638             :   }
     639           1 :   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
     640           1 :   PetscCheckSameComm(svd,1,viewer,2);
     641           1 :   SVDCheckSolved(svd,1);
     642           1 :   if (svd->nconv) {
     643           1 :     ierr = PetscObjectGetName((PetscObject)svd,&ename);CHKERRQ(ierr);
     644           1 :     ierr = SVDComputeVectors(svd);CHKERRQ(ierr);
     645           4 :     for (i=0;i<svd->nconv;i++) {
     646           3 :       k = svd->perm[i];
     647           3 :       ierr = PetscSNPrintf(vname,sizeof(vname),"V%d_%s",(int)i,ename);CHKERRQ(ierr);
     648           3 :       ierr = BVGetColumn(svd->V,k,&x);CHKERRQ(ierr);
     649           3 :       ierr = PetscObjectSetName((PetscObject)x,vname);CHKERRQ(ierr);
     650           3 :       ierr = VecView(x,viewer);CHKERRQ(ierr);
     651           3 :       ierr = BVRestoreColumn(svd->V,k,&x);CHKERRQ(ierr);
     652           3 :       ierr = PetscSNPrintf(vname,sizeof(vname),"U%d_%s",(int)i,ename);CHKERRQ(ierr);
     653           3 :       ierr = BVGetColumn(svd->U,k,&x);CHKERRQ(ierr);
     654           3 :       ierr = PetscObjectSetName((PetscObject)x,vname);CHKERRQ(ierr);
     655           3 :       ierr = VecView(x,viewer);CHKERRQ(ierr);
     656           3 :       ierr = BVRestoreColumn(svd->U,k,&x);CHKERRQ(ierr);
     657             :     }
     658             :   }
     659           1 :   PetscFunctionReturn(0);
     660             : }
     661             : 
     662             : /*@
     663             :    SVDVectorsViewFromOptions - Processes command line options to determine if/how
     664             :    the computed singular vectors are to be viewed.
     665             : 
     666             :    Collective on svd
     667             : 
     668             :    Input Parameter:
     669             : .  svd - the singular value solver context
     670             : 
     671             :    Level: developer
     672             : @*/
     673         187 : PetscErrorCode SVDVectorsViewFromOptions(SVD svd)
     674             : {
     675         187 :   PetscErrorCode    ierr;
     676         187 :   PetscViewer       viewer;
     677         187 :   PetscBool         flg = PETSC_FALSE;
     678         187 :   static PetscBool  incall = PETSC_FALSE;
     679         187 :   PetscViewerFormat format;
     680             : 
     681         187 :   PetscFunctionBegin;
     682         187 :   if (incall) PetscFunctionReturn(0);
     683         187 :   incall = PETSC_TRUE;
     684         187 :   ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject)svd),((PetscObject)svd)->options,((PetscObject)svd)->prefix,"-svd_view_vectors",&viewer,&format,&flg);CHKERRQ(ierr);
     685         187 :   if (flg) {
     686           1 :     ierr = PetscViewerPushFormat(viewer,format);CHKERRQ(ierr);
     687           1 :     ierr = SVDVectorsView(svd,viewer);CHKERRQ(ierr);
     688           1 :     ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
     689           1 :     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
     690             :   }
     691         187 :   incall = PETSC_FALSE;
     692         187 :   PetscFunctionReturn(0);
     693             : }
     694             : 

Generated by: LCOV version 1.14