Date: 2022-03-31T04:48:06+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:   KSPMonitor_SAWs *mon;

     30:   PetscNewLog(ksp,&mon);
     31:   mon->viewer = PETSC_VIEWER_SAWS_(PetscObjectComm((PetscObject)ksp));
     33:   *ctx = (void*)mon;
     34:   return 0;
     35: }

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

     40:    Collective

     42:    Input Parameter:
     43: .  ctx - monitor context

     45:    Level: developer

     47: .seealso: KSPMonitorSAWsCreate()
     48: @*/
     49: PetscErrorCode KSPMonitorSAWsDestroy(void **ctx)
     50: {
     51:   KSPMonitor_SAWs *mon = (KSPMonitor_SAWs*)*ctx;

     53:   PetscFree2(mon->eigr,mon->eigi);
     54:   PetscFree(*ctx);
     55:   return 0;
     56: }

     58: /*@C
     59:    KSPMonitorSAWs - monitor solution using SAWs

     61:    Logically Collective on ksp

     63:    Input Parameters:
     64: +  ksp   - iterative context
     65: .  n     - iteration number
     66: .  rnorm - 2-norm (preconditioned) residual value (may be estimated).
     67: -  ctx -  PetscViewer of type SAWs

     69:    Level: advanced

     71: .seealso: KSPMonitorSingularValue(), KSPComputeExtremeSingularValues(), PetscViewerSAWsOpen()
     72: @*/
     73: PetscErrorCode KSPMonitorSAWs(KSP ksp,PetscInt n,PetscReal rnorm,void *ctx)
     74: {
     75:   KSPMonitor_SAWs *mon   = (KSPMonitor_SAWs*)ctx;
     76:   PetscReal       emax,emin;
     77:   PetscMPIInt     rank;

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

     82:   PetscFree2(mon->eigr,mon->eigi);
     83:   PetscMalloc2(n,&mon->eigr,n,&mon->eigi);
     84:   if (n) {
     85:     KSPComputeEigenvalues(ksp,n,mon->eigr,mon->eigi,&mon->neigs);

     87:     MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
     88:     if (rank == 0) {
     89:       SAWs_Delete("/PETSc/ksp_monitor_saws/eigr");
     90:       SAWs_Delete("/PETSc/ksp_monitor_saws/eigi");

     92:       PetscStackCallSAWs(SAWs_Register,("/PETSc/ksp_monitor_saws/rnorm",&ksp->rnorm,1,SAWs_READ,SAWs_DOUBLE));
     93:       PetscStackCallSAWs(SAWs_Register,("/PETSc/ksp_monitor_saws/neigs",&mon->neigs,1,SAWs_READ,SAWs_INT));
     94:       if (mon->neigs > 0) {
     95:         PetscStackCallSAWs(SAWs_Register,("/PETSc/ksp_monitor_saws/eigr",mon->eigr,mon->neigs,SAWs_READ,SAWs_DOUBLE));
     96:         PetscStackCallSAWs(SAWs_Register,("/PETSc/ksp_monitor_saws/eigi",mon->eigi,mon->neigs,SAWs_READ,SAWs_DOUBLE));
     97:       }
     98:       PetscInfo(ksp,"KSP extreme singular values min=%g max=%g\n",(double)emin,(double)emax);
     99:       PetscSAWsBlock();
    100:     }
    101:   }
    102:   return 0;
    103: }