xref: /petsc/src/ts/tests/ex29.c (revision 8fb5bd83c3955fefcf33a54e3bb66920a9fa884b)
1 static char help[] ="Tests TS time span \n\n";
2 
3 #include <petscts.h>
4 
5 static PetscErrorCode RHSFunction(TS ts,PetscReal t,Vec X,Vec F,void *ctx)
6 {
7   PetscInt          i,n;
8   const PetscScalar *xx;
9   /* */ PetscScalar *ff;
10 
11   PetscFunctionBegin;
12   PetscCall(VecGetLocalSize(X,&n));
13   PetscCall(VecGetArrayRead(X,&xx));
14   PetscCall(VecGetArray(F,&ff));
15   if (n >= 1) ff[0] = 1;
16   for (i = 1; i < n; i++) ff[i] = (i+1)*(xx[i-1]+PetscPowReal(t,i))/2;
17   PetscCall(VecRestoreArrayRead(X,&xx));
18   PetscCall(VecRestoreArray(F,&ff));
19   PetscFunctionReturn(0);
20 }
21 
22 int main(int argc, char *argv[])
23 {
24   TS              ts;
25   Vec             X,*Xs;
26   PetscInt        i,n,N = 9;
27   PetscReal       tspan[3] = {0, 0.5, 1.0};
28   const PetscReal *tspan2;
29 
30   PetscCall(PetscInitialize(&argc,&argv,NULL,help));
31   PetscCall(TSCreate(PETSC_COMM_SELF,&ts));
32   PetscCall(TSSetType(ts,TSRK));
33   PetscCall(TSSetRHSFunction(ts,NULL,RHSFunction,NULL));
34   PetscCall(VecCreateSeq(PETSC_COMM_SELF,N,&X));
35   PetscCall(VecZeroEntries(X));
36   PetscCall(TSSetTimeSpan(ts,3,tspan));
37   PetscCall(TSSetExactFinalTime(ts,TS_EXACTFINALTIME_MATCHSTEP));
38   PetscCall(TSSetFromOptions(ts));
39   PetscCall(TSSolve(ts,X));
40   PetscCall(TSGetTimeSpanSolutions(ts,&n,&Xs));
41   PetscCall(TSGetTimeSpan(ts,&n,&tspan2));
42   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"Time Span: "));
43   for (i=0; i<n; i++) PetscCall(PetscPrintf(PETSC_COMM_WORLD," %g",(double)tspan2[i]));
44   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"\n"));
45   PetscCall(TSDestroy(&ts));
46   PetscCall(VecDestroy(&X));
47   PetscCall(PetscFinalize());
48   return 0;
49 }
50 
51 /*TEST
52 
53 testset:
54   test:
55     suffix: 1
56     args: -ts_monitor
57   test:
58     suffix: 2
59     args: -ts_monitor -ts_time_span 0,0.3,0.6,1.0
60 TEST*/
61