Actual source code: plexexodusii.c

  1: #include <petsc/private/dmpleximpl.h>
  2: #include <petsc/private/viewerimpl.h>

  4: /*@
  5:   PetscViewerExodusIIGetId - Get the file id of the `PETSCVIEWEREXODUSII` file

  7:   Logically Collective

  9:   Input Parameter:
 10: . viewer - the `PetscViewer`

 12:   Output Parameter:
 13: . exoid - The ExodusII file id

 15:   Level: intermediate

 17: .seealso: `PETSCVIEWEREXODUSII`, `PetscViewer`, `PetscViewerFileSetMode()`, `PetscViewerCreate()`, `PetscViewerSetType()`, `PetscViewerBinaryOpen()`
 18: @*/
 19: PetscErrorCode PetscViewerExodusIIGetId(PetscViewer viewer, PetscExodusIIInt *exoid)
 20: {
 21:   PetscFunctionBegin;
 23:   PetscUseMethod(viewer, "PetscViewerGetId_C", (PetscViewer, PetscExodusIIInt *), (viewer, exoid));
 24:   PetscFunctionReturn(PETSC_SUCCESS);
 25: }

 27: /*@
 28:   PetscViewerExodusIISetOrder - Set the elements order in the ExodusII file.

 30:   Collective

 32:   Input Parameters:
 33: + viewer - the `PETSCVIEWEREXODUSII` viewer
 34: - order  - elements order

 36:   Level: beginner

 38: .seealso: `PETSCVIEWEREXODUSII`, `PetscViewer`, `PetscViewerExodusIIGetId()`, `PetscViewerExodusIIGetOrder()`
 39: @*/
 40: PetscErrorCode PetscViewerExodusIISetOrder(PetscViewer viewer, PetscInt order)
 41: {
 42:   PetscFunctionBegin;
 44:   PetscTryMethod(viewer, "PetscViewerSetOrder_C", (PetscViewer, PetscInt), (viewer, order));
 45:   PetscFunctionReturn(PETSC_SUCCESS);
 46: }

 48: /*@
 49:   PetscViewerExodusIIGetOrder - Get the elements order in the ExodusII file.

 51:   Collective

 53:   Input Parameter:
 54: . viewer - the `PETSCVIEWEREXODUSII` viewer

 56:   Output Parameter:
 57: . order - elements order

 59:   Level: beginner

 61: .seealso: `PETSCVIEWEREXODUSII`, `PetscViewer`, `PetscViewerExodusIIGetId()`, `PetscViewerExodusIISetOrder()`
 62: @*/
 63: PetscErrorCode PetscViewerExodusIIGetOrder(PetscViewer viewer, PetscInt *order)
 64: {
 65:   PetscFunctionBegin;
 67:   PetscTryMethod(viewer, "PetscViewerGetOrder_C", (PetscViewer, PetscInt *), (viewer, order));
 68:   PetscFunctionReturn(PETSC_SUCCESS);
 69: }

 71: /*@
 72:   PetscViewerExodusIIOpen - Opens a file for ExodusII input/output.

 74:   Collective

 76:   Input Parameters:
 77: + comm - MPI communicator
 78: . name - name of file
 79: - type - type of file
 80: .vb
 81:     FILE_MODE_WRITE - create new file for binary output
 82:     FILE_MODE_READ - open existing file for binary input
 83:     FILE_MODE_APPEND - open existing file for binary output
 84: .ve

 86:   Output Parameter:
 87: . exo - `PETSCVIEWEREXODUSII` `PetscViewer` for Exodus II input/output to use with the specified file

 89:   Level: beginner

 91: .seealso: `PETSCVIEWEREXODUSII`, `PetscViewer`, `PetscViewerPushFormat()`, `PetscViewerDestroy()`,
 92:           `DMLoad()`, `PetscFileMode`, `PetscViewerSetType()`, `PetscViewerFileSetMode()`, `PetscViewerFileSetName()`
 93: @*/
 94: PetscErrorCode PetscViewerExodusIIOpen(MPI_Comm comm, const char name[], PetscFileMode type, PetscViewer *exo)
 95: {
 96:   PetscFunctionBegin;
 97:   PetscCall(PetscViewerCreate(comm, exo));
 98:   PetscCall(PetscViewerSetType(*exo, PETSCVIEWEREXODUSII));
 99:   PetscCall(PetscViewerFileSetMode(*exo, type));
100:   PetscCall(PetscViewerFileSetName(*exo, name));
101:   PetscCall(PetscViewerSetFromOptions(*exo));
102:   PetscFunctionReturn(PETSC_SUCCESS);
103: }

105: /*@
106:   DMPlexCreateExodusFromFile - Create a `DMPLEX` mesh from an ExodusII file.

108:   Collective

110:   Input Parameters:
111: + comm        - The MPI communicator
112: . filename    - The name of the ExodusII file
113: - interpolate - Create faces and edges in the mesh

115:   Output Parameter:
116: . dm - The `DM` object representing the mesh

118:   Level: beginner

120: .seealso: [](ch_unstructured), `DM`, `PETSCVIEWEREXODUSII`, `DMPLEX`, `DMCreate()`, `DMPlexCreateExodus()`
121: @*/
122: PetscErrorCode DMPlexCreateExodusFromFile(MPI_Comm comm, const char filename[], PetscBool interpolate, DM *dm)
123: {
124:   PetscFunctionBegin;
125: #if defined(PETSC_HAVE_EXODUSII)
126:   PetscMPIInt        rank;
127:   PetscExodusIIInt   CPU_word_size = sizeof(PetscReal), IO_word_size = 0, exoid = -1;
128:   PetscExodusIIFloat version;

130:   PetscAssertPointer(filename, 2);
131:   PetscCallMPI(MPI_Comm_rank(comm, &rank));
132:   if (rank == 0) {
133:     exoid = ex_open(filename, EX_READ, &CPU_word_size, &IO_word_size, &version);
134:     PetscCheck(exoid >= 0, PETSC_COMM_SELF, PETSC_ERR_LIB, "ex_open(\"%s\",...) did not return a valid file ID", filename);
135:   }
136:   PetscCall(DMPlexCreateExodus(comm, exoid, interpolate, dm));
137:   if (rank == 0) PetscCallExternal(ex_close, exoid);
138:   PetscFunctionReturn(PETSC_SUCCESS);
139: #else
140:   SETERRQ(comm, PETSC_ERR_SUP, "Loading meshes requires EXODUSII support. Reconfigure using --with-exodusii-dir or -download-exodusii");
141: #endif
142: }