void PETSC_STDCALL tsgetsnesiterations_(TS ts,PetscInt *nits, int *__ierr ){ *__ierr = TSGetSNESIterations( (TS)PetscToPointer((ts) ),nits); }
int main(int argc,char **argv) { PetscFunctionList plist = NULL; char pname[256]; TS ts; /* nonlinear solver */ Vec x,r; /* solution, residual vectors */ Mat A; /* Jacobian matrix */ Problem problem; PetscBool use_monitor; PetscInt steps,maxsteps = 1000,nonlinits,linits,snesfails,rejects; PetscReal ftime; MonitorCtx mon; PetscErrorCode ierr; PetscMPIInt size; /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Initialize program - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ PetscInitialize(&argc,&argv,(char*)0,help); ierr = MPI_Comm_size(PETSC_COMM_WORLD,&size);CHKERRQ(ierr); if (size > 1) SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_SUP,"Only for sequential runs"); /* Register the available problems */ ierr = PetscFunctionListAdd(&plist,"rober",&RoberCreate);CHKERRQ(ierr); ierr = PetscFunctionListAdd(&plist,"ce",&CECreate);CHKERRQ(ierr); ierr = PetscFunctionListAdd(&plist,"orego",&OregoCreate);CHKERRQ(ierr); ierr = PetscStrcpy(pname,"ce");CHKERRQ(ierr); /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Set runtime options - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ ierr = PetscOptionsBegin(PETSC_COMM_WORLD,NULL,"Timestepping benchmark options","");CHKERRQ(ierr); { ierr = PetscOptionsFList("-problem_type","Name of problem to run","",plist,pname,pname,sizeof(pname),NULL);CHKERRQ(ierr); use_monitor = PETSC_FALSE; ierr = PetscOptionsBool("-monitor_error","Display errors relative to exact solutions","",use_monitor,&use_monitor,NULL);CHKERRQ(ierr); } ierr = PetscOptionsEnd();CHKERRQ(ierr); /* Create the new problem */ ierr = PetscNew(&problem);CHKERRQ(ierr); problem->comm = MPI_COMM_WORLD; { PetscErrorCode (*pcreate)(Problem); ierr = PetscFunctionListFind(plist,pname,&pcreate);CHKERRQ(ierr); if (!pcreate) SETERRQ1(PETSC_COMM_SELF,1,"No problem '%s'",pname); ierr = (*pcreate)(problem);CHKERRQ(ierr); } /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Create necessary matrix and vectors - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ ierr = MatCreate(PETSC_COMM_WORLD,&A);CHKERRQ(ierr); ierr = MatSetSizes(A,problem->n,problem->n,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr); ierr = MatSetFromOptions(A);CHKERRQ(ierr); ierr = MatSetUp(A);CHKERRQ(ierr); ierr = MatGetVecs(A,&x,NULL);CHKERRQ(ierr); ierr = VecDuplicate(x,&r);CHKERRQ(ierr); mon.comm = PETSC_COMM_WORLD; mon.problem = problem; ierr = VecDuplicate(x,&mon.x);CHKERRQ(ierr); /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Create timestepping solver context - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ ierr = TSCreate(PETSC_COMM_WORLD,&ts);CHKERRQ(ierr); ierr = TSSetProblemType(ts,TS_NONLINEAR);CHKERRQ(ierr); ierr = TSSetType(ts,TSROSW);CHKERRQ(ierr); /* Rosenbrock-W */ ierr = TSSetIFunction(ts,NULL,problem->function,problem->data);CHKERRQ(ierr); ierr = TSSetIJacobian(ts,A,A,problem->jacobian,problem->data);CHKERRQ(ierr); ierr = TSSetDuration(ts,maxsteps,problem->final_time);CHKERRQ(ierr); ierr = TSSetMaxStepRejections(ts,10);CHKERRQ(ierr); ierr = TSSetMaxSNESFailures(ts,-1);CHKERRQ(ierr); /* unlimited */ if (use_monitor) { ierr = TSMonitorSet(ts,&MonitorError,&mon,NULL);CHKERRQ(ierr); } /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Set initial conditions - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ ierr = (*problem->solution)(0,x,problem->data);CHKERRQ(ierr); ierr = TSSetInitialTimeStep(ts,0.0,.001);CHKERRQ(ierr); ierr = TSSetSolution(ts,x);CHKERRQ(ierr); /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Set runtime options - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ ierr = TSSetFromOptions(ts);CHKERRQ(ierr); /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Solve nonlinear system - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ ierr = TSSolve(ts,x);CHKERRQ(ierr); ierr = TSGetSolveTime(ts,&ftime);CHKERRQ(ierr); ierr = TSGetTimeStepNumber(ts,&steps);CHKERRQ(ierr); ierr = TSGetSNESFailures(ts,&snesfails);CHKERRQ(ierr); ierr = TSGetStepRejections(ts,&rejects);CHKERRQ(ierr); ierr = TSGetSNESIterations(ts,&nonlinits);CHKERRQ(ierr); ierr = TSGetKSPIterations(ts,&linits);CHKERRQ(ierr); ierr = PetscPrintf(PETSC_COMM_WORLD,"steps %D (%D rejected, %D SNES fails), ftime %G, nonlinits %D, linits %D\n",steps,rejects,snesfails,ftime,nonlinits,linits);CHKERRQ(ierr); if (problem->hasexact) { ierr = MonitorError(ts,steps,ftime,x,&mon);CHKERRQ(ierr); } /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Free work space. All PETSc objects should be destroyed when they are no longer needed. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ ierr = MatDestroy(&A);CHKERRQ(ierr); ierr = VecDestroy(&x);CHKERRQ(ierr); ierr = VecDestroy(&r);CHKERRQ(ierr); ierr = VecDestroy(&mon.x);CHKERRQ(ierr); ierr = TSDestroy(&ts);CHKERRQ(ierr); if (problem->destroy) { ierr = (*problem->destroy)(problem);CHKERRQ(ierr); } ierr = PetscFree(problem);CHKERRQ(ierr); ierr = PetscFunctionListDestroy(&plist);CHKERRQ(ierr); ierr = PetscFinalize(); PetscFunctionReturn(0); }
int main(int argc,char **argv) { TS ts; /* time integration context */ Vec X; /* solution, residual vectors */ Mat J; /* Jacobian matrix */ PetscErrorCode ierr; PetscScalar *x; PetscReal ftime; PetscInt i,steps,nits,lits; PetscBool view_final; Ctx ctx; PetscInitialize(&argc,&argv,(char*)0,help); ctx.n = 3; ierr = PetscOptionsGetInt(NULL,NULL,"-n",&ctx.n,NULL);CHKERRQ(ierr); if (ctx.n < 2) SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_ARG_OUTOFRANGE,"The dimension specified with -n must be at least 2"); view_final = PETSC_FALSE; ierr = PetscOptionsGetBool(NULL,NULL,"-view_final",&view_final,NULL);CHKERRQ(ierr); ctx.monitor_short = PETSC_FALSE; ierr = PetscOptionsGetBool(NULL,NULL,"-monitor_short",&ctx.monitor_short,NULL);CHKERRQ(ierr); /* Create Jacobian matrix data structure and state vector */ ierr = MatCreate(PETSC_COMM_WORLD,&J);CHKERRQ(ierr); ierr = MatSetSizes(J,PETSC_DECIDE,PETSC_DECIDE,ctx.n,ctx.n);CHKERRQ(ierr); ierr = MatSetFromOptions(J);CHKERRQ(ierr); ierr = MatSetUp(J);CHKERRQ(ierr); ierr = MatCreateVecs(J,&X,NULL);CHKERRQ(ierr); /* Create time integration context */ ierr = TSCreate(PETSC_COMM_WORLD,&ts);CHKERRQ(ierr); ierr = TSSetType(ts,TSPSEUDO);CHKERRQ(ierr); ierr = TSSetIFunction(ts,NULL,FormIFunction,&ctx);CHKERRQ(ierr); ierr = TSSetIJacobian(ts,J,J,FormIJacobian,&ctx);CHKERRQ(ierr); ierr = TSSetDuration(ts,1000,1e14);CHKERRQ(ierr); ierr = TSSetExactFinalTime(ts,TS_EXACTFINALTIME_STEPOVER);CHKERRQ(ierr); ierr = TSSetInitialTimeStep(ts,0.0,1e-3);CHKERRQ(ierr); ierr = TSMonitorSet(ts,MonitorObjective,&ctx,NULL);CHKERRQ(ierr); /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Customize time integrator; set runtime options - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ ierr = TSSetFromOptions(ts);CHKERRQ(ierr); /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Evaluate initial guess; then solve nonlinear system - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ ierr = VecSet(X,0.0);CHKERRQ(ierr); ierr = VecGetArray(X,&x);CHKERRQ(ierr); #if 1 x[0] = 5.; x[1] = -5.; for (i=2; i<ctx.n; i++) x[i] = 5.; #else x[0] = 1.0; x[1] = 15.0; for (i=2; i<ctx.n; i++) x[i] = 10.0; #endif ierr = VecRestoreArray(X,&x);CHKERRQ(ierr); ierr = TSSolve(ts,X);CHKERRQ(ierr); ierr = TSGetSolveTime(ts,&ftime);CHKERRQ(ierr); ierr = TSGetTimeStepNumber(ts,&steps);CHKERRQ(ierr); ierr = TSGetSNESIterations(ts,&nits);CHKERRQ(ierr); ierr = TSGetKSPIterations(ts,&lits);CHKERRQ(ierr); ierr = PetscPrintf(PETSC_COMM_WORLD,"Time integrator took (%D,%D,%D) iterations to reach final time %g\n",steps,nits,lits,(double)ftime);CHKERRQ(ierr); if (view_final) { ierr = VecView(X,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); } /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Free work space. All PETSc objects should be destroyed when they are no longer needed. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ ierr = VecDestroy(&X);CHKERRQ(ierr); ierr = MatDestroy(&J);CHKERRQ(ierr); ierr = TSDestroy(&ts);CHKERRQ(ierr); ierr = PetscFinalize(); return 0; }