Actual source code: trajvisualization.c
petsc-3.9.4 2018-09-11
2: #include <petsc/private/tsimpl.h>
4: static PetscErrorCode OutputBIN(MPI_Comm comm,const char *filename,PetscViewer *viewer)
5: {
9: PetscViewerCreate(PETSC_COMM_WORLD,viewer);
10: PetscViewerSetType(*viewer,PETSCVIEWERBINARY);
11: PetscViewerFileSetMode(*viewer,FILE_MODE_WRITE);
12: PetscViewerFileSetName(*viewer,filename);
13: return(0);
14: }
16: static PetscErrorCode TSTrajectorySet_Visualization(TSTrajectory tj,TS ts,PetscInt stepnum,PetscReal time,Vec X)
17: {
18: PetscViewer viewer;
19: char filename[PETSC_MAX_PATH_LEN];
20: PetscReal tprev;
22: MPI_Comm comm;
25: PetscObjectGetComm((PetscObject)ts,&comm);
26: if (stepnum == 0) {
27: PetscMPIInt rank;
28: MPI_Comm_rank(comm,&rank);
29: if (!rank) {
30: PetscRMTree("Visualization-data");
31: PetscMkdir("Visualization-data");
32: }
33: if (tj->names) {
34: PetscViewer bnames;
35: PetscViewerBinaryOpen(comm,"Visualization-data/variablenames",FILE_MODE_WRITE,&bnames);
36: PetscViewerBinaryWriteStringArray(bnames,(const char *const *)tj->names);
37: PetscViewerDestroy(&bnames);
38: }
39: PetscSNPrintf(filename,sizeof(filename),"Visualization-data/SA-%06d.bin",stepnum);
40: OutputBIN(comm,filename,&viewer);
41: if (!tj->transform) {
42: VecView(X,viewer);
43: } else {
44: Vec XX;
45: (*tj->transform)(tj->transformctx,X,&XX);
46: VecView(XX,viewer);
47: VecDestroy(&XX);
48: }
49: PetscViewerBinaryWrite(viewer,&time,1,PETSC_REAL,PETSC_FALSE);
50: PetscViewerDestroy(&viewer);
51: return(0);
52: }
53: PetscSNPrintf(filename,sizeof(filename),"Visualization-data/SA-%06d.bin",stepnum);
54: OutputBIN(comm,filename,&viewer);
55: if (!tj->transform) {
56: VecView(X,viewer);
57: } else {
58: Vec XX;
59: (*tj->transform)(tj->transformctx,X,&XX);
60: VecView(XX,viewer);
61: VecDestroy(&XX);
62: }
63: PetscViewerBinaryWrite(viewer,&time,1,PETSC_REAL,PETSC_FALSE);
65: TSGetPrevTime(ts,&tprev);
66: PetscViewerBinaryWrite(viewer,&tprev,1,PETSC_REAL,PETSC_FALSE);
68: PetscViewerDestroy(&viewer);
69: return(0);
70: }
72: /*MC
73: TSTRAJECTORYVISUALIZATION - Stores each solution of the ODE/DAE in a file
75: Saves each timestep into a seperate file in Visualization-data/SA-%06d.bin
77: This version saves only the solutions at each timestep, it does not save the solution at each stage,
78: see TSTRAJECTORYBASIC that saves all stages
80: $PETSC_DIR/share/petsc/matlab/PetscReadBinaryTrajectory.m and $PETSC_DIR/lib/petsc/bin/PetscBinaryIOTrajectory.py
81: can read in files created with this format into MATLAB and Python.
83: Level: intermediate
85: .seealso: TSTrajectoryCreate(), TS, TSTrajectorySetType(), TSTrajectoryType, TSTrajectorySetVariableNames()
87: M*/
88: PETSC_EXTERN PetscErrorCode TSTrajectoryCreate_Visualization(TSTrajectory tj,TS ts)
89: {
91: tj->ops->set = TSTrajectorySet_Visualization;
92: return(0);
93: }