xref: /petsc/src/ts/interface/tseig.c (revision 3d39420eb3b347d70dc42661d49c4af6da9186f5)
14036925cSBarry Smith 
24036925cSBarry Smith #include <petsc-private/tsimpl.h>        /*I "petscts.h"  I*/
34036925cSBarry Smith 
44036925cSBarry Smith /* ------------------------------------------------------------------------*/
54036925cSBarry Smith struct _n_TSMonitorSPEigCtx {
64036925cSBarry Smith   PetscDrawSP drawsp;
74036925cSBarry Smith   KSP         ksp;
84036925cSBarry Smith   PetscRandom rand;
94036925cSBarry Smith   PetscInt    howoften;  /* when > 0 uses step % howoften, when negative only final solution plotted */
104036925cSBarry Smith };
114036925cSBarry Smith 
124036925cSBarry Smith 
134036925cSBarry Smith #undef __FUNCT__
144036925cSBarry Smith #define __FUNCT__ "TSMonitorSPEigCtxCreate"
154036925cSBarry Smith /*@C
164036925cSBarry Smith    TSMonitorSPEigCtxCreate - Creates a context for use with TS to monitor the eigenvalues of the linearized operator
174036925cSBarry Smith 
184036925cSBarry Smith    Collective on TS
194036925cSBarry Smith 
204036925cSBarry Smith    Input Parameters:
214036925cSBarry Smith +  host - the X display to open, or null for the local machine
224036925cSBarry Smith .  label - the title to put in the title bar
234036925cSBarry Smith .  x, y - the screen coordinates of the upper left coordinate of the window
244036925cSBarry Smith .  m, n - the screen width and height in pixels
254036925cSBarry Smith -  howoften - if positive then determines the frequency of the plotting, if -1 then only at the final time
264036925cSBarry Smith 
274036925cSBarry Smith    Output Parameter:
284036925cSBarry Smith .  ctx - the context
294036925cSBarry Smith 
304036925cSBarry Smith    Options Database Key:
314036925cSBarry Smith .  -ts_monitor_eig - plot egienvalues of linearized right hand side
324036925cSBarry Smith 
334036925cSBarry Smith    Notes:
344036925cSBarry Smith    Use TSMonitorSPEigCtxDestroy() to destroy.
354036925cSBarry Smith 
364036925cSBarry Smith    Level: intermediate
374036925cSBarry Smith 
384036925cSBarry Smith .keywords: TS, monitor, line graph, residual, seealso
394036925cSBarry Smith 
404036925cSBarry Smith .seealso: TSMonitorSPEigTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError()
414036925cSBarry Smith 
424036925cSBarry Smith @*/
434036925cSBarry Smith PetscErrorCode  TSMonitorSPEigCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorSPEigCtx *ctx)
444036925cSBarry Smith {
454036925cSBarry Smith   PetscDraw      win;
464036925cSBarry Smith   PetscErrorCode ierr;
474036925cSBarry Smith   PC             pc;
484036925cSBarry Smith 
494036925cSBarry Smith   PetscFunctionBegin;
504036925cSBarry Smith   ierr = PetscNew(struct _n_TSMonitorSPEigCtx,ctx);CHKERRQ(ierr);
514036925cSBarry Smith   ierr = PetscDrawCreate(comm,host,label,x,y,m,n,&win);CHKERRQ(ierr);
524036925cSBarry Smith   ierr = PetscDrawSetFromOptions(win);CHKERRQ(ierr);
534036925cSBarry Smith   ierr = PetscDrawSPCreate(win,1,&(*ctx)->drawsp);CHKERRQ(ierr);
544036925cSBarry Smith   ierr = PetscRandomCreate(comm,&(*ctx)->rand);CHKERRQ(ierr);
554036925cSBarry Smith   ierr = PetscRandomSetFromOptions((*ctx)->rand);CHKERRQ(ierr);
564036925cSBarry Smith   ierr = KSPCreate(comm,&(*ctx)->ksp);CHKERRQ(ierr);
574036925cSBarry Smith   ierr = KSPSetOptionsPrefix((*ctx)->ksp,"ts_monitor_eig_");CHKERRQ(ierr); /* this is wrong, used use also prefix from the TS */
584036925cSBarry Smith   ierr = KSPSetType((*ctx)->ksp,KSPGMRES);CHKERRQ(ierr);
594036925cSBarry Smith   ierr = KSPGMRESSetRestart((*ctx)->ksp,200);CHKERRQ(ierr);
604036925cSBarry Smith   ierr = KSPSetTolerances((*ctx)->ksp,1.e-10,PETSC_DEFAULT,PETSC_DEFAULT,200);CHKERRQ(ierr);
614036925cSBarry Smith   ierr = KSPSetComputeSingularValues((*ctx)->ksp,PETSC_TRUE);CHKERRQ(ierr);
624036925cSBarry Smith   ierr = KSPSetFromOptions((*ctx)->ksp);CHKERRQ(ierr);
634036925cSBarry Smith   ierr = KSPGetPC((*ctx)->ksp,&pc);CHKERRQ(ierr);
644036925cSBarry Smith   ierr = PCSetType(pc,PCNONE);CHKERRQ(ierr);
654036925cSBarry Smith   (*ctx)->howoften = howoften;
664036925cSBarry Smith   PetscFunctionReturn(0);
674036925cSBarry Smith }
684036925cSBarry Smith 
694036925cSBarry Smith #undef __FUNCT__
704036925cSBarry Smith #define __FUNCT__ "TSMonitorSPEig"
714036925cSBarry Smith PetscErrorCode TSMonitorSPEig(TS ts,PetscInt step,PetscReal ptime,Vec v,void *monctx)
724036925cSBarry Smith {
734036925cSBarry Smith   TSMonitorSPEigCtx ctx = (TSMonitorSPEigCtx) monctx;
744036925cSBarry Smith   PetscErrorCode    ierr;
754036925cSBarry Smith   KSP               ksp = ctx->ksp;
764036925cSBarry Smith   PetscInt          n,nits,neig,i;
774036925cSBarry Smith   PetscReal         *r,*c;
784036925cSBarry Smith   PetscDrawSP       drawsp = ctx->drawsp;
794036925cSBarry Smith   MatStructure      structure;
804036925cSBarry Smith   Mat               A,B;
814036925cSBarry Smith   Vec               xdot;
824036925cSBarry Smith   SNES              snes;
834036925cSBarry Smith 
844036925cSBarry Smith   PetscFunctionBegin;
854036925cSBarry Smith   if (!step) PetscFunctionReturn(0);
864036925cSBarry Smith   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && (step == -1))){
874036925cSBarry Smith     ierr = VecDuplicate(v,&xdot);CHKERRQ(ierr);
884036925cSBarry Smith     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
894036925cSBarry Smith     ierr = SNESGetJacobian(snes,&A,&B,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
90*3d39420eSBarry Smith     ierr = SNESComputeJacobian(snes,v,&A,&B,&structure);CHKERRQ(ierr);
91*3d39420eSBarry Smith     /*    ierr = TSComputeIJacobian(ts,ptime,v,xdot,1.0,&A,&B,&structure,PETSC_FALSE);CHKERRQ(ierr);   */
92*3d39420eSBarry Smith     ierr = MatScale(A,-1.0);CHKERRQ(ierr);
93*3d39420eSBarry Smith     if (A != B) {
94*3d39420eSBarry Smith       ierr = MatScale(B,-1.0);CHKERRQ(ierr);
95*3d39420eSBarry Smith     }
964036925cSBarry Smith     ierr = KSPSetOperators(ksp,A,B,structure);CHKERRQ(ierr);
974036925cSBarry Smith     ierr = VecSetRandom(xdot,ctx->rand);CHKERRQ(ierr);
984036925cSBarry Smith     ierr = KSPSolve(ksp,xdot,xdot);CHKERRQ(ierr);
994036925cSBarry Smith     ierr = VecDestroy(&xdot);CHKERRQ(ierr);
1004036925cSBarry Smith     ierr = KSPGetIterationNumber(ksp,&nits);CHKERRQ(ierr);
1014036925cSBarry Smith     n = nits+2;
1024036925cSBarry Smith 
1034036925cSBarry Smith     if (nits) {
1044036925cSBarry Smith       ierr = PetscMalloc2(n,PetscReal,&r,n,PetscReal,&c);CHKERRQ(ierr);
1054036925cSBarry Smith       ierr = KSPComputeEigenvalues(ksp,n,r,c,&neig);CHKERRQ(ierr);
1064036925cSBarry Smith       for (i=0; i<neig; i++) {
1074036925cSBarry Smith         ierr = PetscDrawSPAddPoint(drawsp,r+i,c+i);CHKERRQ(ierr);
1084036925cSBarry Smith       }
1094036925cSBarry Smith       ierr = PetscDrawSPDraw(drawsp);CHKERRQ(ierr);
1104036925cSBarry Smith       ierr = PetscFree2(r,c);CHKERRQ(ierr);
1114036925cSBarry Smith     }
1124036925cSBarry Smith   }
1134036925cSBarry Smith   PetscFunctionReturn(0);
1144036925cSBarry Smith }
1154036925cSBarry Smith 
1164036925cSBarry Smith #undef __FUNCT__
1174036925cSBarry Smith #define __FUNCT__ "TSMonitorSPEigCtxDestroy"
1184036925cSBarry Smith /*@C
1194036925cSBarry Smith    TSMonitorSPEigCtxDestroy - Destroys a scatter plot context that was created with TSMonitorSPEigCtxCreate().
1204036925cSBarry Smith 
1214036925cSBarry Smith    Collective on TSMonitorSPEigCtx
1224036925cSBarry Smith 
1234036925cSBarry Smith    Input Parameter:
1244036925cSBarry Smith .  ctx - the monitor context
1254036925cSBarry Smith 
1264036925cSBarry Smith    Level: intermediate
1274036925cSBarry Smith 
1284036925cSBarry Smith .keywords: TS, monitor, line graph, destroy
1294036925cSBarry Smith 
1304036925cSBarry Smith .seealso: TSMonitorSPEigCtxCreate(),  TSMonitorSet(), TSMonitorSPEig();
1314036925cSBarry Smith @*/
1324036925cSBarry Smith PetscErrorCode  TSMonitorSPEigCtxDestroy(TSMonitorSPEigCtx *ctx)
1334036925cSBarry Smith {
1344036925cSBarry Smith   PetscDraw      draw;
1354036925cSBarry Smith   PetscErrorCode ierr;
1364036925cSBarry Smith 
1374036925cSBarry Smith   PetscFunctionBegin;
1384036925cSBarry Smith   ierr = PetscDrawSPGetDraw((*ctx)->drawsp,&draw);CHKERRQ(ierr);
1394036925cSBarry Smith   ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr);
1404036925cSBarry Smith   ierr = PetscDrawSPDestroy(&(*ctx)->drawsp);CHKERRQ(ierr);
1414036925cSBarry Smith   ierr = KSPDestroy(&(*ctx)->ksp);CHKERRQ(ierr);
1424036925cSBarry Smith   ierr = PetscRandomDestroy(&(*ctx)->rand);CHKERRQ(ierr);
1434036925cSBarry Smith   ierr = PetscFree(*ctx);CHKERRQ(ierr);
1444036925cSBarry Smith   PetscFunctionReturn(0);
1454036925cSBarry Smith }
1464036925cSBarry Smith 
147