xref: /petsc/src/dm/impls/plex/plexexodusii.c (revision ccfb0f9f40a0131988d7995ed9679700dae2a75a)
1 #include <petsc/private/dmpleximpl.h> /*I   "petscdmplex.h"   I*/
2 #include <petsc/private/viewerimpl.h>
3 
4 /*@
5   PetscViewerExodusIIGetId - Get the file id of the `PETSCVIEWEREXODUSII` file
6 
7   Logically Collective
8 
9   Input Parameter:
10 . viewer - the `PetscViewer`
11 
12   Output Parameter:
13 . exoid - The ExodusII file id
14 
15   Level: intermediate
16 
17 .seealso: `PETSCVIEWEREXODUSII`, `PetscViewer`, `PetscViewerFileSetMode()`, `PetscViewerCreate()`, `PetscViewerSetType()`, `PetscViewerBinaryOpen()`
18 @*/
19 PetscErrorCode PetscViewerExodusIIGetId(PetscViewer viewer, int *exoid)
20 {
21   PetscFunctionBegin;
22   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
23   PetscTryMethod(viewer, "PetscViewerGetId_C", (PetscViewer, int *), (viewer, exoid));
24   PetscFunctionReturn(PETSC_SUCCESS);
25 }
26 
27 /*@
28   PetscViewerExodusIISetOrder - Set the elements order in the ExodusII file.
29 
30   Collective
31 
32   Input Parameters:
33 + viewer - the `PETSCVIEWEREXODUSII` viewer
34 - order  - elements order
35 
36   Output Parameter:
37 
38   Level: beginner
39 
40 .seealso: `PETSCVIEWEREXODUSII`, `PetscViewer`, `PetscViewerExodusIIGetId()`, `PetscViewerExodusIIGetOrder()`
41 @*/
42 PetscErrorCode PetscViewerExodusIISetOrder(PetscViewer viewer, PetscInt order)
43 {
44   PetscFunctionBegin;
45   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
46   PetscTryMethod(viewer, "PetscViewerSetOrder_C", (PetscViewer, PetscInt), (viewer, order));
47   PetscFunctionReturn(PETSC_SUCCESS);
48 }
49 
50 /*@
51   PetscViewerExodusIIGetOrder - Get the elements order in the ExodusII file.
52 
53   Collective
54 
55   Input Parameters:
56 + viewer - the `PETSCVIEWEREXODUSII` viewer
57 - order  - elements order
58 
59   Output Parameter:
60 
61   Level: beginner
62 
63 .seealso: `PETSCVIEWEREXODUSII`, `PetscViewer`, `PetscViewerExodusIIGetId()`, `PetscViewerExodusIISetOrder()`
64 @*/
65 PetscErrorCode PetscViewerExodusIIGetOrder(PetscViewer viewer, PetscInt *order)
66 {
67   PetscFunctionBegin;
68   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
69   PetscTryMethod(viewer, "PetscViewerGetOrder_C", (PetscViewer, PetscInt *), (viewer, order));
70   PetscFunctionReturn(PETSC_SUCCESS);
71 }
72 
73 /*@
74   PetscViewerExodusIIOpen - Opens a file for ExodusII input/output.
75 
76   Collective
77 
78   Input Parameters:
79 + comm - MPI communicator
80 . name - name of file
81 - type - type of file
82 .vb
83     FILE_MODE_WRITE - create new file for binary output
84     FILE_MODE_READ - open existing file for binary input
85     FILE_MODE_APPEND - open existing file for binary output
86 .ve
87 
88   Output Parameter:
89 . exo - `PETSCVIEWEREXODUSII` `PetscViewer` for Exodus II input/output to use with the specified file
90 
91   Level: beginner
92 
93 .seealso: `PETSCVIEWEREXODUSII`, `PetscViewer`, `PetscViewerPushFormat()`, `PetscViewerDestroy()`,
94           `DMLoad()`, `PetscFileMode`, `PetscViewerSetType()`, `PetscViewerFileSetMode()`, `PetscViewerFileSetName()`
95 @*/
96 PetscErrorCode PetscViewerExodusIIOpen(MPI_Comm comm, const char name[], PetscFileMode type, PetscViewer *exo)
97 {
98   PetscFunctionBegin;
99   PetscCall(PetscViewerCreate(comm, exo));
100   PetscCall(PetscViewerSetType(*exo, PETSCVIEWEREXODUSII));
101   PetscCall(PetscViewerFileSetMode(*exo, type));
102   PetscCall(PetscViewerFileSetName(*exo, name));
103   PetscCall(PetscViewerSetFromOptions(*exo));
104   PetscFunctionReturn(PETSC_SUCCESS);
105 }
106 
107 /*@
108   DMPlexCreateExodusFromFile - Create a `DMPLEX` mesh from an ExodusII file.
109 
110   Collective
111 
112   Input Parameters:
113 + comm        - The MPI communicator
114 . filename    - The name of the ExodusII file
115 - interpolate - Create faces and edges in the mesh
116 
117   Output Parameter:
118 . dm - The `DM` object representing the mesh
119 
120   Level: beginner
121 
122 .seealso: [](ch_unstructured), `DM`, `PETSCVIEWEREXODUSII`, `DMPLEX`, `DMCreate()`, `DMPlexCreateExodus()`
123 @*/
124 PetscErrorCode DMPlexCreateExodusFromFile(MPI_Comm comm, const char filename[], PetscBool interpolate, DM *dm)
125 {
126   PetscFunctionBegin;
127 #if defined(PETSC_HAVE_EXODUSII)
128   PetscMPIInt      rank;
129   PetscExodusIIInt CPU_word_size = sizeof(PetscReal), IO_word_size = 0, exoid = -1;
130   float            version;
131 
132   PetscAssertPointer(filename, 2);
133   PetscCallMPI(MPI_Comm_rank(comm, &rank));
134   if (rank == 0) {
135     exoid = ex_open(filename, EX_READ, &CPU_word_size, &IO_word_size, &version);
136     PetscCheck(exoid > 0, PETSC_COMM_SELF, PETSC_ERR_LIB, "ex_open(\"%s\",...) did not return a valid file ID", filename);
137   }
138   PetscCall(DMPlexCreateExodus(comm, exoid, interpolate, dm));
139   if (rank == 0) PetscCallExternal(ex_close, exoid);
140   PetscFunctionReturn(PETSC_SUCCESS);
141 #else
142   SETERRQ(comm, PETSC_ERR_SUP, "Loading meshes requires EXODUSII support. Reconfigure using --with-exodusii-dir or -download-exodusii");
143 #endif
144 }
145