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