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