Date: 2022-03-04T17:39:00+00:00

Actual source code: kspsaws.c

      1: #include <petsc/private/kspimpl.h>
      2: #include <petscviewersaws.h>

      4: typedef struct {
      5:   PetscViewer    viewer;
      6:   PetscInt       neigs;
      7:   PetscReal      *eigi;
      8:   PetscReal      *eigr;
      9: } KSPMonitor_SAWs;

     11: /*@C
     12:    KSPMonitorSAWsCreate - create an SAWs monitor context

     14:    Collective

     16:    Input Parameter:
     17: .  ksp - KSP to monitor

     19:    Output Parameter:
     20: .  ctx - context for monitor

     22:    Level: developer

     24: .seealso: KSPMonitorSAWs(), KSPMonitorSAWsDestroy()
     25: @*/
     26: PetscErrorCode KSPMonitorSAWsCreate(KSP ksp,void **ctx)
     27: {
     28:   PetscErrorCode  ierr;
     29:   KSPMonitor_SAWs *mon;

     32:   PetscNewLog(ksp,&mon);
     33:   mon->viewer = PETSC_VIEWER_SAWS_(PetscObjectComm((PetscObject)ksp));
     34:   if (!mon->viewer) SETERRQ(PetscObjectComm((PetscObject)ksp),PETSC_ERR_PLIB,"Cannot create SAWs default viewer");
     35:   *ctx = (void*)mon;
     36:   return(0);
     37: }

     39: /*@C
     40:    KSPMonitorSAWsDestroy - destroy a monitor context created with KSPMonitorSAWsCreate()

     42:    Collective

     44:    Input Parameter:
     45: .  ctx - monitor context

     47:    Level: developer

     49: .seealso: KSPMonitorSAWsCreate()
     50: @*/
     51: PetscErrorCode KSPMonitorSAWsDestroy(void **ctx)
     52: {
     53:   KSPMonitor_SAWs *mon = (KSPMonitor_SAWs*)*ctx;
     54:   PetscErrorCode  ierr;

     57:   PetscFree2(mon->eigr,mon->eigi);
     58:   PetscFree(*ctx);
     59:   return(0);
     60: }

     62: /*@C
     63:    KSPMonitorSAWs - monitor solution using SAWs

     65:    Logically Collective on ksp

     67:    Input Parameters:
     68: +  ksp   - iterative context
     69: .  n     - iteration number
     70: .  rnorm - 2-norm (preconditioned) residual value (may be estimated).
     71: -  ctx -  PetscViewer of type SAWs

     73:    Level: advanced

     75: .seealso: KSPMonitorSingularValue(), KSPComputeExtremeSingularValues(), PetscViewerSAWsOpen()
     76: @*/
     77: PetscErrorCode KSPMonitorSAWs(KSP ksp,PetscInt n,PetscReal rnorm,void *ctx)
     78: {
     79:   PetscErrorCode  ierr;
     80:   KSPMonitor_SAWs *mon   = (KSPMonitor_SAWs*)ctx;
     81:   PetscReal       emax,emin;
     82:   PetscMPIInt     rank;

     86:   KSPComputeExtremeSingularValues(ksp,&emax,&emin);

     88:   PetscFree2(mon->eigr,mon->eigi);
     89:   PetscMalloc2(n,&mon->eigr,n,&mon->eigi);
     90:   if (n) {
     91:     KSPComputeEigenvalues(ksp,n,mon->eigr,mon->eigi,&mon->neigs);

     93:     MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
     94:     if (rank == 0) {
     95:       SAWs_Delete("/PETSc/ksp_monitor_saws/eigr");
     96:       SAWs_Delete("/PETSc/ksp_monitor_saws/eigi");

     98:       PetscStackCallSAWs(SAWs_Register,("/PETSc/ksp_monitor_saws/rnorm",&ksp->rnorm,1,SAWs_READ,SAWs_DOUBLE));
     99:       PetscStackCallSAWs(SAWs_Register,("/PETSc/ksp_monitor_saws/neigs",&mon->neigs,1,SAWs_READ,SAWs_INT));
    100:       if (mon->neigs > 0) {
    101:         PetscStackCallSAWs(SAWs_Register,("/PETSc/ksp_monitor_saws/eigr",mon->eigr,mon->neigs,SAWs_READ,SAWs_DOUBLE));
    102:         PetscStackCallSAWs(SAWs_Register,("/PETSc/ksp_monitor_saws/eigi",mon->eigi,mon->neigs,SAWs_READ,SAWs_DOUBLE));
    103:       }
    104:       PetscInfo2(ksp,"KSP extreme singular values min=%g max=%g\n",(double)emin,(double)emax);
    105:       PetscSAWsBlock();
    106:     }
    107:   }
    108:   return(0);
    109: }