xref: /petsc/src/ts/trajectory/impls/basic/trajbasic.c (revision 266563713869c22e96e8afed6a4cee0c7e22cf0f)
1 
2 #include <petsc/private/tsimpl.h>        /*I "petscts.h"  I*/
3 
4 #undef __FUNCT__
5 #define __FUNCT__ "OutputBIN"
6 static PetscErrorCode OutputBIN(const char *filename, PetscViewer *viewer)
7 {
8   PetscErrorCode ierr;
9 
10   PetscFunctionBeginUser;
11   ierr = PetscViewerCreate(PETSC_COMM_WORLD, viewer);CHKERRQ(ierr);
12   ierr = PetscViewerSetType(*viewer, PETSCVIEWERBINARY);CHKERRQ(ierr);
13   ierr = PetscViewerFileSetMode(*viewer,FILE_MODE_WRITE);CHKERRQ(ierr);
14   ierr = PetscViewerFileSetName(*viewer, filename);CHKERRQ(ierr);
15   PetscFunctionReturn(0);
16 }
17 
18 
19 #undef __FUNCT__
20 #define __FUNCT__ "TSTrajectorySet_Basic"
21 PetscErrorCode TSTrajectorySet_Basic(TSTrajectory jac,TS ts,PetscInt stepnum,PetscReal time,Vec X)
22 {
23   PetscViewer    viewer;
24   PetscInt       ns,i;
25   Vec            *Y;
26   char           filename[PETSC_MAX_PATH_LEN];
27   PetscReal      tprev;
28   PetscErrorCode ierr;
29 
30   PetscFunctionBeginUser;
31   if (stepnum == 0) {
32 #if defined(PETSC_HAVE_POPEN)
33     ierr = TSGetTotalSteps(ts,&stepnum);CHKERRQ(ierr);
34     if (stepnum == 0) {
35       PetscMPIInt rank;
36       ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)ts),&rank);CHKERRQ(ierr);
37       if (!rank) {
38         char command[PETSC_MAX_PATH_LEN];
39         FILE *fd;
40         int  err;
41 
42         ierr = PetscMemzero(command,sizeof(command));CHKERRQ(ierr);
43         ierr = PetscSNPrintf(command,PETSC_MAX_PATH_LEN,"rm -fr %s","SA-data");CHKERRQ(ierr);
44         ierr = PetscPOpen(PETSC_COMM_SELF,NULL,command,"r",&fd);CHKERRQ(ierr);
45         ierr = PetscPClose(PETSC_COMM_SELF,fd,&err);CHKERRQ(ierr);
46         ierr = PetscSNPrintf(command,PETSC_MAX_PATH_LEN,"mkdir %s","SA-data");CHKERRQ(ierr);
47         ierr = PetscPOpen(PETSC_COMM_SELF,NULL,command,"r",&fd);CHKERRQ(ierr);
48         ierr = PetscPClose(PETSC_COMM_SELF,fd,&err);CHKERRQ(ierr);
49       }
50     }
51 #endif
52     ierr = PetscSNPrintf(filename,sizeof(filename),"SA-data/SA-%06d.bin",stepnum);CHKERRQ(ierr);
53     ierr = OutputBIN(filename,&viewer);CHKERRQ(ierr);
54     ierr = VecView(X,viewer);CHKERRQ(ierr);
55     ierr = PetscViewerBinaryWrite(viewer,&time,1,PETSC_REAL,PETSC_FALSE);CHKERRQ(ierr);
56     PetscFunctionReturn(0);
57   }
58   ierr = TSGetTotalSteps(ts,&stepnum);CHKERRQ(ierr);
59   ierr = PetscSNPrintf(filename,sizeof(filename),"SA-data/SA-%06d.bin",stepnum);CHKERRQ(ierr);
60   ierr = OutputBIN(filename,&viewer);CHKERRQ(ierr);
61   ierr = VecView(X,viewer);CHKERRQ(ierr);
62   ierr = PetscViewerBinaryWrite(viewer,&time,1,PETSC_REAL,PETSC_FALSE);CHKERRQ(ierr);
63 
64   ierr = TSGetStages(ts,&ns,&Y);CHKERRQ(ierr);
65   for (i=0;i<ns;i++) {
66     ierr = VecView(Y[i],viewer);CHKERRQ(ierr);
67   }
68 
69   ierr = TSGetPrevTime(ts,&tprev);CHKERRQ(ierr);
70   ierr = PetscViewerBinaryWrite(viewer,&tprev,1,PETSC_REAL,PETSC_FALSE);CHKERRQ(ierr);
71 
72   ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
73   PetscFunctionReturn(0);
74 }
75 
76 #undef __FUNCT__
77 #define __FUNCT__ "TSTrajectoryGet_Basic"
78 PetscErrorCode TSTrajectoryGet_Basic(TSTrajectory jac,TS ts,PetscInt stepnum,PetscReal *t)
79 {
80   Vec            Sol,*Y;
81   PetscInt       Nr,i;
82   PetscViewer    viewer;
83   PetscReal      timepre;
84   char           filename[PETSC_MAX_PATH_LEN];
85   PetscErrorCode ierr;
86 
87   PetscFunctionBeginUser;
88   ierr = TSGetTotalSteps(ts,&stepnum);CHKERRQ(ierr);
89   ierr = PetscSNPrintf(filename,sizeof filename,"SA-data/SA-%06d.bin",stepnum);CHKERRQ(ierr);
90   ierr = PetscViewerBinaryOpen(PETSC_COMM_WORLD,filename,FILE_MODE_READ,&viewer);CHKERRQ(ierr);
91 
92   ierr = TSGetSolution(ts,&Sol);CHKERRQ(ierr);
93   ierr = VecLoad(Sol,viewer);CHKERRQ(ierr);
94 
95   ierr = PetscViewerBinaryRead(viewer,t,1,NULL,PETSC_REAL);CHKERRQ(ierr);
96 
97   if (stepnum != 0) {
98     ierr = TSGetStages(ts,&Nr,&Y);CHKERRQ(ierr);
99     for (i=0;i<Nr ;i++) {
100       ierr = VecLoad(Y[i],viewer);CHKERRQ(ierr);
101     }
102     ierr = PetscViewerBinaryRead(viewer,&timepre,1,NULL,PETSC_REAL);CHKERRQ(ierr);
103   }
104 
105   ierr = TSSetTimeStep(ts,-(*t)+timepre);CHKERRQ(ierr);
106   ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
107   PetscFunctionReturn(0);
108 }
109 
110 /*MC
111       TSTRAJECTORYBASIC - Stores each solution of the ODE/ADE in a file
112 
113   Level: intermediate
114 
115 .seealso:  TSTrajectoryCreate(), TS, TSTrajectorySetType()
116 
117 M*/
118 #undef __FUNCT__
119 #define __FUNCT__ "TSTrajectoryCreate_Basic"
120 PETSC_EXTERN PetscErrorCode TSTrajectoryCreate_Basic(TSTrajectory ts)
121 {
122   PetscFunctionBegin;
123   ts->ops->set  = TSTrajectorySet_Basic;
124   ts->ops->get  = TSTrajectoryGet_Basic;
125   PetscFunctionReturn(0);
126 }
127