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